211364c428a48657f001dbb859c1fa9a33e4b500
[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 /* Ensure only legit values are used. */
166 #define DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE(cu_index, value) \
167 do { \
168 gdb_assert ((unsigned int) (value) <= 1); \
169 GDB_INDEX_SYMBOL_STATIC_SET_VALUE((cu_index), (value)); \
170 } while (0)
171
172 /* Ensure only legit values are used. */
173 #define DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE(cu_index, value) \
174 do { \
175 gdb_assert ((value) >= GDB_INDEX_SYMBOL_KIND_TYPE \
176 && (value) <= GDB_INDEX_SYMBOL_KIND_OTHER); \
177 GDB_INDEX_SYMBOL_KIND_SET_VALUE((cu_index), (value)); \
178 } while (0)
179
180 /* Ensure we don't use more than the alloted nuber of bits for the CU. */
181 #define DW2_GDB_INDEX_CU_SET_VALUE(cu_index, value) \
182 do { \
183 gdb_assert (((value) & ~GDB_INDEX_CU_MASK) == 0); \
184 GDB_INDEX_CU_SET_VALUE((cu_index), (value)); \
185 } while (0)
186
187 #if WORDS_BIGENDIAN
188
189 /* Convert VALUE between big- and little-endian. */
190
191 static offset_type
192 byte_swap (offset_type value)
193 {
194 offset_type result;
195
196 result = (value & 0xff) << 24;
197 result |= (value & 0xff00) << 8;
198 result |= (value & 0xff0000) >> 8;
199 result |= (value & 0xff000000) >> 24;
200 return result;
201 }
202
203 #define MAYBE_SWAP(V) byte_swap (V)
204
205 #else
206 #define MAYBE_SWAP(V) static_cast<offset_type> (V)
207 #endif /* WORDS_BIGENDIAN */
208
209 /* An index into a (C++) symbol name component in a symbol name as
210 recorded in the mapped_index's symbol table. For each C++ symbol
211 in the symbol table, we record one entry for the start of each
212 component in the symbol in a table of name components, and then
213 sort the table, in order to be able to binary search symbol names,
214 ignoring leading namespaces, both completion and regular look up.
215 For example, for symbol "A::B::C", we'll have an entry that points
216 to "A::B::C", another that points to "B::C", and another for "C".
217 Note that function symbols in GDB index have no parameter
218 information, just the function/method names. You can convert a
219 name_component to a "const char *" using the
220 'mapped_index::symbol_name_at(offset_type)' method. */
221
222 struct name_component
223 {
224 /* Offset in the symbol name where the component starts. Stored as
225 a (32-bit) offset instead of a pointer to save memory and improve
226 locality on 64-bit architectures. */
227 offset_type name_offset;
228
229 /* The symbol's index in the symbol and constant pool tables of a
230 mapped_index. */
231 offset_type idx;
232 };
233
234 /* Base class containing bits shared by both .gdb_index and
235 .debug_name indexes. */
236
237 struct mapped_index_base
238 {
239 /* The name_component table (a sorted vector). See name_component's
240 description above. */
241 std::vector<name_component> name_components;
242
243 /* How NAME_COMPONENTS is sorted. */
244 enum case_sensitivity name_components_casing;
245
246 /* Return the number of names in the symbol table. */
247 virtual size_t symbol_name_count () const = 0;
248
249 /* Get the name of the symbol at IDX in the symbol table. */
250 virtual const char *symbol_name_at (offset_type idx) const = 0;
251
252 /* Return whether the name at IDX in the symbol table should be
253 ignored. */
254 virtual bool symbol_name_slot_invalid (offset_type idx) const
255 {
256 return false;
257 }
258
259 /* Build the symbol name component sorted vector, if we haven't
260 yet. */
261 void build_name_components ();
262
263 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
264 possible matches for LN_NO_PARAMS in the name component
265 vector. */
266 std::pair<std::vector<name_component>::const_iterator,
267 std::vector<name_component>::const_iterator>
268 find_name_components_bounds (const lookup_name_info &ln_no_params) const;
269
270 /* Prevent deleting/destroying via a base class pointer. */
271 protected:
272 ~mapped_index_base() = default;
273 };
274
275 /* A description of the mapped index. The file format is described in
276 a comment by the code that writes the index. */
277 struct mapped_index final : public mapped_index_base
278 {
279 /* A slot/bucket in the symbol table hash. */
280 struct symbol_table_slot
281 {
282 const offset_type name;
283 const offset_type vec;
284 };
285
286 /* Index data format version. */
287 int version;
288
289 /* The total length of the buffer. */
290 off_t total_size;
291
292 /* The address table data. */
293 gdb::array_view<const gdb_byte> address_table;
294
295 /* The symbol table, implemented as a hash table. */
296 gdb::array_view<symbol_table_slot> symbol_table;
297
298 /* A pointer to the constant pool. */
299 const char *constant_pool;
300
301 bool symbol_name_slot_invalid (offset_type idx) const override
302 {
303 const auto &bucket = this->symbol_table[idx];
304 return bucket.name == 0 && bucket.vec;
305 }
306
307 /* Convenience method to get at the name of the symbol at IDX in the
308 symbol table. */
309 const char *symbol_name_at (offset_type idx) const override
310 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
311
312 size_t symbol_name_count () const override
313 { return this->symbol_table.size (); }
314 };
315
316 /* A description of the mapped .debug_names.
317 Uninitialized map has CU_COUNT 0. */
318 struct mapped_debug_names final : public mapped_index_base
319 {
320 mapped_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile_)
321 : dwarf2_per_objfile (dwarf2_per_objfile_)
322 {}
323
324 struct dwarf2_per_objfile *dwarf2_per_objfile;
325 bfd_endian dwarf5_byte_order;
326 bool dwarf5_is_dwarf64;
327 bool augmentation_is_gdb;
328 uint8_t offset_size;
329 uint32_t cu_count = 0;
330 uint32_t tu_count, bucket_count, name_count;
331 const gdb_byte *cu_table_reordered, *tu_table_reordered;
332 const uint32_t *bucket_table_reordered, *hash_table_reordered;
333 const gdb_byte *name_table_string_offs_reordered;
334 const gdb_byte *name_table_entry_offs_reordered;
335 const gdb_byte *entry_pool;
336
337 struct index_val
338 {
339 ULONGEST dwarf_tag;
340 struct attr
341 {
342 /* Attribute name DW_IDX_*. */
343 ULONGEST dw_idx;
344
345 /* Attribute form DW_FORM_*. */
346 ULONGEST form;
347
348 /* Value if FORM is DW_FORM_implicit_const. */
349 LONGEST implicit_const;
350 };
351 std::vector<attr> attr_vec;
352 };
353
354 std::unordered_map<ULONGEST, index_val> abbrev_map;
355
356 const char *namei_to_name (uint32_t namei) const;
357
358 /* Implementation of the mapped_index_base virtual interface, for
359 the name_components cache. */
360
361 const char *symbol_name_at (offset_type idx) const override
362 { return namei_to_name (idx); }
363
364 size_t symbol_name_count () const override
365 { return this->name_count; }
366 };
367
368 typedef struct dwarf2_per_cu_data *dwarf2_per_cu_ptr;
369 DEF_VEC_P (dwarf2_per_cu_ptr);
370
371 struct tu_stats
372 {
373 int nr_uniq_abbrev_tables;
374 int nr_symtabs;
375 int nr_symtab_sharers;
376 int nr_stmt_less_type_units;
377 int nr_all_type_units_reallocs;
378 };
379
380 /* Collection of data recorded per objfile.
381 This hangs off of dwarf2_objfile_data_key. */
382
383 struct dwarf2_per_objfile : public allocate_on_obstack
384 {
385 /* Construct a dwarf2_per_objfile for OBJFILE. NAMES points to the
386 dwarf2 section names, or is NULL if the standard ELF names are
387 used. */
388 dwarf2_per_objfile (struct objfile *objfile,
389 const dwarf2_debug_sections *names);
390
391 ~dwarf2_per_objfile ();
392
393 DISABLE_COPY_AND_ASSIGN (dwarf2_per_objfile);
394
395 /* Free all cached compilation units. */
396 void free_cached_comp_units ();
397 private:
398 /* This function is mapped across the sections and remembers the
399 offset and size of each of the debugging sections we are
400 interested in. */
401 void locate_sections (bfd *abfd, asection *sectp,
402 const dwarf2_debug_sections &names);
403
404 public:
405 dwarf2_section_info info {};
406 dwarf2_section_info abbrev {};
407 dwarf2_section_info line {};
408 dwarf2_section_info loc {};
409 dwarf2_section_info loclists {};
410 dwarf2_section_info macinfo {};
411 dwarf2_section_info macro {};
412 dwarf2_section_info str {};
413 dwarf2_section_info line_str {};
414 dwarf2_section_info ranges {};
415 dwarf2_section_info rnglists {};
416 dwarf2_section_info addr {};
417 dwarf2_section_info frame {};
418 dwarf2_section_info eh_frame {};
419 dwarf2_section_info gdb_index {};
420 dwarf2_section_info debug_names {};
421 dwarf2_section_info debug_aranges {};
422
423 VEC (dwarf2_section_info_def) *types = NULL;
424
425 /* Back link. */
426 struct objfile *objfile = NULL;
427
428 /* Table of all the compilation units. This is used to locate
429 the target compilation unit of a particular reference. */
430 struct dwarf2_per_cu_data **all_comp_units = NULL;
431
432 /* The number of compilation units in ALL_COMP_UNITS. */
433 int n_comp_units = 0;
434
435 /* The number of .debug_types-related CUs. */
436 int n_type_units = 0;
437
438 /* The number of elements allocated in all_type_units.
439 If there are skeleton-less TUs, we add them to all_type_units lazily. */
440 int n_allocated_type_units = 0;
441
442 /* The .debug_types-related CUs (TUs).
443 This is stored in malloc space because we may realloc it. */
444 struct signatured_type **all_type_units = NULL;
445
446 /* Table of struct type_unit_group objects.
447 The hash key is the DW_AT_stmt_list value. */
448 htab_t type_unit_groups {};
449
450 /* A table mapping .debug_types signatures to its signatured_type entry.
451 This is NULL if the .debug_types section hasn't been read in yet. */
452 htab_t signatured_types {};
453
454 /* Type unit statistics, to see how well the scaling improvements
455 are doing. */
456 struct tu_stats tu_stats {};
457
458 /* A chain of compilation units that are currently read in, so that
459 they can be freed later. */
460 dwarf2_per_cu_data *read_in_chain = NULL;
461
462 /* A table mapping DW_AT_dwo_name values to struct dwo_file objects.
463 This is NULL if the table hasn't been allocated yet. */
464 htab_t dwo_files {};
465
466 /* True if we've checked for whether there is a DWP file. */
467 bool dwp_checked = false;
468
469 /* The DWP file if there is one, or NULL. */
470 struct dwp_file *dwp_file = NULL;
471
472 /* The shared '.dwz' file, if one exists. This is used when the
473 original data was compressed using 'dwz -m'. */
474 struct dwz_file *dwz_file = NULL;
475
476 /* A flag indicating whether this objfile has a section loaded at a
477 VMA of 0. */
478 bool has_section_at_zero = false;
479
480 /* True if we are using the mapped index,
481 or we are faking it for OBJF_READNOW's sake. */
482 bool using_index = false;
483
484 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
485 mapped_index *index_table = NULL;
486
487 /* The mapped index, or NULL if .debug_names is missing or not being used. */
488 std::unique_ptr<mapped_debug_names> debug_names_table;
489
490 /* When using index_table, this keeps track of all quick_file_names entries.
491 TUs typically share line table entries with a CU, so we maintain a
492 separate table of all line table entries to support the sharing.
493 Note that while there can be way more TUs than CUs, we've already
494 sorted all the TUs into "type unit groups", grouped by their
495 DW_AT_stmt_list value. Therefore the only sharing done here is with a
496 CU and its associated TU group if there is one. */
497 htab_t quick_file_names_table {};
498
499 /* Set during partial symbol reading, to prevent queueing of full
500 symbols. */
501 bool reading_partial_symbols = false;
502
503 /* Table mapping type DIEs to their struct type *.
504 This is NULL if not allocated yet.
505 The mapping is done via (CU/TU + DIE offset) -> type. */
506 htab_t die_type_hash {};
507
508 /* The CUs we recently read. */
509 VEC (dwarf2_per_cu_ptr) *just_read_cus = NULL;
510
511 /* Table containing line_header indexed by offset and offset_in_dwz. */
512 htab_t line_header_hash {};
513
514 /* Table containing all filenames. This is an optional because the
515 table is lazily constructed on first access. */
516 gdb::optional<filename_seen_cache> filenames_cache;
517 };
518
519 /* Get the dwarf2_per_objfile associated to OBJFILE. */
520
521 struct dwarf2_per_objfile *
522 get_dwarf2_per_objfile (struct objfile *objfile)
523 {
524 return ((struct dwarf2_per_objfile *)
525 objfile_data (objfile, dwarf2_objfile_data_key));
526 }
527
528 /* Set the dwarf2_per_objfile associated to OBJFILE. */
529
530 void
531 set_dwarf2_per_objfile (struct objfile *objfile,
532 struct dwarf2_per_objfile *dwarf2_per_objfile)
533 {
534 gdb_assert (get_dwarf2_per_objfile (objfile) == NULL);
535 set_objfile_data (objfile, dwarf2_objfile_data_key, dwarf2_per_objfile);
536 }
537
538 /* Default names of the debugging sections. */
539
540 /* Note that if the debugging section has been compressed, it might
541 have a name like .zdebug_info. */
542
543 static const struct dwarf2_debug_sections dwarf2_elf_names =
544 {
545 { ".debug_info", ".zdebug_info" },
546 { ".debug_abbrev", ".zdebug_abbrev" },
547 { ".debug_line", ".zdebug_line" },
548 { ".debug_loc", ".zdebug_loc" },
549 { ".debug_loclists", ".zdebug_loclists" },
550 { ".debug_macinfo", ".zdebug_macinfo" },
551 { ".debug_macro", ".zdebug_macro" },
552 { ".debug_str", ".zdebug_str" },
553 { ".debug_line_str", ".zdebug_line_str" },
554 { ".debug_ranges", ".zdebug_ranges" },
555 { ".debug_rnglists", ".zdebug_rnglists" },
556 { ".debug_types", ".zdebug_types" },
557 { ".debug_addr", ".zdebug_addr" },
558 { ".debug_frame", ".zdebug_frame" },
559 { ".eh_frame", NULL },
560 { ".gdb_index", ".zgdb_index" },
561 { ".debug_names", ".zdebug_names" },
562 { ".debug_aranges", ".zdebug_aranges" },
563 23
564 };
565
566 /* List of DWO/DWP sections. */
567
568 static const struct dwop_section_names
569 {
570 struct dwarf2_section_names abbrev_dwo;
571 struct dwarf2_section_names info_dwo;
572 struct dwarf2_section_names line_dwo;
573 struct dwarf2_section_names loc_dwo;
574 struct dwarf2_section_names loclists_dwo;
575 struct dwarf2_section_names macinfo_dwo;
576 struct dwarf2_section_names macro_dwo;
577 struct dwarf2_section_names str_dwo;
578 struct dwarf2_section_names str_offsets_dwo;
579 struct dwarf2_section_names types_dwo;
580 struct dwarf2_section_names cu_index;
581 struct dwarf2_section_names tu_index;
582 }
583 dwop_section_names =
584 {
585 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
586 { ".debug_info.dwo", ".zdebug_info.dwo" },
587 { ".debug_line.dwo", ".zdebug_line.dwo" },
588 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
589 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
590 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
591 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
592 { ".debug_str.dwo", ".zdebug_str.dwo" },
593 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
594 { ".debug_types.dwo", ".zdebug_types.dwo" },
595 { ".debug_cu_index", ".zdebug_cu_index" },
596 { ".debug_tu_index", ".zdebug_tu_index" },
597 };
598
599 /* local data types */
600
601 /* The data in a compilation unit header, after target2host
602 translation, looks like this. */
603 struct comp_unit_head
604 {
605 unsigned int length;
606 short version;
607 unsigned char addr_size;
608 unsigned char signed_addr_p;
609 sect_offset abbrev_sect_off;
610
611 /* Size of file offsets; either 4 or 8. */
612 unsigned int offset_size;
613
614 /* Size of the length field; either 4 or 12. */
615 unsigned int initial_length_size;
616
617 enum dwarf_unit_type unit_type;
618
619 /* Offset to the first byte of this compilation unit header in the
620 .debug_info section, for resolving relative reference dies. */
621 sect_offset sect_off;
622
623 /* Offset to first die in this cu from the start of the cu.
624 This will be the first byte following the compilation unit header. */
625 cu_offset first_die_cu_offset;
626
627 /* 64-bit signature of this type unit - it is valid only for
628 UNIT_TYPE DW_UT_type. */
629 ULONGEST signature;
630
631 /* For types, offset in the type's DIE of the type defined by this TU. */
632 cu_offset type_cu_offset_in_tu;
633 };
634
635 /* Type used for delaying computation of method physnames.
636 See comments for compute_delayed_physnames. */
637 struct delayed_method_info
638 {
639 /* The type to which the method is attached, i.e., its parent class. */
640 struct type *type;
641
642 /* The index of the method in the type's function fieldlists. */
643 int fnfield_index;
644
645 /* The index of the method in the fieldlist. */
646 int index;
647
648 /* The name of the DIE. */
649 const char *name;
650
651 /* The DIE associated with this method. */
652 struct die_info *die;
653 };
654
655 /* Internal state when decoding a particular compilation unit. */
656 struct dwarf2_cu
657 {
658 explicit dwarf2_cu (struct dwarf2_per_cu_data *per_cu);
659 ~dwarf2_cu ();
660
661 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
662
663 /* The header of the compilation unit. */
664 struct comp_unit_head header {};
665
666 /* Base address of this compilation unit. */
667 CORE_ADDR base_address = 0;
668
669 /* Non-zero if base_address has been set. */
670 int base_known = 0;
671
672 /* The language we are debugging. */
673 enum language language = language_unknown;
674 const struct language_defn *language_defn = nullptr;
675
676 const char *producer = nullptr;
677
678 /* The generic symbol table building routines have separate lists for
679 file scope symbols and all all other scopes (local scopes). So
680 we need to select the right one to pass to add_symbol_to_list().
681 We do it by keeping a pointer to the correct list in list_in_scope.
682
683 FIXME: The original dwarf code just treated the file scope as the
684 first local scope, and all other local scopes as nested local
685 scopes, and worked fine. Check to see if we really need to
686 distinguish these in buildsym.c. */
687 struct pending **list_in_scope = nullptr;
688
689 /* Hash table holding all the loaded partial DIEs
690 with partial_die->offset.SECT_OFF as hash. */
691 htab_t partial_dies = nullptr;
692
693 /* Storage for things with the same lifetime as this read-in compilation
694 unit, including partial DIEs. */
695 auto_obstack comp_unit_obstack;
696
697 /* When multiple dwarf2_cu structures are living in memory, this field
698 chains them all together, so that they can be released efficiently.
699 We will probably also want a generation counter so that most-recently-used
700 compilation units are cached... */
701 struct dwarf2_per_cu_data *read_in_chain = nullptr;
702
703 /* Backlink to our per_cu entry. */
704 struct dwarf2_per_cu_data *per_cu;
705
706 /* How many compilation units ago was this CU last referenced? */
707 int last_used = 0;
708
709 /* A hash table of DIE cu_offset for following references with
710 die_info->offset.sect_off as hash. */
711 htab_t die_hash = nullptr;
712
713 /* Full DIEs if read in. */
714 struct die_info *dies = nullptr;
715
716 /* A set of pointers to dwarf2_per_cu_data objects for compilation
717 units referenced by this one. Only set during full symbol processing;
718 partial symbol tables do not have dependencies. */
719 htab_t dependencies = nullptr;
720
721 /* Header data from the line table, during full symbol processing. */
722 struct line_header *line_header = nullptr;
723 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
724 it's owned by dwarf2_per_objfile::line_header_hash. If non-NULL,
725 this is the DW_TAG_compile_unit die for this CU. We'll hold on
726 to the line header as long as this DIE is being processed. See
727 process_die_scope. */
728 die_info *line_header_die_owner = nullptr;
729
730 /* A list of methods which need to have physnames computed
731 after all type information has been read. */
732 std::vector<delayed_method_info> method_list;
733
734 /* To be copied to symtab->call_site_htab. */
735 htab_t call_site_htab = nullptr;
736
737 /* Non-NULL if this CU came from a DWO file.
738 There is an invariant here that is important to remember:
739 Except for attributes copied from the top level DIE in the "main"
740 (or "stub") file in preparation for reading the DWO file
741 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
742 Either there isn't a DWO file (in which case this is NULL and the point
743 is moot), or there is and either we're not going to read it (in which
744 case this is NULL) or there is and we are reading it (in which case this
745 is non-NULL). */
746 struct dwo_unit *dwo_unit = nullptr;
747
748 /* The DW_AT_addr_base attribute if present, zero otherwise
749 (zero is a valid value though).
750 Note this value comes from the Fission stub CU/TU's DIE. */
751 ULONGEST addr_base = 0;
752
753 /* The DW_AT_ranges_base attribute if present, zero otherwise
754 (zero is a valid value though).
755 Note this value comes from the Fission stub CU/TU's DIE.
756 Also note that the value is zero in the non-DWO case so this value can
757 be used without needing to know whether DWO files are in use or not.
758 N.B. This does not apply to DW_AT_ranges appearing in
759 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
760 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
761 DW_AT_ranges_base *would* have to be applied, and we'd have to care
762 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
763 ULONGEST ranges_base = 0;
764
765 /* When reading debug info generated by older versions of rustc, we
766 have to rewrite some union types to be struct types with a
767 variant part. This rewriting must be done after the CU is fully
768 read in, because otherwise at the point of rewriting some struct
769 type might not have been fully processed. So, we keep a list of
770 all such types here and process them after expansion. */
771 std::vector<struct type *> rust_unions;
772
773 /* Mark used when releasing cached dies. */
774 unsigned int mark : 1;
775
776 /* This CU references .debug_loc. See the symtab->locations_valid field.
777 This test is imperfect as there may exist optimized debug code not using
778 any location list and still facing inlining issues if handled as
779 unoptimized code. For a future better test see GCC PR other/32998. */
780 unsigned int has_loclist : 1;
781
782 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is set
783 if all the producer_is_* fields are valid. This information is cached
784 because profiling CU expansion showed excessive time spent in
785 producer_is_gxx_lt_4_6. */
786 unsigned int checked_producer : 1;
787 unsigned int producer_is_gxx_lt_4_6 : 1;
788 unsigned int producer_is_gcc_lt_4_3 : 1;
789 unsigned int producer_is_icc_lt_14 : 1;
790
791 /* When set, the file that we're processing is known to have
792 debugging info for C++ namespaces. GCC 3.3.x did not produce
793 this information, but later versions do. */
794
795 unsigned int processing_has_namespace_info : 1;
796
797 struct partial_die_info *find_partial_die (sect_offset sect_off);
798 };
799
800 /* Persistent data held for a compilation unit, even when not
801 processing it. We put a pointer to this structure in the
802 read_symtab_private field of the psymtab. */
803
804 struct dwarf2_per_cu_data
805 {
806 /* The start offset and length of this compilation unit.
807 NOTE: Unlike comp_unit_head.length, this length includes
808 initial_length_size.
809 If the DIE refers to a DWO file, this is always of the original die,
810 not the DWO file. */
811 sect_offset sect_off;
812 unsigned int length;
813
814 /* DWARF standard version this data has been read from (such as 4 or 5). */
815 short dwarf_version;
816
817 /* Flag indicating this compilation unit will be read in before
818 any of the current compilation units are processed. */
819 unsigned int queued : 1;
820
821 /* This flag will be set when reading partial DIEs if we need to load
822 absolutely all DIEs for this compilation unit, instead of just the ones
823 we think are interesting. It gets set if we look for a DIE in the
824 hash table and don't find it. */
825 unsigned int load_all_dies : 1;
826
827 /* Non-zero if this CU is from .debug_types.
828 Struct dwarf2_per_cu_data is contained in struct signatured_type iff
829 this is non-zero. */
830 unsigned int is_debug_types : 1;
831
832 /* Non-zero if this CU is from the .dwz file. */
833 unsigned int is_dwz : 1;
834
835 /* Non-zero if reading a TU directly from a DWO file, bypassing the stub.
836 This flag is only valid if is_debug_types is true.
837 We can't read a CU directly from a DWO file: There are required
838 attributes in the stub. */
839 unsigned int reading_dwo_directly : 1;
840
841 /* Non-zero if the TU has been read.
842 This is used to assist the "Stay in DWO Optimization" for Fission:
843 When reading a DWO, it's faster to read TUs from the DWO instead of
844 fetching them from random other DWOs (due to comdat folding).
845 If the TU has already been read, the optimization is unnecessary
846 (and unwise - we don't want to change where gdb thinks the TU lives
847 "midflight").
848 This flag is only valid if is_debug_types is true. */
849 unsigned int tu_read : 1;
850
851 /* The section this CU/TU lives in.
852 If the DIE refers to a DWO file, this is always the original die,
853 not the DWO file. */
854 struct dwarf2_section_info *section;
855
856 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
857 of the CU cache it gets reset to NULL again. This is left as NULL for
858 dummy CUs (a CU header, but nothing else). */
859 struct dwarf2_cu *cu;
860
861 /* The corresponding dwarf2_per_objfile. */
862 struct dwarf2_per_objfile *dwarf2_per_objfile;
863
864 /* When dwarf2_per_objfile->using_index is true, the 'quick' field
865 is active. Otherwise, the 'psymtab' field is active. */
866 union
867 {
868 /* The partial symbol table associated with this compilation unit,
869 or NULL for unread partial units. */
870 struct partial_symtab *psymtab;
871
872 /* Data needed by the "quick" functions. */
873 struct dwarf2_per_cu_quick_data *quick;
874 } v;
875
876 /* The CUs we import using DW_TAG_imported_unit. This is filled in
877 while reading psymtabs, used to compute the psymtab dependencies,
878 and then cleared. Then it is filled in again while reading full
879 symbols, and only deleted when the objfile is destroyed.
880
881 This is also used to work around a difference between the way gold
882 generates .gdb_index version <=7 and the way gdb does. Arguably this
883 is a gold bug. For symbols coming from TUs, gold records in the index
884 the CU that includes the TU instead of the TU itself. This breaks
885 dw2_lookup_symbol: It assumes that if the index says symbol X lives
886 in CU/TU Y, then one need only expand Y and a subsequent lookup in Y
887 will find X. Alas TUs live in their own symtab, so after expanding CU Y
888 we need to look in TU Z to find X. Fortunately, this is akin to
889 DW_TAG_imported_unit, so we just use the same mechanism: For
890 .gdb_index version <=7 this also records the TUs that the CU referred
891 to. Concurrently with this change gdb was modified to emit version 8
892 indices so we only pay a price for gold generated indices.
893 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
894 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
895 };
896
897 /* Entry in the signatured_types hash table. */
898
899 struct signatured_type
900 {
901 /* The "per_cu" object of this type.
902 This struct is used iff per_cu.is_debug_types.
903 N.B.: This is the first member so that it's easy to convert pointers
904 between them. */
905 struct dwarf2_per_cu_data per_cu;
906
907 /* The type's signature. */
908 ULONGEST signature;
909
910 /* Offset in the TU of the type's DIE, as read from the TU header.
911 If this TU is a DWO stub and the definition lives in a DWO file
912 (specified by DW_AT_GNU_dwo_name), this value is unusable. */
913 cu_offset type_offset_in_tu;
914
915 /* Offset in the section of the type's DIE.
916 If the definition lives in a DWO file, this is the offset in the
917 .debug_types.dwo section.
918 The value is zero until the actual value is known.
919 Zero is otherwise not a valid section offset. */
920 sect_offset type_offset_in_section;
921
922 /* Type units are grouped by their DW_AT_stmt_list entry so that they
923 can share them. This points to the containing symtab. */
924 struct type_unit_group *type_unit_group;
925
926 /* The type.
927 The first time we encounter this type we fully read it in and install it
928 in the symbol tables. Subsequent times we only need the type. */
929 struct type *type;
930
931 /* Containing DWO unit.
932 This field is valid iff per_cu.reading_dwo_directly. */
933 struct dwo_unit *dwo_unit;
934 };
935
936 typedef struct signatured_type *sig_type_ptr;
937 DEF_VEC_P (sig_type_ptr);
938
939 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
940 This includes type_unit_group and quick_file_names. */
941
942 struct stmt_list_hash
943 {
944 /* The DWO unit this table is from or NULL if there is none. */
945 struct dwo_unit *dwo_unit;
946
947 /* Offset in .debug_line or .debug_line.dwo. */
948 sect_offset line_sect_off;
949 };
950
951 /* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
952 an object of this type. */
953
954 struct type_unit_group
955 {
956 /* dwarf2read.c's main "handle" on a TU symtab.
957 To simplify things we create an artificial CU that "includes" all the
958 type units using this stmt_list so that the rest of the code still has
959 a "per_cu" handle on the symtab.
960 This PER_CU is recognized by having no section. */
961 #define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
962 struct dwarf2_per_cu_data per_cu;
963
964 /* The TUs that share this DW_AT_stmt_list entry.
965 This is added to while parsing type units to build partial symtabs,
966 and is deleted afterwards and not used again. */
967 VEC (sig_type_ptr) *tus;
968
969 /* The compunit symtab.
970 Type units in a group needn't all be defined in the same source file,
971 so we create an essentially anonymous symtab as the compunit symtab. */
972 struct compunit_symtab *compunit_symtab;
973
974 /* The data used to construct the hash key. */
975 struct stmt_list_hash hash;
976
977 /* The number of symtabs from the line header.
978 The value here must match line_header.num_file_names. */
979 unsigned int num_symtabs;
980
981 /* The symbol tables for this TU (obtained from the files listed in
982 DW_AT_stmt_list).
983 WARNING: The order of entries here must match the order of entries
984 in the line header. After the first TU using this type_unit_group, the
985 line header for the subsequent TUs is recreated from this. This is done
986 because we need to use the same symtabs for each TU using the same
987 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
988 there's no guarantee the line header doesn't have duplicate entries. */
989 struct symtab **symtabs;
990 };
991
992 /* These sections are what may appear in a (real or virtual) DWO file. */
993
994 struct dwo_sections
995 {
996 struct dwarf2_section_info abbrev;
997 struct dwarf2_section_info line;
998 struct dwarf2_section_info loc;
999 struct dwarf2_section_info loclists;
1000 struct dwarf2_section_info macinfo;
1001 struct dwarf2_section_info macro;
1002 struct dwarf2_section_info str;
1003 struct dwarf2_section_info str_offsets;
1004 /* In the case of a virtual DWO file, these two are unused. */
1005 struct dwarf2_section_info info;
1006 VEC (dwarf2_section_info_def) *types;
1007 };
1008
1009 /* CUs/TUs in DWP/DWO files. */
1010
1011 struct dwo_unit
1012 {
1013 /* Backlink to the containing struct dwo_file. */
1014 struct dwo_file *dwo_file;
1015
1016 /* The "id" that distinguishes this CU/TU.
1017 .debug_info calls this "dwo_id", .debug_types calls this "signature".
1018 Since signatures came first, we stick with it for consistency. */
1019 ULONGEST signature;
1020
1021 /* The section this CU/TU lives in, in the DWO file. */
1022 struct dwarf2_section_info *section;
1023
1024 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
1025 sect_offset sect_off;
1026 unsigned int length;
1027
1028 /* For types, offset in the type's DIE of the type defined by this TU. */
1029 cu_offset type_offset_in_tu;
1030 };
1031
1032 /* include/dwarf2.h defines the DWP section codes.
1033 It defines a max value but it doesn't define a min value, which we
1034 use for error checking, so provide one. */
1035
1036 enum dwp_v2_section_ids
1037 {
1038 DW_SECT_MIN = 1
1039 };
1040
1041 /* Data for one DWO file.
1042
1043 This includes virtual DWO files (a virtual DWO file is a DWO file as it
1044 appears in a DWP file). DWP files don't really have DWO files per se -
1045 comdat folding of types "loses" the DWO file they came from, and from
1046 a high level view DWP files appear to contain a mass of random types.
1047 However, to maintain consistency with the non-DWP case we pretend DWP
1048 files contain virtual DWO files, and we assign each TU with one virtual
1049 DWO file (generally based on the line and abbrev section offsets -
1050 a heuristic that seems to work in practice). */
1051
1052 struct dwo_file
1053 {
1054 /* The DW_AT_GNU_dwo_name attribute.
1055 For virtual DWO files the name is constructed from the section offsets
1056 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
1057 from related CU+TUs. */
1058 const char *dwo_name;
1059
1060 /* The DW_AT_comp_dir attribute. */
1061 const char *comp_dir;
1062
1063 /* The bfd, when the file is open. Otherwise this is NULL.
1064 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
1065 bfd *dbfd;
1066
1067 /* The sections that make up this DWO file.
1068 Remember that for virtual DWO files in DWP V2, these are virtual
1069 sections (for lack of a better name). */
1070 struct dwo_sections sections;
1071
1072 /* The CUs in the file.
1073 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
1074 an extension to handle LLVM's Link Time Optimization output (where
1075 multiple source files may be compiled into a single object/dwo pair). */
1076 htab_t cus;
1077
1078 /* Table of TUs in the file.
1079 Each element is a struct dwo_unit. */
1080 htab_t tus;
1081 };
1082
1083 /* These sections are what may appear in a DWP file. */
1084
1085 struct dwp_sections
1086 {
1087 /* These are used by both DWP version 1 and 2. */
1088 struct dwarf2_section_info str;
1089 struct dwarf2_section_info cu_index;
1090 struct dwarf2_section_info tu_index;
1091
1092 /* These are only used by DWP version 2 files.
1093 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
1094 sections are referenced by section number, and are not recorded here.
1095 In DWP version 2 there is at most one copy of all these sections, each
1096 section being (effectively) comprised of the concatenation of all of the
1097 individual sections that exist in the version 1 format.
1098 To keep the code simple we treat each of these concatenated pieces as a
1099 section itself (a virtual section?). */
1100 struct dwarf2_section_info abbrev;
1101 struct dwarf2_section_info info;
1102 struct dwarf2_section_info line;
1103 struct dwarf2_section_info loc;
1104 struct dwarf2_section_info macinfo;
1105 struct dwarf2_section_info macro;
1106 struct dwarf2_section_info str_offsets;
1107 struct dwarf2_section_info types;
1108 };
1109
1110 /* These sections are what may appear in a virtual DWO file in DWP version 1.
1111 A virtual DWO file is a DWO file as it appears in a DWP file. */
1112
1113 struct virtual_v1_dwo_sections
1114 {
1115 struct dwarf2_section_info abbrev;
1116 struct dwarf2_section_info line;
1117 struct dwarf2_section_info loc;
1118 struct dwarf2_section_info macinfo;
1119 struct dwarf2_section_info macro;
1120 struct dwarf2_section_info str_offsets;
1121 /* Each DWP hash table entry records one CU or one TU.
1122 That is recorded here, and copied to dwo_unit.section. */
1123 struct dwarf2_section_info info_or_types;
1124 };
1125
1126 /* Similar to virtual_v1_dwo_sections, but for DWP version 2.
1127 In version 2, the sections of the DWO files are concatenated together
1128 and stored in one section of that name. Thus each ELF section contains
1129 several "virtual" sections. */
1130
1131 struct virtual_v2_dwo_sections
1132 {
1133 bfd_size_type abbrev_offset;
1134 bfd_size_type abbrev_size;
1135
1136 bfd_size_type line_offset;
1137 bfd_size_type line_size;
1138
1139 bfd_size_type loc_offset;
1140 bfd_size_type loc_size;
1141
1142 bfd_size_type macinfo_offset;
1143 bfd_size_type macinfo_size;
1144
1145 bfd_size_type macro_offset;
1146 bfd_size_type macro_size;
1147
1148 bfd_size_type str_offsets_offset;
1149 bfd_size_type str_offsets_size;
1150
1151 /* Each DWP hash table entry records one CU or one TU.
1152 That is recorded here, and copied to dwo_unit.section. */
1153 bfd_size_type info_or_types_offset;
1154 bfd_size_type info_or_types_size;
1155 };
1156
1157 /* Contents of DWP hash tables. */
1158
1159 struct dwp_hash_table
1160 {
1161 uint32_t version, nr_columns;
1162 uint32_t nr_units, nr_slots;
1163 const gdb_byte *hash_table, *unit_table;
1164 union
1165 {
1166 struct
1167 {
1168 const gdb_byte *indices;
1169 } v1;
1170 struct
1171 {
1172 /* This is indexed by column number and gives the id of the section
1173 in that column. */
1174 #define MAX_NR_V2_DWO_SECTIONS \
1175 (1 /* .debug_info or .debug_types */ \
1176 + 1 /* .debug_abbrev */ \
1177 + 1 /* .debug_line */ \
1178 + 1 /* .debug_loc */ \
1179 + 1 /* .debug_str_offsets */ \
1180 + 1 /* .debug_macro or .debug_macinfo */)
1181 int section_ids[MAX_NR_V2_DWO_SECTIONS];
1182 const gdb_byte *offsets;
1183 const gdb_byte *sizes;
1184 } v2;
1185 } section_pool;
1186 };
1187
1188 /* Data for one DWP file. */
1189
1190 struct dwp_file
1191 {
1192 /* Name of the file. */
1193 const char *name;
1194
1195 /* File format version. */
1196 int version;
1197
1198 /* The bfd. */
1199 bfd *dbfd;
1200
1201 /* Section info for this file. */
1202 struct dwp_sections sections;
1203
1204 /* Table of CUs in the file. */
1205 const struct dwp_hash_table *cus;
1206
1207 /* Table of TUs in the file. */
1208 const struct dwp_hash_table *tus;
1209
1210 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
1211 htab_t loaded_cus;
1212 htab_t loaded_tus;
1213
1214 /* Table to map ELF section numbers to their sections.
1215 This is only needed for the DWP V1 file format. */
1216 unsigned int num_sections;
1217 asection **elf_sections;
1218 };
1219
1220 /* This represents a '.dwz' file. */
1221
1222 struct dwz_file
1223 {
1224 /* A dwz file can only contain a few sections. */
1225 struct dwarf2_section_info abbrev;
1226 struct dwarf2_section_info info;
1227 struct dwarf2_section_info str;
1228 struct dwarf2_section_info line;
1229 struct dwarf2_section_info macro;
1230 struct dwarf2_section_info gdb_index;
1231 struct dwarf2_section_info debug_names;
1232
1233 /* The dwz's BFD. */
1234 bfd *dwz_bfd;
1235 };
1236
1237 /* Struct used to pass misc. parameters to read_die_and_children, et
1238 al. which are used for both .debug_info and .debug_types dies.
1239 All parameters here are unchanging for the life of the call. This
1240 struct exists to abstract away the constant parameters of die reading. */
1241
1242 struct die_reader_specs
1243 {
1244 /* The bfd of die_section. */
1245 bfd* abfd;
1246
1247 /* The CU of the DIE we are parsing. */
1248 struct dwarf2_cu *cu;
1249
1250 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
1251 struct dwo_file *dwo_file;
1252
1253 /* The section the die comes from.
1254 This is either .debug_info or .debug_types, or the .dwo variants. */
1255 struct dwarf2_section_info *die_section;
1256
1257 /* die_section->buffer. */
1258 const gdb_byte *buffer;
1259
1260 /* The end of the buffer. */
1261 const gdb_byte *buffer_end;
1262
1263 /* The value of the DW_AT_comp_dir attribute. */
1264 const char *comp_dir;
1265
1266 /* The abbreviation table to use when reading the DIEs. */
1267 struct abbrev_table *abbrev_table;
1268 };
1269
1270 /* Type of function passed to init_cutu_and_read_dies, et.al. */
1271 typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
1272 const gdb_byte *info_ptr,
1273 struct die_info *comp_unit_die,
1274 int has_children,
1275 void *data);
1276
1277 /* A 1-based directory index. This is a strong typedef to prevent
1278 accidentally using a directory index as a 0-based index into an
1279 array/vector. */
1280 enum class dir_index : unsigned int {};
1281
1282 /* Likewise, a 1-based file name index. */
1283 enum class file_name_index : unsigned int {};
1284
1285 struct file_entry
1286 {
1287 file_entry () = default;
1288
1289 file_entry (const char *name_, dir_index d_index_,
1290 unsigned int mod_time_, unsigned int length_)
1291 : name (name_),
1292 d_index (d_index_),
1293 mod_time (mod_time_),
1294 length (length_)
1295 {}
1296
1297 /* Return the include directory at D_INDEX stored in LH. Returns
1298 NULL if D_INDEX is out of bounds. */
1299 const char *include_dir (const line_header *lh) const;
1300
1301 /* The file name. Note this is an observing pointer. The memory is
1302 owned by debug_line_buffer. */
1303 const char *name {};
1304
1305 /* The directory index (1-based). */
1306 dir_index d_index {};
1307
1308 unsigned int mod_time {};
1309
1310 unsigned int length {};
1311
1312 /* True if referenced by the Line Number Program. */
1313 bool included_p {};
1314
1315 /* The associated symbol table, if any. */
1316 struct symtab *symtab {};
1317 };
1318
1319 /* The line number information for a compilation unit (found in the
1320 .debug_line section) begins with a "statement program header",
1321 which contains the following information. */
1322 struct line_header
1323 {
1324 line_header ()
1325 : offset_in_dwz {}
1326 {}
1327
1328 /* Add an entry to the include directory table. */
1329 void add_include_dir (const char *include_dir);
1330
1331 /* Add an entry to the file name table. */
1332 void add_file_name (const char *name, dir_index d_index,
1333 unsigned int mod_time, unsigned int length);
1334
1335 /* Return the include dir at INDEX (1-based). Returns NULL if INDEX
1336 is out of bounds. */
1337 const char *include_dir_at (dir_index index) const
1338 {
1339 /* Convert directory index number (1-based) to vector index
1340 (0-based). */
1341 size_t vec_index = to_underlying (index) - 1;
1342
1343 if (vec_index >= include_dirs.size ())
1344 return NULL;
1345 return include_dirs[vec_index];
1346 }
1347
1348 /* Return the file name at INDEX (1-based). Returns NULL if INDEX
1349 is out of bounds. */
1350 file_entry *file_name_at (file_name_index index)
1351 {
1352 /* Convert file name index number (1-based) to vector index
1353 (0-based). */
1354 size_t vec_index = to_underlying (index) - 1;
1355
1356 if (vec_index >= file_names.size ())
1357 return NULL;
1358 return &file_names[vec_index];
1359 }
1360
1361 /* Const version of the above. */
1362 const file_entry *file_name_at (unsigned int index) const
1363 {
1364 if (index >= file_names.size ())
1365 return NULL;
1366 return &file_names[index];
1367 }
1368
1369 /* Offset of line number information in .debug_line section. */
1370 sect_offset sect_off {};
1371
1372 /* OFFSET is for struct dwz_file associated with dwarf2_per_objfile. */
1373 unsigned offset_in_dwz : 1; /* Can't initialize bitfields in-class. */
1374
1375 unsigned int total_length {};
1376 unsigned short version {};
1377 unsigned int header_length {};
1378 unsigned char minimum_instruction_length {};
1379 unsigned char maximum_ops_per_instruction {};
1380 unsigned char default_is_stmt {};
1381 int line_base {};
1382 unsigned char line_range {};
1383 unsigned char opcode_base {};
1384
1385 /* standard_opcode_lengths[i] is the number of operands for the
1386 standard opcode whose value is i. This means that
1387 standard_opcode_lengths[0] is unused, and the last meaningful
1388 element is standard_opcode_lengths[opcode_base - 1]. */
1389 std::unique_ptr<unsigned char[]> standard_opcode_lengths;
1390
1391 /* The include_directories table. Note these are observing
1392 pointers. The memory is owned by debug_line_buffer. */
1393 std::vector<const char *> include_dirs;
1394
1395 /* The file_names table. */
1396 std::vector<file_entry> file_names;
1397
1398 /* The start and end of the statement program following this
1399 header. These point into dwarf2_per_objfile->line_buffer. */
1400 const gdb_byte *statement_program_start {}, *statement_program_end {};
1401 };
1402
1403 typedef std::unique_ptr<line_header> line_header_up;
1404
1405 const char *
1406 file_entry::include_dir (const line_header *lh) const
1407 {
1408 return lh->include_dir_at (d_index);
1409 }
1410
1411 /* When we construct a partial symbol table entry we only
1412 need this much information. */
1413 struct partial_die_info : public allocate_on_obstack
1414 {
1415 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
1416
1417 /* Disable assign but still keep copy ctor, which is needed
1418 load_partial_dies. */
1419 partial_die_info& operator=(const partial_die_info& rhs) = delete;
1420
1421 /* Adjust the partial die before generating a symbol for it. This
1422 function may set the is_external flag or change the DIE's
1423 name. */
1424 void fixup (struct dwarf2_cu *cu);
1425
1426 /* Read a minimal amount of information into the minimal die
1427 structure. */
1428 const gdb_byte *read (const struct die_reader_specs *reader,
1429 const struct abbrev_info &abbrev,
1430 const gdb_byte *info_ptr);
1431
1432 /* Offset of this DIE. */
1433 const sect_offset sect_off;
1434
1435 /* DWARF-2 tag for this DIE. */
1436 const ENUM_BITFIELD(dwarf_tag) tag : 16;
1437
1438 /* Assorted flags describing the data found in this DIE. */
1439 const unsigned int has_children : 1;
1440
1441 unsigned int is_external : 1;
1442 unsigned int is_declaration : 1;
1443 unsigned int has_type : 1;
1444 unsigned int has_specification : 1;
1445 unsigned int has_pc_info : 1;
1446 unsigned int may_be_inlined : 1;
1447
1448 /* This DIE has been marked DW_AT_main_subprogram. */
1449 unsigned int main_subprogram : 1;
1450
1451 /* Flag set if the SCOPE field of this structure has been
1452 computed. */
1453 unsigned int scope_set : 1;
1454
1455 /* Flag set if the DIE has a byte_size attribute. */
1456 unsigned int has_byte_size : 1;
1457
1458 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1459 unsigned int has_const_value : 1;
1460
1461 /* Flag set if any of the DIE's children are template arguments. */
1462 unsigned int has_template_arguments : 1;
1463
1464 /* Flag set if fixup has been called on this die. */
1465 unsigned int fixup_called : 1;
1466
1467 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1468 unsigned int is_dwz : 1;
1469
1470 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1471 unsigned int spec_is_dwz : 1;
1472
1473 /* The name of this DIE. Normally the value of DW_AT_name, but
1474 sometimes a default name for unnamed DIEs. */
1475 const char *name = nullptr;
1476
1477 /* The linkage name, if present. */
1478 const char *linkage_name = nullptr;
1479
1480 /* The scope to prepend to our children. This is generally
1481 allocated on the comp_unit_obstack, so will disappear
1482 when this compilation unit leaves the cache. */
1483 const char *scope = nullptr;
1484
1485 /* Some data associated with the partial DIE. The tag determines
1486 which field is live. */
1487 union
1488 {
1489 /* The location description associated with this DIE, if any. */
1490 struct dwarf_block *locdesc;
1491 /* The offset of an import, for DW_TAG_imported_unit. */
1492 sect_offset sect_off;
1493 } d {};
1494
1495 /* If HAS_PC_INFO, the PC range associated with this DIE. */
1496 CORE_ADDR lowpc = 0;
1497 CORE_ADDR highpc = 0;
1498
1499 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1500 DW_AT_sibling, if any. */
1501 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1502 could return DW_AT_sibling values to its caller load_partial_dies. */
1503 const gdb_byte *sibling = nullptr;
1504
1505 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1506 DW_AT_specification (or DW_AT_abstract_origin or
1507 DW_AT_extension). */
1508 sect_offset spec_offset {};
1509
1510 /* Pointers to this DIE's parent, first child, and next sibling,
1511 if any. */
1512 struct partial_die_info *die_parent = nullptr;
1513 struct partial_die_info *die_child = nullptr;
1514 struct partial_die_info *die_sibling = nullptr;
1515
1516 friend struct partial_die_info *
1517 dwarf2_cu::find_partial_die (sect_offset sect_off);
1518
1519 private:
1520 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1521 partial_die_info (sect_offset sect_off)
1522 : partial_die_info (sect_off, DW_TAG_padding, 0)
1523 {
1524 }
1525
1526 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1527 int has_children_)
1528 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1529 {
1530 is_external = 0;
1531 is_declaration = 0;
1532 has_type = 0;
1533 has_specification = 0;
1534 has_pc_info = 0;
1535 may_be_inlined = 0;
1536 main_subprogram = 0;
1537 scope_set = 0;
1538 has_byte_size = 0;
1539 has_const_value = 0;
1540 has_template_arguments = 0;
1541 fixup_called = 0;
1542 is_dwz = 0;
1543 spec_is_dwz = 0;
1544 }
1545 };
1546
1547 /* This data structure holds the information of an abbrev. */
1548 struct abbrev_info
1549 {
1550 unsigned int number; /* number identifying abbrev */
1551 enum dwarf_tag tag; /* dwarf tag */
1552 unsigned short has_children; /* boolean */
1553 unsigned short num_attrs; /* number of attributes */
1554 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1555 struct abbrev_info *next; /* next in chain */
1556 };
1557
1558 struct attr_abbrev
1559 {
1560 ENUM_BITFIELD(dwarf_attribute) name : 16;
1561 ENUM_BITFIELD(dwarf_form) form : 16;
1562
1563 /* It is valid only if FORM is DW_FORM_implicit_const. */
1564 LONGEST implicit_const;
1565 };
1566
1567 /* Size of abbrev_table.abbrev_hash_table. */
1568 #define ABBREV_HASH_SIZE 121
1569
1570 /* Top level data structure to contain an abbreviation table. */
1571
1572 struct abbrev_table
1573 {
1574 explicit abbrev_table (sect_offset off)
1575 : sect_off (off)
1576 {
1577 m_abbrevs =
1578 XOBNEWVEC (&abbrev_obstack, struct abbrev_info *, ABBREV_HASH_SIZE);
1579 memset (m_abbrevs, 0, ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
1580 }
1581
1582 DISABLE_COPY_AND_ASSIGN (abbrev_table);
1583
1584 /* Allocate space for a struct abbrev_info object in
1585 ABBREV_TABLE. */
1586 struct abbrev_info *alloc_abbrev ();
1587
1588 /* Add an abbreviation to the table. */
1589 void add_abbrev (unsigned int abbrev_number, struct abbrev_info *abbrev);
1590
1591 /* Look up an abbrev in the table.
1592 Returns NULL if the abbrev is not found. */
1593
1594 struct abbrev_info *lookup_abbrev (unsigned int abbrev_number);
1595
1596
1597 /* Where the abbrev table came from.
1598 This is used as a sanity check when the table is used. */
1599 const sect_offset sect_off;
1600
1601 /* Storage for the abbrev table. */
1602 auto_obstack abbrev_obstack;
1603
1604 private:
1605
1606 /* Hash table of abbrevs.
1607 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1608 It could be statically allocated, but the previous code didn't so we
1609 don't either. */
1610 struct abbrev_info **m_abbrevs;
1611 };
1612
1613 typedef std::unique_ptr<struct abbrev_table> abbrev_table_up;
1614
1615 /* Attributes have a name and a value. */
1616 struct attribute
1617 {
1618 ENUM_BITFIELD(dwarf_attribute) name : 16;
1619 ENUM_BITFIELD(dwarf_form) form : 15;
1620
1621 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1622 field should be in u.str (existing only for DW_STRING) but it is kept
1623 here for better struct attribute alignment. */
1624 unsigned int string_is_canonical : 1;
1625
1626 union
1627 {
1628 const char *str;
1629 struct dwarf_block *blk;
1630 ULONGEST unsnd;
1631 LONGEST snd;
1632 CORE_ADDR addr;
1633 ULONGEST signature;
1634 }
1635 u;
1636 };
1637
1638 /* This data structure holds a complete die structure. */
1639 struct die_info
1640 {
1641 /* DWARF-2 tag for this DIE. */
1642 ENUM_BITFIELD(dwarf_tag) tag : 16;
1643
1644 /* Number of attributes */
1645 unsigned char num_attrs;
1646
1647 /* True if we're presently building the full type name for the
1648 type derived from this DIE. */
1649 unsigned char building_fullname : 1;
1650
1651 /* True if this die is in process. PR 16581. */
1652 unsigned char in_process : 1;
1653
1654 /* Abbrev number */
1655 unsigned int abbrev;
1656
1657 /* Offset in .debug_info or .debug_types section. */
1658 sect_offset sect_off;
1659
1660 /* The dies in a compilation unit form an n-ary tree. PARENT
1661 points to this die's parent; CHILD points to the first child of
1662 this node; and all the children of a given node are chained
1663 together via their SIBLING fields. */
1664 struct die_info *child; /* Its first child, if any. */
1665 struct die_info *sibling; /* Its next sibling, if any. */
1666 struct die_info *parent; /* Its parent, if any. */
1667
1668 /* An array of attributes, with NUM_ATTRS elements. There may be
1669 zero, but it's not common and zero-sized arrays are not
1670 sufficiently portable C. */
1671 struct attribute attrs[1];
1672 };
1673
1674 /* Get at parts of an attribute structure. */
1675
1676 #define DW_STRING(attr) ((attr)->u.str)
1677 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
1678 #define DW_UNSND(attr) ((attr)->u.unsnd)
1679 #define DW_BLOCK(attr) ((attr)->u.blk)
1680 #define DW_SND(attr) ((attr)->u.snd)
1681 #define DW_ADDR(attr) ((attr)->u.addr)
1682 #define DW_SIGNATURE(attr) ((attr)->u.signature)
1683
1684 /* Blocks are a bunch of untyped bytes. */
1685 struct dwarf_block
1686 {
1687 size_t size;
1688
1689 /* Valid only if SIZE is not zero. */
1690 const gdb_byte *data;
1691 };
1692
1693 #ifndef ATTR_ALLOC_CHUNK
1694 #define ATTR_ALLOC_CHUNK 4
1695 #endif
1696
1697 /* Allocate fields for structs, unions and enums in this size. */
1698 #ifndef DW_FIELD_ALLOC_CHUNK
1699 #define DW_FIELD_ALLOC_CHUNK 4
1700 #endif
1701
1702 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1703 but this would require a corresponding change in unpack_field_as_long
1704 and friends. */
1705 static int bits_per_byte = 8;
1706
1707 /* When reading a variant or variant part, we track a bit more
1708 information about the field, and store it in an object of this
1709 type. */
1710
1711 struct variant_field
1712 {
1713 /* If we see a DW_TAG_variant, then this will be the discriminant
1714 value. */
1715 ULONGEST discriminant_value;
1716 /* If we see a DW_TAG_variant, then this will be set if this is the
1717 default branch. */
1718 bool default_branch;
1719 /* While reading a DW_TAG_variant_part, this will be set if this
1720 field is the discriminant. */
1721 bool is_discriminant;
1722 };
1723
1724 struct nextfield
1725 {
1726 int accessibility = 0;
1727 int virtuality = 0;
1728 /* Extra information to describe a variant or variant part. */
1729 struct variant_field variant {};
1730 struct field field {};
1731 };
1732
1733 struct fnfieldlist
1734 {
1735 const char *name = nullptr;
1736 std::vector<struct fn_field> fnfields;
1737 };
1738
1739 /* The routines that read and process dies for a C struct or C++ class
1740 pass lists of data member fields and lists of member function fields
1741 in an instance of a field_info structure, as defined below. */
1742 struct field_info
1743 {
1744 /* List of data member and baseclasses fields. */
1745 std::vector<struct nextfield> fields;
1746 std::vector<struct nextfield> baseclasses;
1747
1748 /* Number of fields (including baseclasses). */
1749 int nfields = 0;
1750
1751 /* Set if the accesibility of one of the fields is not public. */
1752 int non_public_fields = 0;
1753
1754 /* Member function fieldlist array, contains name of possibly overloaded
1755 member function, number of overloaded member functions and a pointer
1756 to the head of the member function field chain. */
1757 std::vector<struct fnfieldlist> fnfieldlists;
1758
1759 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1760 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1761 std::vector<struct decl_field> typedef_field_list;
1762
1763 /* Nested types defined by this class and the number of elements in this
1764 list. */
1765 std::vector<struct decl_field> nested_types_list;
1766 };
1767
1768 /* One item on the queue of compilation units to read in full symbols
1769 for. */
1770 struct dwarf2_queue_item
1771 {
1772 struct dwarf2_per_cu_data *per_cu;
1773 enum language pretend_language;
1774 struct dwarf2_queue_item *next;
1775 };
1776
1777 /* The current queue. */
1778 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1779
1780 /* Loaded secondary compilation units are kept in memory until they
1781 have not been referenced for the processing of this many
1782 compilation units. Set this to zero to disable caching. Cache
1783 sizes of up to at least twenty will improve startup time for
1784 typical inter-CU-reference binaries, at an obvious memory cost. */
1785 static int dwarf_max_cache_age = 5;
1786 static void
1787 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1788 struct cmd_list_element *c, const char *value)
1789 {
1790 fprintf_filtered (file, _("The upper bound on the age of cached "
1791 "DWARF compilation units is %s.\n"),
1792 value);
1793 }
1794 \f
1795 /* local function prototypes */
1796
1797 static const char *get_section_name (const struct dwarf2_section_info *);
1798
1799 static const char *get_section_file_name (const struct dwarf2_section_info *);
1800
1801 static void dwarf2_find_base_address (struct die_info *die,
1802 struct dwarf2_cu *cu);
1803
1804 static struct partial_symtab *create_partial_symtab
1805 (struct dwarf2_per_cu_data *per_cu, const char *name);
1806
1807 static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1808 const gdb_byte *info_ptr,
1809 struct die_info *type_unit_die,
1810 int has_children, void *data);
1811
1812 static void dwarf2_build_psymtabs_hard
1813 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1814
1815 static void scan_partial_symbols (struct partial_die_info *,
1816 CORE_ADDR *, CORE_ADDR *,
1817 int, struct dwarf2_cu *);
1818
1819 static void add_partial_symbol (struct partial_die_info *,
1820 struct dwarf2_cu *);
1821
1822 static void add_partial_namespace (struct partial_die_info *pdi,
1823 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1824 int set_addrmap, struct dwarf2_cu *cu);
1825
1826 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1827 CORE_ADDR *highpc, int set_addrmap,
1828 struct dwarf2_cu *cu);
1829
1830 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1831 struct dwarf2_cu *cu);
1832
1833 static void add_partial_subprogram (struct partial_die_info *pdi,
1834 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1835 int need_pc, struct dwarf2_cu *cu);
1836
1837 static void dwarf2_read_symtab (struct partial_symtab *,
1838 struct objfile *);
1839
1840 static void psymtab_to_symtab_1 (struct partial_symtab *);
1841
1842 static abbrev_table_up abbrev_table_read_table
1843 (struct dwarf2_per_objfile *dwarf2_per_objfile, struct dwarf2_section_info *,
1844 sect_offset);
1845
1846 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1847
1848 static struct partial_die_info *load_partial_dies
1849 (const struct die_reader_specs *, const gdb_byte *, int);
1850
1851 static struct partial_die_info *find_partial_die (sect_offset, int,
1852 struct dwarf2_cu *);
1853
1854 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1855 struct attribute *, struct attr_abbrev *,
1856 const gdb_byte *);
1857
1858 static unsigned int read_1_byte (bfd *, const gdb_byte *);
1859
1860 static int read_1_signed_byte (bfd *, const gdb_byte *);
1861
1862 static unsigned int read_2_bytes (bfd *, const gdb_byte *);
1863
1864 static unsigned int read_4_bytes (bfd *, const gdb_byte *);
1865
1866 static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
1867
1868 static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
1869 unsigned int *);
1870
1871 static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
1872
1873 static LONGEST read_checked_initial_length_and_offset
1874 (bfd *, const gdb_byte *, const struct comp_unit_head *,
1875 unsigned int *, unsigned int *);
1876
1877 static LONGEST read_offset (bfd *, const gdb_byte *,
1878 const struct comp_unit_head *,
1879 unsigned int *);
1880
1881 static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
1882
1883 static sect_offset read_abbrev_offset
1884 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1885 struct dwarf2_section_info *, sect_offset);
1886
1887 static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
1888
1889 static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
1890
1891 static const char *read_indirect_string
1892 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1893 const struct comp_unit_head *, unsigned int *);
1894
1895 static const char *read_indirect_line_string
1896 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1897 const struct comp_unit_head *, unsigned int *);
1898
1899 static const char *read_indirect_string_at_offset
1900 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
1901 LONGEST str_offset);
1902
1903 static const char *read_indirect_string_from_dwz
1904 (struct objfile *objfile, struct dwz_file *, LONGEST);
1905
1906 static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
1907
1908 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1909 const gdb_byte *,
1910 unsigned int *);
1911
1912 static const char *read_str_index (const struct die_reader_specs *reader,
1913 ULONGEST str_index);
1914
1915 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1916
1917 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1918 struct dwarf2_cu *);
1919
1920 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1921 unsigned int);
1922
1923 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1924 struct dwarf2_cu *cu);
1925
1926 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1927 struct dwarf2_cu *cu);
1928
1929 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1930
1931 static struct die_info *die_specification (struct die_info *die,
1932 struct dwarf2_cu **);
1933
1934 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1935 struct dwarf2_cu *cu);
1936
1937 static void dwarf_decode_lines (struct line_header *, const char *,
1938 struct dwarf2_cu *, struct partial_symtab *,
1939 CORE_ADDR, int decode_mapping);
1940
1941 static void dwarf2_start_subfile (const char *, const char *);
1942
1943 static struct compunit_symtab *dwarf2_start_symtab (struct dwarf2_cu *,
1944 const char *, const char *,
1945 CORE_ADDR);
1946
1947 static struct symbol *new_symbol (struct die_info *, struct type *,
1948 struct dwarf2_cu *, struct symbol * = NULL);
1949
1950 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1951 struct dwarf2_cu *);
1952
1953 static void dwarf2_const_value_attr (const struct attribute *attr,
1954 struct type *type,
1955 const char *name,
1956 struct obstack *obstack,
1957 struct dwarf2_cu *cu, LONGEST *value,
1958 const gdb_byte **bytes,
1959 struct dwarf2_locexpr_baton **baton);
1960
1961 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1962
1963 static int need_gnat_info (struct dwarf2_cu *);
1964
1965 static struct type *die_descriptive_type (struct die_info *,
1966 struct dwarf2_cu *);
1967
1968 static void set_descriptive_type (struct type *, struct die_info *,
1969 struct dwarf2_cu *);
1970
1971 static struct type *die_containing_type (struct die_info *,
1972 struct dwarf2_cu *);
1973
1974 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1975 struct dwarf2_cu *);
1976
1977 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1978
1979 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1980
1981 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1982
1983 static char *typename_concat (struct obstack *obs, const char *prefix,
1984 const char *suffix, int physname,
1985 struct dwarf2_cu *cu);
1986
1987 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1988
1989 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1990
1991 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1992
1993 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1994
1995 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1996
1997 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1998
1999 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
2000 struct dwarf2_cu *, struct partial_symtab *);
2001
2002 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
2003 values. Keep the items ordered with increasing constraints compliance. */
2004 enum pc_bounds_kind
2005 {
2006 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
2007 PC_BOUNDS_NOT_PRESENT,
2008
2009 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
2010 were present but they do not form a valid range of PC addresses. */
2011 PC_BOUNDS_INVALID,
2012
2013 /* Discontiguous range was found - that is DW_AT_ranges was found. */
2014 PC_BOUNDS_RANGES,
2015
2016 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
2017 PC_BOUNDS_HIGH_LOW,
2018 };
2019
2020 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
2021 CORE_ADDR *, CORE_ADDR *,
2022 struct dwarf2_cu *,
2023 struct partial_symtab *);
2024
2025 static void get_scope_pc_bounds (struct die_info *,
2026 CORE_ADDR *, CORE_ADDR *,
2027 struct dwarf2_cu *);
2028
2029 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
2030 CORE_ADDR, struct dwarf2_cu *);
2031
2032 static void dwarf2_add_field (struct field_info *, struct die_info *,
2033 struct dwarf2_cu *);
2034
2035 static void dwarf2_attach_fields_to_type (struct field_info *,
2036 struct type *, struct dwarf2_cu *);
2037
2038 static void dwarf2_add_member_fn (struct field_info *,
2039 struct die_info *, struct type *,
2040 struct dwarf2_cu *);
2041
2042 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
2043 struct type *,
2044 struct dwarf2_cu *);
2045
2046 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
2047
2048 static void read_common_block (struct die_info *, struct dwarf2_cu *);
2049
2050 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
2051
2052 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
2053
2054 static struct using_direct **using_directives (enum language);
2055
2056 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
2057
2058 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
2059
2060 static struct type *read_module_type (struct die_info *die,
2061 struct dwarf2_cu *cu);
2062
2063 static const char *namespace_name (struct die_info *die,
2064 int *is_anonymous, struct dwarf2_cu *);
2065
2066 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
2067
2068 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
2069
2070 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
2071 struct dwarf2_cu *);
2072
2073 static struct die_info *read_die_and_siblings_1
2074 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
2075 struct die_info *);
2076
2077 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
2078 const gdb_byte *info_ptr,
2079 const gdb_byte **new_info_ptr,
2080 struct die_info *parent);
2081
2082 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
2083 struct die_info **, const gdb_byte *,
2084 int *, int);
2085
2086 static const gdb_byte *read_full_die (const struct die_reader_specs *,
2087 struct die_info **, const gdb_byte *,
2088 int *);
2089
2090 static void process_die (struct die_info *, struct dwarf2_cu *);
2091
2092 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
2093 struct obstack *);
2094
2095 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
2096
2097 static const char *dwarf2_full_name (const char *name,
2098 struct die_info *die,
2099 struct dwarf2_cu *cu);
2100
2101 static const char *dwarf2_physname (const char *name, struct die_info *die,
2102 struct dwarf2_cu *cu);
2103
2104 static struct die_info *dwarf2_extension (struct die_info *die,
2105 struct dwarf2_cu **);
2106
2107 static const char *dwarf_tag_name (unsigned int);
2108
2109 static const char *dwarf_attr_name (unsigned int);
2110
2111 static const char *dwarf_form_name (unsigned int);
2112
2113 static const char *dwarf_bool_name (unsigned int);
2114
2115 static const char *dwarf_type_encoding_name (unsigned int);
2116
2117 static struct die_info *sibling_die (struct die_info *);
2118
2119 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
2120
2121 static void dump_die_for_error (struct die_info *);
2122
2123 static void dump_die_1 (struct ui_file *, int level, int max_level,
2124 struct die_info *);
2125
2126 /*static*/ void dump_die (struct die_info *, int max_level);
2127
2128 static void store_in_ref_table (struct die_info *,
2129 struct dwarf2_cu *);
2130
2131 static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
2132
2133 static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
2134
2135 static struct die_info *follow_die_ref_or_sig (struct die_info *,
2136 const struct attribute *,
2137 struct dwarf2_cu **);
2138
2139 static struct die_info *follow_die_ref (struct die_info *,
2140 const struct attribute *,
2141 struct dwarf2_cu **);
2142
2143 static struct die_info *follow_die_sig (struct die_info *,
2144 const struct attribute *,
2145 struct dwarf2_cu **);
2146
2147 static struct type *get_signatured_type (struct die_info *, ULONGEST,
2148 struct dwarf2_cu *);
2149
2150 static struct type *get_DW_AT_signature_type (struct die_info *,
2151 const struct attribute *,
2152 struct dwarf2_cu *);
2153
2154 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
2155
2156 static void read_signatured_type (struct signatured_type *);
2157
2158 static int attr_to_dynamic_prop (const struct attribute *attr,
2159 struct die_info *die, struct dwarf2_cu *cu,
2160 struct dynamic_prop *prop);
2161
2162 /* memory allocation interface */
2163
2164 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
2165
2166 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
2167
2168 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
2169
2170 static int attr_form_is_block (const struct attribute *);
2171
2172 static int attr_form_is_section_offset (const struct attribute *);
2173
2174 static int attr_form_is_constant (const struct attribute *);
2175
2176 static int attr_form_is_ref (const struct attribute *);
2177
2178 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
2179 struct dwarf2_loclist_baton *baton,
2180 const struct attribute *attr);
2181
2182 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
2183 struct symbol *sym,
2184 struct dwarf2_cu *cu,
2185 int is_block);
2186
2187 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
2188 const gdb_byte *info_ptr,
2189 struct abbrev_info *abbrev);
2190
2191 static hashval_t partial_die_hash (const void *item);
2192
2193 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
2194
2195 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
2196 (sect_offset sect_off, unsigned int offset_in_dwz,
2197 struct dwarf2_per_objfile *dwarf2_per_objfile);
2198
2199 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
2200 struct die_info *comp_unit_die,
2201 enum language pretend_language);
2202
2203 static void free_cached_comp_units (void *);
2204
2205 static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
2206
2207 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
2208
2209 static struct type *set_die_type (struct die_info *, struct type *,
2210 struct dwarf2_cu *);
2211
2212 static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
2213
2214 static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
2215
2216 static void load_full_comp_unit (struct dwarf2_per_cu_data *,
2217 enum language);
2218
2219 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
2220 enum language);
2221
2222 static void process_full_type_unit (struct dwarf2_per_cu_data *,
2223 enum language);
2224
2225 static void dwarf2_add_dependence (struct dwarf2_cu *,
2226 struct dwarf2_per_cu_data *);
2227
2228 static void dwarf2_mark (struct dwarf2_cu *);
2229
2230 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
2231
2232 static struct type *get_die_type_at_offset (sect_offset,
2233 struct dwarf2_per_cu_data *);
2234
2235 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
2236
2237 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
2238 enum language pretend_language);
2239
2240 static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
2241
2242 /* Class, the destructor of which frees all allocated queue entries. This
2243 will only have work to do if an error was thrown while processing the
2244 dwarf. If no error was thrown then the queue entries should have all
2245 been processed, and freed, as we went along. */
2246
2247 class dwarf2_queue_guard
2248 {
2249 public:
2250 dwarf2_queue_guard () = default;
2251
2252 /* Free any entries remaining on the queue. There should only be
2253 entries left if we hit an error while processing the dwarf. */
2254 ~dwarf2_queue_guard ()
2255 {
2256 struct dwarf2_queue_item *item, *last;
2257
2258 item = dwarf2_queue;
2259 while (item)
2260 {
2261 /* Anything still marked queued is likely to be in an
2262 inconsistent state, so discard it. */
2263 if (item->per_cu->queued)
2264 {
2265 if (item->per_cu->cu != NULL)
2266 free_one_cached_comp_unit (item->per_cu);
2267 item->per_cu->queued = 0;
2268 }
2269
2270 last = item;
2271 item = item->next;
2272 xfree (last);
2273 }
2274
2275 dwarf2_queue = dwarf2_queue_tail = NULL;
2276 }
2277 };
2278
2279 /* The return type of find_file_and_directory. Note, the enclosed
2280 string pointers are only valid while this object is valid. */
2281
2282 struct file_and_directory
2283 {
2284 /* The filename. This is never NULL. */
2285 const char *name;
2286
2287 /* The compilation directory. NULL if not known. If we needed to
2288 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
2289 points directly to the DW_AT_comp_dir string attribute owned by
2290 the obstack that owns the DIE. */
2291 const char *comp_dir;
2292
2293 /* If we needed to build a new string for comp_dir, this is what
2294 owns the storage. */
2295 std::string comp_dir_storage;
2296 };
2297
2298 static file_and_directory find_file_and_directory (struct die_info *die,
2299 struct dwarf2_cu *cu);
2300
2301 static char *file_full_name (int file, struct line_header *lh,
2302 const char *comp_dir);
2303
2304 /* Expected enum dwarf_unit_type for read_comp_unit_head. */
2305 enum class rcuh_kind { COMPILE, TYPE };
2306
2307 static const gdb_byte *read_and_check_comp_unit_head
2308 (struct dwarf2_per_objfile* dwarf2_per_objfile,
2309 struct comp_unit_head *header,
2310 struct dwarf2_section_info *section,
2311 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
2312 rcuh_kind section_kind);
2313
2314 static void init_cutu_and_read_dies
2315 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
2316 int use_existing_cu, int keep,
2317 die_reader_func_ftype *die_reader_func, void *data);
2318
2319 static void init_cutu_and_read_dies_simple
2320 (struct dwarf2_per_cu_data *this_cu,
2321 die_reader_func_ftype *die_reader_func, void *data);
2322
2323 static htab_t allocate_signatured_type_table (struct objfile *objfile);
2324
2325 static htab_t allocate_dwo_unit_table (struct objfile *objfile);
2326
2327 static struct dwo_unit *lookup_dwo_unit_in_dwp
2328 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2329 struct dwp_file *dwp_file, const char *comp_dir,
2330 ULONGEST signature, int is_debug_types);
2331
2332 static struct dwp_file *get_dwp_file
2333 (struct dwarf2_per_objfile *dwarf2_per_objfile);
2334
2335 static struct dwo_unit *lookup_dwo_comp_unit
2336 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
2337
2338 static struct dwo_unit *lookup_dwo_type_unit
2339 (struct signatured_type *, const char *, const char *);
2340
2341 static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
2342
2343 static void free_dwo_file_cleanup (void *);
2344
2345 struct free_dwo_file_cleanup_data
2346 {
2347 struct dwo_file *dwo_file;
2348 struct dwarf2_per_objfile *dwarf2_per_objfile;
2349 };
2350
2351 static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
2352
2353 static void check_producer (struct dwarf2_cu *cu);
2354
2355 static void free_line_header_voidp (void *arg);
2356 \f
2357 /* Various complaints about symbol reading that don't abort the process. */
2358
2359 static void
2360 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2361 {
2362 complaint (&symfile_complaints,
2363 _("statement list doesn't fit in .debug_line section"));
2364 }
2365
2366 static void
2367 dwarf2_debug_line_missing_file_complaint (void)
2368 {
2369 complaint (&symfile_complaints,
2370 _(".debug_line section has line data without a file"));
2371 }
2372
2373 static void
2374 dwarf2_debug_line_missing_end_sequence_complaint (void)
2375 {
2376 complaint (&symfile_complaints,
2377 _(".debug_line section has line "
2378 "program sequence without an end"));
2379 }
2380
2381 static void
2382 dwarf2_complex_location_expr_complaint (void)
2383 {
2384 complaint (&symfile_complaints, _("location expression too complex"));
2385 }
2386
2387 static void
2388 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
2389 int arg3)
2390 {
2391 complaint (&symfile_complaints,
2392 _("const value length mismatch for '%s', got %d, expected %d"),
2393 arg1, arg2, arg3);
2394 }
2395
2396 static void
2397 dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2398 {
2399 complaint (&symfile_complaints,
2400 _("debug info runs off end of %s section"
2401 " [in module %s]"),
2402 get_section_name (section),
2403 get_section_file_name (section));
2404 }
2405
2406 static void
2407 dwarf2_macro_malformed_definition_complaint (const char *arg1)
2408 {
2409 complaint (&symfile_complaints,
2410 _("macro debug info contains a "
2411 "malformed macro definition:\n`%s'"),
2412 arg1);
2413 }
2414
2415 static void
2416 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
2417 {
2418 complaint (&symfile_complaints,
2419 _("invalid attribute class or form for '%s' in '%s'"),
2420 arg1, arg2);
2421 }
2422
2423 /* Hash function for line_header_hash. */
2424
2425 static hashval_t
2426 line_header_hash (const struct line_header *ofs)
2427 {
2428 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
2429 }
2430
2431 /* Hash function for htab_create_alloc_ex for line_header_hash. */
2432
2433 static hashval_t
2434 line_header_hash_voidp (const void *item)
2435 {
2436 const struct line_header *ofs = (const struct line_header *) item;
2437
2438 return line_header_hash (ofs);
2439 }
2440
2441 /* Equality function for line_header_hash. */
2442
2443 static int
2444 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
2445 {
2446 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
2447 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
2448
2449 return (ofs_lhs->sect_off == ofs_rhs->sect_off
2450 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
2451 }
2452
2453 \f
2454
2455 /* Read the given attribute value as an address, taking the attribute's
2456 form into account. */
2457
2458 static CORE_ADDR
2459 attr_value_as_address (struct attribute *attr)
2460 {
2461 CORE_ADDR addr;
2462
2463 if (attr->form != DW_FORM_addr && attr->form != DW_FORM_GNU_addr_index)
2464 {
2465 /* Aside from a few clearly defined exceptions, attributes that
2466 contain an address must always be in DW_FORM_addr form.
2467 Unfortunately, some compilers happen to be violating this
2468 requirement by encoding addresses using other forms, such
2469 as DW_FORM_data4 for example. For those broken compilers,
2470 we try to do our best, without any guarantee of success,
2471 to interpret the address correctly. It would also be nice
2472 to generate a complaint, but that would require us to maintain
2473 a list of legitimate cases where a non-address form is allowed,
2474 as well as update callers to pass in at least the CU's DWARF
2475 version. This is more overhead than what we're willing to
2476 expand for a pretty rare case. */
2477 addr = DW_UNSND (attr);
2478 }
2479 else
2480 addr = DW_ADDR (attr);
2481
2482 return addr;
2483 }
2484
2485 /* The suffix for an index file. */
2486 #define INDEX4_SUFFIX ".gdb-index"
2487 #define INDEX5_SUFFIX ".debug_names"
2488 #define DEBUG_STR_SUFFIX ".debug_str"
2489
2490 /* See declaration. */
2491
2492 dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
2493 const dwarf2_debug_sections *names)
2494 : objfile (objfile_)
2495 {
2496 if (names == NULL)
2497 names = &dwarf2_elf_names;
2498
2499 bfd *obfd = objfile->obfd;
2500
2501 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
2502 locate_sections (obfd, sec, *names);
2503 }
2504
2505 static void free_dwo_files (htab_t dwo_files, struct objfile *objfile);
2506
2507 dwarf2_per_objfile::~dwarf2_per_objfile ()
2508 {
2509 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
2510 free_cached_comp_units ();
2511
2512 if (quick_file_names_table)
2513 htab_delete (quick_file_names_table);
2514
2515 if (line_header_hash)
2516 htab_delete (line_header_hash);
2517
2518 for (int ix = 0; ix < n_comp_units; ++ix)
2519 VEC_free (dwarf2_per_cu_ptr, all_comp_units[ix]->imported_symtabs);
2520
2521 for (int ix = 0; ix < n_type_units; ++ix)
2522 VEC_free (dwarf2_per_cu_ptr,
2523 all_type_units[ix]->per_cu.imported_symtabs);
2524 xfree (all_type_units);
2525
2526 VEC_free (dwarf2_section_info_def, types);
2527
2528 if (dwo_files != NULL)
2529 free_dwo_files (dwo_files, objfile);
2530 if (dwp_file != NULL)
2531 gdb_bfd_unref (dwp_file->dbfd);
2532
2533 if (dwz_file != NULL && dwz_file->dwz_bfd)
2534 gdb_bfd_unref (dwz_file->dwz_bfd);
2535
2536 if (index_table != NULL)
2537 index_table->~mapped_index ();
2538
2539 /* Everything else should be on the objfile obstack. */
2540 }
2541
2542 /* See declaration. */
2543
2544 void
2545 dwarf2_per_objfile::free_cached_comp_units ()
2546 {
2547 dwarf2_per_cu_data *per_cu = read_in_chain;
2548 dwarf2_per_cu_data **last_chain = &read_in_chain;
2549 while (per_cu != NULL)
2550 {
2551 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
2552
2553 delete per_cu->cu;
2554 *last_chain = next_cu;
2555 per_cu = next_cu;
2556 }
2557 }
2558
2559 /* Try to locate the sections we need for DWARF 2 debugging
2560 information and return true if we have enough to do something.
2561 NAMES points to the dwarf2 section names, or is NULL if the standard
2562 ELF names are used. */
2563
2564 int
2565 dwarf2_has_info (struct objfile *objfile,
2566 const struct dwarf2_debug_sections *names)
2567 {
2568 if (objfile->flags & OBJF_READNEVER)
2569 return 0;
2570
2571 struct dwarf2_per_objfile *dwarf2_per_objfile
2572 = get_dwarf2_per_objfile (objfile);
2573
2574 if (dwarf2_per_objfile == NULL)
2575 {
2576 /* Initialize per-objfile state. */
2577 dwarf2_per_objfile
2578 = new (&objfile->objfile_obstack) struct dwarf2_per_objfile (objfile,
2579 names);
2580 set_dwarf2_per_objfile (objfile, dwarf2_per_objfile);
2581 }
2582 return (!dwarf2_per_objfile->info.is_virtual
2583 && dwarf2_per_objfile->info.s.section != NULL
2584 && !dwarf2_per_objfile->abbrev.is_virtual
2585 && dwarf2_per_objfile->abbrev.s.section != NULL);
2586 }
2587
2588 /* Return the containing section of virtual section SECTION. */
2589
2590 static struct dwarf2_section_info *
2591 get_containing_section (const struct dwarf2_section_info *section)
2592 {
2593 gdb_assert (section->is_virtual);
2594 return section->s.containing_section;
2595 }
2596
2597 /* Return the bfd owner of SECTION. */
2598
2599 static struct bfd *
2600 get_section_bfd_owner (const struct dwarf2_section_info *section)
2601 {
2602 if (section->is_virtual)
2603 {
2604 section = get_containing_section (section);
2605 gdb_assert (!section->is_virtual);
2606 }
2607 return section->s.section->owner;
2608 }
2609
2610 /* Return the bfd section of SECTION.
2611 Returns NULL if the section is not present. */
2612
2613 static asection *
2614 get_section_bfd_section (const struct dwarf2_section_info *section)
2615 {
2616 if (section->is_virtual)
2617 {
2618 section = get_containing_section (section);
2619 gdb_assert (!section->is_virtual);
2620 }
2621 return section->s.section;
2622 }
2623
2624 /* Return the name of SECTION. */
2625
2626 static const char *
2627 get_section_name (const struct dwarf2_section_info *section)
2628 {
2629 asection *sectp = get_section_bfd_section (section);
2630
2631 gdb_assert (sectp != NULL);
2632 return bfd_section_name (get_section_bfd_owner (section), sectp);
2633 }
2634
2635 /* Return the name of the file SECTION is in. */
2636
2637 static const char *
2638 get_section_file_name (const struct dwarf2_section_info *section)
2639 {
2640 bfd *abfd = get_section_bfd_owner (section);
2641
2642 return bfd_get_filename (abfd);
2643 }
2644
2645 /* Return the id of SECTION.
2646 Returns 0 if SECTION doesn't exist. */
2647
2648 static int
2649 get_section_id (const struct dwarf2_section_info *section)
2650 {
2651 asection *sectp = get_section_bfd_section (section);
2652
2653 if (sectp == NULL)
2654 return 0;
2655 return sectp->id;
2656 }
2657
2658 /* Return the flags of SECTION.
2659 SECTION (or containing section if this is a virtual section) must exist. */
2660
2661 static int
2662 get_section_flags (const struct dwarf2_section_info *section)
2663 {
2664 asection *sectp = get_section_bfd_section (section);
2665
2666 gdb_assert (sectp != NULL);
2667 return bfd_get_section_flags (sectp->owner, sectp);
2668 }
2669
2670 /* When loading sections, we look either for uncompressed section or for
2671 compressed section names. */
2672
2673 static int
2674 section_is_p (const char *section_name,
2675 const struct dwarf2_section_names *names)
2676 {
2677 if (names->normal != NULL
2678 && strcmp (section_name, names->normal) == 0)
2679 return 1;
2680 if (names->compressed != NULL
2681 && strcmp (section_name, names->compressed) == 0)
2682 return 1;
2683 return 0;
2684 }
2685
2686 /* See declaration. */
2687
2688 void
2689 dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
2690 const dwarf2_debug_sections &names)
2691 {
2692 flagword aflag = bfd_get_section_flags (abfd, sectp);
2693
2694 if ((aflag & SEC_HAS_CONTENTS) == 0)
2695 {
2696 }
2697 else if (section_is_p (sectp->name, &names.info))
2698 {
2699 this->info.s.section = sectp;
2700 this->info.size = bfd_get_section_size (sectp);
2701 }
2702 else if (section_is_p (sectp->name, &names.abbrev))
2703 {
2704 this->abbrev.s.section = sectp;
2705 this->abbrev.size = bfd_get_section_size (sectp);
2706 }
2707 else if (section_is_p (sectp->name, &names.line))
2708 {
2709 this->line.s.section = sectp;
2710 this->line.size = bfd_get_section_size (sectp);
2711 }
2712 else if (section_is_p (sectp->name, &names.loc))
2713 {
2714 this->loc.s.section = sectp;
2715 this->loc.size = bfd_get_section_size (sectp);
2716 }
2717 else if (section_is_p (sectp->name, &names.loclists))
2718 {
2719 this->loclists.s.section = sectp;
2720 this->loclists.size = bfd_get_section_size (sectp);
2721 }
2722 else if (section_is_p (sectp->name, &names.macinfo))
2723 {
2724 this->macinfo.s.section = sectp;
2725 this->macinfo.size = bfd_get_section_size (sectp);
2726 }
2727 else if (section_is_p (sectp->name, &names.macro))
2728 {
2729 this->macro.s.section = sectp;
2730 this->macro.size = bfd_get_section_size (sectp);
2731 }
2732 else if (section_is_p (sectp->name, &names.str))
2733 {
2734 this->str.s.section = sectp;
2735 this->str.size = bfd_get_section_size (sectp);
2736 }
2737 else if (section_is_p (sectp->name, &names.line_str))
2738 {
2739 this->line_str.s.section = sectp;
2740 this->line_str.size = bfd_get_section_size (sectp);
2741 }
2742 else if (section_is_p (sectp->name, &names.addr))
2743 {
2744 this->addr.s.section = sectp;
2745 this->addr.size = bfd_get_section_size (sectp);
2746 }
2747 else if (section_is_p (sectp->name, &names.frame))
2748 {
2749 this->frame.s.section = sectp;
2750 this->frame.size = bfd_get_section_size (sectp);
2751 }
2752 else if (section_is_p (sectp->name, &names.eh_frame))
2753 {
2754 this->eh_frame.s.section = sectp;
2755 this->eh_frame.size = bfd_get_section_size (sectp);
2756 }
2757 else if (section_is_p (sectp->name, &names.ranges))
2758 {
2759 this->ranges.s.section = sectp;
2760 this->ranges.size = bfd_get_section_size (sectp);
2761 }
2762 else if (section_is_p (sectp->name, &names.rnglists))
2763 {
2764 this->rnglists.s.section = sectp;
2765 this->rnglists.size = bfd_get_section_size (sectp);
2766 }
2767 else if (section_is_p (sectp->name, &names.types))
2768 {
2769 struct dwarf2_section_info type_section;
2770
2771 memset (&type_section, 0, sizeof (type_section));
2772 type_section.s.section = sectp;
2773 type_section.size = bfd_get_section_size (sectp);
2774
2775 VEC_safe_push (dwarf2_section_info_def, this->types,
2776 &type_section);
2777 }
2778 else if (section_is_p (sectp->name, &names.gdb_index))
2779 {
2780 this->gdb_index.s.section = sectp;
2781 this->gdb_index.size = bfd_get_section_size (sectp);
2782 }
2783 else if (section_is_p (sectp->name, &names.debug_names))
2784 {
2785 this->debug_names.s.section = sectp;
2786 this->debug_names.size = bfd_get_section_size (sectp);
2787 }
2788 else if (section_is_p (sectp->name, &names.debug_aranges))
2789 {
2790 this->debug_aranges.s.section = sectp;
2791 this->debug_aranges.size = bfd_get_section_size (sectp);
2792 }
2793
2794 if ((bfd_get_section_flags (abfd, sectp) & (SEC_LOAD | SEC_ALLOC))
2795 && bfd_section_vma (abfd, sectp) == 0)
2796 this->has_section_at_zero = true;
2797 }
2798
2799 /* A helper function that decides whether a section is empty,
2800 or not present. */
2801
2802 static int
2803 dwarf2_section_empty_p (const struct dwarf2_section_info *section)
2804 {
2805 if (section->is_virtual)
2806 return section->size == 0;
2807 return section->s.section == NULL || section->size == 0;
2808 }
2809
2810 /* Read the contents of the section INFO.
2811 OBJFILE is the main object file, but not necessarily the file where
2812 the section comes from. E.g., for DWO files the bfd of INFO is the bfd
2813 of the DWO file.
2814 If the section is compressed, uncompress it before returning. */
2815
2816 static void
2817 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
2818 {
2819 asection *sectp;
2820 bfd *abfd;
2821 gdb_byte *buf, *retbuf;
2822
2823 if (info->readin)
2824 return;
2825 info->buffer = NULL;
2826 info->readin = 1;
2827
2828 if (dwarf2_section_empty_p (info))
2829 return;
2830
2831 sectp = get_section_bfd_section (info);
2832
2833 /* If this is a virtual section we need to read in the real one first. */
2834 if (info->is_virtual)
2835 {
2836 struct dwarf2_section_info *containing_section =
2837 get_containing_section (info);
2838
2839 gdb_assert (sectp != NULL);
2840 if ((sectp->flags & SEC_RELOC) != 0)
2841 {
2842 error (_("Dwarf Error: DWP format V2 with relocations is not"
2843 " supported in section %s [in module %s]"),
2844 get_section_name (info), get_section_file_name (info));
2845 }
2846 dwarf2_read_section (objfile, containing_section);
2847 /* Other code should have already caught virtual sections that don't
2848 fit. */
2849 gdb_assert (info->virtual_offset + info->size
2850 <= containing_section->size);
2851 /* If the real section is empty or there was a problem reading the
2852 section we shouldn't get here. */
2853 gdb_assert (containing_section->buffer != NULL);
2854 info->buffer = containing_section->buffer + info->virtual_offset;
2855 return;
2856 }
2857
2858 /* If the section has relocations, we must read it ourselves.
2859 Otherwise we attach it to the BFD. */
2860 if ((sectp->flags & SEC_RELOC) == 0)
2861 {
2862 info->buffer = gdb_bfd_map_section (sectp, &info->size);
2863 return;
2864 }
2865
2866 buf = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, info->size);
2867 info->buffer = buf;
2868
2869 /* When debugging .o files, we may need to apply relocations; see
2870 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
2871 We never compress sections in .o files, so we only need to
2872 try this when the section is not compressed. */
2873 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
2874 if (retbuf != NULL)
2875 {
2876 info->buffer = retbuf;
2877 return;
2878 }
2879
2880 abfd = get_section_bfd_owner (info);
2881 gdb_assert (abfd != NULL);
2882
2883 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2884 || bfd_bread (buf, info->size, abfd) != info->size)
2885 {
2886 error (_("Dwarf Error: Can't read DWARF data"
2887 " in section %s [in module %s]"),
2888 bfd_section_name (abfd, sectp), bfd_get_filename (abfd));
2889 }
2890 }
2891
2892 /* A helper function that returns the size of a section in a safe way.
2893 If you are positive that the section has been read before using the
2894 size, then it is safe to refer to the dwarf2_section_info object's
2895 "size" field directly. In other cases, you must call this
2896 function, because for compressed sections the size field is not set
2897 correctly until the section has been read. */
2898
2899 static bfd_size_type
2900 dwarf2_section_size (struct objfile *objfile,
2901 struct dwarf2_section_info *info)
2902 {
2903 if (!info->readin)
2904 dwarf2_read_section (objfile, info);
2905 return info->size;
2906 }
2907
2908 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2909 SECTION_NAME. */
2910
2911 void
2912 dwarf2_get_section_info (struct objfile *objfile,
2913 enum dwarf2_section_enum sect,
2914 asection **sectp, const gdb_byte **bufp,
2915 bfd_size_type *sizep)
2916 {
2917 struct dwarf2_per_objfile *data
2918 = (struct dwarf2_per_objfile *) objfile_data (objfile,
2919 dwarf2_objfile_data_key);
2920 struct dwarf2_section_info *info;
2921
2922 /* We may see an objfile without any DWARF, in which case we just
2923 return nothing. */
2924 if (data == NULL)
2925 {
2926 *sectp = NULL;
2927 *bufp = NULL;
2928 *sizep = 0;
2929 return;
2930 }
2931 switch (sect)
2932 {
2933 case DWARF2_DEBUG_FRAME:
2934 info = &data->frame;
2935 break;
2936 case DWARF2_EH_FRAME:
2937 info = &data->eh_frame;
2938 break;
2939 default:
2940 gdb_assert_not_reached ("unexpected section");
2941 }
2942
2943 dwarf2_read_section (objfile, info);
2944
2945 *sectp = get_section_bfd_section (info);
2946 *bufp = info->buffer;
2947 *sizep = info->size;
2948 }
2949
2950 /* A helper function to find the sections for a .dwz file. */
2951
2952 static void
2953 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2954 {
2955 struct dwz_file *dwz_file = (struct dwz_file *) arg;
2956
2957 /* Note that we only support the standard ELF names, because .dwz
2958 is ELF-only (at the time of writing). */
2959 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2960 {
2961 dwz_file->abbrev.s.section = sectp;
2962 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2963 }
2964 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2965 {
2966 dwz_file->info.s.section = sectp;
2967 dwz_file->info.size = bfd_get_section_size (sectp);
2968 }
2969 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2970 {
2971 dwz_file->str.s.section = sectp;
2972 dwz_file->str.size = bfd_get_section_size (sectp);
2973 }
2974 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2975 {
2976 dwz_file->line.s.section = sectp;
2977 dwz_file->line.size = bfd_get_section_size (sectp);
2978 }
2979 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2980 {
2981 dwz_file->macro.s.section = sectp;
2982 dwz_file->macro.size = bfd_get_section_size (sectp);
2983 }
2984 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2985 {
2986 dwz_file->gdb_index.s.section = sectp;
2987 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2988 }
2989 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2990 {
2991 dwz_file->debug_names.s.section = sectp;
2992 dwz_file->debug_names.size = bfd_get_section_size (sectp);
2993 }
2994 }
2995
2996 /* Open the separate '.dwz' debug file, if needed. Return NULL if
2997 there is no .gnu_debugaltlink section in the file. Error if there
2998 is such a section but the file cannot be found. */
2999
3000 static struct dwz_file *
3001 dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
3002 {
3003 const char *filename;
3004 struct dwz_file *result;
3005 bfd_size_type buildid_len_arg;
3006 size_t buildid_len;
3007 bfd_byte *buildid;
3008
3009 if (dwarf2_per_objfile->dwz_file != NULL)
3010 return dwarf2_per_objfile->dwz_file;
3011
3012 bfd_set_error (bfd_error_no_error);
3013 gdb::unique_xmalloc_ptr<char> data
3014 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
3015 &buildid_len_arg, &buildid));
3016 if (data == NULL)
3017 {
3018 if (bfd_get_error () == bfd_error_no_error)
3019 return NULL;
3020 error (_("could not read '.gnu_debugaltlink' section: %s"),
3021 bfd_errmsg (bfd_get_error ()));
3022 }
3023
3024 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
3025
3026 buildid_len = (size_t) buildid_len_arg;
3027
3028 filename = data.get ();
3029
3030 std::string abs_storage;
3031 if (!IS_ABSOLUTE_PATH (filename))
3032 {
3033 gdb::unique_xmalloc_ptr<char> abs
3034 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
3035
3036 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
3037 filename = abs_storage.c_str ();
3038 }
3039
3040 /* First try the file name given in the section. If that doesn't
3041 work, try to use the build-id instead. */
3042 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
3043 if (dwz_bfd != NULL)
3044 {
3045 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
3046 dwz_bfd.release ();
3047 }
3048
3049 if (dwz_bfd == NULL)
3050 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
3051
3052 if (dwz_bfd == NULL)
3053 error (_("could not find '.gnu_debugaltlink' file for %s"),
3054 objfile_name (dwarf2_per_objfile->objfile));
3055
3056 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
3057 struct dwz_file);
3058 result->dwz_bfd = dwz_bfd.release ();
3059
3060 bfd_map_over_sections (result->dwz_bfd, locate_dwz_sections, result);
3061
3062 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, result->dwz_bfd);
3063 dwarf2_per_objfile->dwz_file = result;
3064 return result;
3065 }
3066 \f
3067 /* DWARF quick_symbols_functions support. */
3068
3069 /* TUs can share .debug_line entries, and there can be a lot more TUs than
3070 unique line tables, so we maintain a separate table of all .debug_line
3071 derived entries to support the sharing.
3072 All the quick functions need is the list of file names. We discard the
3073 line_header when we're done and don't need to record it here. */
3074 struct quick_file_names
3075 {
3076 /* The data used to construct the hash key. */
3077 struct stmt_list_hash hash;
3078
3079 /* The number of entries in file_names, real_names. */
3080 unsigned int num_file_names;
3081
3082 /* The file names from the line table, after being run through
3083 file_full_name. */
3084 const char **file_names;
3085
3086 /* The file names from the line table after being run through
3087 gdb_realpath. These are computed lazily. */
3088 const char **real_names;
3089 };
3090
3091 /* When using the index (and thus not using psymtabs), each CU has an
3092 object of this type. This is used to hold information needed by
3093 the various "quick" methods. */
3094 struct dwarf2_per_cu_quick_data
3095 {
3096 /* The file table. This can be NULL if there was no file table
3097 or it's currently not read in.
3098 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
3099 struct quick_file_names *file_names;
3100
3101 /* The corresponding symbol table. This is NULL if symbols for this
3102 CU have not yet been read. */
3103 struct compunit_symtab *compunit_symtab;
3104
3105 /* A temporary mark bit used when iterating over all CUs in
3106 expand_symtabs_matching. */
3107 unsigned int mark : 1;
3108
3109 /* True if we've tried to read the file table and found there isn't one.
3110 There will be no point in trying to read it again next time. */
3111 unsigned int no_file_data : 1;
3112 };
3113
3114 /* Utility hash function for a stmt_list_hash. */
3115
3116 static hashval_t
3117 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
3118 {
3119 hashval_t v = 0;
3120
3121 if (stmt_list_hash->dwo_unit != NULL)
3122 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
3123 v += to_underlying (stmt_list_hash->line_sect_off);
3124 return v;
3125 }
3126
3127 /* Utility equality function for a stmt_list_hash. */
3128
3129 static int
3130 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
3131 const struct stmt_list_hash *rhs)
3132 {
3133 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
3134 return 0;
3135 if (lhs->dwo_unit != NULL
3136 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
3137 return 0;
3138
3139 return lhs->line_sect_off == rhs->line_sect_off;
3140 }
3141
3142 /* Hash function for a quick_file_names. */
3143
3144 static hashval_t
3145 hash_file_name_entry (const void *e)
3146 {
3147 const struct quick_file_names *file_data
3148 = (const struct quick_file_names *) e;
3149
3150 return hash_stmt_list_entry (&file_data->hash);
3151 }
3152
3153 /* Equality function for a quick_file_names. */
3154
3155 static int
3156 eq_file_name_entry (const void *a, const void *b)
3157 {
3158 const struct quick_file_names *ea = (const struct quick_file_names *) a;
3159 const struct quick_file_names *eb = (const struct quick_file_names *) b;
3160
3161 return eq_stmt_list_entry (&ea->hash, &eb->hash);
3162 }
3163
3164 /* Delete function for a quick_file_names. */
3165
3166 static void
3167 delete_file_name_entry (void *e)
3168 {
3169 struct quick_file_names *file_data = (struct quick_file_names *) e;
3170 int i;
3171
3172 for (i = 0; i < file_data->num_file_names; ++i)
3173 {
3174 xfree ((void*) file_data->file_names[i]);
3175 if (file_data->real_names)
3176 xfree ((void*) file_data->real_names[i]);
3177 }
3178
3179 /* The space for the struct itself lives on objfile_obstack,
3180 so we don't free it here. */
3181 }
3182
3183 /* Create a quick_file_names hash table. */
3184
3185 static htab_t
3186 create_quick_file_names_table (unsigned int nr_initial_entries)
3187 {
3188 return htab_create_alloc (nr_initial_entries,
3189 hash_file_name_entry, eq_file_name_entry,
3190 delete_file_name_entry, xcalloc, xfree);
3191 }
3192
3193 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
3194 have to be created afterwards. You should call age_cached_comp_units after
3195 processing PER_CU->CU. dw2_setup must have been already called. */
3196
3197 static void
3198 load_cu (struct dwarf2_per_cu_data *per_cu)
3199 {
3200 if (per_cu->is_debug_types)
3201 load_full_type_unit (per_cu);
3202 else
3203 load_full_comp_unit (per_cu, language_minimal);
3204
3205 if (per_cu->cu == NULL)
3206 return; /* Dummy CU. */
3207
3208 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
3209 }
3210
3211 /* Read in the symbols for PER_CU. */
3212
3213 static void
3214 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
3215 {
3216 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3217
3218 /* Skip type_unit_groups, reading the type units they contain
3219 is handled elsewhere. */
3220 if (IS_TYPE_UNIT_GROUP (per_cu))
3221 return;
3222
3223 /* The destructor of dwarf2_queue_guard frees any entries left on
3224 the queue. After this point we're guaranteed to leave this function
3225 with the dwarf queue empty. */
3226 dwarf2_queue_guard q_guard;
3227
3228 if (dwarf2_per_objfile->using_index
3229 ? per_cu->v.quick->compunit_symtab == NULL
3230 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
3231 {
3232 queue_comp_unit (per_cu, language_minimal);
3233 load_cu (per_cu);
3234
3235 /* If we just loaded a CU from a DWO, and we're working with an index
3236 that may badly handle TUs, load all the TUs in that DWO as well.
3237 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
3238 if (!per_cu->is_debug_types
3239 && per_cu->cu != NULL
3240 && per_cu->cu->dwo_unit != NULL
3241 && dwarf2_per_objfile->index_table != NULL
3242 && dwarf2_per_objfile->index_table->version <= 7
3243 /* DWP files aren't supported yet. */
3244 && get_dwp_file (dwarf2_per_objfile) == NULL)
3245 queue_and_load_all_dwo_tus (per_cu);
3246 }
3247
3248 process_queue (dwarf2_per_objfile);
3249
3250 /* Age the cache, releasing compilation units that have not
3251 been used recently. */
3252 age_cached_comp_units (dwarf2_per_objfile);
3253 }
3254
3255 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
3256 the objfile from which this CU came. Returns the resulting symbol
3257 table. */
3258
3259 static struct compunit_symtab *
3260 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
3261 {
3262 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3263
3264 gdb_assert (dwarf2_per_objfile->using_index);
3265 if (!per_cu->v.quick->compunit_symtab)
3266 {
3267 struct cleanup *back_to = make_cleanup (free_cached_comp_units,
3268 dwarf2_per_objfile);
3269 scoped_restore decrementer = increment_reading_symtab ();
3270 dw2_do_instantiate_symtab (per_cu);
3271 process_cu_includes (dwarf2_per_objfile);
3272 do_cleanups (back_to);
3273 }
3274
3275 return per_cu->v.quick->compunit_symtab;
3276 }
3277
3278 /* Return the CU/TU given its index.
3279
3280 This is intended for loops like:
3281
3282 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3283 + dwarf2_per_objfile->n_type_units); ++i)
3284 {
3285 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
3286
3287 ...;
3288 }
3289 */
3290
3291 static struct dwarf2_per_cu_data *
3292 dw2_get_cutu (struct dwarf2_per_objfile *dwarf2_per_objfile,
3293 int index)
3294 {
3295 if (index >= dwarf2_per_objfile->n_comp_units)
3296 {
3297 index -= dwarf2_per_objfile->n_comp_units;
3298 gdb_assert (index < dwarf2_per_objfile->n_type_units);
3299 return &dwarf2_per_objfile->all_type_units[index]->per_cu;
3300 }
3301
3302 return dwarf2_per_objfile->all_comp_units[index];
3303 }
3304
3305 /* Return the CU given its index.
3306 This differs from dw2_get_cutu in that it's for when you know INDEX
3307 refers to a CU. */
3308
3309 static struct dwarf2_per_cu_data *
3310 dw2_get_cu (struct dwarf2_per_objfile *dwarf2_per_objfile, int index)
3311 {
3312 gdb_assert (index >= 0 && index < dwarf2_per_objfile->n_comp_units);
3313
3314 return dwarf2_per_objfile->all_comp_units[index];
3315 }
3316
3317 /* Return a new dwarf2_per_cu_data allocated on OBJFILE's
3318 objfile_obstack, and constructed with the specified field
3319 values. */
3320
3321 static dwarf2_per_cu_data *
3322 create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
3323 struct dwarf2_section_info *section,
3324 int is_dwz,
3325 sect_offset sect_off, ULONGEST length)
3326 {
3327 struct objfile *objfile = dwarf2_per_objfile->objfile;
3328 dwarf2_per_cu_data *the_cu
3329 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3330 struct dwarf2_per_cu_data);
3331 the_cu->sect_off = sect_off;
3332 the_cu->length = length;
3333 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
3334 the_cu->section = section;
3335 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3336 struct dwarf2_per_cu_quick_data);
3337 the_cu->is_dwz = is_dwz;
3338 return the_cu;
3339 }
3340
3341 /* A helper for create_cus_from_index that handles a given list of
3342 CUs. */
3343
3344 static void
3345 create_cus_from_index_list (struct objfile *objfile,
3346 const gdb_byte *cu_list, offset_type n_elements,
3347 struct dwarf2_section_info *section,
3348 int is_dwz,
3349 int base_offset)
3350 {
3351 offset_type i;
3352 struct dwarf2_per_objfile *dwarf2_per_objfile
3353 = get_dwarf2_per_objfile (objfile);
3354
3355 for (i = 0; i < n_elements; i += 2)
3356 {
3357 gdb_static_assert (sizeof (ULONGEST) >= 8);
3358
3359 sect_offset sect_off
3360 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
3361 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
3362 cu_list += 2 * 8;
3363
3364 dwarf2_per_objfile->all_comp_units[base_offset + i / 2]
3365 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
3366 sect_off, length);
3367 }
3368 }
3369
3370 /* Read the CU list from the mapped index, and use it to create all
3371 the CU objects for this objfile. */
3372
3373 static void
3374 create_cus_from_index (struct objfile *objfile,
3375 const gdb_byte *cu_list, offset_type cu_list_elements,
3376 const gdb_byte *dwz_list, offset_type dwz_elements)
3377 {
3378 struct dwz_file *dwz;
3379 struct dwarf2_per_objfile *dwarf2_per_objfile
3380 = get_dwarf2_per_objfile (objfile);
3381
3382 dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
3383 dwarf2_per_objfile->all_comp_units =
3384 XOBNEWVEC (&objfile->objfile_obstack, struct dwarf2_per_cu_data *,
3385 dwarf2_per_objfile->n_comp_units);
3386
3387 create_cus_from_index_list (objfile, cu_list, cu_list_elements,
3388 &dwarf2_per_objfile->info, 0, 0);
3389
3390 if (dwz_elements == 0)
3391 return;
3392
3393 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3394 create_cus_from_index_list (objfile, dwz_list, dwz_elements, &dwz->info, 1,
3395 cu_list_elements / 2);
3396 }
3397
3398 /* Create the signatured type hash table from the index. */
3399
3400 static void
3401 create_signatured_type_table_from_index (struct objfile *objfile,
3402 struct dwarf2_section_info *section,
3403 const gdb_byte *bytes,
3404 offset_type elements)
3405 {
3406 offset_type i;
3407 htab_t sig_types_hash;
3408 struct dwarf2_per_objfile *dwarf2_per_objfile
3409 = get_dwarf2_per_objfile (objfile);
3410
3411 dwarf2_per_objfile->n_type_units
3412 = dwarf2_per_objfile->n_allocated_type_units
3413 = elements / 3;
3414 dwarf2_per_objfile->all_type_units =
3415 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
3416
3417 sig_types_hash = allocate_signatured_type_table (objfile);
3418
3419 for (i = 0; i < elements; i += 3)
3420 {
3421 struct signatured_type *sig_type;
3422 ULONGEST signature;
3423 void **slot;
3424 cu_offset type_offset_in_tu;
3425
3426 gdb_static_assert (sizeof (ULONGEST) >= 8);
3427 sect_offset sect_off
3428 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
3429 type_offset_in_tu
3430 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
3431 BFD_ENDIAN_LITTLE);
3432 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
3433 bytes += 3 * 8;
3434
3435 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3436 struct signatured_type);
3437 sig_type->signature = signature;
3438 sig_type->type_offset_in_tu = type_offset_in_tu;
3439 sig_type->per_cu.is_debug_types = 1;
3440 sig_type->per_cu.section = section;
3441 sig_type->per_cu.sect_off = sect_off;
3442 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3443 sig_type->per_cu.v.quick
3444 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3445 struct dwarf2_per_cu_quick_data);
3446
3447 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3448 *slot = sig_type;
3449
3450 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
3451 }
3452
3453 dwarf2_per_objfile->signatured_types = sig_types_hash;
3454 }
3455
3456 /* Create the signatured type hash table from .debug_names. */
3457
3458 static void
3459 create_signatured_type_table_from_debug_names
3460 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3461 const mapped_debug_names &map,
3462 struct dwarf2_section_info *section,
3463 struct dwarf2_section_info *abbrev_section)
3464 {
3465 struct objfile *objfile = dwarf2_per_objfile->objfile;
3466
3467 dwarf2_read_section (objfile, section);
3468 dwarf2_read_section (objfile, abbrev_section);
3469
3470 dwarf2_per_objfile->n_type_units
3471 = dwarf2_per_objfile->n_allocated_type_units
3472 = map.tu_count;
3473 dwarf2_per_objfile->all_type_units
3474 = XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
3475
3476 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
3477
3478 for (uint32_t i = 0; i < map.tu_count; ++i)
3479 {
3480 struct signatured_type *sig_type;
3481 ULONGEST signature;
3482 void **slot;
3483 cu_offset type_offset_in_tu;
3484
3485 sect_offset sect_off
3486 = (sect_offset) (extract_unsigned_integer
3487 (map.tu_table_reordered + i * map.offset_size,
3488 map.offset_size,
3489 map.dwarf5_byte_order));
3490
3491 comp_unit_head cu_header;
3492 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
3493 abbrev_section,
3494 section->buffer + to_underlying (sect_off),
3495 rcuh_kind::TYPE);
3496
3497 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3498 struct signatured_type);
3499 sig_type->signature = cu_header.signature;
3500 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
3501 sig_type->per_cu.is_debug_types = 1;
3502 sig_type->per_cu.section = section;
3503 sig_type->per_cu.sect_off = sect_off;
3504 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3505 sig_type->per_cu.v.quick
3506 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3507 struct dwarf2_per_cu_quick_data);
3508
3509 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3510 *slot = sig_type;
3511
3512 dwarf2_per_objfile->all_type_units[i] = sig_type;
3513 }
3514
3515 dwarf2_per_objfile->signatured_types = sig_types_hash;
3516 }
3517
3518 /* Read the address map data from the mapped index, and use it to
3519 populate the objfile's psymtabs_addrmap. */
3520
3521 static void
3522 create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
3523 struct mapped_index *index)
3524 {
3525 struct objfile *objfile = dwarf2_per_objfile->objfile;
3526 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3527 const gdb_byte *iter, *end;
3528 struct addrmap *mutable_map;
3529 CORE_ADDR baseaddr;
3530
3531 auto_obstack temp_obstack;
3532
3533 mutable_map = addrmap_create_mutable (&temp_obstack);
3534
3535 iter = index->address_table.data ();
3536 end = iter + index->address_table.size ();
3537
3538 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3539
3540 while (iter < end)
3541 {
3542 ULONGEST hi, lo, cu_index;
3543 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3544 iter += 8;
3545 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3546 iter += 8;
3547 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
3548 iter += 4;
3549
3550 if (lo > hi)
3551 {
3552 complaint (&symfile_complaints,
3553 _(".gdb_index address table has invalid range (%s - %s)"),
3554 hex_string (lo), hex_string (hi));
3555 continue;
3556 }
3557
3558 if (cu_index >= dwarf2_per_objfile->n_comp_units)
3559 {
3560 complaint (&symfile_complaints,
3561 _(".gdb_index address table has invalid CU number %u"),
3562 (unsigned) cu_index);
3563 continue;
3564 }
3565
3566 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr);
3567 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr);
3568 addrmap_set_empty (mutable_map, lo, hi - 1,
3569 dw2_get_cutu (dwarf2_per_objfile, cu_index));
3570 }
3571
3572 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
3573 &objfile->objfile_obstack);
3574 }
3575
3576 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
3577 populate the objfile's psymtabs_addrmap. */
3578
3579 static void
3580 create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
3581 struct dwarf2_section_info *section)
3582 {
3583 struct objfile *objfile = dwarf2_per_objfile->objfile;
3584 bfd *abfd = objfile->obfd;
3585 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3586 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
3587 SECT_OFF_TEXT (objfile));
3588
3589 auto_obstack temp_obstack;
3590 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
3591
3592 std::unordered_map<sect_offset,
3593 dwarf2_per_cu_data *,
3594 gdb::hash_enum<sect_offset>>
3595 debug_info_offset_to_per_cu;
3596 for (int cui = 0; cui < dwarf2_per_objfile->n_comp_units; ++cui)
3597 {
3598 dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, cui);
3599 const auto insertpair
3600 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
3601 if (!insertpair.second)
3602 {
3603 warning (_("Section .debug_aranges in %s has duplicate "
3604 "debug_info_offset %s, ignoring .debug_aranges."),
3605 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
3606 return;
3607 }
3608 }
3609
3610 dwarf2_read_section (objfile, section);
3611
3612 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
3613
3614 const gdb_byte *addr = section->buffer;
3615
3616 while (addr < section->buffer + section->size)
3617 {
3618 const gdb_byte *const entry_addr = addr;
3619 unsigned int bytes_read;
3620
3621 const LONGEST entry_length = read_initial_length (abfd, addr,
3622 &bytes_read);
3623 addr += bytes_read;
3624
3625 const gdb_byte *const entry_end = addr + entry_length;
3626 const bool dwarf5_is_dwarf64 = bytes_read != 4;
3627 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
3628 if (addr + entry_length > section->buffer + section->size)
3629 {
3630 warning (_("Section .debug_aranges in %s entry at offset %zu "
3631 "length %s exceeds section length %s, "
3632 "ignoring .debug_aranges."),
3633 objfile_name (objfile), entry_addr - section->buffer,
3634 plongest (bytes_read + entry_length),
3635 pulongest (section->size));
3636 return;
3637 }
3638
3639 /* The version number. */
3640 const uint16_t version = read_2_bytes (abfd, addr);
3641 addr += 2;
3642 if (version != 2)
3643 {
3644 warning (_("Section .debug_aranges in %s entry at offset %zu "
3645 "has unsupported version %d, ignoring .debug_aranges."),
3646 objfile_name (objfile), entry_addr - section->buffer,
3647 version);
3648 return;
3649 }
3650
3651 const uint64_t debug_info_offset
3652 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
3653 addr += offset_size;
3654 const auto per_cu_it
3655 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
3656 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
3657 {
3658 warning (_("Section .debug_aranges in %s entry at offset %zu "
3659 "debug_info_offset %s does not exists, "
3660 "ignoring .debug_aranges."),
3661 objfile_name (objfile), entry_addr - section->buffer,
3662 pulongest (debug_info_offset));
3663 return;
3664 }
3665 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
3666
3667 const uint8_t address_size = *addr++;
3668 if (address_size < 1 || address_size > 8)
3669 {
3670 warning (_("Section .debug_aranges in %s entry at offset %zu "
3671 "address_size %u is invalid, ignoring .debug_aranges."),
3672 objfile_name (objfile), entry_addr - section->buffer,
3673 address_size);
3674 return;
3675 }
3676
3677 const uint8_t segment_selector_size = *addr++;
3678 if (segment_selector_size != 0)
3679 {
3680 warning (_("Section .debug_aranges in %s entry at offset %zu "
3681 "segment_selector_size %u is not supported, "
3682 "ignoring .debug_aranges."),
3683 objfile_name (objfile), entry_addr - section->buffer,
3684 segment_selector_size);
3685 return;
3686 }
3687
3688 /* Must pad to an alignment boundary that is twice the address
3689 size. It is undocumented by the DWARF standard but GCC does
3690 use it. */
3691 for (size_t padding = ((-(addr - section->buffer))
3692 & (2 * address_size - 1));
3693 padding > 0; padding--)
3694 if (*addr++ != 0)
3695 {
3696 warning (_("Section .debug_aranges in %s entry at offset %zu "
3697 "padding is not zero, ignoring .debug_aranges."),
3698 objfile_name (objfile), entry_addr - section->buffer);
3699 return;
3700 }
3701
3702 for (;;)
3703 {
3704 if (addr + 2 * address_size > entry_end)
3705 {
3706 warning (_("Section .debug_aranges in %s entry at offset %zu "
3707 "address list is not properly terminated, "
3708 "ignoring .debug_aranges."),
3709 objfile_name (objfile), entry_addr - section->buffer);
3710 return;
3711 }
3712 ULONGEST start = extract_unsigned_integer (addr, address_size,
3713 dwarf5_byte_order);
3714 addr += address_size;
3715 ULONGEST length = extract_unsigned_integer (addr, address_size,
3716 dwarf5_byte_order);
3717 addr += address_size;
3718 if (start == 0 && length == 0)
3719 break;
3720 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
3721 {
3722 /* Symbol was eliminated due to a COMDAT group. */
3723 continue;
3724 }
3725 ULONGEST end = start + length;
3726 start = gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr);
3727 end = gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr);
3728 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
3729 }
3730 }
3731
3732 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
3733 &objfile->objfile_obstack);
3734 }
3735
3736 /* The hash function for strings in the mapped index. This is the same as
3737 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
3738 implementation. This is necessary because the hash function is tied to the
3739 format of the mapped index file. The hash values do not have to match with
3740 SYMBOL_HASH_NEXT.
3741
3742 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
3743
3744 static hashval_t
3745 mapped_index_string_hash (int index_version, const void *p)
3746 {
3747 const unsigned char *str = (const unsigned char *) p;
3748 hashval_t r = 0;
3749 unsigned char c;
3750
3751 while ((c = *str++) != 0)
3752 {
3753 if (index_version >= 5)
3754 c = tolower (c);
3755 r = r * 67 + c - 113;
3756 }
3757
3758 return r;
3759 }
3760
3761 /* Find a slot in the mapped index INDEX for the object named NAME.
3762 If NAME is found, set *VEC_OUT to point to the CU vector in the
3763 constant pool and return true. If NAME cannot be found, return
3764 false. */
3765
3766 static bool
3767 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3768 offset_type **vec_out)
3769 {
3770 offset_type hash;
3771 offset_type slot, step;
3772 int (*cmp) (const char *, const char *);
3773
3774 gdb::unique_xmalloc_ptr<char> without_params;
3775 if (current_language->la_language == language_cplus
3776 || current_language->la_language == language_fortran
3777 || current_language->la_language == language_d)
3778 {
3779 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3780 not contain any. */
3781
3782 if (strchr (name, '(') != NULL)
3783 {
3784 without_params = cp_remove_params (name);
3785
3786 if (without_params != NULL)
3787 name = without_params.get ();
3788 }
3789 }
3790
3791 /* Index version 4 did not support case insensitive searches. But the
3792 indices for case insensitive languages are built in lowercase, therefore
3793 simulate our NAME being searched is also lowercased. */
3794 hash = mapped_index_string_hash ((index->version == 4
3795 && case_sensitivity == case_sensitive_off
3796 ? 5 : index->version),
3797 name);
3798
3799 slot = hash & (index->symbol_table.size () - 1);
3800 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
3801 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
3802
3803 for (;;)
3804 {
3805 const char *str;
3806
3807 const auto &bucket = index->symbol_table[slot];
3808 if (bucket.name == 0 && bucket.vec == 0)
3809 return false;
3810
3811 str = index->constant_pool + MAYBE_SWAP (bucket.name);
3812 if (!cmp (name, str))
3813 {
3814 *vec_out = (offset_type *) (index->constant_pool
3815 + MAYBE_SWAP (bucket.vec));
3816 return true;
3817 }
3818
3819 slot = (slot + step) & (index->symbol_table.size () - 1);
3820 }
3821 }
3822
3823 /* A helper function that reads the .gdb_index from SECTION and fills
3824 in MAP. FILENAME is the name of the file containing the section;
3825 it is used for error reporting. DEPRECATED_OK is nonzero if it is
3826 ok to use deprecated sections.
3827
3828 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3829 out parameters that are filled in with information about the CU and
3830 TU lists in the section.
3831
3832 Returns 1 if all went well, 0 otherwise. */
3833
3834 static int
3835 read_index_from_section (struct objfile *objfile,
3836 const char *filename,
3837 int deprecated_ok,
3838 struct dwarf2_section_info *section,
3839 struct mapped_index *map,
3840 const gdb_byte **cu_list,
3841 offset_type *cu_list_elements,
3842 const gdb_byte **types_list,
3843 offset_type *types_list_elements)
3844 {
3845 const gdb_byte *addr;
3846 offset_type version;
3847 offset_type *metadata;
3848 int i;
3849
3850 if (dwarf2_section_empty_p (section))
3851 return 0;
3852
3853 /* Older elfutils strip versions could keep the section in the main
3854 executable while splitting it for the separate debug info file. */
3855 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
3856 return 0;
3857
3858 dwarf2_read_section (objfile, section);
3859
3860 addr = section->buffer;
3861 /* Version check. */
3862 version = MAYBE_SWAP (*(offset_type *) addr);
3863 /* Versions earlier than 3 emitted every copy of a psymbol. This
3864 causes the index to behave very poorly for certain requests. Version 3
3865 contained incomplete addrmap. So, it seems better to just ignore such
3866 indices. */
3867 if (version < 4)
3868 {
3869 static int warning_printed = 0;
3870 if (!warning_printed)
3871 {
3872 warning (_("Skipping obsolete .gdb_index section in %s."),
3873 filename);
3874 warning_printed = 1;
3875 }
3876 return 0;
3877 }
3878 /* Index version 4 uses a different hash function than index version
3879 5 and later.
3880
3881 Versions earlier than 6 did not emit psymbols for inlined
3882 functions. Using these files will cause GDB not to be able to
3883 set breakpoints on inlined functions by name, so we ignore these
3884 indices unless the user has done
3885 "set use-deprecated-index-sections on". */
3886 if (version < 6 && !deprecated_ok)
3887 {
3888 static int warning_printed = 0;
3889 if (!warning_printed)
3890 {
3891 warning (_("\
3892 Skipping deprecated .gdb_index section in %s.\n\
3893 Do \"set use-deprecated-index-sections on\" before the file is read\n\
3894 to use the section anyway."),
3895 filename);
3896 warning_printed = 1;
3897 }
3898 return 0;
3899 }
3900 /* Version 7 indices generated by gold refer to the CU for a symbol instead
3901 of the TU (for symbols coming from TUs),
3902 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3903 Plus gold-generated indices can have duplicate entries for global symbols,
3904 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3905 These are just performance bugs, and we can't distinguish gdb-generated
3906 indices from gold-generated ones, so issue no warning here. */
3907
3908 /* Indexes with higher version than the one supported by GDB may be no
3909 longer backward compatible. */
3910 if (version > 8)
3911 return 0;
3912
3913 map->version = version;
3914 map->total_size = section->size;
3915
3916 metadata = (offset_type *) (addr + sizeof (offset_type));
3917
3918 i = 0;
3919 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3920 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3921 / 8);
3922 ++i;
3923
3924 *types_list = addr + MAYBE_SWAP (metadata[i]);
3925 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3926 - MAYBE_SWAP (metadata[i]))
3927 / 8);
3928 ++i;
3929
3930 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3931 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3932 map->address_table
3933 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
3934 ++i;
3935
3936 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3937 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3938 map->symbol_table
3939 = gdb::array_view<mapped_index::symbol_table_slot>
3940 ((mapped_index::symbol_table_slot *) symbol_table,
3941 (mapped_index::symbol_table_slot *) symbol_table_end);
3942
3943 ++i;
3944 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
3945
3946 return 1;
3947 }
3948
3949 /* Read .gdb_index. If everything went ok, initialize the "quick"
3950 elements of all the CUs and return 1. Otherwise, return 0. */
3951
3952 static int
3953 dwarf2_read_index (struct objfile *objfile)
3954 {
3955 struct mapped_index local_map, *map;
3956 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3957 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3958 struct dwz_file *dwz;
3959 struct dwarf2_per_objfile *dwarf2_per_objfile
3960 = get_dwarf2_per_objfile (objfile);
3961
3962 if (!read_index_from_section (objfile, objfile_name (objfile),
3963 use_deprecated_index_sections,
3964 &dwarf2_per_objfile->gdb_index, &local_map,
3965 &cu_list, &cu_list_elements,
3966 &types_list, &types_list_elements))
3967 return 0;
3968
3969 /* Don't use the index if it's empty. */
3970 if (local_map.symbol_table.empty ())
3971 return 0;
3972
3973 /* If there is a .dwz file, read it so we can get its CU list as
3974 well. */
3975 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3976 if (dwz != NULL)
3977 {
3978 struct mapped_index dwz_map;
3979 const gdb_byte *dwz_types_ignore;
3980 offset_type dwz_types_elements_ignore;
3981
3982 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
3983 1,
3984 &dwz->gdb_index, &dwz_map,
3985 &dwz_list, &dwz_list_elements,
3986 &dwz_types_ignore,
3987 &dwz_types_elements_ignore))
3988 {
3989 warning (_("could not read '.gdb_index' section from %s; skipping"),
3990 bfd_get_filename (dwz->dwz_bfd));
3991 return 0;
3992 }
3993 }
3994
3995 create_cus_from_index (objfile, cu_list, cu_list_elements, dwz_list,
3996 dwz_list_elements);
3997
3998 if (types_list_elements)
3999 {
4000 struct dwarf2_section_info *section;
4001
4002 /* We can only handle a single .debug_types when we have an
4003 index. */
4004 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
4005 return 0;
4006
4007 section = VEC_index (dwarf2_section_info_def,
4008 dwarf2_per_objfile->types, 0);
4009
4010 create_signatured_type_table_from_index (objfile, section, types_list,
4011 types_list_elements);
4012 }
4013
4014 create_addrmap_from_index (dwarf2_per_objfile, &local_map);
4015
4016 map = XOBNEW (&objfile->objfile_obstack, struct mapped_index);
4017 map = new (map) mapped_index ();
4018 *map = local_map;
4019
4020 dwarf2_per_objfile->index_table = map;
4021 dwarf2_per_objfile->using_index = 1;
4022 dwarf2_per_objfile->quick_file_names_table =
4023 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
4024
4025 return 1;
4026 }
4027
4028 /* die_reader_func for dw2_get_file_names. */
4029
4030 static void
4031 dw2_get_file_names_reader (const struct die_reader_specs *reader,
4032 const gdb_byte *info_ptr,
4033 struct die_info *comp_unit_die,
4034 int has_children,
4035 void *data)
4036 {
4037 struct dwarf2_cu *cu = reader->cu;
4038 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
4039 struct dwarf2_per_objfile *dwarf2_per_objfile
4040 = cu->per_cu->dwarf2_per_objfile;
4041 struct objfile *objfile = dwarf2_per_objfile->objfile;
4042 struct dwarf2_per_cu_data *lh_cu;
4043 struct attribute *attr;
4044 int i;
4045 void **slot;
4046 struct quick_file_names *qfn;
4047
4048 gdb_assert (! this_cu->is_debug_types);
4049
4050 /* Our callers never want to match partial units -- instead they
4051 will match the enclosing full CU. */
4052 if (comp_unit_die->tag == DW_TAG_partial_unit)
4053 {
4054 this_cu->v.quick->no_file_data = 1;
4055 return;
4056 }
4057
4058 lh_cu = this_cu;
4059 slot = NULL;
4060
4061 line_header_up lh;
4062 sect_offset line_offset {};
4063
4064 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
4065 if (attr)
4066 {
4067 struct quick_file_names find_entry;
4068
4069 line_offset = (sect_offset) DW_UNSND (attr);
4070
4071 /* We may have already read in this line header (TU line header sharing).
4072 If we have we're done. */
4073 find_entry.hash.dwo_unit = cu->dwo_unit;
4074 find_entry.hash.line_sect_off = line_offset;
4075 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
4076 &find_entry, INSERT);
4077 if (*slot != NULL)
4078 {
4079 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
4080 return;
4081 }
4082
4083 lh = dwarf_decode_line_header (line_offset, cu);
4084 }
4085 if (lh == NULL)
4086 {
4087 lh_cu->v.quick->no_file_data = 1;
4088 return;
4089 }
4090
4091 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
4092 qfn->hash.dwo_unit = cu->dwo_unit;
4093 qfn->hash.line_sect_off = line_offset;
4094 gdb_assert (slot != NULL);
4095 *slot = qfn;
4096
4097 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
4098
4099 qfn->num_file_names = lh->file_names.size ();
4100 qfn->file_names =
4101 XOBNEWVEC (&objfile->objfile_obstack, const char *, lh->file_names.size ());
4102 for (i = 0; i < lh->file_names.size (); ++i)
4103 qfn->file_names[i] = file_full_name (i + 1, lh.get (), fnd.comp_dir);
4104 qfn->real_names = NULL;
4105
4106 lh_cu->v.quick->file_names = qfn;
4107 }
4108
4109 /* A helper for the "quick" functions which attempts to read the line
4110 table for THIS_CU. */
4111
4112 static struct quick_file_names *
4113 dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
4114 {
4115 /* This should never be called for TUs. */
4116 gdb_assert (! this_cu->is_debug_types);
4117 /* Nor type unit groups. */
4118 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
4119
4120 if (this_cu->v.quick->file_names != NULL)
4121 return this_cu->v.quick->file_names;
4122 /* If we know there is no line data, no point in looking again. */
4123 if (this_cu->v.quick->no_file_data)
4124 return NULL;
4125
4126 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
4127
4128 if (this_cu->v.quick->no_file_data)
4129 return NULL;
4130 return this_cu->v.quick->file_names;
4131 }
4132
4133 /* A helper for the "quick" functions which computes and caches the
4134 real path for a given file name from the line table. */
4135
4136 static const char *
4137 dw2_get_real_path (struct objfile *objfile,
4138 struct quick_file_names *qfn, int index)
4139 {
4140 if (qfn->real_names == NULL)
4141 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
4142 qfn->num_file_names, const char *);
4143
4144 if (qfn->real_names[index] == NULL)
4145 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
4146
4147 return qfn->real_names[index];
4148 }
4149
4150 static struct symtab *
4151 dw2_find_last_source_symtab (struct objfile *objfile)
4152 {
4153 struct dwarf2_per_objfile *dwarf2_per_objfile
4154 = get_dwarf2_per_objfile (objfile);
4155 int index = dwarf2_per_objfile->n_comp_units - 1;
4156 dwarf2_per_cu_data *dwarf_cu = dw2_get_cutu (dwarf2_per_objfile, index);
4157 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu);
4158
4159 if (cust == NULL)
4160 return NULL;
4161
4162 return compunit_primary_filetab (cust);
4163 }
4164
4165 /* Traversal function for dw2_forget_cached_source_info. */
4166
4167 static int
4168 dw2_free_cached_file_names (void **slot, void *info)
4169 {
4170 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
4171
4172 if (file_data->real_names)
4173 {
4174 int i;
4175
4176 for (i = 0; i < file_data->num_file_names; ++i)
4177 {
4178 xfree ((void*) file_data->real_names[i]);
4179 file_data->real_names[i] = NULL;
4180 }
4181 }
4182
4183 return 1;
4184 }
4185
4186 static void
4187 dw2_forget_cached_source_info (struct objfile *objfile)
4188 {
4189 struct dwarf2_per_objfile *dwarf2_per_objfile
4190 = get_dwarf2_per_objfile (objfile);
4191
4192 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
4193 dw2_free_cached_file_names, NULL);
4194 }
4195
4196 /* Helper function for dw2_map_symtabs_matching_filename that expands
4197 the symtabs and calls the iterator. */
4198
4199 static int
4200 dw2_map_expand_apply (struct objfile *objfile,
4201 struct dwarf2_per_cu_data *per_cu,
4202 const char *name, const char *real_path,
4203 gdb::function_view<bool (symtab *)> callback)
4204 {
4205 struct compunit_symtab *last_made = objfile->compunit_symtabs;
4206
4207 /* Don't visit already-expanded CUs. */
4208 if (per_cu->v.quick->compunit_symtab)
4209 return 0;
4210
4211 /* This may expand more than one symtab, and we want to iterate over
4212 all of them. */
4213 dw2_instantiate_symtab (per_cu);
4214
4215 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
4216 last_made, callback);
4217 }
4218
4219 /* Implementation of the map_symtabs_matching_filename method. */
4220
4221 static bool
4222 dw2_map_symtabs_matching_filename
4223 (struct objfile *objfile, const char *name, const char *real_path,
4224 gdb::function_view<bool (symtab *)> callback)
4225 {
4226 int i;
4227 const char *name_basename = lbasename (name);
4228 struct dwarf2_per_objfile *dwarf2_per_objfile
4229 = get_dwarf2_per_objfile (objfile);
4230
4231 /* The rule is CUs specify all the files, including those used by
4232 any TU, so there's no need to scan TUs here. */
4233
4234 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4235 {
4236 int j;
4237 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (dwarf2_per_objfile, i);
4238 struct quick_file_names *file_data;
4239
4240 /* We only need to look at symtabs not already expanded. */
4241 if (per_cu->v.quick->compunit_symtab)
4242 continue;
4243
4244 file_data = dw2_get_file_names (per_cu);
4245 if (file_data == NULL)
4246 continue;
4247
4248 for (j = 0; j < file_data->num_file_names; ++j)
4249 {
4250 const char *this_name = file_data->file_names[j];
4251 const char *this_real_name;
4252
4253 if (compare_filenames_for_search (this_name, name))
4254 {
4255 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
4256 callback))
4257 return true;
4258 continue;
4259 }
4260
4261 /* Before we invoke realpath, which can get expensive when many
4262 files are involved, do a quick comparison of the basenames. */
4263 if (! basenames_may_differ
4264 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
4265 continue;
4266
4267 this_real_name = dw2_get_real_path (objfile, file_data, j);
4268 if (compare_filenames_for_search (this_real_name, name))
4269 {
4270 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
4271 callback))
4272 return true;
4273 continue;
4274 }
4275
4276 if (real_path != NULL)
4277 {
4278 gdb_assert (IS_ABSOLUTE_PATH (real_path));
4279 gdb_assert (IS_ABSOLUTE_PATH (name));
4280 if (this_real_name != NULL
4281 && FILENAME_CMP (real_path, this_real_name) == 0)
4282 {
4283 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
4284 callback))
4285 return true;
4286 continue;
4287 }
4288 }
4289 }
4290 }
4291
4292 return false;
4293 }
4294
4295 /* Struct used to manage iterating over all CUs looking for a symbol. */
4296
4297 struct dw2_symtab_iterator
4298 {
4299 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
4300 struct dwarf2_per_objfile *dwarf2_per_objfile;
4301 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
4302 int want_specific_block;
4303 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
4304 Unused if !WANT_SPECIFIC_BLOCK. */
4305 int block_index;
4306 /* The kind of symbol we're looking for. */
4307 domain_enum domain;
4308 /* The list of CUs from the index entry of the symbol,
4309 or NULL if not found. */
4310 offset_type *vec;
4311 /* The next element in VEC to look at. */
4312 int next;
4313 /* The number of elements in VEC, or zero if there is no match. */
4314 int length;
4315 /* Have we seen a global version of the symbol?
4316 If so we can ignore all further global instances.
4317 This is to work around gold/15646, inefficient gold-generated
4318 indices. */
4319 int global_seen;
4320 };
4321
4322 /* Initialize the index symtab iterator ITER.
4323 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
4324 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
4325
4326 static void
4327 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
4328 struct dwarf2_per_objfile *dwarf2_per_objfile,
4329 int want_specific_block,
4330 int block_index,
4331 domain_enum domain,
4332 const char *name)
4333 {
4334 iter->dwarf2_per_objfile = dwarf2_per_objfile;
4335 iter->want_specific_block = want_specific_block;
4336 iter->block_index = block_index;
4337 iter->domain = domain;
4338 iter->next = 0;
4339 iter->global_seen = 0;
4340
4341 mapped_index *index = dwarf2_per_objfile->index_table;
4342
4343 /* index is NULL if OBJF_READNOW. */
4344 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
4345 iter->length = MAYBE_SWAP (*iter->vec);
4346 else
4347 {
4348 iter->vec = NULL;
4349 iter->length = 0;
4350 }
4351 }
4352
4353 /* Return the next matching CU or NULL if there are no more. */
4354
4355 static struct dwarf2_per_cu_data *
4356 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
4357 {
4358 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
4359
4360 for ( ; iter->next < iter->length; ++iter->next)
4361 {
4362 offset_type cu_index_and_attrs =
4363 MAYBE_SWAP (iter->vec[iter->next + 1]);
4364 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4365 struct dwarf2_per_cu_data *per_cu;
4366 int want_static = iter->block_index != GLOBAL_BLOCK;
4367 /* This value is only valid for index versions >= 7. */
4368 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4369 gdb_index_symbol_kind symbol_kind =
4370 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4371 /* Only check the symbol attributes if they're present.
4372 Indices prior to version 7 don't record them,
4373 and indices >= 7 may elide them for certain symbols
4374 (gold does this). */
4375 int attrs_valid =
4376 (dwarf2_per_objfile->index_table->version >= 7
4377 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4378
4379 /* Don't crash on bad data. */
4380 if (cu_index >= (dwarf2_per_objfile->n_comp_units
4381 + dwarf2_per_objfile->n_type_units))
4382 {
4383 complaint (&symfile_complaints,
4384 _(".gdb_index entry has bad CU index"
4385 " [in module %s]"),
4386 objfile_name (dwarf2_per_objfile->objfile));
4387 continue;
4388 }
4389
4390 per_cu = dw2_get_cutu (dwarf2_per_objfile, cu_index);
4391
4392 /* Skip if already read in. */
4393 if (per_cu->v.quick->compunit_symtab)
4394 continue;
4395
4396 /* Check static vs global. */
4397 if (attrs_valid)
4398 {
4399 if (iter->want_specific_block
4400 && want_static != is_static)
4401 continue;
4402 /* Work around gold/15646. */
4403 if (!is_static && iter->global_seen)
4404 continue;
4405 if (!is_static)
4406 iter->global_seen = 1;
4407 }
4408
4409 /* Only check the symbol's kind if it has one. */
4410 if (attrs_valid)
4411 {
4412 switch (iter->domain)
4413 {
4414 case VAR_DOMAIN:
4415 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
4416 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
4417 /* Some types are also in VAR_DOMAIN. */
4418 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4419 continue;
4420 break;
4421 case STRUCT_DOMAIN:
4422 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4423 continue;
4424 break;
4425 case LABEL_DOMAIN:
4426 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4427 continue;
4428 break;
4429 default:
4430 break;
4431 }
4432 }
4433
4434 ++iter->next;
4435 return per_cu;
4436 }
4437
4438 return NULL;
4439 }
4440
4441 static struct compunit_symtab *
4442 dw2_lookup_symbol (struct objfile *objfile, int block_index,
4443 const char *name, domain_enum domain)
4444 {
4445 struct compunit_symtab *stab_best = NULL;
4446 struct dwarf2_per_objfile *dwarf2_per_objfile
4447 = get_dwarf2_per_objfile (objfile);
4448
4449 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
4450
4451 struct dw2_symtab_iterator iter;
4452 struct dwarf2_per_cu_data *per_cu;
4453
4454 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, 1, block_index, domain, name);
4455
4456 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4457 {
4458 struct symbol *sym, *with_opaque = NULL;
4459 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu);
4460 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
4461 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
4462
4463 sym = block_find_symbol (block, name, domain,
4464 block_find_non_opaque_type_preferred,
4465 &with_opaque);
4466
4467 /* Some caution must be observed with overloaded functions
4468 and methods, since the index will not contain any overload
4469 information (but NAME might contain it). */
4470
4471 if (sym != NULL
4472 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
4473 return stab;
4474 if (with_opaque != NULL
4475 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
4476 stab_best = stab;
4477
4478 /* Keep looking through other CUs. */
4479 }
4480
4481 return stab_best;
4482 }
4483
4484 static void
4485 dw2_print_stats (struct objfile *objfile)
4486 {
4487 struct dwarf2_per_objfile *dwarf2_per_objfile
4488 = get_dwarf2_per_objfile (objfile);
4489 int total = dwarf2_per_objfile->n_comp_units + dwarf2_per_objfile->n_type_units;
4490 int count = 0;
4491
4492 for (int i = 0; i < total; ++i)
4493 {
4494 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
4495
4496 if (!per_cu->v.quick->compunit_symtab)
4497 ++count;
4498 }
4499 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
4500 printf_filtered (_(" Number of unread CUs: %d\n"), count);
4501 }
4502
4503 /* This dumps minimal information about the index.
4504 It is called via "mt print objfiles".
4505 One use is to verify .gdb_index has been loaded by the
4506 gdb.dwarf2/gdb-index.exp testcase. */
4507
4508 static void
4509 dw2_dump (struct objfile *objfile)
4510 {
4511 struct dwarf2_per_objfile *dwarf2_per_objfile
4512 = get_dwarf2_per_objfile (objfile);
4513
4514 gdb_assert (dwarf2_per_objfile->using_index);
4515 printf_filtered (".gdb_index:");
4516 if (dwarf2_per_objfile->index_table != NULL)
4517 {
4518 printf_filtered (" version %d\n",
4519 dwarf2_per_objfile->index_table->version);
4520 }
4521 else
4522 printf_filtered (" faked for \"readnow\"\n");
4523 printf_filtered ("\n");
4524 }
4525
4526 static void
4527 dw2_relocate (struct objfile *objfile,
4528 const struct section_offsets *new_offsets,
4529 const struct section_offsets *delta)
4530 {
4531 /* There's nothing to relocate here. */
4532 }
4533
4534 static void
4535 dw2_expand_symtabs_for_function (struct objfile *objfile,
4536 const char *func_name)
4537 {
4538 struct dwarf2_per_objfile *dwarf2_per_objfile
4539 = get_dwarf2_per_objfile (objfile);
4540
4541 struct dw2_symtab_iterator iter;
4542 struct dwarf2_per_cu_data *per_cu;
4543
4544 /* Note: It doesn't matter what we pass for block_index here. */
4545 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, 0, GLOBAL_BLOCK, VAR_DOMAIN,
4546 func_name);
4547
4548 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4549 dw2_instantiate_symtab (per_cu);
4550
4551 }
4552
4553 static void
4554 dw2_expand_all_symtabs (struct objfile *objfile)
4555 {
4556 struct dwarf2_per_objfile *dwarf2_per_objfile
4557 = get_dwarf2_per_objfile (objfile);
4558 int total_units = (dwarf2_per_objfile->n_comp_units
4559 + dwarf2_per_objfile->n_type_units);
4560
4561 for (int i = 0; i < total_units; ++i)
4562 {
4563 struct dwarf2_per_cu_data *per_cu
4564 = dw2_get_cutu (dwarf2_per_objfile, i);
4565
4566 dw2_instantiate_symtab (per_cu);
4567 }
4568 }
4569
4570 static void
4571 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
4572 const char *fullname)
4573 {
4574 struct dwarf2_per_objfile *dwarf2_per_objfile
4575 = get_dwarf2_per_objfile (objfile);
4576
4577 /* We don't need to consider type units here.
4578 This is only called for examining code, e.g. expand_line_sal.
4579 There can be an order of magnitude (or more) more type units
4580 than comp units, and we avoid them if we can. */
4581
4582 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4583 {
4584 int j;
4585 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
4586 struct quick_file_names *file_data;
4587
4588 /* We only need to look at symtabs not already expanded. */
4589 if (per_cu->v.quick->compunit_symtab)
4590 continue;
4591
4592 file_data = dw2_get_file_names (per_cu);
4593 if (file_data == NULL)
4594 continue;
4595
4596 for (j = 0; j < file_data->num_file_names; ++j)
4597 {
4598 const char *this_fullname = file_data->file_names[j];
4599
4600 if (filename_cmp (this_fullname, fullname) == 0)
4601 {
4602 dw2_instantiate_symtab (per_cu);
4603 break;
4604 }
4605 }
4606 }
4607 }
4608
4609 static void
4610 dw2_map_matching_symbols (struct objfile *objfile,
4611 const char * name, domain_enum domain,
4612 int global,
4613 int (*callback) (struct block *,
4614 struct symbol *, void *),
4615 void *data, symbol_name_match_type match,
4616 symbol_compare_ftype *ordered_compare)
4617 {
4618 /* Currently unimplemented; used for Ada. The function can be called if the
4619 current language is Ada for a non-Ada objfile using GNU index. As Ada
4620 does not look for non-Ada symbols this function should just return. */
4621 }
4622
4623 /* Symbol name matcher for .gdb_index names.
4624
4625 Symbol names in .gdb_index have a few particularities:
4626
4627 - There's no indication of which is the language of each symbol.
4628
4629 Since each language has its own symbol name matching algorithm,
4630 and we don't know which language is the right one, we must match
4631 each symbol against all languages. This would be a potential
4632 performance problem if it were not mitigated by the
4633 mapped_index::name_components lookup table, which significantly
4634 reduces the number of times we need to call into this matcher,
4635 making it a non-issue.
4636
4637 - Symbol names in the index have no overload (parameter)
4638 information. I.e., in C++, "foo(int)" and "foo(long)" both
4639 appear as "foo" in the index, for example.
4640
4641 This means that the lookup names passed to the symbol name
4642 matcher functions must have no parameter information either
4643 because (e.g.) symbol search name "foo" does not match
4644 lookup-name "foo(int)" [while swapping search name for lookup
4645 name would match].
4646 */
4647 class gdb_index_symbol_name_matcher
4648 {
4649 public:
4650 /* Prepares the vector of comparison functions for LOOKUP_NAME. */
4651 gdb_index_symbol_name_matcher (const lookup_name_info &lookup_name);
4652
4653 /* Walk all the matcher routines and match SYMBOL_NAME against them.
4654 Returns true if any matcher matches. */
4655 bool matches (const char *symbol_name);
4656
4657 private:
4658 /* A reference to the lookup name we're matching against. */
4659 const lookup_name_info &m_lookup_name;
4660
4661 /* A vector holding all the different symbol name matchers, for all
4662 languages. */
4663 std::vector<symbol_name_matcher_ftype *> m_symbol_name_matcher_funcs;
4664 };
4665
4666 gdb_index_symbol_name_matcher::gdb_index_symbol_name_matcher
4667 (const lookup_name_info &lookup_name)
4668 : m_lookup_name (lookup_name)
4669 {
4670 /* Prepare the vector of comparison functions upfront, to avoid
4671 doing the same work for each symbol. Care is taken to avoid
4672 matching with the same matcher more than once if/when multiple
4673 languages use the same matcher function. */
4674 auto &matchers = m_symbol_name_matcher_funcs;
4675 matchers.reserve (nr_languages);
4676
4677 matchers.push_back (default_symbol_name_matcher);
4678
4679 for (int i = 0; i < nr_languages; i++)
4680 {
4681 const language_defn *lang = language_def ((enum language) i);
4682 symbol_name_matcher_ftype *name_matcher
4683 = get_symbol_name_matcher (lang, m_lookup_name);
4684
4685 /* Don't insert the same comparison routine more than once.
4686 Note that we do this linear walk instead of a seemingly
4687 cheaper sorted insert, or use a std::set or something like
4688 that, because relative order of function addresses is not
4689 stable. This is not a problem in practice because the number
4690 of supported languages is low, and the cost here is tiny
4691 compared to the number of searches we'll do afterwards using
4692 this object. */
4693 if (name_matcher != default_symbol_name_matcher
4694 && (std::find (matchers.begin (), matchers.end (), name_matcher)
4695 == matchers.end ()))
4696 matchers.push_back (name_matcher);
4697 }
4698 }
4699
4700 bool
4701 gdb_index_symbol_name_matcher::matches (const char *symbol_name)
4702 {
4703 for (auto matches_name : m_symbol_name_matcher_funcs)
4704 if (matches_name (symbol_name, m_lookup_name, NULL))
4705 return true;
4706
4707 return false;
4708 }
4709
4710 /* Starting from a search name, return the string that finds the upper
4711 bound of all strings that start with SEARCH_NAME in a sorted name
4712 list. Returns the empty string to indicate that the upper bound is
4713 the end of the list. */
4714
4715 static std::string
4716 make_sort_after_prefix_name (const char *search_name)
4717 {
4718 /* When looking to complete "func", we find the upper bound of all
4719 symbols that start with "func" by looking for where we'd insert
4720 the closest string that would follow "func" in lexicographical
4721 order. Usually, that's "func"-with-last-character-incremented,
4722 i.e. "fund". Mind non-ASCII characters, though. Usually those
4723 will be UTF-8 multi-byte sequences, but we can't be certain.
4724 Especially mind the 0xff character, which is a valid character in
4725 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
4726 rule out compilers allowing it in identifiers. Note that
4727 conveniently, strcmp/strcasecmp are specified to compare
4728 characters interpreted as unsigned char. So what we do is treat
4729 the whole string as a base 256 number composed of a sequence of
4730 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
4731 to 0, and carries 1 to the following more-significant position.
4732 If the very first character in SEARCH_NAME ends up incremented
4733 and carries/overflows, then the upper bound is the end of the
4734 list. The string after the empty string is also the empty
4735 string.
4736
4737 Some examples of this operation:
4738
4739 SEARCH_NAME => "+1" RESULT
4740
4741 "abc" => "abd"
4742 "ab\xff" => "ac"
4743 "\xff" "a" "\xff" => "\xff" "b"
4744 "\xff" => ""
4745 "\xff\xff" => ""
4746 "" => ""
4747
4748 Then, with these symbols for example:
4749
4750 func
4751 func1
4752 fund
4753
4754 completing "func" looks for symbols between "func" and
4755 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
4756 which finds "func" and "func1", but not "fund".
4757
4758 And with:
4759
4760 funcÿ (Latin1 'ÿ' [0xff])
4761 funcÿ1
4762 fund
4763
4764 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
4765 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
4766
4767 And with:
4768
4769 ÿÿ (Latin1 'ÿ' [0xff])
4770 ÿÿ1
4771
4772 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
4773 the end of the list.
4774 */
4775 std::string after = search_name;
4776 while (!after.empty () && (unsigned char) after.back () == 0xff)
4777 after.pop_back ();
4778 if (!after.empty ())
4779 after.back () = (unsigned char) after.back () + 1;
4780 return after;
4781 }
4782
4783 /* See declaration. */
4784
4785 std::pair<std::vector<name_component>::const_iterator,
4786 std::vector<name_component>::const_iterator>
4787 mapped_index_base::find_name_components_bounds
4788 (const lookup_name_info &lookup_name_without_params) const
4789 {
4790 auto *name_cmp
4791 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4792
4793 const char *cplus
4794 = lookup_name_without_params.cplus ().lookup_name ().c_str ();
4795
4796 /* Comparison function object for lower_bound that matches against a
4797 given symbol name. */
4798 auto lookup_compare_lower = [&] (const name_component &elem,
4799 const char *name)
4800 {
4801 const char *elem_qualified = this->symbol_name_at (elem.idx);
4802 const char *elem_name = elem_qualified + elem.name_offset;
4803 return name_cmp (elem_name, name) < 0;
4804 };
4805
4806 /* Comparison function object for upper_bound that matches against a
4807 given symbol name. */
4808 auto lookup_compare_upper = [&] (const char *name,
4809 const name_component &elem)
4810 {
4811 const char *elem_qualified = this->symbol_name_at (elem.idx);
4812 const char *elem_name = elem_qualified + elem.name_offset;
4813 return name_cmp (name, elem_name) < 0;
4814 };
4815
4816 auto begin = this->name_components.begin ();
4817 auto end = this->name_components.end ();
4818
4819 /* Find the lower bound. */
4820 auto lower = [&] ()
4821 {
4822 if (lookup_name_without_params.completion_mode () && cplus[0] == '\0')
4823 return begin;
4824 else
4825 return std::lower_bound (begin, end, cplus, lookup_compare_lower);
4826 } ();
4827
4828 /* Find the upper bound. */
4829 auto upper = [&] ()
4830 {
4831 if (lookup_name_without_params.completion_mode ())
4832 {
4833 /* In completion mode, we want UPPER to point past all
4834 symbols names that have the same prefix. I.e., with
4835 these symbols, and completing "func":
4836
4837 function << lower bound
4838 function1
4839 other_function << upper bound
4840
4841 We find the upper bound by looking for the insertion
4842 point of "func"-with-last-character-incremented,
4843 i.e. "fund". */
4844 std::string after = make_sort_after_prefix_name (cplus);
4845 if (after.empty ())
4846 return end;
4847 return std::lower_bound (lower, end, after.c_str (),
4848 lookup_compare_lower);
4849 }
4850 else
4851 return std::upper_bound (lower, end, cplus, lookup_compare_upper);
4852 } ();
4853
4854 return {lower, upper};
4855 }
4856
4857 /* See declaration. */
4858
4859 void
4860 mapped_index_base::build_name_components ()
4861 {
4862 if (!this->name_components.empty ())
4863 return;
4864
4865 this->name_components_casing = case_sensitivity;
4866 auto *name_cmp
4867 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4868
4869 /* The code below only knows how to break apart components of C++
4870 symbol names (and other languages that use '::' as
4871 namespace/module separator). If we add support for wild matching
4872 to some language that uses some other operator (E.g., Ada, Go and
4873 D use '.'), then we'll need to try splitting the symbol name
4874 according to that language too. Note that Ada does support wild
4875 matching, but doesn't currently support .gdb_index. */
4876 auto count = this->symbol_name_count ();
4877 for (offset_type idx = 0; idx < count; idx++)
4878 {
4879 if (this->symbol_name_slot_invalid (idx))
4880 continue;
4881
4882 const char *name = this->symbol_name_at (idx);
4883
4884 /* Add each name component to the name component table. */
4885 unsigned int previous_len = 0;
4886 for (unsigned int current_len = cp_find_first_component (name);
4887 name[current_len] != '\0';
4888 current_len += cp_find_first_component (name + current_len))
4889 {
4890 gdb_assert (name[current_len] == ':');
4891 this->name_components.push_back ({previous_len, idx});
4892 /* Skip the '::'. */
4893 current_len += 2;
4894 previous_len = current_len;
4895 }
4896 this->name_components.push_back ({previous_len, idx});
4897 }
4898
4899 /* Sort name_components elements by name. */
4900 auto name_comp_compare = [&] (const name_component &left,
4901 const name_component &right)
4902 {
4903 const char *left_qualified = this->symbol_name_at (left.idx);
4904 const char *right_qualified = this->symbol_name_at (right.idx);
4905
4906 const char *left_name = left_qualified + left.name_offset;
4907 const char *right_name = right_qualified + right.name_offset;
4908
4909 return name_cmp (left_name, right_name) < 0;
4910 };
4911
4912 std::sort (this->name_components.begin (),
4913 this->name_components.end (),
4914 name_comp_compare);
4915 }
4916
4917 /* Helper for dw2_expand_symtabs_matching that works with a
4918 mapped_index_base instead of the containing objfile. This is split
4919 to a separate function in order to be able to unit test the
4920 name_components matching using a mock mapped_index_base. For each
4921 symbol name that matches, calls MATCH_CALLBACK, passing it the
4922 symbol's index in the mapped_index_base symbol table. */
4923
4924 static void
4925 dw2_expand_symtabs_matching_symbol
4926 (mapped_index_base &index,
4927 const lookup_name_info &lookup_name_in,
4928 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4929 enum search_domain kind,
4930 gdb::function_view<void (offset_type)> match_callback)
4931 {
4932 lookup_name_info lookup_name_without_params
4933 = lookup_name_in.make_ignore_params ();
4934 gdb_index_symbol_name_matcher lookup_name_matcher
4935 (lookup_name_without_params);
4936
4937 /* Build the symbol name component sorted vector, if we haven't
4938 yet. */
4939 index.build_name_components ();
4940
4941 auto bounds = index.find_name_components_bounds (lookup_name_without_params);
4942
4943 /* Now for each symbol name in range, check to see if we have a name
4944 match, and if so, call the MATCH_CALLBACK callback. */
4945
4946 /* The same symbol may appear more than once in the range though.
4947 E.g., if we're looking for symbols that complete "w", and we have
4948 a symbol named "w1::w2", we'll find the two name components for
4949 that same symbol in the range. To be sure we only call the
4950 callback once per symbol, we first collect the symbol name
4951 indexes that matched in a temporary vector and ignore
4952 duplicates. */
4953 std::vector<offset_type> matches;
4954 matches.reserve (std::distance (bounds.first, bounds.second));
4955
4956 for (; bounds.first != bounds.second; ++bounds.first)
4957 {
4958 const char *qualified = index.symbol_name_at (bounds.first->idx);
4959
4960 if (!lookup_name_matcher.matches (qualified)
4961 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4962 continue;
4963
4964 matches.push_back (bounds.first->idx);
4965 }
4966
4967 std::sort (matches.begin (), matches.end ());
4968
4969 /* Finally call the callback, once per match. */
4970 ULONGEST prev = -1;
4971 for (offset_type idx : matches)
4972 {
4973 if (prev != idx)
4974 {
4975 match_callback (idx);
4976 prev = idx;
4977 }
4978 }
4979
4980 /* Above we use a type wider than idx's for 'prev', since 0 and
4981 (offset_type)-1 are both possible values. */
4982 static_assert (sizeof (prev) > sizeof (offset_type), "");
4983 }
4984
4985 #if GDB_SELF_TEST
4986
4987 namespace selftests { namespace dw2_expand_symtabs_matching {
4988
4989 /* A mock .gdb_index/.debug_names-like name index table, enough to
4990 exercise dw2_expand_symtabs_matching_symbol, which works with the
4991 mapped_index_base interface. Builds an index from the symbol list
4992 passed as parameter to the constructor. */
4993 class mock_mapped_index : public mapped_index_base
4994 {
4995 public:
4996 mock_mapped_index (gdb::array_view<const char *> symbols)
4997 : m_symbol_table (symbols)
4998 {}
4999
5000 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
5001
5002 /* Return the number of names in the symbol table. */
5003 virtual size_t symbol_name_count () const
5004 {
5005 return m_symbol_table.size ();
5006 }
5007
5008 /* Get the name of the symbol at IDX in the symbol table. */
5009 virtual const char *symbol_name_at (offset_type idx) const
5010 {
5011 return m_symbol_table[idx];
5012 }
5013
5014 private:
5015 gdb::array_view<const char *> m_symbol_table;
5016 };
5017
5018 /* Convenience function that converts a NULL pointer to a "<null>"
5019 string, to pass to print routines. */
5020
5021 static const char *
5022 string_or_null (const char *str)
5023 {
5024 return str != NULL ? str : "<null>";
5025 }
5026
5027 /* Check if a lookup_name_info built from
5028 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
5029 index. EXPECTED_LIST is the list of expected matches, in expected
5030 matching order. If no match expected, then an empty list is
5031 specified. Returns true on success. On failure prints a warning
5032 indicating the file:line that failed, and returns false. */
5033
5034 static bool
5035 check_match (const char *file, int line,
5036 mock_mapped_index &mock_index,
5037 const char *name, symbol_name_match_type match_type,
5038 bool completion_mode,
5039 std::initializer_list<const char *> expected_list)
5040 {
5041 lookup_name_info lookup_name (name, match_type, completion_mode);
5042
5043 bool matched = true;
5044
5045 auto mismatch = [&] (const char *expected_str,
5046 const char *got)
5047 {
5048 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
5049 "expected=\"%s\", got=\"%s\"\n"),
5050 file, line,
5051 (match_type == symbol_name_match_type::FULL
5052 ? "FULL" : "WILD"),
5053 name, string_or_null (expected_str), string_or_null (got));
5054 matched = false;
5055 };
5056
5057 auto expected_it = expected_list.begin ();
5058 auto expected_end = expected_list.end ();
5059
5060 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
5061 NULL, ALL_DOMAIN,
5062 [&] (offset_type idx)
5063 {
5064 const char *matched_name = mock_index.symbol_name_at (idx);
5065 const char *expected_str
5066 = expected_it == expected_end ? NULL : *expected_it++;
5067
5068 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
5069 mismatch (expected_str, matched_name);
5070 });
5071
5072 const char *expected_str
5073 = expected_it == expected_end ? NULL : *expected_it++;
5074 if (expected_str != NULL)
5075 mismatch (expected_str, NULL);
5076
5077 return matched;
5078 }
5079
5080 /* The symbols added to the mock mapped_index for testing (in
5081 canonical form). */
5082 static const char *test_symbols[] = {
5083 "function",
5084 "std::bar",
5085 "std::zfunction",
5086 "std::zfunction2",
5087 "w1::w2",
5088 "ns::foo<char*>",
5089 "ns::foo<int>",
5090 "ns::foo<long>",
5091 "ns2::tmpl<int>::foo2",
5092 "(anonymous namespace)::A::B::C",
5093
5094 /* These are used to check that the increment-last-char in the
5095 matching algorithm for completion doesn't match "t1_fund" when
5096 completing "t1_func". */
5097 "t1_func",
5098 "t1_func1",
5099 "t1_fund",
5100 "t1_fund1",
5101
5102 /* A UTF-8 name with multi-byte sequences to make sure that
5103 cp-name-parser understands this as a single identifier ("função"
5104 is "function" in PT). */
5105 u8"u8função",
5106
5107 /* \377 (0xff) is Latin1 'ÿ'. */
5108 "yfunc\377",
5109
5110 /* \377 (0xff) is Latin1 'ÿ'. */
5111 "\377",
5112 "\377\377123",
5113
5114 /* A name with all sorts of complications. Starts with "z" to make
5115 it easier for the completion tests below. */
5116 #define Z_SYM_NAME \
5117 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
5118 "::tuple<(anonymous namespace)::ui*, " \
5119 "std::default_delete<(anonymous namespace)::ui>, void>"
5120
5121 Z_SYM_NAME
5122 };
5123
5124 /* Returns true if the mapped_index_base::find_name_component_bounds
5125 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
5126 in completion mode. */
5127
5128 static bool
5129 check_find_bounds_finds (mapped_index_base &index,
5130 const char *search_name,
5131 gdb::array_view<const char *> expected_syms)
5132 {
5133 lookup_name_info lookup_name (search_name,
5134 symbol_name_match_type::FULL, true);
5135
5136 auto bounds = index.find_name_components_bounds (lookup_name);
5137
5138 size_t distance = std::distance (bounds.first, bounds.second);
5139 if (distance != expected_syms.size ())
5140 return false;
5141
5142 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
5143 {
5144 auto nc_elem = bounds.first + exp_elem;
5145 const char *qualified = index.symbol_name_at (nc_elem->idx);
5146 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
5147 return false;
5148 }
5149
5150 return true;
5151 }
5152
5153 /* Test the lower-level mapped_index::find_name_component_bounds
5154 method. */
5155
5156 static void
5157 test_mapped_index_find_name_component_bounds ()
5158 {
5159 mock_mapped_index mock_index (test_symbols);
5160
5161 mock_index.build_name_components ();
5162
5163 /* Test the lower-level mapped_index::find_name_component_bounds
5164 method in completion mode. */
5165 {
5166 static const char *expected_syms[] = {
5167 "t1_func",
5168 "t1_func1",
5169 };
5170
5171 SELF_CHECK (check_find_bounds_finds (mock_index,
5172 "t1_func", expected_syms));
5173 }
5174
5175 /* Check that the increment-last-char in the name matching algorithm
5176 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
5177 {
5178 static const char *expected_syms1[] = {
5179 "\377",
5180 "\377\377123",
5181 };
5182 SELF_CHECK (check_find_bounds_finds (mock_index,
5183 "\377", expected_syms1));
5184
5185 static const char *expected_syms2[] = {
5186 "\377\377123",
5187 };
5188 SELF_CHECK (check_find_bounds_finds (mock_index,
5189 "\377\377", expected_syms2));
5190 }
5191 }
5192
5193 /* Test dw2_expand_symtabs_matching_symbol. */
5194
5195 static void
5196 test_dw2_expand_symtabs_matching_symbol ()
5197 {
5198 mock_mapped_index mock_index (test_symbols);
5199
5200 /* We let all tests run until the end even if some fails, for debug
5201 convenience. */
5202 bool any_mismatch = false;
5203
5204 /* Create the expected symbols list (an initializer_list). Needed
5205 because lists have commas, and we need to pass them to CHECK,
5206 which is a macro. */
5207 #define EXPECT(...) { __VA_ARGS__ }
5208
5209 /* Wrapper for check_match that passes down the current
5210 __FILE__/__LINE__. */
5211 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
5212 any_mismatch |= !check_match (__FILE__, __LINE__, \
5213 mock_index, \
5214 NAME, MATCH_TYPE, COMPLETION_MODE, \
5215 EXPECTED_LIST)
5216
5217 /* Identity checks. */
5218 for (const char *sym : test_symbols)
5219 {
5220 /* Should be able to match all existing symbols. */
5221 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
5222 EXPECT (sym));
5223
5224 /* Should be able to match all existing symbols with
5225 parameters. */
5226 std::string with_params = std::string (sym) + "(int)";
5227 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
5228 EXPECT (sym));
5229
5230 /* Should be able to match all existing symbols with
5231 parameters and qualifiers. */
5232 with_params = std::string (sym) + " ( int ) const";
5233 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
5234 EXPECT (sym));
5235
5236 /* This should really find sym, but cp-name-parser.y doesn't
5237 know about lvalue/rvalue qualifiers yet. */
5238 with_params = std::string (sym) + " ( int ) &&";
5239 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
5240 {});
5241 }
5242
5243 /* Check that the name matching algorithm for completion doesn't get
5244 confused with Latin1 'ÿ' / 0xff. */
5245 {
5246 static const char str[] = "\377";
5247 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
5248 EXPECT ("\377", "\377\377123"));
5249 }
5250
5251 /* Check that the increment-last-char in the matching algorithm for
5252 completion doesn't match "t1_fund" when completing "t1_func". */
5253 {
5254 static const char str[] = "t1_func";
5255 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
5256 EXPECT ("t1_func", "t1_func1"));
5257 }
5258
5259 /* Check that completion mode works at each prefix of the expected
5260 symbol name. */
5261 {
5262 static const char str[] = "function(int)";
5263 size_t len = strlen (str);
5264 std::string lookup;
5265
5266 for (size_t i = 1; i < len; i++)
5267 {
5268 lookup.assign (str, i);
5269 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
5270 EXPECT ("function"));
5271 }
5272 }
5273
5274 /* While "w" is a prefix of both components, the match function
5275 should still only be called once. */
5276 {
5277 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
5278 EXPECT ("w1::w2"));
5279 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
5280 EXPECT ("w1::w2"));
5281 }
5282
5283 /* Same, with a "complicated" symbol. */
5284 {
5285 static const char str[] = Z_SYM_NAME;
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 (Z_SYM_NAME));
5294 }
5295 }
5296
5297 /* In FULL mode, an incomplete symbol doesn't match. */
5298 {
5299 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
5300 {});
5301 }
5302
5303 /* A complete symbol with parameters matches any overload, since the
5304 index has no overload info. */
5305 {
5306 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
5307 EXPECT ("std::zfunction", "std::zfunction2"));
5308 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
5309 EXPECT ("std::zfunction", "std::zfunction2"));
5310 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
5311 EXPECT ("std::zfunction", "std::zfunction2"));
5312 }
5313
5314 /* Check that whitespace is ignored appropriately. A symbol with a
5315 template argument list. */
5316 {
5317 static const char expected[] = "ns::foo<int>";
5318 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
5319 EXPECT (expected));
5320 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
5321 EXPECT (expected));
5322 }
5323
5324 /* Check that whitespace is ignored appropriately. A symbol with a
5325 template argument list that includes a pointer. */
5326 {
5327 static const char expected[] = "ns::foo<char*>";
5328 /* Try both completion and non-completion modes. */
5329 static const bool completion_mode[2] = {false, true};
5330 for (size_t i = 0; i < 2; i++)
5331 {
5332 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
5333 completion_mode[i], EXPECT (expected));
5334 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
5335 completion_mode[i], EXPECT (expected));
5336
5337 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
5338 completion_mode[i], EXPECT (expected));
5339 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
5340 completion_mode[i], EXPECT (expected));
5341 }
5342 }
5343
5344 {
5345 /* Check method qualifiers are ignored. */
5346 static const char expected[] = "ns::foo<char*>";
5347 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
5348 symbol_name_match_type::FULL, true, EXPECT (expected));
5349 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
5350 symbol_name_match_type::FULL, true, EXPECT (expected));
5351 CHECK_MATCH ("foo < char * > ( int ) const",
5352 symbol_name_match_type::WILD, true, EXPECT (expected));
5353 CHECK_MATCH ("foo < char * > ( int ) &&",
5354 symbol_name_match_type::WILD, true, EXPECT (expected));
5355 }
5356
5357 /* Test lookup names that don't match anything. */
5358 {
5359 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
5360 {});
5361
5362 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
5363 {});
5364 }
5365
5366 /* Some wild matching tests, exercising "(anonymous namespace)",
5367 which should not be confused with a parameter list. */
5368 {
5369 static const char *syms[] = {
5370 "A::B::C",
5371 "B::C",
5372 "C",
5373 "A :: B :: C ( int )",
5374 "B :: C ( int )",
5375 "C ( int )",
5376 };
5377
5378 for (const char *s : syms)
5379 {
5380 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
5381 EXPECT ("(anonymous namespace)::A::B::C"));
5382 }
5383 }
5384
5385 {
5386 static const char expected[] = "ns2::tmpl<int>::foo2";
5387 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
5388 EXPECT (expected));
5389 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
5390 EXPECT (expected));
5391 }
5392
5393 SELF_CHECK (!any_mismatch);
5394
5395 #undef EXPECT
5396 #undef CHECK_MATCH
5397 }
5398
5399 static void
5400 run_test ()
5401 {
5402 test_mapped_index_find_name_component_bounds ();
5403 test_dw2_expand_symtabs_matching_symbol ();
5404 }
5405
5406 }} // namespace selftests::dw2_expand_symtabs_matching
5407
5408 #endif /* GDB_SELF_TEST */
5409
5410 /* If FILE_MATCHER is NULL or if PER_CU has
5411 dwarf2_per_cu_quick_data::MARK set (see
5412 dw_expand_symtabs_matching_file_matcher), expand the CU and call
5413 EXPANSION_NOTIFY on it. */
5414
5415 static void
5416 dw2_expand_symtabs_matching_one
5417 (struct dwarf2_per_cu_data *per_cu,
5418 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5419 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
5420 {
5421 if (file_matcher == NULL || per_cu->v.quick->mark)
5422 {
5423 bool symtab_was_null
5424 = (per_cu->v.quick->compunit_symtab == NULL);
5425
5426 dw2_instantiate_symtab (per_cu);
5427
5428 if (expansion_notify != NULL
5429 && symtab_was_null
5430 && per_cu->v.quick->compunit_symtab != NULL)
5431 expansion_notify (per_cu->v.quick->compunit_symtab);
5432 }
5433 }
5434
5435 /* Helper for dw2_expand_matching symtabs. Called on each symbol
5436 matched, to expand corresponding CUs that were marked. IDX is the
5437 index of the symbol name that matched. */
5438
5439 static void
5440 dw2_expand_marked_cus
5441 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
5442 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5443 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5444 search_domain kind)
5445 {
5446 offset_type *vec, vec_len, vec_idx;
5447 bool global_seen = false;
5448 mapped_index &index = *dwarf2_per_objfile->index_table;
5449
5450 vec = (offset_type *) (index.constant_pool
5451 + MAYBE_SWAP (index.symbol_table[idx].vec));
5452 vec_len = MAYBE_SWAP (vec[0]);
5453 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
5454 {
5455 struct dwarf2_per_cu_data *per_cu;
5456 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
5457 /* This value is only valid for index versions >= 7. */
5458 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
5459 gdb_index_symbol_kind symbol_kind =
5460 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
5461 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
5462 /* Only check the symbol attributes if they're present.
5463 Indices prior to version 7 don't record them,
5464 and indices >= 7 may elide them for certain symbols
5465 (gold does this). */
5466 int attrs_valid =
5467 (index.version >= 7
5468 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
5469
5470 /* Work around gold/15646. */
5471 if (attrs_valid)
5472 {
5473 if (!is_static && global_seen)
5474 continue;
5475 if (!is_static)
5476 global_seen = true;
5477 }
5478
5479 /* Only check the symbol's kind if it has one. */
5480 if (attrs_valid)
5481 {
5482 switch (kind)
5483 {
5484 case VARIABLES_DOMAIN:
5485 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
5486 continue;
5487 break;
5488 case FUNCTIONS_DOMAIN:
5489 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
5490 continue;
5491 break;
5492 case TYPES_DOMAIN:
5493 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
5494 continue;
5495 break;
5496 default:
5497 break;
5498 }
5499 }
5500
5501 /* Don't crash on bad data. */
5502 if (cu_index >= (dwarf2_per_objfile->n_comp_units
5503 + dwarf2_per_objfile->n_type_units))
5504 {
5505 complaint (&symfile_complaints,
5506 _(".gdb_index entry has bad CU index"
5507 " [in module %s]"),
5508 objfile_name (dwarf2_per_objfile->objfile));
5509 continue;
5510 }
5511
5512 per_cu = dw2_get_cutu (dwarf2_per_objfile, cu_index);
5513 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5514 expansion_notify);
5515 }
5516 }
5517
5518 /* If FILE_MATCHER is non-NULL, set all the
5519 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
5520 that match FILE_MATCHER. */
5521
5522 static void
5523 dw_expand_symtabs_matching_file_matcher
5524 (struct dwarf2_per_objfile *dwarf2_per_objfile,
5525 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
5526 {
5527 if (file_matcher == NULL)
5528 return;
5529
5530 objfile *const objfile = dwarf2_per_objfile->objfile;
5531
5532 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
5533 htab_eq_pointer,
5534 NULL, xcalloc, xfree));
5535 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
5536 htab_eq_pointer,
5537 NULL, xcalloc, xfree));
5538
5539 /* The rule is CUs specify all the files, including those used by
5540 any TU, so there's no need to scan TUs here. */
5541
5542 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5543 {
5544 int j;
5545 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (dwarf2_per_objfile, i);
5546 struct quick_file_names *file_data;
5547 void **slot;
5548
5549 QUIT;
5550
5551 per_cu->v.quick->mark = 0;
5552
5553 /* We only need to look at symtabs not already expanded. */
5554 if (per_cu->v.quick->compunit_symtab)
5555 continue;
5556
5557 file_data = dw2_get_file_names (per_cu);
5558 if (file_data == NULL)
5559 continue;
5560
5561 if (htab_find (visited_not_found.get (), file_data) != NULL)
5562 continue;
5563 else if (htab_find (visited_found.get (), file_data) != NULL)
5564 {
5565 per_cu->v.quick->mark = 1;
5566 continue;
5567 }
5568
5569 for (j = 0; j < file_data->num_file_names; ++j)
5570 {
5571 const char *this_real_name;
5572
5573 if (file_matcher (file_data->file_names[j], false))
5574 {
5575 per_cu->v.quick->mark = 1;
5576 break;
5577 }
5578
5579 /* Before we invoke realpath, which can get expensive when many
5580 files are involved, do a quick comparison of the basenames. */
5581 if (!basenames_may_differ
5582 && !file_matcher (lbasename (file_data->file_names[j]),
5583 true))
5584 continue;
5585
5586 this_real_name = dw2_get_real_path (objfile, file_data, j);
5587 if (file_matcher (this_real_name, false))
5588 {
5589 per_cu->v.quick->mark = 1;
5590 break;
5591 }
5592 }
5593
5594 slot = htab_find_slot (per_cu->v.quick->mark
5595 ? visited_found.get ()
5596 : visited_not_found.get (),
5597 file_data, INSERT);
5598 *slot = file_data;
5599 }
5600 }
5601
5602 static void
5603 dw2_expand_symtabs_matching
5604 (struct objfile *objfile,
5605 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5606 const lookup_name_info &lookup_name,
5607 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5608 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5609 enum search_domain kind)
5610 {
5611 struct dwarf2_per_objfile *dwarf2_per_objfile
5612 = get_dwarf2_per_objfile (objfile);
5613
5614 /* index_table is NULL if OBJF_READNOW. */
5615 if (!dwarf2_per_objfile->index_table)
5616 return;
5617
5618 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
5619
5620 mapped_index &index = *dwarf2_per_objfile->index_table;
5621
5622 dw2_expand_symtabs_matching_symbol (index, lookup_name,
5623 symbol_matcher,
5624 kind, [&] (offset_type idx)
5625 {
5626 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
5627 expansion_notify, kind);
5628 });
5629 }
5630
5631 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
5632 symtab. */
5633
5634 static struct compunit_symtab *
5635 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
5636 CORE_ADDR pc)
5637 {
5638 int i;
5639
5640 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
5641 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
5642 return cust;
5643
5644 if (cust->includes == NULL)
5645 return NULL;
5646
5647 for (i = 0; cust->includes[i]; ++i)
5648 {
5649 struct compunit_symtab *s = cust->includes[i];
5650
5651 s = recursively_find_pc_sect_compunit_symtab (s, pc);
5652 if (s != NULL)
5653 return s;
5654 }
5655
5656 return NULL;
5657 }
5658
5659 static struct compunit_symtab *
5660 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
5661 struct bound_minimal_symbol msymbol,
5662 CORE_ADDR pc,
5663 struct obj_section *section,
5664 int warn_if_readin)
5665 {
5666 struct dwarf2_per_cu_data *data;
5667 struct compunit_symtab *result;
5668
5669 if (!objfile->psymtabs_addrmap)
5670 return NULL;
5671
5672 data = (struct dwarf2_per_cu_data *) addrmap_find (objfile->psymtabs_addrmap,
5673 pc);
5674 if (!data)
5675 return NULL;
5676
5677 if (warn_if_readin && data->v.quick->compunit_symtab)
5678 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
5679 paddress (get_objfile_arch (objfile), pc));
5680
5681 result
5682 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data),
5683 pc);
5684 gdb_assert (result != NULL);
5685 return result;
5686 }
5687
5688 static void
5689 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
5690 void *data, int need_fullname)
5691 {
5692 struct dwarf2_per_objfile *dwarf2_per_objfile
5693 = get_dwarf2_per_objfile (objfile);
5694
5695 if (!dwarf2_per_objfile->filenames_cache)
5696 {
5697 dwarf2_per_objfile->filenames_cache.emplace ();
5698
5699 htab_up visited (htab_create_alloc (10,
5700 htab_hash_pointer, htab_eq_pointer,
5701 NULL, xcalloc, xfree));
5702
5703 /* The rule is CUs specify all the files, including those used
5704 by any TU, so there's no need to scan TUs here. We can
5705 ignore file names coming from already-expanded CUs. */
5706
5707 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5708 {
5709 dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
5710
5711 if (per_cu->v.quick->compunit_symtab)
5712 {
5713 void **slot = htab_find_slot (visited.get (),
5714 per_cu->v.quick->file_names,
5715 INSERT);
5716
5717 *slot = per_cu->v.quick->file_names;
5718 }
5719 }
5720
5721 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5722 {
5723 dwarf2_per_cu_data *per_cu = dw2_get_cu (dwarf2_per_objfile, i);
5724 struct quick_file_names *file_data;
5725 void **slot;
5726
5727 /* We only need to look at symtabs not already expanded. */
5728 if (per_cu->v.quick->compunit_symtab)
5729 continue;
5730
5731 file_data = dw2_get_file_names (per_cu);
5732 if (file_data == NULL)
5733 continue;
5734
5735 slot = htab_find_slot (visited.get (), file_data, INSERT);
5736 if (*slot)
5737 {
5738 /* Already visited. */
5739 continue;
5740 }
5741 *slot = file_data;
5742
5743 for (int j = 0; j < file_data->num_file_names; ++j)
5744 {
5745 const char *filename = file_data->file_names[j];
5746 dwarf2_per_objfile->filenames_cache->seen (filename);
5747 }
5748 }
5749 }
5750
5751 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
5752 {
5753 gdb::unique_xmalloc_ptr<char> this_real_name;
5754
5755 if (need_fullname)
5756 this_real_name = gdb_realpath (filename);
5757 (*fun) (filename, this_real_name.get (), data);
5758 });
5759 }
5760
5761 static int
5762 dw2_has_symbols (struct objfile *objfile)
5763 {
5764 return 1;
5765 }
5766
5767 const struct quick_symbol_functions dwarf2_gdb_index_functions =
5768 {
5769 dw2_has_symbols,
5770 dw2_find_last_source_symtab,
5771 dw2_forget_cached_source_info,
5772 dw2_map_symtabs_matching_filename,
5773 dw2_lookup_symbol,
5774 dw2_print_stats,
5775 dw2_dump,
5776 dw2_relocate,
5777 dw2_expand_symtabs_for_function,
5778 dw2_expand_all_symtabs,
5779 dw2_expand_symtabs_with_fullname,
5780 dw2_map_matching_symbols,
5781 dw2_expand_symtabs_matching,
5782 dw2_find_pc_sect_compunit_symtab,
5783 NULL,
5784 dw2_map_symbol_filenames
5785 };
5786
5787 /* DWARF-5 debug_names reader. */
5788
5789 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
5790 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
5791
5792 /* A helper function that reads the .debug_names section in SECTION
5793 and fills in MAP. FILENAME is the name of the file containing the
5794 section; it is used for error reporting.
5795
5796 Returns true if all went well, false otherwise. */
5797
5798 static bool
5799 read_debug_names_from_section (struct objfile *objfile,
5800 const char *filename,
5801 struct dwarf2_section_info *section,
5802 mapped_debug_names &map)
5803 {
5804 if (dwarf2_section_empty_p (section))
5805 return false;
5806
5807 /* Older elfutils strip versions could keep the section in the main
5808 executable while splitting it for the separate debug info file. */
5809 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
5810 return false;
5811
5812 dwarf2_read_section (objfile, section);
5813
5814 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
5815
5816 const gdb_byte *addr = section->buffer;
5817
5818 bfd *const abfd = get_section_bfd_owner (section);
5819
5820 unsigned int bytes_read;
5821 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5822 addr += bytes_read;
5823
5824 map.dwarf5_is_dwarf64 = bytes_read != 4;
5825 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5826 if (bytes_read + length != section->size)
5827 {
5828 /* There may be multiple per-CU indices. */
5829 warning (_("Section .debug_names in %s length %s does not match "
5830 "section length %s, ignoring .debug_names."),
5831 filename, plongest (bytes_read + length),
5832 pulongest (section->size));
5833 return false;
5834 }
5835
5836 /* The version number. */
5837 uint16_t version = read_2_bytes (abfd, addr);
5838 addr += 2;
5839 if (version != 5)
5840 {
5841 warning (_("Section .debug_names in %s has unsupported version %d, "
5842 "ignoring .debug_names."),
5843 filename, version);
5844 return false;
5845 }
5846
5847 /* Padding. */
5848 uint16_t padding = read_2_bytes (abfd, addr);
5849 addr += 2;
5850 if (padding != 0)
5851 {
5852 warning (_("Section .debug_names in %s has unsupported padding %d, "
5853 "ignoring .debug_names."),
5854 filename, padding);
5855 return false;
5856 }
5857
5858 /* comp_unit_count - The number of CUs in the CU list. */
5859 map.cu_count = read_4_bytes (abfd, addr);
5860 addr += 4;
5861
5862 /* local_type_unit_count - The number of TUs in the local TU
5863 list. */
5864 map.tu_count = read_4_bytes (abfd, addr);
5865 addr += 4;
5866
5867 /* foreign_type_unit_count - The number of TUs in the foreign TU
5868 list. */
5869 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5870 addr += 4;
5871 if (foreign_tu_count != 0)
5872 {
5873 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5874 "ignoring .debug_names."),
5875 filename, static_cast<unsigned long> (foreign_tu_count));
5876 return false;
5877 }
5878
5879 /* bucket_count - The number of hash buckets in the hash lookup
5880 table. */
5881 map.bucket_count = read_4_bytes (abfd, addr);
5882 addr += 4;
5883
5884 /* name_count - The number of unique names in the index. */
5885 map.name_count = read_4_bytes (abfd, addr);
5886 addr += 4;
5887
5888 /* abbrev_table_size - The size in bytes of the abbreviations
5889 table. */
5890 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5891 addr += 4;
5892
5893 /* augmentation_string_size - The size in bytes of the augmentation
5894 string. This value is rounded up to a multiple of 4. */
5895 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5896 addr += 4;
5897 map.augmentation_is_gdb = ((augmentation_string_size
5898 == sizeof (dwarf5_augmentation))
5899 && memcmp (addr, dwarf5_augmentation,
5900 sizeof (dwarf5_augmentation)) == 0);
5901 augmentation_string_size += (-augmentation_string_size) & 3;
5902 addr += augmentation_string_size;
5903
5904 /* List of CUs */
5905 map.cu_table_reordered = addr;
5906 addr += map.cu_count * map.offset_size;
5907
5908 /* List of Local TUs */
5909 map.tu_table_reordered = addr;
5910 addr += map.tu_count * map.offset_size;
5911
5912 /* Hash Lookup Table */
5913 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5914 addr += map.bucket_count * 4;
5915 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5916 addr += map.name_count * 4;
5917
5918 /* Name Table */
5919 map.name_table_string_offs_reordered = addr;
5920 addr += map.name_count * map.offset_size;
5921 map.name_table_entry_offs_reordered = addr;
5922 addr += map.name_count * map.offset_size;
5923
5924 const gdb_byte *abbrev_table_start = addr;
5925 for (;;)
5926 {
5927 unsigned int bytes_read;
5928 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5929 addr += bytes_read;
5930 if (index_num == 0)
5931 break;
5932
5933 const auto insertpair
5934 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5935 if (!insertpair.second)
5936 {
5937 warning (_("Section .debug_names in %s has duplicate index %s, "
5938 "ignoring .debug_names."),
5939 filename, pulongest (index_num));
5940 return false;
5941 }
5942 mapped_debug_names::index_val &indexval = insertpair.first->second;
5943 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5944 addr += bytes_read;
5945
5946 for (;;)
5947 {
5948 mapped_debug_names::index_val::attr attr;
5949 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5950 addr += bytes_read;
5951 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5952 addr += bytes_read;
5953 if (attr.form == DW_FORM_implicit_const)
5954 {
5955 attr.implicit_const = read_signed_leb128 (abfd, addr,
5956 &bytes_read);
5957 addr += bytes_read;
5958 }
5959 if (attr.dw_idx == 0 && attr.form == 0)
5960 break;
5961 indexval.attr_vec.push_back (std::move (attr));
5962 }
5963 }
5964 if (addr != abbrev_table_start + abbrev_table_size)
5965 {
5966 warning (_("Section .debug_names in %s has abbreviation_table "
5967 "of size %zu vs. written as %u, ignoring .debug_names."),
5968 filename, addr - abbrev_table_start, abbrev_table_size);
5969 return false;
5970 }
5971 map.entry_pool = addr;
5972
5973 return true;
5974 }
5975
5976 /* A helper for create_cus_from_debug_names that handles the MAP's CU
5977 list. */
5978
5979 static void
5980 create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
5981 const mapped_debug_names &map,
5982 dwarf2_section_info &section,
5983 bool is_dwz, int base_offset)
5984 {
5985 sect_offset sect_off_prev;
5986 for (uint32_t i = 0; i <= map.cu_count; ++i)
5987 {
5988 sect_offset sect_off_next;
5989 if (i < map.cu_count)
5990 {
5991 sect_off_next
5992 = (sect_offset) (extract_unsigned_integer
5993 (map.cu_table_reordered + i * map.offset_size,
5994 map.offset_size,
5995 map.dwarf5_byte_order));
5996 }
5997 else
5998 sect_off_next = (sect_offset) section.size;
5999 if (i >= 1)
6000 {
6001 const ULONGEST length = sect_off_next - sect_off_prev;
6002 dwarf2_per_objfile->all_comp_units[base_offset + (i - 1)]
6003 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
6004 sect_off_prev, length);
6005 }
6006 sect_off_prev = sect_off_next;
6007 }
6008 }
6009
6010 /* Read the CU list from the mapped index, and use it to create all
6011 the CU objects for this dwarf2_per_objfile. */
6012
6013 static void
6014 create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
6015 const mapped_debug_names &map,
6016 const mapped_debug_names &dwz_map)
6017 {
6018 struct objfile *objfile = dwarf2_per_objfile->objfile;
6019
6020 dwarf2_per_objfile->n_comp_units = map.cu_count + dwz_map.cu_count;
6021 dwarf2_per_objfile->all_comp_units
6022 = XOBNEWVEC (&objfile->objfile_obstack, struct dwarf2_per_cu_data *,
6023 dwarf2_per_objfile->n_comp_units);
6024
6025 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
6026 dwarf2_per_objfile->info,
6027 false /* is_dwz */,
6028 0 /* base_offset */);
6029
6030 if (dwz_map.cu_count == 0)
6031 return;
6032
6033 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
6034 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
6035 true /* is_dwz */,
6036 map.cu_count /* base_offset */);
6037 }
6038
6039 /* Read .debug_names. If everything went ok, initialize the "quick"
6040 elements of all the CUs and return true. Otherwise, return false. */
6041
6042 static bool
6043 dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
6044 {
6045 mapped_debug_names local_map (dwarf2_per_objfile);
6046 mapped_debug_names dwz_map (dwarf2_per_objfile);
6047 struct objfile *objfile = dwarf2_per_objfile->objfile;
6048
6049 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
6050 &dwarf2_per_objfile->debug_names,
6051 local_map))
6052 return false;
6053
6054 /* Don't use the index if it's empty. */
6055 if (local_map.name_count == 0)
6056 return false;
6057
6058 /* If there is a .dwz file, read it so we can get its CU list as
6059 well. */
6060 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
6061 if (dwz != NULL)
6062 {
6063 if (!read_debug_names_from_section (objfile,
6064 bfd_get_filename (dwz->dwz_bfd),
6065 &dwz->debug_names, dwz_map))
6066 {
6067 warning (_("could not read '.debug_names' section from %s; skipping"),
6068 bfd_get_filename (dwz->dwz_bfd));
6069 return false;
6070 }
6071 }
6072
6073 create_cus_from_debug_names (dwarf2_per_objfile, local_map, dwz_map);
6074
6075 if (local_map.tu_count != 0)
6076 {
6077 /* We can only handle a single .debug_types when we have an
6078 index. */
6079 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
6080 return false;
6081
6082 dwarf2_section_info *section = VEC_index (dwarf2_section_info_def,
6083 dwarf2_per_objfile->types, 0);
6084
6085 create_signatured_type_table_from_debug_names
6086 (dwarf2_per_objfile, local_map, section, &dwarf2_per_objfile->abbrev);
6087 }
6088
6089 create_addrmap_from_aranges (dwarf2_per_objfile,
6090 &dwarf2_per_objfile->debug_aranges);
6091
6092 dwarf2_per_objfile->debug_names_table.reset
6093 (new mapped_debug_names (dwarf2_per_objfile));
6094 *dwarf2_per_objfile->debug_names_table = std::move (local_map);
6095 dwarf2_per_objfile->using_index = 1;
6096 dwarf2_per_objfile->quick_file_names_table =
6097 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
6098
6099 return true;
6100 }
6101
6102 /* Symbol name hashing function as specified by DWARF-5. */
6103
6104 static uint32_t
6105 dwarf5_djb_hash (const char *str_)
6106 {
6107 const unsigned char *str = (const unsigned char *) str_;
6108
6109 /* Note: tolower here ignores UTF-8, which isn't fully compliant.
6110 See http://dwarfstd.org/ShowIssue.php?issue=161027.1. */
6111
6112 uint32_t hash = 5381;
6113 while (int c = *str++)
6114 hash = hash * 33 + tolower (c);
6115 return hash;
6116 }
6117
6118 /* Type used to manage iterating over all CUs looking for a symbol for
6119 .debug_names. */
6120
6121 class dw2_debug_names_iterator
6122 {
6123 public:
6124 /* If WANT_SPECIFIC_BLOCK is true, only look for symbols in block
6125 BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
6126 dw2_debug_names_iterator (const mapped_debug_names &map,
6127 bool want_specific_block,
6128 block_enum block_index, domain_enum domain,
6129 const char *name)
6130 : m_map (map), m_want_specific_block (want_specific_block),
6131 m_block_index (block_index), m_domain (domain),
6132 m_addr (find_vec_in_debug_names (map, name))
6133 {}
6134
6135 dw2_debug_names_iterator (const mapped_debug_names &map,
6136 search_domain search, uint32_t namei)
6137 : m_map (map),
6138 m_search (search),
6139 m_addr (find_vec_in_debug_names (map, namei))
6140 {}
6141
6142 /* Return the next matching CU or NULL if there are no more. */
6143 dwarf2_per_cu_data *next ();
6144
6145 private:
6146 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
6147 const char *name);
6148 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
6149 uint32_t namei);
6150
6151 /* The internalized form of .debug_names. */
6152 const mapped_debug_names &m_map;
6153
6154 /* If true, only look for symbols that match BLOCK_INDEX. */
6155 const bool m_want_specific_block = false;
6156
6157 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
6158 Unused if !WANT_SPECIFIC_BLOCK - FIRST_LOCAL_BLOCK is an invalid
6159 value. */
6160 const block_enum m_block_index = FIRST_LOCAL_BLOCK;
6161
6162 /* The kind of symbol we're looking for. */
6163 const domain_enum m_domain = UNDEF_DOMAIN;
6164 const search_domain m_search = ALL_DOMAIN;
6165
6166 /* The list of CUs from the index entry of the symbol, or NULL if
6167 not found. */
6168 const gdb_byte *m_addr;
6169 };
6170
6171 const char *
6172 mapped_debug_names::namei_to_name (uint32_t namei) const
6173 {
6174 const ULONGEST namei_string_offs
6175 = extract_unsigned_integer ((name_table_string_offs_reordered
6176 + namei * offset_size),
6177 offset_size,
6178 dwarf5_byte_order);
6179 return read_indirect_string_at_offset
6180 (dwarf2_per_objfile, dwarf2_per_objfile->objfile->obfd, namei_string_offs);
6181 }
6182
6183 /* Find a slot in .debug_names for the object named NAME. If NAME is
6184 found, return pointer to its pool data. If NAME cannot be found,
6185 return NULL. */
6186
6187 const gdb_byte *
6188 dw2_debug_names_iterator::find_vec_in_debug_names
6189 (const mapped_debug_names &map, const char *name)
6190 {
6191 int (*cmp) (const char *, const char *);
6192
6193 if (current_language->la_language == language_cplus
6194 || current_language->la_language == language_fortran
6195 || current_language->la_language == language_d)
6196 {
6197 /* NAME is already canonical. Drop any qualifiers as
6198 .debug_names does not contain any. */
6199
6200 if (strchr (name, '(') != NULL)
6201 {
6202 gdb::unique_xmalloc_ptr<char> without_params
6203 = cp_remove_params (name);
6204
6205 if (without_params != NULL)
6206 {
6207 name = without_params.get();
6208 }
6209 }
6210 }
6211
6212 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
6213
6214 const uint32_t full_hash = dwarf5_djb_hash (name);
6215 uint32_t namei
6216 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
6217 (map.bucket_table_reordered
6218 + (full_hash % map.bucket_count)), 4,
6219 map.dwarf5_byte_order);
6220 if (namei == 0)
6221 return NULL;
6222 --namei;
6223 if (namei >= map.name_count)
6224 {
6225 complaint (&symfile_complaints,
6226 _("Wrong .debug_names with name index %u but name_count=%u "
6227 "[in module %s]"),
6228 namei, map.name_count,
6229 objfile_name (map.dwarf2_per_objfile->objfile));
6230 return NULL;
6231 }
6232
6233 for (;;)
6234 {
6235 const uint32_t namei_full_hash
6236 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
6237 (map.hash_table_reordered + namei), 4,
6238 map.dwarf5_byte_order);
6239 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
6240 return NULL;
6241
6242 if (full_hash == namei_full_hash)
6243 {
6244 const char *const namei_string = map.namei_to_name (namei);
6245
6246 #if 0 /* An expensive sanity check. */
6247 if (namei_full_hash != dwarf5_djb_hash (namei_string))
6248 {
6249 complaint (&symfile_complaints,
6250 _("Wrong .debug_names hash for string at index %u "
6251 "[in module %s]"),
6252 namei, objfile_name (dwarf2_per_objfile->objfile));
6253 return NULL;
6254 }
6255 #endif
6256
6257 if (cmp (namei_string, name) == 0)
6258 {
6259 const ULONGEST namei_entry_offs
6260 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
6261 + namei * map.offset_size),
6262 map.offset_size, map.dwarf5_byte_order);
6263 return map.entry_pool + namei_entry_offs;
6264 }
6265 }
6266
6267 ++namei;
6268 if (namei >= map.name_count)
6269 return NULL;
6270 }
6271 }
6272
6273 const gdb_byte *
6274 dw2_debug_names_iterator::find_vec_in_debug_names
6275 (const mapped_debug_names &map, uint32_t namei)
6276 {
6277 if (namei >= map.name_count)
6278 {
6279 complaint (&symfile_complaints,
6280 _("Wrong .debug_names with name index %u but name_count=%u "
6281 "[in module %s]"),
6282 namei, map.name_count,
6283 objfile_name (map.dwarf2_per_objfile->objfile));
6284 return NULL;
6285 }
6286
6287 const ULONGEST namei_entry_offs
6288 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
6289 + namei * map.offset_size),
6290 map.offset_size, map.dwarf5_byte_order);
6291 return map.entry_pool + namei_entry_offs;
6292 }
6293
6294 /* See dw2_debug_names_iterator. */
6295
6296 dwarf2_per_cu_data *
6297 dw2_debug_names_iterator::next ()
6298 {
6299 if (m_addr == NULL)
6300 return NULL;
6301
6302 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
6303 struct objfile *objfile = dwarf2_per_objfile->objfile;
6304 bfd *const abfd = objfile->obfd;
6305
6306 again:
6307
6308 unsigned int bytes_read;
6309 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
6310 m_addr += bytes_read;
6311 if (abbrev == 0)
6312 return NULL;
6313
6314 const auto indexval_it = m_map.abbrev_map.find (abbrev);
6315 if (indexval_it == m_map.abbrev_map.cend ())
6316 {
6317 complaint (&symfile_complaints,
6318 _("Wrong .debug_names undefined abbrev code %s "
6319 "[in module %s]"),
6320 pulongest (abbrev), objfile_name (objfile));
6321 return NULL;
6322 }
6323 const mapped_debug_names::index_val &indexval = indexval_it->second;
6324 bool have_is_static = false;
6325 bool is_static;
6326 dwarf2_per_cu_data *per_cu = NULL;
6327 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
6328 {
6329 ULONGEST ull;
6330 switch (attr.form)
6331 {
6332 case DW_FORM_implicit_const:
6333 ull = attr.implicit_const;
6334 break;
6335 case DW_FORM_flag_present:
6336 ull = 1;
6337 break;
6338 case DW_FORM_udata:
6339 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
6340 m_addr += bytes_read;
6341 break;
6342 default:
6343 complaint (&symfile_complaints,
6344 _("Unsupported .debug_names form %s [in module %s]"),
6345 dwarf_form_name (attr.form),
6346 objfile_name (objfile));
6347 return NULL;
6348 }
6349 switch (attr.dw_idx)
6350 {
6351 case DW_IDX_compile_unit:
6352 /* Don't crash on bad data. */
6353 if (ull >= dwarf2_per_objfile->n_comp_units)
6354 {
6355 complaint (&symfile_complaints,
6356 _(".debug_names entry has bad CU index %s"
6357 " [in module %s]"),
6358 pulongest (ull),
6359 objfile_name (dwarf2_per_objfile->objfile));
6360 continue;
6361 }
6362 per_cu = dw2_get_cutu (dwarf2_per_objfile, ull);
6363 break;
6364 case DW_IDX_type_unit:
6365 /* Don't crash on bad data. */
6366 if (ull >= dwarf2_per_objfile->n_type_units)
6367 {
6368 complaint (&symfile_complaints,
6369 _(".debug_names entry has bad TU index %s"
6370 " [in module %s]"),
6371 pulongest (ull),
6372 objfile_name (dwarf2_per_objfile->objfile));
6373 continue;
6374 }
6375 per_cu = dw2_get_cutu (dwarf2_per_objfile,
6376 dwarf2_per_objfile->n_comp_units + ull);
6377 break;
6378 case DW_IDX_GNU_internal:
6379 if (!m_map.augmentation_is_gdb)
6380 break;
6381 have_is_static = true;
6382 is_static = true;
6383 break;
6384 case DW_IDX_GNU_external:
6385 if (!m_map.augmentation_is_gdb)
6386 break;
6387 have_is_static = true;
6388 is_static = false;
6389 break;
6390 }
6391 }
6392
6393 /* Skip if already read in. */
6394 if (per_cu->v.quick->compunit_symtab)
6395 goto again;
6396
6397 /* Check static vs global. */
6398 if (have_is_static)
6399 {
6400 const bool want_static = m_block_index != GLOBAL_BLOCK;
6401 if (m_want_specific_block && want_static != is_static)
6402 goto again;
6403 }
6404
6405 /* Match dw2_symtab_iter_next, symbol_kind
6406 and debug_names::psymbol_tag. */
6407 switch (m_domain)
6408 {
6409 case VAR_DOMAIN:
6410 switch (indexval.dwarf_tag)
6411 {
6412 case DW_TAG_variable:
6413 case DW_TAG_subprogram:
6414 /* Some types are also in VAR_DOMAIN. */
6415 case DW_TAG_typedef:
6416 case DW_TAG_structure_type:
6417 break;
6418 default:
6419 goto again;
6420 }
6421 break;
6422 case STRUCT_DOMAIN:
6423 switch (indexval.dwarf_tag)
6424 {
6425 case DW_TAG_typedef:
6426 case DW_TAG_structure_type:
6427 break;
6428 default:
6429 goto again;
6430 }
6431 break;
6432 case LABEL_DOMAIN:
6433 switch (indexval.dwarf_tag)
6434 {
6435 case 0:
6436 case DW_TAG_variable:
6437 break;
6438 default:
6439 goto again;
6440 }
6441 break;
6442 default:
6443 break;
6444 }
6445
6446 /* Match dw2_expand_symtabs_matching, symbol_kind and
6447 debug_names::psymbol_tag. */
6448 switch (m_search)
6449 {
6450 case VARIABLES_DOMAIN:
6451 switch (indexval.dwarf_tag)
6452 {
6453 case DW_TAG_variable:
6454 break;
6455 default:
6456 goto again;
6457 }
6458 break;
6459 case FUNCTIONS_DOMAIN:
6460 switch (indexval.dwarf_tag)
6461 {
6462 case DW_TAG_subprogram:
6463 break;
6464 default:
6465 goto again;
6466 }
6467 break;
6468 case TYPES_DOMAIN:
6469 switch (indexval.dwarf_tag)
6470 {
6471 case DW_TAG_typedef:
6472 case DW_TAG_structure_type:
6473 break;
6474 default:
6475 goto again;
6476 }
6477 break;
6478 default:
6479 break;
6480 }
6481
6482 return per_cu;
6483 }
6484
6485 static struct compunit_symtab *
6486 dw2_debug_names_lookup_symbol (struct objfile *objfile, int block_index_int,
6487 const char *name, domain_enum domain)
6488 {
6489 const block_enum block_index = static_cast<block_enum> (block_index_int);
6490 struct dwarf2_per_objfile *dwarf2_per_objfile
6491 = get_dwarf2_per_objfile (objfile);
6492
6493 const auto &mapp = dwarf2_per_objfile->debug_names_table;
6494 if (!mapp)
6495 {
6496 /* index is NULL if OBJF_READNOW. */
6497 return NULL;
6498 }
6499 const auto &map = *mapp;
6500
6501 dw2_debug_names_iterator iter (map, true /* want_specific_block */,
6502 block_index, domain, name);
6503
6504 struct compunit_symtab *stab_best = NULL;
6505 struct dwarf2_per_cu_data *per_cu;
6506 while ((per_cu = iter.next ()) != NULL)
6507 {
6508 struct symbol *sym, *with_opaque = NULL;
6509 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu);
6510 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
6511 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
6512
6513 sym = block_find_symbol (block, name, domain,
6514 block_find_non_opaque_type_preferred,
6515 &with_opaque);
6516
6517 /* Some caution must be observed with overloaded functions and
6518 methods, since the index will not contain any overload
6519 information (but NAME might contain it). */
6520
6521 if (sym != NULL
6522 && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
6523 return stab;
6524 if (with_opaque != NULL
6525 && strcmp_iw (SYMBOL_SEARCH_NAME (with_opaque), name) == 0)
6526 stab_best = stab;
6527
6528 /* Keep looking through other CUs. */
6529 }
6530
6531 return stab_best;
6532 }
6533
6534 /* This dumps minimal information about .debug_names. It is called
6535 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
6536 uses this to verify that .debug_names has been loaded. */
6537
6538 static void
6539 dw2_debug_names_dump (struct objfile *objfile)
6540 {
6541 struct dwarf2_per_objfile *dwarf2_per_objfile
6542 = get_dwarf2_per_objfile (objfile);
6543
6544 gdb_assert (dwarf2_per_objfile->using_index);
6545 printf_filtered (".debug_names:");
6546 if (dwarf2_per_objfile->debug_names_table)
6547 printf_filtered (" exists\n");
6548 else
6549 printf_filtered (" faked for \"readnow\"\n");
6550 printf_filtered ("\n");
6551 }
6552
6553 static void
6554 dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
6555 const char *func_name)
6556 {
6557 struct dwarf2_per_objfile *dwarf2_per_objfile
6558 = get_dwarf2_per_objfile (objfile);
6559
6560 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
6561 if (dwarf2_per_objfile->debug_names_table)
6562 {
6563 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6564
6565 /* Note: It doesn't matter what we pass for block_index here. */
6566 dw2_debug_names_iterator iter (map, false /* want_specific_block */,
6567 GLOBAL_BLOCK, VAR_DOMAIN, func_name);
6568
6569 struct dwarf2_per_cu_data *per_cu;
6570 while ((per_cu = iter.next ()) != NULL)
6571 dw2_instantiate_symtab (per_cu);
6572 }
6573 }
6574
6575 static void
6576 dw2_debug_names_expand_symtabs_matching
6577 (struct objfile *objfile,
6578 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
6579 const lookup_name_info &lookup_name,
6580 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
6581 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
6582 enum search_domain kind)
6583 {
6584 struct dwarf2_per_objfile *dwarf2_per_objfile
6585 = get_dwarf2_per_objfile (objfile);
6586
6587 /* debug_names_table is NULL if OBJF_READNOW. */
6588 if (!dwarf2_per_objfile->debug_names_table)
6589 return;
6590
6591 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
6592
6593 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6594
6595 dw2_expand_symtabs_matching_symbol (map, lookup_name,
6596 symbol_matcher,
6597 kind, [&] (offset_type namei)
6598 {
6599 /* The name was matched, now expand corresponding CUs that were
6600 marked. */
6601 dw2_debug_names_iterator iter (map, kind, namei);
6602
6603 struct dwarf2_per_cu_data *per_cu;
6604 while ((per_cu = iter.next ()) != NULL)
6605 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
6606 expansion_notify);
6607 });
6608 }
6609
6610 const struct quick_symbol_functions dwarf2_debug_names_functions =
6611 {
6612 dw2_has_symbols,
6613 dw2_find_last_source_symtab,
6614 dw2_forget_cached_source_info,
6615 dw2_map_symtabs_matching_filename,
6616 dw2_debug_names_lookup_symbol,
6617 dw2_print_stats,
6618 dw2_debug_names_dump,
6619 dw2_relocate,
6620 dw2_debug_names_expand_symtabs_for_function,
6621 dw2_expand_all_symtabs,
6622 dw2_expand_symtabs_with_fullname,
6623 dw2_map_matching_symbols,
6624 dw2_debug_names_expand_symtabs_matching,
6625 dw2_find_pc_sect_compunit_symtab,
6626 NULL,
6627 dw2_map_symbol_filenames
6628 };
6629
6630 /* See symfile.h. */
6631
6632 bool
6633 dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
6634 {
6635 struct dwarf2_per_objfile *dwarf2_per_objfile
6636 = get_dwarf2_per_objfile (objfile);
6637
6638 /* If we're about to read full symbols, don't bother with the
6639 indices. In this case we also don't care if some other debug
6640 format is making psymtabs, because they are all about to be
6641 expanded anyway. */
6642 if ((objfile->flags & OBJF_READNOW))
6643 {
6644 int i;
6645
6646 dwarf2_per_objfile->using_index = 1;
6647 create_all_comp_units (dwarf2_per_objfile);
6648 create_all_type_units (dwarf2_per_objfile);
6649 dwarf2_per_objfile->quick_file_names_table =
6650 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
6651
6652 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
6653 + dwarf2_per_objfile->n_type_units); ++i)
6654 {
6655 dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
6656
6657 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6658 struct dwarf2_per_cu_quick_data);
6659 }
6660
6661 /* Return 1 so that gdb sees the "quick" functions. However,
6662 these functions will be no-ops because we will have expanded
6663 all symtabs. */
6664 *index_kind = dw_index_kind::GDB_INDEX;
6665 return true;
6666 }
6667
6668 if (dwarf2_read_debug_names (dwarf2_per_objfile))
6669 {
6670 *index_kind = dw_index_kind::DEBUG_NAMES;
6671 return true;
6672 }
6673
6674 if (dwarf2_read_index (objfile))
6675 {
6676 *index_kind = dw_index_kind::GDB_INDEX;
6677 return true;
6678 }
6679
6680 return false;
6681 }
6682
6683 \f
6684
6685 /* Build a partial symbol table. */
6686
6687 void
6688 dwarf2_build_psymtabs (struct objfile *objfile)
6689 {
6690 struct dwarf2_per_objfile *dwarf2_per_objfile
6691 = get_dwarf2_per_objfile (objfile);
6692
6693 if (objfile->global_psymbols.capacity () == 0
6694 && objfile->static_psymbols.capacity () == 0)
6695 init_psymbol_list (objfile, 1024);
6696
6697 TRY
6698 {
6699 /* This isn't really ideal: all the data we allocate on the
6700 objfile's obstack is still uselessly kept around. However,
6701 freeing it seems unsafe. */
6702 psymtab_discarder psymtabs (objfile);
6703 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
6704 psymtabs.keep ();
6705 }
6706 CATCH (except, RETURN_MASK_ERROR)
6707 {
6708 exception_print (gdb_stderr, except);
6709 }
6710 END_CATCH
6711 }
6712
6713 /* Return the total length of the CU described by HEADER. */
6714
6715 static unsigned int
6716 get_cu_length (const struct comp_unit_head *header)
6717 {
6718 return header->initial_length_size + header->length;
6719 }
6720
6721 /* Return TRUE if SECT_OFF is within CU_HEADER. */
6722
6723 static inline bool
6724 offset_in_cu_p (const comp_unit_head *cu_header, sect_offset sect_off)
6725 {
6726 sect_offset bottom = cu_header->sect_off;
6727 sect_offset top = cu_header->sect_off + get_cu_length (cu_header);
6728
6729 return sect_off >= bottom && sect_off < top;
6730 }
6731
6732 /* Find the base address of the compilation unit for range lists and
6733 location lists. It will normally be specified by DW_AT_low_pc.
6734 In DWARF-3 draft 4, the base address could be overridden by
6735 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6736 compilation units with discontinuous ranges. */
6737
6738 static void
6739 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6740 {
6741 struct attribute *attr;
6742
6743 cu->base_known = 0;
6744 cu->base_address = 0;
6745
6746 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
6747 if (attr)
6748 {
6749 cu->base_address = attr_value_as_address (attr);
6750 cu->base_known = 1;
6751 }
6752 else
6753 {
6754 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6755 if (attr)
6756 {
6757 cu->base_address = attr_value_as_address (attr);
6758 cu->base_known = 1;
6759 }
6760 }
6761 }
6762
6763 /* Read in the comp unit header information from the debug_info at info_ptr.
6764 Use rcuh_kind::COMPILE as the default type if not known by the caller.
6765 NOTE: This leaves members offset, first_die_offset to be filled in
6766 by the caller. */
6767
6768 static const gdb_byte *
6769 read_comp_unit_head (struct comp_unit_head *cu_header,
6770 const gdb_byte *info_ptr,
6771 struct dwarf2_section_info *section,
6772 rcuh_kind section_kind)
6773 {
6774 int signed_addr;
6775 unsigned int bytes_read;
6776 const char *filename = get_section_file_name (section);
6777 bfd *abfd = get_section_bfd_owner (section);
6778
6779 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
6780 cu_header->initial_length_size = bytes_read;
6781 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
6782 info_ptr += bytes_read;
6783 cu_header->version = read_2_bytes (abfd, info_ptr);
6784 info_ptr += 2;
6785 if (cu_header->version < 5)
6786 switch (section_kind)
6787 {
6788 case rcuh_kind::COMPILE:
6789 cu_header->unit_type = DW_UT_compile;
6790 break;
6791 case rcuh_kind::TYPE:
6792 cu_header->unit_type = DW_UT_type;
6793 break;
6794 default:
6795 internal_error (__FILE__, __LINE__,
6796 _("read_comp_unit_head: invalid section_kind"));
6797 }
6798 else
6799 {
6800 cu_header->unit_type = static_cast<enum dwarf_unit_type>
6801 (read_1_byte (abfd, info_ptr));
6802 info_ptr += 1;
6803 switch (cu_header->unit_type)
6804 {
6805 case DW_UT_compile:
6806 if (section_kind != rcuh_kind::COMPILE)
6807 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6808 "(is DW_UT_compile, should be DW_UT_type) [in module %s]"),
6809 filename);
6810 break;
6811 case DW_UT_type:
6812 section_kind = rcuh_kind::TYPE;
6813 break;
6814 default:
6815 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6816 "(is %d, should be %d or %d) [in module %s]"),
6817 cu_header->unit_type, DW_UT_compile, DW_UT_type, filename);
6818 }
6819
6820 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6821 info_ptr += 1;
6822 }
6823 cu_header->abbrev_sect_off = (sect_offset) read_offset (abfd, info_ptr,
6824 cu_header,
6825 &bytes_read);
6826 info_ptr += bytes_read;
6827 if (cu_header->version < 5)
6828 {
6829 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6830 info_ptr += 1;
6831 }
6832 signed_addr = bfd_get_sign_extend_vma (abfd);
6833 if (signed_addr < 0)
6834 internal_error (__FILE__, __LINE__,
6835 _("read_comp_unit_head: dwarf from non elf file"));
6836 cu_header->signed_addr_p = signed_addr;
6837
6838 if (section_kind == rcuh_kind::TYPE)
6839 {
6840 LONGEST type_offset;
6841
6842 cu_header->signature = read_8_bytes (abfd, info_ptr);
6843 info_ptr += 8;
6844
6845 type_offset = read_offset (abfd, info_ptr, cu_header, &bytes_read);
6846 info_ptr += bytes_read;
6847 cu_header->type_cu_offset_in_tu = (cu_offset) type_offset;
6848 if (to_underlying (cu_header->type_cu_offset_in_tu) != type_offset)
6849 error (_("Dwarf Error: Too big type_offset in compilation unit "
6850 "header (is %s) [in module %s]"), plongest (type_offset),
6851 filename);
6852 }
6853
6854 return info_ptr;
6855 }
6856
6857 /* Helper function that returns the proper abbrev section for
6858 THIS_CU. */
6859
6860 static struct dwarf2_section_info *
6861 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6862 {
6863 struct dwarf2_section_info *abbrev;
6864 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6865
6866 if (this_cu->is_dwz)
6867 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
6868 else
6869 abbrev = &dwarf2_per_objfile->abbrev;
6870
6871 return abbrev;
6872 }
6873
6874 /* Subroutine of read_and_check_comp_unit_head and
6875 read_and_check_type_unit_head to simplify them.
6876 Perform various error checking on the header. */
6877
6878 static void
6879 error_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6880 struct comp_unit_head *header,
6881 struct dwarf2_section_info *section,
6882 struct dwarf2_section_info *abbrev_section)
6883 {
6884 const char *filename = get_section_file_name (section);
6885
6886 if (header->version < 2 || header->version > 5)
6887 error (_("Dwarf Error: wrong version in compilation unit header "
6888 "(is %d, should be 2, 3, 4 or 5) [in module %s]"), header->version,
6889 filename);
6890
6891 if (to_underlying (header->abbrev_sect_off)
6892 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
6893 error (_("Dwarf Error: bad offset (%s) in compilation unit header "
6894 "(offset %s + 6) [in module %s]"),
6895 sect_offset_str (header->abbrev_sect_off),
6896 sect_offset_str (header->sect_off),
6897 filename);
6898
6899 /* Cast to ULONGEST to use 64-bit arithmetic when possible to
6900 avoid potential 32-bit overflow. */
6901 if (((ULONGEST) header->sect_off + get_cu_length (header))
6902 > section->size)
6903 error (_("Dwarf Error: bad length (0x%x) in compilation unit header "
6904 "(offset %s + 0) [in module %s]"),
6905 header->length, sect_offset_str (header->sect_off),
6906 filename);
6907 }
6908
6909 /* Read in a CU/TU header and perform some basic error checking.
6910 The contents of the header are stored in HEADER.
6911 The result is a pointer to the start of the first DIE. */
6912
6913 static const gdb_byte *
6914 read_and_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6915 struct comp_unit_head *header,
6916 struct dwarf2_section_info *section,
6917 struct dwarf2_section_info *abbrev_section,
6918 const gdb_byte *info_ptr,
6919 rcuh_kind section_kind)
6920 {
6921 const gdb_byte *beg_of_comp_unit = info_ptr;
6922
6923 header->sect_off = (sect_offset) (beg_of_comp_unit - section->buffer);
6924
6925 info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
6926
6927 header->first_die_cu_offset = (cu_offset) (info_ptr - beg_of_comp_unit);
6928
6929 error_check_comp_unit_head (dwarf2_per_objfile, header, section,
6930 abbrev_section);
6931
6932 return info_ptr;
6933 }
6934
6935 /* Fetch the abbreviation table offset from a comp or type unit header. */
6936
6937 static sect_offset
6938 read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
6939 struct dwarf2_section_info *section,
6940 sect_offset sect_off)
6941 {
6942 bfd *abfd = get_section_bfd_owner (section);
6943 const gdb_byte *info_ptr;
6944 unsigned int initial_length_size, offset_size;
6945 uint16_t version;
6946
6947 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
6948 info_ptr = section->buffer + to_underlying (sect_off);
6949 read_initial_length (abfd, info_ptr, &initial_length_size);
6950 offset_size = initial_length_size == 4 ? 4 : 8;
6951 info_ptr += initial_length_size;
6952
6953 version = read_2_bytes (abfd, info_ptr);
6954 info_ptr += 2;
6955 if (version >= 5)
6956 {
6957 /* Skip unit type and address size. */
6958 info_ptr += 2;
6959 }
6960
6961 return (sect_offset) read_offset_1 (abfd, info_ptr, offset_size);
6962 }
6963
6964 /* Allocate a new partial symtab for file named NAME and mark this new
6965 partial symtab as being an include of PST. */
6966
6967 static void
6968 dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
6969 struct objfile *objfile)
6970 {
6971 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
6972
6973 if (!IS_ABSOLUTE_PATH (subpst->filename))
6974 {
6975 /* It shares objfile->objfile_obstack. */
6976 subpst->dirname = pst->dirname;
6977 }
6978
6979 subpst->textlow = 0;
6980 subpst->texthigh = 0;
6981
6982 subpst->dependencies
6983 = XOBNEW (&objfile->objfile_obstack, struct partial_symtab *);
6984 subpst->dependencies[0] = pst;
6985 subpst->number_of_dependencies = 1;
6986
6987 subpst->globals_offset = 0;
6988 subpst->n_global_syms = 0;
6989 subpst->statics_offset = 0;
6990 subpst->n_static_syms = 0;
6991 subpst->compunit_symtab = NULL;
6992 subpst->read_symtab = pst->read_symtab;
6993 subpst->readin = 0;
6994
6995 /* No private part is necessary for include psymtabs. This property
6996 can be used to differentiate between such include psymtabs and
6997 the regular ones. */
6998 subpst->read_symtab_private = NULL;
6999 }
7000
7001 /* Read the Line Number Program data and extract the list of files
7002 included by the source file represented by PST. Build an include
7003 partial symtab for each of these included files. */
7004
7005 static void
7006 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
7007 struct die_info *die,
7008 struct partial_symtab *pst)
7009 {
7010 line_header_up lh;
7011 struct attribute *attr;
7012
7013 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
7014 if (attr)
7015 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
7016 if (lh == NULL)
7017 return; /* No linetable, so no includes. */
7018
7019 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
7020 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst, pst->textlow, 1);
7021 }
7022
7023 static hashval_t
7024 hash_signatured_type (const void *item)
7025 {
7026 const struct signatured_type *sig_type
7027 = (const struct signatured_type *) item;
7028
7029 /* This drops the top 32 bits of the signature, but is ok for a hash. */
7030 return sig_type->signature;
7031 }
7032
7033 static int
7034 eq_signatured_type (const void *item_lhs, const void *item_rhs)
7035 {
7036 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
7037 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
7038
7039 return lhs->signature == rhs->signature;
7040 }
7041
7042 /* Allocate a hash table for signatured types. */
7043
7044 static htab_t
7045 allocate_signatured_type_table (struct objfile *objfile)
7046 {
7047 return htab_create_alloc_ex (41,
7048 hash_signatured_type,
7049 eq_signatured_type,
7050 NULL,
7051 &objfile->objfile_obstack,
7052 hashtab_obstack_allocate,
7053 dummy_obstack_deallocate);
7054 }
7055
7056 /* A helper function to add a signatured type CU to a table. */
7057
7058 static int
7059 add_signatured_type_cu_to_table (void **slot, void *datum)
7060 {
7061 struct signatured_type *sigt = (struct signatured_type *) *slot;
7062 struct signatured_type ***datap = (struct signatured_type ***) datum;
7063
7064 **datap = sigt;
7065 ++*datap;
7066
7067 return 1;
7068 }
7069
7070 /* A helper for create_debug_types_hash_table. Read types from SECTION
7071 and fill them into TYPES_HTAB. It will process only type units,
7072 therefore DW_UT_type. */
7073
7074 static void
7075 create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
7076 struct dwo_file *dwo_file,
7077 dwarf2_section_info *section, htab_t &types_htab,
7078 rcuh_kind section_kind)
7079 {
7080 struct objfile *objfile = dwarf2_per_objfile->objfile;
7081 struct dwarf2_section_info *abbrev_section;
7082 bfd *abfd;
7083 const gdb_byte *info_ptr, *end_ptr;
7084
7085 abbrev_section = (dwo_file != NULL
7086 ? &dwo_file->sections.abbrev
7087 : &dwarf2_per_objfile->abbrev);
7088
7089 if (dwarf_read_debug)
7090 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
7091 get_section_name (section),
7092 get_section_file_name (abbrev_section));
7093
7094 dwarf2_read_section (objfile, section);
7095 info_ptr = section->buffer;
7096
7097 if (info_ptr == NULL)
7098 return;
7099
7100 /* We can't set abfd until now because the section may be empty or
7101 not present, in which case the bfd is unknown. */
7102 abfd = get_section_bfd_owner (section);
7103
7104 /* We don't use init_cutu_and_read_dies_simple, or some such, here
7105 because we don't need to read any dies: the signature is in the
7106 header. */
7107
7108 end_ptr = info_ptr + section->size;
7109 while (info_ptr < end_ptr)
7110 {
7111 struct signatured_type *sig_type;
7112 struct dwo_unit *dwo_tu;
7113 void **slot;
7114 const gdb_byte *ptr = info_ptr;
7115 struct comp_unit_head header;
7116 unsigned int length;
7117
7118 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
7119
7120 /* Initialize it due to a false compiler warning. */
7121 header.signature = -1;
7122 header.type_cu_offset_in_tu = (cu_offset) -1;
7123
7124 /* We need to read the type's signature in order to build the hash
7125 table, but we don't need anything else just yet. */
7126
7127 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
7128 abbrev_section, ptr, section_kind);
7129
7130 length = get_cu_length (&header);
7131
7132 /* Skip dummy type units. */
7133 if (ptr >= info_ptr + length
7134 || peek_abbrev_code (abfd, ptr) == 0
7135 || header.unit_type != DW_UT_type)
7136 {
7137 info_ptr += length;
7138 continue;
7139 }
7140
7141 if (types_htab == NULL)
7142 {
7143 if (dwo_file)
7144 types_htab = allocate_dwo_unit_table (objfile);
7145 else
7146 types_htab = allocate_signatured_type_table (objfile);
7147 }
7148
7149 if (dwo_file)
7150 {
7151 sig_type = NULL;
7152 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7153 struct dwo_unit);
7154 dwo_tu->dwo_file = dwo_file;
7155 dwo_tu->signature = header.signature;
7156 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
7157 dwo_tu->section = section;
7158 dwo_tu->sect_off = sect_off;
7159 dwo_tu->length = length;
7160 }
7161 else
7162 {
7163 /* N.B.: type_offset is not usable if this type uses a DWO file.
7164 The real type_offset is in the DWO file. */
7165 dwo_tu = NULL;
7166 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7167 struct signatured_type);
7168 sig_type->signature = header.signature;
7169 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
7170 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
7171 sig_type->per_cu.is_debug_types = 1;
7172 sig_type->per_cu.section = section;
7173 sig_type->per_cu.sect_off = sect_off;
7174 sig_type->per_cu.length = length;
7175 }
7176
7177 slot = htab_find_slot (types_htab,
7178 dwo_file ? (void*) dwo_tu : (void *) sig_type,
7179 INSERT);
7180 gdb_assert (slot != NULL);
7181 if (*slot != NULL)
7182 {
7183 sect_offset dup_sect_off;
7184
7185 if (dwo_file)
7186 {
7187 const struct dwo_unit *dup_tu
7188 = (const struct dwo_unit *) *slot;
7189
7190 dup_sect_off = dup_tu->sect_off;
7191 }
7192 else
7193 {
7194 const struct signatured_type *dup_tu
7195 = (const struct signatured_type *) *slot;
7196
7197 dup_sect_off = dup_tu->per_cu.sect_off;
7198 }
7199
7200 complaint (&symfile_complaints,
7201 _("debug type entry at offset %s is duplicate to"
7202 " the entry at offset %s, signature %s"),
7203 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
7204 hex_string (header.signature));
7205 }
7206 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
7207
7208 if (dwarf_read_debug > 1)
7209 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
7210 sect_offset_str (sect_off),
7211 hex_string (header.signature));
7212
7213 info_ptr += length;
7214 }
7215 }
7216
7217 /* Create the hash table of all entries in the .debug_types
7218 (or .debug_types.dwo) section(s).
7219 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
7220 otherwise it is NULL.
7221
7222 The result is a pointer to the hash table or NULL if there are no types.
7223
7224 Note: This function processes DWO files only, not DWP files. */
7225
7226 static void
7227 create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
7228 struct dwo_file *dwo_file,
7229 VEC (dwarf2_section_info_def) *types,
7230 htab_t &types_htab)
7231 {
7232 int ix;
7233 struct dwarf2_section_info *section;
7234
7235 if (VEC_empty (dwarf2_section_info_def, types))
7236 return;
7237
7238 for (ix = 0;
7239 VEC_iterate (dwarf2_section_info_def, types, ix, section);
7240 ++ix)
7241 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, section,
7242 types_htab, rcuh_kind::TYPE);
7243 }
7244
7245 /* Create the hash table of all entries in the .debug_types section,
7246 and initialize all_type_units.
7247 The result is zero if there is an error (e.g. missing .debug_types section),
7248 otherwise non-zero. */
7249
7250 static int
7251 create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
7252 {
7253 htab_t types_htab = NULL;
7254 struct signatured_type **iter;
7255
7256 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
7257 &dwarf2_per_objfile->info, types_htab,
7258 rcuh_kind::COMPILE);
7259 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
7260 dwarf2_per_objfile->types, types_htab);
7261 if (types_htab == NULL)
7262 {
7263 dwarf2_per_objfile->signatured_types = NULL;
7264 return 0;
7265 }
7266
7267 dwarf2_per_objfile->signatured_types = types_htab;
7268
7269 dwarf2_per_objfile->n_type_units
7270 = dwarf2_per_objfile->n_allocated_type_units
7271 = htab_elements (types_htab);
7272 dwarf2_per_objfile->all_type_units =
7273 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
7274 iter = &dwarf2_per_objfile->all_type_units[0];
7275 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
7276 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
7277 == dwarf2_per_objfile->n_type_units);
7278
7279 return 1;
7280 }
7281
7282 /* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
7283 If SLOT is non-NULL, it is the entry to use in the hash table.
7284 Otherwise we find one. */
7285
7286 static struct signatured_type *
7287 add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
7288 void **slot)
7289 {
7290 struct objfile *objfile = dwarf2_per_objfile->objfile;
7291 int n_type_units = dwarf2_per_objfile->n_type_units;
7292 struct signatured_type *sig_type;
7293
7294 gdb_assert (n_type_units <= dwarf2_per_objfile->n_allocated_type_units);
7295 ++n_type_units;
7296 if (n_type_units > dwarf2_per_objfile->n_allocated_type_units)
7297 {
7298 if (dwarf2_per_objfile->n_allocated_type_units == 0)
7299 dwarf2_per_objfile->n_allocated_type_units = 1;
7300 dwarf2_per_objfile->n_allocated_type_units *= 2;
7301 dwarf2_per_objfile->all_type_units
7302 = XRESIZEVEC (struct signatured_type *,
7303 dwarf2_per_objfile->all_type_units,
7304 dwarf2_per_objfile->n_allocated_type_units);
7305 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
7306 }
7307 dwarf2_per_objfile->n_type_units = n_type_units;
7308
7309 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7310 struct signatured_type);
7311 dwarf2_per_objfile->all_type_units[n_type_units - 1] = sig_type;
7312 sig_type->signature = sig;
7313 sig_type->per_cu.is_debug_types = 1;
7314 if (dwarf2_per_objfile->using_index)
7315 {
7316 sig_type->per_cu.v.quick =
7317 OBSTACK_ZALLOC (&objfile->objfile_obstack,
7318 struct dwarf2_per_cu_quick_data);
7319 }
7320
7321 if (slot == NULL)
7322 {
7323 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7324 sig_type, INSERT);
7325 }
7326 gdb_assert (*slot == NULL);
7327 *slot = sig_type;
7328 /* The rest of sig_type must be filled in by the caller. */
7329 return sig_type;
7330 }
7331
7332 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
7333 Fill in SIG_ENTRY with DWO_ENTRY. */
7334
7335 static void
7336 fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
7337 struct signatured_type *sig_entry,
7338 struct dwo_unit *dwo_entry)
7339 {
7340 /* Make sure we're not clobbering something we don't expect to. */
7341 gdb_assert (! sig_entry->per_cu.queued);
7342 gdb_assert (sig_entry->per_cu.cu == NULL);
7343 if (dwarf2_per_objfile->using_index)
7344 {
7345 gdb_assert (sig_entry->per_cu.v.quick != NULL);
7346 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
7347 }
7348 else
7349 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
7350 gdb_assert (sig_entry->signature == dwo_entry->signature);
7351 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
7352 gdb_assert (sig_entry->type_unit_group == NULL);
7353 gdb_assert (sig_entry->dwo_unit == NULL);
7354
7355 sig_entry->per_cu.section = dwo_entry->section;
7356 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7357 sig_entry->per_cu.length = dwo_entry->length;
7358 sig_entry->per_cu.reading_dwo_directly = 1;
7359 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
7360 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
7361 sig_entry->dwo_unit = dwo_entry;
7362 }
7363
7364 /* Subroutine of lookup_signatured_type.
7365 If we haven't read the TU yet, create the signatured_type data structure
7366 for a TU to be read in directly from a DWO file, bypassing the stub.
7367 This is the "Stay in DWO Optimization": When there is no DWP file and we're
7368 using .gdb_index, then when reading a CU we want to stay in the DWO file
7369 containing that CU. Otherwise we could end up reading several other DWO
7370 files (due to comdat folding) to process the transitive closure of all the
7371 mentioned TUs, and that can be slow. The current DWO file will have every
7372 type signature that it needs.
7373 We only do this for .gdb_index because in the psymtab case we already have
7374 to read all the DWOs to build the type unit groups. */
7375
7376 static struct signatured_type *
7377 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7378 {
7379 struct dwarf2_per_objfile *dwarf2_per_objfile
7380 = cu->per_cu->dwarf2_per_objfile;
7381 struct objfile *objfile = dwarf2_per_objfile->objfile;
7382 struct dwo_file *dwo_file;
7383 struct dwo_unit find_dwo_entry, *dwo_entry;
7384 struct signatured_type find_sig_entry, *sig_entry;
7385 void **slot;
7386
7387 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7388
7389 /* If TU skeletons have been removed then we may not have read in any
7390 TUs yet. */
7391 if (dwarf2_per_objfile->signatured_types == NULL)
7392 {
7393 dwarf2_per_objfile->signatured_types
7394 = allocate_signatured_type_table (objfile);
7395 }
7396
7397 /* We only ever need to read in one copy of a signatured type.
7398 Use the global signatured_types array to do our own comdat-folding
7399 of types. If this is the first time we're reading this TU, and
7400 the TU has an entry in .gdb_index, replace the recorded data from
7401 .gdb_index with this TU. */
7402
7403 find_sig_entry.signature = sig;
7404 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7405 &find_sig_entry, INSERT);
7406 sig_entry = (struct signatured_type *) *slot;
7407
7408 /* We can get here with the TU already read, *or* in the process of being
7409 read. Don't reassign the global entry to point to this DWO if that's
7410 the case. Also note that if the TU is already being read, it may not
7411 have come from a DWO, the program may be a mix of Fission-compiled
7412 code and non-Fission-compiled code. */
7413
7414 /* Have we already tried to read this TU?
7415 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7416 needn't exist in the global table yet). */
7417 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
7418 return sig_entry;
7419
7420 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
7421 dwo_unit of the TU itself. */
7422 dwo_file = cu->dwo_unit->dwo_file;
7423
7424 /* Ok, this is the first time we're reading this TU. */
7425 if (dwo_file->tus == NULL)
7426 return NULL;
7427 find_dwo_entry.signature = sig;
7428 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
7429 if (dwo_entry == NULL)
7430 return NULL;
7431
7432 /* If the global table doesn't have an entry for this TU, add one. */
7433 if (sig_entry == NULL)
7434 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
7435
7436 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
7437 sig_entry->per_cu.tu_read = 1;
7438 return sig_entry;
7439 }
7440
7441 /* Subroutine of lookup_signatured_type.
7442 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
7443 then try the DWP file. If the TU stub (skeleton) has been removed then
7444 it won't be in .gdb_index. */
7445
7446 static struct signatured_type *
7447 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7448 {
7449 struct dwarf2_per_objfile *dwarf2_per_objfile
7450 = cu->per_cu->dwarf2_per_objfile;
7451 struct objfile *objfile = dwarf2_per_objfile->objfile;
7452 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
7453 struct dwo_unit *dwo_entry;
7454 struct signatured_type find_sig_entry, *sig_entry;
7455 void **slot;
7456
7457 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7458 gdb_assert (dwp_file != NULL);
7459
7460 /* If TU skeletons have been removed then we may not have read in any
7461 TUs yet. */
7462 if (dwarf2_per_objfile->signatured_types == NULL)
7463 {
7464 dwarf2_per_objfile->signatured_types
7465 = allocate_signatured_type_table (objfile);
7466 }
7467
7468 find_sig_entry.signature = sig;
7469 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7470 &find_sig_entry, INSERT);
7471 sig_entry = (struct signatured_type *) *slot;
7472
7473 /* Have we already tried to read this TU?
7474 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7475 needn't exist in the global table yet). */
7476 if (sig_entry != NULL)
7477 return sig_entry;
7478
7479 if (dwp_file->tus == NULL)
7480 return NULL;
7481 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
7482 sig, 1 /* is_debug_types */);
7483 if (dwo_entry == NULL)
7484 return NULL;
7485
7486 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
7487 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
7488
7489 return sig_entry;
7490 }
7491
7492 /* Lookup a signature based type for DW_FORM_ref_sig8.
7493 Returns NULL if signature SIG is not present in the table.
7494 It is up to the caller to complain about this. */
7495
7496 static struct signatured_type *
7497 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7498 {
7499 struct dwarf2_per_objfile *dwarf2_per_objfile
7500 = cu->per_cu->dwarf2_per_objfile;
7501
7502 if (cu->dwo_unit
7503 && dwarf2_per_objfile->using_index)
7504 {
7505 /* We're in a DWO/DWP file, and we're using .gdb_index.
7506 These cases require special processing. */
7507 if (get_dwp_file (dwarf2_per_objfile) == NULL)
7508 return lookup_dwo_signatured_type (cu, sig);
7509 else
7510 return lookup_dwp_signatured_type (cu, sig);
7511 }
7512 else
7513 {
7514 struct signatured_type find_entry, *entry;
7515
7516 if (dwarf2_per_objfile->signatured_types == NULL)
7517 return NULL;
7518 find_entry.signature = sig;
7519 entry = ((struct signatured_type *)
7520 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
7521 return entry;
7522 }
7523 }
7524 \f
7525 /* Low level DIE reading support. */
7526
7527 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
7528
7529 static void
7530 init_cu_die_reader (struct die_reader_specs *reader,
7531 struct dwarf2_cu *cu,
7532 struct dwarf2_section_info *section,
7533 struct dwo_file *dwo_file,
7534 struct abbrev_table *abbrev_table)
7535 {
7536 gdb_assert (section->readin && section->buffer != NULL);
7537 reader->abfd = get_section_bfd_owner (section);
7538 reader->cu = cu;
7539 reader->dwo_file = dwo_file;
7540 reader->die_section = section;
7541 reader->buffer = section->buffer;
7542 reader->buffer_end = section->buffer + section->size;
7543 reader->comp_dir = NULL;
7544 reader->abbrev_table = abbrev_table;
7545 }
7546
7547 /* Subroutine of init_cutu_and_read_dies to simplify it.
7548 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
7549 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
7550 already.
7551
7552 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
7553 from it to the DIE in the DWO. If NULL we are skipping the stub.
7554 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
7555 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
7556 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
7557 STUB_COMP_DIR may be non-NULL.
7558 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
7559 are filled in with the info of the DIE from the DWO file.
7560 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
7561 from the dwo. Since *RESULT_READER references this abbrev table, it must be
7562 kept around for at least as long as *RESULT_READER.
7563
7564 The result is non-zero if a valid (non-dummy) DIE was found. */
7565
7566 static int
7567 read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
7568 struct dwo_unit *dwo_unit,
7569 struct die_info *stub_comp_unit_die,
7570 const char *stub_comp_dir,
7571 struct die_reader_specs *result_reader,
7572 const gdb_byte **result_info_ptr,
7573 struct die_info **result_comp_unit_die,
7574 int *result_has_children,
7575 abbrev_table_up *result_dwo_abbrev_table)
7576 {
7577 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7578 struct objfile *objfile = dwarf2_per_objfile->objfile;
7579 struct dwarf2_cu *cu = this_cu->cu;
7580 bfd *abfd;
7581 const gdb_byte *begin_info_ptr, *info_ptr;
7582 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
7583 int i,num_extra_attrs;
7584 struct dwarf2_section_info *dwo_abbrev_section;
7585 struct attribute *attr;
7586 struct die_info *comp_unit_die;
7587
7588 /* At most one of these may be provided. */
7589 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
7590
7591 /* These attributes aren't processed until later:
7592 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
7593 DW_AT_comp_dir is used now, to find the DWO file, but it is also
7594 referenced later. However, these attributes are found in the stub
7595 which we won't have later. In order to not impose this complication
7596 on the rest of the code, we read them here and copy them to the
7597 DWO CU/TU die. */
7598
7599 stmt_list = NULL;
7600 low_pc = NULL;
7601 high_pc = NULL;
7602 ranges = NULL;
7603 comp_dir = NULL;
7604
7605 if (stub_comp_unit_die != NULL)
7606 {
7607 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
7608 DWO file. */
7609 if (! this_cu->is_debug_types)
7610 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
7611 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
7612 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
7613 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
7614 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
7615
7616 /* There should be a DW_AT_addr_base attribute here (if needed).
7617 We need the value before we can process DW_FORM_GNU_addr_index. */
7618 cu->addr_base = 0;
7619 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
7620 if (attr)
7621 cu->addr_base = DW_UNSND (attr);
7622
7623 /* There should be a DW_AT_ranges_base attribute here (if needed).
7624 We need the value before we can process DW_AT_ranges. */
7625 cu->ranges_base = 0;
7626 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
7627 if (attr)
7628 cu->ranges_base = DW_UNSND (attr);
7629 }
7630 else if (stub_comp_dir != NULL)
7631 {
7632 /* Reconstruct the comp_dir attribute to simplify the code below. */
7633 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
7634 comp_dir->name = DW_AT_comp_dir;
7635 comp_dir->form = DW_FORM_string;
7636 DW_STRING_IS_CANONICAL (comp_dir) = 0;
7637 DW_STRING (comp_dir) = stub_comp_dir;
7638 }
7639
7640 /* Set up for reading the DWO CU/TU. */
7641 cu->dwo_unit = dwo_unit;
7642 dwarf2_section_info *section = dwo_unit->section;
7643 dwarf2_read_section (objfile, section);
7644 abfd = get_section_bfd_owner (section);
7645 begin_info_ptr = info_ptr = (section->buffer
7646 + to_underlying (dwo_unit->sect_off));
7647 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
7648
7649 if (this_cu->is_debug_types)
7650 {
7651 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
7652
7653 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7654 &cu->header, section,
7655 dwo_abbrev_section,
7656 info_ptr, rcuh_kind::TYPE);
7657 /* This is not an assert because it can be caused by bad debug info. */
7658 if (sig_type->signature != cu->header.signature)
7659 {
7660 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
7661 " TU at offset %s [in module %s]"),
7662 hex_string (sig_type->signature),
7663 hex_string (cu->header.signature),
7664 sect_offset_str (dwo_unit->sect_off),
7665 bfd_get_filename (abfd));
7666 }
7667 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
7668 /* For DWOs coming from DWP files, we don't know the CU length
7669 nor the type's offset in the TU until now. */
7670 dwo_unit->length = get_cu_length (&cu->header);
7671 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
7672
7673 /* Establish the type offset that can be used to lookup the type.
7674 For DWO files, we don't know it until now. */
7675 sig_type->type_offset_in_section
7676 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
7677 }
7678 else
7679 {
7680 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7681 &cu->header, section,
7682 dwo_abbrev_section,
7683 info_ptr, rcuh_kind::COMPILE);
7684 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
7685 /* For DWOs coming from DWP files, we don't know the CU length
7686 until now. */
7687 dwo_unit->length = get_cu_length (&cu->header);
7688 }
7689
7690 *result_dwo_abbrev_table
7691 = abbrev_table_read_table (dwarf2_per_objfile, dwo_abbrev_section,
7692 cu->header.abbrev_sect_off);
7693 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
7694 result_dwo_abbrev_table->get ());
7695
7696 /* Read in the die, but leave space to copy over the attributes
7697 from the stub. This has the benefit of simplifying the rest of
7698 the code - all the work to maintain the illusion of a single
7699 DW_TAG_{compile,type}_unit DIE is done here. */
7700 num_extra_attrs = ((stmt_list != NULL)
7701 + (low_pc != NULL)
7702 + (high_pc != NULL)
7703 + (ranges != NULL)
7704 + (comp_dir != NULL));
7705 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
7706 result_has_children, num_extra_attrs);
7707
7708 /* Copy over the attributes from the stub to the DIE we just read in. */
7709 comp_unit_die = *result_comp_unit_die;
7710 i = comp_unit_die->num_attrs;
7711 if (stmt_list != NULL)
7712 comp_unit_die->attrs[i++] = *stmt_list;
7713 if (low_pc != NULL)
7714 comp_unit_die->attrs[i++] = *low_pc;
7715 if (high_pc != NULL)
7716 comp_unit_die->attrs[i++] = *high_pc;
7717 if (ranges != NULL)
7718 comp_unit_die->attrs[i++] = *ranges;
7719 if (comp_dir != NULL)
7720 comp_unit_die->attrs[i++] = *comp_dir;
7721 comp_unit_die->num_attrs += num_extra_attrs;
7722
7723 if (dwarf_die_debug)
7724 {
7725 fprintf_unfiltered (gdb_stdlog,
7726 "Read die from %s@0x%x of %s:\n",
7727 get_section_name (section),
7728 (unsigned) (begin_info_ptr - section->buffer),
7729 bfd_get_filename (abfd));
7730 dump_die (comp_unit_die, dwarf_die_debug);
7731 }
7732
7733 /* Save the comp_dir attribute. If there is no DWP file then we'll read
7734 TUs by skipping the stub and going directly to the entry in the DWO file.
7735 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
7736 to get it via circuitous means. Blech. */
7737 if (comp_dir != NULL)
7738 result_reader->comp_dir = DW_STRING (comp_dir);
7739
7740 /* Skip dummy compilation units. */
7741 if (info_ptr >= begin_info_ptr + dwo_unit->length
7742 || peek_abbrev_code (abfd, info_ptr) == 0)
7743 return 0;
7744
7745 *result_info_ptr = info_ptr;
7746 return 1;
7747 }
7748
7749 /* Subroutine of init_cutu_and_read_dies to simplify it.
7750 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
7751 Returns NULL if the specified DWO unit cannot be found. */
7752
7753 static struct dwo_unit *
7754 lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
7755 struct die_info *comp_unit_die)
7756 {
7757 struct dwarf2_cu *cu = this_cu->cu;
7758 ULONGEST signature;
7759 struct dwo_unit *dwo_unit;
7760 const char *comp_dir, *dwo_name;
7761
7762 gdb_assert (cu != NULL);
7763
7764 /* Yeah, we look dwo_name up again, but it simplifies the code. */
7765 dwo_name = dwarf2_string_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
7766 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7767
7768 if (this_cu->is_debug_types)
7769 {
7770 struct signatured_type *sig_type;
7771
7772 /* Since this_cu is the first member of struct signatured_type,
7773 we can go from a pointer to one to a pointer to the other. */
7774 sig_type = (struct signatured_type *) this_cu;
7775 signature = sig_type->signature;
7776 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
7777 }
7778 else
7779 {
7780 struct attribute *attr;
7781
7782 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
7783 if (! attr)
7784 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
7785 " [in module %s]"),
7786 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
7787 signature = DW_UNSND (attr);
7788 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
7789 signature);
7790 }
7791
7792 return dwo_unit;
7793 }
7794
7795 /* Subroutine of init_cutu_and_read_dies to simplify it.
7796 See it for a description of the parameters.
7797 Read a TU directly from a DWO file, bypassing the stub. */
7798
7799 static void
7800 init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
7801 int use_existing_cu, int keep,
7802 die_reader_func_ftype *die_reader_func,
7803 void *data)
7804 {
7805 std::unique_ptr<dwarf2_cu> new_cu;
7806 struct signatured_type *sig_type;
7807 struct die_reader_specs reader;
7808 const gdb_byte *info_ptr;
7809 struct die_info *comp_unit_die;
7810 int has_children;
7811 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7812
7813 /* Verify we can do the following downcast, and that we have the
7814 data we need. */
7815 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7816 sig_type = (struct signatured_type *) this_cu;
7817 gdb_assert (sig_type->dwo_unit != NULL);
7818
7819 if (use_existing_cu && this_cu->cu != NULL)
7820 {
7821 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
7822 /* There's no need to do the rereading_dwo_cu handling that
7823 init_cutu_and_read_dies does since we don't read the stub. */
7824 }
7825 else
7826 {
7827 /* If !use_existing_cu, this_cu->cu must be NULL. */
7828 gdb_assert (this_cu->cu == NULL);
7829 new_cu.reset (new dwarf2_cu (this_cu));
7830 }
7831
7832 /* A future optimization, if needed, would be to use an existing
7833 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7834 could share abbrev tables. */
7835
7836 /* The abbreviation table used by READER, this must live at least as long as
7837 READER. */
7838 abbrev_table_up dwo_abbrev_table;
7839
7840 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
7841 NULL /* stub_comp_unit_die */,
7842 sig_type->dwo_unit->dwo_file->comp_dir,
7843 &reader, &info_ptr,
7844 &comp_unit_die, &has_children,
7845 &dwo_abbrev_table) == 0)
7846 {
7847 /* Dummy die. */
7848 return;
7849 }
7850
7851 /* All the "real" work is done here. */
7852 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7853
7854 /* This duplicates the code in init_cutu_and_read_dies,
7855 but the alternative is making the latter more complex.
7856 This function is only for the special case of using DWO files directly:
7857 no point in overly complicating the general case just to handle this. */
7858 if (new_cu != NULL && keep)
7859 {
7860 /* Link this CU into read_in_chain. */
7861 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7862 dwarf2_per_objfile->read_in_chain = this_cu;
7863 /* The chain owns it now. */
7864 new_cu.release ();
7865 }
7866 }
7867
7868 /* Initialize a CU (or TU) and read its DIEs.
7869 If the CU defers to a DWO file, read the DWO file as well.
7870
7871 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7872 Otherwise the table specified in the comp unit header is read in and used.
7873 This is an optimization for when we already have the abbrev table.
7874
7875 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
7876 Otherwise, a new CU is allocated with xmalloc.
7877
7878 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
7879 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
7880
7881 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
7882 linker) then DIE_READER_FUNC will not get called. */
7883
7884 static void
7885 init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
7886 struct abbrev_table *abbrev_table,
7887 int use_existing_cu, int keep,
7888 die_reader_func_ftype *die_reader_func,
7889 void *data)
7890 {
7891 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7892 struct objfile *objfile = dwarf2_per_objfile->objfile;
7893 struct dwarf2_section_info *section = this_cu->section;
7894 bfd *abfd = get_section_bfd_owner (section);
7895 struct dwarf2_cu *cu;
7896 const gdb_byte *begin_info_ptr, *info_ptr;
7897 struct die_reader_specs reader;
7898 struct die_info *comp_unit_die;
7899 int has_children;
7900 struct attribute *attr;
7901 struct signatured_type *sig_type = NULL;
7902 struct dwarf2_section_info *abbrev_section;
7903 /* Non-zero if CU currently points to a DWO file and we need to
7904 reread it. When this happens we need to reread the skeleton die
7905 before we can reread the DWO file (this only applies to CUs, not TUs). */
7906 int rereading_dwo_cu = 0;
7907
7908 if (dwarf_die_debug)
7909 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7910 this_cu->is_debug_types ? "type" : "comp",
7911 sect_offset_str (this_cu->sect_off));
7912
7913 if (use_existing_cu)
7914 gdb_assert (keep);
7915
7916 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7917 file (instead of going through the stub), short-circuit all of this. */
7918 if (this_cu->reading_dwo_directly)
7919 {
7920 /* Narrow down the scope of possibilities to have to understand. */
7921 gdb_assert (this_cu->is_debug_types);
7922 gdb_assert (abbrev_table == NULL);
7923 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep,
7924 die_reader_func, data);
7925 return;
7926 }
7927
7928 /* This is cheap if the section is already read in. */
7929 dwarf2_read_section (objfile, section);
7930
7931 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7932
7933 abbrev_section = get_abbrev_section_for_cu (this_cu);
7934
7935 std::unique_ptr<dwarf2_cu> new_cu;
7936 if (use_existing_cu && this_cu->cu != NULL)
7937 {
7938 cu = this_cu->cu;
7939 /* If this CU is from a DWO file we need to start over, we need to
7940 refetch the attributes from the skeleton CU.
7941 This could be optimized by retrieving those attributes from when we
7942 were here the first time: the previous comp_unit_die was stored in
7943 comp_unit_obstack. But there's no data yet that we need this
7944 optimization. */
7945 if (cu->dwo_unit != NULL)
7946 rereading_dwo_cu = 1;
7947 }
7948 else
7949 {
7950 /* If !use_existing_cu, this_cu->cu must be NULL. */
7951 gdb_assert (this_cu->cu == NULL);
7952 new_cu.reset (new dwarf2_cu (this_cu));
7953 cu = new_cu.get ();
7954 }
7955
7956 /* Get the header. */
7957 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
7958 {
7959 /* We already have the header, there's no need to read it in again. */
7960 info_ptr += to_underlying (cu->header.first_die_cu_offset);
7961 }
7962 else
7963 {
7964 if (this_cu->is_debug_types)
7965 {
7966 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7967 &cu->header, section,
7968 abbrev_section, info_ptr,
7969 rcuh_kind::TYPE);
7970
7971 /* Since per_cu is the first member of struct signatured_type,
7972 we can go from a pointer to one to a pointer to the other. */
7973 sig_type = (struct signatured_type *) this_cu;
7974 gdb_assert (sig_type->signature == cu->header.signature);
7975 gdb_assert (sig_type->type_offset_in_tu
7976 == cu->header.type_cu_offset_in_tu);
7977 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7978
7979 /* LENGTH has not been set yet for type units if we're
7980 using .gdb_index. */
7981 this_cu->length = get_cu_length (&cu->header);
7982
7983 /* Establish the type offset that can be used to lookup the type. */
7984 sig_type->type_offset_in_section =
7985 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
7986
7987 this_cu->dwarf_version = cu->header.version;
7988 }
7989 else
7990 {
7991 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7992 &cu->header, section,
7993 abbrev_section,
7994 info_ptr,
7995 rcuh_kind::COMPILE);
7996
7997 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7998 gdb_assert (this_cu->length == get_cu_length (&cu->header));
7999 this_cu->dwarf_version = cu->header.version;
8000 }
8001 }
8002
8003 /* Skip dummy compilation units. */
8004 if (info_ptr >= begin_info_ptr + this_cu->length
8005 || peek_abbrev_code (abfd, info_ptr) == 0)
8006 return;
8007
8008 /* If we don't have them yet, read the abbrevs for this compilation unit.
8009 And if we need to read them now, make sure they're freed when we're
8010 done (own the table through ABBREV_TABLE_HOLDER). */
8011 abbrev_table_up abbrev_table_holder;
8012 if (abbrev_table != NULL)
8013 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
8014 else
8015 {
8016 abbrev_table_holder
8017 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
8018 cu->header.abbrev_sect_off);
8019 abbrev_table = abbrev_table_holder.get ();
8020 }
8021
8022 /* Read the top level CU/TU die. */
8023 init_cu_die_reader (&reader, cu, section, NULL, abbrev_table);
8024 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
8025
8026 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
8027 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
8028 table from the DWO file and pass the ownership over to us. It will be
8029 referenced from READER, so we must make sure to free it after we're done
8030 with READER.
8031
8032 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
8033 DWO CU, that this test will fail (the attribute will not be present). */
8034 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
8035 abbrev_table_up dwo_abbrev_table;
8036 if (attr)
8037 {
8038 struct dwo_unit *dwo_unit;
8039 struct die_info *dwo_comp_unit_die;
8040
8041 if (has_children)
8042 {
8043 complaint (&symfile_complaints,
8044 _("compilation unit with DW_AT_GNU_dwo_name"
8045 " has children (offset %s) [in module %s]"),
8046 sect_offset_str (this_cu->sect_off),
8047 bfd_get_filename (abfd));
8048 }
8049 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
8050 if (dwo_unit != NULL)
8051 {
8052 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
8053 comp_unit_die, NULL,
8054 &reader, &info_ptr,
8055 &dwo_comp_unit_die, &has_children,
8056 &dwo_abbrev_table) == 0)
8057 {
8058 /* Dummy die. */
8059 return;
8060 }
8061 comp_unit_die = dwo_comp_unit_die;
8062 }
8063 else
8064 {
8065 /* Yikes, we couldn't find the rest of the DIE, we only have
8066 the stub. A complaint has already been logged. There's
8067 not much more we can do except pass on the stub DIE to
8068 die_reader_func. We don't want to throw an error on bad
8069 debug info. */
8070 }
8071 }
8072
8073 /* All of the above is setup for this call. Yikes. */
8074 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
8075
8076 /* Done, clean up. */
8077 if (new_cu != NULL && keep)
8078 {
8079 /* Link this CU into read_in_chain. */
8080 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
8081 dwarf2_per_objfile->read_in_chain = this_cu;
8082 /* The chain owns it now. */
8083 new_cu.release ();
8084 }
8085 }
8086
8087 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name if present.
8088 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed
8089 to have already done the lookup to find the DWO file).
8090
8091 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
8092 THIS_CU->is_debug_types, but nothing else.
8093
8094 We fill in THIS_CU->length.
8095
8096 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
8097 linker) then DIE_READER_FUNC will not get called.
8098
8099 THIS_CU->cu is always freed when done.
8100 This is done in order to not leave THIS_CU->cu in a state where we have
8101 to care whether it refers to the "main" CU or the DWO CU. */
8102
8103 static void
8104 init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
8105 struct dwo_file *dwo_file,
8106 die_reader_func_ftype *die_reader_func,
8107 void *data)
8108 {
8109 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
8110 struct objfile *objfile = dwarf2_per_objfile->objfile;
8111 struct dwarf2_section_info *section = this_cu->section;
8112 bfd *abfd = get_section_bfd_owner (section);
8113 struct dwarf2_section_info *abbrev_section;
8114 const gdb_byte *begin_info_ptr, *info_ptr;
8115 struct die_reader_specs reader;
8116 struct die_info *comp_unit_die;
8117 int has_children;
8118
8119 if (dwarf_die_debug)
8120 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
8121 this_cu->is_debug_types ? "type" : "comp",
8122 sect_offset_str (this_cu->sect_off));
8123
8124 gdb_assert (this_cu->cu == NULL);
8125
8126 abbrev_section = (dwo_file != NULL
8127 ? &dwo_file->sections.abbrev
8128 : get_abbrev_section_for_cu (this_cu));
8129
8130 /* This is cheap if the section is already read in. */
8131 dwarf2_read_section (objfile, section);
8132
8133 struct dwarf2_cu cu (this_cu);
8134
8135 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
8136 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
8137 &cu.header, section,
8138 abbrev_section, info_ptr,
8139 (this_cu->is_debug_types
8140 ? rcuh_kind::TYPE
8141 : rcuh_kind::COMPILE));
8142
8143 this_cu->length = get_cu_length (&cu.header);
8144
8145 /* Skip dummy compilation units. */
8146 if (info_ptr >= begin_info_ptr + this_cu->length
8147 || peek_abbrev_code (abfd, info_ptr) == 0)
8148 return;
8149
8150 abbrev_table_up abbrev_table
8151 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
8152 cu.header.abbrev_sect_off);
8153
8154 init_cu_die_reader (&reader, &cu, section, dwo_file, abbrev_table.get ());
8155 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
8156
8157 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
8158 }
8159
8160 /* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
8161 does not lookup the specified DWO file.
8162 This cannot be used to read DWO files.
8163
8164 THIS_CU->cu is always freed when done.
8165 This is done in order to not leave THIS_CU->cu in a state where we have
8166 to care whether it refers to the "main" CU or the DWO CU.
8167 We can revisit this if the data shows there's a performance issue. */
8168
8169 static void
8170 init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
8171 die_reader_func_ftype *die_reader_func,
8172 void *data)
8173 {
8174 init_cutu_and_read_dies_no_follow (this_cu, NULL, die_reader_func, data);
8175 }
8176 \f
8177 /* Type Unit Groups.
8178
8179 Type Unit Groups are a way to collapse the set of all TUs (type units) into
8180 a more manageable set. The grouping is done by DW_AT_stmt_list entry
8181 so that all types coming from the same compilation (.o file) are grouped
8182 together. A future step could be to put the types in the same symtab as
8183 the CU the types ultimately came from. */
8184
8185 static hashval_t
8186 hash_type_unit_group (const void *item)
8187 {
8188 const struct type_unit_group *tu_group
8189 = (const struct type_unit_group *) item;
8190
8191 return hash_stmt_list_entry (&tu_group->hash);
8192 }
8193
8194 static int
8195 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
8196 {
8197 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
8198 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
8199
8200 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
8201 }
8202
8203 /* Allocate a hash table for type unit groups. */
8204
8205 static htab_t
8206 allocate_type_unit_groups_table (struct objfile *objfile)
8207 {
8208 return htab_create_alloc_ex (3,
8209 hash_type_unit_group,
8210 eq_type_unit_group,
8211 NULL,
8212 &objfile->objfile_obstack,
8213 hashtab_obstack_allocate,
8214 dummy_obstack_deallocate);
8215 }
8216
8217 /* Type units that don't have DW_AT_stmt_list are grouped into their own
8218 partial symtabs. We combine several TUs per psymtab to not let the size
8219 of any one psymtab grow too big. */
8220 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
8221 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
8222
8223 /* Helper routine for get_type_unit_group.
8224 Create the type_unit_group object used to hold one or more TUs. */
8225
8226 static struct type_unit_group *
8227 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
8228 {
8229 struct dwarf2_per_objfile *dwarf2_per_objfile
8230 = cu->per_cu->dwarf2_per_objfile;
8231 struct objfile *objfile = dwarf2_per_objfile->objfile;
8232 struct dwarf2_per_cu_data *per_cu;
8233 struct type_unit_group *tu_group;
8234
8235 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
8236 struct type_unit_group);
8237 per_cu = &tu_group->per_cu;
8238 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8239
8240 if (dwarf2_per_objfile->using_index)
8241 {
8242 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
8243 struct dwarf2_per_cu_quick_data);
8244 }
8245 else
8246 {
8247 unsigned int line_offset = to_underlying (line_offset_struct);
8248 struct partial_symtab *pst;
8249 char *name;
8250
8251 /* Give the symtab a useful name for debug purposes. */
8252 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
8253 name = xstrprintf ("<type_units_%d>",
8254 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
8255 else
8256 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
8257
8258 pst = create_partial_symtab (per_cu, name);
8259 pst->anonymous = 1;
8260
8261 xfree (name);
8262 }
8263
8264 tu_group->hash.dwo_unit = cu->dwo_unit;
8265 tu_group->hash.line_sect_off = line_offset_struct;
8266
8267 return tu_group;
8268 }
8269
8270 /* Look up the type_unit_group for type unit CU, and create it if necessary.
8271 STMT_LIST is a DW_AT_stmt_list attribute. */
8272
8273 static struct type_unit_group *
8274 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
8275 {
8276 struct dwarf2_per_objfile *dwarf2_per_objfile
8277 = cu->per_cu->dwarf2_per_objfile;
8278 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8279 struct type_unit_group *tu_group;
8280 void **slot;
8281 unsigned int line_offset;
8282 struct type_unit_group type_unit_group_for_lookup;
8283
8284 if (dwarf2_per_objfile->type_unit_groups == NULL)
8285 {
8286 dwarf2_per_objfile->type_unit_groups =
8287 allocate_type_unit_groups_table (dwarf2_per_objfile->objfile);
8288 }
8289
8290 /* Do we need to create a new group, or can we use an existing one? */
8291
8292 if (stmt_list)
8293 {
8294 line_offset = DW_UNSND (stmt_list);
8295 ++tu_stats->nr_symtab_sharers;
8296 }
8297 else
8298 {
8299 /* Ugh, no stmt_list. Rare, but we have to handle it.
8300 We can do various things here like create one group per TU or
8301 spread them over multiple groups to split up the expansion work.
8302 To avoid worst case scenarios (too many groups or too large groups)
8303 we, umm, group them in bunches. */
8304 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
8305 | (tu_stats->nr_stmt_less_type_units
8306 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
8307 ++tu_stats->nr_stmt_less_type_units;
8308 }
8309
8310 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
8311 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
8312 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
8313 &type_unit_group_for_lookup, INSERT);
8314 if (*slot != NULL)
8315 {
8316 tu_group = (struct type_unit_group *) *slot;
8317 gdb_assert (tu_group != NULL);
8318 }
8319 else
8320 {
8321 sect_offset line_offset_struct = (sect_offset) line_offset;
8322 tu_group = create_type_unit_group (cu, line_offset_struct);
8323 *slot = tu_group;
8324 ++tu_stats->nr_symtabs;
8325 }
8326
8327 return tu_group;
8328 }
8329 \f
8330 /* Partial symbol tables. */
8331
8332 /* Create a psymtab named NAME and assign it to PER_CU.
8333
8334 The caller must fill in the following details:
8335 dirname, textlow, texthigh. */
8336
8337 static struct partial_symtab *
8338 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
8339 {
8340 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
8341 struct partial_symtab *pst;
8342
8343 pst = start_psymtab_common (objfile, name, 0,
8344 objfile->global_psymbols,
8345 objfile->static_psymbols);
8346
8347 pst->psymtabs_addrmap_supported = 1;
8348
8349 /* This is the glue that links PST into GDB's symbol API. */
8350 pst->read_symtab_private = per_cu;
8351 pst->read_symtab = dwarf2_read_symtab;
8352 per_cu->v.psymtab = pst;
8353
8354 return pst;
8355 }
8356
8357 /* The DATA object passed to process_psymtab_comp_unit_reader has this
8358 type. */
8359
8360 struct process_psymtab_comp_unit_data
8361 {
8362 /* True if we are reading a DW_TAG_partial_unit. */
8363
8364 int want_partial_unit;
8365
8366 /* The "pretend" language that is used if the CU doesn't declare a
8367 language. */
8368
8369 enum language pretend_language;
8370 };
8371
8372 /* die_reader_func for process_psymtab_comp_unit. */
8373
8374 static void
8375 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
8376 const gdb_byte *info_ptr,
8377 struct die_info *comp_unit_die,
8378 int has_children,
8379 void *data)
8380 {
8381 struct dwarf2_cu *cu = reader->cu;
8382 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
8383 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8384 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8385 CORE_ADDR baseaddr;
8386 CORE_ADDR best_lowpc = 0, best_highpc = 0;
8387 struct partial_symtab *pst;
8388 enum pc_bounds_kind cu_bounds_kind;
8389 const char *filename;
8390 struct process_psymtab_comp_unit_data *info
8391 = (struct process_psymtab_comp_unit_data *) data;
8392
8393 if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
8394 return;
8395
8396 gdb_assert (! per_cu->is_debug_types);
8397
8398 prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
8399
8400 cu->list_in_scope = &file_symbols;
8401
8402 /* Allocate a new partial symbol table structure. */
8403 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
8404 if (filename == NULL)
8405 filename = "";
8406
8407 pst = create_partial_symtab (per_cu, filename);
8408
8409 /* This must be done before calling dwarf2_build_include_psymtabs. */
8410 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
8411
8412 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8413
8414 dwarf2_find_base_address (comp_unit_die, cu);
8415
8416 /* Possibly set the default values of LOWPC and HIGHPC from
8417 `DW_AT_ranges'. */
8418 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
8419 &best_highpc, cu, pst);
8420 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
8421 /* Store the contiguous range if it is not empty; it can be empty for
8422 CUs with no code. */
8423 addrmap_set_empty (objfile->psymtabs_addrmap,
8424 gdbarch_adjust_dwarf2_addr (gdbarch,
8425 best_lowpc + baseaddr),
8426 gdbarch_adjust_dwarf2_addr (gdbarch,
8427 best_highpc + baseaddr) - 1,
8428 pst);
8429
8430 /* Check if comp unit has_children.
8431 If so, read the rest of the partial symbols from this comp unit.
8432 If not, there's no more debug_info for this comp unit. */
8433 if (has_children)
8434 {
8435 struct partial_die_info *first_die;
8436 CORE_ADDR lowpc, highpc;
8437
8438 lowpc = ((CORE_ADDR) -1);
8439 highpc = ((CORE_ADDR) 0);
8440
8441 first_die = load_partial_dies (reader, info_ptr, 1);
8442
8443 scan_partial_symbols (first_die, &lowpc, &highpc,
8444 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
8445
8446 /* If we didn't find a lowpc, set it to highpc to avoid
8447 complaints from `maint check'. */
8448 if (lowpc == ((CORE_ADDR) -1))
8449 lowpc = highpc;
8450
8451 /* If the compilation unit didn't have an explicit address range,
8452 then use the information extracted from its child dies. */
8453 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
8454 {
8455 best_lowpc = lowpc;
8456 best_highpc = highpc;
8457 }
8458 }
8459 pst->textlow = gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr);
8460 pst->texthigh = gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr);
8461
8462 end_psymtab_common (objfile, pst);
8463
8464 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
8465 {
8466 int i;
8467 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
8468 struct dwarf2_per_cu_data *iter;
8469
8470 /* Fill in 'dependencies' here; we fill in 'users' in a
8471 post-pass. */
8472 pst->number_of_dependencies = len;
8473 pst->dependencies =
8474 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
8475 for (i = 0;
8476 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
8477 i, iter);
8478 ++i)
8479 pst->dependencies[i] = iter->v.psymtab;
8480
8481 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
8482 }
8483
8484 /* Get the list of files included in the current compilation unit,
8485 and build a psymtab for each of them. */
8486 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
8487
8488 if (dwarf_read_debug)
8489 {
8490 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8491
8492 fprintf_unfiltered (gdb_stdlog,
8493 "Psymtab for %s unit @%s: %s - %s"
8494 ", %d global, %d static syms\n",
8495 per_cu->is_debug_types ? "type" : "comp",
8496 sect_offset_str (per_cu->sect_off),
8497 paddress (gdbarch, pst->textlow),
8498 paddress (gdbarch, pst->texthigh),
8499 pst->n_global_syms, pst->n_static_syms);
8500 }
8501 }
8502
8503 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8504 Process compilation unit THIS_CU for a psymtab. */
8505
8506 static void
8507 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
8508 int want_partial_unit,
8509 enum language pretend_language)
8510 {
8511 /* If this compilation unit was already read in, free the
8512 cached copy in order to read it in again. This is
8513 necessary because we skipped some symbols when we first
8514 read in the compilation unit (see load_partial_dies).
8515 This problem could be avoided, but the benefit is unclear. */
8516 if (this_cu->cu != NULL)
8517 free_one_cached_comp_unit (this_cu);
8518
8519 if (this_cu->is_debug_types)
8520 init_cutu_and_read_dies (this_cu, NULL, 0, 0, build_type_psymtabs_reader,
8521 NULL);
8522 else
8523 {
8524 process_psymtab_comp_unit_data info;
8525 info.want_partial_unit = want_partial_unit;
8526 info.pretend_language = pretend_language;
8527 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
8528 process_psymtab_comp_unit_reader, &info);
8529 }
8530
8531 /* Age out any secondary CUs. */
8532 age_cached_comp_units (this_cu->dwarf2_per_objfile);
8533 }
8534
8535 /* Reader function for build_type_psymtabs. */
8536
8537 static void
8538 build_type_psymtabs_reader (const struct die_reader_specs *reader,
8539 const gdb_byte *info_ptr,
8540 struct die_info *type_unit_die,
8541 int has_children,
8542 void *data)
8543 {
8544 struct dwarf2_per_objfile *dwarf2_per_objfile
8545 = reader->cu->per_cu->dwarf2_per_objfile;
8546 struct objfile *objfile = dwarf2_per_objfile->objfile;
8547 struct dwarf2_cu *cu = reader->cu;
8548 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8549 struct signatured_type *sig_type;
8550 struct type_unit_group *tu_group;
8551 struct attribute *attr;
8552 struct partial_die_info *first_die;
8553 CORE_ADDR lowpc, highpc;
8554 struct partial_symtab *pst;
8555
8556 gdb_assert (data == NULL);
8557 gdb_assert (per_cu->is_debug_types);
8558 sig_type = (struct signatured_type *) per_cu;
8559
8560 if (! has_children)
8561 return;
8562
8563 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
8564 tu_group = get_type_unit_group (cu, attr);
8565
8566 VEC_safe_push (sig_type_ptr, tu_group->tus, sig_type);
8567
8568 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
8569 cu->list_in_scope = &file_symbols;
8570 pst = create_partial_symtab (per_cu, "");
8571 pst->anonymous = 1;
8572
8573 first_die = load_partial_dies (reader, info_ptr, 1);
8574
8575 lowpc = (CORE_ADDR) -1;
8576 highpc = (CORE_ADDR) 0;
8577 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
8578
8579 end_psymtab_common (objfile, pst);
8580 }
8581
8582 /* Struct used to sort TUs by their abbreviation table offset. */
8583
8584 struct tu_abbrev_offset
8585 {
8586 struct signatured_type *sig_type;
8587 sect_offset abbrev_offset;
8588 };
8589
8590 /* Helper routine for build_type_psymtabs_1, passed to std::sort. */
8591
8592 static bool
8593 sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
8594 const struct tu_abbrev_offset &b)
8595 {
8596 return a.abbrev_offset < b.abbrev_offset;
8597 }
8598
8599 /* Efficiently read all the type units.
8600 This does the bulk of the work for build_type_psymtabs.
8601
8602 The efficiency is because we sort TUs by the abbrev table they use and
8603 only read each abbrev table once. In one program there are 200K TUs
8604 sharing 8K abbrev tables.
8605
8606 The main purpose of this function is to support building the
8607 dwarf2_per_objfile->type_unit_groups table.
8608 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
8609 can collapse the search space by grouping them by stmt_list.
8610 The savings can be significant, in the same program from above the 200K TUs
8611 share 8K stmt_list tables.
8612
8613 FUNC is expected to call get_type_unit_group, which will create the
8614 struct type_unit_group if necessary and add it to
8615 dwarf2_per_objfile->type_unit_groups. */
8616
8617 static void
8618 build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
8619 {
8620 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8621 abbrev_table_up abbrev_table;
8622 sect_offset abbrev_offset;
8623 int i;
8624
8625 /* It's up to the caller to not call us multiple times. */
8626 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
8627
8628 if (dwarf2_per_objfile->n_type_units == 0)
8629 return;
8630
8631 /* TUs typically share abbrev tables, and there can be way more TUs than
8632 abbrev tables. Sort by abbrev table to reduce the number of times we
8633 read each abbrev table in.
8634 Alternatives are to punt or to maintain a cache of abbrev tables.
8635 This is simpler and efficient enough for now.
8636
8637 Later we group TUs by their DW_AT_stmt_list value (as this defines the
8638 symtab to use). Typically TUs with the same abbrev offset have the same
8639 stmt_list value too so in practice this should work well.
8640
8641 The basic algorithm here is:
8642
8643 sort TUs by abbrev table
8644 for each TU with same abbrev table:
8645 read abbrev table if first user
8646 read TU top level DIE
8647 [IWBN if DWO skeletons had DW_AT_stmt_list]
8648 call FUNC */
8649
8650 if (dwarf_read_debug)
8651 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
8652
8653 /* Sort in a separate table to maintain the order of all_type_units
8654 for .gdb_index: TU indices directly index all_type_units. */
8655 std::vector<struct tu_abbrev_offset> sorted_by_abbrev
8656 (dwarf2_per_objfile->n_type_units);
8657 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
8658 {
8659 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
8660
8661 sorted_by_abbrev[i].sig_type = sig_type;
8662 sorted_by_abbrev[i].abbrev_offset =
8663 read_abbrev_offset (dwarf2_per_objfile,
8664 sig_type->per_cu.section,
8665 sig_type->per_cu.sect_off);
8666 }
8667 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
8668 sort_tu_by_abbrev_offset);
8669
8670 abbrev_offset = (sect_offset) ~(unsigned) 0;
8671
8672 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
8673 {
8674 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
8675
8676 /* Switch to the next abbrev table if necessary. */
8677 if (abbrev_table == NULL
8678 || tu->abbrev_offset != abbrev_offset)
8679 {
8680 abbrev_offset = tu->abbrev_offset;
8681 abbrev_table =
8682 abbrev_table_read_table (dwarf2_per_objfile,
8683 &dwarf2_per_objfile->abbrev,
8684 abbrev_offset);
8685 ++tu_stats->nr_uniq_abbrev_tables;
8686 }
8687
8688 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table.get (),
8689 0, 0, build_type_psymtabs_reader, NULL);
8690 }
8691 }
8692
8693 /* Print collected type unit statistics. */
8694
8695 static void
8696 print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
8697 {
8698 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8699
8700 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
8701 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
8702 dwarf2_per_objfile->n_type_units);
8703 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
8704 tu_stats->nr_uniq_abbrev_tables);
8705 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
8706 tu_stats->nr_symtabs);
8707 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
8708 tu_stats->nr_symtab_sharers);
8709 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
8710 tu_stats->nr_stmt_less_type_units);
8711 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
8712 tu_stats->nr_all_type_units_reallocs);
8713 }
8714
8715 /* Traversal function for build_type_psymtabs. */
8716
8717 static int
8718 build_type_psymtab_dependencies (void **slot, void *info)
8719 {
8720 struct dwarf2_per_objfile *dwarf2_per_objfile
8721 = (struct dwarf2_per_objfile *) info;
8722 struct objfile *objfile = dwarf2_per_objfile->objfile;
8723 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
8724 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
8725 struct partial_symtab *pst = per_cu->v.psymtab;
8726 int len = VEC_length (sig_type_ptr, tu_group->tus);
8727 struct signatured_type *iter;
8728 int i;
8729
8730 gdb_assert (len > 0);
8731 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
8732
8733 pst->number_of_dependencies = len;
8734 pst->dependencies =
8735 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
8736 for (i = 0;
8737 VEC_iterate (sig_type_ptr, tu_group->tus, i, iter);
8738 ++i)
8739 {
8740 gdb_assert (iter->per_cu.is_debug_types);
8741 pst->dependencies[i] = iter->per_cu.v.psymtab;
8742 iter->type_unit_group = tu_group;
8743 }
8744
8745 VEC_free (sig_type_ptr, tu_group->tus);
8746
8747 return 1;
8748 }
8749
8750 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8751 Build partial symbol tables for the .debug_types comp-units. */
8752
8753 static void
8754 build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
8755 {
8756 if (! create_all_type_units (dwarf2_per_objfile))
8757 return;
8758
8759 build_type_psymtabs_1 (dwarf2_per_objfile);
8760 }
8761
8762 /* Traversal function for process_skeletonless_type_unit.
8763 Read a TU in a DWO file and build partial symbols for it. */
8764
8765 static int
8766 process_skeletonless_type_unit (void **slot, void *info)
8767 {
8768 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
8769 struct dwarf2_per_objfile *dwarf2_per_objfile
8770 = (struct dwarf2_per_objfile *) info;
8771 struct signatured_type find_entry, *entry;
8772
8773 /* If this TU doesn't exist in the global table, add it and read it in. */
8774
8775 if (dwarf2_per_objfile->signatured_types == NULL)
8776 {
8777 dwarf2_per_objfile->signatured_types
8778 = allocate_signatured_type_table (dwarf2_per_objfile->objfile);
8779 }
8780
8781 find_entry.signature = dwo_unit->signature;
8782 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
8783 INSERT);
8784 /* If we've already seen this type there's nothing to do. What's happening
8785 is we're doing our own version of comdat-folding here. */
8786 if (*slot != NULL)
8787 return 1;
8788
8789 /* This does the job that create_all_type_units would have done for
8790 this TU. */
8791 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
8792 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
8793 *slot = entry;
8794
8795 /* This does the job that build_type_psymtabs_1 would have done. */
8796 init_cutu_and_read_dies (&entry->per_cu, NULL, 0, 0,
8797 build_type_psymtabs_reader, NULL);
8798
8799 return 1;
8800 }
8801
8802 /* Traversal function for process_skeletonless_type_units. */
8803
8804 static int
8805 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
8806 {
8807 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8808
8809 if (dwo_file->tus != NULL)
8810 {
8811 htab_traverse_noresize (dwo_file->tus,
8812 process_skeletonless_type_unit, info);
8813 }
8814
8815 return 1;
8816 }
8817
8818 /* Scan all TUs of DWO files, verifying we've processed them.
8819 This is needed in case a TU was emitted without its skeleton.
8820 Note: This can't be done until we know what all the DWO files are. */
8821
8822 static void
8823 process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
8824 {
8825 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
8826 if (get_dwp_file (dwarf2_per_objfile) == NULL
8827 && dwarf2_per_objfile->dwo_files != NULL)
8828 {
8829 htab_traverse_noresize (dwarf2_per_objfile->dwo_files,
8830 process_dwo_file_for_skeletonless_type_units,
8831 dwarf2_per_objfile);
8832 }
8833 }
8834
8835 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
8836
8837 static void
8838 set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
8839 {
8840 int i;
8841
8842 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
8843 {
8844 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
8845 struct partial_symtab *pst = per_cu->v.psymtab;
8846 int j;
8847
8848 if (pst == NULL)
8849 continue;
8850
8851 for (j = 0; j < pst->number_of_dependencies; ++j)
8852 {
8853 /* Set the 'user' field only if it is not already set. */
8854 if (pst->dependencies[j]->user == NULL)
8855 pst->dependencies[j]->user = pst;
8856 }
8857 }
8858 }
8859
8860 /* Build the partial symbol table by doing a quick pass through the
8861 .debug_info and .debug_abbrev sections. */
8862
8863 static void
8864 dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
8865 {
8866 struct cleanup *back_to;
8867 int i;
8868 struct objfile *objfile = dwarf2_per_objfile->objfile;
8869
8870 if (dwarf_read_debug)
8871 {
8872 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
8873 objfile_name (objfile));
8874 }
8875
8876 dwarf2_per_objfile->reading_partial_symbols = 1;
8877
8878 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
8879
8880 /* Any cached compilation units will be linked by the per-objfile
8881 read_in_chain. Make sure to free them when we're done. */
8882 back_to = make_cleanup (free_cached_comp_units, dwarf2_per_objfile);
8883
8884 build_type_psymtabs (dwarf2_per_objfile);
8885
8886 create_all_comp_units (dwarf2_per_objfile);
8887
8888 /* Create a temporary address map on a temporary obstack. We later
8889 copy this to the final obstack. */
8890 auto_obstack temp_obstack;
8891
8892 scoped_restore save_psymtabs_addrmap
8893 = make_scoped_restore (&objfile->psymtabs_addrmap,
8894 addrmap_create_mutable (&temp_obstack));
8895
8896 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
8897 {
8898 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
8899
8900 process_psymtab_comp_unit (per_cu, 0, language_minimal);
8901 }
8902
8903 /* This has to wait until we read the CUs, we need the list of DWOs. */
8904 process_skeletonless_type_units (dwarf2_per_objfile);
8905
8906 /* Now that all TUs have been processed we can fill in the dependencies. */
8907 if (dwarf2_per_objfile->type_unit_groups != NULL)
8908 {
8909 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
8910 build_type_psymtab_dependencies, dwarf2_per_objfile);
8911 }
8912
8913 if (dwarf_read_debug)
8914 print_tu_stats (dwarf2_per_objfile);
8915
8916 set_partial_user (dwarf2_per_objfile);
8917
8918 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
8919 &objfile->objfile_obstack);
8920 /* At this point we want to keep the address map. */
8921 save_psymtabs_addrmap.release ();
8922
8923 do_cleanups (back_to);
8924
8925 if (dwarf_read_debug)
8926 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
8927 objfile_name (objfile));
8928 }
8929
8930 /* die_reader_func for load_partial_comp_unit. */
8931
8932 static void
8933 load_partial_comp_unit_reader (const struct die_reader_specs *reader,
8934 const gdb_byte *info_ptr,
8935 struct die_info *comp_unit_die,
8936 int has_children,
8937 void *data)
8938 {
8939 struct dwarf2_cu *cu = reader->cu;
8940
8941 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
8942
8943 /* Check if comp unit has_children.
8944 If so, read the rest of the partial symbols from this comp unit.
8945 If not, there's no more debug_info for this comp unit. */
8946 if (has_children)
8947 load_partial_dies (reader, info_ptr, 0);
8948 }
8949
8950 /* Load the partial DIEs for a secondary CU into memory.
8951 This is also used when rereading a primary CU with load_all_dies. */
8952
8953 static void
8954 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
8955 {
8956 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
8957 load_partial_comp_unit_reader, NULL);
8958 }
8959
8960 static void
8961 read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
8962 struct dwarf2_section_info *section,
8963 struct dwarf2_section_info *abbrev_section,
8964 unsigned int is_dwz,
8965 int *n_allocated,
8966 int *n_comp_units,
8967 struct dwarf2_per_cu_data ***all_comp_units)
8968 {
8969 const gdb_byte *info_ptr;
8970 struct objfile *objfile = dwarf2_per_objfile->objfile;
8971
8972 if (dwarf_read_debug)
8973 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
8974 get_section_name (section),
8975 get_section_file_name (section));
8976
8977 dwarf2_read_section (objfile, section);
8978
8979 info_ptr = section->buffer;
8980
8981 while (info_ptr < section->buffer + section->size)
8982 {
8983 struct dwarf2_per_cu_data *this_cu;
8984
8985 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
8986
8987 comp_unit_head cu_header;
8988 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
8989 abbrev_section, info_ptr,
8990 rcuh_kind::COMPILE);
8991
8992 /* Save the compilation unit for later lookup. */
8993 if (cu_header.unit_type != DW_UT_type)
8994 {
8995 this_cu = XOBNEW (&objfile->objfile_obstack,
8996 struct dwarf2_per_cu_data);
8997 memset (this_cu, 0, sizeof (*this_cu));
8998 }
8999 else
9000 {
9001 auto sig_type = XOBNEW (&objfile->objfile_obstack,
9002 struct signatured_type);
9003 memset (sig_type, 0, sizeof (*sig_type));
9004 sig_type->signature = cu_header.signature;
9005 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
9006 this_cu = &sig_type->per_cu;
9007 }
9008 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
9009 this_cu->sect_off = sect_off;
9010 this_cu->length = cu_header.length + cu_header.initial_length_size;
9011 this_cu->is_dwz = is_dwz;
9012 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
9013 this_cu->section = section;
9014
9015 if (*n_comp_units == *n_allocated)
9016 {
9017 *n_allocated *= 2;
9018 *all_comp_units = XRESIZEVEC (struct dwarf2_per_cu_data *,
9019 *all_comp_units, *n_allocated);
9020 }
9021 (*all_comp_units)[*n_comp_units] = this_cu;
9022 ++*n_comp_units;
9023
9024 info_ptr = info_ptr + this_cu->length;
9025 }
9026 }
9027
9028 /* Create a list of all compilation units in OBJFILE.
9029 This is only done for -readnow and building partial symtabs. */
9030
9031 static void
9032 create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
9033 {
9034 int n_allocated;
9035 int n_comp_units;
9036 struct dwarf2_per_cu_data **all_comp_units;
9037 struct dwz_file *dwz;
9038 struct objfile *objfile = dwarf2_per_objfile->objfile;
9039
9040 n_comp_units = 0;
9041 n_allocated = 10;
9042 all_comp_units = XNEWVEC (struct dwarf2_per_cu_data *, n_allocated);
9043
9044 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
9045 &dwarf2_per_objfile->abbrev, 0,
9046 &n_allocated, &n_comp_units, &all_comp_units);
9047
9048 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
9049 if (dwz != NULL)
9050 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
9051 1, &n_allocated, &n_comp_units,
9052 &all_comp_units);
9053
9054 dwarf2_per_objfile->all_comp_units = XOBNEWVEC (&objfile->objfile_obstack,
9055 struct dwarf2_per_cu_data *,
9056 n_comp_units);
9057 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
9058 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
9059 xfree (all_comp_units);
9060 dwarf2_per_objfile->n_comp_units = n_comp_units;
9061 }
9062
9063 /* Process all loaded DIEs for compilation unit CU, starting at
9064 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
9065 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
9066 DW_AT_ranges). See the comments of add_partial_subprogram on how
9067 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
9068
9069 static void
9070 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
9071 CORE_ADDR *highpc, int set_addrmap,
9072 struct dwarf2_cu *cu)
9073 {
9074 struct partial_die_info *pdi;
9075
9076 /* Now, march along the PDI's, descending into ones which have
9077 interesting children but skipping the children of the other ones,
9078 until we reach the end of the compilation unit. */
9079
9080 pdi = first_die;
9081
9082 while (pdi != NULL)
9083 {
9084 pdi->fixup (cu);
9085
9086 /* Anonymous namespaces or modules have no name but have interesting
9087 children, so we need to look at them. Ditto for anonymous
9088 enums. */
9089
9090 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
9091 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
9092 || pdi->tag == DW_TAG_imported_unit
9093 || pdi->tag == DW_TAG_inlined_subroutine)
9094 {
9095 switch (pdi->tag)
9096 {
9097 case DW_TAG_subprogram:
9098 case DW_TAG_inlined_subroutine:
9099 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
9100 break;
9101 case DW_TAG_constant:
9102 case DW_TAG_variable:
9103 case DW_TAG_typedef:
9104 case DW_TAG_union_type:
9105 if (!pdi->is_declaration)
9106 {
9107 add_partial_symbol (pdi, cu);
9108 }
9109 break;
9110 case DW_TAG_class_type:
9111 case DW_TAG_interface_type:
9112 case DW_TAG_structure_type:
9113 if (!pdi->is_declaration)
9114 {
9115 add_partial_symbol (pdi, cu);
9116 }
9117 if ((cu->language == language_rust
9118 || cu->language == language_cplus) && pdi->has_children)
9119 scan_partial_symbols (pdi->die_child, lowpc, highpc,
9120 set_addrmap, cu);
9121 break;
9122 case DW_TAG_enumeration_type:
9123 if (!pdi->is_declaration)
9124 add_partial_enumeration (pdi, cu);
9125 break;
9126 case DW_TAG_base_type:
9127 case DW_TAG_subrange_type:
9128 /* File scope base type definitions are added to the partial
9129 symbol table. */
9130 add_partial_symbol (pdi, cu);
9131 break;
9132 case DW_TAG_namespace:
9133 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
9134 break;
9135 case DW_TAG_module:
9136 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
9137 break;
9138 case DW_TAG_imported_unit:
9139 {
9140 struct dwarf2_per_cu_data *per_cu;
9141
9142 /* For now we don't handle imported units in type units. */
9143 if (cu->per_cu->is_debug_types)
9144 {
9145 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9146 " supported in type units [in module %s]"),
9147 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
9148 }
9149
9150 per_cu = dwarf2_find_containing_comp_unit
9151 (pdi->d.sect_off, pdi->is_dwz,
9152 cu->per_cu->dwarf2_per_objfile);
9153
9154 /* Go read the partial unit, if needed. */
9155 if (per_cu->v.psymtab == NULL)
9156 process_psymtab_comp_unit (per_cu, 1, cu->language);
9157
9158 VEC_safe_push (dwarf2_per_cu_ptr,
9159 cu->per_cu->imported_symtabs, per_cu);
9160 }
9161 break;
9162 case DW_TAG_imported_declaration:
9163 add_partial_symbol (pdi, cu);
9164 break;
9165 default:
9166 break;
9167 }
9168 }
9169
9170 /* If the die has a sibling, skip to the sibling. */
9171
9172 pdi = pdi->die_sibling;
9173 }
9174 }
9175
9176 /* Functions used to compute the fully scoped name of a partial DIE.
9177
9178 Normally, this is simple. For C++, the parent DIE's fully scoped
9179 name is concatenated with "::" and the partial DIE's name.
9180 Enumerators are an exception; they use the scope of their parent
9181 enumeration type, i.e. the name of the enumeration type is not
9182 prepended to the enumerator.
9183
9184 There are two complexities. One is DW_AT_specification; in this
9185 case "parent" means the parent of the target of the specification,
9186 instead of the direct parent of the DIE. The other is compilers
9187 which do not emit DW_TAG_namespace; in this case we try to guess
9188 the fully qualified name of structure types from their members'
9189 linkage names. This must be done using the DIE's children rather
9190 than the children of any DW_AT_specification target. We only need
9191 to do this for structures at the top level, i.e. if the target of
9192 any DW_AT_specification (if any; otherwise the DIE itself) does not
9193 have a parent. */
9194
9195 /* Compute the scope prefix associated with PDI's parent, in
9196 compilation unit CU. The result will be allocated on CU's
9197 comp_unit_obstack, or a copy of the already allocated PDI->NAME
9198 field. NULL is returned if no prefix is necessary. */
9199 static const char *
9200 partial_die_parent_scope (struct partial_die_info *pdi,
9201 struct dwarf2_cu *cu)
9202 {
9203 const char *grandparent_scope;
9204 struct partial_die_info *parent, *real_pdi;
9205
9206 /* We need to look at our parent DIE; if we have a DW_AT_specification,
9207 then this means the parent of the specification DIE. */
9208
9209 real_pdi = pdi;
9210 while (real_pdi->has_specification)
9211 real_pdi = find_partial_die (real_pdi->spec_offset,
9212 real_pdi->spec_is_dwz, cu);
9213
9214 parent = real_pdi->die_parent;
9215 if (parent == NULL)
9216 return NULL;
9217
9218 if (parent->scope_set)
9219 return parent->scope;
9220
9221 parent->fixup (cu);
9222
9223 grandparent_scope = partial_die_parent_scope (parent, cu);
9224
9225 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
9226 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
9227 Work around this problem here. */
9228 if (cu->language == language_cplus
9229 && parent->tag == DW_TAG_namespace
9230 && strcmp (parent->name, "::") == 0
9231 && grandparent_scope == NULL)
9232 {
9233 parent->scope = NULL;
9234 parent->scope_set = 1;
9235 return NULL;
9236 }
9237
9238 if (pdi->tag == DW_TAG_enumerator)
9239 /* Enumerators should not get the name of the enumeration as a prefix. */
9240 parent->scope = grandparent_scope;
9241 else if (parent->tag == DW_TAG_namespace
9242 || parent->tag == DW_TAG_module
9243 || parent->tag == DW_TAG_structure_type
9244 || parent->tag == DW_TAG_class_type
9245 || parent->tag == DW_TAG_interface_type
9246 || parent->tag == DW_TAG_union_type
9247 || parent->tag == DW_TAG_enumeration_type)
9248 {
9249 if (grandparent_scope == NULL)
9250 parent->scope = parent->name;
9251 else
9252 parent->scope = typename_concat (&cu->comp_unit_obstack,
9253 grandparent_scope,
9254 parent->name, 0, cu);
9255 }
9256 else
9257 {
9258 /* FIXME drow/2004-04-01: What should we be doing with
9259 function-local names? For partial symbols, we should probably be
9260 ignoring them. */
9261 complaint (&symfile_complaints,
9262 _("unhandled containing DIE tag %d for DIE at %s"),
9263 parent->tag, sect_offset_str (pdi->sect_off));
9264 parent->scope = grandparent_scope;
9265 }
9266
9267 parent->scope_set = 1;
9268 return parent->scope;
9269 }
9270
9271 /* Return the fully scoped name associated with PDI, from compilation unit
9272 CU. The result will be allocated with malloc. */
9273
9274 static char *
9275 partial_die_full_name (struct partial_die_info *pdi,
9276 struct dwarf2_cu *cu)
9277 {
9278 const char *parent_scope;
9279
9280 /* If this is a template instantiation, we can not work out the
9281 template arguments from partial DIEs. So, unfortunately, we have
9282 to go through the full DIEs. At least any work we do building
9283 types here will be reused if full symbols are loaded later. */
9284 if (pdi->has_template_arguments)
9285 {
9286 pdi->fixup (cu);
9287
9288 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
9289 {
9290 struct die_info *die;
9291 struct attribute attr;
9292 struct dwarf2_cu *ref_cu = cu;
9293
9294 /* DW_FORM_ref_addr is using section offset. */
9295 attr.name = (enum dwarf_attribute) 0;
9296 attr.form = DW_FORM_ref_addr;
9297 attr.u.unsnd = to_underlying (pdi->sect_off);
9298 die = follow_die_ref (NULL, &attr, &ref_cu);
9299
9300 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
9301 }
9302 }
9303
9304 parent_scope = partial_die_parent_scope (pdi, cu);
9305 if (parent_scope == NULL)
9306 return NULL;
9307 else
9308 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
9309 }
9310
9311 static void
9312 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
9313 {
9314 struct dwarf2_per_objfile *dwarf2_per_objfile
9315 = cu->per_cu->dwarf2_per_objfile;
9316 struct objfile *objfile = dwarf2_per_objfile->objfile;
9317 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9318 CORE_ADDR addr = 0;
9319 const char *actual_name = NULL;
9320 CORE_ADDR baseaddr;
9321 char *built_actual_name;
9322
9323 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9324
9325 built_actual_name = partial_die_full_name (pdi, cu);
9326 if (built_actual_name != NULL)
9327 actual_name = built_actual_name;
9328
9329 if (actual_name == NULL)
9330 actual_name = pdi->name;
9331
9332 switch (pdi->tag)
9333 {
9334 case DW_TAG_inlined_subroutine:
9335 case DW_TAG_subprogram:
9336 addr = gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr);
9337 if (pdi->is_external || cu->language == language_ada)
9338 {
9339 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
9340 of the global scope. But in Ada, we want to be able to access
9341 nested procedures globally. So all Ada subprograms are stored
9342 in the global scope. */
9343 add_psymbol_to_list (actual_name, strlen (actual_name),
9344 built_actual_name != NULL,
9345 VAR_DOMAIN, LOC_BLOCK,
9346 &objfile->global_psymbols,
9347 addr, cu->language, objfile);
9348 }
9349 else
9350 {
9351 add_psymbol_to_list (actual_name, strlen (actual_name),
9352 built_actual_name != NULL,
9353 VAR_DOMAIN, LOC_BLOCK,
9354 &objfile->static_psymbols,
9355 addr, cu->language, objfile);
9356 }
9357
9358 if (pdi->main_subprogram && actual_name != NULL)
9359 set_objfile_main_name (objfile, actual_name, cu->language);
9360 break;
9361 case DW_TAG_constant:
9362 {
9363 std::vector<partial_symbol *> *list;
9364
9365 if (pdi->is_external)
9366 list = &objfile->global_psymbols;
9367 else
9368 list = &objfile->static_psymbols;
9369 add_psymbol_to_list (actual_name, strlen (actual_name),
9370 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
9371 list, 0, cu->language, objfile);
9372 }
9373 break;
9374 case DW_TAG_variable:
9375 if (pdi->d.locdesc)
9376 addr = decode_locdesc (pdi->d.locdesc, cu);
9377
9378 if (pdi->d.locdesc
9379 && addr == 0
9380 && !dwarf2_per_objfile->has_section_at_zero)
9381 {
9382 /* A global or static variable may also have been stripped
9383 out by the linker if unused, in which case its address
9384 will be nullified; do not add such variables into partial
9385 symbol table then. */
9386 }
9387 else if (pdi->is_external)
9388 {
9389 /* Global Variable.
9390 Don't enter into the minimal symbol tables as there is
9391 a minimal symbol table entry from the ELF symbols already.
9392 Enter into partial symbol table if it has a location
9393 descriptor or a type.
9394 If the location descriptor is missing, new_symbol will create
9395 a LOC_UNRESOLVED symbol, the address of the variable will then
9396 be determined from the minimal symbol table whenever the variable
9397 is referenced.
9398 The address for the partial symbol table entry is not
9399 used by GDB, but it comes in handy for debugging partial symbol
9400 table building. */
9401
9402 if (pdi->d.locdesc || pdi->has_type)
9403 add_psymbol_to_list (actual_name, strlen (actual_name),
9404 built_actual_name != NULL,
9405 VAR_DOMAIN, LOC_STATIC,
9406 &objfile->global_psymbols,
9407 addr + baseaddr,
9408 cu->language, objfile);
9409 }
9410 else
9411 {
9412 int has_loc = pdi->d.locdesc != NULL;
9413
9414 /* Static Variable. Skip symbols whose value we cannot know (those
9415 without location descriptors or constant values). */
9416 if (!has_loc && !pdi->has_const_value)
9417 {
9418 xfree (built_actual_name);
9419 return;
9420 }
9421
9422 add_psymbol_to_list (actual_name, strlen (actual_name),
9423 built_actual_name != NULL,
9424 VAR_DOMAIN, LOC_STATIC,
9425 &objfile->static_psymbols,
9426 has_loc ? addr + baseaddr : (CORE_ADDR) 0,
9427 cu->language, objfile);
9428 }
9429 break;
9430 case DW_TAG_typedef:
9431 case DW_TAG_base_type:
9432 case DW_TAG_subrange_type:
9433 add_psymbol_to_list (actual_name, strlen (actual_name),
9434 built_actual_name != NULL,
9435 VAR_DOMAIN, LOC_TYPEDEF,
9436 &objfile->static_psymbols,
9437 0, cu->language, objfile);
9438 break;
9439 case DW_TAG_imported_declaration:
9440 case DW_TAG_namespace:
9441 add_psymbol_to_list (actual_name, strlen (actual_name),
9442 built_actual_name != NULL,
9443 VAR_DOMAIN, LOC_TYPEDEF,
9444 &objfile->global_psymbols,
9445 0, cu->language, objfile);
9446 break;
9447 case DW_TAG_module:
9448 add_psymbol_to_list (actual_name, strlen (actual_name),
9449 built_actual_name != NULL,
9450 MODULE_DOMAIN, LOC_TYPEDEF,
9451 &objfile->global_psymbols,
9452 0, cu->language, objfile);
9453 break;
9454 case DW_TAG_class_type:
9455 case DW_TAG_interface_type:
9456 case DW_TAG_structure_type:
9457 case DW_TAG_union_type:
9458 case DW_TAG_enumeration_type:
9459 /* Skip external references. The DWARF standard says in the section
9460 about "Structure, Union, and Class Type Entries": "An incomplete
9461 structure, union or class type is represented by a structure,
9462 union or class entry that does not have a byte size attribute
9463 and that has a DW_AT_declaration attribute." */
9464 if (!pdi->has_byte_size && pdi->is_declaration)
9465 {
9466 xfree (built_actual_name);
9467 return;
9468 }
9469
9470 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
9471 static vs. global. */
9472 add_psymbol_to_list (actual_name, strlen (actual_name),
9473 built_actual_name != NULL,
9474 STRUCT_DOMAIN, LOC_TYPEDEF,
9475 cu->language == language_cplus
9476 ? &objfile->global_psymbols
9477 : &objfile->static_psymbols,
9478 0, cu->language, objfile);
9479
9480 break;
9481 case DW_TAG_enumerator:
9482 add_psymbol_to_list (actual_name, strlen (actual_name),
9483 built_actual_name != NULL,
9484 VAR_DOMAIN, LOC_CONST,
9485 cu->language == language_cplus
9486 ? &objfile->global_psymbols
9487 : &objfile->static_psymbols,
9488 0, cu->language, objfile);
9489 break;
9490 default:
9491 break;
9492 }
9493
9494 xfree (built_actual_name);
9495 }
9496
9497 /* Read a partial die corresponding to a namespace; also, add a symbol
9498 corresponding to that namespace to the symbol table. NAMESPACE is
9499 the name of the enclosing namespace. */
9500
9501 static void
9502 add_partial_namespace (struct partial_die_info *pdi,
9503 CORE_ADDR *lowpc, CORE_ADDR *highpc,
9504 int set_addrmap, struct dwarf2_cu *cu)
9505 {
9506 /* Add a symbol for the namespace. */
9507
9508 add_partial_symbol (pdi, cu);
9509
9510 /* Now scan partial symbols in that namespace. */
9511
9512 if (pdi->has_children)
9513 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
9514 }
9515
9516 /* Read a partial die corresponding to a Fortran module. */
9517
9518 static void
9519 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
9520 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
9521 {
9522 /* Add a symbol for the namespace. */
9523
9524 add_partial_symbol (pdi, cu);
9525
9526 /* Now scan partial symbols in that module. */
9527
9528 if (pdi->has_children)
9529 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
9530 }
9531
9532 /* Read a partial die corresponding to a subprogram or an inlined
9533 subprogram and create a partial symbol for that subprogram.
9534 When the CU language allows it, this routine also defines a partial
9535 symbol for each nested subprogram that this subprogram contains.
9536 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
9537 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
9538
9539 PDI may also be a lexical block, in which case we simply search
9540 recursively for subprograms defined inside that lexical block.
9541 Again, this is only performed when the CU language allows this
9542 type of definitions. */
9543
9544 static void
9545 add_partial_subprogram (struct partial_die_info *pdi,
9546 CORE_ADDR *lowpc, CORE_ADDR *highpc,
9547 int set_addrmap, struct dwarf2_cu *cu)
9548 {
9549 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
9550 {
9551 if (pdi->has_pc_info)
9552 {
9553 if (pdi->lowpc < *lowpc)
9554 *lowpc = pdi->lowpc;
9555 if (pdi->highpc > *highpc)
9556 *highpc = pdi->highpc;
9557 if (set_addrmap)
9558 {
9559 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9560 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9561 CORE_ADDR baseaddr;
9562 CORE_ADDR highpc;
9563 CORE_ADDR lowpc;
9564
9565 baseaddr = ANOFFSET (objfile->section_offsets,
9566 SECT_OFF_TEXT (objfile));
9567 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
9568 pdi->lowpc + baseaddr);
9569 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
9570 pdi->highpc + baseaddr);
9571 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
9572 cu->per_cu->v.psymtab);
9573 }
9574 }
9575
9576 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
9577 {
9578 if (!pdi->is_declaration)
9579 /* Ignore subprogram DIEs that do not have a name, they are
9580 illegal. Do not emit a complaint at this point, we will
9581 do so when we convert this psymtab into a symtab. */
9582 if (pdi->name)
9583 add_partial_symbol (pdi, cu);
9584 }
9585 }
9586
9587 if (! pdi->has_children)
9588 return;
9589
9590 if (cu->language == language_ada)
9591 {
9592 pdi = pdi->die_child;
9593 while (pdi != NULL)
9594 {
9595 pdi->fixup (cu);
9596 if (pdi->tag == DW_TAG_subprogram
9597 || pdi->tag == DW_TAG_inlined_subroutine
9598 || pdi->tag == DW_TAG_lexical_block)
9599 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
9600 pdi = pdi->die_sibling;
9601 }
9602 }
9603 }
9604
9605 /* Read a partial die corresponding to an enumeration type. */
9606
9607 static void
9608 add_partial_enumeration (struct partial_die_info *enum_pdi,
9609 struct dwarf2_cu *cu)
9610 {
9611 struct partial_die_info *pdi;
9612
9613 if (enum_pdi->name != NULL)
9614 add_partial_symbol (enum_pdi, cu);
9615
9616 pdi = enum_pdi->die_child;
9617 while (pdi)
9618 {
9619 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
9620 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
9621 else
9622 add_partial_symbol (pdi, cu);
9623 pdi = pdi->die_sibling;
9624 }
9625 }
9626
9627 /* Return the initial uleb128 in the die at INFO_PTR. */
9628
9629 static unsigned int
9630 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
9631 {
9632 unsigned int bytes_read;
9633
9634 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9635 }
9636
9637 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
9638 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
9639
9640 Return the corresponding abbrev, or NULL if the number is zero (indicating
9641 an empty DIE). In either case *BYTES_READ will be set to the length of
9642 the initial number. */
9643
9644 static struct abbrev_info *
9645 peek_die_abbrev (const die_reader_specs &reader,
9646 const gdb_byte *info_ptr, unsigned int *bytes_read)
9647 {
9648 dwarf2_cu *cu = reader.cu;
9649 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
9650 unsigned int abbrev_number
9651 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
9652
9653 if (abbrev_number == 0)
9654 return NULL;
9655
9656 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
9657 if (!abbrev)
9658 {
9659 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9660 " at offset %s [in module %s]"),
9661 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9662 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
9663 }
9664
9665 return abbrev;
9666 }
9667
9668 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9669 Returns a pointer to the end of a series of DIEs, terminated by an empty
9670 DIE. Any children of the skipped DIEs will also be skipped. */
9671
9672 static const gdb_byte *
9673 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
9674 {
9675 while (1)
9676 {
9677 unsigned int bytes_read;
9678 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
9679
9680 if (abbrev == NULL)
9681 return info_ptr + bytes_read;
9682 else
9683 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
9684 }
9685 }
9686
9687 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9688 INFO_PTR should point just after the initial uleb128 of a DIE, and the
9689 abbrev corresponding to that skipped uleb128 should be passed in
9690 ABBREV. Returns a pointer to this DIE's sibling, skipping any
9691 children. */
9692
9693 static const gdb_byte *
9694 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
9695 struct abbrev_info *abbrev)
9696 {
9697 unsigned int bytes_read;
9698 struct attribute attr;
9699 bfd *abfd = reader->abfd;
9700 struct dwarf2_cu *cu = reader->cu;
9701 const gdb_byte *buffer = reader->buffer;
9702 const gdb_byte *buffer_end = reader->buffer_end;
9703 unsigned int form, i;
9704
9705 for (i = 0; i < abbrev->num_attrs; i++)
9706 {
9707 /* The only abbrev we care about is DW_AT_sibling. */
9708 if (abbrev->attrs[i].name == DW_AT_sibling)
9709 {
9710 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
9711 if (attr.form == DW_FORM_ref_addr)
9712 complaint (&symfile_complaints,
9713 _("ignoring absolute DW_AT_sibling"));
9714 else
9715 {
9716 sect_offset off = dwarf2_get_ref_die_offset (&attr);
9717 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
9718
9719 if (sibling_ptr < info_ptr)
9720 complaint (&symfile_complaints,
9721 _("DW_AT_sibling points backwards"));
9722 else if (sibling_ptr > reader->buffer_end)
9723 dwarf2_section_buffer_overflow_complaint (reader->die_section);
9724 else
9725 return sibling_ptr;
9726 }
9727 }
9728
9729 /* If it isn't DW_AT_sibling, skip this attribute. */
9730 form = abbrev->attrs[i].form;
9731 skip_attribute:
9732 switch (form)
9733 {
9734 case DW_FORM_ref_addr:
9735 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
9736 and later it is offset sized. */
9737 if (cu->header.version == 2)
9738 info_ptr += cu->header.addr_size;
9739 else
9740 info_ptr += cu->header.offset_size;
9741 break;
9742 case DW_FORM_GNU_ref_alt:
9743 info_ptr += cu->header.offset_size;
9744 break;
9745 case DW_FORM_addr:
9746 info_ptr += cu->header.addr_size;
9747 break;
9748 case DW_FORM_data1:
9749 case DW_FORM_ref1:
9750 case DW_FORM_flag:
9751 info_ptr += 1;
9752 break;
9753 case DW_FORM_flag_present:
9754 case DW_FORM_implicit_const:
9755 break;
9756 case DW_FORM_data2:
9757 case DW_FORM_ref2:
9758 info_ptr += 2;
9759 break;
9760 case DW_FORM_data4:
9761 case DW_FORM_ref4:
9762 info_ptr += 4;
9763 break;
9764 case DW_FORM_data8:
9765 case DW_FORM_ref8:
9766 case DW_FORM_ref_sig8:
9767 info_ptr += 8;
9768 break;
9769 case DW_FORM_data16:
9770 info_ptr += 16;
9771 break;
9772 case DW_FORM_string:
9773 read_direct_string (abfd, info_ptr, &bytes_read);
9774 info_ptr += bytes_read;
9775 break;
9776 case DW_FORM_sec_offset:
9777 case DW_FORM_strp:
9778 case DW_FORM_GNU_strp_alt:
9779 info_ptr += cu->header.offset_size;
9780 break;
9781 case DW_FORM_exprloc:
9782 case DW_FORM_block:
9783 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9784 info_ptr += bytes_read;
9785 break;
9786 case DW_FORM_block1:
9787 info_ptr += 1 + read_1_byte (abfd, info_ptr);
9788 break;
9789 case DW_FORM_block2:
9790 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
9791 break;
9792 case DW_FORM_block4:
9793 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
9794 break;
9795 case DW_FORM_sdata:
9796 case DW_FORM_udata:
9797 case DW_FORM_ref_udata:
9798 case DW_FORM_GNU_addr_index:
9799 case DW_FORM_GNU_str_index:
9800 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
9801 break;
9802 case DW_FORM_indirect:
9803 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9804 info_ptr += bytes_read;
9805 /* We need to continue parsing from here, so just go back to
9806 the top. */
9807 goto skip_attribute;
9808
9809 default:
9810 error (_("Dwarf Error: Cannot handle %s "
9811 "in DWARF reader [in module %s]"),
9812 dwarf_form_name (form),
9813 bfd_get_filename (abfd));
9814 }
9815 }
9816
9817 if (abbrev->has_children)
9818 return skip_children (reader, info_ptr);
9819 else
9820 return info_ptr;
9821 }
9822
9823 /* Locate ORIG_PDI's sibling.
9824 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
9825
9826 static const gdb_byte *
9827 locate_pdi_sibling (const struct die_reader_specs *reader,
9828 struct partial_die_info *orig_pdi,
9829 const gdb_byte *info_ptr)
9830 {
9831 /* Do we know the sibling already? */
9832
9833 if (orig_pdi->sibling)
9834 return orig_pdi->sibling;
9835
9836 /* Are there any children to deal with? */
9837
9838 if (!orig_pdi->has_children)
9839 return info_ptr;
9840
9841 /* Skip the children the long way. */
9842
9843 return skip_children (reader, info_ptr);
9844 }
9845
9846 /* Expand this partial symbol table into a full symbol table. SELF is
9847 not NULL. */
9848
9849 static void
9850 dwarf2_read_symtab (struct partial_symtab *self,
9851 struct objfile *objfile)
9852 {
9853 struct dwarf2_per_objfile *dwarf2_per_objfile
9854 = get_dwarf2_per_objfile (objfile);
9855
9856 if (self->readin)
9857 {
9858 warning (_("bug: psymtab for %s is already read in."),
9859 self->filename);
9860 }
9861 else
9862 {
9863 if (info_verbose)
9864 {
9865 printf_filtered (_("Reading in symbols for %s..."),
9866 self->filename);
9867 gdb_flush (gdb_stdout);
9868 }
9869
9870 /* If this psymtab is constructed from a debug-only objfile, the
9871 has_section_at_zero flag will not necessarily be correct. We
9872 can get the correct value for this flag by looking at the data
9873 associated with the (presumably stripped) associated objfile. */
9874 if (objfile->separate_debug_objfile_backlink)
9875 {
9876 struct dwarf2_per_objfile *dpo_backlink
9877 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
9878
9879 dwarf2_per_objfile->has_section_at_zero
9880 = dpo_backlink->has_section_at_zero;
9881 }
9882
9883 dwarf2_per_objfile->reading_partial_symbols = 0;
9884
9885 psymtab_to_symtab_1 (self);
9886
9887 /* Finish up the debug error message. */
9888 if (info_verbose)
9889 printf_filtered (_("done.\n"));
9890 }
9891
9892 process_cu_includes (dwarf2_per_objfile);
9893 }
9894 \f
9895 /* Reading in full CUs. */
9896
9897 /* Add PER_CU to the queue. */
9898
9899 static void
9900 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
9901 enum language pretend_language)
9902 {
9903 struct dwarf2_queue_item *item;
9904
9905 per_cu->queued = 1;
9906 item = XNEW (struct dwarf2_queue_item);
9907 item->per_cu = per_cu;
9908 item->pretend_language = pretend_language;
9909 item->next = NULL;
9910
9911 if (dwarf2_queue == NULL)
9912 dwarf2_queue = item;
9913 else
9914 dwarf2_queue_tail->next = item;
9915
9916 dwarf2_queue_tail = item;
9917 }
9918
9919 /* If PER_CU is not yet queued, add it to the queue.
9920 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9921 dependency.
9922 The result is non-zero if PER_CU was queued, otherwise the result is zero
9923 meaning either PER_CU is already queued or it is already loaded.
9924
9925 N.B. There is an invariant here that if a CU is queued then it is loaded.
9926 The caller is required to load PER_CU if we return non-zero. */
9927
9928 static int
9929 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
9930 struct dwarf2_per_cu_data *per_cu,
9931 enum language pretend_language)
9932 {
9933 /* We may arrive here during partial symbol reading, if we need full
9934 DIEs to process an unusual case (e.g. template arguments). Do
9935 not queue PER_CU, just tell our caller to load its DIEs. */
9936 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
9937 {
9938 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
9939 return 1;
9940 return 0;
9941 }
9942
9943 /* Mark the dependence relation so that we don't flush PER_CU
9944 too early. */
9945 if (dependent_cu != NULL)
9946 dwarf2_add_dependence (dependent_cu, per_cu);
9947
9948 /* If it's already on the queue, we have nothing to do. */
9949 if (per_cu->queued)
9950 return 0;
9951
9952 /* If the compilation unit is already loaded, just mark it as
9953 used. */
9954 if (per_cu->cu != NULL)
9955 {
9956 per_cu->cu->last_used = 0;
9957 return 0;
9958 }
9959
9960 /* Add it to the queue. */
9961 queue_comp_unit (per_cu, pretend_language);
9962
9963 return 1;
9964 }
9965
9966 /* Process the queue. */
9967
9968 static void
9969 process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
9970 {
9971 struct dwarf2_queue_item *item, *next_item;
9972
9973 if (dwarf_read_debug)
9974 {
9975 fprintf_unfiltered (gdb_stdlog,
9976 "Expanding one or more symtabs of objfile %s ...\n",
9977 objfile_name (dwarf2_per_objfile->objfile));
9978 }
9979
9980 /* The queue starts out with one item, but following a DIE reference
9981 may load a new CU, adding it to the end of the queue. */
9982 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
9983 {
9984 if ((dwarf2_per_objfile->using_index
9985 ? !item->per_cu->v.quick->compunit_symtab
9986 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
9987 /* Skip dummy CUs. */
9988 && item->per_cu->cu != NULL)
9989 {
9990 struct dwarf2_per_cu_data *per_cu = item->per_cu;
9991 unsigned int debug_print_threshold;
9992 char buf[100];
9993
9994 if (per_cu->is_debug_types)
9995 {
9996 struct signatured_type *sig_type =
9997 (struct signatured_type *) per_cu;
9998
9999 sprintf (buf, "TU %s at offset %s",
10000 hex_string (sig_type->signature),
10001 sect_offset_str (per_cu->sect_off));
10002 /* There can be 100s of TUs.
10003 Only print them in verbose mode. */
10004 debug_print_threshold = 2;
10005 }
10006 else
10007 {
10008 sprintf (buf, "CU at offset %s",
10009 sect_offset_str (per_cu->sect_off));
10010 debug_print_threshold = 1;
10011 }
10012
10013 if (dwarf_read_debug >= debug_print_threshold)
10014 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
10015
10016 if (per_cu->is_debug_types)
10017 process_full_type_unit (per_cu, item->pretend_language);
10018 else
10019 process_full_comp_unit (per_cu, item->pretend_language);
10020
10021 if (dwarf_read_debug >= debug_print_threshold)
10022 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
10023 }
10024
10025 item->per_cu->queued = 0;
10026 next_item = item->next;
10027 xfree (item);
10028 }
10029
10030 dwarf2_queue_tail = NULL;
10031
10032 if (dwarf_read_debug)
10033 {
10034 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
10035 objfile_name (dwarf2_per_objfile->objfile));
10036 }
10037 }
10038
10039 /* Read in full symbols for PST, and anything it depends on. */
10040
10041 static void
10042 psymtab_to_symtab_1 (struct partial_symtab *pst)
10043 {
10044 struct dwarf2_per_cu_data *per_cu;
10045 int i;
10046
10047 if (pst->readin)
10048 return;
10049
10050 for (i = 0; i < pst->number_of_dependencies; i++)
10051 if (!pst->dependencies[i]->readin
10052 && pst->dependencies[i]->user == NULL)
10053 {
10054 /* Inform about additional files that need to be read in. */
10055 if (info_verbose)
10056 {
10057 /* FIXME: i18n: Need to make this a single string. */
10058 fputs_filtered (" ", gdb_stdout);
10059 wrap_here ("");
10060 fputs_filtered ("and ", gdb_stdout);
10061 wrap_here ("");
10062 printf_filtered ("%s...", pst->dependencies[i]->filename);
10063 wrap_here (""); /* Flush output. */
10064 gdb_flush (gdb_stdout);
10065 }
10066 psymtab_to_symtab_1 (pst->dependencies[i]);
10067 }
10068
10069 per_cu = (struct dwarf2_per_cu_data *) pst->read_symtab_private;
10070
10071 if (per_cu == NULL)
10072 {
10073 /* It's an include file, no symbols to read for it.
10074 Everything is in the parent symtab. */
10075 pst->readin = 1;
10076 return;
10077 }
10078
10079 dw2_do_instantiate_symtab (per_cu);
10080 }
10081
10082 /* Trivial hash function for die_info: the hash value of a DIE
10083 is its offset in .debug_info for this objfile. */
10084
10085 static hashval_t
10086 die_hash (const void *item)
10087 {
10088 const struct die_info *die = (const struct die_info *) item;
10089
10090 return to_underlying (die->sect_off);
10091 }
10092
10093 /* Trivial comparison function for die_info structures: two DIEs
10094 are equal if they have the same offset. */
10095
10096 static int
10097 die_eq (const void *item_lhs, const void *item_rhs)
10098 {
10099 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
10100 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
10101
10102 return die_lhs->sect_off == die_rhs->sect_off;
10103 }
10104
10105 /* die_reader_func for load_full_comp_unit.
10106 This is identical to read_signatured_type_reader,
10107 but is kept separate for now. */
10108
10109 static void
10110 load_full_comp_unit_reader (const struct die_reader_specs *reader,
10111 const gdb_byte *info_ptr,
10112 struct die_info *comp_unit_die,
10113 int has_children,
10114 void *data)
10115 {
10116 struct dwarf2_cu *cu = reader->cu;
10117 enum language *language_ptr = (enum language *) data;
10118
10119 gdb_assert (cu->die_hash == NULL);
10120 cu->die_hash =
10121 htab_create_alloc_ex (cu->header.length / 12,
10122 die_hash,
10123 die_eq,
10124 NULL,
10125 &cu->comp_unit_obstack,
10126 hashtab_obstack_allocate,
10127 dummy_obstack_deallocate);
10128
10129 if (has_children)
10130 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
10131 &info_ptr, comp_unit_die);
10132 cu->dies = comp_unit_die;
10133 /* comp_unit_die is not stored in die_hash, no need. */
10134
10135 /* We try not to read any attributes in this function, because not
10136 all CUs needed for references have been loaded yet, and symbol
10137 table processing isn't initialized. But we have to set the CU language,
10138 or we won't be able to build types correctly.
10139 Similarly, if we do not read the producer, we can not apply
10140 producer-specific interpretation. */
10141 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
10142 }
10143
10144 /* Load the DIEs associated with PER_CU into memory. */
10145
10146 static void
10147 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
10148 enum language pretend_language)
10149 {
10150 gdb_assert (! this_cu->is_debug_types);
10151
10152 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
10153 load_full_comp_unit_reader, &pretend_language);
10154 }
10155
10156 /* Add a DIE to the delayed physname list. */
10157
10158 static void
10159 add_to_method_list (struct type *type, int fnfield_index, int index,
10160 const char *name, struct die_info *die,
10161 struct dwarf2_cu *cu)
10162 {
10163 struct delayed_method_info mi;
10164 mi.type = type;
10165 mi.fnfield_index = fnfield_index;
10166 mi.index = index;
10167 mi.name = name;
10168 mi.die = die;
10169 cu->method_list.push_back (mi);
10170 }
10171
10172 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
10173 "const" / "volatile". If so, decrements LEN by the length of the
10174 modifier and return true. Otherwise return false. */
10175
10176 template<size_t N>
10177 static bool
10178 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
10179 {
10180 size_t mod_len = sizeof (mod) - 1;
10181 if (len > mod_len && startswith (physname + (len - mod_len), mod))
10182 {
10183 len -= mod_len;
10184 return true;
10185 }
10186 return false;
10187 }
10188
10189 /* Compute the physnames of any methods on the CU's method list.
10190
10191 The computation of method physnames is delayed in order to avoid the
10192 (bad) condition that one of the method's formal parameters is of an as yet
10193 incomplete type. */
10194
10195 static void
10196 compute_delayed_physnames (struct dwarf2_cu *cu)
10197 {
10198 /* Only C++ delays computing physnames. */
10199 if (cu->method_list.empty ())
10200 return;
10201 gdb_assert (cu->language == language_cplus);
10202
10203 for (struct delayed_method_info &mi : cu->method_list)
10204 {
10205 const char *physname;
10206 struct fn_fieldlist *fn_flp
10207 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
10208 physname = dwarf2_physname (mi.name, mi.die, cu);
10209 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
10210 = physname ? physname : "";
10211
10212 /* Since there's no tag to indicate whether a method is a
10213 const/volatile overload, extract that information out of the
10214 demangled name. */
10215 if (physname != NULL)
10216 {
10217 size_t len = strlen (physname);
10218
10219 while (1)
10220 {
10221 if (physname[len] == ')') /* shortcut */
10222 break;
10223 else if (check_modifier (physname, len, " const"))
10224 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
10225 else if (check_modifier (physname, len, " volatile"))
10226 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
10227 else
10228 break;
10229 }
10230 }
10231 }
10232
10233 /* The list is no longer needed. */
10234 cu->method_list.clear ();
10235 }
10236
10237 /* Go objects should be embedded in a DW_TAG_module DIE,
10238 and it's not clear if/how imported objects will appear.
10239 To keep Go support simple until that's worked out,
10240 go back through what we've read and create something usable.
10241 We could do this while processing each DIE, and feels kinda cleaner,
10242 but that way is more invasive.
10243 This is to, for example, allow the user to type "p var" or "b main"
10244 without having to specify the package name, and allow lookups
10245 of module.object to work in contexts that use the expression
10246 parser. */
10247
10248 static void
10249 fixup_go_packaging (struct dwarf2_cu *cu)
10250 {
10251 char *package_name = NULL;
10252 struct pending *list;
10253 int i;
10254
10255 for (list = global_symbols; list != NULL; list = list->next)
10256 {
10257 for (i = 0; i < list->nsyms; ++i)
10258 {
10259 struct symbol *sym = list->symbol[i];
10260
10261 if (SYMBOL_LANGUAGE (sym) == language_go
10262 && SYMBOL_CLASS (sym) == LOC_BLOCK)
10263 {
10264 char *this_package_name = go_symbol_package_name (sym);
10265
10266 if (this_package_name == NULL)
10267 continue;
10268 if (package_name == NULL)
10269 package_name = this_package_name;
10270 else
10271 {
10272 struct objfile *objfile
10273 = cu->per_cu->dwarf2_per_objfile->objfile;
10274 if (strcmp (package_name, this_package_name) != 0)
10275 complaint (&symfile_complaints,
10276 _("Symtab %s has objects from two different Go packages: %s and %s"),
10277 (symbol_symtab (sym) != NULL
10278 ? symtab_to_filename_for_display
10279 (symbol_symtab (sym))
10280 : objfile_name (objfile)),
10281 this_package_name, package_name);
10282 xfree (this_package_name);
10283 }
10284 }
10285 }
10286 }
10287
10288 if (package_name != NULL)
10289 {
10290 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10291 const char *saved_package_name
10292 = (const char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
10293 package_name,
10294 strlen (package_name));
10295 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
10296 saved_package_name);
10297 struct symbol *sym;
10298
10299 TYPE_TAG_NAME (type) = TYPE_NAME (type);
10300
10301 sym = allocate_symbol (objfile);
10302 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
10303 SYMBOL_SET_NAMES (sym, saved_package_name,
10304 strlen (saved_package_name), 0, objfile);
10305 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
10306 e.g., "main" finds the "main" module and not C's main(). */
10307 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
10308 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
10309 SYMBOL_TYPE (sym) = type;
10310
10311 add_symbol_to_list (sym, &global_symbols);
10312
10313 xfree (package_name);
10314 }
10315 }
10316
10317 /* Allocate a fully-qualified name consisting of the two parts on the
10318 obstack. */
10319
10320 static const char *
10321 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
10322 {
10323 return obconcat (obstack, p1, "::", p2, (char *) NULL);
10324 }
10325
10326 /* A helper that allocates a struct discriminant_info to attach to a
10327 union type. */
10328
10329 static struct discriminant_info *
10330 alloc_discriminant_info (struct type *type, int discriminant_index,
10331 int default_index)
10332 {
10333 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
10334 gdb_assert (discriminant_index == -1
10335 || (discriminant_index >= 0
10336 && discriminant_index < TYPE_NFIELDS (type)));
10337 gdb_assert (default_index == -1
10338 || (default_index >= 0 && default_index < TYPE_NFIELDS (type)));
10339
10340 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
10341
10342 struct discriminant_info *disc
10343 = ((struct discriminant_info *)
10344 TYPE_ZALLOC (type,
10345 offsetof (struct discriminant_info, discriminants)
10346 + TYPE_NFIELDS (type) * sizeof (disc->discriminants[0])));
10347 disc->default_index = default_index;
10348 disc->discriminant_index = discriminant_index;
10349
10350 struct dynamic_prop prop;
10351 prop.kind = PROP_UNDEFINED;
10352 prop.data.baton = disc;
10353
10354 add_dyn_prop (DYN_PROP_DISCRIMINATED, prop, type);
10355
10356 return disc;
10357 }
10358
10359 /* Some versions of rustc emitted enums in an unusual way.
10360
10361 Ordinary enums were emitted as unions. The first element of each
10362 structure in the union was named "RUST$ENUM$DISR". This element
10363 held the discriminant.
10364
10365 These versions of Rust also implemented the "non-zero"
10366 optimization. When the enum had two values, and one is empty and
10367 the other holds a pointer that cannot be zero, the pointer is used
10368 as the discriminant, with a zero value meaning the empty variant.
10369 Here, the union's first member is of the form
10370 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
10371 where the fieldnos are the indices of the fields that should be
10372 traversed in order to find the field (which may be several fields deep)
10373 and the variantname is the name of the variant of the case when the
10374 field is zero.
10375
10376 This function recognizes whether TYPE is of one of these forms,
10377 and, if so, smashes it to be a variant type. */
10378
10379 static void
10380 quirk_rust_enum (struct type *type, struct objfile *objfile)
10381 {
10382 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
10383
10384 /* We don't need to deal with empty enums. */
10385 if (TYPE_NFIELDS (type) == 0)
10386 return;
10387
10388 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
10389 if (TYPE_NFIELDS (type) == 1
10390 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
10391 {
10392 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
10393
10394 /* Decode the field name to find the offset of the
10395 discriminant. */
10396 ULONGEST bit_offset = 0;
10397 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
10398 while (name[0] >= '0' && name[0] <= '9')
10399 {
10400 char *tail;
10401 unsigned long index = strtoul (name, &tail, 10);
10402 name = tail;
10403 if (*name != '$'
10404 || index >= TYPE_NFIELDS (field_type)
10405 || (TYPE_FIELD_LOC_KIND (field_type, index)
10406 != FIELD_LOC_KIND_BITPOS))
10407 {
10408 complaint (&symfile_complaints,
10409 _("Could not parse Rust enum encoding string \"%s\""
10410 "[in module %s]"),
10411 TYPE_FIELD_NAME (type, 0),
10412 objfile_name (objfile));
10413 return;
10414 }
10415 ++name;
10416
10417 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
10418 field_type = TYPE_FIELD_TYPE (field_type, index);
10419 }
10420
10421 /* Make a union to hold the variants. */
10422 struct type *union_type = alloc_type (objfile);
10423 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10424 TYPE_NFIELDS (union_type) = 3;
10425 TYPE_FIELDS (union_type)
10426 = (struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field));
10427 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
10428
10429 /* Put the discriminant must at index 0. */
10430 TYPE_FIELD_TYPE (union_type, 0) = field_type;
10431 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
10432 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
10433 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 0), bit_offset);
10434
10435 /* The order of fields doesn't really matter, so put the real
10436 field at index 1 and the data-less field at index 2. */
10437 struct discriminant_info *disc
10438 = alloc_discriminant_info (union_type, 0, 1);
10439 TYPE_FIELD (union_type, 1) = TYPE_FIELD (type, 0);
10440 TYPE_FIELD_NAME (union_type, 1)
10441 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1)));
10442 TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1))
10443 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
10444 TYPE_FIELD_NAME (union_type, 1));
10445
10446 const char *dataless_name
10447 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
10448 name);
10449 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
10450 dataless_name);
10451 TYPE_FIELD_TYPE (union_type, 2) = dataless_type;
10452 /* NAME points into the original discriminant name, which
10453 already has the correct lifetime. */
10454 TYPE_FIELD_NAME (union_type, 2) = name;
10455 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 2), 0);
10456 disc->discriminants[2] = 0;
10457
10458 /* Smash this type to be a structure type. We have to do this
10459 because the type has already been recorded. */
10460 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10461 TYPE_NFIELDS (type) = 1;
10462 TYPE_FIELDS (type)
10463 = (struct field *) TYPE_ZALLOC (type, sizeof (struct field));
10464
10465 /* Install the variant part. */
10466 TYPE_FIELD_TYPE (type, 0) = union_type;
10467 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10468 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10469 }
10470 else if (TYPE_NFIELDS (type) == 1)
10471 {
10472 /* We assume that a union with a single field is a univariant
10473 enum. */
10474 /* Smash this type to be a structure type. We have to do this
10475 because the type has already been recorded. */
10476 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10477
10478 /* Make a union to hold the variants. */
10479 struct type *union_type = alloc_type (objfile);
10480 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10481 TYPE_NFIELDS (union_type) = TYPE_NFIELDS (type);
10482 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
10483 TYPE_FIELDS (union_type) = TYPE_FIELDS (type);
10484
10485 struct type *field_type = TYPE_FIELD_TYPE (union_type, 0);
10486 const char *variant_name
10487 = rust_last_path_segment (TYPE_NAME (field_type));
10488 TYPE_FIELD_NAME (union_type, 0) = variant_name;
10489 TYPE_NAME (field_type)
10490 = rust_fully_qualify (&objfile->objfile_obstack,
10491 TYPE_NAME (type), variant_name);
10492
10493 /* Install the union in the outer struct type. */
10494 TYPE_NFIELDS (type) = 1;
10495 TYPE_FIELDS (type)
10496 = (struct field *) TYPE_ZALLOC (union_type, sizeof (struct field));
10497 TYPE_FIELD_TYPE (type, 0) = union_type;
10498 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10499 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10500
10501 alloc_discriminant_info (union_type, -1, 0);
10502 }
10503 else
10504 {
10505 struct type *disr_type = nullptr;
10506 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
10507 {
10508 disr_type = TYPE_FIELD_TYPE (type, i);
10509
10510 if (TYPE_NFIELDS (disr_type) == 0)
10511 {
10512 /* Could be data-less variant, so keep going. */
10513 }
10514 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
10515 "RUST$ENUM$DISR") != 0)
10516 {
10517 /* Not a Rust enum. */
10518 return;
10519 }
10520 else
10521 {
10522 /* Found one. */
10523 break;
10524 }
10525 }
10526
10527 /* If we got here without a discriminant, then it's probably
10528 just a union. */
10529 if (disr_type == nullptr)
10530 return;
10531
10532 /* Smash this type to be a structure type. We have to do this
10533 because the type has already been recorded. */
10534 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10535
10536 /* Make a union to hold the variants. */
10537 struct field *disr_field = &TYPE_FIELD (disr_type, 0);
10538 struct type *union_type = alloc_type (objfile);
10539 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10540 TYPE_NFIELDS (union_type) = 1 + TYPE_NFIELDS (type);
10541 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
10542 TYPE_FIELDS (union_type)
10543 = (struct field *) TYPE_ZALLOC (union_type,
10544 (TYPE_NFIELDS (union_type)
10545 * sizeof (struct field)));
10546
10547 memcpy (TYPE_FIELDS (union_type) + 1, TYPE_FIELDS (type),
10548 TYPE_NFIELDS (type) * sizeof (struct field));
10549
10550 /* Install the discriminant at index 0 in the union. */
10551 TYPE_FIELD (union_type, 0) = *disr_field;
10552 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
10553 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
10554
10555 /* Install the union in the outer struct type. */
10556 TYPE_FIELD_TYPE (type, 0) = union_type;
10557 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10558 TYPE_NFIELDS (type) = 1;
10559
10560 /* Set the size and offset of the union type. */
10561 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10562
10563 /* We need a way to find the correct discriminant given a
10564 variant name. For convenience we build a map here. */
10565 struct type *enum_type = FIELD_TYPE (*disr_field);
10566 std::unordered_map<std::string, ULONGEST> discriminant_map;
10567 for (int i = 0; i < TYPE_NFIELDS (enum_type); ++i)
10568 {
10569 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
10570 {
10571 const char *name
10572 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
10573 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
10574 }
10575 }
10576
10577 int n_fields = TYPE_NFIELDS (union_type);
10578 struct discriminant_info *disc
10579 = alloc_discriminant_info (union_type, 0, -1);
10580 /* Skip the discriminant here. */
10581 for (int i = 1; i < n_fields; ++i)
10582 {
10583 /* Find the final word in the name of this variant's type.
10584 That name can be used to look up the correct
10585 discriminant. */
10586 const char *variant_name
10587 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type,
10588 i)));
10589
10590 auto iter = discriminant_map.find (variant_name);
10591 if (iter != discriminant_map.end ())
10592 disc->discriminants[i] = iter->second;
10593
10594 /* Remove the discriminant field. */
10595 struct type *sub_type = TYPE_FIELD_TYPE (union_type, i);
10596 --TYPE_NFIELDS (sub_type);
10597 ++TYPE_FIELDS (sub_type);
10598 TYPE_FIELD_NAME (union_type, i) = variant_name;
10599 TYPE_NAME (sub_type)
10600 = rust_fully_qualify (&objfile->objfile_obstack,
10601 TYPE_NAME (type), variant_name);
10602 }
10603 }
10604 }
10605
10606 /* Rewrite some Rust unions to be structures with variants parts. */
10607
10608 static void
10609 rust_union_quirks (struct dwarf2_cu *cu)
10610 {
10611 gdb_assert (cu->language == language_rust);
10612 for (struct type *type : cu->rust_unions)
10613 quirk_rust_enum (type, cu->per_cu->dwarf2_per_objfile->objfile);
10614 }
10615
10616 /* Return the symtab for PER_CU. This works properly regardless of
10617 whether we're using the index or psymtabs. */
10618
10619 static struct compunit_symtab *
10620 get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
10621 {
10622 return (per_cu->dwarf2_per_objfile->using_index
10623 ? per_cu->v.quick->compunit_symtab
10624 : per_cu->v.psymtab->compunit_symtab);
10625 }
10626
10627 /* A helper function for computing the list of all symbol tables
10628 included by PER_CU. */
10629
10630 static void
10631 recursively_compute_inclusions (VEC (compunit_symtab_ptr) **result,
10632 htab_t all_children, htab_t all_type_symtabs,
10633 struct dwarf2_per_cu_data *per_cu,
10634 struct compunit_symtab *immediate_parent)
10635 {
10636 void **slot;
10637 int ix;
10638 struct compunit_symtab *cust;
10639 struct dwarf2_per_cu_data *iter;
10640
10641 slot = htab_find_slot (all_children, per_cu, INSERT);
10642 if (*slot != NULL)
10643 {
10644 /* This inclusion and its children have been processed. */
10645 return;
10646 }
10647
10648 *slot = per_cu;
10649 /* Only add a CU if it has a symbol table. */
10650 cust = get_compunit_symtab (per_cu);
10651 if (cust != NULL)
10652 {
10653 /* If this is a type unit only add its symbol table if we haven't
10654 seen it yet (type unit per_cu's can share symtabs). */
10655 if (per_cu->is_debug_types)
10656 {
10657 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
10658 if (*slot == NULL)
10659 {
10660 *slot = cust;
10661 VEC_safe_push (compunit_symtab_ptr, *result, cust);
10662 if (cust->user == NULL)
10663 cust->user = immediate_parent;
10664 }
10665 }
10666 else
10667 {
10668 VEC_safe_push (compunit_symtab_ptr, *result, cust);
10669 if (cust->user == NULL)
10670 cust->user = immediate_parent;
10671 }
10672 }
10673
10674 for (ix = 0;
10675 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
10676 ++ix)
10677 {
10678 recursively_compute_inclusions (result, all_children,
10679 all_type_symtabs, iter, cust);
10680 }
10681 }
10682
10683 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
10684 PER_CU. */
10685
10686 static void
10687 compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
10688 {
10689 gdb_assert (! per_cu->is_debug_types);
10690
10691 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
10692 {
10693 int ix, len;
10694 struct dwarf2_per_cu_data *per_cu_iter;
10695 struct compunit_symtab *compunit_symtab_iter;
10696 VEC (compunit_symtab_ptr) *result_symtabs = NULL;
10697 htab_t all_children, all_type_symtabs;
10698 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
10699
10700 /* If we don't have a symtab, we can just skip this case. */
10701 if (cust == NULL)
10702 return;
10703
10704 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10705 NULL, xcalloc, xfree);
10706 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10707 NULL, xcalloc, xfree);
10708
10709 for (ix = 0;
10710 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
10711 ix, per_cu_iter);
10712 ++ix)
10713 {
10714 recursively_compute_inclusions (&result_symtabs, all_children,
10715 all_type_symtabs, per_cu_iter,
10716 cust);
10717 }
10718
10719 /* Now we have a transitive closure of all the included symtabs. */
10720 len = VEC_length (compunit_symtab_ptr, result_symtabs);
10721 cust->includes
10722 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
10723 struct compunit_symtab *, len + 1);
10724 for (ix = 0;
10725 VEC_iterate (compunit_symtab_ptr, result_symtabs, ix,
10726 compunit_symtab_iter);
10727 ++ix)
10728 cust->includes[ix] = compunit_symtab_iter;
10729 cust->includes[len] = NULL;
10730
10731 VEC_free (compunit_symtab_ptr, result_symtabs);
10732 htab_delete (all_children);
10733 htab_delete (all_type_symtabs);
10734 }
10735 }
10736
10737 /* Compute the 'includes' field for the symtabs of all the CUs we just
10738 read. */
10739
10740 static void
10741 process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
10742 {
10743 int ix;
10744 struct dwarf2_per_cu_data *iter;
10745
10746 for (ix = 0;
10747 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
10748 ix, iter);
10749 ++ix)
10750 {
10751 if (! iter->is_debug_types)
10752 compute_compunit_symtab_includes (iter);
10753 }
10754
10755 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
10756 }
10757
10758 /* Generate full symbol information for PER_CU, whose DIEs have
10759 already been loaded into memory. */
10760
10761 static void
10762 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
10763 enum language pretend_language)
10764 {
10765 struct dwarf2_cu *cu = per_cu->cu;
10766 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10767 struct objfile *objfile = dwarf2_per_objfile->objfile;
10768 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10769 CORE_ADDR lowpc, highpc;
10770 struct compunit_symtab *cust;
10771 CORE_ADDR baseaddr;
10772 struct block *static_block;
10773 CORE_ADDR addr;
10774
10775 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10776
10777 buildsym_init ();
10778 scoped_free_pendings free_pending;
10779
10780 /* Clear the list here in case something was left over. */
10781 cu->method_list.clear ();
10782
10783 cu->list_in_scope = &file_symbols;
10784
10785 cu->language = pretend_language;
10786 cu->language_defn = language_def (cu->language);
10787
10788 /* Do line number decoding in read_file_scope () */
10789 process_die (cu->dies, cu);
10790
10791 /* For now fudge the Go package. */
10792 if (cu->language == language_go)
10793 fixup_go_packaging (cu);
10794
10795 /* Now that we have processed all the DIEs in the CU, all the types
10796 should be complete, and it should now be safe to compute all of the
10797 physnames. */
10798 compute_delayed_physnames (cu);
10799
10800 if (cu->language == language_rust)
10801 rust_union_quirks (cu);
10802
10803 /* Some compilers don't define a DW_AT_high_pc attribute for the
10804 compilation unit. If the DW_AT_high_pc is missing, synthesize
10805 it, by scanning the DIE's below the compilation unit. */
10806 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
10807
10808 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
10809 static_block = end_symtab_get_static_block (addr, 0, 1);
10810
10811 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
10812 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
10813 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
10814 addrmap to help ensure it has an accurate map of pc values belonging to
10815 this comp unit. */
10816 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
10817
10818 cust = end_symtab_from_static_block (static_block,
10819 SECT_OFF_TEXT (objfile), 0);
10820
10821 if (cust != NULL)
10822 {
10823 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
10824
10825 /* Set symtab language to language from DW_AT_language. If the
10826 compilation is from a C file generated by language preprocessors, do
10827 not set the language if it was already deduced by start_subfile. */
10828 if (!(cu->language == language_c
10829 && COMPUNIT_FILETABS (cust)->language != language_unknown))
10830 COMPUNIT_FILETABS (cust)->language = cu->language;
10831
10832 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
10833 produce DW_AT_location with location lists but it can be possibly
10834 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
10835 there were bugs in prologue debug info, fixed later in GCC-4.5
10836 by "unwind info for epilogues" patch (which is not directly related).
10837
10838 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
10839 needed, it would be wrong due to missing DW_AT_producer there.
10840
10841 Still one can confuse GDB by using non-standard GCC compilation
10842 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
10843 */
10844 if (cu->has_loclist && gcc_4_minor >= 5)
10845 cust->locations_valid = 1;
10846
10847 if (gcc_4_minor >= 5)
10848 cust->epilogue_unwind_valid = 1;
10849
10850 cust->call_site_htab = cu->call_site_htab;
10851 }
10852
10853 if (dwarf2_per_objfile->using_index)
10854 per_cu->v.quick->compunit_symtab = cust;
10855 else
10856 {
10857 struct partial_symtab *pst = per_cu->v.psymtab;
10858 pst->compunit_symtab = cust;
10859 pst->readin = 1;
10860 }
10861
10862 /* Push it for inclusion processing later. */
10863 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
10864 }
10865
10866 /* Generate full symbol information for type unit PER_CU, whose DIEs have
10867 already been loaded into memory. */
10868
10869 static void
10870 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
10871 enum language pretend_language)
10872 {
10873 struct dwarf2_cu *cu = per_cu->cu;
10874 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10875 struct objfile *objfile = dwarf2_per_objfile->objfile;
10876 struct compunit_symtab *cust;
10877 struct signatured_type *sig_type;
10878
10879 gdb_assert (per_cu->is_debug_types);
10880 sig_type = (struct signatured_type *) per_cu;
10881
10882 buildsym_init ();
10883 scoped_free_pendings free_pending;
10884
10885 /* Clear the list here in case something was left over. */
10886 cu->method_list.clear ();
10887
10888 cu->list_in_scope = &file_symbols;
10889
10890 cu->language = pretend_language;
10891 cu->language_defn = language_def (cu->language);
10892
10893 /* The symbol tables are set up in read_type_unit_scope. */
10894 process_die (cu->dies, cu);
10895
10896 /* For now fudge the Go package. */
10897 if (cu->language == language_go)
10898 fixup_go_packaging (cu);
10899
10900 /* Now that we have processed all the DIEs in the CU, all the types
10901 should be complete, and it should now be safe to compute all of the
10902 physnames. */
10903 compute_delayed_physnames (cu);
10904
10905 if (cu->language == language_rust)
10906 rust_union_quirks (cu);
10907
10908 /* TUs share symbol tables.
10909 If this is the first TU to use this symtab, complete the construction
10910 of it with end_expandable_symtab. Otherwise, complete the addition of
10911 this TU's symbols to the existing symtab. */
10912 if (sig_type->type_unit_group->compunit_symtab == NULL)
10913 {
10914 cust = end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
10915 sig_type->type_unit_group->compunit_symtab = cust;
10916
10917 if (cust != NULL)
10918 {
10919 /* Set symtab language to language from DW_AT_language. If the
10920 compilation is from a C file generated by language preprocessors,
10921 do not set the language if it was already deduced by
10922 start_subfile. */
10923 if (!(cu->language == language_c
10924 && COMPUNIT_FILETABS (cust)->language != language_c))
10925 COMPUNIT_FILETABS (cust)->language = cu->language;
10926 }
10927 }
10928 else
10929 {
10930 augment_type_symtab ();
10931 cust = sig_type->type_unit_group->compunit_symtab;
10932 }
10933
10934 if (dwarf2_per_objfile->using_index)
10935 per_cu->v.quick->compunit_symtab = cust;
10936 else
10937 {
10938 struct partial_symtab *pst = per_cu->v.psymtab;
10939 pst->compunit_symtab = cust;
10940 pst->readin = 1;
10941 }
10942 }
10943
10944 /* Process an imported unit DIE. */
10945
10946 static void
10947 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10948 {
10949 struct attribute *attr;
10950
10951 /* For now we don't handle imported units in type units. */
10952 if (cu->per_cu->is_debug_types)
10953 {
10954 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10955 " supported in type units [in module %s]"),
10956 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
10957 }
10958
10959 attr = dwarf2_attr (die, DW_AT_import, cu);
10960 if (attr != NULL)
10961 {
10962 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
10963 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
10964 dwarf2_per_cu_data *per_cu
10965 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
10966 cu->per_cu->dwarf2_per_objfile);
10967
10968 /* If necessary, add it to the queue and load its DIEs. */
10969 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
10970 load_full_comp_unit (per_cu, cu->language);
10971
10972 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
10973 per_cu);
10974 }
10975 }
10976
10977 /* RAII object that represents a process_die scope: i.e.,
10978 starts/finishes processing a DIE. */
10979 class process_die_scope
10980 {
10981 public:
10982 process_die_scope (die_info *die, dwarf2_cu *cu)
10983 : m_die (die), m_cu (cu)
10984 {
10985 /* We should only be processing DIEs not already in process. */
10986 gdb_assert (!m_die->in_process);
10987 m_die->in_process = true;
10988 }
10989
10990 ~process_die_scope ()
10991 {
10992 m_die->in_process = false;
10993
10994 /* If we're done processing the DIE for the CU that owns the line
10995 header, we don't need the line header anymore. */
10996 if (m_cu->line_header_die_owner == m_die)
10997 {
10998 delete m_cu->line_header;
10999 m_cu->line_header = NULL;
11000 m_cu->line_header_die_owner = NULL;
11001 }
11002 }
11003
11004 private:
11005 die_info *m_die;
11006 dwarf2_cu *m_cu;
11007 };
11008
11009 /* Process a die and its children. */
11010
11011 static void
11012 process_die (struct die_info *die, struct dwarf2_cu *cu)
11013 {
11014 process_die_scope scope (die, cu);
11015
11016 switch (die->tag)
11017 {
11018 case DW_TAG_padding:
11019 break;
11020 case DW_TAG_compile_unit:
11021 case DW_TAG_partial_unit:
11022 read_file_scope (die, cu);
11023 break;
11024 case DW_TAG_type_unit:
11025 read_type_unit_scope (die, cu);
11026 break;
11027 case DW_TAG_subprogram:
11028 case DW_TAG_inlined_subroutine:
11029 read_func_scope (die, cu);
11030 break;
11031 case DW_TAG_lexical_block:
11032 case DW_TAG_try_block:
11033 case DW_TAG_catch_block:
11034 read_lexical_block_scope (die, cu);
11035 break;
11036 case DW_TAG_call_site:
11037 case DW_TAG_GNU_call_site:
11038 read_call_site_scope (die, cu);
11039 break;
11040 case DW_TAG_class_type:
11041 case DW_TAG_interface_type:
11042 case DW_TAG_structure_type:
11043 case DW_TAG_union_type:
11044 process_structure_scope (die, cu);
11045 break;
11046 case DW_TAG_enumeration_type:
11047 process_enumeration_scope (die, cu);
11048 break;
11049
11050 /* These dies have a type, but processing them does not create
11051 a symbol or recurse to process the children. Therefore we can
11052 read them on-demand through read_type_die. */
11053 case DW_TAG_subroutine_type:
11054 case DW_TAG_set_type:
11055 case DW_TAG_array_type:
11056 case DW_TAG_pointer_type:
11057 case DW_TAG_ptr_to_member_type:
11058 case DW_TAG_reference_type:
11059 case DW_TAG_rvalue_reference_type:
11060 case DW_TAG_string_type:
11061 break;
11062
11063 case DW_TAG_base_type:
11064 case DW_TAG_subrange_type:
11065 case DW_TAG_typedef:
11066 /* Add a typedef symbol for the type definition, if it has a
11067 DW_AT_name. */
11068 new_symbol (die, read_type_die (die, cu), cu);
11069 break;
11070 case DW_TAG_common_block:
11071 read_common_block (die, cu);
11072 break;
11073 case DW_TAG_common_inclusion:
11074 break;
11075 case DW_TAG_namespace:
11076 cu->processing_has_namespace_info = 1;
11077 read_namespace (die, cu);
11078 break;
11079 case DW_TAG_module:
11080 cu->processing_has_namespace_info = 1;
11081 read_module (die, cu);
11082 break;
11083 case DW_TAG_imported_declaration:
11084 cu->processing_has_namespace_info = 1;
11085 if (read_namespace_alias (die, cu))
11086 break;
11087 /* The declaration is not a global namespace alias: fall through. */
11088 case DW_TAG_imported_module:
11089 cu->processing_has_namespace_info = 1;
11090 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
11091 || cu->language != language_fortran))
11092 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
11093 dwarf_tag_name (die->tag));
11094 read_import_statement (die, cu);
11095 break;
11096
11097 case DW_TAG_imported_unit:
11098 process_imported_unit_die (die, cu);
11099 break;
11100
11101 case DW_TAG_variable:
11102 read_variable (die, cu);
11103 break;
11104
11105 default:
11106 new_symbol (die, NULL, cu);
11107 break;
11108 }
11109 }
11110 \f
11111 /* DWARF name computation. */
11112
11113 /* A helper function for dwarf2_compute_name which determines whether DIE
11114 needs to have the name of the scope prepended to the name listed in the
11115 die. */
11116
11117 static int
11118 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
11119 {
11120 struct attribute *attr;
11121
11122 switch (die->tag)
11123 {
11124 case DW_TAG_namespace:
11125 case DW_TAG_typedef:
11126 case DW_TAG_class_type:
11127 case DW_TAG_interface_type:
11128 case DW_TAG_structure_type:
11129 case DW_TAG_union_type:
11130 case DW_TAG_enumeration_type:
11131 case DW_TAG_enumerator:
11132 case DW_TAG_subprogram:
11133 case DW_TAG_inlined_subroutine:
11134 case DW_TAG_member:
11135 case DW_TAG_imported_declaration:
11136 return 1;
11137
11138 case DW_TAG_variable:
11139 case DW_TAG_constant:
11140 /* We only need to prefix "globally" visible variables. These include
11141 any variable marked with DW_AT_external or any variable that
11142 lives in a namespace. [Variables in anonymous namespaces
11143 require prefixing, but they are not DW_AT_external.] */
11144
11145 if (dwarf2_attr (die, DW_AT_specification, cu))
11146 {
11147 struct dwarf2_cu *spec_cu = cu;
11148
11149 return die_needs_namespace (die_specification (die, &spec_cu),
11150 spec_cu);
11151 }
11152
11153 attr = dwarf2_attr (die, DW_AT_external, cu);
11154 if (attr == NULL && die->parent->tag != DW_TAG_namespace
11155 && die->parent->tag != DW_TAG_module)
11156 return 0;
11157 /* A variable in a lexical block of some kind does not need a
11158 namespace, even though in C++ such variables may be external
11159 and have a mangled name. */
11160 if (die->parent->tag == DW_TAG_lexical_block
11161 || die->parent->tag == DW_TAG_try_block
11162 || die->parent->tag == DW_TAG_catch_block
11163 || die->parent->tag == DW_TAG_subprogram)
11164 return 0;
11165 return 1;
11166
11167 default:
11168 return 0;
11169 }
11170 }
11171
11172 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
11173 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
11174 defined for the given DIE. */
11175
11176 static struct attribute *
11177 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
11178 {
11179 struct attribute *attr;
11180
11181 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
11182 if (attr == NULL)
11183 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
11184
11185 return attr;
11186 }
11187
11188 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
11189 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
11190 defined for the given DIE. */
11191
11192 static const char *
11193 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
11194 {
11195 const char *linkage_name;
11196
11197 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
11198 if (linkage_name == NULL)
11199 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
11200
11201 return linkage_name;
11202 }
11203
11204 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
11205 compute the physname for the object, which include a method's:
11206 - formal parameters (C++),
11207 - receiver type (Go),
11208
11209 The term "physname" is a bit confusing.
11210 For C++, for example, it is the demangled name.
11211 For Go, for example, it's the mangled name.
11212
11213 For Ada, return the DIE's linkage name rather than the fully qualified
11214 name. PHYSNAME is ignored..
11215
11216 The result is allocated on the objfile_obstack and canonicalized. */
11217
11218 static const char *
11219 dwarf2_compute_name (const char *name,
11220 struct die_info *die, struct dwarf2_cu *cu,
11221 int physname)
11222 {
11223 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
11224
11225 if (name == NULL)
11226 name = dwarf2_name (die, cu);
11227
11228 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
11229 but otherwise compute it by typename_concat inside GDB.
11230 FIXME: Actually this is not really true, or at least not always true.
11231 It's all very confusing. SYMBOL_SET_NAMES doesn't try to demangle
11232 Fortran names because there is no mangling standard. So new_symbol
11233 will set the demangled name to the result of dwarf2_full_name, and it is
11234 the demangled name that GDB uses if it exists. */
11235 if (cu->language == language_ada
11236 || (cu->language == language_fortran && physname))
11237 {
11238 /* For Ada unit, we prefer the linkage name over the name, as
11239 the former contains the exported name, which the user expects
11240 to be able to reference. Ideally, we want the user to be able
11241 to reference this entity using either natural or linkage name,
11242 but we haven't started looking at this enhancement yet. */
11243 const char *linkage_name = dw2_linkage_name (die, cu);
11244
11245 if (linkage_name != NULL)
11246 return linkage_name;
11247 }
11248
11249 /* These are the only languages we know how to qualify names in. */
11250 if (name != NULL
11251 && (cu->language == language_cplus
11252 || cu->language == language_fortran || cu->language == language_d
11253 || cu->language == language_rust))
11254 {
11255 if (die_needs_namespace (die, cu))
11256 {
11257 const char *prefix;
11258 const char *canonical_name = NULL;
11259
11260 string_file buf;
11261
11262 prefix = determine_prefix (die, cu);
11263 if (*prefix != '\0')
11264 {
11265 char *prefixed_name = typename_concat (NULL, prefix, name,
11266 physname, cu);
11267
11268 buf.puts (prefixed_name);
11269 xfree (prefixed_name);
11270 }
11271 else
11272 buf.puts (name);
11273
11274 /* Template parameters may be specified in the DIE's DW_AT_name, or
11275 as children with DW_TAG_template_type_param or
11276 DW_TAG_value_type_param. If the latter, add them to the name
11277 here. If the name already has template parameters, then
11278 skip this step; some versions of GCC emit both, and
11279 it is more efficient to use the pre-computed name.
11280
11281 Something to keep in mind about this process: it is very
11282 unlikely, or in some cases downright impossible, to produce
11283 something that will match the mangled name of a function.
11284 If the definition of the function has the same debug info,
11285 we should be able to match up with it anyway. But fallbacks
11286 using the minimal symbol, for instance to find a method
11287 implemented in a stripped copy of libstdc++, will not work.
11288 If we do not have debug info for the definition, we will have to
11289 match them up some other way.
11290
11291 When we do name matching there is a related problem with function
11292 templates; two instantiated function templates are allowed to
11293 differ only by their return types, which we do not add here. */
11294
11295 if (cu->language == language_cplus && strchr (name, '<') == NULL)
11296 {
11297 struct attribute *attr;
11298 struct die_info *child;
11299 int first = 1;
11300
11301 die->building_fullname = 1;
11302
11303 for (child = die->child; child != NULL; child = child->sibling)
11304 {
11305 struct type *type;
11306 LONGEST value;
11307 const gdb_byte *bytes;
11308 struct dwarf2_locexpr_baton *baton;
11309 struct value *v;
11310
11311 if (child->tag != DW_TAG_template_type_param
11312 && child->tag != DW_TAG_template_value_param)
11313 continue;
11314
11315 if (first)
11316 {
11317 buf.puts ("<");
11318 first = 0;
11319 }
11320 else
11321 buf.puts (", ");
11322
11323 attr = dwarf2_attr (child, DW_AT_type, cu);
11324 if (attr == NULL)
11325 {
11326 complaint (&symfile_complaints,
11327 _("template parameter missing DW_AT_type"));
11328 buf.puts ("UNKNOWN_TYPE");
11329 continue;
11330 }
11331 type = die_type (child, cu);
11332
11333 if (child->tag == DW_TAG_template_type_param)
11334 {
11335 c_print_type (type, "", &buf, -1, 0, &type_print_raw_options);
11336 continue;
11337 }
11338
11339 attr = dwarf2_attr (child, DW_AT_const_value, cu);
11340 if (attr == NULL)
11341 {
11342 complaint (&symfile_complaints,
11343 _("template parameter missing "
11344 "DW_AT_const_value"));
11345 buf.puts ("UNKNOWN_VALUE");
11346 continue;
11347 }
11348
11349 dwarf2_const_value_attr (attr, type, name,
11350 &cu->comp_unit_obstack, cu,
11351 &value, &bytes, &baton);
11352
11353 if (TYPE_NOSIGN (type))
11354 /* GDB prints characters as NUMBER 'CHAR'. If that's
11355 changed, this can use value_print instead. */
11356 c_printchar (value, type, &buf);
11357 else
11358 {
11359 struct value_print_options opts;
11360
11361 if (baton != NULL)
11362 v = dwarf2_evaluate_loc_desc (type, NULL,
11363 baton->data,
11364 baton->size,
11365 baton->per_cu);
11366 else if (bytes != NULL)
11367 {
11368 v = allocate_value (type);
11369 memcpy (value_contents_writeable (v), bytes,
11370 TYPE_LENGTH (type));
11371 }
11372 else
11373 v = value_from_longest (type, value);
11374
11375 /* Specify decimal so that we do not depend on
11376 the radix. */
11377 get_formatted_print_options (&opts, 'd');
11378 opts.raw = 1;
11379 value_print (v, &buf, &opts);
11380 release_value (v);
11381 value_free (v);
11382 }
11383 }
11384
11385 die->building_fullname = 0;
11386
11387 if (!first)
11388 {
11389 /* Close the argument list, with a space if necessary
11390 (nested templates). */
11391 if (!buf.empty () && buf.string ().back () == '>')
11392 buf.puts (" >");
11393 else
11394 buf.puts (">");
11395 }
11396 }
11397
11398 /* For C++ methods, append formal parameter type
11399 information, if PHYSNAME. */
11400
11401 if (physname && die->tag == DW_TAG_subprogram
11402 && cu->language == language_cplus)
11403 {
11404 struct type *type = read_type_die (die, cu);
11405
11406 c_type_print_args (type, &buf, 1, cu->language,
11407 &type_print_raw_options);
11408
11409 if (cu->language == language_cplus)
11410 {
11411 /* Assume that an artificial first parameter is
11412 "this", but do not crash if it is not. RealView
11413 marks unnamed (and thus unused) parameters as
11414 artificial; there is no way to differentiate
11415 the two cases. */
11416 if (TYPE_NFIELDS (type) > 0
11417 && TYPE_FIELD_ARTIFICIAL (type, 0)
11418 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
11419 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
11420 0))))
11421 buf.puts (" const");
11422 }
11423 }
11424
11425 const std::string &intermediate_name = buf.string ();
11426
11427 if (cu->language == language_cplus)
11428 canonical_name
11429 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
11430 &objfile->per_bfd->storage_obstack);
11431
11432 /* If we only computed INTERMEDIATE_NAME, or if
11433 INTERMEDIATE_NAME is already canonical, then we need to
11434 copy it to the appropriate obstack. */
11435 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
11436 name = ((const char *)
11437 obstack_copy0 (&objfile->per_bfd->storage_obstack,
11438 intermediate_name.c_str (),
11439 intermediate_name.length ()));
11440 else
11441 name = canonical_name;
11442 }
11443 }
11444
11445 return name;
11446 }
11447
11448 /* Return the fully qualified name of DIE, based on its DW_AT_name.
11449 If scope qualifiers are appropriate they will be added. The result
11450 will be allocated on the storage_obstack, or NULL if the DIE does
11451 not have a name. NAME may either be from a previous call to
11452 dwarf2_name or NULL.
11453
11454 The output string will be canonicalized (if C++). */
11455
11456 static const char *
11457 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
11458 {
11459 return dwarf2_compute_name (name, die, cu, 0);
11460 }
11461
11462 /* Construct a physname for the given DIE in CU. NAME may either be
11463 from a previous call to dwarf2_name or NULL. The result will be
11464 allocated on the objfile_objstack or NULL if the DIE does not have a
11465 name.
11466
11467 The output string will be canonicalized (if C++). */
11468
11469 static const char *
11470 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
11471 {
11472 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
11473 const char *retval, *mangled = NULL, *canon = NULL;
11474 int need_copy = 1;
11475
11476 /* In this case dwarf2_compute_name is just a shortcut not building anything
11477 on its own. */
11478 if (!die_needs_namespace (die, cu))
11479 return dwarf2_compute_name (name, die, cu, 1);
11480
11481 mangled = dw2_linkage_name (die, cu);
11482
11483 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
11484 See https://github.com/rust-lang/rust/issues/32925. */
11485 if (cu->language == language_rust && mangled != NULL
11486 && strchr (mangled, '{') != NULL)
11487 mangled = NULL;
11488
11489 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
11490 has computed. */
11491 gdb::unique_xmalloc_ptr<char> demangled;
11492 if (mangled != NULL)
11493 {
11494
11495 if (cu->language == language_go)
11496 {
11497 /* This is a lie, but we already lie to the caller new_symbol.
11498 new_symbol assumes we return the mangled name.
11499 This just undoes that lie until things are cleaned up. */
11500 }
11501 else
11502 {
11503 /* Use DMGL_RET_DROP for C++ template functions to suppress
11504 their return type. It is easier for GDB users to search
11505 for such functions as `name(params)' than `long name(params)'.
11506 In such case the minimal symbol names do not match the full
11507 symbol names but for template functions there is never a need
11508 to look up their definition from their declaration so
11509 the only disadvantage remains the minimal symbol variant
11510 `long name(params)' does not have the proper inferior type. */
11511 demangled.reset (gdb_demangle (mangled,
11512 (DMGL_PARAMS | DMGL_ANSI
11513 | DMGL_RET_DROP)));
11514 }
11515 if (demangled)
11516 canon = demangled.get ();
11517 else
11518 {
11519 canon = mangled;
11520 need_copy = 0;
11521 }
11522 }
11523
11524 if (canon == NULL || check_physname)
11525 {
11526 const char *physname = dwarf2_compute_name (name, die, cu, 1);
11527
11528 if (canon != NULL && strcmp (physname, canon) != 0)
11529 {
11530 /* It may not mean a bug in GDB. The compiler could also
11531 compute DW_AT_linkage_name incorrectly. But in such case
11532 GDB would need to be bug-to-bug compatible. */
11533
11534 complaint (&symfile_complaints,
11535 _("Computed physname <%s> does not match demangled <%s> "
11536 "(from linkage <%s>) - DIE at %s [in module %s]"),
11537 physname, canon, mangled, sect_offset_str (die->sect_off),
11538 objfile_name (objfile));
11539
11540 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
11541 is available here - over computed PHYSNAME. It is safer
11542 against both buggy GDB and buggy compilers. */
11543
11544 retval = canon;
11545 }
11546 else
11547 {
11548 retval = physname;
11549 need_copy = 0;
11550 }
11551 }
11552 else
11553 retval = canon;
11554
11555 if (need_copy)
11556 retval = ((const char *)
11557 obstack_copy0 (&objfile->per_bfd->storage_obstack,
11558 retval, strlen (retval)));
11559
11560 return retval;
11561 }
11562
11563 /* Inspect DIE in CU for a namespace alias. If one exists, record
11564 a new symbol for it.
11565
11566 Returns 1 if a namespace alias was recorded, 0 otherwise. */
11567
11568 static int
11569 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
11570 {
11571 struct attribute *attr;
11572
11573 /* If the die does not have a name, this is not a namespace
11574 alias. */
11575 attr = dwarf2_attr (die, DW_AT_name, cu);
11576 if (attr != NULL)
11577 {
11578 int num;
11579 struct die_info *d = die;
11580 struct dwarf2_cu *imported_cu = cu;
11581
11582 /* If the compiler has nested DW_AT_imported_declaration DIEs,
11583 keep inspecting DIEs until we hit the underlying import. */
11584 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
11585 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
11586 {
11587 attr = dwarf2_attr (d, DW_AT_import, cu);
11588 if (attr == NULL)
11589 break;
11590
11591 d = follow_die_ref (d, attr, &imported_cu);
11592 if (d->tag != DW_TAG_imported_declaration)
11593 break;
11594 }
11595
11596 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
11597 {
11598 complaint (&symfile_complaints,
11599 _("DIE at %s has too many recursively imported "
11600 "declarations"), sect_offset_str (d->sect_off));
11601 return 0;
11602 }
11603
11604 if (attr != NULL)
11605 {
11606 struct type *type;
11607 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
11608
11609 type = get_die_type_at_offset (sect_off, cu->per_cu);
11610 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
11611 {
11612 /* This declaration is a global namespace alias. Add
11613 a symbol for it whose type is the aliased namespace. */
11614 new_symbol (die, type, cu);
11615 return 1;
11616 }
11617 }
11618 }
11619
11620 return 0;
11621 }
11622
11623 /* Return the using directives repository (global or local?) to use in the
11624 current context for LANGUAGE.
11625
11626 For Ada, imported declarations can materialize renamings, which *may* be
11627 global. However it is impossible (for now?) in DWARF to distinguish
11628 "external" imported declarations and "static" ones. As all imported
11629 declarations seem to be static in all other languages, make them all CU-wide
11630 global only in Ada. */
11631
11632 static struct using_direct **
11633 using_directives (enum language language)
11634 {
11635 if (language == language_ada && context_stack_depth == 0)
11636 return &global_using_directives;
11637 else
11638 return &local_using_directives;
11639 }
11640
11641 /* Read the import statement specified by the given die and record it. */
11642
11643 static void
11644 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
11645 {
11646 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
11647 struct attribute *import_attr;
11648 struct die_info *imported_die, *child_die;
11649 struct dwarf2_cu *imported_cu;
11650 const char *imported_name;
11651 const char *imported_name_prefix;
11652 const char *canonical_name;
11653 const char *import_alias;
11654 const char *imported_declaration = NULL;
11655 const char *import_prefix;
11656 std::vector<const char *> excludes;
11657
11658 import_attr = dwarf2_attr (die, DW_AT_import, cu);
11659 if (import_attr == NULL)
11660 {
11661 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
11662 dwarf_tag_name (die->tag));
11663 return;
11664 }
11665
11666 imported_cu = cu;
11667 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
11668 imported_name = dwarf2_name (imported_die, imported_cu);
11669 if (imported_name == NULL)
11670 {
11671 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
11672
11673 The import in the following code:
11674 namespace A
11675 {
11676 typedef int B;
11677 }
11678
11679 int main ()
11680 {
11681 using A::B;
11682 B b;
11683 return b;
11684 }
11685
11686 ...
11687 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
11688 <52> DW_AT_decl_file : 1
11689 <53> DW_AT_decl_line : 6
11690 <54> DW_AT_import : <0x75>
11691 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
11692 <59> DW_AT_name : B
11693 <5b> DW_AT_decl_file : 1
11694 <5c> DW_AT_decl_line : 2
11695 <5d> DW_AT_type : <0x6e>
11696 ...
11697 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
11698 <76> DW_AT_byte_size : 4
11699 <77> DW_AT_encoding : 5 (signed)
11700
11701 imports the wrong die ( 0x75 instead of 0x58 ).
11702 This case will be ignored until the gcc bug is fixed. */
11703 return;
11704 }
11705
11706 /* Figure out the local name after import. */
11707 import_alias = dwarf2_name (die, cu);
11708
11709 /* Figure out where the statement is being imported to. */
11710 import_prefix = determine_prefix (die, cu);
11711
11712 /* Figure out what the scope of the imported die is and prepend it
11713 to the name of the imported die. */
11714 imported_name_prefix = determine_prefix (imported_die, imported_cu);
11715
11716 if (imported_die->tag != DW_TAG_namespace
11717 && imported_die->tag != DW_TAG_module)
11718 {
11719 imported_declaration = imported_name;
11720 canonical_name = imported_name_prefix;
11721 }
11722 else if (strlen (imported_name_prefix) > 0)
11723 canonical_name = obconcat (&objfile->objfile_obstack,
11724 imported_name_prefix,
11725 (cu->language == language_d ? "." : "::"),
11726 imported_name, (char *) NULL);
11727 else
11728 canonical_name = imported_name;
11729
11730 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
11731 for (child_die = die->child; child_die && child_die->tag;
11732 child_die = sibling_die (child_die))
11733 {
11734 /* DWARF-4: A Fortran use statement with a “rename list” may be
11735 represented by an imported module entry with an import attribute
11736 referring to the module and owned entries corresponding to those
11737 entities that are renamed as part of being imported. */
11738
11739 if (child_die->tag != DW_TAG_imported_declaration)
11740 {
11741 complaint (&symfile_complaints,
11742 _("child DW_TAG_imported_declaration expected "
11743 "- DIE at %s [in module %s]"),
11744 sect_offset_str (child_die->sect_off),
11745 objfile_name (objfile));
11746 continue;
11747 }
11748
11749 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
11750 if (import_attr == NULL)
11751 {
11752 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
11753 dwarf_tag_name (child_die->tag));
11754 continue;
11755 }
11756
11757 imported_cu = cu;
11758 imported_die = follow_die_ref_or_sig (child_die, import_attr,
11759 &imported_cu);
11760 imported_name = dwarf2_name (imported_die, imported_cu);
11761 if (imported_name == NULL)
11762 {
11763 complaint (&symfile_complaints,
11764 _("child DW_TAG_imported_declaration has unknown "
11765 "imported name - DIE at %s [in module %s]"),
11766 sect_offset_str (child_die->sect_off),
11767 objfile_name (objfile));
11768 continue;
11769 }
11770
11771 excludes.push_back (imported_name);
11772
11773 process_die (child_die, cu);
11774 }
11775
11776 add_using_directive (using_directives (cu->language),
11777 import_prefix,
11778 canonical_name,
11779 import_alias,
11780 imported_declaration,
11781 excludes,
11782 0,
11783 &objfile->objfile_obstack);
11784 }
11785
11786 /* ICC<14 does not output the required DW_AT_declaration on incomplete
11787 types, but gives them a size of zero. Starting with version 14,
11788 ICC is compatible with GCC. */
11789
11790 static int
11791 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
11792 {
11793 if (!cu->checked_producer)
11794 check_producer (cu);
11795
11796 return cu->producer_is_icc_lt_14;
11797 }
11798
11799 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
11800 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
11801 this, it was first present in GCC release 4.3.0. */
11802
11803 static int
11804 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
11805 {
11806 if (!cu->checked_producer)
11807 check_producer (cu);
11808
11809 return cu->producer_is_gcc_lt_4_3;
11810 }
11811
11812 static file_and_directory
11813 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
11814 {
11815 file_and_directory res;
11816
11817 /* Find the filename. Do not use dwarf2_name here, since the filename
11818 is not a source language identifier. */
11819 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
11820 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
11821
11822 if (res.comp_dir == NULL
11823 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
11824 && IS_ABSOLUTE_PATH (res.name))
11825 {
11826 res.comp_dir_storage = ldirname (res.name);
11827 if (!res.comp_dir_storage.empty ())
11828 res.comp_dir = res.comp_dir_storage.c_str ();
11829 }
11830 if (res.comp_dir != NULL)
11831 {
11832 /* Irix 6.2 native cc prepends <machine>.: to the compilation
11833 directory, get rid of it. */
11834 const char *cp = strchr (res.comp_dir, ':');
11835
11836 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
11837 res.comp_dir = cp + 1;
11838 }
11839
11840 if (res.name == NULL)
11841 res.name = "<unknown>";
11842
11843 return res;
11844 }
11845
11846 /* Handle DW_AT_stmt_list for a compilation unit.
11847 DIE is the DW_TAG_compile_unit die for CU.
11848 COMP_DIR is the compilation directory. LOWPC is passed to
11849 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
11850
11851 static void
11852 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
11853 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
11854 {
11855 struct dwarf2_per_objfile *dwarf2_per_objfile
11856 = cu->per_cu->dwarf2_per_objfile;
11857 struct objfile *objfile = dwarf2_per_objfile->objfile;
11858 struct attribute *attr;
11859 struct line_header line_header_local;
11860 hashval_t line_header_local_hash;
11861 void **slot;
11862 int decode_mapping;
11863
11864 gdb_assert (! cu->per_cu->is_debug_types);
11865
11866 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
11867 if (attr == NULL)
11868 return;
11869
11870 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11871
11872 /* The line header hash table is only created if needed (it exists to
11873 prevent redundant reading of the line table for partial_units).
11874 If we're given a partial_unit, we'll need it. If we're given a
11875 compile_unit, then use the line header hash table if it's already
11876 created, but don't create one just yet. */
11877
11878 if (dwarf2_per_objfile->line_header_hash == NULL
11879 && die->tag == DW_TAG_partial_unit)
11880 {
11881 dwarf2_per_objfile->line_header_hash
11882 = htab_create_alloc_ex (127, line_header_hash_voidp,
11883 line_header_eq_voidp,
11884 free_line_header_voidp,
11885 &objfile->objfile_obstack,
11886 hashtab_obstack_allocate,
11887 dummy_obstack_deallocate);
11888 }
11889
11890 line_header_local.sect_off = line_offset;
11891 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11892 line_header_local_hash = line_header_hash (&line_header_local);
11893 if (dwarf2_per_objfile->line_header_hash != NULL)
11894 {
11895 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11896 &line_header_local,
11897 line_header_local_hash, NO_INSERT);
11898
11899 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11900 is not present in *SLOT (since if there is something in *SLOT then
11901 it will be for a partial_unit). */
11902 if (die->tag == DW_TAG_partial_unit && slot != NULL)
11903 {
11904 gdb_assert (*slot != NULL);
11905 cu->line_header = (struct line_header *) *slot;
11906 return;
11907 }
11908 }
11909
11910 /* dwarf_decode_line_header does not yet provide sufficient information.
11911 We always have to call also dwarf_decode_lines for it. */
11912 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11913 if (lh == NULL)
11914 return;
11915
11916 cu->line_header = lh.release ();
11917 cu->line_header_die_owner = die;
11918
11919 if (dwarf2_per_objfile->line_header_hash == NULL)
11920 slot = NULL;
11921 else
11922 {
11923 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11924 &line_header_local,
11925 line_header_local_hash, INSERT);
11926 gdb_assert (slot != NULL);
11927 }
11928 if (slot != NULL && *slot == NULL)
11929 {
11930 /* This newly decoded line number information unit will be owned
11931 by line_header_hash hash table. */
11932 *slot = cu->line_header;
11933 cu->line_header_die_owner = NULL;
11934 }
11935 else
11936 {
11937 /* We cannot free any current entry in (*slot) as that struct line_header
11938 may be already used by multiple CUs. Create only temporary decoded
11939 line_header for this CU - it may happen at most once for each line
11940 number information unit. And if we're not using line_header_hash
11941 then this is what we want as well. */
11942 gdb_assert (die->tag != DW_TAG_partial_unit);
11943 }
11944 decode_mapping = (die->tag != DW_TAG_partial_unit);
11945 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11946 decode_mapping);
11947
11948 }
11949
11950 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
11951
11952 static void
11953 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
11954 {
11955 struct dwarf2_per_objfile *dwarf2_per_objfile
11956 = cu->per_cu->dwarf2_per_objfile;
11957 struct objfile *objfile = dwarf2_per_objfile->objfile;
11958 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11959 CORE_ADDR lowpc = ((CORE_ADDR) -1);
11960 CORE_ADDR highpc = ((CORE_ADDR) 0);
11961 struct attribute *attr;
11962 struct die_info *child_die;
11963 CORE_ADDR baseaddr;
11964
11965 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11966
11967 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
11968
11969 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11970 from finish_block. */
11971 if (lowpc == ((CORE_ADDR) -1))
11972 lowpc = highpc;
11973 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11974
11975 file_and_directory fnd = find_file_and_directory (die, cu);
11976
11977 prepare_one_comp_unit (cu, die, cu->language);
11978
11979 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11980 standardised yet. As a workaround for the language detection we fall
11981 back to the DW_AT_producer string. */
11982 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11983 cu->language = language_opencl;
11984
11985 /* Similar hack for Go. */
11986 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11987 set_cu_language (DW_LANG_Go, cu);
11988
11989 dwarf2_start_symtab (cu, fnd.name, fnd.comp_dir, lowpc);
11990
11991 /* Decode line number information if present. We do this before
11992 processing child DIEs, so that the line header table is available
11993 for DW_AT_decl_file. */
11994 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
11995
11996 /* Process all dies in compilation unit. */
11997 if (die->child != NULL)
11998 {
11999 child_die = die->child;
12000 while (child_die && child_die->tag)
12001 {
12002 process_die (child_die, cu);
12003 child_die = sibling_die (child_die);
12004 }
12005 }
12006
12007 /* Decode macro information, if present. Dwarf 2 macro information
12008 refers to information in the line number info statement program
12009 header, so we can only read it if we've read the header
12010 successfully. */
12011 attr = dwarf2_attr (die, DW_AT_macros, cu);
12012 if (attr == NULL)
12013 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
12014 if (attr && cu->line_header)
12015 {
12016 if (dwarf2_attr (die, DW_AT_macro_info, cu))
12017 complaint (&symfile_complaints,
12018 _("CU refers to both DW_AT_macros and DW_AT_macro_info"));
12019
12020 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
12021 }
12022 else
12023 {
12024 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
12025 if (attr && cu->line_header)
12026 {
12027 unsigned int macro_offset = DW_UNSND (attr);
12028
12029 dwarf_decode_macros (cu, macro_offset, 0);
12030 }
12031 }
12032 }
12033
12034 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
12035 Create the set of symtabs used by this TU, or if this TU is sharing
12036 symtabs with another TU and the symtabs have already been created
12037 then restore those symtabs in the line header.
12038 We don't need the pc/line-number mapping for type units. */
12039
12040 static void
12041 setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
12042 {
12043 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
12044 struct type_unit_group *tu_group;
12045 int first_time;
12046 struct attribute *attr;
12047 unsigned int i;
12048 struct signatured_type *sig_type;
12049
12050 gdb_assert (per_cu->is_debug_types);
12051 sig_type = (struct signatured_type *) per_cu;
12052
12053 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
12054
12055 /* If we're using .gdb_index (includes -readnow) then
12056 per_cu->type_unit_group may not have been set up yet. */
12057 if (sig_type->type_unit_group == NULL)
12058 sig_type->type_unit_group = get_type_unit_group (cu, attr);
12059 tu_group = sig_type->type_unit_group;
12060
12061 /* If we've already processed this stmt_list there's no real need to
12062 do it again, we could fake it and just recreate the part we need
12063 (file name,index -> symtab mapping). If data shows this optimization
12064 is useful we can do it then. */
12065 first_time = tu_group->compunit_symtab == NULL;
12066
12067 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
12068 debug info. */
12069 line_header_up lh;
12070 if (attr != NULL)
12071 {
12072 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
12073 lh = dwarf_decode_line_header (line_offset, cu);
12074 }
12075 if (lh == NULL)
12076 {
12077 if (first_time)
12078 dwarf2_start_symtab (cu, "", NULL, 0);
12079 else
12080 {
12081 gdb_assert (tu_group->symtabs == NULL);
12082 restart_symtab (tu_group->compunit_symtab, "", 0);
12083 }
12084 return;
12085 }
12086
12087 cu->line_header = lh.release ();
12088 cu->line_header_die_owner = die;
12089
12090 if (first_time)
12091 {
12092 struct compunit_symtab *cust = dwarf2_start_symtab (cu, "", NULL, 0);
12093
12094 /* Note: We don't assign tu_group->compunit_symtab yet because we're
12095 still initializing it, and our caller (a few levels up)
12096 process_full_type_unit still needs to know if this is the first
12097 time. */
12098
12099 tu_group->num_symtabs = cu->line_header->file_names.size ();
12100 tu_group->symtabs = XNEWVEC (struct symtab *,
12101 cu->line_header->file_names.size ());
12102
12103 for (i = 0; i < cu->line_header->file_names.size (); ++i)
12104 {
12105 file_entry &fe = cu->line_header->file_names[i];
12106
12107 dwarf2_start_subfile (fe.name, fe.include_dir (cu->line_header));
12108
12109 if (current_subfile->symtab == NULL)
12110 {
12111 /* NOTE: start_subfile will recognize when it's been
12112 passed a file it has already seen. So we can't
12113 assume there's a simple mapping from
12114 cu->line_header->file_names to subfiles, plus
12115 cu->line_header->file_names may contain dups. */
12116 current_subfile->symtab
12117 = allocate_symtab (cust, current_subfile->name);
12118 }
12119
12120 fe.symtab = current_subfile->symtab;
12121 tu_group->symtabs[i] = fe.symtab;
12122 }
12123 }
12124 else
12125 {
12126 restart_symtab (tu_group->compunit_symtab, "", 0);
12127
12128 for (i = 0; i < cu->line_header->file_names.size (); ++i)
12129 {
12130 file_entry &fe = cu->line_header->file_names[i];
12131
12132 fe.symtab = tu_group->symtabs[i];
12133 }
12134 }
12135
12136 /* The main symtab is allocated last. Type units don't have DW_AT_name
12137 so they don't have a "real" (so to speak) symtab anyway.
12138 There is later code that will assign the main symtab to all symbols
12139 that don't have one. We need to handle the case of a symbol with a
12140 missing symtab (DW_AT_decl_file) anyway. */
12141 }
12142
12143 /* Process DW_TAG_type_unit.
12144 For TUs we want to skip the first top level sibling if it's not the
12145 actual type being defined by this TU. In this case the first top
12146 level sibling is there to provide context only. */
12147
12148 static void
12149 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
12150 {
12151 struct die_info *child_die;
12152
12153 prepare_one_comp_unit (cu, die, language_minimal);
12154
12155 /* Initialize (or reinitialize) the machinery for building symtabs.
12156 We do this before processing child DIEs, so that the line header table
12157 is available for DW_AT_decl_file. */
12158 setup_type_unit_groups (die, cu);
12159
12160 if (die->child != NULL)
12161 {
12162 child_die = die->child;
12163 while (child_die && child_die->tag)
12164 {
12165 process_die (child_die, cu);
12166 child_die = sibling_die (child_die);
12167 }
12168 }
12169 }
12170 \f
12171 /* DWO/DWP files.
12172
12173 http://gcc.gnu.org/wiki/DebugFission
12174 http://gcc.gnu.org/wiki/DebugFissionDWP
12175
12176 To simplify handling of both DWO files ("object" files with the DWARF info)
12177 and DWP files (a file with the DWOs packaged up into one file), we treat
12178 DWP files as having a collection of virtual DWO files. */
12179
12180 static hashval_t
12181 hash_dwo_file (const void *item)
12182 {
12183 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
12184 hashval_t hash;
12185
12186 hash = htab_hash_string (dwo_file->dwo_name);
12187 if (dwo_file->comp_dir != NULL)
12188 hash += htab_hash_string (dwo_file->comp_dir);
12189 return hash;
12190 }
12191
12192 static int
12193 eq_dwo_file (const void *item_lhs, const void *item_rhs)
12194 {
12195 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
12196 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
12197
12198 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
12199 return 0;
12200 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
12201 return lhs->comp_dir == rhs->comp_dir;
12202 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
12203 }
12204
12205 /* Allocate a hash table for DWO files. */
12206
12207 static htab_t
12208 allocate_dwo_file_hash_table (struct objfile *objfile)
12209 {
12210 return htab_create_alloc_ex (41,
12211 hash_dwo_file,
12212 eq_dwo_file,
12213 NULL,
12214 &objfile->objfile_obstack,
12215 hashtab_obstack_allocate,
12216 dummy_obstack_deallocate);
12217 }
12218
12219 /* Lookup DWO file DWO_NAME. */
12220
12221 static void **
12222 lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
12223 const char *dwo_name,
12224 const char *comp_dir)
12225 {
12226 struct dwo_file find_entry;
12227 void **slot;
12228
12229 if (dwarf2_per_objfile->dwo_files == NULL)
12230 dwarf2_per_objfile->dwo_files
12231 = allocate_dwo_file_hash_table (dwarf2_per_objfile->objfile);
12232
12233 memset (&find_entry, 0, sizeof (find_entry));
12234 find_entry.dwo_name = dwo_name;
12235 find_entry.comp_dir = comp_dir;
12236 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
12237
12238 return slot;
12239 }
12240
12241 static hashval_t
12242 hash_dwo_unit (const void *item)
12243 {
12244 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
12245
12246 /* This drops the top 32 bits of the id, but is ok for a hash. */
12247 return dwo_unit->signature;
12248 }
12249
12250 static int
12251 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
12252 {
12253 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
12254 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
12255
12256 /* The signature is assumed to be unique within the DWO file.
12257 So while object file CU dwo_id's always have the value zero,
12258 that's OK, assuming each object file DWO file has only one CU,
12259 and that's the rule for now. */
12260 return lhs->signature == rhs->signature;
12261 }
12262
12263 /* Allocate a hash table for DWO CUs,TUs.
12264 There is one of these tables for each of CUs,TUs for each DWO file. */
12265
12266 static htab_t
12267 allocate_dwo_unit_table (struct objfile *objfile)
12268 {
12269 /* Start out with a pretty small number.
12270 Generally DWO files contain only one CU and maybe some TUs. */
12271 return htab_create_alloc_ex (3,
12272 hash_dwo_unit,
12273 eq_dwo_unit,
12274 NULL,
12275 &objfile->objfile_obstack,
12276 hashtab_obstack_allocate,
12277 dummy_obstack_deallocate);
12278 }
12279
12280 /* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
12281
12282 struct create_dwo_cu_data
12283 {
12284 struct dwo_file *dwo_file;
12285 struct dwo_unit dwo_unit;
12286 };
12287
12288 /* die_reader_func for create_dwo_cu. */
12289
12290 static void
12291 create_dwo_cu_reader (const struct die_reader_specs *reader,
12292 const gdb_byte *info_ptr,
12293 struct die_info *comp_unit_die,
12294 int has_children,
12295 void *datap)
12296 {
12297 struct dwarf2_cu *cu = reader->cu;
12298 sect_offset sect_off = cu->per_cu->sect_off;
12299 struct dwarf2_section_info *section = cu->per_cu->section;
12300 struct create_dwo_cu_data *data = (struct create_dwo_cu_data *) datap;
12301 struct dwo_file *dwo_file = data->dwo_file;
12302 struct dwo_unit *dwo_unit = &data->dwo_unit;
12303 struct attribute *attr;
12304
12305 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
12306 if (attr == NULL)
12307 {
12308 complaint (&symfile_complaints,
12309 _("Dwarf Error: debug entry at offset %s is missing"
12310 " its dwo_id [in module %s]"),
12311 sect_offset_str (sect_off), dwo_file->dwo_name);
12312 return;
12313 }
12314
12315 dwo_unit->dwo_file = dwo_file;
12316 dwo_unit->signature = DW_UNSND (attr);
12317 dwo_unit->section = section;
12318 dwo_unit->sect_off = sect_off;
12319 dwo_unit->length = cu->per_cu->length;
12320
12321 if (dwarf_read_debug)
12322 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
12323 sect_offset_str (sect_off),
12324 hex_string (dwo_unit->signature));
12325 }
12326
12327 /* Create the dwo_units for the CUs in a DWO_FILE.
12328 Note: This function processes DWO files only, not DWP files. */
12329
12330 static void
12331 create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
12332 struct dwo_file &dwo_file, dwarf2_section_info &section,
12333 htab_t &cus_htab)
12334 {
12335 struct objfile *objfile = dwarf2_per_objfile->objfile;
12336 const gdb_byte *info_ptr, *end_ptr;
12337
12338 dwarf2_read_section (objfile, &section);
12339 info_ptr = section.buffer;
12340
12341 if (info_ptr == NULL)
12342 return;
12343
12344 if (dwarf_read_debug)
12345 {
12346 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
12347 get_section_name (&section),
12348 get_section_file_name (&section));
12349 }
12350
12351 end_ptr = info_ptr + section.size;
12352 while (info_ptr < end_ptr)
12353 {
12354 struct dwarf2_per_cu_data per_cu;
12355 struct create_dwo_cu_data create_dwo_cu_data;
12356 struct dwo_unit *dwo_unit;
12357 void **slot;
12358 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
12359
12360 memset (&create_dwo_cu_data.dwo_unit, 0,
12361 sizeof (create_dwo_cu_data.dwo_unit));
12362 memset (&per_cu, 0, sizeof (per_cu));
12363 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
12364 per_cu.is_debug_types = 0;
12365 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
12366 per_cu.section = &section;
12367 create_dwo_cu_data.dwo_file = &dwo_file;
12368
12369 init_cutu_and_read_dies_no_follow (
12370 &per_cu, &dwo_file, create_dwo_cu_reader, &create_dwo_cu_data);
12371 info_ptr += per_cu.length;
12372
12373 // If the unit could not be parsed, skip it.
12374 if (create_dwo_cu_data.dwo_unit.dwo_file == NULL)
12375 continue;
12376
12377 if (cus_htab == NULL)
12378 cus_htab = allocate_dwo_unit_table (objfile);
12379
12380 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12381 *dwo_unit = create_dwo_cu_data.dwo_unit;
12382 slot = htab_find_slot (cus_htab, dwo_unit, INSERT);
12383 gdb_assert (slot != NULL);
12384 if (*slot != NULL)
12385 {
12386 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
12387 sect_offset dup_sect_off = dup_cu->sect_off;
12388
12389 complaint (&symfile_complaints,
12390 _("debug cu entry at offset %s is duplicate to"
12391 " the entry at offset %s, signature %s"),
12392 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
12393 hex_string (dwo_unit->signature));
12394 }
12395 *slot = (void *)dwo_unit;
12396 }
12397 }
12398
12399 /* DWP file .debug_{cu,tu}_index section format:
12400 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
12401
12402 DWP Version 1:
12403
12404 Both index sections have the same format, and serve to map a 64-bit
12405 signature to a set of section numbers. Each section begins with a header,
12406 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
12407 indexes, and a pool of 32-bit section numbers. The index sections will be
12408 aligned at 8-byte boundaries in the file.
12409
12410 The index section header consists of:
12411
12412 V, 32 bit version number
12413 -, 32 bits unused
12414 N, 32 bit number of compilation units or type units in the index
12415 M, 32 bit number of slots in the hash table
12416
12417 Numbers are recorded using the byte order of the application binary.
12418
12419 The hash table begins at offset 16 in the section, and consists of an array
12420 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
12421 order of the application binary). Unused slots in the hash table are 0.
12422 (We rely on the extreme unlikeliness of a signature being exactly 0.)
12423
12424 The parallel table begins immediately after the hash table
12425 (at offset 16 + 8 * M from the beginning of the section), and consists of an
12426 array of 32-bit indexes (using the byte order of the application binary),
12427 corresponding 1-1 with slots in the hash table. Each entry in the parallel
12428 table contains a 32-bit index into the pool of section numbers. For unused
12429 hash table slots, the corresponding entry in the parallel table will be 0.
12430
12431 The pool of section numbers begins immediately following the hash table
12432 (at offset 16 + 12 * M from the beginning of the section). The pool of
12433 section numbers consists of an array of 32-bit words (using the byte order
12434 of the application binary). Each item in the array is indexed starting
12435 from 0. The hash table entry provides the index of the first section
12436 number in the set. Additional section numbers in the set follow, and the
12437 set is terminated by a 0 entry (section number 0 is not used in ELF).
12438
12439 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
12440 section must be the first entry in the set, and the .debug_abbrev.dwo must
12441 be the second entry. Other members of the set may follow in any order.
12442
12443 ---
12444
12445 DWP Version 2:
12446
12447 DWP Version 2 combines all the .debug_info, etc. sections into one,
12448 and the entries in the index tables are now offsets into these sections.
12449 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
12450 section.
12451
12452 Index Section Contents:
12453 Header
12454 Hash Table of Signatures dwp_hash_table.hash_table
12455 Parallel Table of Indices dwp_hash_table.unit_table
12456 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
12457 Table of Section Sizes dwp_hash_table.v2.sizes
12458
12459 The index section header consists of:
12460
12461 V, 32 bit version number
12462 L, 32 bit number of columns in the table of section offsets
12463 N, 32 bit number of compilation units or type units in the index
12464 M, 32 bit number of slots in the hash table
12465
12466 Numbers are recorded using the byte order of the application binary.
12467
12468 The hash table has the same format as version 1.
12469 The parallel table of indices has the same format as version 1,
12470 except that the entries are origin-1 indices into the table of sections
12471 offsets and the table of section sizes.
12472
12473 The table of offsets begins immediately following the parallel table
12474 (at offset 16 + 12 * M from the beginning of the section). The table is
12475 a two-dimensional array of 32-bit words (using the byte order of the
12476 application binary), with L columns and N+1 rows, in row-major order.
12477 Each row in the array is indexed starting from 0. The first row provides
12478 a key to the remaining rows: each column in this row provides an identifier
12479 for a debug section, and the offsets in the same column of subsequent rows
12480 refer to that section. The section identifiers are:
12481
12482 DW_SECT_INFO 1 .debug_info.dwo
12483 DW_SECT_TYPES 2 .debug_types.dwo
12484 DW_SECT_ABBREV 3 .debug_abbrev.dwo
12485 DW_SECT_LINE 4 .debug_line.dwo
12486 DW_SECT_LOC 5 .debug_loc.dwo
12487 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
12488 DW_SECT_MACINFO 7 .debug_macinfo.dwo
12489 DW_SECT_MACRO 8 .debug_macro.dwo
12490
12491 The offsets provided by the CU and TU index sections are the base offsets
12492 for the contributions made by each CU or TU to the corresponding section
12493 in the package file. Each CU and TU header contains an abbrev_offset
12494 field, used to find the abbreviations table for that CU or TU within the
12495 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
12496 be interpreted as relative to the base offset given in the index section.
12497 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
12498 should be interpreted as relative to the base offset for .debug_line.dwo,
12499 and offsets into other debug sections obtained from DWARF attributes should
12500 also be interpreted as relative to the corresponding base offset.
12501
12502 The table of sizes begins immediately following the table of offsets.
12503 Like the table of offsets, it is a two-dimensional array of 32-bit words,
12504 with L columns and N rows, in row-major order. Each row in the array is
12505 indexed starting from 1 (row 0 is shared by the two tables).
12506
12507 ---
12508
12509 Hash table lookup is handled the same in version 1 and 2:
12510
12511 We assume that N and M will not exceed 2^32 - 1.
12512 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
12513
12514 Given a 64-bit compilation unit signature or a type signature S, an entry
12515 in the hash table is located as follows:
12516
12517 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
12518 the low-order k bits all set to 1.
12519
12520 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
12521
12522 3) If the hash table entry at index H matches the signature, use that
12523 entry. If the hash table entry at index H is unused (all zeroes),
12524 terminate the search: the signature is not present in the table.
12525
12526 4) Let H = (H + H') modulo M. Repeat at Step 3.
12527
12528 Because M > N and H' and M are relatively prime, the search is guaranteed
12529 to stop at an unused slot or find the match. */
12530
12531 /* Create a hash table to map DWO IDs to their CU/TU entry in
12532 .debug_{info,types}.dwo in DWP_FILE.
12533 Returns NULL if there isn't one.
12534 Note: This function processes DWP files only, not DWO files. */
12535
12536 static struct dwp_hash_table *
12537 create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
12538 struct dwp_file *dwp_file, int is_debug_types)
12539 {
12540 struct objfile *objfile = dwarf2_per_objfile->objfile;
12541 bfd *dbfd = dwp_file->dbfd;
12542 const gdb_byte *index_ptr, *index_end;
12543 struct dwarf2_section_info *index;
12544 uint32_t version, nr_columns, nr_units, nr_slots;
12545 struct dwp_hash_table *htab;
12546
12547 if (is_debug_types)
12548 index = &dwp_file->sections.tu_index;
12549 else
12550 index = &dwp_file->sections.cu_index;
12551
12552 if (dwarf2_section_empty_p (index))
12553 return NULL;
12554 dwarf2_read_section (objfile, index);
12555
12556 index_ptr = index->buffer;
12557 index_end = index_ptr + index->size;
12558
12559 version = read_4_bytes (dbfd, index_ptr);
12560 index_ptr += 4;
12561 if (version == 2)
12562 nr_columns = read_4_bytes (dbfd, index_ptr);
12563 else
12564 nr_columns = 0;
12565 index_ptr += 4;
12566 nr_units = read_4_bytes (dbfd, index_ptr);
12567 index_ptr += 4;
12568 nr_slots = read_4_bytes (dbfd, index_ptr);
12569 index_ptr += 4;
12570
12571 if (version != 1 && version != 2)
12572 {
12573 error (_("Dwarf Error: unsupported DWP file version (%s)"
12574 " [in module %s]"),
12575 pulongest (version), dwp_file->name);
12576 }
12577 if (nr_slots != (nr_slots & -nr_slots))
12578 {
12579 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
12580 " is not power of 2 [in module %s]"),
12581 pulongest (nr_slots), dwp_file->name);
12582 }
12583
12584 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
12585 htab->version = version;
12586 htab->nr_columns = nr_columns;
12587 htab->nr_units = nr_units;
12588 htab->nr_slots = nr_slots;
12589 htab->hash_table = index_ptr;
12590 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
12591
12592 /* Exit early if the table is empty. */
12593 if (nr_slots == 0 || nr_units == 0
12594 || (version == 2 && nr_columns == 0))
12595 {
12596 /* All must be zero. */
12597 if (nr_slots != 0 || nr_units != 0
12598 || (version == 2 && nr_columns != 0))
12599 {
12600 complaint (&symfile_complaints,
12601 _("Empty DWP but nr_slots,nr_units,nr_columns not"
12602 " all zero [in modules %s]"),
12603 dwp_file->name);
12604 }
12605 return htab;
12606 }
12607
12608 if (version == 1)
12609 {
12610 htab->section_pool.v1.indices =
12611 htab->unit_table + sizeof (uint32_t) * nr_slots;
12612 /* It's harder to decide whether the section is too small in v1.
12613 V1 is deprecated anyway so we punt. */
12614 }
12615 else
12616 {
12617 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
12618 int *ids = htab->section_pool.v2.section_ids;
12619 /* Reverse map for error checking. */
12620 int ids_seen[DW_SECT_MAX + 1];
12621 int i;
12622
12623 if (nr_columns < 2)
12624 {
12625 error (_("Dwarf Error: bad DWP hash table, too few columns"
12626 " in section table [in module %s]"),
12627 dwp_file->name);
12628 }
12629 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
12630 {
12631 error (_("Dwarf Error: bad DWP hash table, too many columns"
12632 " in section table [in module %s]"),
12633 dwp_file->name);
12634 }
12635 memset (ids, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
12636 memset (ids_seen, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
12637 for (i = 0; i < nr_columns; ++i)
12638 {
12639 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
12640
12641 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
12642 {
12643 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
12644 " in section table [in module %s]"),
12645 id, dwp_file->name);
12646 }
12647 if (ids_seen[id] != -1)
12648 {
12649 error (_("Dwarf Error: bad DWP hash table, duplicate section"
12650 " id %d in section table [in module %s]"),
12651 id, dwp_file->name);
12652 }
12653 ids_seen[id] = i;
12654 ids[i] = id;
12655 }
12656 /* Must have exactly one info or types section. */
12657 if (((ids_seen[DW_SECT_INFO] != -1)
12658 + (ids_seen[DW_SECT_TYPES] != -1))
12659 != 1)
12660 {
12661 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
12662 " DWO info/types section [in module %s]"),
12663 dwp_file->name);
12664 }
12665 /* Must have an abbrev section. */
12666 if (ids_seen[DW_SECT_ABBREV] == -1)
12667 {
12668 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
12669 " section [in module %s]"),
12670 dwp_file->name);
12671 }
12672 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
12673 htab->section_pool.v2.sizes =
12674 htab->section_pool.v2.offsets + (sizeof (uint32_t)
12675 * nr_units * nr_columns);
12676 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
12677 * nr_units * nr_columns))
12678 > index_end)
12679 {
12680 error (_("Dwarf Error: DWP index section is corrupt (too small)"
12681 " [in module %s]"),
12682 dwp_file->name);
12683 }
12684 }
12685
12686 return htab;
12687 }
12688
12689 /* Update SECTIONS with the data from SECTP.
12690
12691 This function is like the other "locate" section routines that are
12692 passed to bfd_map_over_sections, but in this context the sections to
12693 read comes from the DWP V1 hash table, not the full ELF section table.
12694
12695 The result is non-zero for success, or zero if an error was found. */
12696
12697 static int
12698 locate_v1_virtual_dwo_sections (asection *sectp,
12699 struct virtual_v1_dwo_sections *sections)
12700 {
12701 const struct dwop_section_names *names = &dwop_section_names;
12702
12703 if (section_is_p (sectp->name, &names->abbrev_dwo))
12704 {
12705 /* There can be only one. */
12706 if (sections->abbrev.s.section != NULL)
12707 return 0;
12708 sections->abbrev.s.section = sectp;
12709 sections->abbrev.size = bfd_get_section_size (sectp);
12710 }
12711 else if (section_is_p (sectp->name, &names->info_dwo)
12712 || section_is_p (sectp->name, &names->types_dwo))
12713 {
12714 /* There can be only one. */
12715 if (sections->info_or_types.s.section != NULL)
12716 return 0;
12717 sections->info_or_types.s.section = sectp;
12718 sections->info_or_types.size = bfd_get_section_size (sectp);
12719 }
12720 else if (section_is_p (sectp->name, &names->line_dwo))
12721 {
12722 /* There can be only one. */
12723 if (sections->line.s.section != NULL)
12724 return 0;
12725 sections->line.s.section = sectp;
12726 sections->line.size = bfd_get_section_size (sectp);
12727 }
12728 else if (section_is_p (sectp->name, &names->loc_dwo))
12729 {
12730 /* There can be only one. */
12731 if (sections->loc.s.section != NULL)
12732 return 0;
12733 sections->loc.s.section = sectp;
12734 sections->loc.size = bfd_get_section_size (sectp);
12735 }
12736 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12737 {
12738 /* There can be only one. */
12739 if (sections->macinfo.s.section != NULL)
12740 return 0;
12741 sections->macinfo.s.section = sectp;
12742 sections->macinfo.size = bfd_get_section_size (sectp);
12743 }
12744 else if (section_is_p (sectp->name, &names->macro_dwo))
12745 {
12746 /* There can be only one. */
12747 if (sections->macro.s.section != NULL)
12748 return 0;
12749 sections->macro.s.section = sectp;
12750 sections->macro.size = bfd_get_section_size (sectp);
12751 }
12752 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12753 {
12754 /* There can be only one. */
12755 if (sections->str_offsets.s.section != NULL)
12756 return 0;
12757 sections->str_offsets.s.section = sectp;
12758 sections->str_offsets.size = bfd_get_section_size (sectp);
12759 }
12760 else
12761 {
12762 /* No other kind of section is valid. */
12763 return 0;
12764 }
12765
12766 return 1;
12767 }
12768
12769 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12770 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12771 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12772 This is for DWP version 1 files. */
12773
12774 static struct dwo_unit *
12775 create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12776 struct dwp_file *dwp_file,
12777 uint32_t unit_index,
12778 const char *comp_dir,
12779 ULONGEST signature, int is_debug_types)
12780 {
12781 struct objfile *objfile = dwarf2_per_objfile->objfile;
12782 const struct dwp_hash_table *dwp_htab =
12783 is_debug_types ? dwp_file->tus : dwp_file->cus;
12784 bfd *dbfd = dwp_file->dbfd;
12785 const char *kind = is_debug_types ? "TU" : "CU";
12786 struct dwo_file *dwo_file;
12787 struct dwo_unit *dwo_unit;
12788 struct virtual_v1_dwo_sections sections;
12789 void **dwo_file_slot;
12790 int i;
12791
12792 gdb_assert (dwp_file->version == 1);
12793
12794 if (dwarf_read_debug)
12795 {
12796 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
12797 kind,
12798 pulongest (unit_index), hex_string (signature),
12799 dwp_file->name);
12800 }
12801
12802 /* Fetch the sections of this DWO unit.
12803 Put a limit on the number of sections we look for so that bad data
12804 doesn't cause us to loop forever. */
12805
12806 #define MAX_NR_V1_DWO_SECTIONS \
12807 (1 /* .debug_info or .debug_types */ \
12808 + 1 /* .debug_abbrev */ \
12809 + 1 /* .debug_line */ \
12810 + 1 /* .debug_loc */ \
12811 + 1 /* .debug_str_offsets */ \
12812 + 1 /* .debug_macro or .debug_macinfo */ \
12813 + 1 /* trailing zero */)
12814
12815 memset (&sections, 0, sizeof (sections));
12816
12817 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
12818 {
12819 asection *sectp;
12820 uint32_t section_nr =
12821 read_4_bytes (dbfd,
12822 dwp_htab->section_pool.v1.indices
12823 + (unit_index + i) * sizeof (uint32_t));
12824
12825 if (section_nr == 0)
12826 break;
12827 if (section_nr >= dwp_file->num_sections)
12828 {
12829 error (_("Dwarf Error: bad DWP hash table, section number too large"
12830 " [in module %s]"),
12831 dwp_file->name);
12832 }
12833
12834 sectp = dwp_file->elf_sections[section_nr];
12835 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
12836 {
12837 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12838 " [in module %s]"),
12839 dwp_file->name);
12840 }
12841 }
12842
12843 if (i < 2
12844 || dwarf2_section_empty_p (&sections.info_or_types)
12845 || dwarf2_section_empty_p (&sections.abbrev))
12846 {
12847 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12848 " [in module %s]"),
12849 dwp_file->name);
12850 }
12851 if (i == MAX_NR_V1_DWO_SECTIONS)
12852 {
12853 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12854 " [in module %s]"),
12855 dwp_file->name);
12856 }
12857
12858 /* It's easier for the rest of the code if we fake a struct dwo_file and
12859 have dwo_unit "live" in that. At least for now.
12860
12861 The DWP file can be made up of a random collection of CUs and TUs.
12862 However, for each CU + set of TUs that came from the same original DWO
12863 file, we can combine them back into a virtual DWO file to save space
12864 (fewer struct dwo_file objects to allocate). Remember that for really
12865 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12866
12867 std::string virtual_dwo_name =
12868 string_printf ("virtual-dwo/%d-%d-%d-%d",
12869 get_section_id (&sections.abbrev),
12870 get_section_id (&sections.line),
12871 get_section_id (&sections.loc),
12872 get_section_id (&sections.str_offsets));
12873 /* Can we use an existing virtual DWO file? */
12874 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12875 virtual_dwo_name.c_str (),
12876 comp_dir);
12877 /* Create one if necessary. */
12878 if (*dwo_file_slot == NULL)
12879 {
12880 if (dwarf_read_debug)
12881 {
12882 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12883 virtual_dwo_name.c_str ());
12884 }
12885 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
12886 dwo_file->dwo_name
12887 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
12888 virtual_dwo_name.c_str (),
12889 virtual_dwo_name.size ());
12890 dwo_file->comp_dir = comp_dir;
12891 dwo_file->sections.abbrev = sections.abbrev;
12892 dwo_file->sections.line = sections.line;
12893 dwo_file->sections.loc = sections.loc;
12894 dwo_file->sections.macinfo = sections.macinfo;
12895 dwo_file->sections.macro = sections.macro;
12896 dwo_file->sections.str_offsets = sections.str_offsets;
12897 /* The "str" section is global to the entire DWP file. */
12898 dwo_file->sections.str = dwp_file->sections.str;
12899 /* The info or types section is assigned below to dwo_unit,
12900 there's no need to record it in dwo_file.
12901 Also, we can't simply record type sections in dwo_file because
12902 we record a pointer into the vector in dwo_unit. As we collect more
12903 types we'll grow the vector and eventually have to reallocate space
12904 for it, invalidating all copies of pointers into the previous
12905 contents. */
12906 *dwo_file_slot = dwo_file;
12907 }
12908 else
12909 {
12910 if (dwarf_read_debug)
12911 {
12912 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12913 virtual_dwo_name.c_str ());
12914 }
12915 dwo_file = (struct dwo_file *) *dwo_file_slot;
12916 }
12917
12918 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12919 dwo_unit->dwo_file = dwo_file;
12920 dwo_unit->signature = signature;
12921 dwo_unit->section =
12922 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
12923 *dwo_unit->section = sections.info_or_types;
12924 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12925
12926 return dwo_unit;
12927 }
12928
12929 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
12930 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
12931 piece within that section used by a TU/CU, return a virtual section
12932 of just that piece. */
12933
12934 static struct dwarf2_section_info
12935 create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
12936 struct dwarf2_section_info *section,
12937 bfd_size_type offset, bfd_size_type size)
12938 {
12939 struct dwarf2_section_info result;
12940 asection *sectp;
12941
12942 gdb_assert (section != NULL);
12943 gdb_assert (!section->is_virtual);
12944
12945 memset (&result, 0, sizeof (result));
12946 result.s.containing_section = section;
12947 result.is_virtual = 1;
12948
12949 if (size == 0)
12950 return result;
12951
12952 sectp = get_section_bfd_section (section);
12953
12954 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12955 bounds of the real section. This is a pretty-rare event, so just
12956 flag an error (easier) instead of a warning and trying to cope. */
12957 if (sectp == NULL
12958 || offset + size > bfd_get_section_size (sectp))
12959 {
12960 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
12961 " in section %s [in module %s]"),
12962 sectp ? bfd_section_name (abfd, sectp) : "<unknown>",
12963 objfile_name (dwarf2_per_objfile->objfile));
12964 }
12965
12966 result.virtual_offset = offset;
12967 result.size = size;
12968 return result;
12969 }
12970
12971 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12972 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12973 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12974 This is for DWP version 2 files. */
12975
12976 static struct dwo_unit *
12977 create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12978 struct dwp_file *dwp_file,
12979 uint32_t unit_index,
12980 const char *comp_dir,
12981 ULONGEST signature, int is_debug_types)
12982 {
12983 struct objfile *objfile = dwarf2_per_objfile->objfile;
12984 const struct dwp_hash_table *dwp_htab =
12985 is_debug_types ? dwp_file->tus : dwp_file->cus;
12986 bfd *dbfd = dwp_file->dbfd;
12987 const char *kind = is_debug_types ? "TU" : "CU";
12988 struct dwo_file *dwo_file;
12989 struct dwo_unit *dwo_unit;
12990 struct virtual_v2_dwo_sections sections;
12991 void **dwo_file_slot;
12992 int i;
12993
12994 gdb_assert (dwp_file->version == 2);
12995
12996 if (dwarf_read_debug)
12997 {
12998 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
12999 kind,
13000 pulongest (unit_index), hex_string (signature),
13001 dwp_file->name);
13002 }
13003
13004 /* Fetch the section offsets of this DWO unit. */
13005
13006 memset (&sections, 0, sizeof (sections));
13007
13008 for (i = 0; i < dwp_htab->nr_columns; ++i)
13009 {
13010 uint32_t offset = read_4_bytes (dbfd,
13011 dwp_htab->section_pool.v2.offsets
13012 + (((unit_index - 1) * dwp_htab->nr_columns
13013 + i)
13014 * sizeof (uint32_t)));
13015 uint32_t size = read_4_bytes (dbfd,
13016 dwp_htab->section_pool.v2.sizes
13017 + (((unit_index - 1) * dwp_htab->nr_columns
13018 + i)
13019 * sizeof (uint32_t)));
13020
13021 switch (dwp_htab->section_pool.v2.section_ids[i])
13022 {
13023 case DW_SECT_INFO:
13024 case DW_SECT_TYPES:
13025 sections.info_or_types_offset = offset;
13026 sections.info_or_types_size = size;
13027 break;
13028 case DW_SECT_ABBREV:
13029 sections.abbrev_offset = offset;
13030 sections.abbrev_size = size;
13031 break;
13032 case DW_SECT_LINE:
13033 sections.line_offset = offset;
13034 sections.line_size = size;
13035 break;
13036 case DW_SECT_LOC:
13037 sections.loc_offset = offset;
13038 sections.loc_size = size;
13039 break;
13040 case DW_SECT_STR_OFFSETS:
13041 sections.str_offsets_offset = offset;
13042 sections.str_offsets_size = size;
13043 break;
13044 case DW_SECT_MACINFO:
13045 sections.macinfo_offset = offset;
13046 sections.macinfo_size = size;
13047 break;
13048 case DW_SECT_MACRO:
13049 sections.macro_offset = offset;
13050 sections.macro_size = size;
13051 break;
13052 }
13053 }
13054
13055 /* It's easier for the rest of the code if we fake a struct dwo_file and
13056 have dwo_unit "live" in that. At least for now.
13057
13058 The DWP file can be made up of a random collection of CUs and TUs.
13059 However, for each CU + set of TUs that came from the same original DWO
13060 file, we can combine them back into a virtual DWO file to save space
13061 (fewer struct dwo_file objects to allocate). Remember that for really
13062 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
13063
13064 std::string virtual_dwo_name =
13065 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
13066 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
13067 (long) (sections.line_size ? sections.line_offset : 0),
13068 (long) (sections.loc_size ? sections.loc_offset : 0),
13069 (long) (sections.str_offsets_size
13070 ? sections.str_offsets_offset : 0));
13071 /* Can we use an existing virtual DWO file? */
13072 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
13073 virtual_dwo_name.c_str (),
13074 comp_dir);
13075 /* Create one if necessary. */
13076 if (*dwo_file_slot == NULL)
13077 {
13078 if (dwarf_read_debug)
13079 {
13080 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
13081 virtual_dwo_name.c_str ());
13082 }
13083 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
13084 dwo_file->dwo_name
13085 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
13086 virtual_dwo_name.c_str (),
13087 virtual_dwo_name.size ());
13088 dwo_file->comp_dir = comp_dir;
13089 dwo_file->sections.abbrev =
13090 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
13091 sections.abbrev_offset, sections.abbrev_size);
13092 dwo_file->sections.line =
13093 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
13094 sections.line_offset, sections.line_size);
13095 dwo_file->sections.loc =
13096 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
13097 sections.loc_offset, sections.loc_size);
13098 dwo_file->sections.macinfo =
13099 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
13100 sections.macinfo_offset, sections.macinfo_size);
13101 dwo_file->sections.macro =
13102 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
13103 sections.macro_offset, sections.macro_size);
13104 dwo_file->sections.str_offsets =
13105 create_dwp_v2_section (dwarf2_per_objfile,
13106 &dwp_file->sections.str_offsets,
13107 sections.str_offsets_offset,
13108 sections.str_offsets_size);
13109 /* The "str" section is global to the entire DWP file. */
13110 dwo_file->sections.str = dwp_file->sections.str;
13111 /* The info or types section is assigned below to dwo_unit,
13112 there's no need to record it in dwo_file.
13113 Also, we can't simply record type sections in dwo_file because
13114 we record a pointer into the vector in dwo_unit. As we collect more
13115 types we'll grow the vector and eventually have to reallocate space
13116 for it, invalidating all copies of pointers into the previous
13117 contents. */
13118 *dwo_file_slot = dwo_file;
13119 }
13120 else
13121 {
13122 if (dwarf_read_debug)
13123 {
13124 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
13125 virtual_dwo_name.c_str ());
13126 }
13127 dwo_file = (struct dwo_file *) *dwo_file_slot;
13128 }
13129
13130 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
13131 dwo_unit->dwo_file = dwo_file;
13132 dwo_unit->signature = signature;
13133 dwo_unit->section =
13134 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
13135 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
13136 is_debug_types
13137 ? &dwp_file->sections.types
13138 : &dwp_file->sections.info,
13139 sections.info_or_types_offset,
13140 sections.info_or_types_size);
13141 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
13142
13143 return dwo_unit;
13144 }
13145
13146 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
13147 Returns NULL if the signature isn't found. */
13148
13149 static struct dwo_unit *
13150 lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
13151 struct dwp_file *dwp_file, const char *comp_dir,
13152 ULONGEST signature, int is_debug_types)
13153 {
13154 const struct dwp_hash_table *dwp_htab =
13155 is_debug_types ? dwp_file->tus : dwp_file->cus;
13156 bfd *dbfd = dwp_file->dbfd;
13157 uint32_t mask = dwp_htab->nr_slots - 1;
13158 uint32_t hash = signature & mask;
13159 uint32_t hash2 = ((signature >> 32) & mask) | 1;
13160 unsigned int i;
13161 void **slot;
13162 struct dwo_unit find_dwo_cu;
13163
13164 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
13165 find_dwo_cu.signature = signature;
13166 slot = htab_find_slot (is_debug_types
13167 ? dwp_file->loaded_tus
13168 : dwp_file->loaded_cus,
13169 &find_dwo_cu, INSERT);
13170
13171 if (*slot != NULL)
13172 return (struct dwo_unit *) *slot;
13173
13174 /* Use a for loop so that we don't loop forever on bad debug info. */
13175 for (i = 0; i < dwp_htab->nr_slots; ++i)
13176 {
13177 ULONGEST signature_in_table;
13178
13179 signature_in_table =
13180 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
13181 if (signature_in_table == signature)
13182 {
13183 uint32_t unit_index =
13184 read_4_bytes (dbfd,
13185 dwp_htab->unit_table + hash * sizeof (uint32_t));
13186
13187 if (dwp_file->version == 1)
13188 {
13189 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
13190 dwp_file, unit_index,
13191 comp_dir, signature,
13192 is_debug_types);
13193 }
13194 else
13195 {
13196 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
13197 dwp_file, unit_index,
13198 comp_dir, signature,
13199 is_debug_types);
13200 }
13201 return (struct dwo_unit *) *slot;
13202 }
13203 if (signature_in_table == 0)
13204 return NULL;
13205 hash = (hash + hash2) & mask;
13206 }
13207
13208 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
13209 " [in module %s]"),
13210 dwp_file->name);
13211 }
13212
13213 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
13214 Open the file specified by FILE_NAME and hand it off to BFD for
13215 preliminary analysis. Return a newly initialized bfd *, which
13216 includes a canonicalized copy of FILE_NAME.
13217 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
13218 SEARCH_CWD is true if the current directory is to be searched.
13219 It will be searched before debug-file-directory.
13220 If successful, the file is added to the bfd include table of the
13221 objfile's bfd (see gdb_bfd_record_inclusion).
13222 If unable to find/open the file, return NULL.
13223 NOTE: This function is derived from symfile_bfd_open. */
13224
13225 static gdb_bfd_ref_ptr
13226 try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
13227 const char *file_name, int is_dwp, int search_cwd)
13228 {
13229 int desc;
13230 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
13231 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
13232 to debug_file_directory. */
13233 const char *search_path;
13234 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
13235
13236 gdb::unique_xmalloc_ptr<char> search_path_holder;
13237 if (search_cwd)
13238 {
13239 if (*debug_file_directory != '\0')
13240 {
13241 search_path_holder.reset (concat (".", dirname_separator_string,
13242 debug_file_directory,
13243 (char *) NULL));
13244 search_path = search_path_holder.get ();
13245 }
13246 else
13247 search_path = ".";
13248 }
13249 else
13250 search_path = debug_file_directory;
13251
13252 openp_flags flags = OPF_RETURN_REALPATH;
13253 if (is_dwp)
13254 flags |= OPF_SEARCH_IN_PATH;
13255
13256 gdb::unique_xmalloc_ptr<char> absolute_name;
13257 desc = openp (search_path, flags, file_name,
13258 O_RDONLY | O_BINARY, &absolute_name);
13259 if (desc < 0)
13260 return NULL;
13261
13262 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
13263 gnutarget, desc));
13264 if (sym_bfd == NULL)
13265 return NULL;
13266 bfd_set_cacheable (sym_bfd.get (), 1);
13267
13268 if (!bfd_check_format (sym_bfd.get (), bfd_object))
13269 return NULL;
13270
13271 /* Success. Record the bfd as having been included by the objfile's bfd.
13272 This is important because things like demangled_names_hash lives in the
13273 objfile's per_bfd space and may have references to things like symbol
13274 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
13275 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
13276
13277 return sym_bfd;
13278 }
13279
13280 /* Try to open DWO file FILE_NAME.
13281 COMP_DIR is the DW_AT_comp_dir attribute.
13282 The result is the bfd handle of the file.
13283 If there is a problem finding or opening the file, return NULL.
13284 Upon success, the canonicalized path of the file is stored in the bfd,
13285 same as symfile_bfd_open. */
13286
13287 static gdb_bfd_ref_ptr
13288 open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
13289 const char *file_name, const char *comp_dir)
13290 {
13291 if (IS_ABSOLUTE_PATH (file_name))
13292 return try_open_dwop_file (dwarf2_per_objfile, file_name,
13293 0 /*is_dwp*/, 0 /*search_cwd*/);
13294
13295 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
13296
13297 if (comp_dir != NULL)
13298 {
13299 char *path_to_try = concat (comp_dir, SLASH_STRING,
13300 file_name, (char *) NULL);
13301
13302 /* NOTE: If comp_dir is a relative path, this will also try the
13303 search path, which seems useful. */
13304 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
13305 path_to_try,
13306 0 /*is_dwp*/,
13307 1 /*search_cwd*/));
13308 xfree (path_to_try);
13309 if (abfd != NULL)
13310 return abfd;
13311 }
13312
13313 /* That didn't work, try debug-file-directory, which, despite its name,
13314 is a list of paths. */
13315
13316 if (*debug_file_directory == '\0')
13317 return NULL;
13318
13319 return try_open_dwop_file (dwarf2_per_objfile, file_name,
13320 0 /*is_dwp*/, 1 /*search_cwd*/);
13321 }
13322
13323 /* This function is mapped across the sections and remembers the offset and
13324 size of each of the DWO debugging sections we are interested in. */
13325
13326 static void
13327 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
13328 {
13329 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
13330 const struct dwop_section_names *names = &dwop_section_names;
13331
13332 if (section_is_p (sectp->name, &names->abbrev_dwo))
13333 {
13334 dwo_sections->abbrev.s.section = sectp;
13335 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
13336 }
13337 else if (section_is_p (sectp->name, &names->info_dwo))
13338 {
13339 dwo_sections->info.s.section = sectp;
13340 dwo_sections->info.size = bfd_get_section_size (sectp);
13341 }
13342 else if (section_is_p (sectp->name, &names->line_dwo))
13343 {
13344 dwo_sections->line.s.section = sectp;
13345 dwo_sections->line.size = bfd_get_section_size (sectp);
13346 }
13347 else if (section_is_p (sectp->name, &names->loc_dwo))
13348 {
13349 dwo_sections->loc.s.section = sectp;
13350 dwo_sections->loc.size = bfd_get_section_size (sectp);
13351 }
13352 else if (section_is_p (sectp->name, &names->macinfo_dwo))
13353 {
13354 dwo_sections->macinfo.s.section = sectp;
13355 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
13356 }
13357 else if (section_is_p (sectp->name, &names->macro_dwo))
13358 {
13359 dwo_sections->macro.s.section = sectp;
13360 dwo_sections->macro.size = bfd_get_section_size (sectp);
13361 }
13362 else if (section_is_p (sectp->name, &names->str_dwo))
13363 {
13364 dwo_sections->str.s.section = sectp;
13365 dwo_sections->str.size = bfd_get_section_size (sectp);
13366 }
13367 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13368 {
13369 dwo_sections->str_offsets.s.section = sectp;
13370 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
13371 }
13372 else if (section_is_p (sectp->name, &names->types_dwo))
13373 {
13374 struct dwarf2_section_info type_section;
13375
13376 memset (&type_section, 0, sizeof (type_section));
13377 type_section.s.section = sectp;
13378 type_section.size = bfd_get_section_size (sectp);
13379 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
13380 &type_section);
13381 }
13382 }
13383
13384 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
13385 by PER_CU. This is for the non-DWP case.
13386 The result is NULL if DWO_NAME can't be found. */
13387
13388 static struct dwo_file *
13389 open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
13390 const char *dwo_name, const char *comp_dir)
13391 {
13392 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
13393 struct objfile *objfile = dwarf2_per_objfile->objfile;
13394 struct dwo_file *dwo_file;
13395 struct cleanup *cleanups;
13396
13397 gdb_bfd_ref_ptr dbfd (open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir));
13398 if (dbfd == NULL)
13399 {
13400 if (dwarf_read_debug)
13401 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
13402 return NULL;
13403 }
13404 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
13405 dwo_file->dwo_name = dwo_name;
13406 dwo_file->comp_dir = comp_dir;
13407 dwo_file->dbfd = dbfd.release ();
13408
13409 free_dwo_file_cleanup_data *cleanup_data = XNEW (free_dwo_file_cleanup_data);
13410 cleanup_data->dwo_file = dwo_file;
13411 cleanup_data->dwarf2_per_objfile = dwarf2_per_objfile;
13412
13413 cleanups = make_cleanup (free_dwo_file_cleanup, cleanup_data);
13414
13415 bfd_map_over_sections (dwo_file->dbfd, dwarf2_locate_dwo_sections,
13416 &dwo_file->sections);
13417
13418 create_cus_hash_table (dwarf2_per_objfile, *dwo_file, dwo_file->sections.info,
13419 dwo_file->cus);
13420
13421 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file,
13422 dwo_file->sections.types, dwo_file->tus);
13423
13424 discard_cleanups (cleanups);
13425
13426 if (dwarf_read_debug)
13427 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
13428
13429 return dwo_file;
13430 }
13431
13432 /* This function is mapped across the sections and remembers the offset and
13433 size of each of the DWP debugging sections common to version 1 and 2 that
13434 we are interested in. */
13435
13436 static void
13437 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
13438 void *dwp_file_ptr)
13439 {
13440 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
13441 const struct dwop_section_names *names = &dwop_section_names;
13442 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13443
13444 /* Record the ELF section number for later lookup: this is what the
13445 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13446 gdb_assert (elf_section_nr < dwp_file->num_sections);
13447 dwp_file->elf_sections[elf_section_nr] = sectp;
13448
13449 /* Look for specific sections that we need. */
13450 if (section_is_p (sectp->name, &names->str_dwo))
13451 {
13452 dwp_file->sections.str.s.section = sectp;
13453 dwp_file->sections.str.size = bfd_get_section_size (sectp);
13454 }
13455 else if (section_is_p (sectp->name, &names->cu_index))
13456 {
13457 dwp_file->sections.cu_index.s.section = sectp;
13458 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
13459 }
13460 else if (section_is_p (sectp->name, &names->tu_index))
13461 {
13462 dwp_file->sections.tu_index.s.section = sectp;
13463 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
13464 }
13465 }
13466
13467 /* This function is mapped across the sections and remembers the offset and
13468 size of each of the DWP version 2 debugging sections that we are interested
13469 in. This is split into a separate function because we don't know if we
13470 have version 1 or 2 until we parse the cu_index/tu_index sections. */
13471
13472 static void
13473 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
13474 {
13475 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
13476 const struct dwop_section_names *names = &dwop_section_names;
13477 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13478
13479 /* Record the ELF section number for later lookup: this is what the
13480 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13481 gdb_assert (elf_section_nr < dwp_file->num_sections);
13482 dwp_file->elf_sections[elf_section_nr] = sectp;
13483
13484 /* Look for specific sections that we need. */
13485 if (section_is_p (sectp->name, &names->abbrev_dwo))
13486 {
13487 dwp_file->sections.abbrev.s.section = sectp;
13488 dwp_file->sections.abbrev.size = bfd_get_section_size (sectp);
13489 }
13490 else if (section_is_p (sectp->name, &names->info_dwo))
13491 {
13492 dwp_file->sections.info.s.section = sectp;
13493 dwp_file->sections.info.size = bfd_get_section_size (sectp);
13494 }
13495 else if (section_is_p (sectp->name, &names->line_dwo))
13496 {
13497 dwp_file->sections.line.s.section = sectp;
13498 dwp_file->sections.line.size = bfd_get_section_size (sectp);
13499 }
13500 else if (section_is_p (sectp->name, &names->loc_dwo))
13501 {
13502 dwp_file->sections.loc.s.section = sectp;
13503 dwp_file->sections.loc.size = bfd_get_section_size (sectp);
13504 }
13505 else if (section_is_p (sectp->name, &names->macinfo_dwo))
13506 {
13507 dwp_file->sections.macinfo.s.section = sectp;
13508 dwp_file->sections.macinfo.size = bfd_get_section_size (sectp);
13509 }
13510 else if (section_is_p (sectp->name, &names->macro_dwo))
13511 {
13512 dwp_file->sections.macro.s.section = sectp;
13513 dwp_file->sections.macro.size = bfd_get_section_size (sectp);
13514 }
13515 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13516 {
13517 dwp_file->sections.str_offsets.s.section = sectp;
13518 dwp_file->sections.str_offsets.size = bfd_get_section_size (sectp);
13519 }
13520 else if (section_is_p (sectp->name, &names->types_dwo))
13521 {
13522 dwp_file->sections.types.s.section = sectp;
13523 dwp_file->sections.types.size = bfd_get_section_size (sectp);
13524 }
13525 }
13526
13527 /* Hash function for dwp_file loaded CUs/TUs. */
13528
13529 static hashval_t
13530 hash_dwp_loaded_cutus (const void *item)
13531 {
13532 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
13533
13534 /* This drops the top 32 bits of the signature, but is ok for a hash. */
13535 return dwo_unit->signature;
13536 }
13537
13538 /* Equality function for dwp_file loaded CUs/TUs. */
13539
13540 static int
13541 eq_dwp_loaded_cutus (const void *a, const void *b)
13542 {
13543 const struct dwo_unit *dua = (const struct dwo_unit *) a;
13544 const struct dwo_unit *dub = (const struct dwo_unit *) b;
13545
13546 return dua->signature == dub->signature;
13547 }
13548
13549 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
13550
13551 static htab_t
13552 allocate_dwp_loaded_cutus_table (struct objfile *objfile)
13553 {
13554 return htab_create_alloc_ex (3,
13555 hash_dwp_loaded_cutus,
13556 eq_dwp_loaded_cutus,
13557 NULL,
13558 &objfile->objfile_obstack,
13559 hashtab_obstack_allocate,
13560 dummy_obstack_deallocate);
13561 }
13562
13563 /* Try to open DWP file FILE_NAME.
13564 The result is the bfd handle of the file.
13565 If there is a problem finding or opening the file, return NULL.
13566 Upon success, the canonicalized path of the file is stored in the bfd,
13567 same as symfile_bfd_open. */
13568
13569 static gdb_bfd_ref_ptr
13570 open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
13571 const char *file_name)
13572 {
13573 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
13574 1 /*is_dwp*/,
13575 1 /*search_cwd*/));
13576 if (abfd != NULL)
13577 return abfd;
13578
13579 /* Work around upstream bug 15652.
13580 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
13581 [Whether that's a "bug" is debatable, but it is getting in our way.]
13582 We have no real idea where the dwp file is, because gdb's realpath-ing
13583 of the executable's path may have discarded the needed info.
13584 [IWBN if the dwp file name was recorded in the executable, akin to
13585 .gnu_debuglink, but that doesn't exist yet.]
13586 Strip the directory from FILE_NAME and search again. */
13587 if (*debug_file_directory != '\0')
13588 {
13589 /* Don't implicitly search the current directory here.
13590 If the user wants to search "." to handle this case,
13591 it must be added to debug-file-directory. */
13592 return try_open_dwop_file (dwarf2_per_objfile,
13593 lbasename (file_name), 1 /*is_dwp*/,
13594 0 /*search_cwd*/);
13595 }
13596
13597 return NULL;
13598 }
13599
13600 /* Initialize the use of the DWP file for the current objfile.
13601 By convention the name of the DWP file is ${objfile}.dwp.
13602 The result is NULL if it can't be found. */
13603
13604 static struct dwp_file *
13605 open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
13606 {
13607 struct objfile *objfile = dwarf2_per_objfile->objfile;
13608 struct dwp_file *dwp_file;
13609
13610 /* Try to find first .dwp for the binary file before any symbolic links
13611 resolving. */
13612
13613 /* If the objfile is a debug file, find the name of the real binary
13614 file and get the name of dwp file from there. */
13615 std::string dwp_name;
13616 if (objfile->separate_debug_objfile_backlink != NULL)
13617 {
13618 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
13619 const char *backlink_basename = lbasename (backlink->original_name);
13620
13621 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
13622 }
13623 else
13624 dwp_name = objfile->original_name;
13625
13626 dwp_name += ".dwp";
13627
13628 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
13629 if (dbfd == NULL
13630 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
13631 {
13632 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
13633 dwp_name = objfile_name (objfile);
13634 dwp_name += ".dwp";
13635 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
13636 }
13637
13638 if (dbfd == NULL)
13639 {
13640 if (dwarf_read_debug)
13641 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
13642 return NULL;
13643 }
13644 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
13645 dwp_file->name = bfd_get_filename (dbfd.get ());
13646 dwp_file->dbfd = dbfd.release ();
13647
13648 /* +1: section 0 is unused */
13649 dwp_file->num_sections = bfd_count_sections (dwp_file->dbfd) + 1;
13650 dwp_file->elf_sections =
13651 OBSTACK_CALLOC (&objfile->objfile_obstack,
13652 dwp_file->num_sections, asection *);
13653
13654 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_common_dwp_sections,
13655 dwp_file);
13656
13657 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file, 0);
13658
13659 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file, 1);
13660
13661 /* The DWP file version is stored in the hash table. Oh well. */
13662 if (dwp_file->cus && dwp_file->tus
13663 && dwp_file->cus->version != dwp_file->tus->version)
13664 {
13665 /* Technically speaking, we should try to limp along, but this is
13666 pretty bizarre. We use pulongest here because that's the established
13667 portability solution (e.g, we cannot use %u for uint32_t). */
13668 error (_("Dwarf Error: DWP file CU version %s doesn't match"
13669 " TU version %s [in DWP file %s]"),
13670 pulongest (dwp_file->cus->version),
13671 pulongest (dwp_file->tus->version), dwp_name.c_str ());
13672 }
13673
13674 if (dwp_file->cus)
13675 dwp_file->version = dwp_file->cus->version;
13676 else if (dwp_file->tus)
13677 dwp_file->version = dwp_file->tus->version;
13678 else
13679 dwp_file->version = 2;
13680
13681 if (dwp_file->version == 2)
13682 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_v2_dwp_sections,
13683 dwp_file);
13684
13685 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
13686 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
13687
13688 if (dwarf_read_debug)
13689 {
13690 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
13691 fprintf_unfiltered (gdb_stdlog,
13692 " %s CUs, %s TUs\n",
13693 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
13694 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
13695 }
13696
13697 return dwp_file;
13698 }
13699
13700 /* Wrapper around open_and_init_dwp_file, only open it once. */
13701
13702 static struct dwp_file *
13703 get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
13704 {
13705 if (! dwarf2_per_objfile->dwp_checked)
13706 {
13707 dwarf2_per_objfile->dwp_file
13708 = open_and_init_dwp_file (dwarf2_per_objfile);
13709 dwarf2_per_objfile->dwp_checked = 1;
13710 }
13711 return dwarf2_per_objfile->dwp_file;
13712 }
13713
13714 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
13715 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
13716 or in the DWP file for the objfile, referenced by THIS_UNIT.
13717 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
13718 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
13719
13720 This is called, for example, when wanting to read a variable with a
13721 complex location. Therefore we don't want to do file i/o for every call.
13722 Therefore we don't want to look for a DWO file on every call.
13723 Therefore we first see if we've already seen SIGNATURE in a DWP file,
13724 then we check if we've already seen DWO_NAME, and only THEN do we check
13725 for a DWO file.
13726
13727 The result is a pointer to the dwo_unit object or NULL if we didn't find it
13728 (dwo_id mismatch or couldn't find the DWO/DWP file). */
13729
13730 static struct dwo_unit *
13731 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
13732 const char *dwo_name, const char *comp_dir,
13733 ULONGEST signature, int is_debug_types)
13734 {
13735 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
13736 struct objfile *objfile = dwarf2_per_objfile->objfile;
13737 const char *kind = is_debug_types ? "TU" : "CU";
13738 void **dwo_file_slot;
13739 struct dwo_file *dwo_file;
13740 struct dwp_file *dwp_file;
13741
13742 /* First see if there's a DWP file.
13743 If we have a DWP file but didn't find the DWO inside it, don't
13744 look for the original DWO file. It makes gdb behave differently
13745 depending on whether one is debugging in the build tree. */
13746
13747 dwp_file = get_dwp_file (dwarf2_per_objfile);
13748 if (dwp_file != NULL)
13749 {
13750 const struct dwp_hash_table *dwp_htab =
13751 is_debug_types ? dwp_file->tus : dwp_file->cus;
13752
13753 if (dwp_htab != NULL)
13754 {
13755 struct dwo_unit *dwo_cutu =
13756 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
13757 signature, is_debug_types);
13758
13759 if (dwo_cutu != NULL)
13760 {
13761 if (dwarf_read_debug)
13762 {
13763 fprintf_unfiltered (gdb_stdlog,
13764 "Virtual DWO %s %s found: @%s\n",
13765 kind, hex_string (signature),
13766 host_address_to_string (dwo_cutu));
13767 }
13768 return dwo_cutu;
13769 }
13770 }
13771 }
13772 else
13773 {
13774 /* No DWP file, look for the DWO file. */
13775
13776 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
13777 dwo_name, comp_dir);
13778 if (*dwo_file_slot == NULL)
13779 {
13780 /* Read in the file and build a table of the CUs/TUs it contains. */
13781 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
13782 }
13783 /* NOTE: This will be NULL if unable to open the file. */
13784 dwo_file = (struct dwo_file *) *dwo_file_slot;
13785
13786 if (dwo_file != NULL)
13787 {
13788 struct dwo_unit *dwo_cutu = NULL;
13789
13790 if (is_debug_types && dwo_file->tus)
13791 {
13792 struct dwo_unit find_dwo_cutu;
13793
13794 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13795 find_dwo_cutu.signature = signature;
13796 dwo_cutu
13797 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
13798 }
13799 else if (!is_debug_types && dwo_file->cus)
13800 {
13801 struct dwo_unit find_dwo_cutu;
13802
13803 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13804 find_dwo_cutu.signature = signature;
13805 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus,
13806 &find_dwo_cutu);
13807 }
13808
13809 if (dwo_cutu != NULL)
13810 {
13811 if (dwarf_read_debug)
13812 {
13813 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
13814 kind, dwo_name, hex_string (signature),
13815 host_address_to_string (dwo_cutu));
13816 }
13817 return dwo_cutu;
13818 }
13819 }
13820 }
13821
13822 /* We didn't find it. This could mean a dwo_id mismatch, or
13823 someone deleted the DWO/DWP file, or the search path isn't set up
13824 correctly to find the file. */
13825
13826 if (dwarf_read_debug)
13827 {
13828 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
13829 kind, dwo_name, hex_string (signature));
13830 }
13831
13832 /* This is a warning and not a complaint because it can be caused by
13833 pilot error (e.g., user accidentally deleting the DWO). */
13834 {
13835 /* Print the name of the DWP file if we looked there, helps the user
13836 better diagnose the problem. */
13837 std::string dwp_text;
13838
13839 if (dwp_file != NULL)
13840 dwp_text = string_printf (" [in DWP file %s]",
13841 lbasename (dwp_file->name));
13842
13843 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
13844 " [in module %s]"),
13845 kind, dwo_name, hex_string (signature),
13846 dwp_text.c_str (),
13847 this_unit->is_debug_types ? "TU" : "CU",
13848 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
13849 }
13850 return NULL;
13851 }
13852
13853 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13854 See lookup_dwo_cutu_unit for details. */
13855
13856 static struct dwo_unit *
13857 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
13858 const char *dwo_name, const char *comp_dir,
13859 ULONGEST signature)
13860 {
13861 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
13862 }
13863
13864 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13865 See lookup_dwo_cutu_unit for details. */
13866
13867 static struct dwo_unit *
13868 lookup_dwo_type_unit (struct signatured_type *this_tu,
13869 const char *dwo_name, const char *comp_dir)
13870 {
13871 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
13872 }
13873
13874 /* Traversal function for queue_and_load_all_dwo_tus. */
13875
13876 static int
13877 queue_and_load_dwo_tu (void **slot, void *info)
13878 {
13879 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
13880 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
13881 ULONGEST signature = dwo_unit->signature;
13882 struct signatured_type *sig_type =
13883 lookup_dwo_signatured_type (per_cu->cu, signature);
13884
13885 if (sig_type != NULL)
13886 {
13887 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13888
13889 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13890 a real dependency of PER_CU on SIG_TYPE. That is detected later
13891 while processing PER_CU. */
13892 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
13893 load_full_type_unit (sig_cu);
13894 VEC_safe_push (dwarf2_per_cu_ptr, per_cu->imported_symtabs, sig_cu);
13895 }
13896
13897 return 1;
13898 }
13899
13900 /* Queue all TUs contained in the DWO of PER_CU to be read in.
13901 The DWO may have the only definition of the type, though it may not be
13902 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13903 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13904
13905 static void
13906 queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
13907 {
13908 struct dwo_unit *dwo_unit;
13909 struct dwo_file *dwo_file;
13910
13911 gdb_assert (!per_cu->is_debug_types);
13912 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
13913 gdb_assert (per_cu->cu != NULL);
13914
13915 dwo_unit = per_cu->cu->dwo_unit;
13916 gdb_assert (dwo_unit != NULL);
13917
13918 dwo_file = dwo_unit->dwo_file;
13919 if (dwo_file->tus != NULL)
13920 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
13921 }
13922
13923 /* Free all resources associated with DWO_FILE.
13924 Close the DWO file and munmap the sections.
13925 All memory should be on the objfile obstack. */
13926
13927 static void
13928 free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
13929 {
13930
13931 /* Note: dbfd is NULL for virtual DWO files. */
13932 gdb_bfd_unref (dwo_file->dbfd);
13933
13934 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
13935 }
13936
13937 /* Wrapper for free_dwo_file for use in cleanups. */
13938
13939 static void
13940 free_dwo_file_cleanup (void *arg)
13941 {
13942 struct free_dwo_file_cleanup_data *data
13943 = (struct free_dwo_file_cleanup_data *) arg;
13944 struct objfile *objfile = data->dwarf2_per_objfile->objfile;
13945
13946 free_dwo_file (data->dwo_file, objfile);
13947
13948 xfree (data);
13949 }
13950
13951 /* Traversal function for free_dwo_files. */
13952
13953 static int
13954 free_dwo_file_from_slot (void **slot, void *info)
13955 {
13956 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
13957 struct objfile *objfile = (struct objfile *) info;
13958
13959 free_dwo_file (dwo_file, objfile);
13960
13961 return 1;
13962 }
13963
13964 /* Free all resources associated with DWO_FILES. */
13965
13966 static void
13967 free_dwo_files (htab_t dwo_files, struct objfile *objfile)
13968 {
13969 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
13970 }
13971 \f
13972 /* Read in various DIEs. */
13973
13974 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
13975 Inherit only the children of the DW_AT_abstract_origin DIE not being
13976 already referenced by DW_AT_abstract_origin from the children of the
13977 current DIE. */
13978
13979 static void
13980 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13981 {
13982 struct die_info *child_die;
13983 sect_offset *offsetp;
13984 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13985 struct die_info *origin_die;
13986 /* Iterator of the ORIGIN_DIE children. */
13987 struct die_info *origin_child_die;
13988 struct attribute *attr;
13989 struct dwarf2_cu *origin_cu;
13990 struct pending **origin_previous_list_in_scope;
13991
13992 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13993 if (!attr)
13994 return;
13995
13996 /* Note that following die references may follow to a die in a
13997 different cu. */
13998
13999 origin_cu = cu;
14000 origin_die = follow_die_ref (die, attr, &origin_cu);
14001
14002 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
14003 symbols in. */
14004 origin_previous_list_in_scope = origin_cu->list_in_scope;
14005 origin_cu->list_in_scope = cu->list_in_scope;
14006
14007 if (die->tag != origin_die->tag
14008 && !(die->tag == DW_TAG_inlined_subroutine
14009 && origin_die->tag == DW_TAG_subprogram))
14010 complaint (&symfile_complaints,
14011 _("DIE %s and its abstract origin %s have different tags"),
14012 sect_offset_str (die->sect_off),
14013 sect_offset_str (origin_die->sect_off));
14014
14015 std::vector<sect_offset> offsets;
14016
14017 for (child_die = die->child;
14018 child_die && child_die->tag;
14019 child_die = sibling_die (child_die))
14020 {
14021 struct die_info *child_origin_die;
14022 struct dwarf2_cu *child_origin_cu;
14023
14024 /* We are trying to process concrete instance entries:
14025 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
14026 it's not relevant to our analysis here. i.e. detecting DIEs that are
14027 present in the abstract instance but not referenced in the concrete
14028 one. */
14029 if (child_die->tag == DW_TAG_call_site
14030 || child_die->tag == DW_TAG_GNU_call_site)
14031 continue;
14032
14033 /* For each CHILD_DIE, find the corresponding child of
14034 ORIGIN_DIE. If there is more than one layer of
14035 DW_AT_abstract_origin, follow them all; there shouldn't be,
14036 but GCC versions at least through 4.4 generate this (GCC PR
14037 40573). */
14038 child_origin_die = child_die;
14039 child_origin_cu = cu;
14040 while (1)
14041 {
14042 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
14043 child_origin_cu);
14044 if (attr == NULL)
14045 break;
14046 child_origin_die = follow_die_ref (child_origin_die, attr,
14047 &child_origin_cu);
14048 }
14049
14050 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
14051 counterpart may exist. */
14052 if (child_origin_die != child_die)
14053 {
14054 if (child_die->tag != child_origin_die->tag
14055 && !(child_die->tag == DW_TAG_inlined_subroutine
14056 && child_origin_die->tag == DW_TAG_subprogram))
14057 complaint (&symfile_complaints,
14058 _("Child DIE %s and its abstract origin %s have "
14059 "different tags"),
14060 sect_offset_str (child_die->sect_off),
14061 sect_offset_str (child_origin_die->sect_off));
14062 if (child_origin_die->parent != origin_die)
14063 complaint (&symfile_complaints,
14064 _("Child DIE %s and its abstract origin %s have "
14065 "different parents"),
14066 sect_offset_str (child_die->sect_off),
14067 sect_offset_str (child_origin_die->sect_off));
14068 else
14069 offsets.push_back (child_origin_die->sect_off);
14070 }
14071 }
14072 std::sort (offsets.begin (), offsets.end ());
14073 sect_offset *offsets_end = offsets.data () + offsets.size ();
14074 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
14075 if (offsetp[-1] == *offsetp)
14076 complaint (&symfile_complaints,
14077 _("Multiple children of DIE %s refer "
14078 "to DIE %s as their abstract origin"),
14079 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
14080
14081 offsetp = offsets.data ();
14082 origin_child_die = origin_die->child;
14083 while (origin_child_die && origin_child_die->tag)
14084 {
14085 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
14086 while (offsetp < offsets_end
14087 && *offsetp < origin_child_die->sect_off)
14088 offsetp++;
14089 if (offsetp >= offsets_end
14090 || *offsetp > origin_child_die->sect_off)
14091 {
14092 /* Found that ORIGIN_CHILD_DIE is really not referenced.
14093 Check whether we're already processing ORIGIN_CHILD_DIE.
14094 This can happen with mutually referenced abstract_origins.
14095 PR 16581. */
14096 if (!origin_child_die->in_process)
14097 process_die (origin_child_die, origin_cu);
14098 }
14099 origin_child_die = sibling_die (origin_child_die);
14100 }
14101 origin_cu->list_in_scope = origin_previous_list_in_scope;
14102 }
14103
14104 static void
14105 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
14106 {
14107 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14108 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14109 struct context_stack *newobj;
14110 CORE_ADDR lowpc;
14111 CORE_ADDR highpc;
14112 struct die_info *child_die;
14113 struct attribute *attr, *call_line, *call_file;
14114 const char *name;
14115 CORE_ADDR baseaddr;
14116 struct block *block;
14117 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
14118 std::vector<struct symbol *> template_args;
14119 struct template_symbol *templ_func = NULL;
14120
14121 if (inlined_func)
14122 {
14123 /* If we do not have call site information, we can't show the
14124 caller of this inlined function. That's too confusing, so
14125 only use the scope for local variables. */
14126 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
14127 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
14128 if (call_line == NULL || call_file == NULL)
14129 {
14130 read_lexical_block_scope (die, cu);
14131 return;
14132 }
14133 }
14134
14135 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14136
14137 name = dwarf2_name (die, cu);
14138
14139 /* Ignore functions with missing or empty names. These are actually
14140 illegal according to the DWARF standard. */
14141 if (name == NULL)
14142 {
14143 complaint (&symfile_complaints,
14144 _("missing name for subprogram DIE at %s"),
14145 sect_offset_str (die->sect_off));
14146 return;
14147 }
14148
14149 /* Ignore functions with missing or invalid low and high pc attributes. */
14150 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
14151 <= PC_BOUNDS_INVALID)
14152 {
14153 attr = dwarf2_attr (die, DW_AT_external, cu);
14154 if (!attr || !DW_UNSND (attr))
14155 complaint (&symfile_complaints,
14156 _("cannot get low and high bounds "
14157 "for subprogram DIE at %s"),
14158 sect_offset_str (die->sect_off));
14159 return;
14160 }
14161
14162 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14163 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
14164
14165 /* If we have any template arguments, then we must allocate a
14166 different sort of symbol. */
14167 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
14168 {
14169 if (child_die->tag == DW_TAG_template_type_param
14170 || child_die->tag == DW_TAG_template_value_param)
14171 {
14172 templ_func = allocate_template_symbol (objfile);
14173 templ_func->subclass = SYMBOL_TEMPLATE;
14174 break;
14175 }
14176 }
14177
14178 newobj = push_context (0, lowpc);
14179 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
14180 (struct symbol *) templ_func);
14181
14182 /* If there is a location expression for DW_AT_frame_base, record
14183 it. */
14184 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
14185 if (attr)
14186 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
14187
14188 /* If there is a location for the static link, record it. */
14189 newobj->static_link = NULL;
14190 attr = dwarf2_attr (die, DW_AT_static_link, cu);
14191 if (attr)
14192 {
14193 newobj->static_link
14194 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
14195 attr_to_dynamic_prop (attr, die, cu, newobj->static_link);
14196 }
14197
14198 cu->list_in_scope = &local_symbols;
14199
14200 if (die->child != NULL)
14201 {
14202 child_die = die->child;
14203 while (child_die && child_die->tag)
14204 {
14205 if (child_die->tag == DW_TAG_template_type_param
14206 || child_die->tag == DW_TAG_template_value_param)
14207 {
14208 struct symbol *arg = new_symbol (child_die, NULL, cu);
14209
14210 if (arg != NULL)
14211 template_args.push_back (arg);
14212 }
14213 else
14214 process_die (child_die, cu);
14215 child_die = sibling_die (child_die);
14216 }
14217 }
14218
14219 inherit_abstract_dies (die, cu);
14220
14221 /* If we have a DW_AT_specification, we might need to import using
14222 directives from the context of the specification DIE. See the
14223 comment in determine_prefix. */
14224 if (cu->language == language_cplus
14225 && dwarf2_attr (die, DW_AT_specification, cu))
14226 {
14227 struct dwarf2_cu *spec_cu = cu;
14228 struct die_info *spec_die = die_specification (die, &spec_cu);
14229
14230 while (spec_die)
14231 {
14232 child_die = spec_die->child;
14233 while (child_die && child_die->tag)
14234 {
14235 if (child_die->tag == DW_TAG_imported_module)
14236 process_die (child_die, spec_cu);
14237 child_die = sibling_die (child_die);
14238 }
14239
14240 /* In some cases, GCC generates specification DIEs that
14241 themselves contain DW_AT_specification attributes. */
14242 spec_die = die_specification (spec_die, &spec_cu);
14243 }
14244 }
14245
14246 newobj = pop_context ();
14247 /* Make a block for the local symbols within. */
14248 block = finish_block (newobj->name, &local_symbols, newobj->old_blocks,
14249 newobj->static_link, lowpc, highpc);
14250
14251 /* For C++, set the block's scope. */
14252 if ((cu->language == language_cplus
14253 || cu->language == language_fortran
14254 || cu->language == language_d
14255 || cu->language == language_rust)
14256 && cu->processing_has_namespace_info)
14257 block_set_scope (block, determine_prefix (die, cu),
14258 &objfile->objfile_obstack);
14259
14260 /* If we have address ranges, record them. */
14261 dwarf2_record_block_ranges (die, block, baseaddr, cu);
14262
14263 gdbarch_make_symbol_special (gdbarch, newobj->name, objfile);
14264
14265 /* Attach template arguments to function. */
14266 if (!template_args.empty ())
14267 {
14268 gdb_assert (templ_func != NULL);
14269
14270 templ_func->n_template_arguments = template_args.size ();
14271 templ_func->template_arguments
14272 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
14273 templ_func->n_template_arguments);
14274 memcpy (templ_func->template_arguments,
14275 template_args.data (),
14276 (templ_func->n_template_arguments * sizeof (struct symbol *)));
14277 }
14278
14279 /* In C++, we can have functions nested inside functions (e.g., when
14280 a function declares a class that has methods). This means that
14281 when we finish processing a function scope, we may need to go
14282 back to building a containing block's symbol lists. */
14283 local_symbols = newobj->locals;
14284 local_using_directives = newobj->local_using_directives;
14285
14286 /* If we've finished processing a top-level function, subsequent
14287 symbols go in the file symbol list. */
14288 if (outermost_context_p ())
14289 cu->list_in_scope = &file_symbols;
14290 }
14291
14292 /* Process all the DIES contained within a lexical block scope. Start
14293 a new scope, process the dies, and then close the scope. */
14294
14295 static void
14296 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
14297 {
14298 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14299 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14300 struct context_stack *newobj;
14301 CORE_ADDR lowpc, highpc;
14302 struct die_info *child_die;
14303 CORE_ADDR baseaddr;
14304
14305 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14306
14307 /* Ignore blocks with missing or invalid low and high pc attributes. */
14308 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
14309 as multiple lexical blocks? Handling children in a sane way would
14310 be nasty. Might be easier to properly extend generic blocks to
14311 describe ranges. */
14312 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
14313 {
14314 case PC_BOUNDS_NOT_PRESENT:
14315 /* DW_TAG_lexical_block has no attributes, process its children as if
14316 there was no wrapping by that DW_TAG_lexical_block.
14317 GCC does no longer produces such DWARF since GCC r224161. */
14318 for (child_die = die->child;
14319 child_die != NULL && child_die->tag;
14320 child_die = sibling_die (child_die))
14321 process_die (child_die, cu);
14322 return;
14323 case PC_BOUNDS_INVALID:
14324 return;
14325 }
14326 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14327 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
14328
14329 push_context (0, lowpc);
14330 if (die->child != NULL)
14331 {
14332 child_die = die->child;
14333 while (child_die && child_die->tag)
14334 {
14335 process_die (child_die, cu);
14336 child_die = sibling_die (child_die);
14337 }
14338 }
14339 inherit_abstract_dies (die, cu);
14340 newobj = pop_context ();
14341
14342 if (local_symbols != NULL || local_using_directives != NULL)
14343 {
14344 struct block *block
14345 = finish_block (0, &local_symbols, newobj->old_blocks, NULL,
14346 newobj->start_addr, highpc);
14347
14348 /* Note that recording ranges after traversing children, as we
14349 do here, means that recording a parent's ranges entails
14350 walking across all its children's ranges as they appear in
14351 the address map, which is quadratic behavior.
14352
14353 It would be nicer to record the parent's ranges before
14354 traversing its children, simply overriding whatever you find
14355 there. But since we don't even decide whether to create a
14356 block until after we've traversed its children, that's hard
14357 to do. */
14358 dwarf2_record_block_ranges (die, block, baseaddr, cu);
14359 }
14360 local_symbols = newobj->locals;
14361 local_using_directives = newobj->local_using_directives;
14362 }
14363
14364 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
14365
14366 static void
14367 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
14368 {
14369 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14370 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14371 CORE_ADDR pc, baseaddr;
14372 struct attribute *attr;
14373 struct call_site *call_site, call_site_local;
14374 void **slot;
14375 int nparams;
14376 struct die_info *child_die;
14377
14378 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14379
14380 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
14381 if (attr == NULL)
14382 {
14383 /* This was a pre-DWARF-5 GNU extension alias
14384 for DW_AT_call_return_pc. */
14385 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14386 }
14387 if (!attr)
14388 {
14389 complaint (&symfile_complaints,
14390 _("missing DW_AT_call_return_pc for DW_TAG_call_site "
14391 "DIE %s [in module %s]"),
14392 sect_offset_str (die->sect_off), objfile_name (objfile));
14393 return;
14394 }
14395 pc = attr_value_as_address (attr) + baseaddr;
14396 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
14397
14398 if (cu->call_site_htab == NULL)
14399 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
14400 NULL, &objfile->objfile_obstack,
14401 hashtab_obstack_allocate, NULL);
14402 call_site_local.pc = pc;
14403 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
14404 if (*slot != NULL)
14405 {
14406 complaint (&symfile_complaints,
14407 _("Duplicate PC %s for DW_TAG_call_site "
14408 "DIE %s [in module %s]"),
14409 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
14410 objfile_name (objfile));
14411 return;
14412 }
14413
14414 /* Count parameters at the caller. */
14415
14416 nparams = 0;
14417 for (child_die = die->child; child_die && child_die->tag;
14418 child_die = sibling_die (child_die))
14419 {
14420 if (child_die->tag != DW_TAG_call_site_parameter
14421 && child_die->tag != DW_TAG_GNU_call_site_parameter)
14422 {
14423 complaint (&symfile_complaints,
14424 _("Tag %d is not DW_TAG_call_site_parameter in "
14425 "DW_TAG_call_site child DIE %s [in module %s]"),
14426 child_die->tag, sect_offset_str (child_die->sect_off),
14427 objfile_name (objfile));
14428 continue;
14429 }
14430
14431 nparams++;
14432 }
14433
14434 call_site
14435 = ((struct call_site *)
14436 obstack_alloc (&objfile->objfile_obstack,
14437 sizeof (*call_site)
14438 + (sizeof (*call_site->parameter) * (nparams - 1))));
14439 *slot = call_site;
14440 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
14441 call_site->pc = pc;
14442
14443 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
14444 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
14445 {
14446 struct die_info *func_die;
14447
14448 /* Skip also over DW_TAG_inlined_subroutine. */
14449 for (func_die = die->parent;
14450 func_die && func_die->tag != DW_TAG_subprogram
14451 && func_die->tag != DW_TAG_subroutine_type;
14452 func_die = func_die->parent);
14453
14454 /* DW_AT_call_all_calls is a superset
14455 of DW_AT_call_all_tail_calls. */
14456 if (func_die
14457 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
14458 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
14459 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
14460 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
14461 {
14462 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
14463 not complete. But keep CALL_SITE for look ups via call_site_htab,
14464 both the initial caller containing the real return address PC and
14465 the final callee containing the current PC of a chain of tail
14466 calls do not need to have the tail call list complete. But any
14467 function candidate for a virtual tail call frame searched via
14468 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
14469 determined unambiguously. */
14470 }
14471 else
14472 {
14473 struct type *func_type = NULL;
14474
14475 if (func_die)
14476 func_type = get_die_type (func_die, cu);
14477 if (func_type != NULL)
14478 {
14479 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
14480
14481 /* Enlist this call site to the function. */
14482 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
14483 TYPE_TAIL_CALL_LIST (func_type) = call_site;
14484 }
14485 else
14486 complaint (&symfile_complaints,
14487 _("Cannot find function owning DW_TAG_call_site "
14488 "DIE %s [in module %s]"),
14489 sect_offset_str (die->sect_off), objfile_name (objfile));
14490 }
14491 }
14492
14493 attr = dwarf2_attr (die, DW_AT_call_target, cu);
14494 if (attr == NULL)
14495 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
14496 if (attr == NULL)
14497 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
14498 if (attr == NULL)
14499 {
14500 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
14501 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14502 }
14503 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
14504 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
14505 /* Keep NULL DWARF_BLOCK. */;
14506 else if (attr_form_is_block (attr))
14507 {
14508 struct dwarf2_locexpr_baton *dlbaton;
14509
14510 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
14511 dlbaton->data = DW_BLOCK (attr)->data;
14512 dlbaton->size = DW_BLOCK (attr)->size;
14513 dlbaton->per_cu = cu->per_cu;
14514
14515 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
14516 }
14517 else if (attr_form_is_ref (attr))
14518 {
14519 struct dwarf2_cu *target_cu = cu;
14520 struct die_info *target_die;
14521
14522 target_die = follow_die_ref (die, attr, &target_cu);
14523 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
14524 if (die_is_declaration (target_die, target_cu))
14525 {
14526 const char *target_physname;
14527
14528 /* Prefer the mangled name; otherwise compute the demangled one. */
14529 target_physname = dw2_linkage_name (target_die, target_cu);
14530 if (target_physname == NULL)
14531 target_physname = dwarf2_physname (NULL, target_die, target_cu);
14532 if (target_physname == NULL)
14533 complaint (&symfile_complaints,
14534 _("DW_AT_call_target target DIE has invalid "
14535 "physname, for referencing DIE %s [in module %s]"),
14536 sect_offset_str (die->sect_off), objfile_name (objfile));
14537 else
14538 SET_FIELD_PHYSNAME (call_site->target, target_physname);
14539 }
14540 else
14541 {
14542 CORE_ADDR lowpc;
14543
14544 /* DW_AT_entry_pc should be preferred. */
14545 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
14546 <= PC_BOUNDS_INVALID)
14547 complaint (&symfile_complaints,
14548 _("DW_AT_call_target target DIE has invalid "
14549 "low pc, for referencing DIE %s [in module %s]"),
14550 sect_offset_str (die->sect_off), objfile_name (objfile));
14551 else
14552 {
14553 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14554 SET_FIELD_PHYSADDR (call_site->target, lowpc);
14555 }
14556 }
14557 }
14558 else
14559 complaint (&symfile_complaints,
14560 _("DW_TAG_call_site DW_AT_call_target is neither "
14561 "block nor reference, for DIE %s [in module %s]"),
14562 sect_offset_str (die->sect_off), objfile_name (objfile));
14563
14564 call_site->per_cu = cu->per_cu;
14565
14566 for (child_die = die->child;
14567 child_die && child_die->tag;
14568 child_die = sibling_die (child_die))
14569 {
14570 struct call_site_parameter *parameter;
14571 struct attribute *loc, *origin;
14572
14573 if (child_die->tag != DW_TAG_call_site_parameter
14574 && child_die->tag != DW_TAG_GNU_call_site_parameter)
14575 {
14576 /* Already printed the complaint above. */
14577 continue;
14578 }
14579
14580 gdb_assert (call_site->parameter_count < nparams);
14581 parameter = &call_site->parameter[call_site->parameter_count];
14582
14583 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
14584 specifies DW_TAG_formal_parameter. Value of the data assumed for the
14585 register is contained in DW_AT_call_value. */
14586
14587 loc = dwarf2_attr (child_die, DW_AT_location, cu);
14588 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
14589 if (origin == NULL)
14590 {
14591 /* This was a pre-DWARF-5 GNU extension alias
14592 for DW_AT_call_parameter. */
14593 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
14594 }
14595 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
14596 {
14597 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
14598
14599 sect_offset sect_off
14600 = (sect_offset) dwarf2_get_ref_die_offset (origin);
14601 if (!offset_in_cu_p (&cu->header, sect_off))
14602 {
14603 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
14604 binding can be done only inside one CU. Such referenced DIE
14605 therefore cannot be even moved to DW_TAG_partial_unit. */
14606 complaint (&symfile_complaints,
14607 _("DW_AT_call_parameter offset is not in CU for "
14608 "DW_TAG_call_site child DIE %s [in module %s]"),
14609 sect_offset_str (child_die->sect_off),
14610 objfile_name (objfile));
14611 continue;
14612 }
14613 parameter->u.param_cu_off
14614 = (cu_offset) (sect_off - cu->header.sect_off);
14615 }
14616 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
14617 {
14618 complaint (&symfile_complaints,
14619 _("No DW_FORM_block* DW_AT_location for "
14620 "DW_TAG_call_site child DIE %s [in module %s]"),
14621 sect_offset_str (child_die->sect_off), objfile_name (objfile));
14622 continue;
14623 }
14624 else
14625 {
14626 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
14627 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
14628 if (parameter->u.dwarf_reg != -1)
14629 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
14630 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
14631 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
14632 &parameter->u.fb_offset))
14633 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
14634 else
14635 {
14636 complaint (&symfile_complaints,
14637 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
14638 "for DW_FORM_block* DW_AT_location is supported for "
14639 "DW_TAG_call_site child DIE %s "
14640 "[in module %s]"),
14641 sect_offset_str (child_die->sect_off),
14642 objfile_name (objfile));
14643 continue;
14644 }
14645 }
14646
14647 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
14648 if (attr == NULL)
14649 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
14650 if (!attr_form_is_block (attr))
14651 {
14652 complaint (&symfile_complaints,
14653 _("No DW_FORM_block* DW_AT_call_value for "
14654 "DW_TAG_call_site child DIE %s [in module %s]"),
14655 sect_offset_str (child_die->sect_off),
14656 objfile_name (objfile));
14657 continue;
14658 }
14659 parameter->value = DW_BLOCK (attr)->data;
14660 parameter->value_size = DW_BLOCK (attr)->size;
14661
14662 /* Parameters are not pre-cleared by memset above. */
14663 parameter->data_value = NULL;
14664 parameter->data_value_size = 0;
14665 call_site->parameter_count++;
14666
14667 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
14668 if (attr == NULL)
14669 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
14670 if (attr)
14671 {
14672 if (!attr_form_is_block (attr))
14673 complaint (&symfile_complaints,
14674 _("No DW_FORM_block* DW_AT_call_data_value for "
14675 "DW_TAG_call_site child DIE %s [in module %s]"),
14676 sect_offset_str (child_die->sect_off),
14677 objfile_name (objfile));
14678 else
14679 {
14680 parameter->data_value = DW_BLOCK (attr)->data;
14681 parameter->data_value_size = DW_BLOCK (attr)->size;
14682 }
14683 }
14684 }
14685 }
14686
14687 /* Helper function for read_variable. If DIE represents a virtual
14688 table, then return the type of the concrete object that is
14689 associated with the virtual table. Otherwise, return NULL. */
14690
14691 static struct type *
14692 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
14693 {
14694 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
14695 if (attr == NULL)
14696 return NULL;
14697
14698 /* Find the type DIE. */
14699 struct die_info *type_die = NULL;
14700 struct dwarf2_cu *type_cu = cu;
14701
14702 if (attr_form_is_ref (attr))
14703 type_die = follow_die_ref (die, attr, &type_cu);
14704 if (type_die == NULL)
14705 return NULL;
14706
14707 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
14708 return NULL;
14709 return die_containing_type (type_die, type_cu);
14710 }
14711
14712 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
14713
14714 static void
14715 read_variable (struct die_info *die, struct dwarf2_cu *cu)
14716 {
14717 struct rust_vtable_symbol *storage = NULL;
14718
14719 if (cu->language == language_rust)
14720 {
14721 struct type *containing_type = rust_containing_type (die, cu);
14722
14723 if (containing_type != NULL)
14724 {
14725 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14726
14727 storage = OBSTACK_ZALLOC (&objfile->objfile_obstack,
14728 struct rust_vtable_symbol);
14729 initialize_objfile_symbol (storage);
14730 storage->concrete_type = containing_type;
14731 storage->subclass = SYMBOL_RUST_VTABLE;
14732 }
14733 }
14734
14735 new_symbol (die, NULL, cu, storage);
14736 }
14737
14738 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
14739 reading .debug_rnglists.
14740 Callback's type should be:
14741 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14742 Return true if the attributes are present and valid, otherwise,
14743 return false. */
14744
14745 template <typename Callback>
14746 static bool
14747 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
14748 Callback &&callback)
14749 {
14750 struct dwarf2_per_objfile *dwarf2_per_objfile
14751 = cu->per_cu->dwarf2_per_objfile;
14752 struct objfile *objfile = dwarf2_per_objfile->objfile;
14753 bfd *obfd = objfile->obfd;
14754 /* Base address selection entry. */
14755 CORE_ADDR base;
14756 int found_base;
14757 const gdb_byte *buffer;
14758 CORE_ADDR baseaddr;
14759 bool overflow = false;
14760
14761 found_base = cu->base_known;
14762 base = cu->base_address;
14763
14764 dwarf2_read_section (objfile, &dwarf2_per_objfile->rnglists);
14765 if (offset >= dwarf2_per_objfile->rnglists.size)
14766 {
14767 complaint (&symfile_complaints,
14768 _("Offset %d out of bounds for DW_AT_ranges attribute"),
14769 offset);
14770 return false;
14771 }
14772 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
14773
14774 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14775
14776 while (1)
14777 {
14778 /* Initialize it due to a false compiler warning. */
14779 CORE_ADDR range_beginning = 0, range_end = 0;
14780 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
14781 + dwarf2_per_objfile->rnglists.size);
14782 unsigned int bytes_read;
14783
14784 if (buffer == buf_end)
14785 {
14786 overflow = true;
14787 break;
14788 }
14789 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
14790 switch (rlet)
14791 {
14792 case DW_RLE_end_of_list:
14793 break;
14794 case DW_RLE_base_address:
14795 if (buffer + cu->header.addr_size > buf_end)
14796 {
14797 overflow = true;
14798 break;
14799 }
14800 base = read_address (obfd, buffer, cu, &bytes_read);
14801 found_base = 1;
14802 buffer += bytes_read;
14803 break;
14804 case DW_RLE_start_length:
14805 if (buffer + cu->header.addr_size > buf_end)
14806 {
14807 overflow = true;
14808 break;
14809 }
14810 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14811 buffer += bytes_read;
14812 range_end = (range_beginning
14813 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14814 buffer += bytes_read;
14815 if (buffer > buf_end)
14816 {
14817 overflow = true;
14818 break;
14819 }
14820 break;
14821 case DW_RLE_offset_pair:
14822 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14823 buffer += bytes_read;
14824 if (buffer > buf_end)
14825 {
14826 overflow = true;
14827 break;
14828 }
14829 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14830 buffer += bytes_read;
14831 if (buffer > buf_end)
14832 {
14833 overflow = true;
14834 break;
14835 }
14836 break;
14837 case DW_RLE_start_end:
14838 if (buffer + 2 * cu->header.addr_size > buf_end)
14839 {
14840 overflow = true;
14841 break;
14842 }
14843 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14844 buffer += bytes_read;
14845 range_end = read_address (obfd, buffer, cu, &bytes_read);
14846 buffer += bytes_read;
14847 break;
14848 default:
14849 complaint (&symfile_complaints,
14850 _("Invalid .debug_rnglists data (no base address)"));
14851 return false;
14852 }
14853 if (rlet == DW_RLE_end_of_list || overflow)
14854 break;
14855 if (rlet == DW_RLE_base_address)
14856 continue;
14857
14858 if (!found_base)
14859 {
14860 /* We have no valid base address for the ranges
14861 data. */
14862 complaint (&symfile_complaints,
14863 _("Invalid .debug_rnglists data (no base address)"));
14864 return false;
14865 }
14866
14867 if (range_beginning > range_end)
14868 {
14869 /* Inverted range entries are invalid. */
14870 complaint (&symfile_complaints,
14871 _("Invalid .debug_rnglists data (inverted range)"));
14872 return false;
14873 }
14874
14875 /* Empty range entries have no effect. */
14876 if (range_beginning == range_end)
14877 continue;
14878
14879 range_beginning += base;
14880 range_end += base;
14881
14882 /* A not-uncommon case of bad debug info.
14883 Don't pollute the addrmap with bad data. */
14884 if (range_beginning + baseaddr == 0
14885 && !dwarf2_per_objfile->has_section_at_zero)
14886 {
14887 complaint (&symfile_complaints,
14888 _(".debug_rnglists entry has start address of zero"
14889 " [in module %s]"), objfile_name (objfile));
14890 continue;
14891 }
14892
14893 callback (range_beginning, range_end);
14894 }
14895
14896 if (overflow)
14897 {
14898 complaint (&symfile_complaints,
14899 _("Offset %d is not terminated "
14900 "for DW_AT_ranges attribute"),
14901 offset);
14902 return false;
14903 }
14904
14905 return true;
14906 }
14907
14908 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14909 Callback's type should be:
14910 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14911 Return 1 if the attributes are present and valid, otherwise, return 0. */
14912
14913 template <typename Callback>
14914 static int
14915 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
14916 Callback &&callback)
14917 {
14918 struct dwarf2_per_objfile *dwarf2_per_objfile
14919 = cu->per_cu->dwarf2_per_objfile;
14920 struct objfile *objfile = dwarf2_per_objfile->objfile;
14921 struct comp_unit_head *cu_header = &cu->header;
14922 bfd *obfd = objfile->obfd;
14923 unsigned int addr_size = cu_header->addr_size;
14924 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14925 /* Base address selection entry. */
14926 CORE_ADDR base;
14927 int found_base;
14928 unsigned int dummy;
14929 const gdb_byte *buffer;
14930 CORE_ADDR baseaddr;
14931
14932 if (cu_header->version >= 5)
14933 return dwarf2_rnglists_process (offset, cu, callback);
14934
14935 found_base = cu->base_known;
14936 base = cu->base_address;
14937
14938 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
14939 if (offset >= dwarf2_per_objfile->ranges.size)
14940 {
14941 complaint (&symfile_complaints,
14942 _("Offset %d out of bounds for DW_AT_ranges attribute"),
14943 offset);
14944 return 0;
14945 }
14946 buffer = dwarf2_per_objfile->ranges.buffer + offset;
14947
14948 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14949
14950 while (1)
14951 {
14952 CORE_ADDR range_beginning, range_end;
14953
14954 range_beginning = read_address (obfd, buffer, cu, &dummy);
14955 buffer += addr_size;
14956 range_end = read_address (obfd, buffer, cu, &dummy);
14957 buffer += addr_size;
14958 offset += 2 * addr_size;
14959
14960 /* An end of list marker is a pair of zero addresses. */
14961 if (range_beginning == 0 && range_end == 0)
14962 /* Found the end of list entry. */
14963 break;
14964
14965 /* Each base address selection entry is a pair of 2 values.
14966 The first is the largest possible address, the second is
14967 the base address. Check for a base address here. */
14968 if ((range_beginning & mask) == mask)
14969 {
14970 /* If we found the largest possible address, then we already
14971 have the base address in range_end. */
14972 base = range_end;
14973 found_base = 1;
14974 continue;
14975 }
14976
14977 if (!found_base)
14978 {
14979 /* We have no valid base address for the ranges
14980 data. */
14981 complaint (&symfile_complaints,
14982 _("Invalid .debug_ranges data (no base address)"));
14983 return 0;
14984 }
14985
14986 if (range_beginning > range_end)
14987 {
14988 /* Inverted range entries are invalid. */
14989 complaint (&symfile_complaints,
14990 _("Invalid .debug_ranges data (inverted range)"));
14991 return 0;
14992 }
14993
14994 /* Empty range entries have no effect. */
14995 if (range_beginning == range_end)
14996 continue;
14997
14998 range_beginning += base;
14999 range_end += base;
15000
15001 /* A not-uncommon case of bad debug info.
15002 Don't pollute the addrmap with bad data. */
15003 if (range_beginning + baseaddr == 0
15004 && !dwarf2_per_objfile->has_section_at_zero)
15005 {
15006 complaint (&symfile_complaints,
15007 _(".debug_ranges entry has start address of zero"
15008 " [in module %s]"), objfile_name (objfile));
15009 continue;
15010 }
15011
15012 callback (range_beginning, range_end);
15013 }
15014
15015 return 1;
15016 }
15017
15018 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
15019 Return 1 if the attributes are present and valid, otherwise, return 0.
15020 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
15021
15022 static int
15023 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
15024 CORE_ADDR *high_return, struct dwarf2_cu *cu,
15025 struct partial_symtab *ranges_pst)
15026 {
15027 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15028 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15029 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
15030 SECT_OFF_TEXT (objfile));
15031 int low_set = 0;
15032 CORE_ADDR low = 0;
15033 CORE_ADDR high = 0;
15034 int retval;
15035
15036 retval = dwarf2_ranges_process (offset, cu,
15037 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
15038 {
15039 if (ranges_pst != NULL)
15040 {
15041 CORE_ADDR lowpc;
15042 CORE_ADDR highpc;
15043
15044 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
15045 range_beginning + baseaddr);
15046 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
15047 range_end + baseaddr);
15048 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
15049 ranges_pst);
15050 }
15051
15052 /* FIXME: This is recording everything as a low-high
15053 segment of consecutive addresses. We should have a
15054 data structure for discontiguous block ranges
15055 instead. */
15056 if (! low_set)
15057 {
15058 low = range_beginning;
15059 high = range_end;
15060 low_set = 1;
15061 }
15062 else
15063 {
15064 if (range_beginning < low)
15065 low = range_beginning;
15066 if (range_end > high)
15067 high = range_end;
15068 }
15069 });
15070 if (!retval)
15071 return 0;
15072
15073 if (! low_set)
15074 /* If the first entry is an end-of-list marker, the range
15075 describes an empty scope, i.e. no instructions. */
15076 return 0;
15077
15078 if (low_return)
15079 *low_return = low;
15080 if (high_return)
15081 *high_return = high;
15082 return 1;
15083 }
15084
15085 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
15086 definition for the return value. *LOWPC and *HIGHPC are set iff
15087 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
15088
15089 static enum pc_bounds_kind
15090 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
15091 CORE_ADDR *highpc, struct dwarf2_cu *cu,
15092 struct partial_symtab *pst)
15093 {
15094 struct dwarf2_per_objfile *dwarf2_per_objfile
15095 = cu->per_cu->dwarf2_per_objfile;
15096 struct attribute *attr;
15097 struct attribute *attr_high;
15098 CORE_ADDR low = 0;
15099 CORE_ADDR high = 0;
15100 enum pc_bounds_kind ret;
15101
15102 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
15103 if (attr_high)
15104 {
15105 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
15106 if (attr)
15107 {
15108 low = attr_value_as_address (attr);
15109 high = attr_value_as_address (attr_high);
15110 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
15111 high += low;
15112 }
15113 else
15114 /* Found high w/o low attribute. */
15115 return PC_BOUNDS_INVALID;
15116
15117 /* Found consecutive range of addresses. */
15118 ret = PC_BOUNDS_HIGH_LOW;
15119 }
15120 else
15121 {
15122 attr = dwarf2_attr (die, DW_AT_ranges, cu);
15123 if (attr != NULL)
15124 {
15125 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
15126 We take advantage of the fact that DW_AT_ranges does not appear
15127 in DW_TAG_compile_unit of DWO files. */
15128 int need_ranges_base = die->tag != DW_TAG_compile_unit;
15129 unsigned int ranges_offset = (DW_UNSND (attr)
15130 + (need_ranges_base
15131 ? cu->ranges_base
15132 : 0));
15133
15134 /* Value of the DW_AT_ranges attribute is the offset in the
15135 .debug_ranges section. */
15136 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
15137 return PC_BOUNDS_INVALID;
15138 /* Found discontinuous range of addresses. */
15139 ret = PC_BOUNDS_RANGES;
15140 }
15141 else
15142 return PC_BOUNDS_NOT_PRESENT;
15143 }
15144
15145 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
15146 if (high <= low)
15147 return PC_BOUNDS_INVALID;
15148
15149 /* When using the GNU linker, .gnu.linkonce. sections are used to
15150 eliminate duplicate copies of functions and vtables and such.
15151 The linker will arbitrarily choose one and discard the others.
15152 The AT_*_pc values for such functions refer to local labels in
15153 these sections. If the section from that file was discarded, the
15154 labels are not in the output, so the relocs get a value of 0.
15155 If this is a discarded function, mark the pc bounds as invalid,
15156 so that GDB will ignore it. */
15157 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
15158 return PC_BOUNDS_INVALID;
15159
15160 *lowpc = low;
15161 if (highpc)
15162 *highpc = high;
15163 return ret;
15164 }
15165
15166 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
15167 its low and high PC addresses. Do nothing if these addresses could not
15168 be determined. Otherwise, set LOWPC to the low address if it is smaller,
15169 and HIGHPC to the high address if greater than HIGHPC. */
15170
15171 static void
15172 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
15173 CORE_ADDR *lowpc, CORE_ADDR *highpc,
15174 struct dwarf2_cu *cu)
15175 {
15176 CORE_ADDR low, high;
15177 struct die_info *child = die->child;
15178
15179 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
15180 {
15181 *lowpc = std::min (*lowpc, low);
15182 *highpc = std::max (*highpc, high);
15183 }
15184
15185 /* If the language does not allow nested subprograms (either inside
15186 subprograms or lexical blocks), we're done. */
15187 if (cu->language != language_ada)
15188 return;
15189
15190 /* Check all the children of the given DIE. If it contains nested
15191 subprograms, then check their pc bounds. Likewise, we need to
15192 check lexical blocks as well, as they may also contain subprogram
15193 definitions. */
15194 while (child && child->tag)
15195 {
15196 if (child->tag == DW_TAG_subprogram
15197 || child->tag == DW_TAG_lexical_block)
15198 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
15199 child = sibling_die (child);
15200 }
15201 }
15202
15203 /* Get the low and high pc's represented by the scope DIE, and store
15204 them in *LOWPC and *HIGHPC. If the correct values can't be
15205 determined, set *LOWPC to -1 and *HIGHPC to 0. */
15206
15207 static void
15208 get_scope_pc_bounds (struct die_info *die,
15209 CORE_ADDR *lowpc, CORE_ADDR *highpc,
15210 struct dwarf2_cu *cu)
15211 {
15212 CORE_ADDR best_low = (CORE_ADDR) -1;
15213 CORE_ADDR best_high = (CORE_ADDR) 0;
15214 CORE_ADDR current_low, current_high;
15215
15216 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
15217 >= PC_BOUNDS_RANGES)
15218 {
15219 best_low = current_low;
15220 best_high = current_high;
15221 }
15222 else
15223 {
15224 struct die_info *child = die->child;
15225
15226 while (child && child->tag)
15227 {
15228 switch (child->tag) {
15229 case DW_TAG_subprogram:
15230 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
15231 break;
15232 case DW_TAG_namespace:
15233 case DW_TAG_module:
15234 /* FIXME: carlton/2004-01-16: Should we do this for
15235 DW_TAG_class_type/DW_TAG_structure_type, too? I think
15236 that current GCC's always emit the DIEs corresponding
15237 to definitions of methods of classes as children of a
15238 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
15239 the DIEs giving the declarations, which could be
15240 anywhere). But I don't see any reason why the
15241 standards says that they have to be there. */
15242 get_scope_pc_bounds (child, &current_low, &current_high, cu);
15243
15244 if (current_low != ((CORE_ADDR) -1))
15245 {
15246 best_low = std::min (best_low, current_low);
15247 best_high = std::max (best_high, current_high);
15248 }
15249 break;
15250 default:
15251 /* Ignore. */
15252 break;
15253 }
15254
15255 child = sibling_die (child);
15256 }
15257 }
15258
15259 *lowpc = best_low;
15260 *highpc = best_high;
15261 }
15262
15263 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
15264 in DIE. */
15265
15266 static void
15267 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
15268 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
15269 {
15270 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15271 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15272 struct attribute *attr;
15273 struct attribute *attr_high;
15274
15275 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
15276 if (attr_high)
15277 {
15278 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
15279 if (attr)
15280 {
15281 CORE_ADDR low = attr_value_as_address (attr);
15282 CORE_ADDR high = attr_value_as_address (attr_high);
15283
15284 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
15285 high += low;
15286
15287 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
15288 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
15289 record_block_range (block, low, high - 1);
15290 }
15291 }
15292
15293 attr = dwarf2_attr (die, DW_AT_ranges, cu);
15294 if (attr)
15295 {
15296 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
15297 We take advantage of the fact that DW_AT_ranges does not appear
15298 in DW_TAG_compile_unit of DWO files. */
15299 int need_ranges_base = die->tag != DW_TAG_compile_unit;
15300
15301 /* The value of the DW_AT_ranges attribute is the offset of the
15302 address range list in the .debug_ranges section. */
15303 unsigned long offset = (DW_UNSND (attr)
15304 + (need_ranges_base ? cu->ranges_base : 0));
15305 const gdb_byte *buffer;
15306
15307 /* For some target architectures, but not others, the
15308 read_address function sign-extends the addresses it returns.
15309 To recognize base address selection entries, we need a
15310 mask. */
15311 unsigned int addr_size = cu->header.addr_size;
15312 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
15313
15314 /* The base address, to which the next pair is relative. Note
15315 that this 'base' is a DWARF concept: most entries in a range
15316 list are relative, to reduce the number of relocs against the
15317 debugging information. This is separate from this function's
15318 'baseaddr' argument, which GDB uses to relocate debugging
15319 information from a shared library based on the address at
15320 which the library was loaded. */
15321 CORE_ADDR base = cu->base_address;
15322 int base_known = cu->base_known;
15323
15324 dwarf2_ranges_process (offset, cu,
15325 [&] (CORE_ADDR start, CORE_ADDR end)
15326 {
15327 start += baseaddr;
15328 end += baseaddr;
15329 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
15330 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
15331 record_block_range (block, start, end - 1);
15332 });
15333 }
15334 }
15335
15336 /* Check whether the producer field indicates either of GCC < 4.6, or the
15337 Intel C/C++ compiler, and cache the result in CU. */
15338
15339 static void
15340 check_producer (struct dwarf2_cu *cu)
15341 {
15342 int major, minor;
15343
15344 if (cu->producer == NULL)
15345 {
15346 /* For unknown compilers expect their behavior is DWARF version
15347 compliant.
15348
15349 GCC started to support .debug_types sections by -gdwarf-4 since
15350 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
15351 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
15352 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
15353 interpreted incorrectly by GDB now - GCC PR debug/48229. */
15354 }
15355 else if (producer_is_gcc (cu->producer, &major, &minor))
15356 {
15357 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
15358 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
15359 }
15360 else if (producer_is_icc (cu->producer, &major, &minor))
15361 cu->producer_is_icc_lt_14 = major < 14;
15362 else
15363 {
15364 /* For other non-GCC compilers, expect their behavior is DWARF version
15365 compliant. */
15366 }
15367
15368 cu->checked_producer = 1;
15369 }
15370
15371 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
15372 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
15373 during 4.6.0 experimental. */
15374
15375 static int
15376 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
15377 {
15378 if (!cu->checked_producer)
15379 check_producer (cu);
15380
15381 return cu->producer_is_gxx_lt_4_6;
15382 }
15383
15384 /* Return the default accessibility type if it is not overriden by
15385 DW_AT_accessibility. */
15386
15387 static enum dwarf_access_attribute
15388 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
15389 {
15390 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
15391 {
15392 /* The default DWARF 2 accessibility for members is public, the default
15393 accessibility for inheritance is private. */
15394
15395 if (die->tag != DW_TAG_inheritance)
15396 return DW_ACCESS_public;
15397 else
15398 return DW_ACCESS_private;
15399 }
15400 else
15401 {
15402 /* DWARF 3+ defines the default accessibility a different way. The same
15403 rules apply now for DW_TAG_inheritance as for the members and it only
15404 depends on the container kind. */
15405
15406 if (die->parent->tag == DW_TAG_class_type)
15407 return DW_ACCESS_private;
15408 else
15409 return DW_ACCESS_public;
15410 }
15411 }
15412
15413 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
15414 offset. If the attribute was not found return 0, otherwise return
15415 1. If it was found but could not properly be handled, set *OFFSET
15416 to 0. */
15417
15418 static int
15419 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
15420 LONGEST *offset)
15421 {
15422 struct attribute *attr;
15423
15424 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
15425 if (attr != NULL)
15426 {
15427 *offset = 0;
15428
15429 /* Note that we do not check for a section offset first here.
15430 This is because DW_AT_data_member_location is new in DWARF 4,
15431 so if we see it, we can assume that a constant form is really
15432 a constant and not a section offset. */
15433 if (attr_form_is_constant (attr))
15434 *offset = dwarf2_get_attr_constant_value (attr, 0);
15435 else if (attr_form_is_section_offset (attr))
15436 dwarf2_complex_location_expr_complaint ();
15437 else if (attr_form_is_block (attr))
15438 *offset = decode_locdesc (DW_BLOCK (attr), cu);
15439 else
15440 dwarf2_complex_location_expr_complaint ();
15441
15442 return 1;
15443 }
15444
15445 return 0;
15446 }
15447
15448 /* Add an aggregate field to the field list. */
15449
15450 static void
15451 dwarf2_add_field (struct field_info *fip, struct die_info *die,
15452 struct dwarf2_cu *cu)
15453 {
15454 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15455 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15456 struct nextfield *new_field;
15457 struct attribute *attr;
15458 struct field *fp;
15459 const char *fieldname = "";
15460
15461 if (die->tag == DW_TAG_inheritance)
15462 {
15463 fip->baseclasses.emplace_back ();
15464 new_field = &fip->baseclasses.back ();
15465 }
15466 else
15467 {
15468 fip->fields.emplace_back ();
15469 new_field = &fip->fields.back ();
15470 }
15471
15472 fip->nfields++;
15473
15474 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15475 if (attr)
15476 new_field->accessibility = DW_UNSND (attr);
15477 else
15478 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
15479 if (new_field->accessibility != DW_ACCESS_public)
15480 fip->non_public_fields = 1;
15481
15482 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15483 if (attr)
15484 new_field->virtuality = DW_UNSND (attr);
15485 else
15486 new_field->virtuality = DW_VIRTUALITY_none;
15487
15488 fp = &new_field->field;
15489
15490 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
15491 {
15492 LONGEST offset;
15493
15494 /* Data member other than a C++ static data member. */
15495
15496 /* Get type of field. */
15497 fp->type = die_type (die, cu);
15498
15499 SET_FIELD_BITPOS (*fp, 0);
15500
15501 /* Get bit size of field (zero if none). */
15502 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
15503 if (attr)
15504 {
15505 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
15506 }
15507 else
15508 {
15509 FIELD_BITSIZE (*fp) = 0;
15510 }
15511
15512 /* Get bit offset of field. */
15513 if (handle_data_member_location (die, cu, &offset))
15514 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
15515 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
15516 if (attr)
15517 {
15518 if (gdbarch_bits_big_endian (gdbarch))
15519 {
15520 /* For big endian bits, the DW_AT_bit_offset gives the
15521 additional bit offset from the MSB of the containing
15522 anonymous object to the MSB of the field. We don't
15523 have to do anything special since we don't need to
15524 know the size of the anonymous object. */
15525 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
15526 }
15527 else
15528 {
15529 /* For little endian bits, compute the bit offset to the
15530 MSB of the anonymous object, subtract off the number of
15531 bits from the MSB of the field to the MSB of the
15532 object, and then subtract off the number of bits of
15533 the field itself. The result is the bit offset of
15534 the LSB of the field. */
15535 int anonymous_size;
15536 int bit_offset = DW_UNSND (attr);
15537
15538 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15539 if (attr)
15540 {
15541 /* The size of the anonymous object containing
15542 the bit field is explicit, so use the
15543 indicated size (in bytes). */
15544 anonymous_size = DW_UNSND (attr);
15545 }
15546 else
15547 {
15548 /* The size of the anonymous object containing
15549 the bit field must be inferred from the type
15550 attribute of the data member containing the
15551 bit field. */
15552 anonymous_size = TYPE_LENGTH (fp->type);
15553 }
15554 SET_FIELD_BITPOS (*fp,
15555 (FIELD_BITPOS (*fp)
15556 + anonymous_size * bits_per_byte
15557 - bit_offset - FIELD_BITSIZE (*fp)));
15558 }
15559 }
15560 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
15561 if (attr != NULL)
15562 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
15563 + dwarf2_get_attr_constant_value (attr, 0)));
15564
15565 /* Get name of field. */
15566 fieldname = dwarf2_name (die, cu);
15567 if (fieldname == NULL)
15568 fieldname = "";
15569
15570 /* The name is already allocated along with this objfile, so we don't
15571 need to duplicate it for the type. */
15572 fp->name = fieldname;
15573
15574 /* Change accessibility for artificial fields (e.g. virtual table
15575 pointer or virtual base class pointer) to private. */
15576 if (dwarf2_attr (die, DW_AT_artificial, cu))
15577 {
15578 FIELD_ARTIFICIAL (*fp) = 1;
15579 new_field->accessibility = DW_ACCESS_private;
15580 fip->non_public_fields = 1;
15581 }
15582 }
15583 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
15584 {
15585 /* C++ static member. */
15586
15587 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
15588 is a declaration, but all versions of G++ as of this writing
15589 (so through at least 3.2.1) incorrectly generate
15590 DW_TAG_variable tags. */
15591
15592 const char *physname;
15593
15594 /* Get name of field. */
15595 fieldname = dwarf2_name (die, cu);
15596 if (fieldname == NULL)
15597 return;
15598
15599 attr = dwarf2_attr (die, DW_AT_const_value, cu);
15600 if (attr
15601 /* Only create a symbol if this is an external value.
15602 new_symbol checks this and puts the value in the global symbol
15603 table, which we want. If it is not external, new_symbol
15604 will try to put the value in cu->list_in_scope which is wrong. */
15605 && dwarf2_flag_true_p (die, DW_AT_external, cu))
15606 {
15607 /* A static const member, not much different than an enum as far as
15608 we're concerned, except that we can support more types. */
15609 new_symbol (die, NULL, cu);
15610 }
15611
15612 /* Get physical name. */
15613 physname = dwarf2_physname (fieldname, die, cu);
15614
15615 /* The name is already allocated along with this objfile, so we don't
15616 need to duplicate it for the type. */
15617 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
15618 FIELD_TYPE (*fp) = die_type (die, cu);
15619 FIELD_NAME (*fp) = fieldname;
15620 }
15621 else if (die->tag == DW_TAG_inheritance)
15622 {
15623 LONGEST offset;
15624
15625 /* C++ base class field. */
15626 if (handle_data_member_location (die, cu, &offset))
15627 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
15628 FIELD_BITSIZE (*fp) = 0;
15629 FIELD_TYPE (*fp) = die_type (die, cu);
15630 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
15631 }
15632 else if (die->tag == DW_TAG_variant_part)
15633 {
15634 /* process_structure_scope will treat this DIE as a union. */
15635 process_structure_scope (die, cu);
15636
15637 /* The variant part is relative to the start of the enclosing
15638 structure. */
15639 SET_FIELD_BITPOS (*fp, 0);
15640 fp->type = get_die_type (die, cu);
15641 fp->artificial = 1;
15642 fp->name = "<<variant>>";
15643 }
15644 else
15645 gdb_assert_not_reached ("missing case in dwarf2_add_field");
15646 }
15647
15648 /* Can the type given by DIE define another type? */
15649
15650 static bool
15651 type_can_define_types (const struct die_info *die)
15652 {
15653 switch (die->tag)
15654 {
15655 case DW_TAG_typedef:
15656 case DW_TAG_class_type:
15657 case DW_TAG_structure_type:
15658 case DW_TAG_union_type:
15659 case DW_TAG_enumeration_type:
15660 return true;
15661
15662 default:
15663 return false;
15664 }
15665 }
15666
15667 /* Add a type definition defined in the scope of the FIP's class. */
15668
15669 static void
15670 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
15671 struct dwarf2_cu *cu)
15672 {
15673 struct decl_field fp;
15674 memset (&fp, 0, sizeof (fp));
15675
15676 gdb_assert (type_can_define_types (die));
15677
15678 /* Get name of field. NULL is okay here, meaning an anonymous type. */
15679 fp.name = dwarf2_name (die, cu);
15680 fp.type = read_type_die (die, cu);
15681
15682 /* Save accessibility. */
15683 enum dwarf_access_attribute accessibility;
15684 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15685 if (attr != NULL)
15686 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15687 else
15688 accessibility = dwarf2_default_access_attribute (die, cu);
15689 switch (accessibility)
15690 {
15691 case DW_ACCESS_public:
15692 /* The assumed value if neither private nor protected. */
15693 break;
15694 case DW_ACCESS_private:
15695 fp.is_private = 1;
15696 break;
15697 case DW_ACCESS_protected:
15698 fp.is_protected = 1;
15699 break;
15700 default:
15701 complaint (&symfile_complaints,
15702 _("Unhandled DW_AT_accessibility value (%x)"), accessibility);
15703 }
15704
15705 if (die->tag == DW_TAG_typedef)
15706 fip->typedef_field_list.push_back (fp);
15707 else
15708 fip->nested_types_list.push_back (fp);
15709 }
15710
15711 /* Create the vector of fields, and attach it to the type. */
15712
15713 static void
15714 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
15715 struct dwarf2_cu *cu)
15716 {
15717 int nfields = fip->nfields;
15718
15719 /* Record the field count, allocate space for the array of fields,
15720 and create blank accessibility bitfields if necessary. */
15721 TYPE_NFIELDS (type) = nfields;
15722 TYPE_FIELDS (type) = (struct field *)
15723 TYPE_ZALLOC (type, sizeof (struct field) * nfields);
15724
15725 if (fip->non_public_fields && cu->language != language_ada)
15726 {
15727 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15728
15729 TYPE_FIELD_PRIVATE_BITS (type) =
15730 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15731 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15732
15733 TYPE_FIELD_PROTECTED_BITS (type) =
15734 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15735 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15736
15737 TYPE_FIELD_IGNORE_BITS (type) =
15738 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15739 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
15740 }
15741
15742 /* If the type has baseclasses, allocate and clear a bit vector for
15743 TYPE_FIELD_VIRTUAL_BITS. */
15744 if (!fip->baseclasses.empty () && cu->language != language_ada)
15745 {
15746 int num_bytes = B_BYTES (fip->baseclasses.size ());
15747 unsigned char *pointer;
15748
15749 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15750 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
15751 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
15752 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
15753 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
15754 }
15755
15756 if (TYPE_FLAG_DISCRIMINATED_UNION (type))
15757 {
15758 struct discriminant_info *di = alloc_discriminant_info (type, -1, -1);
15759
15760 for (int index = 0; index < nfields; ++index)
15761 {
15762 struct nextfield &field = fip->fields[index];
15763
15764 if (field.variant.is_discriminant)
15765 di->discriminant_index = index;
15766 else if (field.variant.default_branch)
15767 di->default_index = index;
15768 else
15769 di->discriminants[index] = field.variant.discriminant_value;
15770 }
15771 }
15772
15773 /* Copy the saved-up fields into the field vector. */
15774 for (int i = 0; i < nfields; ++i)
15775 {
15776 struct nextfield &field
15777 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
15778 : fip->fields[i - fip->baseclasses.size ()]);
15779
15780 TYPE_FIELD (type, i) = field.field;
15781 switch (field.accessibility)
15782 {
15783 case DW_ACCESS_private:
15784 if (cu->language != language_ada)
15785 SET_TYPE_FIELD_PRIVATE (type, i);
15786 break;
15787
15788 case DW_ACCESS_protected:
15789 if (cu->language != language_ada)
15790 SET_TYPE_FIELD_PROTECTED (type, i);
15791 break;
15792
15793 case DW_ACCESS_public:
15794 break;
15795
15796 default:
15797 /* Unknown accessibility. Complain and treat it as public. */
15798 {
15799 complaint (&symfile_complaints, _("unsupported accessibility %d"),
15800 field.accessibility);
15801 }
15802 break;
15803 }
15804 if (i < fip->baseclasses.size ())
15805 {
15806 switch (field.virtuality)
15807 {
15808 case DW_VIRTUALITY_virtual:
15809 case DW_VIRTUALITY_pure_virtual:
15810 if (cu->language == language_ada)
15811 error (_("unexpected virtuality in component of Ada type"));
15812 SET_TYPE_FIELD_VIRTUAL (type, i);
15813 break;
15814 }
15815 }
15816 }
15817 }
15818
15819 /* Return true if this member function is a constructor, false
15820 otherwise. */
15821
15822 static int
15823 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15824 {
15825 const char *fieldname;
15826 const char *type_name;
15827 int len;
15828
15829 if (die->parent == NULL)
15830 return 0;
15831
15832 if (die->parent->tag != DW_TAG_structure_type
15833 && die->parent->tag != DW_TAG_union_type
15834 && die->parent->tag != DW_TAG_class_type)
15835 return 0;
15836
15837 fieldname = dwarf2_name (die, cu);
15838 type_name = dwarf2_name (die->parent, cu);
15839 if (fieldname == NULL || type_name == NULL)
15840 return 0;
15841
15842 len = strlen (fieldname);
15843 return (strncmp (fieldname, type_name, len) == 0
15844 && (type_name[len] == '\0' || type_name[len] == '<'));
15845 }
15846
15847 /* Add a member function to the proper fieldlist. */
15848
15849 static void
15850 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
15851 struct type *type, struct dwarf2_cu *cu)
15852 {
15853 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15854 struct attribute *attr;
15855 int i;
15856 struct fnfieldlist *flp = nullptr;
15857 struct fn_field *fnp;
15858 const char *fieldname;
15859 struct type *this_type;
15860 enum dwarf_access_attribute accessibility;
15861
15862 if (cu->language == language_ada)
15863 error (_("unexpected member function in Ada type"));
15864
15865 /* Get name of member function. */
15866 fieldname = dwarf2_name (die, cu);
15867 if (fieldname == NULL)
15868 return;
15869
15870 /* Look up member function name in fieldlist. */
15871 for (i = 0; i < fip->fnfieldlists.size (); i++)
15872 {
15873 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
15874 {
15875 flp = &fip->fnfieldlists[i];
15876 break;
15877 }
15878 }
15879
15880 /* Create a new fnfieldlist if necessary. */
15881 if (flp == nullptr)
15882 {
15883 fip->fnfieldlists.emplace_back ();
15884 flp = &fip->fnfieldlists.back ();
15885 flp->name = fieldname;
15886 i = fip->fnfieldlists.size () - 1;
15887 }
15888
15889 /* Create a new member function field and add it to the vector of
15890 fnfieldlists. */
15891 flp->fnfields.emplace_back ();
15892 fnp = &flp->fnfields.back ();
15893
15894 /* Delay processing of the physname until later. */
15895 if (cu->language == language_cplus)
15896 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15897 die, cu);
15898 else
15899 {
15900 const char *physname = dwarf2_physname (fieldname, die, cu);
15901 fnp->physname = physname ? physname : "";
15902 }
15903
15904 fnp->type = alloc_type (objfile);
15905 this_type = read_type_die (die, cu);
15906 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
15907 {
15908 int nparams = TYPE_NFIELDS (this_type);
15909
15910 /* TYPE is the domain of this method, and THIS_TYPE is the type
15911 of the method itself (TYPE_CODE_METHOD). */
15912 smash_to_method_type (fnp->type, type,
15913 TYPE_TARGET_TYPE (this_type),
15914 TYPE_FIELDS (this_type),
15915 TYPE_NFIELDS (this_type),
15916 TYPE_VARARGS (this_type));
15917
15918 /* Handle static member functions.
15919 Dwarf2 has no clean way to discern C++ static and non-static
15920 member functions. G++ helps GDB by marking the first
15921 parameter for non-static member functions (which is the this
15922 pointer) as artificial. We obtain this information from
15923 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
15924 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
15925 fnp->voffset = VOFFSET_STATIC;
15926 }
15927 else
15928 complaint (&symfile_complaints, _("member function type missing for '%s'"),
15929 dwarf2_full_name (fieldname, die, cu));
15930
15931 /* Get fcontext from DW_AT_containing_type if present. */
15932 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15933 fnp->fcontext = die_containing_type (die, cu);
15934
15935 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15936 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
15937
15938 /* Get accessibility. */
15939 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15940 if (attr)
15941 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15942 else
15943 accessibility = dwarf2_default_access_attribute (die, cu);
15944 switch (accessibility)
15945 {
15946 case DW_ACCESS_private:
15947 fnp->is_private = 1;
15948 break;
15949 case DW_ACCESS_protected:
15950 fnp->is_protected = 1;
15951 break;
15952 }
15953
15954 /* Check for artificial methods. */
15955 attr = dwarf2_attr (die, DW_AT_artificial, cu);
15956 if (attr && DW_UNSND (attr) != 0)
15957 fnp->is_artificial = 1;
15958
15959 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15960
15961 /* Get index in virtual function table if it is a virtual member
15962 function. For older versions of GCC, this is an offset in the
15963 appropriate virtual table, as specified by DW_AT_containing_type.
15964 For everyone else, it is an expression to be evaluated relative
15965 to the object address. */
15966
15967 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
15968 if (attr)
15969 {
15970 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
15971 {
15972 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15973 {
15974 /* Old-style GCC. */
15975 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15976 }
15977 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15978 || (DW_BLOCK (attr)->size > 1
15979 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15980 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15981 {
15982 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15983 if ((fnp->voffset % cu->header.addr_size) != 0)
15984 dwarf2_complex_location_expr_complaint ();
15985 else
15986 fnp->voffset /= cu->header.addr_size;
15987 fnp->voffset += 2;
15988 }
15989 else
15990 dwarf2_complex_location_expr_complaint ();
15991
15992 if (!fnp->fcontext)
15993 {
15994 /* If there is no `this' field and no DW_AT_containing_type,
15995 we cannot actually find a base class context for the
15996 vtable! */
15997 if (TYPE_NFIELDS (this_type) == 0
15998 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15999 {
16000 complaint (&symfile_complaints,
16001 _("cannot determine context for virtual member "
16002 "function \"%s\" (offset %s)"),
16003 fieldname, sect_offset_str (die->sect_off));
16004 }
16005 else
16006 {
16007 fnp->fcontext
16008 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
16009 }
16010 }
16011 }
16012 else if (attr_form_is_section_offset (attr))
16013 {
16014 dwarf2_complex_location_expr_complaint ();
16015 }
16016 else
16017 {
16018 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
16019 fieldname);
16020 }
16021 }
16022 else
16023 {
16024 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
16025 if (attr && DW_UNSND (attr))
16026 {
16027 /* GCC does this, as of 2008-08-25; PR debug/37237. */
16028 complaint (&symfile_complaints,
16029 _("Member function \"%s\" (offset %s) is virtual "
16030 "but the vtable offset is not specified"),
16031 fieldname, sect_offset_str (die->sect_off));
16032 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16033 TYPE_CPLUS_DYNAMIC (type) = 1;
16034 }
16035 }
16036 }
16037
16038 /* Create the vector of member function fields, and attach it to the type. */
16039
16040 static void
16041 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
16042 struct dwarf2_cu *cu)
16043 {
16044 if (cu->language == language_ada)
16045 error (_("unexpected member functions in Ada type"));
16046
16047 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16048 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
16049 TYPE_ALLOC (type,
16050 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
16051
16052 for (int i = 0; i < fip->fnfieldlists.size (); i++)
16053 {
16054 struct fnfieldlist &nf = fip->fnfieldlists[i];
16055 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
16056
16057 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
16058 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
16059 fn_flp->fn_fields = (struct fn_field *)
16060 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
16061
16062 for (int k = 0; k < nf.fnfields.size (); ++k)
16063 fn_flp->fn_fields[k] = nf.fnfields[k];
16064 }
16065
16066 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
16067 }
16068
16069 /* Returns non-zero if NAME is the name of a vtable member in CU's
16070 language, zero otherwise. */
16071 static int
16072 is_vtable_name (const char *name, struct dwarf2_cu *cu)
16073 {
16074 static const char vptr[] = "_vptr";
16075
16076 /* Look for the C++ form of the vtable. */
16077 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
16078 return 1;
16079
16080 return 0;
16081 }
16082
16083 /* GCC outputs unnamed structures that are really pointers to member
16084 functions, with the ABI-specified layout. If TYPE describes
16085 such a structure, smash it into a member function type.
16086
16087 GCC shouldn't do this; it should just output pointer to member DIEs.
16088 This is GCC PR debug/28767. */
16089
16090 static void
16091 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
16092 {
16093 struct type *pfn_type, *self_type, *new_type;
16094
16095 /* Check for a structure with no name and two children. */
16096 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
16097 return;
16098
16099 /* Check for __pfn and __delta members. */
16100 if (TYPE_FIELD_NAME (type, 0) == NULL
16101 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
16102 || TYPE_FIELD_NAME (type, 1) == NULL
16103 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
16104 return;
16105
16106 /* Find the type of the method. */
16107 pfn_type = TYPE_FIELD_TYPE (type, 0);
16108 if (pfn_type == NULL
16109 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
16110 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
16111 return;
16112
16113 /* Look for the "this" argument. */
16114 pfn_type = TYPE_TARGET_TYPE (pfn_type);
16115 if (TYPE_NFIELDS (pfn_type) == 0
16116 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
16117 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
16118 return;
16119
16120 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
16121 new_type = alloc_type (objfile);
16122 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
16123 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
16124 TYPE_VARARGS (pfn_type));
16125 smash_to_methodptr_type (type, new_type);
16126 }
16127
16128
16129 /* Called when we find the DIE that starts a structure or union scope
16130 (definition) to create a type for the structure or union. Fill in
16131 the type's name and general properties; the members will not be
16132 processed until process_structure_scope. A symbol table entry for
16133 the type will also not be done until process_structure_scope (assuming
16134 the type has a name).
16135
16136 NOTE: we need to call these functions regardless of whether or not the
16137 DIE has a DW_AT_name attribute, since it might be an anonymous
16138 structure or union. This gets the type entered into our set of
16139 user defined types. */
16140
16141 static struct type *
16142 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
16143 {
16144 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16145 struct type *type;
16146 struct attribute *attr;
16147 const char *name;
16148
16149 /* If the definition of this type lives in .debug_types, read that type.
16150 Don't follow DW_AT_specification though, that will take us back up
16151 the chain and we want to go down. */
16152 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
16153 if (attr)
16154 {
16155 type = get_DW_AT_signature_type (die, attr, cu);
16156
16157 /* The type's CU may not be the same as CU.
16158 Ensure TYPE is recorded with CU in die_type_hash. */
16159 return set_die_type (die, type, cu);
16160 }
16161
16162 type = alloc_type (objfile);
16163 INIT_CPLUS_SPECIFIC (type);
16164
16165 name = dwarf2_name (die, cu);
16166 if (name != NULL)
16167 {
16168 if (cu->language == language_cplus
16169 || cu->language == language_d
16170 || cu->language == language_rust)
16171 {
16172 const char *full_name = dwarf2_full_name (name, die, cu);
16173
16174 /* dwarf2_full_name might have already finished building the DIE's
16175 type. If so, there is no need to continue. */
16176 if (get_die_type (die, cu) != NULL)
16177 return get_die_type (die, cu);
16178
16179 TYPE_TAG_NAME (type) = full_name;
16180 if (die->tag == DW_TAG_structure_type
16181 || die->tag == DW_TAG_class_type)
16182 TYPE_NAME (type) = TYPE_TAG_NAME (type);
16183 }
16184 else
16185 {
16186 /* The name is already allocated along with this objfile, so
16187 we don't need to duplicate it for the type. */
16188 TYPE_TAG_NAME (type) = name;
16189 if (die->tag == DW_TAG_class_type)
16190 TYPE_NAME (type) = TYPE_TAG_NAME (type);
16191 }
16192 }
16193
16194 if (die->tag == DW_TAG_structure_type)
16195 {
16196 TYPE_CODE (type) = TYPE_CODE_STRUCT;
16197 }
16198 else if (die->tag == DW_TAG_union_type)
16199 {
16200 TYPE_CODE (type) = TYPE_CODE_UNION;
16201 }
16202 else if (die->tag == DW_TAG_variant_part)
16203 {
16204 TYPE_CODE (type) = TYPE_CODE_UNION;
16205 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
16206 }
16207 else
16208 {
16209 TYPE_CODE (type) = TYPE_CODE_STRUCT;
16210 }
16211
16212 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
16213 TYPE_DECLARED_CLASS (type) = 1;
16214
16215 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16216 if (attr)
16217 {
16218 if (attr_form_is_constant (attr))
16219 TYPE_LENGTH (type) = DW_UNSND (attr);
16220 else
16221 {
16222 /* For the moment, dynamic type sizes are not supported
16223 by GDB's struct type. The actual size is determined
16224 on-demand when resolving the type of a given object,
16225 so set the type's length to zero for now. Otherwise,
16226 we record an expression as the length, and that expression
16227 could lead to a very large value, which could eventually
16228 lead to us trying to allocate that much memory when creating
16229 a value of that type. */
16230 TYPE_LENGTH (type) = 0;
16231 }
16232 }
16233 else
16234 {
16235 TYPE_LENGTH (type) = 0;
16236 }
16237
16238 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
16239 {
16240 /* ICC<14 does not output the required DW_AT_declaration on
16241 incomplete types, but gives them a size of zero. */
16242 TYPE_STUB (type) = 1;
16243 }
16244 else
16245 TYPE_STUB_SUPPORTED (type) = 1;
16246
16247 if (die_is_declaration (die, cu))
16248 TYPE_STUB (type) = 1;
16249 else if (attr == NULL && die->child == NULL
16250 && producer_is_realview (cu->producer))
16251 /* RealView does not output the required DW_AT_declaration
16252 on incomplete types. */
16253 TYPE_STUB (type) = 1;
16254
16255 /* We need to add the type field to the die immediately so we don't
16256 infinitely recurse when dealing with pointers to the structure
16257 type within the structure itself. */
16258 set_die_type (die, type, cu);
16259
16260 /* set_die_type should be already done. */
16261 set_descriptive_type (type, die, cu);
16262
16263 return type;
16264 }
16265
16266 /* A helper for process_structure_scope that handles a single member
16267 DIE. */
16268
16269 static void
16270 handle_struct_member_die (struct die_info *child_die, struct type *type,
16271 struct field_info *fi,
16272 std::vector<struct symbol *> *template_args,
16273 struct dwarf2_cu *cu)
16274 {
16275 if (child_die->tag == DW_TAG_member
16276 || child_die->tag == DW_TAG_variable
16277 || child_die->tag == DW_TAG_variant_part)
16278 {
16279 /* NOTE: carlton/2002-11-05: A C++ static data member
16280 should be a DW_TAG_member that is a declaration, but
16281 all versions of G++ as of this writing (so through at
16282 least 3.2.1) incorrectly generate DW_TAG_variable
16283 tags for them instead. */
16284 dwarf2_add_field (fi, child_die, cu);
16285 }
16286 else if (child_die->tag == DW_TAG_subprogram)
16287 {
16288 /* Rust doesn't have member functions in the C++ sense.
16289 However, it does emit ordinary functions as children
16290 of a struct DIE. */
16291 if (cu->language == language_rust)
16292 read_func_scope (child_die, cu);
16293 else
16294 {
16295 /* C++ member function. */
16296 dwarf2_add_member_fn (fi, child_die, type, cu);
16297 }
16298 }
16299 else if (child_die->tag == DW_TAG_inheritance)
16300 {
16301 /* C++ base class field. */
16302 dwarf2_add_field (fi, child_die, cu);
16303 }
16304 else if (type_can_define_types (child_die))
16305 dwarf2_add_type_defn (fi, child_die, cu);
16306 else if (child_die->tag == DW_TAG_template_type_param
16307 || child_die->tag == DW_TAG_template_value_param)
16308 {
16309 struct symbol *arg = new_symbol (child_die, NULL, cu);
16310
16311 if (arg != NULL)
16312 template_args->push_back (arg);
16313 }
16314 else if (child_die->tag == DW_TAG_variant)
16315 {
16316 /* In a variant we want to get the discriminant and also add a
16317 field for our sole member child. */
16318 struct attribute *discr = dwarf2_attr (child_die, DW_AT_discr_value, cu);
16319
16320 for (struct die_info *variant_child = child_die->child;
16321 variant_child != NULL;
16322 variant_child = sibling_die (variant_child))
16323 {
16324 if (variant_child->tag == DW_TAG_member)
16325 {
16326 handle_struct_member_die (variant_child, type, fi,
16327 template_args, cu);
16328 /* Only handle the one. */
16329 break;
16330 }
16331 }
16332
16333 /* We don't handle this but we might as well report it if we see
16334 it. */
16335 if (dwarf2_attr (child_die, DW_AT_discr_list, cu) != nullptr)
16336 complaint (&symfile_complaints,
16337 _("DW_AT_discr_list is not supported yet"
16338 " - DIE at %s [in module %s]"),
16339 sect_offset_str (child_die->sect_off),
16340 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16341
16342 /* The first field was just added, so we can stash the
16343 discriminant there. */
16344 gdb_assert (!fi->fields.empty ());
16345 if (discr == NULL)
16346 fi->fields.back ().variant.default_branch = true;
16347 else
16348 fi->fields.back ().variant.discriminant_value = DW_UNSND (discr);
16349 }
16350 }
16351
16352 /* Finish creating a structure or union type, including filling in
16353 its members and creating a symbol for it. */
16354
16355 static void
16356 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
16357 {
16358 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16359 struct die_info *child_die;
16360 struct type *type;
16361
16362 type = get_die_type (die, cu);
16363 if (type == NULL)
16364 type = read_structure_type (die, cu);
16365
16366 /* When reading a DW_TAG_variant_part, we need to notice when we
16367 read the discriminant member, so we can record it later in the
16368 discriminant_info. */
16369 bool is_variant_part = TYPE_FLAG_DISCRIMINATED_UNION (type);
16370 sect_offset discr_offset;
16371
16372 if (is_variant_part)
16373 {
16374 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
16375 if (discr == NULL)
16376 {
16377 /* Maybe it's a univariant form, an extension we support.
16378 In this case arrange not to check the offset. */
16379 is_variant_part = false;
16380 }
16381 else if (attr_form_is_ref (discr))
16382 {
16383 struct dwarf2_cu *target_cu = cu;
16384 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
16385
16386 discr_offset = target_die->sect_off;
16387 }
16388 else
16389 {
16390 complaint (&symfile_complaints,
16391 _("DW_AT_discr does not have DIE reference form"
16392 " - DIE at %s [in module %s]"),
16393 sect_offset_str (die->sect_off),
16394 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16395 is_variant_part = false;
16396 }
16397 }
16398
16399 if (die->child != NULL && ! die_is_declaration (die, cu))
16400 {
16401 struct field_info fi;
16402 std::vector<struct symbol *> template_args;
16403
16404 child_die = die->child;
16405
16406 while (child_die && child_die->tag)
16407 {
16408 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
16409
16410 if (is_variant_part && discr_offset == child_die->sect_off)
16411 fi.fields.back ().variant.is_discriminant = true;
16412
16413 child_die = sibling_die (child_die);
16414 }
16415
16416 /* Attach template arguments to type. */
16417 if (!template_args.empty ())
16418 {
16419 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16420 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
16421 TYPE_TEMPLATE_ARGUMENTS (type)
16422 = XOBNEWVEC (&objfile->objfile_obstack,
16423 struct symbol *,
16424 TYPE_N_TEMPLATE_ARGUMENTS (type));
16425 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
16426 template_args.data (),
16427 (TYPE_N_TEMPLATE_ARGUMENTS (type)
16428 * sizeof (struct symbol *)));
16429 }
16430
16431 /* Attach fields and member functions to the type. */
16432 if (fi.nfields)
16433 dwarf2_attach_fields_to_type (&fi, type, cu);
16434 if (!fi.fnfieldlists.empty ())
16435 {
16436 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
16437
16438 /* Get the type which refers to the base class (possibly this
16439 class itself) which contains the vtable pointer for the current
16440 class from the DW_AT_containing_type attribute. This use of
16441 DW_AT_containing_type is a GNU extension. */
16442
16443 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
16444 {
16445 struct type *t = die_containing_type (die, cu);
16446
16447 set_type_vptr_basetype (type, t);
16448 if (type == t)
16449 {
16450 int i;
16451
16452 /* Our own class provides vtbl ptr. */
16453 for (i = TYPE_NFIELDS (t) - 1;
16454 i >= TYPE_N_BASECLASSES (t);
16455 --i)
16456 {
16457 const char *fieldname = TYPE_FIELD_NAME (t, i);
16458
16459 if (is_vtable_name (fieldname, cu))
16460 {
16461 set_type_vptr_fieldno (type, i);
16462 break;
16463 }
16464 }
16465
16466 /* Complain if virtual function table field not found. */
16467 if (i < TYPE_N_BASECLASSES (t))
16468 complaint (&symfile_complaints,
16469 _("virtual function table pointer "
16470 "not found when defining class '%s'"),
16471 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
16472 "");
16473 }
16474 else
16475 {
16476 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
16477 }
16478 }
16479 else if (cu->producer
16480 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
16481 {
16482 /* The IBM XLC compiler does not provide direct indication
16483 of the containing type, but the vtable pointer is
16484 always named __vfp. */
16485
16486 int i;
16487
16488 for (i = TYPE_NFIELDS (type) - 1;
16489 i >= TYPE_N_BASECLASSES (type);
16490 --i)
16491 {
16492 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
16493 {
16494 set_type_vptr_fieldno (type, i);
16495 set_type_vptr_basetype (type, type);
16496 break;
16497 }
16498 }
16499 }
16500 }
16501
16502 /* Copy fi.typedef_field_list linked list elements content into the
16503 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
16504 if (!fi.typedef_field_list.empty ())
16505 {
16506 int count = fi.typedef_field_list.size ();
16507
16508 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16509 TYPE_TYPEDEF_FIELD_ARRAY (type)
16510 = ((struct decl_field *)
16511 TYPE_ALLOC (type,
16512 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
16513 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
16514
16515 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
16516 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
16517 }
16518
16519 /* Copy fi.nested_types_list linked list elements content into the
16520 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
16521 if (!fi.nested_types_list.empty () && cu->language != language_ada)
16522 {
16523 int count = fi.nested_types_list.size ();
16524
16525 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16526 TYPE_NESTED_TYPES_ARRAY (type)
16527 = ((struct decl_field *)
16528 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
16529 TYPE_NESTED_TYPES_COUNT (type) = count;
16530
16531 for (int i = 0; i < fi.nested_types_list.size (); ++i)
16532 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
16533 }
16534 }
16535
16536 quirk_gcc_member_function_pointer (type, objfile);
16537 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
16538 cu->rust_unions.push_back (type);
16539
16540 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16541 snapshots) has been known to create a die giving a declaration
16542 for a class that has, as a child, a die giving a definition for a
16543 nested class. So we have to process our children even if the
16544 current die is a declaration. Normally, of course, a declaration
16545 won't have any children at all. */
16546
16547 child_die = die->child;
16548
16549 while (child_die != NULL && child_die->tag)
16550 {
16551 if (child_die->tag == DW_TAG_member
16552 || child_die->tag == DW_TAG_variable
16553 || child_die->tag == DW_TAG_inheritance
16554 || child_die->tag == DW_TAG_template_value_param
16555 || child_die->tag == DW_TAG_template_type_param)
16556 {
16557 /* Do nothing. */
16558 }
16559 else
16560 process_die (child_die, cu);
16561
16562 child_die = sibling_die (child_die);
16563 }
16564
16565 /* Do not consider external references. According to the DWARF standard,
16566 these DIEs are identified by the fact that they have no byte_size
16567 attribute, and a declaration attribute. */
16568 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16569 || !die_is_declaration (die, cu))
16570 new_symbol (die, type, cu);
16571 }
16572
16573 /* Assuming DIE is an enumeration type, and TYPE is its associated type,
16574 update TYPE using some information only available in DIE's children. */
16575
16576 static void
16577 update_enumeration_type_from_children (struct die_info *die,
16578 struct type *type,
16579 struct dwarf2_cu *cu)
16580 {
16581 struct die_info *child_die;
16582 int unsigned_enum = 1;
16583 int flag_enum = 1;
16584 ULONGEST mask = 0;
16585
16586 auto_obstack obstack;
16587
16588 for (child_die = die->child;
16589 child_die != NULL && child_die->tag;
16590 child_die = sibling_die (child_die))
16591 {
16592 struct attribute *attr;
16593 LONGEST value;
16594 const gdb_byte *bytes;
16595 struct dwarf2_locexpr_baton *baton;
16596 const char *name;
16597
16598 if (child_die->tag != DW_TAG_enumerator)
16599 continue;
16600
16601 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16602 if (attr == NULL)
16603 continue;
16604
16605 name = dwarf2_name (child_die, cu);
16606 if (name == NULL)
16607 name = "<anonymous enumerator>";
16608
16609 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16610 &value, &bytes, &baton);
16611 if (value < 0)
16612 {
16613 unsigned_enum = 0;
16614 flag_enum = 0;
16615 }
16616 else if ((mask & value) != 0)
16617 flag_enum = 0;
16618 else
16619 mask |= value;
16620
16621 /* If we already know that the enum type is neither unsigned, nor
16622 a flag type, no need to look at the rest of the enumerates. */
16623 if (!unsigned_enum && !flag_enum)
16624 break;
16625 }
16626
16627 if (unsigned_enum)
16628 TYPE_UNSIGNED (type) = 1;
16629 if (flag_enum)
16630 TYPE_FLAG_ENUM (type) = 1;
16631 }
16632
16633 /* Given a DW_AT_enumeration_type die, set its type. We do not
16634 complete the type's fields yet, or create any symbols. */
16635
16636 static struct type *
16637 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
16638 {
16639 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16640 struct type *type;
16641 struct attribute *attr;
16642 const char *name;
16643
16644 /* If the definition of this type lives in .debug_types, read that type.
16645 Don't follow DW_AT_specification though, that will take us back up
16646 the chain and we want to go down. */
16647 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
16648 if (attr)
16649 {
16650 type = get_DW_AT_signature_type (die, attr, cu);
16651
16652 /* The type's CU may not be the same as CU.
16653 Ensure TYPE is recorded with CU in die_type_hash. */
16654 return set_die_type (die, type, cu);
16655 }
16656
16657 type = alloc_type (objfile);
16658
16659 TYPE_CODE (type) = TYPE_CODE_ENUM;
16660 name = dwarf2_full_name (NULL, die, cu);
16661 if (name != NULL)
16662 TYPE_TAG_NAME (type) = name;
16663
16664 attr = dwarf2_attr (die, DW_AT_type, cu);
16665 if (attr != NULL)
16666 {
16667 struct type *underlying_type = die_type (die, cu);
16668
16669 TYPE_TARGET_TYPE (type) = underlying_type;
16670 }
16671
16672 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16673 if (attr)
16674 {
16675 TYPE_LENGTH (type) = DW_UNSND (attr);
16676 }
16677 else
16678 {
16679 TYPE_LENGTH (type) = 0;
16680 }
16681
16682 /* The enumeration DIE can be incomplete. In Ada, any type can be
16683 declared as private in the package spec, and then defined only
16684 inside the package body. Such types are known as Taft Amendment
16685 Types. When another package uses such a type, an incomplete DIE
16686 may be generated by the compiler. */
16687 if (die_is_declaration (die, cu))
16688 TYPE_STUB (type) = 1;
16689
16690 /* Finish the creation of this type by using the enum's children.
16691 We must call this even when the underlying type has been provided
16692 so that we can determine if we're looking at a "flag" enum. */
16693 update_enumeration_type_from_children (die, type, cu);
16694
16695 /* If this type has an underlying type that is not a stub, then we
16696 may use its attributes. We always use the "unsigned" attribute
16697 in this situation, because ordinarily we guess whether the type
16698 is unsigned -- but the guess can be wrong and the underlying type
16699 can tell us the reality. However, we defer to a local size
16700 attribute if one exists, because this lets the compiler override
16701 the underlying type if needed. */
16702 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16703 {
16704 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
16705 if (TYPE_LENGTH (type) == 0)
16706 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
16707 }
16708
16709 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16710
16711 return set_die_type (die, type, cu);
16712 }
16713
16714 /* Given a pointer to a die which begins an enumeration, process all
16715 the dies that define the members of the enumeration, and create the
16716 symbol for the enumeration type.
16717
16718 NOTE: We reverse the order of the element list. */
16719
16720 static void
16721 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16722 {
16723 struct type *this_type;
16724
16725 this_type = get_die_type (die, cu);
16726 if (this_type == NULL)
16727 this_type = read_enumeration_type (die, cu);
16728
16729 if (die->child != NULL)
16730 {
16731 struct die_info *child_die;
16732 struct symbol *sym;
16733 struct field *fields = NULL;
16734 int num_fields = 0;
16735 const char *name;
16736
16737 child_die = die->child;
16738 while (child_die && child_die->tag)
16739 {
16740 if (child_die->tag != DW_TAG_enumerator)
16741 {
16742 process_die (child_die, cu);
16743 }
16744 else
16745 {
16746 name = dwarf2_name (child_die, cu);
16747 if (name)
16748 {
16749 sym = new_symbol (child_die, this_type, cu);
16750
16751 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
16752 {
16753 fields = (struct field *)
16754 xrealloc (fields,
16755 (num_fields + DW_FIELD_ALLOC_CHUNK)
16756 * sizeof (struct field));
16757 }
16758
16759 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
16760 FIELD_TYPE (fields[num_fields]) = NULL;
16761 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
16762 FIELD_BITSIZE (fields[num_fields]) = 0;
16763
16764 num_fields++;
16765 }
16766 }
16767
16768 child_die = sibling_die (child_die);
16769 }
16770
16771 if (num_fields)
16772 {
16773 TYPE_NFIELDS (this_type) = num_fields;
16774 TYPE_FIELDS (this_type) = (struct field *)
16775 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
16776 memcpy (TYPE_FIELDS (this_type), fields,
16777 sizeof (struct field) * num_fields);
16778 xfree (fields);
16779 }
16780 }
16781
16782 /* If we are reading an enum from a .debug_types unit, and the enum
16783 is a declaration, and the enum is not the signatured type in the
16784 unit, then we do not want to add a symbol for it. Adding a
16785 symbol would in some cases obscure the true definition of the
16786 enum, giving users an incomplete type when the definition is
16787 actually available. Note that we do not want to do this for all
16788 enums which are just declarations, because C++0x allows forward
16789 enum declarations. */
16790 if (cu->per_cu->is_debug_types
16791 && die_is_declaration (die, cu))
16792 {
16793 struct signatured_type *sig_type;
16794
16795 sig_type = (struct signatured_type *) cu->per_cu;
16796 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16797 if (sig_type->type_offset_in_section != die->sect_off)
16798 return;
16799 }
16800
16801 new_symbol (die, this_type, cu);
16802 }
16803
16804 /* Extract all information from a DW_TAG_array_type DIE and put it in
16805 the DIE's type field. For now, this only handles one dimensional
16806 arrays. */
16807
16808 static struct type *
16809 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
16810 {
16811 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16812 struct die_info *child_die;
16813 struct type *type;
16814 struct type *element_type, *range_type, *index_type;
16815 struct attribute *attr;
16816 const char *name;
16817 struct dynamic_prop *byte_stride_prop = NULL;
16818 unsigned int bit_stride = 0;
16819
16820 element_type = die_type (die, cu);
16821
16822 /* The die_type call above may have already set the type for this DIE. */
16823 type = get_die_type (die, cu);
16824 if (type)
16825 return type;
16826
16827 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16828 if (attr != NULL)
16829 {
16830 int stride_ok;
16831
16832 byte_stride_prop
16833 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
16834 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop);
16835 if (!stride_ok)
16836 {
16837 complaint (&symfile_complaints,
16838 _("unable to read array DW_AT_byte_stride "
16839 " - DIE at %s [in module %s]"),
16840 sect_offset_str (die->sect_off),
16841 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16842 /* Ignore this attribute. We will likely not be able to print
16843 arrays of this type correctly, but there is little we can do
16844 to help if we cannot read the attribute's value. */
16845 byte_stride_prop = NULL;
16846 }
16847 }
16848
16849 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16850 if (attr != NULL)
16851 bit_stride = DW_UNSND (attr);
16852
16853 /* Irix 6.2 native cc creates array types without children for
16854 arrays with unspecified length. */
16855 if (die->child == NULL)
16856 {
16857 index_type = objfile_type (objfile)->builtin_int;
16858 range_type = create_static_range_type (NULL, index_type, 0, -1);
16859 type = create_array_type_with_stride (NULL, element_type, range_type,
16860 byte_stride_prop, bit_stride);
16861 return set_die_type (die, type, cu);
16862 }
16863
16864 std::vector<struct type *> range_types;
16865 child_die = die->child;
16866 while (child_die && child_die->tag)
16867 {
16868 if (child_die->tag == DW_TAG_subrange_type)
16869 {
16870 struct type *child_type = read_type_die (child_die, cu);
16871
16872 if (child_type != NULL)
16873 {
16874 /* The range type was succesfully read. Save it for the
16875 array type creation. */
16876 range_types.push_back (child_type);
16877 }
16878 }
16879 child_die = sibling_die (child_die);
16880 }
16881
16882 /* Dwarf2 dimensions are output from left to right, create the
16883 necessary array types in backwards order. */
16884
16885 type = element_type;
16886
16887 if (read_array_order (die, cu) == DW_ORD_col_major)
16888 {
16889 int i = 0;
16890
16891 while (i < range_types.size ())
16892 type = create_array_type_with_stride (NULL, type, range_types[i++],
16893 byte_stride_prop, bit_stride);
16894 }
16895 else
16896 {
16897 size_t ndim = range_types.size ();
16898 while (ndim-- > 0)
16899 type = create_array_type_with_stride (NULL, type, range_types[ndim],
16900 byte_stride_prop, bit_stride);
16901 }
16902
16903 /* Understand Dwarf2 support for vector types (like they occur on
16904 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16905 array type. This is not part of the Dwarf2/3 standard yet, but a
16906 custom vendor extension. The main difference between a regular
16907 array and the vector variant is that vectors are passed by value
16908 to functions. */
16909 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
16910 if (attr)
16911 make_vector_type (type);
16912
16913 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16914 implementation may choose to implement triple vectors using this
16915 attribute. */
16916 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16917 if (attr)
16918 {
16919 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16920 TYPE_LENGTH (type) = DW_UNSND (attr);
16921 else
16922 complaint (&symfile_complaints,
16923 _("DW_AT_byte_size for array type smaller "
16924 "than the total size of elements"));
16925 }
16926
16927 name = dwarf2_name (die, cu);
16928 if (name)
16929 TYPE_NAME (type) = name;
16930
16931 /* Install the type in the die. */
16932 set_die_type (die, type, cu);
16933
16934 /* set_die_type should be already done. */
16935 set_descriptive_type (type, die, cu);
16936
16937 return type;
16938 }
16939
16940 static enum dwarf_array_dim_ordering
16941 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
16942 {
16943 struct attribute *attr;
16944
16945 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16946
16947 if (attr)
16948 return (enum dwarf_array_dim_ordering) DW_SND (attr);
16949
16950 /* GNU F77 is a special case, as at 08/2004 array type info is the
16951 opposite order to the dwarf2 specification, but data is still
16952 laid out as per normal fortran.
16953
16954 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16955 version checking. */
16956
16957 if (cu->language == language_fortran
16958 && cu->producer && strstr (cu->producer, "GNU F77"))
16959 {
16960 return DW_ORD_row_major;
16961 }
16962
16963 switch (cu->language_defn->la_array_ordering)
16964 {
16965 case array_column_major:
16966 return DW_ORD_col_major;
16967 case array_row_major:
16968 default:
16969 return DW_ORD_row_major;
16970 };
16971 }
16972
16973 /* Extract all information from a DW_TAG_set_type DIE and put it in
16974 the DIE's type field. */
16975
16976 static struct type *
16977 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16978 {
16979 struct type *domain_type, *set_type;
16980 struct attribute *attr;
16981
16982 domain_type = die_type (die, cu);
16983
16984 /* The die_type call above may have already set the type for this DIE. */
16985 set_type = get_die_type (die, cu);
16986 if (set_type)
16987 return set_type;
16988
16989 set_type = create_set_type (NULL, domain_type);
16990
16991 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16992 if (attr)
16993 TYPE_LENGTH (set_type) = DW_UNSND (attr);
16994
16995 return set_die_type (die, set_type, cu);
16996 }
16997
16998 /* A helper for read_common_block that creates a locexpr baton.
16999 SYM is the symbol which we are marking as computed.
17000 COMMON_DIE is the DIE for the common block.
17001 COMMON_LOC is the location expression attribute for the common
17002 block itself.
17003 MEMBER_LOC is the location expression attribute for the particular
17004 member of the common block that we are processing.
17005 CU is the CU from which the above come. */
17006
17007 static void
17008 mark_common_block_symbol_computed (struct symbol *sym,
17009 struct die_info *common_die,
17010 struct attribute *common_loc,
17011 struct attribute *member_loc,
17012 struct dwarf2_cu *cu)
17013 {
17014 struct dwarf2_per_objfile *dwarf2_per_objfile
17015 = cu->per_cu->dwarf2_per_objfile;
17016 struct objfile *objfile = dwarf2_per_objfile->objfile;
17017 struct dwarf2_locexpr_baton *baton;
17018 gdb_byte *ptr;
17019 unsigned int cu_off;
17020 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
17021 LONGEST offset = 0;
17022
17023 gdb_assert (common_loc && member_loc);
17024 gdb_assert (attr_form_is_block (common_loc));
17025 gdb_assert (attr_form_is_block (member_loc)
17026 || attr_form_is_constant (member_loc));
17027
17028 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
17029 baton->per_cu = cu->per_cu;
17030 gdb_assert (baton->per_cu);
17031
17032 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
17033
17034 if (attr_form_is_constant (member_loc))
17035 {
17036 offset = dwarf2_get_attr_constant_value (member_loc, 0);
17037 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
17038 }
17039 else
17040 baton->size += DW_BLOCK (member_loc)->size;
17041
17042 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
17043 baton->data = ptr;
17044
17045 *ptr++ = DW_OP_call4;
17046 cu_off = common_die->sect_off - cu->per_cu->sect_off;
17047 store_unsigned_integer (ptr, 4, byte_order, cu_off);
17048 ptr += 4;
17049
17050 if (attr_form_is_constant (member_loc))
17051 {
17052 *ptr++ = DW_OP_addr;
17053 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
17054 ptr += cu->header.addr_size;
17055 }
17056 else
17057 {
17058 /* We have to copy the data here, because DW_OP_call4 will only
17059 use a DW_AT_location attribute. */
17060 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
17061 ptr += DW_BLOCK (member_loc)->size;
17062 }
17063
17064 *ptr++ = DW_OP_plus;
17065 gdb_assert (ptr - baton->data == baton->size);
17066
17067 SYMBOL_LOCATION_BATON (sym) = baton;
17068 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
17069 }
17070
17071 /* Create appropriate locally-scoped variables for all the
17072 DW_TAG_common_block entries. Also create a struct common_block
17073 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
17074 is used to sepate the common blocks name namespace from regular
17075 variable names. */
17076
17077 static void
17078 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
17079 {
17080 struct attribute *attr;
17081
17082 attr = dwarf2_attr (die, DW_AT_location, cu);
17083 if (attr)
17084 {
17085 /* Support the .debug_loc offsets. */
17086 if (attr_form_is_block (attr))
17087 {
17088 /* Ok. */
17089 }
17090 else if (attr_form_is_section_offset (attr))
17091 {
17092 dwarf2_complex_location_expr_complaint ();
17093 attr = NULL;
17094 }
17095 else
17096 {
17097 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17098 "common block member");
17099 attr = NULL;
17100 }
17101 }
17102
17103 if (die->child != NULL)
17104 {
17105 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17106 struct die_info *child_die;
17107 size_t n_entries = 0, size;
17108 struct common_block *common_block;
17109 struct symbol *sym;
17110
17111 for (child_die = die->child;
17112 child_die && child_die->tag;
17113 child_die = sibling_die (child_die))
17114 ++n_entries;
17115
17116 size = (sizeof (struct common_block)
17117 + (n_entries - 1) * sizeof (struct symbol *));
17118 common_block
17119 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
17120 size);
17121 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
17122 common_block->n_entries = 0;
17123
17124 for (child_die = die->child;
17125 child_die && child_die->tag;
17126 child_die = sibling_die (child_die))
17127 {
17128 /* Create the symbol in the DW_TAG_common_block block in the current
17129 symbol scope. */
17130 sym = new_symbol (child_die, NULL, cu);
17131 if (sym != NULL)
17132 {
17133 struct attribute *member_loc;
17134
17135 common_block->contents[common_block->n_entries++] = sym;
17136
17137 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
17138 cu);
17139 if (member_loc)
17140 {
17141 /* GDB has handled this for a long time, but it is
17142 not specified by DWARF. It seems to have been
17143 emitted by gfortran at least as recently as:
17144 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
17145 complaint (&symfile_complaints,
17146 _("Variable in common block has "
17147 "DW_AT_data_member_location "
17148 "- DIE at %s [in module %s]"),
17149 sect_offset_str (child_die->sect_off),
17150 objfile_name (objfile));
17151
17152 if (attr_form_is_section_offset (member_loc))
17153 dwarf2_complex_location_expr_complaint ();
17154 else if (attr_form_is_constant (member_loc)
17155 || attr_form_is_block (member_loc))
17156 {
17157 if (attr)
17158 mark_common_block_symbol_computed (sym, die, attr,
17159 member_loc, cu);
17160 }
17161 else
17162 dwarf2_complex_location_expr_complaint ();
17163 }
17164 }
17165 }
17166
17167 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
17168 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
17169 }
17170 }
17171
17172 /* Create a type for a C++ namespace. */
17173
17174 static struct type *
17175 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
17176 {
17177 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17178 const char *previous_prefix, *name;
17179 int is_anonymous;
17180 struct type *type;
17181
17182 /* For extensions, reuse the type of the original namespace. */
17183 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
17184 {
17185 struct die_info *ext_die;
17186 struct dwarf2_cu *ext_cu = cu;
17187
17188 ext_die = dwarf2_extension (die, &ext_cu);
17189 type = read_type_die (ext_die, ext_cu);
17190
17191 /* EXT_CU may not be the same as CU.
17192 Ensure TYPE is recorded with CU in die_type_hash. */
17193 return set_die_type (die, type, cu);
17194 }
17195
17196 name = namespace_name (die, &is_anonymous, cu);
17197
17198 /* Now build the name of the current namespace. */
17199
17200 previous_prefix = determine_prefix (die, cu);
17201 if (previous_prefix[0] != '\0')
17202 name = typename_concat (&objfile->objfile_obstack,
17203 previous_prefix, name, 0, cu);
17204
17205 /* Create the type. */
17206 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
17207 TYPE_TAG_NAME (type) = TYPE_NAME (type);
17208
17209 return set_die_type (die, type, cu);
17210 }
17211
17212 /* Read a namespace scope. */
17213
17214 static void
17215 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
17216 {
17217 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17218 int is_anonymous;
17219
17220 /* Add a symbol associated to this if we haven't seen the namespace
17221 before. Also, add a using directive if it's an anonymous
17222 namespace. */
17223
17224 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
17225 {
17226 struct type *type;
17227
17228 type = read_type_die (die, cu);
17229 new_symbol (die, type, cu);
17230
17231 namespace_name (die, &is_anonymous, cu);
17232 if (is_anonymous)
17233 {
17234 const char *previous_prefix = determine_prefix (die, cu);
17235
17236 std::vector<const char *> excludes;
17237 add_using_directive (using_directives (cu->language),
17238 previous_prefix, TYPE_NAME (type), NULL,
17239 NULL, excludes, 0, &objfile->objfile_obstack);
17240 }
17241 }
17242
17243 if (die->child != NULL)
17244 {
17245 struct die_info *child_die = die->child;
17246
17247 while (child_die && child_die->tag)
17248 {
17249 process_die (child_die, cu);
17250 child_die = sibling_die (child_die);
17251 }
17252 }
17253 }
17254
17255 /* Read a Fortran module as type. This DIE can be only a declaration used for
17256 imported module. Still we need that type as local Fortran "use ... only"
17257 declaration imports depend on the created type in determine_prefix. */
17258
17259 static struct type *
17260 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
17261 {
17262 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17263 const char *module_name;
17264 struct type *type;
17265
17266 module_name = dwarf2_name (die, cu);
17267 if (!module_name)
17268 complaint (&symfile_complaints,
17269 _("DW_TAG_module has no name, offset %s"),
17270 sect_offset_str (die->sect_off));
17271 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
17272
17273 /* determine_prefix uses TYPE_TAG_NAME. */
17274 TYPE_TAG_NAME (type) = TYPE_NAME (type);
17275
17276 return set_die_type (die, type, cu);
17277 }
17278
17279 /* Read a Fortran module. */
17280
17281 static void
17282 read_module (struct die_info *die, struct dwarf2_cu *cu)
17283 {
17284 struct die_info *child_die = die->child;
17285 struct type *type;
17286
17287 type = read_type_die (die, cu);
17288 new_symbol (die, type, cu);
17289
17290 while (child_die && child_die->tag)
17291 {
17292 process_die (child_die, cu);
17293 child_die = sibling_die (child_die);
17294 }
17295 }
17296
17297 /* Return the name of the namespace represented by DIE. Set
17298 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
17299 namespace. */
17300
17301 static const char *
17302 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
17303 {
17304 struct die_info *current_die;
17305 const char *name = NULL;
17306
17307 /* Loop through the extensions until we find a name. */
17308
17309 for (current_die = die;
17310 current_die != NULL;
17311 current_die = dwarf2_extension (die, &cu))
17312 {
17313 /* We don't use dwarf2_name here so that we can detect the absence
17314 of a name -> anonymous namespace. */
17315 name = dwarf2_string_attr (die, DW_AT_name, cu);
17316
17317 if (name != NULL)
17318 break;
17319 }
17320
17321 /* Is it an anonymous namespace? */
17322
17323 *is_anonymous = (name == NULL);
17324 if (*is_anonymous)
17325 name = CP_ANONYMOUS_NAMESPACE_STR;
17326
17327 return name;
17328 }
17329
17330 /* Extract all information from a DW_TAG_pointer_type DIE and add to
17331 the user defined type vector. */
17332
17333 static struct type *
17334 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
17335 {
17336 struct gdbarch *gdbarch
17337 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
17338 struct comp_unit_head *cu_header = &cu->header;
17339 struct type *type;
17340 struct attribute *attr_byte_size;
17341 struct attribute *attr_address_class;
17342 int byte_size, addr_class;
17343 struct type *target_type;
17344
17345 target_type = die_type (die, cu);
17346
17347 /* The die_type call above may have already set the type for this DIE. */
17348 type = get_die_type (die, cu);
17349 if (type)
17350 return type;
17351
17352 type = lookup_pointer_type (target_type);
17353
17354 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
17355 if (attr_byte_size)
17356 byte_size = DW_UNSND (attr_byte_size);
17357 else
17358 byte_size = cu_header->addr_size;
17359
17360 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
17361 if (attr_address_class)
17362 addr_class = DW_UNSND (attr_address_class);
17363 else
17364 addr_class = DW_ADDR_none;
17365
17366 /* If the pointer size or address class is different than the
17367 default, create a type variant marked as such and set the
17368 length accordingly. */
17369 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
17370 {
17371 if (gdbarch_address_class_type_flags_p (gdbarch))
17372 {
17373 int type_flags;
17374
17375 type_flags = gdbarch_address_class_type_flags
17376 (gdbarch, byte_size, addr_class);
17377 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
17378 == 0);
17379 type = make_type_with_address_space (type, type_flags);
17380 }
17381 else if (TYPE_LENGTH (type) != byte_size)
17382 {
17383 complaint (&symfile_complaints,
17384 _("invalid pointer size %d"), byte_size);
17385 }
17386 else
17387 {
17388 /* Should we also complain about unhandled address classes? */
17389 }
17390 }
17391
17392 TYPE_LENGTH (type) = byte_size;
17393 return set_die_type (die, type, cu);
17394 }
17395
17396 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
17397 the user defined type vector. */
17398
17399 static struct type *
17400 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
17401 {
17402 struct type *type;
17403 struct type *to_type;
17404 struct type *domain;
17405
17406 to_type = die_type (die, cu);
17407 domain = die_containing_type (die, cu);
17408
17409 /* The calls above may have already set the type for this DIE. */
17410 type = get_die_type (die, cu);
17411 if (type)
17412 return type;
17413
17414 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
17415 type = lookup_methodptr_type (to_type);
17416 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
17417 {
17418 struct type *new_type
17419 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
17420
17421 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
17422 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
17423 TYPE_VARARGS (to_type));
17424 type = lookup_methodptr_type (new_type);
17425 }
17426 else
17427 type = lookup_memberptr_type (to_type, domain);
17428
17429 return set_die_type (die, type, cu);
17430 }
17431
17432 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
17433 the user defined type vector. */
17434
17435 static struct type *
17436 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
17437 enum type_code refcode)
17438 {
17439 struct comp_unit_head *cu_header = &cu->header;
17440 struct type *type, *target_type;
17441 struct attribute *attr;
17442
17443 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17444
17445 target_type = die_type (die, cu);
17446
17447 /* The die_type call above may have already set the type for this DIE. */
17448 type = get_die_type (die, cu);
17449 if (type)
17450 return type;
17451
17452 type = lookup_reference_type (target_type, refcode);
17453 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17454 if (attr)
17455 {
17456 TYPE_LENGTH (type) = DW_UNSND (attr);
17457 }
17458 else
17459 {
17460 TYPE_LENGTH (type) = cu_header->addr_size;
17461 }
17462 return set_die_type (die, type, cu);
17463 }
17464
17465 /* Add the given cv-qualifiers to the element type of the array. GCC
17466 outputs DWARF type qualifiers that apply to an array, not the
17467 element type. But GDB relies on the array element type to carry
17468 the cv-qualifiers. This mimics section 6.7.3 of the C99
17469 specification. */
17470
17471 static struct type *
17472 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17473 struct type *base_type, int cnst, int voltl)
17474 {
17475 struct type *el_type, *inner_array;
17476
17477 base_type = copy_type (base_type);
17478 inner_array = base_type;
17479
17480 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
17481 {
17482 TYPE_TARGET_TYPE (inner_array) =
17483 copy_type (TYPE_TARGET_TYPE (inner_array));
17484 inner_array = TYPE_TARGET_TYPE (inner_array);
17485 }
17486
17487 el_type = TYPE_TARGET_TYPE (inner_array);
17488 cnst |= TYPE_CONST (el_type);
17489 voltl |= TYPE_VOLATILE (el_type);
17490 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17491
17492 return set_die_type (die, base_type, cu);
17493 }
17494
17495 static struct type *
17496 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
17497 {
17498 struct type *base_type, *cv_type;
17499
17500 base_type = die_type (die, cu);
17501
17502 /* The die_type call above may have already set the type for this DIE. */
17503 cv_type = get_die_type (die, cu);
17504 if (cv_type)
17505 return cv_type;
17506
17507 /* In case the const qualifier is applied to an array type, the element type
17508 is so qualified, not the array type (section 6.7.3 of C99). */
17509 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17510 return add_array_cv_type (die, cu, base_type, 1, 0);
17511
17512 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17513 return set_die_type (die, cv_type, cu);
17514 }
17515
17516 static struct type *
17517 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
17518 {
17519 struct type *base_type, *cv_type;
17520
17521 base_type = die_type (die, cu);
17522
17523 /* The die_type call above may have already set the type for this DIE. */
17524 cv_type = get_die_type (die, cu);
17525 if (cv_type)
17526 return cv_type;
17527
17528 /* In case the volatile qualifier is applied to an array type, the
17529 element type is so qualified, not the array type (section 6.7.3
17530 of C99). */
17531 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17532 return add_array_cv_type (die, cu, base_type, 0, 1);
17533
17534 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17535 return set_die_type (die, cv_type, cu);
17536 }
17537
17538 /* Handle DW_TAG_restrict_type. */
17539
17540 static struct type *
17541 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17542 {
17543 struct type *base_type, *cv_type;
17544
17545 base_type = die_type (die, cu);
17546
17547 /* The die_type call above may have already set the type for this DIE. */
17548 cv_type = get_die_type (die, cu);
17549 if (cv_type)
17550 return cv_type;
17551
17552 cv_type = make_restrict_type (base_type);
17553 return set_die_type (die, cv_type, cu);
17554 }
17555
17556 /* Handle DW_TAG_atomic_type. */
17557
17558 static struct type *
17559 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17560 {
17561 struct type *base_type, *cv_type;
17562
17563 base_type = die_type (die, cu);
17564
17565 /* The die_type call above may have already set the type for this DIE. */
17566 cv_type = get_die_type (die, cu);
17567 if (cv_type)
17568 return cv_type;
17569
17570 cv_type = make_atomic_type (base_type);
17571 return set_die_type (die, cv_type, cu);
17572 }
17573
17574 /* Extract all information from a DW_TAG_string_type DIE and add to
17575 the user defined type vector. It isn't really a user defined type,
17576 but it behaves like one, with other DIE's using an AT_user_def_type
17577 attribute to reference it. */
17578
17579 static struct type *
17580 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
17581 {
17582 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17583 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17584 struct type *type, *range_type, *index_type, *char_type;
17585 struct attribute *attr;
17586 unsigned int length;
17587
17588 attr = dwarf2_attr (die, DW_AT_string_length, cu);
17589 if (attr)
17590 {
17591 length = DW_UNSND (attr);
17592 }
17593 else
17594 {
17595 /* Check for the DW_AT_byte_size attribute. */
17596 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17597 if (attr)
17598 {
17599 length = DW_UNSND (attr);
17600 }
17601 else
17602 {
17603 length = 1;
17604 }
17605 }
17606
17607 index_type = objfile_type (objfile)->builtin_int;
17608 range_type = create_static_range_type (NULL, index_type, 1, length);
17609 char_type = language_string_char_type (cu->language_defn, gdbarch);
17610 type = create_string_type (NULL, char_type, range_type);
17611
17612 return set_die_type (die, type, cu);
17613 }
17614
17615 /* Assuming that DIE corresponds to a function, returns nonzero
17616 if the function is prototyped. */
17617
17618 static int
17619 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17620 {
17621 struct attribute *attr;
17622
17623 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17624 if (attr && (DW_UNSND (attr) != 0))
17625 return 1;
17626
17627 /* The DWARF standard implies that the DW_AT_prototyped attribute
17628 is only meaninful for C, but the concept also extends to other
17629 languages that allow unprototyped functions (Eg: Objective C).
17630 For all other languages, assume that functions are always
17631 prototyped. */
17632 if (cu->language != language_c
17633 && cu->language != language_objc
17634 && cu->language != language_opencl)
17635 return 1;
17636
17637 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17638 prototyped and unprototyped functions; default to prototyped,
17639 since that is more common in modern code (and RealView warns
17640 about unprototyped functions). */
17641 if (producer_is_realview (cu->producer))
17642 return 1;
17643
17644 return 0;
17645 }
17646
17647 /* Handle DIES due to C code like:
17648
17649 struct foo
17650 {
17651 int (*funcp)(int a, long l);
17652 int b;
17653 };
17654
17655 ('funcp' generates a DW_TAG_subroutine_type DIE). */
17656
17657 static struct type *
17658 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
17659 {
17660 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17661 struct type *type; /* Type that this function returns. */
17662 struct type *ftype; /* Function that returns above type. */
17663 struct attribute *attr;
17664
17665 type = die_type (die, cu);
17666
17667 /* The die_type call above may have already set the type for this DIE. */
17668 ftype = get_die_type (die, cu);
17669 if (ftype)
17670 return ftype;
17671
17672 ftype = lookup_function_type (type);
17673
17674 if (prototyped_function_p (die, cu))
17675 TYPE_PROTOTYPED (ftype) = 1;
17676
17677 /* Store the calling convention in the type if it's available in
17678 the subroutine die. Otherwise set the calling convention to
17679 the default value DW_CC_normal. */
17680 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
17681 if (attr)
17682 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
17683 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17684 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17685 else
17686 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
17687
17688 /* Record whether the function returns normally to its caller or not
17689 if the DWARF producer set that information. */
17690 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17691 if (attr && (DW_UNSND (attr) != 0))
17692 TYPE_NO_RETURN (ftype) = 1;
17693
17694 /* We need to add the subroutine type to the die immediately so
17695 we don't infinitely recurse when dealing with parameters
17696 declared as the same subroutine type. */
17697 set_die_type (die, ftype, cu);
17698
17699 if (die->child != NULL)
17700 {
17701 struct type *void_type = objfile_type (objfile)->builtin_void;
17702 struct die_info *child_die;
17703 int nparams, iparams;
17704
17705 /* Count the number of parameters.
17706 FIXME: GDB currently ignores vararg functions, but knows about
17707 vararg member functions. */
17708 nparams = 0;
17709 child_die = die->child;
17710 while (child_die && child_die->tag)
17711 {
17712 if (child_die->tag == DW_TAG_formal_parameter)
17713 nparams++;
17714 else if (child_die->tag == DW_TAG_unspecified_parameters)
17715 TYPE_VARARGS (ftype) = 1;
17716 child_die = sibling_die (child_die);
17717 }
17718
17719 /* Allocate storage for parameters and fill them in. */
17720 TYPE_NFIELDS (ftype) = nparams;
17721 TYPE_FIELDS (ftype) = (struct field *)
17722 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
17723
17724 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17725 even if we error out during the parameters reading below. */
17726 for (iparams = 0; iparams < nparams; iparams++)
17727 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17728
17729 iparams = 0;
17730 child_die = die->child;
17731 while (child_die && child_die->tag)
17732 {
17733 if (child_die->tag == DW_TAG_formal_parameter)
17734 {
17735 struct type *arg_type;
17736
17737 /* DWARF version 2 has no clean way to discern C++
17738 static and non-static member functions. G++ helps
17739 GDB by marking the first parameter for non-static
17740 member functions (which is the this pointer) as
17741 artificial. We pass this information to
17742 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17743
17744 DWARF version 3 added DW_AT_object_pointer, which GCC
17745 4.5 does not yet generate. */
17746 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
17747 if (attr)
17748 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17749 else
17750 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
17751 arg_type = die_type (child_die, cu);
17752
17753 /* RealView does not mark THIS as const, which the testsuite
17754 expects. GCC marks THIS as const in method definitions,
17755 but not in the class specifications (GCC PR 43053). */
17756 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17757 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17758 {
17759 int is_this = 0;
17760 struct dwarf2_cu *arg_cu = cu;
17761 const char *name = dwarf2_name (child_die, cu);
17762
17763 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17764 if (attr)
17765 {
17766 /* If the compiler emits this, use it. */
17767 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17768 is_this = 1;
17769 }
17770 else if (name && strcmp (name, "this") == 0)
17771 /* Function definitions will have the argument names. */
17772 is_this = 1;
17773 else if (name == NULL && iparams == 0)
17774 /* Declarations may not have the names, so like
17775 elsewhere in GDB, assume an artificial first
17776 argument is "this". */
17777 is_this = 1;
17778
17779 if (is_this)
17780 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17781 arg_type, 0);
17782 }
17783
17784 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
17785 iparams++;
17786 }
17787 child_die = sibling_die (child_die);
17788 }
17789 }
17790
17791 return ftype;
17792 }
17793
17794 static struct type *
17795 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
17796 {
17797 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17798 const char *name = NULL;
17799 struct type *this_type, *target_type;
17800
17801 name = dwarf2_full_name (NULL, die, cu);
17802 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17803 TYPE_TARGET_STUB (this_type) = 1;
17804 set_die_type (die, this_type, cu);
17805 target_type = die_type (die, cu);
17806 if (target_type != this_type)
17807 TYPE_TARGET_TYPE (this_type) = target_type;
17808 else
17809 {
17810 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17811 spec and cause infinite loops in GDB. */
17812 complaint (&symfile_complaints,
17813 _("Self-referential DW_TAG_typedef "
17814 "- DIE at %s [in module %s]"),
17815 sect_offset_str (die->sect_off), objfile_name (objfile));
17816 TYPE_TARGET_TYPE (this_type) = NULL;
17817 }
17818 return this_type;
17819 }
17820
17821 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17822 (which may be different from NAME) to the architecture back-end to allow
17823 it to guess the correct format if necessary. */
17824
17825 static struct type *
17826 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17827 const char *name_hint)
17828 {
17829 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17830 const struct floatformat **format;
17831 struct type *type;
17832
17833 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17834 if (format)
17835 type = init_float_type (objfile, bits, name, format);
17836 else
17837 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17838
17839 return type;
17840 }
17841
17842 /* Find a representation of a given base type and install
17843 it in the TYPE field of the die. */
17844
17845 static struct type *
17846 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
17847 {
17848 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17849 struct type *type;
17850 struct attribute *attr;
17851 int encoding = 0, bits = 0;
17852 const char *name;
17853
17854 attr = dwarf2_attr (die, DW_AT_encoding, cu);
17855 if (attr)
17856 {
17857 encoding = DW_UNSND (attr);
17858 }
17859 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17860 if (attr)
17861 {
17862 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
17863 }
17864 name = dwarf2_name (die, cu);
17865 if (!name)
17866 {
17867 complaint (&symfile_complaints,
17868 _("DW_AT_name missing from DW_TAG_base_type"));
17869 }
17870
17871 switch (encoding)
17872 {
17873 case DW_ATE_address:
17874 /* Turn DW_ATE_address into a void * pointer. */
17875 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
17876 type = init_pointer_type (objfile, bits, name, type);
17877 break;
17878 case DW_ATE_boolean:
17879 type = init_boolean_type (objfile, bits, 1, name);
17880 break;
17881 case DW_ATE_complex_float:
17882 type = dwarf2_init_float_type (objfile, bits / 2, NULL, name);
17883 type = init_complex_type (objfile, name, type);
17884 break;
17885 case DW_ATE_decimal_float:
17886 type = init_decfloat_type (objfile, bits, name);
17887 break;
17888 case DW_ATE_float:
17889 type = dwarf2_init_float_type (objfile, bits, name, name);
17890 break;
17891 case DW_ATE_signed:
17892 type = init_integer_type (objfile, bits, 0, name);
17893 break;
17894 case DW_ATE_unsigned:
17895 if (cu->language == language_fortran
17896 && name
17897 && startswith (name, "character("))
17898 type = init_character_type (objfile, bits, 1, name);
17899 else
17900 type = init_integer_type (objfile, bits, 1, name);
17901 break;
17902 case DW_ATE_signed_char:
17903 if (cu->language == language_ada || cu->language == language_m2
17904 || cu->language == language_pascal
17905 || cu->language == language_fortran)
17906 type = init_character_type (objfile, bits, 0, name);
17907 else
17908 type = init_integer_type (objfile, bits, 0, name);
17909 break;
17910 case DW_ATE_unsigned_char:
17911 if (cu->language == language_ada || cu->language == language_m2
17912 || cu->language == language_pascal
17913 || cu->language == language_fortran
17914 || cu->language == language_rust)
17915 type = init_character_type (objfile, bits, 1, name);
17916 else
17917 type = init_integer_type (objfile, bits, 1, name);
17918 break;
17919 case DW_ATE_UTF:
17920 {
17921 gdbarch *arch = get_objfile_arch (objfile);
17922
17923 if (bits == 16)
17924 type = builtin_type (arch)->builtin_char16;
17925 else if (bits == 32)
17926 type = builtin_type (arch)->builtin_char32;
17927 else
17928 {
17929 complaint (&symfile_complaints,
17930 _("unsupported DW_ATE_UTF bit size: '%d'"),
17931 bits);
17932 type = init_integer_type (objfile, bits, 1, name);
17933 }
17934 return set_die_type (die, type, cu);
17935 }
17936 break;
17937
17938 default:
17939 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
17940 dwarf_type_encoding_name (encoding));
17941 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17942 break;
17943 }
17944
17945 if (name && strcmp (name, "char") == 0)
17946 TYPE_NOSIGN (type) = 1;
17947
17948 return set_die_type (die, type, cu);
17949 }
17950
17951 /* Parse dwarf attribute if it's a block, reference or constant and put the
17952 resulting value of the attribute into struct bound_prop.
17953 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17954
17955 static int
17956 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17957 struct dwarf2_cu *cu, struct dynamic_prop *prop)
17958 {
17959 struct dwarf2_property_baton *baton;
17960 struct obstack *obstack
17961 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
17962
17963 if (attr == NULL || prop == NULL)
17964 return 0;
17965
17966 if (attr_form_is_block (attr))
17967 {
17968 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17969 baton->referenced_type = NULL;
17970 baton->locexpr.per_cu = cu->per_cu;
17971 baton->locexpr.size = DW_BLOCK (attr)->size;
17972 baton->locexpr.data = DW_BLOCK (attr)->data;
17973 prop->data.baton = baton;
17974 prop->kind = PROP_LOCEXPR;
17975 gdb_assert (prop->data.baton != NULL);
17976 }
17977 else if (attr_form_is_ref (attr))
17978 {
17979 struct dwarf2_cu *target_cu = cu;
17980 struct die_info *target_die;
17981 struct attribute *target_attr;
17982
17983 target_die = follow_die_ref (die, attr, &target_cu);
17984 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
17985 if (target_attr == NULL)
17986 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17987 target_cu);
17988 if (target_attr == NULL)
17989 return 0;
17990
17991 switch (target_attr->name)
17992 {
17993 case DW_AT_location:
17994 if (attr_form_is_section_offset (target_attr))
17995 {
17996 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17997 baton->referenced_type = die_type (target_die, target_cu);
17998 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17999 prop->data.baton = baton;
18000 prop->kind = PROP_LOCLIST;
18001 gdb_assert (prop->data.baton != NULL);
18002 }
18003 else if (attr_form_is_block (target_attr))
18004 {
18005 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18006 baton->referenced_type = die_type (target_die, target_cu);
18007 baton->locexpr.per_cu = cu->per_cu;
18008 baton->locexpr.size = DW_BLOCK (target_attr)->size;
18009 baton->locexpr.data = DW_BLOCK (target_attr)->data;
18010 prop->data.baton = baton;
18011 prop->kind = PROP_LOCEXPR;
18012 gdb_assert (prop->data.baton != NULL);
18013 }
18014 else
18015 {
18016 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18017 "dynamic property");
18018 return 0;
18019 }
18020 break;
18021 case DW_AT_data_member_location:
18022 {
18023 LONGEST offset;
18024
18025 if (!handle_data_member_location (target_die, target_cu,
18026 &offset))
18027 return 0;
18028
18029 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18030 baton->referenced_type = read_type_die (target_die->parent,
18031 target_cu);
18032 baton->offset_info.offset = offset;
18033 baton->offset_info.type = die_type (target_die, target_cu);
18034 prop->data.baton = baton;
18035 prop->kind = PROP_ADDR_OFFSET;
18036 break;
18037 }
18038 }
18039 }
18040 else if (attr_form_is_constant (attr))
18041 {
18042 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
18043 prop->kind = PROP_CONST;
18044 }
18045 else
18046 {
18047 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
18048 dwarf2_name (die, cu));
18049 return 0;
18050 }
18051
18052 return 1;
18053 }
18054
18055 /* Read the given DW_AT_subrange DIE. */
18056
18057 static struct type *
18058 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
18059 {
18060 struct type *base_type, *orig_base_type;
18061 struct type *range_type;
18062 struct attribute *attr;
18063 struct dynamic_prop low, high;
18064 int low_default_is_valid;
18065 int high_bound_is_count = 0;
18066 const char *name;
18067 LONGEST negative_mask;
18068
18069 orig_base_type = die_type (die, cu);
18070 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
18071 whereas the real type might be. So, we use ORIG_BASE_TYPE when
18072 creating the range type, but we use the result of check_typedef
18073 when examining properties of the type. */
18074 base_type = check_typedef (orig_base_type);
18075
18076 /* The die_type call above may have already set the type for this DIE. */
18077 range_type = get_die_type (die, cu);
18078 if (range_type)
18079 return range_type;
18080
18081 low.kind = PROP_CONST;
18082 high.kind = PROP_CONST;
18083 high.data.const_val = 0;
18084
18085 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
18086 omitting DW_AT_lower_bound. */
18087 switch (cu->language)
18088 {
18089 case language_c:
18090 case language_cplus:
18091 low.data.const_val = 0;
18092 low_default_is_valid = 1;
18093 break;
18094 case language_fortran:
18095 low.data.const_val = 1;
18096 low_default_is_valid = 1;
18097 break;
18098 case language_d:
18099 case language_objc:
18100 case language_rust:
18101 low.data.const_val = 0;
18102 low_default_is_valid = (cu->header.version >= 4);
18103 break;
18104 case language_ada:
18105 case language_m2:
18106 case language_pascal:
18107 low.data.const_val = 1;
18108 low_default_is_valid = (cu->header.version >= 4);
18109 break;
18110 default:
18111 low.data.const_val = 0;
18112 low_default_is_valid = 0;
18113 break;
18114 }
18115
18116 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
18117 if (attr)
18118 attr_to_dynamic_prop (attr, die, cu, &low);
18119 else if (!low_default_is_valid)
18120 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
18121 "- DIE at %s [in module %s]"),
18122 sect_offset_str (die->sect_off),
18123 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
18124
18125 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
18126 if (!attr_to_dynamic_prop (attr, die, cu, &high))
18127 {
18128 attr = dwarf2_attr (die, DW_AT_count, cu);
18129 if (attr_to_dynamic_prop (attr, die, cu, &high))
18130 {
18131 /* If bounds are constant do the final calculation here. */
18132 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
18133 high.data.const_val = low.data.const_val + high.data.const_val - 1;
18134 else
18135 high_bound_is_count = 1;
18136 }
18137 }
18138
18139 /* Dwarf-2 specifications explicitly allows to create subrange types
18140 without specifying a base type.
18141 In that case, the base type must be set to the type of
18142 the lower bound, upper bound or count, in that order, if any of these
18143 three attributes references an object that has a type.
18144 If no base type is found, the Dwarf-2 specifications say that
18145 a signed integer type of size equal to the size of an address should
18146 be used.
18147 For the following C code: `extern char gdb_int [];'
18148 GCC produces an empty range DIE.
18149 FIXME: muller/2010-05-28: Possible references to object for low bound,
18150 high bound or count are not yet handled by this code. */
18151 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
18152 {
18153 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18154 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18155 int addr_size = gdbarch_addr_bit (gdbarch) /8;
18156 struct type *int_type = objfile_type (objfile)->builtin_int;
18157
18158 /* Test "int", "long int", and "long long int" objfile types,
18159 and select the first one having a size above or equal to the
18160 architecture address size. */
18161 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
18162 base_type = int_type;
18163 else
18164 {
18165 int_type = objfile_type (objfile)->builtin_long;
18166 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
18167 base_type = int_type;
18168 else
18169 {
18170 int_type = objfile_type (objfile)->builtin_long_long;
18171 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
18172 base_type = int_type;
18173 }
18174 }
18175 }
18176
18177 /* Normally, the DWARF producers are expected to use a signed
18178 constant form (Eg. DW_FORM_sdata) to express negative bounds.
18179 But this is unfortunately not always the case, as witnessed
18180 with GCC, for instance, where the ambiguous DW_FORM_dataN form
18181 is used instead. To work around that ambiguity, we treat
18182 the bounds as signed, and thus sign-extend their values, when
18183 the base type is signed. */
18184 negative_mask =
18185 -((LONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
18186 if (low.kind == PROP_CONST
18187 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
18188 low.data.const_val |= negative_mask;
18189 if (high.kind == PROP_CONST
18190 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
18191 high.data.const_val |= negative_mask;
18192
18193 range_type = create_range_type (NULL, orig_base_type, &low, &high);
18194
18195 if (high_bound_is_count)
18196 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
18197
18198 /* Ada expects an empty array on no boundary attributes. */
18199 if (attr == NULL && cu->language != language_ada)
18200 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
18201
18202 name = dwarf2_name (die, cu);
18203 if (name)
18204 TYPE_NAME (range_type) = name;
18205
18206 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
18207 if (attr)
18208 TYPE_LENGTH (range_type) = DW_UNSND (attr);
18209
18210 set_die_type (die, range_type, cu);
18211
18212 /* set_die_type should be already done. */
18213 set_descriptive_type (range_type, die, cu);
18214
18215 return range_type;
18216 }
18217
18218 static struct type *
18219 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
18220 {
18221 struct type *type;
18222
18223 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
18224 NULL);
18225 TYPE_NAME (type) = dwarf2_name (die, cu);
18226
18227 /* In Ada, an unspecified type is typically used when the description
18228 of the type is defered to a different unit. When encountering
18229 such a type, we treat it as a stub, and try to resolve it later on,
18230 when needed. */
18231 if (cu->language == language_ada)
18232 TYPE_STUB (type) = 1;
18233
18234 return set_die_type (die, type, cu);
18235 }
18236
18237 /* Read a single die and all its descendents. Set the die's sibling
18238 field to NULL; set other fields in the die correctly, and set all
18239 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
18240 location of the info_ptr after reading all of those dies. PARENT
18241 is the parent of the die in question. */
18242
18243 static struct die_info *
18244 read_die_and_children (const struct die_reader_specs *reader,
18245 const gdb_byte *info_ptr,
18246 const gdb_byte **new_info_ptr,
18247 struct die_info *parent)
18248 {
18249 struct die_info *die;
18250 const gdb_byte *cur_ptr;
18251 int has_children;
18252
18253 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
18254 if (die == NULL)
18255 {
18256 *new_info_ptr = cur_ptr;
18257 return NULL;
18258 }
18259 store_in_ref_table (die, reader->cu);
18260
18261 if (has_children)
18262 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
18263 else
18264 {
18265 die->child = NULL;
18266 *new_info_ptr = cur_ptr;
18267 }
18268
18269 die->sibling = NULL;
18270 die->parent = parent;
18271 return die;
18272 }
18273
18274 /* Read a die, all of its descendents, and all of its siblings; set
18275 all of the fields of all of the dies correctly. Arguments are as
18276 in read_die_and_children. */
18277
18278 static struct die_info *
18279 read_die_and_siblings_1 (const struct die_reader_specs *reader,
18280 const gdb_byte *info_ptr,
18281 const gdb_byte **new_info_ptr,
18282 struct die_info *parent)
18283 {
18284 struct die_info *first_die, *last_sibling;
18285 const gdb_byte *cur_ptr;
18286
18287 cur_ptr = info_ptr;
18288 first_die = last_sibling = NULL;
18289
18290 while (1)
18291 {
18292 struct die_info *die
18293 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
18294
18295 if (die == NULL)
18296 {
18297 *new_info_ptr = cur_ptr;
18298 return first_die;
18299 }
18300
18301 if (!first_die)
18302 first_die = die;
18303 else
18304 last_sibling->sibling = die;
18305
18306 last_sibling = die;
18307 }
18308 }
18309
18310 /* Read a die, all of its descendents, and all of its siblings; set
18311 all of the fields of all of the dies correctly. Arguments are as
18312 in read_die_and_children.
18313 This the main entry point for reading a DIE and all its children. */
18314
18315 static struct die_info *
18316 read_die_and_siblings (const struct die_reader_specs *reader,
18317 const gdb_byte *info_ptr,
18318 const gdb_byte **new_info_ptr,
18319 struct die_info *parent)
18320 {
18321 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
18322 new_info_ptr, parent);
18323
18324 if (dwarf_die_debug)
18325 {
18326 fprintf_unfiltered (gdb_stdlog,
18327 "Read die from %s@0x%x of %s:\n",
18328 get_section_name (reader->die_section),
18329 (unsigned) (info_ptr - reader->die_section->buffer),
18330 bfd_get_filename (reader->abfd));
18331 dump_die (die, dwarf_die_debug);
18332 }
18333
18334 return die;
18335 }
18336
18337 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
18338 attributes.
18339 The caller is responsible for filling in the extra attributes
18340 and updating (*DIEP)->num_attrs.
18341 Set DIEP to point to a newly allocated die with its information,
18342 except for its child, sibling, and parent fields.
18343 Set HAS_CHILDREN to tell whether the die has children or not. */
18344
18345 static const gdb_byte *
18346 read_full_die_1 (const struct die_reader_specs *reader,
18347 struct die_info **diep, const gdb_byte *info_ptr,
18348 int *has_children, int num_extra_attrs)
18349 {
18350 unsigned int abbrev_number, bytes_read, i;
18351 struct abbrev_info *abbrev;
18352 struct die_info *die;
18353 struct dwarf2_cu *cu = reader->cu;
18354 bfd *abfd = reader->abfd;
18355
18356 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
18357 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18358 info_ptr += bytes_read;
18359 if (!abbrev_number)
18360 {
18361 *diep = NULL;
18362 *has_children = 0;
18363 return info_ptr;
18364 }
18365
18366 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
18367 if (!abbrev)
18368 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18369 abbrev_number,
18370 bfd_get_filename (abfd));
18371
18372 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
18373 die->sect_off = sect_off;
18374 die->tag = abbrev->tag;
18375 die->abbrev = abbrev_number;
18376
18377 /* Make the result usable.
18378 The caller needs to update num_attrs after adding the extra
18379 attributes. */
18380 die->num_attrs = abbrev->num_attrs;
18381
18382 for (i = 0; i < abbrev->num_attrs; ++i)
18383 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18384 info_ptr);
18385
18386 *diep = die;
18387 *has_children = abbrev->has_children;
18388 return info_ptr;
18389 }
18390
18391 /* Read a die and all its attributes.
18392 Set DIEP to point to a newly allocated die with its information,
18393 except for its child, sibling, and parent fields.
18394 Set HAS_CHILDREN to tell whether the die has children or not. */
18395
18396 static const gdb_byte *
18397 read_full_die (const struct die_reader_specs *reader,
18398 struct die_info **diep, const gdb_byte *info_ptr,
18399 int *has_children)
18400 {
18401 const gdb_byte *result;
18402
18403 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
18404
18405 if (dwarf_die_debug)
18406 {
18407 fprintf_unfiltered (gdb_stdlog,
18408 "Read die from %s@0x%x of %s:\n",
18409 get_section_name (reader->die_section),
18410 (unsigned) (info_ptr - reader->die_section->buffer),
18411 bfd_get_filename (reader->abfd));
18412 dump_die (*diep, dwarf_die_debug);
18413 }
18414
18415 return result;
18416 }
18417 \f
18418 /* Abbreviation tables.
18419
18420 In DWARF version 2, the description of the debugging information is
18421 stored in a separate .debug_abbrev section. Before we read any
18422 dies from a section we read in all abbreviations and install them
18423 in a hash table. */
18424
18425 /* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
18426
18427 struct abbrev_info *
18428 abbrev_table::alloc_abbrev ()
18429 {
18430 struct abbrev_info *abbrev;
18431
18432 abbrev = XOBNEW (&abbrev_obstack, struct abbrev_info);
18433 memset (abbrev, 0, sizeof (struct abbrev_info));
18434
18435 return abbrev;
18436 }
18437
18438 /* Add an abbreviation to the table. */
18439
18440 void
18441 abbrev_table::add_abbrev (unsigned int abbrev_number,
18442 struct abbrev_info *abbrev)
18443 {
18444 unsigned int hash_number;
18445
18446 hash_number = abbrev_number % ABBREV_HASH_SIZE;
18447 abbrev->next = m_abbrevs[hash_number];
18448 m_abbrevs[hash_number] = abbrev;
18449 }
18450
18451 /* Look up an abbrev in the table.
18452 Returns NULL if the abbrev is not found. */
18453
18454 struct abbrev_info *
18455 abbrev_table::lookup_abbrev (unsigned int abbrev_number)
18456 {
18457 unsigned int hash_number;
18458 struct abbrev_info *abbrev;
18459
18460 hash_number = abbrev_number % ABBREV_HASH_SIZE;
18461 abbrev = m_abbrevs[hash_number];
18462
18463 while (abbrev)
18464 {
18465 if (abbrev->number == abbrev_number)
18466 return abbrev;
18467 abbrev = abbrev->next;
18468 }
18469 return NULL;
18470 }
18471
18472 /* Read in an abbrev table. */
18473
18474 static abbrev_table_up
18475 abbrev_table_read_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
18476 struct dwarf2_section_info *section,
18477 sect_offset sect_off)
18478 {
18479 struct objfile *objfile = dwarf2_per_objfile->objfile;
18480 bfd *abfd = get_section_bfd_owner (section);
18481 const gdb_byte *abbrev_ptr;
18482 struct abbrev_info *cur_abbrev;
18483 unsigned int abbrev_number, bytes_read, abbrev_name;
18484 unsigned int abbrev_form;
18485 struct attr_abbrev *cur_attrs;
18486 unsigned int allocated_attrs;
18487
18488 abbrev_table_up abbrev_table (new struct abbrev_table (sect_off));
18489
18490 dwarf2_read_section (objfile, section);
18491 abbrev_ptr = section->buffer + to_underlying (sect_off);
18492 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18493 abbrev_ptr += bytes_read;
18494
18495 allocated_attrs = ATTR_ALLOC_CHUNK;
18496 cur_attrs = XNEWVEC (struct attr_abbrev, allocated_attrs);
18497
18498 /* Loop until we reach an abbrev number of 0. */
18499 while (abbrev_number)
18500 {
18501 cur_abbrev = abbrev_table->alloc_abbrev ();
18502
18503 /* read in abbrev header */
18504 cur_abbrev->number = abbrev_number;
18505 cur_abbrev->tag
18506 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18507 abbrev_ptr += bytes_read;
18508 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
18509 abbrev_ptr += 1;
18510
18511 /* now read in declarations */
18512 for (;;)
18513 {
18514 LONGEST implicit_const;
18515
18516 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18517 abbrev_ptr += bytes_read;
18518 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18519 abbrev_ptr += bytes_read;
18520 if (abbrev_form == DW_FORM_implicit_const)
18521 {
18522 implicit_const = read_signed_leb128 (abfd, abbrev_ptr,
18523 &bytes_read);
18524 abbrev_ptr += bytes_read;
18525 }
18526 else
18527 {
18528 /* Initialize it due to a false compiler warning. */
18529 implicit_const = -1;
18530 }
18531
18532 if (abbrev_name == 0)
18533 break;
18534
18535 if (cur_abbrev->num_attrs == allocated_attrs)
18536 {
18537 allocated_attrs += ATTR_ALLOC_CHUNK;
18538 cur_attrs
18539 = XRESIZEVEC (struct attr_abbrev, cur_attrs, allocated_attrs);
18540 }
18541
18542 cur_attrs[cur_abbrev->num_attrs].name
18543 = (enum dwarf_attribute) abbrev_name;
18544 cur_attrs[cur_abbrev->num_attrs].form
18545 = (enum dwarf_form) abbrev_form;
18546 cur_attrs[cur_abbrev->num_attrs].implicit_const = implicit_const;
18547 ++cur_abbrev->num_attrs;
18548 }
18549
18550 cur_abbrev->attrs =
18551 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
18552 cur_abbrev->num_attrs);
18553 memcpy (cur_abbrev->attrs, cur_attrs,
18554 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
18555
18556 abbrev_table->add_abbrev (abbrev_number, cur_abbrev);
18557
18558 /* Get next abbreviation.
18559 Under Irix6 the abbreviations for a compilation unit are not
18560 always properly terminated with an abbrev number of 0.
18561 Exit loop if we encounter an abbreviation which we have
18562 already read (which means we are about to read the abbreviations
18563 for the next compile unit) or if the end of the abbreviation
18564 table is reached. */
18565 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
18566 break;
18567 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18568 abbrev_ptr += bytes_read;
18569 if (abbrev_table->lookup_abbrev (abbrev_number) != NULL)
18570 break;
18571 }
18572
18573 xfree (cur_attrs);
18574 return abbrev_table;
18575 }
18576
18577 /* Returns nonzero if TAG represents a type that we might generate a partial
18578 symbol for. */
18579
18580 static int
18581 is_type_tag_for_partial (int tag)
18582 {
18583 switch (tag)
18584 {
18585 #if 0
18586 /* Some types that would be reasonable to generate partial symbols for,
18587 that we don't at present. */
18588 case DW_TAG_array_type:
18589 case DW_TAG_file_type:
18590 case DW_TAG_ptr_to_member_type:
18591 case DW_TAG_set_type:
18592 case DW_TAG_string_type:
18593 case DW_TAG_subroutine_type:
18594 #endif
18595 case DW_TAG_base_type:
18596 case DW_TAG_class_type:
18597 case DW_TAG_interface_type:
18598 case DW_TAG_enumeration_type:
18599 case DW_TAG_structure_type:
18600 case DW_TAG_subrange_type:
18601 case DW_TAG_typedef:
18602 case DW_TAG_union_type:
18603 return 1;
18604 default:
18605 return 0;
18606 }
18607 }
18608
18609 /* Load all DIEs that are interesting for partial symbols into memory. */
18610
18611 static struct partial_die_info *
18612 load_partial_dies (const struct die_reader_specs *reader,
18613 const gdb_byte *info_ptr, int building_psymtab)
18614 {
18615 struct dwarf2_cu *cu = reader->cu;
18616 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18617 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
18618 unsigned int bytes_read;
18619 unsigned int load_all = 0;
18620 int nesting_level = 1;
18621
18622 parent_die = NULL;
18623 last_die = NULL;
18624
18625 gdb_assert (cu->per_cu != NULL);
18626 if (cu->per_cu->load_all_dies)
18627 load_all = 1;
18628
18629 cu->partial_dies
18630 = htab_create_alloc_ex (cu->header.length / 12,
18631 partial_die_hash,
18632 partial_die_eq,
18633 NULL,
18634 &cu->comp_unit_obstack,
18635 hashtab_obstack_allocate,
18636 dummy_obstack_deallocate);
18637
18638 while (1)
18639 {
18640 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
18641
18642 /* A NULL abbrev means the end of a series of children. */
18643 if (abbrev == NULL)
18644 {
18645 if (--nesting_level == 0)
18646 return first_die;
18647
18648 info_ptr += bytes_read;
18649 last_die = parent_die;
18650 parent_die = parent_die->die_parent;
18651 continue;
18652 }
18653
18654 /* Check for template arguments. We never save these; if
18655 they're seen, we just mark the parent, and go on our way. */
18656 if (parent_die != NULL
18657 && cu->language == language_cplus
18658 && (abbrev->tag == DW_TAG_template_type_param
18659 || abbrev->tag == DW_TAG_template_value_param))
18660 {
18661 parent_die->has_template_arguments = 1;
18662
18663 if (!load_all)
18664 {
18665 /* We don't need a partial DIE for the template argument. */
18666 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18667 continue;
18668 }
18669 }
18670
18671 /* We only recurse into c++ subprograms looking for template arguments.
18672 Skip their other children. */
18673 if (!load_all
18674 && cu->language == language_cplus
18675 && parent_die != NULL
18676 && parent_die->tag == DW_TAG_subprogram)
18677 {
18678 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18679 continue;
18680 }
18681
18682 /* Check whether this DIE is interesting enough to save. Normally
18683 we would not be interested in members here, but there may be
18684 later variables referencing them via DW_AT_specification (for
18685 static members). */
18686 if (!load_all
18687 && !is_type_tag_for_partial (abbrev->tag)
18688 && abbrev->tag != DW_TAG_constant
18689 && abbrev->tag != DW_TAG_enumerator
18690 && abbrev->tag != DW_TAG_subprogram
18691 && abbrev->tag != DW_TAG_inlined_subroutine
18692 && abbrev->tag != DW_TAG_lexical_block
18693 && abbrev->tag != DW_TAG_variable
18694 && abbrev->tag != DW_TAG_namespace
18695 && abbrev->tag != DW_TAG_module
18696 && abbrev->tag != DW_TAG_member
18697 && abbrev->tag != DW_TAG_imported_unit
18698 && abbrev->tag != DW_TAG_imported_declaration)
18699 {
18700 /* Otherwise we skip to the next sibling, if any. */
18701 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18702 continue;
18703 }
18704
18705 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18706 abbrev);
18707
18708 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
18709
18710 /* This two-pass algorithm for processing partial symbols has a
18711 high cost in cache pressure. Thus, handle some simple cases
18712 here which cover the majority of C partial symbols. DIEs
18713 which neither have specification tags in them, nor could have
18714 specification tags elsewhere pointing at them, can simply be
18715 processed and discarded.
18716
18717 This segment is also optional; scan_partial_symbols and
18718 add_partial_symbol will handle these DIEs if we chain
18719 them in normally. When compilers which do not emit large
18720 quantities of duplicate debug information are more common,
18721 this code can probably be removed. */
18722
18723 /* Any complete simple types at the top level (pretty much all
18724 of them, for a language without namespaces), can be processed
18725 directly. */
18726 if (parent_die == NULL
18727 && pdi.has_specification == 0
18728 && pdi.is_declaration == 0
18729 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18730 || pdi.tag == DW_TAG_base_type
18731 || pdi.tag == DW_TAG_subrange_type))
18732 {
18733 if (building_psymtab && pdi.name != NULL)
18734 add_psymbol_to_list (pdi.name, strlen (pdi.name), 0,
18735 VAR_DOMAIN, LOC_TYPEDEF,
18736 &objfile->static_psymbols,
18737 0, cu->language, objfile);
18738 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18739 continue;
18740 }
18741
18742 /* The exception for DW_TAG_typedef with has_children above is
18743 a workaround of GCC PR debug/47510. In the case of this complaint
18744 type_name_no_tag_or_error will error on such types later.
18745
18746 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18747 it could not find the child DIEs referenced later, this is checked
18748 above. In correct DWARF DW_TAG_typedef should have no children. */
18749
18750 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
18751 complaint (&symfile_complaints,
18752 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
18753 "- DIE at %s [in module %s]"),
18754 sect_offset_str (pdi.sect_off), objfile_name (objfile));
18755
18756 /* If we're at the second level, and we're an enumerator, and
18757 our parent has no specification (meaning possibly lives in a
18758 namespace elsewhere), then we can add the partial symbol now
18759 instead of queueing it. */
18760 if (pdi.tag == DW_TAG_enumerator
18761 && parent_die != NULL
18762 && parent_die->die_parent == NULL
18763 && parent_die->tag == DW_TAG_enumeration_type
18764 && parent_die->has_specification == 0)
18765 {
18766 if (pdi.name == NULL)
18767 complaint (&symfile_complaints,
18768 _("malformed enumerator DIE ignored"));
18769 else if (building_psymtab)
18770 add_psymbol_to_list (pdi.name, strlen (pdi.name), 0,
18771 VAR_DOMAIN, LOC_CONST,
18772 cu->language == language_cplus
18773 ? &objfile->global_psymbols
18774 : &objfile->static_psymbols,
18775 0, cu->language, objfile);
18776
18777 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18778 continue;
18779 }
18780
18781 struct partial_die_info *part_die
18782 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
18783
18784 /* We'll save this DIE so link it in. */
18785 part_die->die_parent = parent_die;
18786 part_die->die_sibling = NULL;
18787 part_die->die_child = NULL;
18788
18789 if (last_die && last_die == parent_die)
18790 last_die->die_child = part_die;
18791 else if (last_die)
18792 last_die->die_sibling = part_die;
18793
18794 last_die = part_die;
18795
18796 if (first_die == NULL)
18797 first_die = part_die;
18798
18799 /* Maybe add the DIE to the hash table. Not all DIEs that we
18800 find interesting need to be in the hash table, because we
18801 also have the parent/sibling/child chains; only those that we
18802 might refer to by offset later during partial symbol reading.
18803
18804 For now this means things that might have be the target of a
18805 DW_AT_specification, DW_AT_abstract_origin, or
18806 DW_AT_extension. DW_AT_extension will refer only to
18807 namespaces; DW_AT_abstract_origin refers to functions (and
18808 many things under the function DIE, but we do not recurse
18809 into function DIEs during partial symbol reading) and
18810 possibly variables as well; DW_AT_specification refers to
18811 declarations. Declarations ought to have the DW_AT_declaration
18812 flag. It happens that GCC forgets to put it in sometimes, but
18813 only for functions, not for types.
18814
18815 Adding more things than necessary to the hash table is harmless
18816 except for the performance cost. Adding too few will result in
18817 wasted time in find_partial_die, when we reread the compilation
18818 unit with load_all_dies set. */
18819
18820 if (load_all
18821 || abbrev->tag == DW_TAG_constant
18822 || abbrev->tag == DW_TAG_subprogram
18823 || abbrev->tag == DW_TAG_variable
18824 || abbrev->tag == DW_TAG_namespace
18825 || part_die->is_declaration)
18826 {
18827 void **slot;
18828
18829 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
18830 to_underlying (part_die->sect_off),
18831 INSERT);
18832 *slot = part_die;
18833 }
18834
18835 /* For some DIEs we want to follow their children (if any). For C
18836 we have no reason to follow the children of structures; for other
18837 languages we have to, so that we can get at method physnames
18838 to infer fully qualified class names, for DW_AT_specification,
18839 and for C++ template arguments. For C++, we also look one level
18840 inside functions to find template arguments (if the name of the
18841 function does not already contain the template arguments).
18842
18843 For Ada, we need to scan the children of subprograms and lexical
18844 blocks as well because Ada allows the definition of nested
18845 entities that could be interesting for the debugger, such as
18846 nested subprograms for instance. */
18847 if (last_die->has_children
18848 && (load_all
18849 || last_die->tag == DW_TAG_namespace
18850 || last_die->tag == DW_TAG_module
18851 || last_die->tag == DW_TAG_enumeration_type
18852 || (cu->language == language_cplus
18853 && last_die->tag == DW_TAG_subprogram
18854 && (last_die->name == NULL
18855 || strchr (last_die->name, '<') == NULL))
18856 || (cu->language != language_c
18857 && (last_die->tag == DW_TAG_class_type
18858 || last_die->tag == DW_TAG_interface_type
18859 || last_die->tag == DW_TAG_structure_type
18860 || last_die->tag == DW_TAG_union_type))
18861 || (cu->language == language_ada
18862 && (last_die->tag == DW_TAG_subprogram
18863 || last_die->tag == DW_TAG_lexical_block))))
18864 {
18865 nesting_level++;
18866 parent_die = last_die;
18867 continue;
18868 }
18869
18870 /* Otherwise we skip to the next sibling, if any. */
18871 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
18872
18873 /* Back to the top, do it again. */
18874 }
18875 }
18876
18877 partial_die_info::partial_die_info (sect_offset sect_off_,
18878 struct abbrev_info *abbrev)
18879 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18880 {
18881 }
18882
18883 /* Read a minimal amount of information into the minimal die structure.
18884 INFO_PTR should point just after the initial uleb128 of a DIE. */
18885
18886 const gdb_byte *
18887 partial_die_info::read (const struct die_reader_specs *reader,
18888 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
18889 {
18890 struct dwarf2_cu *cu = reader->cu;
18891 struct dwarf2_per_objfile *dwarf2_per_objfile
18892 = cu->per_cu->dwarf2_per_objfile;
18893 unsigned int i;
18894 int has_low_pc_attr = 0;
18895 int has_high_pc_attr = 0;
18896 int high_pc_relative = 0;
18897
18898 for (i = 0; i < abbrev.num_attrs; ++i)
18899 {
18900 struct attribute attr;
18901
18902 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i], info_ptr);
18903
18904 /* Store the data if it is of an attribute we want to keep in a
18905 partial symbol table. */
18906 switch (attr.name)
18907 {
18908 case DW_AT_name:
18909 switch (tag)
18910 {
18911 case DW_TAG_compile_unit:
18912 case DW_TAG_partial_unit:
18913 case DW_TAG_type_unit:
18914 /* Compilation units have a DW_AT_name that is a filename, not
18915 a source language identifier. */
18916 case DW_TAG_enumeration_type:
18917 case DW_TAG_enumerator:
18918 /* These tags always have simple identifiers already; no need
18919 to canonicalize them. */
18920 name = DW_STRING (&attr);
18921 break;
18922 default:
18923 {
18924 struct objfile *objfile = dwarf2_per_objfile->objfile;
18925
18926 name
18927 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
18928 &objfile->per_bfd->storage_obstack);
18929 }
18930 break;
18931 }
18932 break;
18933 case DW_AT_linkage_name:
18934 case DW_AT_MIPS_linkage_name:
18935 /* Note that both forms of linkage name might appear. We
18936 assume they will be the same, and we only store the last
18937 one we see. */
18938 if (cu->language == language_ada)
18939 name = DW_STRING (&attr);
18940 linkage_name = DW_STRING (&attr);
18941 break;
18942 case DW_AT_low_pc:
18943 has_low_pc_attr = 1;
18944 lowpc = attr_value_as_address (&attr);
18945 break;
18946 case DW_AT_high_pc:
18947 has_high_pc_attr = 1;
18948 highpc = attr_value_as_address (&attr);
18949 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
18950 high_pc_relative = 1;
18951 break;
18952 case DW_AT_location:
18953 /* Support the .debug_loc offsets. */
18954 if (attr_form_is_block (&attr))
18955 {
18956 d.locdesc = DW_BLOCK (&attr);
18957 }
18958 else if (attr_form_is_section_offset (&attr))
18959 {
18960 dwarf2_complex_location_expr_complaint ();
18961 }
18962 else
18963 {
18964 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18965 "partial symbol information");
18966 }
18967 break;
18968 case DW_AT_external:
18969 is_external = DW_UNSND (&attr);
18970 break;
18971 case DW_AT_declaration:
18972 is_declaration = DW_UNSND (&attr);
18973 break;
18974 case DW_AT_type:
18975 has_type = 1;
18976 break;
18977 case DW_AT_abstract_origin:
18978 case DW_AT_specification:
18979 case DW_AT_extension:
18980 has_specification = 1;
18981 spec_offset = dwarf2_get_ref_die_offset (&attr);
18982 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18983 || cu->per_cu->is_dwz);
18984 break;
18985 case DW_AT_sibling:
18986 /* Ignore absolute siblings, they might point outside of
18987 the current compile unit. */
18988 if (attr.form == DW_FORM_ref_addr)
18989 complaint (&symfile_complaints,
18990 _("ignoring absolute DW_AT_sibling"));
18991 else
18992 {
18993 const gdb_byte *buffer = reader->buffer;
18994 sect_offset off = dwarf2_get_ref_die_offset (&attr);
18995 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
18996
18997 if (sibling_ptr < info_ptr)
18998 complaint (&symfile_complaints,
18999 _("DW_AT_sibling points backwards"));
19000 else if (sibling_ptr > reader->buffer_end)
19001 dwarf2_section_buffer_overflow_complaint (reader->die_section);
19002 else
19003 sibling = sibling_ptr;
19004 }
19005 break;
19006 case DW_AT_byte_size:
19007 has_byte_size = 1;
19008 break;
19009 case DW_AT_const_value:
19010 has_const_value = 1;
19011 break;
19012 case DW_AT_calling_convention:
19013 /* DWARF doesn't provide a way to identify a program's source-level
19014 entry point. DW_AT_calling_convention attributes are only meant
19015 to describe functions' calling conventions.
19016
19017 However, because it's a necessary piece of information in
19018 Fortran, and before DWARF 4 DW_CC_program was the only
19019 piece of debugging information whose definition refers to
19020 a 'main program' at all, several compilers marked Fortran
19021 main programs with DW_CC_program --- even when those
19022 functions use the standard calling conventions.
19023
19024 Although DWARF now specifies a way to provide this
19025 information, we support this practice for backward
19026 compatibility. */
19027 if (DW_UNSND (&attr) == DW_CC_program
19028 && cu->language == language_fortran)
19029 main_subprogram = 1;
19030 break;
19031 case DW_AT_inline:
19032 if (DW_UNSND (&attr) == DW_INL_inlined
19033 || DW_UNSND (&attr) == DW_INL_declared_inlined)
19034 may_be_inlined = 1;
19035 break;
19036
19037 case DW_AT_import:
19038 if (tag == DW_TAG_imported_unit)
19039 {
19040 d.sect_off = dwarf2_get_ref_die_offset (&attr);
19041 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
19042 || cu->per_cu->is_dwz);
19043 }
19044 break;
19045
19046 case DW_AT_main_subprogram:
19047 main_subprogram = DW_UNSND (&attr);
19048 break;
19049
19050 default:
19051 break;
19052 }
19053 }
19054
19055 if (high_pc_relative)
19056 highpc += lowpc;
19057
19058 if (has_low_pc_attr && has_high_pc_attr)
19059 {
19060 /* When using the GNU linker, .gnu.linkonce. sections are used to
19061 eliminate duplicate copies of functions and vtables and such.
19062 The linker will arbitrarily choose one and discard the others.
19063 The AT_*_pc values for such functions refer to local labels in
19064 these sections. If the section from that file was discarded, the
19065 labels are not in the output, so the relocs get a value of 0.
19066 If this is a discarded function, mark the pc bounds as invalid,
19067 so that GDB will ignore it. */
19068 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
19069 {
19070 struct objfile *objfile = dwarf2_per_objfile->objfile;
19071 struct gdbarch *gdbarch = get_objfile_arch (objfile);
19072
19073 complaint (&symfile_complaints,
19074 _("DW_AT_low_pc %s is zero "
19075 "for DIE at %s [in module %s]"),
19076 paddress (gdbarch, lowpc),
19077 sect_offset_str (sect_off),
19078 objfile_name (objfile));
19079 }
19080 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
19081 else if (lowpc >= highpc)
19082 {
19083 struct objfile *objfile = dwarf2_per_objfile->objfile;
19084 struct gdbarch *gdbarch = get_objfile_arch (objfile);
19085
19086 complaint (&symfile_complaints,
19087 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
19088 "for DIE at %s [in module %s]"),
19089 paddress (gdbarch, lowpc),
19090 paddress (gdbarch, highpc),
19091 sect_offset_str (sect_off),
19092 objfile_name (objfile));
19093 }
19094 else
19095 has_pc_info = 1;
19096 }
19097
19098 return info_ptr;
19099 }
19100
19101 /* Find a cached partial DIE at OFFSET in CU. */
19102
19103 struct partial_die_info *
19104 dwarf2_cu::find_partial_die (sect_offset sect_off)
19105 {
19106 struct partial_die_info *lookup_die = NULL;
19107 struct partial_die_info part_die (sect_off);
19108
19109 lookup_die = ((struct partial_die_info *)
19110 htab_find_with_hash (partial_dies, &part_die,
19111 to_underlying (sect_off)));
19112
19113 return lookup_die;
19114 }
19115
19116 /* Find a partial DIE at OFFSET, which may or may not be in CU,
19117 except in the case of .debug_types DIEs which do not reference
19118 outside their CU (they do however referencing other types via
19119 DW_FORM_ref_sig8). */
19120
19121 static struct partial_die_info *
19122 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
19123 {
19124 struct dwarf2_per_objfile *dwarf2_per_objfile
19125 = cu->per_cu->dwarf2_per_objfile;
19126 struct objfile *objfile = dwarf2_per_objfile->objfile;
19127 struct dwarf2_per_cu_data *per_cu = NULL;
19128 struct partial_die_info *pd = NULL;
19129
19130 if (offset_in_dwz == cu->per_cu->is_dwz
19131 && offset_in_cu_p (&cu->header, sect_off))
19132 {
19133 pd = cu->find_partial_die (sect_off);
19134 if (pd != NULL)
19135 return pd;
19136 /* We missed recording what we needed.
19137 Load all dies and try again. */
19138 per_cu = cu->per_cu;
19139 }
19140 else
19141 {
19142 /* TUs don't reference other CUs/TUs (except via type signatures). */
19143 if (cu->per_cu->is_debug_types)
19144 {
19145 error (_("Dwarf Error: Type Unit at offset %s contains"
19146 " external reference to offset %s [in module %s].\n"),
19147 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
19148 bfd_get_filename (objfile->obfd));
19149 }
19150 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
19151 dwarf2_per_objfile);
19152
19153 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
19154 load_partial_comp_unit (per_cu);
19155
19156 per_cu->cu->last_used = 0;
19157 pd = per_cu->cu->find_partial_die (sect_off);
19158 }
19159
19160 /* If we didn't find it, and not all dies have been loaded,
19161 load them all and try again. */
19162
19163 if (pd == NULL && per_cu->load_all_dies == 0)
19164 {
19165 per_cu->load_all_dies = 1;
19166
19167 /* This is nasty. When we reread the DIEs, somewhere up the call chain
19168 THIS_CU->cu may already be in use. So we can't just free it and
19169 replace its DIEs with the ones we read in. Instead, we leave those
19170 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
19171 and clobber THIS_CU->cu->partial_dies with the hash table for the new
19172 set. */
19173 load_partial_comp_unit (per_cu);
19174
19175 pd = per_cu->cu->find_partial_die (sect_off);
19176 }
19177
19178 if (pd == NULL)
19179 internal_error (__FILE__, __LINE__,
19180 _("could not find partial DIE %s "
19181 "in cache [from module %s]\n"),
19182 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
19183 return pd;
19184 }
19185
19186 /* See if we can figure out if the class lives in a namespace. We do
19187 this by looking for a member function; its demangled name will
19188 contain namespace info, if there is any. */
19189
19190 static void
19191 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
19192 struct dwarf2_cu *cu)
19193 {
19194 /* NOTE: carlton/2003-10-07: Getting the info this way changes
19195 what template types look like, because the demangler
19196 frequently doesn't give the same name as the debug info. We
19197 could fix this by only using the demangled name to get the
19198 prefix (but see comment in read_structure_type). */
19199
19200 struct partial_die_info *real_pdi;
19201 struct partial_die_info *child_pdi;
19202
19203 /* If this DIE (this DIE's specification, if any) has a parent, then
19204 we should not do this. We'll prepend the parent's fully qualified
19205 name when we create the partial symbol. */
19206
19207 real_pdi = struct_pdi;
19208 while (real_pdi->has_specification)
19209 real_pdi = find_partial_die (real_pdi->spec_offset,
19210 real_pdi->spec_is_dwz, cu);
19211
19212 if (real_pdi->die_parent != NULL)
19213 return;
19214
19215 for (child_pdi = struct_pdi->die_child;
19216 child_pdi != NULL;
19217 child_pdi = child_pdi->die_sibling)
19218 {
19219 if (child_pdi->tag == DW_TAG_subprogram
19220 && child_pdi->linkage_name != NULL)
19221 {
19222 char *actual_class_name
19223 = language_class_name_from_physname (cu->language_defn,
19224 child_pdi->linkage_name);
19225 if (actual_class_name != NULL)
19226 {
19227 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19228 struct_pdi->name
19229 = ((const char *)
19230 obstack_copy0 (&objfile->per_bfd->storage_obstack,
19231 actual_class_name,
19232 strlen (actual_class_name)));
19233 xfree (actual_class_name);
19234 }
19235 break;
19236 }
19237 }
19238 }
19239
19240 void
19241 partial_die_info::fixup (struct dwarf2_cu *cu)
19242 {
19243 /* Once we've fixed up a die, there's no point in doing so again.
19244 This also avoids a memory leak if we were to call
19245 guess_partial_die_structure_name multiple times. */
19246 if (fixup_called)
19247 return;
19248
19249 /* If we found a reference attribute and the DIE has no name, try
19250 to find a name in the referred to DIE. */
19251
19252 if (name == NULL && has_specification)
19253 {
19254 struct partial_die_info *spec_die;
19255
19256 spec_die = find_partial_die (spec_offset, spec_is_dwz, cu);
19257
19258 spec_die->fixup (cu);
19259
19260 if (spec_die->name)
19261 {
19262 name = spec_die->name;
19263
19264 /* Copy DW_AT_external attribute if it is set. */
19265 if (spec_die->is_external)
19266 is_external = spec_die->is_external;
19267 }
19268 }
19269
19270 /* Set default names for some unnamed DIEs. */
19271
19272 if (name == NULL && tag == DW_TAG_namespace)
19273 name = CP_ANONYMOUS_NAMESPACE_STR;
19274
19275 /* If there is no parent die to provide a namespace, and there are
19276 children, see if we can determine the namespace from their linkage
19277 name. */
19278 if (cu->language == language_cplus
19279 && !VEC_empty (dwarf2_section_info_def,
19280 cu->per_cu->dwarf2_per_objfile->types)
19281 && die_parent == NULL
19282 && has_children
19283 && (tag == DW_TAG_class_type
19284 || tag == DW_TAG_structure_type
19285 || tag == DW_TAG_union_type))
19286 guess_partial_die_structure_name (this, cu);
19287
19288 /* GCC might emit a nameless struct or union that has a linkage
19289 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
19290 if (name == NULL
19291 && (tag == DW_TAG_class_type
19292 || tag == DW_TAG_interface_type
19293 || tag == DW_TAG_structure_type
19294 || tag == DW_TAG_union_type)
19295 && linkage_name != NULL)
19296 {
19297 char *demangled;
19298
19299 demangled = gdb_demangle (linkage_name, DMGL_TYPES);
19300 if (demangled)
19301 {
19302 const char *base;
19303
19304 /* Strip any leading namespaces/classes, keep only the base name.
19305 DW_AT_name for named DIEs does not contain the prefixes. */
19306 base = strrchr (demangled, ':');
19307 if (base && base > demangled && base[-1] == ':')
19308 base++;
19309 else
19310 base = demangled;
19311
19312 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19313 name
19314 = ((const char *)
19315 obstack_copy0 (&objfile->per_bfd->storage_obstack,
19316 base, strlen (base)));
19317 xfree (demangled);
19318 }
19319 }
19320
19321 fixup_called = 1;
19322 }
19323
19324 /* Read an attribute value described by an attribute form. */
19325
19326 static const gdb_byte *
19327 read_attribute_value (const struct die_reader_specs *reader,
19328 struct attribute *attr, unsigned form,
19329 LONGEST implicit_const, const gdb_byte *info_ptr)
19330 {
19331 struct dwarf2_cu *cu = reader->cu;
19332 struct dwarf2_per_objfile *dwarf2_per_objfile
19333 = cu->per_cu->dwarf2_per_objfile;
19334 struct objfile *objfile = dwarf2_per_objfile->objfile;
19335 struct gdbarch *gdbarch = get_objfile_arch (objfile);
19336 bfd *abfd = reader->abfd;
19337 struct comp_unit_head *cu_header = &cu->header;
19338 unsigned int bytes_read;
19339 struct dwarf_block *blk;
19340
19341 attr->form = (enum dwarf_form) form;
19342 switch (form)
19343 {
19344 case DW_FORM_ref_addr:
19345 if (cu->header.version == 2)
19346 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
19347 else
19348 DW_UNSND (attr) = read_offset (abfd, info_ptr,
19349 &cu->header, &bytes_read);
19350 info_ptr += bytes_read;
19351 break;
19352 case DW_FORM_GNU_ref_alt:
19353 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
19354 info_ptr += bytes_read;
19355 break;
19356 case DW_FORM_addr:
19357 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
19358 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
19359 info_ptr += bytes_read;
19360 break;
19361 case DW_FORM_block2:
19362 blk = dwarf_alloc_block (cu);
19363 blk->size = read_2_bytes (abfd, info_ptr);
19364 info_ptr += 2;
19365 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19366 info_ptr += blk->size;
19367 DW_BLOCK (attr) = blk;
19368 break;
19369 case DW_FORM_block4:
19370 blk = dwarf_alloc_block (cu);
19371 blk->size = read_4_bytes (abfd, info_ptr);
19372 info_ptr += 4;
19373 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19374 info_ptr += blk->size;
19375 DW_BLOCK (attr) = blk;
19376 break;
19377 case DW_FORM_data2:
19378 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
19379 info_ptr += 2;
19380 break;
19381 case DW_FORM_data4:
19382 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
19383 info_ptr += 4;
19384 break;
19385 case DW_FORM_data8:
19386 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
19387 info_ptr += 8;
19388 break;
19389 case DW_FORM_data16:
19390 blk = dwarf_alloc_block (cu);
19391 blk->size = 16;
19392 blk->data = read_n_bytes (abfd, info_ptr, 16);
19393 info_ptr += 16;
19394 DW_BLOCK (attr) = blk;
19395 break;
19396 case DW_FORM_sec_offset:
19397 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
19398 info_ptr += bytes_read;
19399 break;
19400 case DW_FORM_string:
19401 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
19402 DW_STRING_IS_CANONICAL (attr) = 0;
19403 info_ptr += bytes_read;
19404 break;
19405 case DW_FORM_strp:
19406 if (!cu->per_cu->is_dwz)
19407 {
19408 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
19409 abfd, info_ptr, cu_header,
19410 &bytes_read);
19411 DW_STRING_IS_CANONICAL (attr) = 0;
19412 info_ptr += bytes_read;
19413 break;
19414 }
19415 /* FALLTHROUGH */
19416 case DW_FORM_line_strp:
19417 if (!cu->per_cu->is_dwz)
19418 {
19419 DW_STRING (attr) = read_indirect_line_string (dwarf2_per_objfile,
19420 abfd, info_ptr,
19421 cu_header, &bytes_read);
19422 DW_STRING_IS_CANONICAL (attr) = 0;
19423 info_ptr += bytes_read;
19424 break;
19425 }
19426 /* FALLTHROUGH */
19427 case DW_FORM_GNU_strp_alt:
19428 {
19429 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
19430 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
19431 &bytes_read);
19432
19433 DW_STRING (attr) = read_indirect_string_from_dwz (objfile,
19434 dwz, str_offset);
19435 DW_STRING_IS_CANONICAL (attr) = 0;
19436 info_ptr += bytes_read;
19437 }
19438 break;
19439 case DW_FORM_exprloc:
19440 case DW_FORM_block:
19441 blk = dwarf_alloc_block (cu);
19442 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19443 info_ptr += bytes_read;
19444 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19445 info_ptr += blk->size;
19446 DW_BLOCK (attr) = blk;
19447 break;
19448 case DW_FORM_block1:
19449 blk = dwarf_alloc_block (cu);
19450 blk->size = read_1_byte (abfd, info_ptr);
19451 info_ptr += 1;
19452 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19453 info_ptr += blk->size;
19454 DW_BLOCK (attr) = blk;
19455 break;
19456 case DW_FORM_data1:
19457 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19458 info_ptr += 1;
19459 break;
19460 case DW_FORM_flag:
19461 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19462 info_ptr += 1;
19463 break;
19464 case DW_FORM_flag_present:
19465 DW_UNSND (attr) = 1;
19466 break;
19467 case DW_FORM_sdata:
19468 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19469 info_ptr += bytes_read;
19470 break;
19471 case DW_FORM_udata:
19472 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19473 info_ptr += bytes_read;
19474 break;
19475 case DW_FORM_ref1:
19476 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19477 + read_1_byte (abfd, info_ptr));
19478 info_ptr += 1;
19479 break;
19480 case DW_FORM_ref2:
19481 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19482 + read_2_bytes (abfd, info_ptr));
19483 info_ptr += 2;
19484 break;
19485 case DW_FORM_ref4:
19486 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19487 + read_4_bytes (abfd, info_ptr));
19488 info_ptr += 4;
19489 break;
19490 case DW_FORM_ref8:
19491 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19492 + read_8_bytes (abfd, info_ptr));
19493 info_ptr += 8;
19494 break;
19495 case DW_FORM_ref_sig8:
19496 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
19497 info_ptr += 8;
19498 break;
19499 case DW_FORM_ref_udata:
19500 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19501 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
19502 info_ptr += bytes_read;
19503 break;
19504 case DW_FORM_indirect:
19505 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19506 info_ptr += bytes_read;
19507 if (form == DW_FORM_implicit_const)
19508 {
19509 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19510 info_ptr += bytes_read;
19511 }
19512 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
19513 info_ptr);
19514 break;
19515 case DW_FORM_implicit_const:
19516 DW_SND (attr) = implicit_const;
19517 break;
19518 case DW_FORM_GNU_addr_index:
19519 if (reader->dwo_file == NULL)
19520 {
19521 /* For now flag a hard error.
19522 Later we can turn this into a complaint. */
19523 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19524 dwarf_form_name (form),
19525 bfd_get_filename (abfd));
19526 }
19527 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
19528 info_ptr += bytes_read;
19529 break;
19530 case DW_FORM_GNU_str_index:
19531 if (reader->dwo_file == NULL)
19532 {
19533 /* For now flag a hard error.
19534 Later we can turn this into a complaint if warranted. */
19535 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19536 dwarf_form_name (form),
19537 bfd_get_filename (abfd));
19538 }
19539 {
19540 ULONGEST str_index =
19541 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19542
19543 DW_STRING (attr) = read_str_index (reader, str_index);
19544 DW_STRING_IS_CANONICAL (attr) = 0;
19545 info_ptr += bytes_read;
19546 }
19547 break;
19548 default:
19549 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
19550 dwarf_form_name (form),
19551 bfd_get_filename (abfd));
19552 }
19553
19554 /* Super hack. */
19555 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
19556 attr->form = DW_FORM_GNU_ref_alt;
19557
19558 /* We have seen instances where the compiler tried to emit a byte
19559 size attribute of -1 which ended up being encoded as an unsigned
19560 0xffffffff. Although 0xffffffff is technically a valid size value,
19561 an object of this size seems pretty unlikely so we can relatively
19562 safely treat these cases as if the size attribute was invalid and
19563 treat them as zero by default. */
19564 if (attr->name == DW_AT_byte_size
19565 && form == DW_FORM_data4
19566 && DW_UNSND (attr) >= 0xffffffff)
19567 {
19568 complaint
19569 (&symfile_complaints,
19570 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
19571 hex_string (DW_UNSND (attr)));
19572 DW_UNSND (attr) = 0;
19573 }
19574
19575 return info_ptr;
19576 }
19577
19578 /* Read an attribute described by an abbreviated attribute. */
19579
19580 static const gdb_byte *
19581 read_attribute (const struct die_reader_specs *reader,
19582 struct attribute *attr, struct attr_abbrev *abbrev,
19583 const gdb_byte *info_ptr)
19584 {
19585 attr->name = abbrev->name;
19586 return read_attribute_value (reader, attr, abbrev->form,
19587 abbrev->implicit_const, info_ptr);
19588 }
19589
19590 /* Read dwarf information from a buffer. */
19591
19592 static unsigned int
19593 read_1_byte (bfd *abfd, const gdb_byte *buf)
19594 {
19595 return bfd_get_8 (abfd, buf);
19596 }
19597
19598 static int
19599 read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
19600 {
19601 return bfd_get_signed_8 (abfd, buf);
19602 }
19603
19604 static unsigned int
19605 read_2_bytes (bfd *abfd, const gdb_byte *buf)
19606 {
19607 return bfd_get_16 (abfd, buf);
19608 }
19609
19610 static int
19611 read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
19612 {
19613 return bfd_get_signed_16 (abfd, buf);
19614 }
19615
19616 static unsigned int
19617 read_4_bytes (bfd *abfd, const gdb_byte *buf)
19618 {
19619 return bfd_get_32 (abfd, buf);
19620 }
19621
19622 static int
19623 read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
19624 {
19625 return bfd_get_signed_32 (abfd, buf);
19626 }
19627
19628 static ULONGEST
19629 read_8_bytes (bfd *abfd, const gdb_byte *buf)
19630 {
19631 return bfd_get_64 (abfd, buf);
19632 }
19633
19634 static CORE_ADDR
19635 read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
19636 unsigned int *bytes_read)
19637 {
19638 struct comp_unit_head *cu_header = &cu->header;
19639 CORE_ADDR retval = 0;
19640
19641 if (cu_header->signed_addr_p)
19642 {
19643 switch (cu_header->addr_size)
19644 {
19645 case 2:
19646 retval = bfd_get_signed_16 (abfd, buf);
19647 break;
19648 case 4:
19649 retval = bfd_get_signed_32 (abfd, buf);
19650 break;
19651 case 8:
19652 retval = bfd_get_signed_64 (abfd, buf);
19653 break;
19654 default:
19655 internal_error (__FILE__, __LINE__,
19656 _("read_address: bad switch, signed [in module %s]"),
19657 bfd_get_filename (abfd));
19658 }
19659 }
19660 else
19661 {
19662 switch (cu_header->addr_size)
19663 {
19664 case 2:
19665 retval = bfd_get_16 (abfd, buf);
19666 break;
19667 case 4:
19668 retval = bfd_get_32 (abfd, buf);
19669 break;
19670 case 8:
19671 retval = bfd_get_64 (abfd, buf);
19672 break;
19673 default:
19674 internal_error (__FILE__, __LINE__,
19675 _("read_address: bad switch, "
19676 "unsigned [in module %s]"),
19677 bfd_get_filename (abfd));
19678 }
19679 }
19680
19681 *bytes_read = cu_header->addr_size;
19682 return retval;
19683 }
19684
19685 /* Read the initial length from a section. The (draft) DWARF 3
19686 specification allows the initial length to take up either 4 bytes
19687 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
19688 bytes describe the length and all offsets will be 8 bytes in length
19689 instead of 4.
19690
19691 An older, non-standard 64-bit format is also handled by this
19692 function. The older format in question stores the initial length
19693 as an 8-byte quantity without an escape value. Lengths greater
19694 than 2^32 aren't very common which means that the initial 4 bytes
19695 is almost always zero. Since a length value of zero doesn't make
19696 sense for the 32-bit format, this initial zero can be considered to
19697 be an escape value which indicates the presence of the older 64-bit
19698 format. As written, the code can't detect (old format) lengths
19699 greater than 4GB. If it becomes necessary to handle lengths
19700 somewhat larger than 4GB, we could allow other small values (such
19701 as the non-sensical values of 1, 2, and 3) to also be used as
19702 escape values indicating the presence of the old format.
19703
19704 The value returned via bytes_read should be used to increment the
19705 relevant pointer after calling read_initial_length().
19706
19707 [ Note: read_initial_length() and read_offset() are based on the
19708 document entitled "DWARF Debugging Information Format", revision
19709 3, draft 8, dated November 19, 2001. This document was obtained
19710 from:
19711
19712 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
19713
19714 This document is only a draft and is subject to change. (So beware.)
19715
19716 Details regarding the older, non-standard 64-bit format were
19717 determined empirically by examining 64-bit ELF files produced by
19718 the SGI toolchain on an IRIX 6.5 machine.
19719
19720 - Kevin, July 16, 2002
19721 ] */
19722
19723 static LONGEST
19724 read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
19725 {
19726 LONGEST length = bfd_get_32 (abfd, buf);
19727
19728 if (length == 0xffffffff)
19729 {
19730 length = bfd_get_64 (abfd, buf + 4);
19731 *bytes_read = 12;
19732 }
19733 else if (length == 0)
19734 {
19735 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
19736 length = bfd_get_64 (abfd, buf);
19737 *bytes_read = 8;
19738 }
19739 else
19740 {
19741 *bytes_read = 4;
19742 }
19743
19744 return length;
19745 }
19746
19747 /* Cover function for read_initial_length.
19748 Returns the length of the object at BUF, and stores the size of the
19749 initial length in *BYTES_READ and stores the size that offsets will be in
19750 *OFFSET_SIZE.
19751 If the initial length size is not equivalent to that specified in
19752 CU_HEADER then issue a complaint.
19753 This is useful when reading non-comp-unit headers. */
19754
19755 static LONGEST
19756 read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
19757 const struct comp_unit_head *cu_header,
19758 unsigned int *bytes_read,
19759 unsigned int *offset_size)
19760 {
19761 LONGEST length = read_initial_length (abfd, buf, bytes_read);
19762
19763 gdb_assert (cu_header->initial_length_size == 4
19764 || cu_header->initial_length_size == 8
19765 || cu_header->initial_length_size == 12);
19766
19767 if (cu_header->initial_length_size != *bytes_read)
19768 complaint (&symfile_complaints,
19769 _("intermixed 32-bit and 64-bit DWARF sections"));
19770
19771 *offset_size = (*bytes_read == 4) ? 4 : 8;
19772 return length;
19773 }
19774
19775 /* Read an offset from the data stream. The size of the offset is
19776 given by cu_header->offset_size. */
19777
19778 static LONGEST
19779 read_offset (bfd *abfd, const gdb_byte *buf,
19780 const struct comp_unit_head *cu_header,
19781 unsigned int *bytes_read)
19782 {
19783 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
19784
19785 *bytes_read = cu_header->offset_size;
19786 return offset;
19787 }
19788
19789 /* Read an offset from the data stream. */
19790
19791 static LONGEST
19792 read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
19793 {
19794 LONGEST retval = 0;
19795
19796 switch (offset_size)
19797 {
19798 case 4:
19799 retval = bfd_get_32 (abfd, buf);
19800 break;
19801 case 8:
19802 retval = bfd_get_64 (abfd, buf);
19803 break;
19804 default:
19805 internal_error (__FILE__, __LINE__,
19806 _("read_offset_1: bad switch [in module %s]"),
19807 bfd_get_filename (abfd));
19808 }
19809
19810 return retval;
19811 }
19812
19813 static const gdb_byte *
19814 read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
19815 {
19816 /* If the size of a host char is 8 bits, we can return a pointer
19817 to the buffer, otherwise we have to copy the data to a buffer
19818 allocated on the temporary obstack. */
19819 gdb_assert (HOST_CHAR_BIT == 8);
19820 return buf;
19821 }
19822
19823 static const char *
19824 read_direct_string (bfd *abfd, const gdb_byte *buf,
19825 unsigned int *bytes_read_ptr)
19826 {
19827 /* If the size of a host char is 8 bits, we can return a pointer
19828 to the string, otherwise we have to copy the string to a buffer
19829 allocated on the temporary obstack. */
19830 gdb_assert (HOST_CHAR_BIT == 8);
19831 if (*buf == '\0')
19832 {
19833 *bytes_read_ptr = 1;
19834 return NULL;
19835 }
19836 *bytes_read_ptr = strlen ((const char *) buf) + 1;
19837 return (const char *) buf;
19838 }
19839
19840 /* Return pointer to string at section SECT offset STR_OFFSET with error
19841 reporting strings FORM_NAME and SECT_NAME. */
19842
19843 static const char *
19844 read_indirect_string_at_offset_from (struct objfile *objfile,
19845 bfd *abfd, LONGEST str_offset,
19846 struct dwarf2_section_info *sect,
19847 const char *form_name,
19848 const char *sect_name)
19849 {
19850 dwarf2_read_section (objfile, sect);
19851 if (sect->buffer == NULL)
19852 error (_("%s used without %s section [in module %s]"),
19853 form_name, sect_name, bfd_get_filename (abfd));
19854 if (str_offset >= sect->size)
19855 error (_("%s pointing outside of %s section [in module %s]"),
19856 form_name, sect_name, bfd_get_filename (abfd));
19857 gdb_assert (HOST_CHAR_BIT == 8);
19858 if (sect->buffer[str_offset] == '\0')
19859 return NULL;
19860 return (const char *) (sect->buffer + str_offset);
19861 }
19862
19863 /* Return pointer to string at .debug_str offset STR_OFFSET. */
19864
19865 static const char *
19866 read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19867 bfd *abfd, LONGEST str_offset)
19868 {
19869 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19870 abfd, str_offset,
19871 &dwarf2_per_objfile->str,
19872 "DW_FORM_strp", ".debug_str");
19873 }
19874
19875 /* Return pointer to string at .debug_line_str offset STR_OFFSET. */
19876
19877 static const char *
19878 read_indirect_line_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19879 bfd *abfd, LONGEST str_offset)
19880 {
19881 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19882 abfd, str_offset,
19883 &dwarf2_per_objfile->line_str,
19884 "DW_FORM_line_strp",
19885 ".debug_line_str");
19886 }
19887
19888 /* Read a string at offset STR_OFFSET in the .debug_str section from
19889 the .dwz file DWZ. Throw an error if the offset is too large. If
19890 the string consists of a single NUL byte, return NULL; otherwise
19891 return a pointer to the string. */
19892
19893 static const char *
19894 read_indirect_string_from_dwz (struct objfile *objfile, struct dwz_file *dwz,
19895 LONGEST str_offset)
19896 {
19897 dwarf2_read_section (objfile, &dwz->str);
19898
19899 if (dwz->str.buffer == NULL)
19900 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
19901 "section [in module %s]"),
19902 bfd_get_filename (dwz->dwz_bfd));
19903 if (str_offset >= dwz->str.size)
19904 error (_("DW_FORM_GNU_strp_alt pointing outside of "
19905 ".debug_str section [in module %s]"),
19906 bfd_get_filename (dwz->dwz_bfd));
19907 gdb_assert (HOST_CHAR_BIT == 8);
19908 if (dwz->str.buffer[str_offset] == '\0')
19909 return NULL;
19910 return (const char *) (dwz->str.buffer + str_offset);
19911 }
19912
19913 /* Return pointer to string at .debug_str offset as read from BUF.
19914 BUF is assumed to be in a compilation unit described by CU_HEADER.
19915 Return *BYTES_READ_PTR count of bytes read from BUF. */
19916
19917 static const char *
19918 read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
19919 const gdb_byte *buf,
19920 const struct comp_unit_head *cu_header,
19921 unsigned int *bytes_read_ptr)
19922 {
19923 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19924
19925 return read_indirect_string_at_offset (dwarf2_per_objfile, abfd, str_offset);
19926 }
19927
19928 /* Return pointer to string at .debug_line_str offset as read from BUF.
19929 BUF is assumed to be in a compilation unit described by CU_HEADER.
19930 Return *BYTES_READ_PTR count of bytes read from BUF. */
19931
19932 static const char *
19933 read_indirect_line_string (struct dwarf2_per_objfile *dwarf2_per_objfile,
19934 bfd *abfd, const gdb_byte *buf,
19935 const struct comp_unit_head *cu_header,
19936 unsigned int *bytes_read_ptr)
19937 {
19938 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19939
19940 return read_indirect_line_string_at_offset (dwarf2_per_objfile, abfd,
19941 str_offset);
19942 }
19943
19944 ULONGEST
19945 read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
19946 unsigned int *bytes_read_ptr)
19947 {
19948 ULONGEST result;
19949 unsigned int num_read;
19950 int shift;
19951 unsigned char byte;
19952
19953 result = 0;
19954 shift = 0;
19955 num_read = 0;
19956 while (1)
19957 {
19958 byte = bfd_get_8 (abfd, buf);
19959 buf++;
19960 num_read++;
19961 result |= ((ULONGEST) (byte & 127) << shift);
19962 if ((byte & 128) == 0)
19963 {
19964 break;
19965 }
19966 shift += 7;
19967 }
19968 *bytes_read_ptr = num_read;
19969 return result;
19970 }
19971
19972 static LONGEST
19973 read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
19974 unsigned int *bytes_read_ptr)
19975 {
19976 LONGEST result;
19977 int shift, num_read;
19978 unsigned char byte;
19979
19980 result = 0;
19981 shift = 0;
19982 num_read = 0;
19983 while (1)
19984 {
19985 byte = bfd_get_8 (abfd, buf);
19986 buf++;
19987 num_read++;
19988 result |= ((LONGEST) (byte & 127) << shift);
19989 shift += 7;
19990 if ((byte & 128) == 0)
19991 {
19992 break;
19993 }
19994 }
19995 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
19996 result |= -(((LONGEST) 1) << shift);
19997 *bytes_read_ptr = num_read;
19998 return result;
19999 }
20000
20001 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
20002 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
20003 ADDR_SIZE is the size of addresses from the CU header. */
20004
20005 static CORE_ADDR
20006 read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
20007 unsigned int addr_index, ULONGEST addr_base, int addr_size)
20008 {
20009 struct objfile *objfile = dwarf2_per_objfile->objfile;
20010 bfd *abfd = objfile->obfd;
20011 const gdb_byte *info_ptr;
20012
20013 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
20014 if (dwarf2_per_objfile->addr.buffer == NULL)
20015 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
20016 objfile_name (objfile));
20017 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
20018 error (_("DW_FORM_addr_index pointing outside of "
20019 ".debug_addr section [in module %s]"),
20020 objfile_name (objfile));
20021 info_ptr = (dwarf2_per_objfile->addr.buffer
20022 + addr_base + addr_index * addr_size);
20023 if (addr_size == 4)
20024 return bfd_get_32 (abfd, info_ptr);
20025 else
20026 return bfd_get_64 (abfd, info_ptr);
20027 }
20028
20029 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
20030
20031 static CORE_ADDR
20032 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
20033 {
20034 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
20035 cu->addr_base, cu->header.addr_size);
20036 }
20037
20038 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
20039
20040 static CORE_ADDR
20041 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
20042 unsigned int *bytes_read)
20043 {
20044 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
20045 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
20046
20047 return read_addr_index (cu, addr_index);
20048 }
20049
20050 /* Data structure to pass results from dwarf2_read_addr_index_reader
20051 back to dwarf2_read_addr_index. */
20052
20053 struct dwarf2_read_addr_index_data
20054 {
20055 ULONGEST addr_base;
20056 int addr_size;
20057 };
20058
20059 /* die_reader_func for dwarf2_read_addr_index. */
20060
20061 static void
20062 dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
20063 const gdb_byte *info_ptr,
20064 struct die_info *comp_unit_die,
20065 int has_children,
20066 void *data)
20067 {
20068 struct dwarf2_cu *cu = reader->cu;
20069 struct dwarf2_read_addr_index_data *aidata =
20070 (struct dwarf2_read_addr_index_data *) data;
20071
20072 aidata->addr_base = cu->addr_base;
20073 aidata->addr_size = cu->header.addr_size;
20074 }
20075
20076 /* Given an index in .debug_addr, fetch the value.
20077 NOTE: This can be called during dwarf expression evaluation,
20078 long after the debug information has been read, and thus per_cu->cu
20079 may no longer exist. */
20080
20081 CORE_ADDR
20082 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
20083 unsigned int addr_index)
20084 {
20085 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
20086 struct objfile *objfile = dwarf2_per_objfile->objfile;
20087 struct dwarf2_cu *cu = per_cu->cu;
20088 ULONGEST addr_base;
20089 int addr_size;
20090
20091 /* We need addr_base and addr_size.
20092 If we don't have PER_CU->cu, we have to get it.
20093 Nasty, but the alternative is storing the needed info in PER_CU,
20094 which at this point doesn't seem justified: it's not clear how frequently
20095 it would get used and it would increase the size of every PER_CU.
20096 Entry points like dwarf2_per_cu_addr_size do a similar thing
20097 so we're not in uncharted territory here.
20098 Alas we need to be a bit more complicated as addr_base is contained
20099 in the DIE.
20100
20101 We don't need to read the entire CU(/TU).
20102 We just need the header and top level die.
20103
20104 IWBN to use the aging mechanism to let us lazily later discard the CU.
20105 For now we skip this optimization. */
20106
20107 if (cu != NULL)
20108 {
20109 addr_base = cu->addr_base;
20110 addr_size = cu->header.addr_size;
20111 }
20112 else
20113 {
20114 struct dwarf2_read_addr_index_data aidata;
20115
20116 /* Note: We can't use init_cutu_and_read_dies_simple here,
20117 we need addr_base. */
20118 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
20119 dwarf2_read_addr_index_reader, &aidata);
20120 addr_base = aidata.addr_base;
20121 addr_size = aidata.addr_size;
20122 }
20123
20124 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
20125 addr_size);
20126 }
20127
20128 /* Given a DW_FORM_GNU_str_index, fetch the string.
20129 This is only used by the Fission support. */
20130
20131 static const char *
20132 read_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
20133 {
20134 struct dwarf2_cu *cu = reader->cu;
20135 struct dwarf2_per_objfile *dwarf2_per_objfile
20136 = cu->per_cu->dwarf2_per_objfile;
20137 struct objfile *objfile = dwarf2_per_objfile->objfile;
20138 const char *objf_name = objfile_name (objfile);
20139 bfd *abfd = objfile->obfd;
20140 struct dwarf2_section_info *str_section = &reader->dwo_file->sections.str;
20141 struct dwarf2_section_info *str_offsets_section =
20142 &reader->dwo_file->sections.str_offsets;
20143 const gdb_byte *info_ptr;
20144 ULONGEST str_offset;
20145 static const char form_name[] = "DW_FORM_GNU_str_index";
20146
20147 dwarf2_read_section (objfile, str_section);
20148 dwarf2_read_section (objfile, str_offsets_section);
20149 if (str_section->buffer == NULL)
20150 error (_("%s used without .debug_str.dwo section"
20151 " in CU at offset %s [in module %s]"),
20152 form_name, sect_offset_str (cu->header.sect_off), objf_name);
20153 if (str_offsets_section->buffer == NULL)
20154 error (_("%s used without .debug_str_offsets.dwo section"
20155 " in CU at offset %s [in module %s]"),
20156 form_name, sect_offset_str (cu->header.sect_off), objf_name);
20157 if (str_index * cu->header.offset_size >= str_offsets_section->size)
20158 error (_("%s pointing outside of .debug_str_offsets.dwo"
20159 " section in CU at offset %s [in module %s]"),
20160 form_name, sect_offset_str (cu->header.sect_off), objf_name);
20161 info_ptr = (str_offsets_section->buffer
20162 + str_index * cu->header.offset_size);
20163 if (cu->header.offset_size == 4)
20164 str_offset = bfd_get_32 (abfd, info_ptr);
20165 else
20166 str_offset = bfd_get_64 (abfd, info_ptr);
20167 if (str_offset >= str_section->size)
20168 error (_("Offset from %s pointing outside of"
20169 " .debug_str.dwo section in CU at offset %s [in module %s]"),
20170 form_name, sect_offset_str (cu->header.sect_off), objf_name);
20171 return (const char *) (str_section->buffer + str_offset);
20172 }
20173
20174 /* Return the length of an LEB128 number in BUF. */
20175
20176 static int
20177 leb128_size (const gdb_byte *buf)
20178 {
20179 const gdb_byte *begin = buf;
20180 gdb_byte byte;
20181
20182 while (1)
20183 {
20184 byte = *buf++;
20185 if ((byte & 128) == 0)
20186 return buf - begin;
20187 }
20188 }
20189
20190 static void
20191 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
20192 {
20193 switch (lang)
20194 {
20195 case DW_LANG_C89:
20196 case DW_LANG_C99:
20197 case DW_LANG_C11:
20198 case DW_LANG_C:
20199 case DW_LANG_UPC:
20200 cu->language = language_c;
20201 break;
20202 case DW_LANG_Java:
20203 case DW_LANG_C_plus_plus:
20204 case DW_LANG_C_plus_plus_11:
20205 case DW_LANG_C_plus_plus_14:
20206 cu->language = language_cplus;
20207 break;
20208 case DW_LANG_D:
20209 cu->language = language_d;
20210 break;
20211 case DW_LANG_Fortran77:
20212 case DW_LANG_Fortran90:
20213 case DW_LANG_Fortran95:
20214 case DW_LANG_Fortran03:
20215 case DW_LANG_Fortran08:
20216 cu->language = language_fortran;
20217 break;
20218 case DW_LANG_Go:
20219 cu->language = language_go;
20220 break;
20221 case DW_LANG_Mips_Assembler:
20222 cu->language = language_asm;
20223 break;
20224 case DW_LANG_Ada83:
20225 case DW_LANG_Ada95:
20226 cu->language = language_ada;
20227 break;
20228 case DW_LANG_Modula2:
20229 cu->language = language_m2;
20230 break;
20231 case DW_LANG_Pascal83:
20232 cu->language = language_pascal;
20233 break;
20234 case DW_LANG_ObjC:
20235 cu->language = language_objc;
20236 break;
20237 case DW_LANG_Rust:
20238 case DW_LANG_Rust_old:
20239 cu->language = language_rust;
20240 break;
20241 case DW_LANG_Cobol74:
20242 case DW_LANG_Cobol85:
20243 default:
20244 cu->language = language_minimal;
20245 break;
20246 }
20247 cu->language_defn = language_def (cu->language);
20248 }
20249
20250 /* Return the named attribute or NULL if not there. */
20251
20252 static struct attribute *
20253 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20254 {
20255 for (;;)
20256 {
20257 unsigned int i;
20258 struct attribute *spec = NULL;
20259
20260 for (i = 0; i < die->num_attrs; ++i)
20261 {
20262 if (die->attrs[i].name == name)
20263 return &die->attrs[i];
20264 if (die->attrs[i].name == DW_AT_specification
20265 || die->attrs[i].name == DW_AT_abstract_origin)
20266 spec = &die->attrs[i];
20267 }
20268
20269 if (!spec)
20270 break;
20271
20272 die = follow_die_ref (die, spec, &cu);
20273 }
20274
20275 return NULL;
20276 }
20277
20278 /* Return the named attribute or NULL if not there,
20279 but do not follow DW_AT_specification, etc.
20280 This is for use in contexts where we're reading .debug_types dies.
20281 Following DW_AT_specification, DW_AT_abstract_origin will take us
20282 back up the chain, and we want to go down. */
20283
20284 static struct attribute *
20285 dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
20286 {
20287 unsigned int i;
20288
20289 for (i = 0; i < die->num_attrs; ++i)
20290 if (die->attrs[i].name == name)
20291 return &die->attrs[i];
20292
20293 return NULL;
20294 }
20295
20296 /* Return the string associated with a string-typed attribute, or NULL if it
20297 is either not found or is of an incorrect type. */
20298
20299 static const char *
20300 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20301 {
20302 struct attribute *attr;
20303 const char *str = NULL;
20304
20305 attr = dwarf2_attr (die, name, cu);
20306
20307 if (attr != NULL)
20308 {
20309 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
20310 || attr->form == DW_FORM_string
20311 || attr->form == DW_FORM_GNU_str_index
20312 || attr->form == DW_FORM_GNU_strp_alt)
20313 str = DW_STRING (attr);
20314 else
20315 complaint (&symfile_complaints,
20316 _("string type expected for attribute %s for "
20317 "DIE at %s in module %s"),
20318 dwarf_attr_name (name), sect_offset_str (die->sect_off),
20319 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
20320 }
20321
20322 return str;
20323 }
20324
20325 /* Return non-zero iff the attribute NAME is defined for the given DIE,
20326 and holds a non-zero value. This function should only be used for
20327 DW_FORM_flag or DW_FORM_flag_present attributes. */
20328
20329 static int
20330 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
20331 {
20332 struct attribute *attr = dwarf2_attr (die, name, cu);
20333
20334 return (attr && DW_UNSND (attr));
20335 }
20336
20337 static int
20338 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
20339 {
20340 /* A DIE is a declaration if it has a DW_AT_declaration attribute
20341 which value is non-zero. However, we have to be careful with
20342 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
20343 (via dwarf2_flag_true_p) follows this attribute. So we may
20344 end up accidently finding a declaration attribute that belongs
20345 to a different DIE referenced by the specification attribute,
20346 even though the given DIE does not have a declaration attribute. */
20347 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
20348 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
20349 }
20350
20351 /* Return the die giving the specification for DIE, if there is
20352 one. *SPEC_CU is the CU containing DIE on input, and the CU
20353 containing the return value on output. If there is no
20354 specification, but there is an abstract origin, that is
20355 returned. */
20356
20357 static struct die_info *
20358 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
20359 {
20360 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
20361 *spec_cu);
20362
20363 if (spec_attr == NULL)
20364 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
20365
20366 if (spec_attr == NULL)
20367 return NULL;
20368 else
20369 return follow_die_ref (die, spec_attr, spec_cu);
20370 }
20371
20372 /* Stub for free_line_header to match void * callback types. */
20373
20374 static void
20375 free_line_header_voidp (void *arg)
20376 {
20377 struct line_header *lh = (struct line_header *) arg;
20378
20379 delete lh;
20380 }
20381
20382 void
20383 line_header::add_include_dir (const char *include_dir)
20384 {
20385 if (dwarf_line_debug >= 2)
20386 fprintf_unfiltered (gdb_stdlog, "Adding dir %zu: %s\n",
20387 include_dirs.size () + 1, include_dir);
20388
20389 include_dirs.push_back (include_dir);
20390 }
20391
20392 void
20393 line_header::add_file_name (const char *name,
20394 dir_index d_index,
20395 unsigned int mod_time,
20396 unsigned int length)
20397 {
20398 if (dwarf_line_debug >= 2)
20399 fprintf_unfiltered (gdb_stdlog, "Adding file %u: %s\n",
20400 (unsigned) file_names.size () + 1, name);
20401
20402 file_names.emplace_back (name, d_index, mod_time, length);
20403 }
20404
20405 /* A convenience function to find the proper .debug_line section for a CU. */
20406
20407 static struct dwarf2_section_info *
20408 get_debug_line_section (struct dwarf2_cu *cu)
20409 {
20410 struct dwarf2_section_info *section;
20411 struct dwarf2_per_objfile *dwarf2_per_objfile
20412 = cu->per_cu->dwarf2_per_objfile;
20413
20414 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
20415 DWO file. */
20416 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20417 section = &cu->dwo_unit->dwo_file->sections.line;
20418 else if (cu->per_cu->is_dwz)
20419 {
20420 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
20421
20422 section = &dwz->line;
20423 }
20424 else
20425 section = &dwarf2_per_objfile->line;
20426
20427 return section;
20428 }
20429
20430 /* Read directory or file name entry format, starting with byte of
20431 format count entries, ULEB128 pairs of entry formats, ULEB128 of
20432 entries count and the entries themselves in the described entry
20433 format. */
20434
20435 static void
20436 read_formatted_entries (struct dwarf2_per_objfile *dwarf2_per_objfile,
20437 bfd *abfd, const gdb_byte **bufp,
20438 struct line_header *lh,
20439 const struct comp_unit_head *cu_header,
20440 void (*callback) (struct line_header *lh,
20441 const char *name,
20442 dir_index d_index,
20443 unsigned int mod_time,
20444 unsigned int length))
20445 {
20446 gdb_byte format_count, formati;
20447 ULONGEST data_count, datai;
20448 const gdb_byte *buf = *bufp;
20449 const gdb_byte *format_header_data;
20450 unsigned int bytes_read;
20451
20452 format_count = read_1_byte (abfd, buf);
20453 buf += 1;
20454 format_header_data = buf;
20455 for (formati = 0; formati < format_count; formati++)
20456 {
20457 read_unsigned_leb128 (abfd, buf, &bytes_read);
20458 buf += bytes_read;
20459 read_unsigned_leb128 (abfd, buf, &bytes_read);
20460 buf += bytes_read;
20461 }
20462
20463 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
20464 buf += bytes_read;
20465 for (datai = 0; datai < data_count; datai++)
20466 {
20467 const gdb_byte *format = format_header_data;
20468 struct file_entry fe;
20469
20470 for (formati = 0; formati < format_count; formati++)
20471 {
20472 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
20473 format += bytes_read;
20474
20475 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
20476 format += bytes_read;
20477
20478 gdb::optional<const char *> string;
20479 gdb::optional<unsigned int> uint;
20480
20481 switch (form)
20482 {
20483 case DW_FORM_string:
20484 string.emplace (read_direct_string (abfd, buf, &bytes_read));
20485 buf += bytes_read;
20486 break;
20487
20488 case DW_FORM_line_strp:
20489 string.emplace (read_indirect_line_string (dwarf2_per_objfile,
20490 abfd, buf,
20491 cu_header,
20492 &bytes_read));
20493 buf += bytes_read;
20494 break;
20495
20496 case DW_FORM_data1:
20497 uint.emplace (read_1_byte (abfd, buf));
20498 buf += 1;
20499 break;
20500
20501 case DW_FORM_data2:
20502 uint.emplace (read_2_bytes (abfd, buf));
20503 buf += 2;
20504 break;
20505
20506 case DW_FORM_data4:
20507 uint.emplace (read_4_bytes (abfd, buf));
20508 buf += 4;
20509 break;
20510
20511 case DW_FORM_data8:
20512 uint.emplace (read_8_bytes (abfd, buf));
20513 buf += 8;
20514 break;
20515
20516 case DW_FORM_udata:
20517 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
20518 buf += bytes_read;
20519 break;
20520
20521 case DW_FORM_block:
20522 /* It is valid only for DW_LNCT_timestamp which is ignored by
20523 current GDB. */
20524 break;
20525 }
20526
20527 switch (content_type)
20528 {
20529 case DW_LNCT_path:
20530 if (string.has_value ())
20531 fe.name = *string;
20532 break;
20533 case DW_LNCT_directory_index:
20534 if (uint.has_value ())
20535 fe.d_index = (dir_index) *uint;
20536 break;
20537 case DW_LNCT_timestamp:
20538 if (uint.has_value ())
20539 fe.mod_time = *uint;
20540 break;
20541 case DW_LNCT_size:
20542 if (uint.has_value ())
20543 fe.length = *uint;
20544 break;
20545 case DW_LNCT_MD5:
20546 break;
20547 default:
20548 complaint (&symfile_complaints,
20549 _("Unknown format content type %s"),
20550 pulongest (content_type));
20551 }
20552 }
20553
20554 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
20555 }
20556
20557 *bufp = buf;
20558 }
20559
20560 /* Read the statement program header starting at OFFSET in
20561 .debug_line, or .debug_line.dwo. Return a pointer
20562 to a struct line_header, allocated using xmalloc.
20563 Returns NULL if there is a problem reading the header, e.g., if it
20564 has a version we don't understand.
20565
20566 NOTE: the strings in the include directory and file name tables of
20567 the returned object point into the dwarf line section buffer,
20568 and must not be freed. */
20569
20570 static line_header_up
20571 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
20572 {
20573 const gdb_byte *line_ptr;
20574 unsigned int bytes_read, offset_size;
20575 int i;
20576 const char *cur_dir, *cur_file;
20577 struct dwarf2_section_info *section;
20578 bfd *abfd;
20579 struct dwarf2_per_objfile *dwarf2_per_objfile
20580 = cu->per_cu->dwarf2_per_objfile;
20581
20582 section = get_debug_line_section (cu);
20583 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
20584 if (section->buffer == NULL)
20585 {
20586 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20587 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
20588 else
20589 complaint (&symfile_complaints, _("missing .debug_line section"));
20590 return 0;
20591 }
20592
20593 /* We can't do this until we know the section is non-empty.
20594 Only then do we know we have such a section. */
20595 abfd = get_section_bfd_owner (section);
20596
20597 /* Make sure that at least there's room for the total_length field.
20598 That could be 12 bytes long, but we're just going to fudge that. */
20599 if (to_underlying (sect_off) + 4 >= section->size)
20600 {
20601 dwarf2_statement_list_fits_in_line_number_section_complaint ();
20602 return 0;
20603 }
20604
20605 line_header_up lh (new line_header ());
20606
20607 lh->sect_off = sect_off;
20608 lh->offset_in_dwz = cu->per_cu->is_dwz;
20609
20610 line_ptr = section->buffer + to_underlying (sect_off);
20611
20612 /* Read in the header. */
20613 lh->total_length =
20614 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
20615 &bytes_read, &offset_size);
20616 line_ptr += bytes_read;
20617 if (line_ptr + lh->total_length > (section->buffer + section->size))
20618 {
20619 dwarf2_statement_list_fits_in_line_number_section_complaint ();
20620 return 0;
20621 }
20622 lh->statement_program_end = line_ptr + lh->total_length;
20623 lh->version = read_2_bytes (abfd, line_ptr);
20624 line_ptr += 2;
20625 if (lh->version > 5)
20626 {
20627 /* This is a version we don't understand. The format could have
20628 changed in ways we don't handle properly so just punt. */
20629 complaint (&symfile_complaints,
20630 _("unsupported version in .debug_line section"));
20631 return NULL;
20632 }
20633 if (lh->version >= 5)
20634 {
20635 gdb_byte segment_selector_size;
20636
20637 /* Skip address size. */
20638 read_1_byte (abfd, line_ptr);
20639 line_ptr += 1;
20640
20641 segment_selector_size = read_1_byte (abfd, line_ptr);
20642 line_ptr += 1;
20643 if (segment_selector_size != 0)
20644 {
20645 complaint (&symfile_complaints,
20646 _("unsupported segment selector size %u "
20647 "in .debug_line section"),
20648 segment_selector_size);
20649 return NULL;
20650 }
20651 }
20652 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
20653 line_ptr += offset_size;
20654 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
20655 line_ptr += 1;
20656 if (lh->version >= 4)
20657 {
20658 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
20659 line_ptr += 1;
20660 }
20661 else
20662 lh->maximum_ops_per_instruction = 1;
20663
20664 if (lh->maximum_ops_per_instruction == 0)
20665 {
20666 lh->maximum_ops_per_instruction = 1;
20667 complaint (&symfile_complaints,
20668 _("invalid maximum_ops_per_instruction "
20669 "in `.debug_line' section"));
20670 }
20671
20672 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
20673 line_ptr += 1;
20674 lh->line_base = read_1_signed_byte (abfd, line_ptr);
20675 line_ptr += 1;
20676 lh->line_range = read_1_byte (abfd, line_ptr);
20677 line_ptr += 1;
20678 lh->opcode_base = read_1_byte (abfd, line_ptr);
20679 line_ptr += 1;
20680 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
20681
20682 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
20683 for (i = 1; i < lh->opcode_base; ++i)
20684 {
20685 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
20686 line_ptr += 1;
20687 }
20688
20689 if (lh->version >= 5)
20690 {
20691 /* Read directory table. */
20692 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20693 &cu->header,
20694 [] (struct line_header *lh, const char *name,
20695 dir_index d_index, unsigned int mod_time,
20696 unsigned int length)
20697 {
20698 lh->add_include_dir (name);
20699 });
20700
20701 /* Read file name table. */
20702 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20703 &cu->header,
20704 [] (struct line_header *lh, const char *name,
20705 dir_index d_index, unsigned int mod_time,
20706 unsigned int length)
20707 {
20708 lh->add_file_name (name, d_index, mod_time, length);
20709 });
20710 }
20711 else
20712 {
20713 /* Read directory table. */
20714 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20715 {
20716 line_ptr += bytes_read;
20717 lh->add_include_dir (cur_dir);
20718 }
20719 line_ptr += bytes_read;
20720
20721 /* Read file name table. */
20722 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20723 {
20724 unsigned int mod_time, length;
20725 dir_index d_index;
20726
20727 line_ptr += bytes_read;
20728 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20729 line_ptr += bytes_read;
20730 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20731 line_ptr += bytes_read;
20732 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20733 line_ptr += bytes_read;
20734
20735 lh->add_file_name (cur_file, d_index, mod_time, length);
20736 }
20737 line_ptr += bytes_read;
20738 }
20739 lh->statement_program_start = line_ptr;
20740
20741 if (line_ptr > (section->buffer + section->size))
20742 complaint (&symfile_complaints,
20743 _("line number info header doesn't "
20744 "fit in `.debug_line' section"));
20745
20746 return lh;
20747 }
20748
20749 /* Subroutine of dwarf_decode_lines to simplify it.
20750 Return the file name of the psymtab for included file FILE_INDEX
20751 in line header LH of PST.
20752 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20753 If space for the result is malloc'd, *NAME_HOLDER will be set.
20754 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
20755
20756 static const char *
20757 psymtab_include_file_name (const struct line_header *lh, int file_index,
20758 const struct partial_symtab *pst,
20759 const char *comp_dir,
20760 gdb::unique_xmalloc_ptr<char> *name_holder)
20761 {
20762 const file_entry &fe = lh->file_names[file_index];
20763 const char *include_name = fe.name;
20764 const char *include_name_to_compare = include_name;
20765 const char *pst_filename;
20766 int file_is_pst;
20767
20768 const char *dir_name = fe.include_dir (lh);
20769
20770 gdb::unique_xmalloc_ptr<char> hold_compare;
20771 if (!IS_ABSOLUTE_PATH (include_name)
20772 && (dir_name != NULL || comp_dir != NULL))
20773 {
20774 /* Avoid creating a duplicate psymtab for PST.
20775 We do this by comparing INCLUDE_NAME and PST_FILENAME.
20776 Before we do the comparison, however, we need to account
20777 for DIR_NAME and COMP_DIR.
20778 First prepend dir_name (if non-NULL). If we still don't
20779 have an absolute path prepend comp_dir (if non-NULL).
20780 However, the directory we record in the include-file's
20781 psymtab does not contain COMP_DIR (to match the
20782 corresponding symtab(s)).
20783
20784 Example:
20785
20786 bash$ cd /tmp
20787 bash$ gcc -g ./hello.c
20788 include_name = "hello.c"
20789 dir_name = "."
20790 DW_AT_comp_dir = comp_dir = "/tmp"
20791 DW_AT_name = "./hello.c"
20792
20793 */
20794
20795 if (dir_name != NULL)
20796 {
20797 name_holder->reset (concat (dir_name, SLASH_STRING,
20798 include_name, (char *) NULL));
20799 include_name = name_holder->get ();
20800 include_name_to_compare = include_name;
20801 }
20802 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
20803 {
20804 hold_compare.reset (concat (comp_dir, SLASH_STRING,
20805 include_name, (char *) NULL));
20806 include_name_to_compare = hold_compare.get ();
20807 }
20808 }
20809
20810 pst_filename = pst->filename;
20811 gdb::unique_xmalloc_ptr<char> copied_name;
20812 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
20813 {
20814 copied_name.reset (concat (pst->dirname, SLASH_STRING,
20815 pst_filename, (char *) NULL));
20816 pst_filename = copied_name.get ();
20817 }
20818
20819 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
20820
20821 if (file_is_pst)
20822 return NULL;
20823 return include_name;
20824 }
20825
20826 /* State machine to track the state of the line number program. */
20827
20828 class lnp_state_machine
20829 {
20830 public:
20831 /* Initialize a machine state for the start of a line number
20832 program. */
20833 lnp_state_machine (gdbarch *arch, line_header *lh, bool record_lines_p);
20834
20835 file_entry *current_file ()
20836 {
20837 /* lh->file_names is 0-based, but the file name numbers in the
20838 statement program are 1-based. */
20839 return m_line_header->file_name_at (m_file);
20840 }
20841
20842 /* Record the line in the state machine. END_SEQUENCE is true if
20843 we're processing the end of a sequence. */
20844 void record_line (bool end_sequence);
20845
20846 /* Check address and if invalid nop-out the rest of the lines in this
20847 sequence. */
20848 void check_line_address (struct dwarf2_cu *cu,
20849 const gdb_byte *line_ptr,
20850 CORE_ADDR lowpc, CORE_ADDR address);
20851
20852 void handle_set_discriminator (unsigned int discriminator)
20853 {
20854 m_discriminator = discriminator;
20855 m_line_has_non_zero_discriminator |= discriminator != 0;
20856 }
20857
20858 /* Handle DW_LNE_set_address. */
20859 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
20860 {
20861 m_op_index = 0;
20862 address += baseaddr;
20863 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
20864 }
20865
20866 /* Handle DW_LNS_advance_pc. */
20867 void handle_advance_pc (CORE_ADDR adjust);
20868
20869 /* Handle a special opcode. */
20870 void handle_special_opcode (unsigned char op_code);
20871
20872 /* Handle DW_LNS_advance_line. */
20873 void handle_advance_line (int line_delta)
20874 {
20875 advance_line (line_delta);
20876 }
20877
20878 /* Handle DW_LNS_set_file. */
20879 void handle_set_file (file_name_index file);
20880
20881 /* Handle DW_LNS_negate_stmt. */
20882 void handle_negate_stmt ()
20883 {
20884 m_is_stmt = !m_is_stmt;
20885 }
20886
20887 /* Handle DW_LNS_const_add_pc. */
20888 void handle_const_add_pc ();
20889
20890 /* Handle DW_LNS_fixed_advance_pc. */
20891 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
20892 {
20893 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20894 m_op_index = 0;
20895 }
20896
20897 /* Handle DW_LNS_copy. */
20898 void handle_copy ()
20899 {
20900 record_line (false);
20901 m_discriminator = 0;
20902 }
20903
20904 /* Handle DW_LNE_end_sequence. */
20905 void handle_end_sequence ()
20906 {
20907 m_record_line_callback = ::record_line;
20908 }
20909
20910 private:
20911 /* Advance the line by LINE_DELTA. */
20912 void advance_line (int line_delta)
20913 {
20914 m_line += line_delta;
20915
20916 if (line_delta != 0)
20917 m_line_has_non_zero_discriminator = m_discriminator != 0;
20918 }
20919
20920 gdbarch *m_gdbarch;
20921
20922 /* True if we're recording lines.
20923 Otherwise we're building partial symtabs and are just interested in
20924 finding include files mentioned by the line number program. */
20925 bool m_record_lines_p;
20926
20927 /* The line number header. */
20928 line_header *m_line_header;
20929
20930 /* These are part of the standard DWARF line number state machine,
20931 and initialized according to the DWARF spec. */
20932
20933 unsigned char m_op_index = 0;
20934 /* The line table index (1-based) of the current file. */
20935 file_name_index m_file = (file_name_index) 1;
20936 unsigned int m_line = 1;
20937
20938 /* These are initialized in the constructor. */
20939
20940 CORE_ADDR m_address;
20941 bool m_is_stmt;
20942 unsigned int m_discriminator;
20943
20944 /* Additional bits of state we need to track. */
20945
20946 /* The last file that we called dwarf2_start_subfile for.
20947 This is only used for TLLs. */
20948 unsigned int m_last_file = 0;
20949 /* The last file a line number was recorded for. */
20950 struct subfile *m_last_subfile = NULL;
20951
20952 /* The function to call to record a line. */
20953 record_line_ftype *m_record_line_callback = NULL;
20954
20955 /* The last line number that was recorded, used to coalesce
20956 consecutive entries for the same line. This can happen, for
20957 example, when discriminators are present. PR 17276. */
20958 unsigned int m_last_line = 0;
20959 bool m_line_has_non_zero_discriminator = false;
20960 };
20961
20962 void
20963 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
20964 {
20965 CORE_ADDR addr_adj = (((m_op_index + adjust)
20966 / m_line_header->maximum_ops_per_instruction)
20967 * m_line_header->minimum_instruction_length);
20968 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20969 m_op_index = ((m_op_index + adjust)
20970 % m_line_header->maximum_ops_per_instruction);
20971 }
20972
20973 void
20974 lnp_state_machine::handle_special_opcode (unsigned char op_code)
20975 {
20976 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
20977 CORE_ADDR addr_adj = (((m_op_index
20978 + (adj_opcode / m_line_header->line_range))
20979 / m_line_header->maximum_ops_per_instruction)
20980 * m_line_header->minimum_instruction_length);
20981 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20982 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
20983 % m_line_header->maximum_ops_per_instruction);
20984
20985 int line_delta = (m_line_header->line_base
20986 + (adj_opcode % m_line_header->line_range));
20987 advance_line (line_delta);
20988 record_line (false);
20989 m_discriminator = 0;
20990 }
20991
20992 void
20993 lnp_state_machine::handle_set_file (file_name_index file)
20994 {
20995 m_file = file;
20996
20997 const file_entry *fe = current_file ();
20998 if (fe == NULL)
20999 dwarf2_debug_line_missing_file_complaint ();
21000 else if (m_record_lines_p)
21001 {
21002 const char *dir = fe->include_dir (m_line_header);
21003
21004 m_last_subfile = current_subfile;
21005 m_line_has_non_zero_discriminator = m_discriminator != 0;
21006 dwarf2_start_subfile (fe->name, dir);
21007 }
21008 }
21009
21010 void
21011 lnp_state_machine::handle_const_add_pc ()
21012 {
21013 CORE_ADDR adjust
21014 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
21015
21016 CORE_ADDR addr_adj
21017 = (((m_op_index + adjust)
21018 / m_line_header->maximum_ops_per_instruction)
21019 * m_line_header->minimum_instruction_length);
21020
21021 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21022 m_op_index = ((m_op_index + adjust)
21023 % m_line_header->maximum_ops_per_instruction);
21024 }
21025
21026 /* Ignore this record_line request. */
21027
21028 static void
21029 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
21030 {
21031 return;
21032 }
21033
21034 /* Return non-zero if we should add LINE to the line number table.
21035 LINE is the line to add, LAST_LINE is the last line that was added,
21036 LAST_SUBFILE is the subfile for LAST_LINE.
21037 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
21038 had a non-zero discriminator.
21039
21040 We have to be careful in the presence of discriminators.
21041 E.g., for this line:
21042
21043 for (i = 0; i < 100000; i++);
21044
21045 clang can emit four line number entries for that one line,
21046 each with a different discriminator.
21047 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
21048
21049 However, we want gdb to coalesce all four entries into one.
21050 Otherwise the user could stepi into the middle of the line and
21051 gdb would get confused about whether the pc really was in the
21052 middle of the line.
21053
21054 Things are further complicated by the fact that two consecutive
21055 line number entries for the same line is a heuristic used by gcc
21056 to denote the end of the prologue. So we can't just discard duplicate
21057 entries, we have to be selective about it. The heuristic we use is
21058 that we only collapse consecutive entries for the same line if at least
21059 one of those entries has a non-zero discriminator. PR 17276.
21060
21061 Note: Addresses in the line number state machine can never go backwards
21062 within one sequence, thus this coalescing is ok. */
21063
21064 static int
21065 dwarf_record_line_p (unsigned int line, unsigned int last_line,
21066 int line_has_non_zero_discriminator,
21067 struct subfile *last_subfile)
21068 {
21069 if (current_subfile != last_subfile)
21070 return 1;
21071 if (line != last_line)
21072 return 1;
21073 /* Same line for the same file that we've seen already.
21074 As a last check, for pr 17276, only record the line if the line
21075 has never had a non-zero discriminator. */
21076 if (!line_has_non_zero_discriminator)
21077 return 1;
21078 return 0;
21079 }
21080
21081 /* Use P_RECORD_LINE to record line number LINE beginning at address ADDRESS
21082 in the line table of subfile SUBFILE. */
21083
21084 static void
21085 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
21086 unsigned int line, CORE_ADDR address,
21087 record_line_ftype p_record_line)
21088 {
21089 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
21090
21091 if (dwarf_line_debug)
21092 {
21093 fprintf_unfiltered (gdb_stdlog,
21094 "Recording line %u, file %s, address %s\n",
21095 line, lbasename (subfile->name),
21096 paddress (gdbarch, address));
21097 }
21098
21099 (*p_record_line) (subfile, line, addr);
21100 }
21101
21102 /* Subroutine of dwarf_decode_lines_1 to simplify it.
21103 Mark the end of a set of line number records.
21104 The arguments are the same as for dwarf_record_line_1.
21105 If SUBFILE is NULL the request is ignored. */
21106
21107 static void
21108 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
21109 CORE_ADDR address, record_line_ftype p_record_line)
21110 {
21111 if (subfile == NULL)
21112 return;
21113
21114 if (dwarf_line_debug)
21115 {
21116 fprintf_unfiltered (gdb_stdlog,
21117 "Finishing current line, file %s, address %s\n",
21118 lbasename (subfile->name),
21119 paddress (gdbarch, address));
21120 }
21121
21122 dwarf_record_line_1 (gdbarch, subfile, 0, address, p_record_line);
21123 }
21124
21125 void
21126 lnp_state_machine::record_line (bool end_sequence)
21127 {
21128 if (dwarf_line_debug)
21129 {
21130 fprintf_unfiltered (gdb_stdlog,
21131 "Processing actual line %u: file %u,"
21132 " address %s, is_stmt %u, discrim %u\n",
21133 m_line, to_underlying (m_file),
21134 paddress (m_gdbarch, m_address),
21135 m_is_stmt, m_discriminator);
21136 }
21137
21138 file_entry *fe = current_file ();
21139
21140 if (fe == NULL)
21141 dwarf2_debug_line_missing_file_complaint ();
21142 /* For now we ignore lines not starting on an instruction boundary.
21143 But not when processing end_sequence for compatibility with the
21144 previous version of the code. */
21145 else if (m_op_index == 0 || end_sequence)
21146 {
21147 fe->included_p = 1;
21148 if (m_record_lines_p && m_is_stmt)
21149 {
21150 if (m_last_subfile != current_subfile || end_sequence)
21151 {
21152 dwarf_finish_line (m_gdbarch, m_last_subfile,
21153 m_address, m_record_line_callback);
21154 }
21155
21156 if (!end_sequence)
21157 {
21158 if (dwarf_record_line_p (m_line, m_last_line,
21159 m_line_has_non_zero_discriminator,
21160 m_last_subfile))
21161 {
21162 dwarf_record_line_1 (m_gdbarch, current_subfile,
21163 m_line, m_address,
21164 m_record_line_callback);
21165 }
21166 m_last_subfile = current_subfile;
21167 m_last_line = m_line;
21168 }
21169 }
21170 }
21171 }
21172
21173 lnp_state_machine::lnp_state_machine (gdbarch *arch, line_header *lh,
21174 bool record_lines_p)
21175 {
21176 m_gdbarch = arch;
21177 m_record_lines_p = record_lines_p;
21178 m_line_header = lh;
21179
21180 m_record_line_callback = ::record_line;
21181
21182 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
21183 was a line entry for it so that the backend has a chance to adjust it
21184 and also record it in case it needs it. This is currently used by MIPS
21185 code, cf. `mips_adjust_dwarf2_line'. */
21186 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
21187 m_is_stmt = lh->default_is_stmt;
21188 m_discriminator = 0;
21189 }
21190
21191 void
21192 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
21193 const gdb_byte *line_ptr,
21194 CORE_ADDR lowpc, CORE_ADDR address)
21195 {
21196 /* If address < lowpc then it's not a usable value, it's outside the
21197 pc range of the CU. However, we restrict the test to only address
21198 values of zero to preserve GDB's previous behaviour which is to
21199 handle the specific case of a function being GC'd by the linker. */
21200
21201 if (address == 0 && address < lowpc)
21202 {
21203 /* This line table is for a function which has been
21204 GCd by the linker. Ignore it. PR gdb/12528 */
21205
21206 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21207 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
21208
21209 complaint (&symfile_complaints,
21210 _(".debug_line address at offset 0x%lx is 0 [in module %s]"),
21211 line_offset, objfile_name (objfile));
21212 m_record_line_callback = noop_record_line;
21213 /* Note: record_line_callback is left as noop_record_line until
21214 we see DW_LNE_end_sequence. */
21215 }
21216 }
21217
21218 /* Subroutine of dwarf_decode_lines to simplify it.
21219 Process the line number information in LH.
21220 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
21221 program in order to set included_p for every referenced header. */
21222
21223 static void
21224 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
21225 const int decode_for_pst_p, CORE_ADDR lowpc)
21226 {
21227 const gdb_byte *line_ptr, *extended_end;
21228 const gdb_byte *line_end;
21229 unsigned int bytes_read, extended_len;
21230 unsigned char op_code, extended_op;
21231 CORE_ADDR baseaddr;
21232 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21233 bfd *abfd = objfile->obfd;
21234 struct gdbarch *gdbarch = get_objfile_arch (objfile);
21235 /* True if we're recording line info (as opposed to building partial
21236 symtabs and just interested in finding include files mentioned by
21237 the line number program). */
21238 bool record_lines_p = !decode_for_pst_p;
21239
21240 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
21241
21242 line_ptr = lh->statement_program_start;
21243 line_end = lh->statement_program_end;
21244
21245 /* Read the statement sequences until there's nothing left. */
21246 while (line_ptr < line_end)
21247 {
21248 /* The DWARF line number program state machine. Reset the state
21249 machine at the start of each sequence. */
21250 lnp_state_machine state_machine (gdbarch, lh, record_lines_p);
21251 bool end_sequence = false;
21252
21253 if (record_lines_p)
21254 {
21255 /* Start a subfile for the current file of the state
21256 machine. */
21257 const file_entry *fe = state_machine.current_file ();
21258
21259 if (fe != NULL)
21260 dwarf2_start_subfile (fe->name, fe->include_dir (lh));
21261 }
21262
21263 /* Decode the table. */
21264 while (line_ptr < line_end && !end_sequence)
21265 {
21266 op_code = read_1_byte (abfd, line_ptr);
21267 line_ptr += 1;
21268
21269 if (op_code >= lh->opcode_base)
21270 {
21271 /* Special opcode. */
21272 state_machine.handle_special_opcode (op_code);
21273 }
21274 else switch (op_code)
21275 {
21276 case DW_LNS_extended_op:
21277 extended_len = read_unsigned_leb128 (abfd, line_ptr,
21278 &bytes_read);
21279 line_ptr += bytes_read;
21280 extended_end = line_ptr + extended_len;
21281 extended_op = read_1_byte (abfd, line_ptr);
21282 line_ptr += 1;
21283 switch (extended_op)
21284 {
21285 case DW_LNE_end_sequence:
21286 state_machine.handle_end_sequence ();
21287 end_sequence = true;
21288 break;
21289 case DW_LNE_set_address:
21290 {
21291 CORE_ADDR address
21292 = read_address (abfd, line_ptr, cu, &bytes_read);
21293 line_ptr += bytes_read;
21294
21295 state_machine.check_line_address (cu, line_ptr,
21296 lowpc, address);
21297 state_machine.handle_set_address (baseaddr, address);
21298 }
21299 break;
21300 case DW_LNE_define_file:
21301 {
21302 const char *cur_file;
21303 unsigned int mod_time, length;
21304 dir_index dindex;
21305
21306 cur_file = read_direct_string (abfd, line_ptr,
21307 &bytes_read);
21308 line_ptr += bytes_read;
21309 dindex = (dir_index)
21310 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21311 line_ptr += bytes_read;
21312 mod_time =
21313 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21314 line_ptr += bytes_read;
21315 length =
21316 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21317 line_ptr += bytes_read;
21318 lh->add_file_name (cur_file, dindex, mod_time, length);
21319 }
21320 break;
21321 case DW_LNE_set_discriminator:
21322 {
21323 /* The discriminator is not interesting to the
21324 debugger; just ignore it. We still need to
21325 check its value though:
21326 if there are consecutive entries for the same
21327 (non-prologue) line we want to coalesce them.
21328 PR 17276. */
21329 unsigned int discr
21330 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21331 line_ptr += bytes_read;
21332
21333 state_machine.handle_set_discriminator (discr);
21334 }
21335 break;
21336 default:
21337 complaint (&symfile_complaints,
21338 _("mangled .debug_line section"));
21339 return;
21340 }
21341 /* Make sure that we parsed the extended op correctly. If e.g.
21342 we expected a different address size than the producer used,
21343 we may have read the wrong number of bytes. */
21344 if (line_ptr != extended_end)
21345 {
21346 complaint (&symfile_complaints,
21347 _("mangled .debug_line section"));
21348 return;
21349 }
21350 break;
21351 case DW_LNS_copy:
21352 state_machine.handle_copy ();
21353 break;
21354 case DW_LNS_advance_pc:
21355 {
21356 CORE_ADDR adjust
21357 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21358 line_ptr += bytes_read;
21359
21360 state_machine.handle_advance_pc (adjust);
21361 }
21362 break;
21363 case DW_LNS_advance_line:
21364 {
21365 int line_delta
21366 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
21367 line_ptr += bytes_read;
21368
21369 state_machine.handle_advance_line (line_delta);
21370 }
21371 break;
21372 case DW_LNS_set_file:
21373 {
21374 file_name_index file
21375 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
21376 &bytes_read);
21377 line_ptr += bytes_read;
21378
21379 state_machine.handle_set_file (file);
21380 }
21381 break;
21382 case DW_LNS_set_column:
21383 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21384 line_ptr += bytes_read;
21385 break;
21386 case DW_LNS_negate_stmt:
21387 state_machine.handle_negate_stmt ();
21388 break;
21389 case DW_LNS_set_basic_block:
21390 break;
21391 /* Add to the address register of the state machine the
21392 address increment value corresponding to special opcode
21393 255. I.e., this value is scaled by the minimum
21394 instruction length since special opcode 255 would have
21395 scaled the increment. */
21396 case DW_LNS_const_add_pc:
21397 state_machine.handle_const_add_pc ();
21398 break;
21399 case DW_LNS_fixed_advance_pc:
21400 {
21401 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
21402 line_ptr += 2;
21403
21404 state_machine.handle_fixed_advance_pc (addr_adj);
21405 }
21406 break;
21407 default:
21408 {
21409 /* Unknown standard opcode, ignore it. */
21410 int i;
21411
21412 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
21413 {
21414 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21415 line_ptr += bytes_read;
21416 }
21417 }
21418 }
21419 }
21420
21421 if (!end_sequence)
21422 dwarf2_debug_line_missing_end_sequence_complaint ();
21423
21424 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
21425 in which case we still finish recording the last line). */
21426 state_machine.record_line (true);
21427 }
21428 }
21429
21430 /* Decode the Line Number Program (LNP) for the given line_header
21431 structure and CU. The actual information extracted and the type
21432 of structures created from the LNP depends on the value of PST.
21433
21434 1. If PST is NULL, then this procedure uses the data from the program
21435 to create all necessary symbol tables, and their linetables.
21436
21437 2. If PST is not NULL, this procedure reads the program to determine
21438 the list of files included by the unit represented by PST, and
21439 builds all the associated partial symbol tables.
21440
21441 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21442 It is used for relative paths in the line table.
21443 NOTE: When processing partial symtabs (pst != NULL),
21444 comp_dir == pst->dirname.
21445
21446 NOTE: It is important that psymtabs have the same file name (via strcmp)
21447 as the corresponding symtab. Since COMP_DIR is not used in the name of the
21448 symtab we don't use it in the name of the psymtabs we create.
21449 E.g. expand_line_sal requires this when finding psymtabs to expand.
21450 A good testcase for this is mb-inline.exp.
21451
21452 LOWPC is the lowest address in CU (or 0 if not known).
21453
21454 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21455 for its PC<->lines mapping information. Otherwise only the filename
21456 table is read in. */
21457
21458 static void
21459 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
21460 struct dwarf2_cu *cu, struct partial_symtab *pst,
21461 CORE_ADDR lowpc, int decode_mapping)
21462 {
21463 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21464 const int decode_for_pst_p = (pst != NULL);
21465
21466 if (decode_mapping)
21467 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
21468
21469 if (decode_for_pst_p)
21470 {
21471 int file_index;
21472
21473 /* Now that we're done scanning the Line Header Program, we can
21474 create the psymtab of each included file. */
21475 for (file_index = 0; file_index < lh->file_names.size (); file_index++)
21476 if (lh->file_names[file_index].included_p == 1)
21477 {
21478 gdb::unique_xmalloc_ptr<char> name_holder;
21479 const char *include_name =
21480 psymtab_include_file_name (lh, file_index, pst, comp_dir,
21481 &name_holder);
21482 if (include_name != NULL)
21483 dwarf2_create_include_psymtab (include_name, pst, objfile);
21484 }
21485 }
21486 else
21487 {
21488 /* Make sure a symtab is created for every file, even files
21489 which contain only variables (i.e. no code with associated
21490 line numbers). */
21491 struct compunit_symtab *cust = buildsym_compunit_symtab ();
21492 int i;
21493
21494 for (i = 0; i < lh->file_names.size (); i++)
21495 {
21496 file_entry &fe = lh->file_names[i];
21497
21498 dwarf2_start_subfile (fe.name, fe.include_dir (lh));
21499
21500 if (current_subfile->symtab == NULL)
21501 {
21502 current_subfile->symtab
21503 = allocate_symtab (cust, current_subfile->name);
21504 }
21505 fe.symtab = current_subfile->symtab;
21506 }
21507 }
21508 }
21509
21510 /* Start a subfile for DWARF. FILENAME is the name of the file and
21511 DIRNAME the name of the source directory which contains FILENAME
21512 or NULL if not known.
21513 This routine tries to keep line numbers from identical absolute and
21514 relative file names in a common subfile.
21515
21516 Using the `list' example from the GDB testsuite, which resides in
21517 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21518 of /srcdir/list0.c yields the following debugging information for list0.c:
21519
21520 DW_AT_name: /srcdir/list0.c
21521 DW_AT_comp_dir: /compdir
21522 files.files[0].name: list0.h
21523 files.files[0].dir: /srcdir
21524 files.files[1].name: list0.c
21525 files.files[1].dir: /srcdir
21526
21527 The line number information for list0.c has to end up in a single
21528 subfile, so that `break /srcdir/list0.c:1' works as expected.
21529 start_subfile will ensure that this happens provided that we pass the
21530 concatenation of files.files[1].dir and files.files[1].name as the
21531 subfile's name. */
21532
21533 static void
21534 dwarf2_start_subfile (const char *filename, const char *dirname)
21535 {
21536 char *copy = NULL;
21537
21538 /* In order not to lose the line information directory,
21539 we concatenate it to the filename when it makes sense.
21540 Note that the Dwarf3 standard says (speaking of filenames in line
21541 information): ``The directory index is ignored for file names
21542 that represent full path names''. Thus ignoring dirname in the
21543 `else' branch below isn't an issue. */
21544
21545 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
21546 {
21547 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
21548 filename = copy;
21549 }
21550
21551 start_subfile (filename);
21552
21553 if (copy != NULL)
21554 xfree (copy);
21555 }
21556
21557 /* Start a symtab for DWARF.
21558 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
21559
21560 static struct compunit_symtab *
21561 dwarf2_start_symtab (struct dwarf2_cu *cu,
21562 const char *name, const char *comp_dir, CORE_ADDR low_pc)
21563 {
21564 struct compunit_symtab *cust
21565 = start_symtab (cu->per_cu->dwarf2_per_objfile->objfile, name, comp_dir,
21566 low_pc, cu->language);
21567
21568 record_debugformat ("DWARF 2");
21569 record_producer (cu->producer);
21570
21571 /* We assume that we're processing GCC output. */
21572 processing_gcc_compilation = 2;
21573
21574 cu->processing_has_namespace_info = 0;
21575
21576 return cust;
21577 }
21578
21579 static void
21580 var_decode_location (struct attribute *attr, struct symbol *sym,
21581 struct dwarf2_cu *cu)
21582 {
21583 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21584 struct comp_unit_head *cu_header = &cu->header;
21585
21586 /* NOTE drow/2003-01-30: There used to be a comment and some special
21587 code here to turn a symbol with DW_AT_external and a
21588 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21589 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21590 with some versions of binutils) where shared libraries could have
21591 relocations against symbols in their debug information - the
21592 minimal symbol would have the right address, but the debug info
21593 would not. It's no longer necessary, because we will explicitly
21594 apply relocations when we read in the debug information now. */
21595
21596 /* A DW_AT_location attribute with no contents indicates that a
21597 variable has been optimized away. */
21598 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
21599 {
21600 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21601 return;
21602 }
21603
21604 /* Handle one degenerate form of location expression specially, to
21605 preserve GDB's previous behavior when section offsets are
21606 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
21607 then mark this symbol as LOC_STATIC. */
21608
21609 if (attr_form_is_block (attr)
21610 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
21611 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
21612 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
21613 && (DW_BLOCK (attr)->size
21614 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
21615 {
21616 unsigned int dummy;
21617
21618 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
21619 SYMBOL_VALUE_ADDRESS (sym) =
21620 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
21621 else
21622 SYMBOL_VALUE_ADDRESS (sym) =
21623 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
21624 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
21625 fixup_symbol_section (sym, objfile);
21626 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
21627 SYMBOL_SECTION (sym));
21628 return;
21629 }
21630
21631 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21632 expression evaluator, and use LOC_COMPUTED only when necessary
21633 (i.e. when the value of a register or memory location is
21634 referenced, or a thread-local block, etc.). Then again, it might
21635 not be worthwhile. I'm assuming that it isn't unless performance
21636 or memory numbers show me otherwise. */
21637
21638 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
21639
21640 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
21641 cu->has_loclist = 1;
21642 }
21643
21644 /* Given a pointer to a DWARF information entry, figure out if we need
21645 to make a symbol table entry for it, and if so, create a new entry
21646 and return a pointer to it.
21647 If TYPE is NULL, determine symbol type from the die, otherwise
21648 used the passed type.
21649 If SPACE is not NULL, use it to hold the new symbol. If it is
21650 NULL, allocate a new symbol on the objfile's obstack. */
21651
21652 static struct symbol *
21653 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21654 struct symbol *space)
21655 {
21656 struct dwarf2_per_objfile *dwarf2_per_objfile
21657 = cu->per_cu->dwarf2_per_objfile;
21658 struct objfile *objfile = dwarf2_per_objfile->objfile;
21659 struct gdbarch *gdbarch = get_objfile_arch (objfile);
21660 struct symbol *sym = NULL;
21661 const char *name;
21662 struct attribute *attr = NULL;
21663 struct attribute *attr2 = NULL;
21664 CORE_ADDR baseaddr;
21665 struct pending **list_to_add = NULL;
21666
21667 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
21668
21669 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
21670
21671 name = dwarf2_name (die, cu);
21672 if (name)
21673 {
21674 const char *linkagename;
21675 int suppress_add = 0;
21676
21677 if (space)
21678 sym = space;
21679 else
21680 sym = allocate_symbol (objfile);
21681 OBJSTAT (objfile, n_syms++);
21682
21683 /* Cache this symbol's name and the name's demangled form (if any). */
21684 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
21685 linkagename = dwarf2_physname (name, die, cu);
21686 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
21687
21688 /* Fortran does not have mangling standard and the mangling does differ
21689 between gfortran, iFort etc. */
21690 if (cu->language == language_fortran
21691 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
21692 symbol_set_demangled_name (&(sym->ginfo),
21693 dwarf2_full_name (name, die, cu),
21694 NULL);
21695
21696 /* Default assumptions.
21697 Use the passed type or decode it from the die. */
21698 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21699 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21700 if (type != NULL)
21701 SYMBOL_TYPE (sym) = type;
21702 else
21703 SYMBOL_TYPE (sym) = die_type (die, cu);
21704 attr = dwarf2_attr (die,
21705 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21706 cu);
21707 if (attr)
21708 {
21709 SYMBOL_LINE (sym) = DW_UNSND (attr);
21710 }
21711
21712 attr = dwarf2_attr (die,
21713 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21714 cu);
21715 if (attr)
21716 {
21717 file_name_index file_index = (file_name_index) DW_UNSND (attr);
21718 struct file_entry *fe;
21719
21720 if (cu->line_header != NULL)
21721 fe = cu->line_header->file_name_at (file_index);
21722 else
21723 fe = NULL;
21724
21725 if (fe == NULL)
21726 complaint (&symfile_complaints,
21727 _("file index out of range"));
21728 else
21729 symbol_set_symtab (sym, fe->symtab);
21730 }
21731
21732 switch (die->tag)
21733 {
21734 case DW_TAG_label:
21735 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
21736 if (attr)
21737 {
21738 CORE_ADDR addr;
21739
21740 addr = attr_value_as_address (attr);
21741 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
21742 SYMBOL_VALUE_ADDRESS (sym) = addr;
21743 }
21744 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
21745 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
21746 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
21747 add_symbol_to_list (sym, cu->list_in_scope);
21748 break;
21749 case DW_TAG_subprogram:
21750 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21751 finish_block. */
21752 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21753 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21754 if ((attr2 && (DW_UNSND (attr2) != 0))
21755 || cu->language == language_ada)
21756 {
21757 /* Subprograms marked external are stored as a global symbol.
21758 Ada subprograms, whether marked external or not, are always
21759 stored as a global symbol, because we want to be able to
21760 access them globally. For instance, we want to be able
21761 to break on a nested subprogram without having to
21762 specify the context. */
21763 list_to_add = &global_symbols;
21764 }
21765 else
21766 {
21767 list_to_add = cu->list_in_scope;
21768 }
21769 break;
21770 case DW_TAG_inlined_subroutine:
21771 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21772 finish_block. */
21773 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21774 SYMBOL_INLINED (sym) = 1;
21775 list_to_add = cu->list_in_scope;
21776 break;
21777 case DW_TAG_template_value_param:
21778 suppress_add = 1;
21779 /* Fall through. */
21780 case DW_TAG_constant:
21781 case DW_TAG_variable:
21782 case DW_TAG_member:
21783 /* Compilation with minimal debug info may result in
21784 variables with missing type entries. Change the
21785 misleading `void' type to something sensible. */
21786 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
21787 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
21788
21789 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21790 /* In the case of DW_TAG_member, we should only be called for
21791 static const members. */
21792 if (die->tag == DW_TAG_member)
21793 {
21794 /* dwarf2_add_field uses die_is_declaration,
21795 so we do the same. */
21796 gdb_assert (die_is_declaration (die, cu));
21797 gdb_assert (attr);
21798 }
21799 if (attr)
21800 {
21801 dwarf2_const_value (attr, sym, cu);
21802 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21803 if (!suppress_add)
21804 {
21805 if (attr2 && (DW_UNSND (attr2) != 0))
21806 list_to_add = &global_symbols;
21807 else
21808 list_to_add = cu->list_in_scope;
21809 }
21810 break;
21811 }
21812 attr = dwarf2_attr (die, DW_AT_location, cu);
21813 if (attr)
21814 {
21815 var_decode_location (attr, sym, cu);
21816 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21817
21818 /* Fortran explicitly imports any global symbols to the local
21819 scope by DW_TAG_common_block. */
21820 if (cu->language == language_fortran && die->parent
21821 && die->parent->tag == DW_TAG_common_block)
21822 attr2 = NULL;
21823
21824 if (SYMBOL_CLASS (sym) == LOC_STATIC
21825 && SYMBOL_VALUE_ADDRESS (sym) == 0
21826 && !dwarf2_per_objfile->has_section_at_zero)
21827 {
21828 /* When a static variable is eliminated by the linker,
21829 the corresponding debug information is not stripped
21830 out, but the variable address is set to null;
21831 do not add such variables into symbol table. */
21832 }
21833 else if (attr2 && (DW_UNSND (attr2) != 0))
21834 {
21835 /* Workaround gfortran PR debug/40040 - it uses
21836 DW_AT_location for variables in -fPIC libraries which may
21837 get overriden by other libraries/executable and get
21838 a different address. Resolve it by the minimal symbol
21839 which may come from inferior's executable using copy
21840 relocation. Make this workaround only for gfortran as for
21841 other compilers GDB cannot guess the minimal symbol
21842 Fortran mangling kind. */
21843 if (cu->language == language_fortran && die->parent
21844 && die->parent->tag == DW_TAG_module
21845 && cu->producer
21846 && startswith (cu->producer, "GNU Fortran"))
21847 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
21848
21849 /* A variable with DW_AT_external is never static,
21850 but it may be block-scoped. */
21851 list_to_add = (cu->list_in_scope == &file_symbols
21852 ? &global_symbols : cu->list_in_scope);
21853 }
21854 else
21855 list_to_add = cu->list_in_scope;
21856 }
21857 else
21858 {
21859 /* We do not know the address of this symbol.
21860 If it is an external symbol and we have type information
21861 for it, enter the symbol as a LOC_UNRESOLVED symbol.
21862 The address of the variable will then be determined from
21863 the minimal symbol table whenever the variable is
21864 referenced. */
21865 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21866
21867 /* Fortran explicitly imports any global symbols to the local
21868 scope by DW_TAG_common_block. */
21869 if (cu->language == language_fortran && die->parent
21870 && die->parent->tag == DW_TAG_common_block)
21871 {
21872 /* SYMBOL_CLASS doesn't matter here because
21873 read_common_block is going to reset it. */
21874 if (!suppress_add)
21875 list_to_add = cu->list_in_scope;
21876 }
21877 else if (attr2 && (DW_UNSND (attr2) != 0)
21878 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
21879 {
21880 /* A variable with DW_AT_external is never static, but it
21881 may be block-scoped. */
21882 list_to_add = (cu->list_in_scope == &file_symbols
21883 ? &global_symbols : cu->list_in_scope);
21884
21885 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
21886 }
21887 else if (!die_is_declaration (die, cu))
21888 {
21889 /* Use the default LOC_OPTIMIZED_OUT class. */
21890 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
21891 if (!suppress_add)
21892 list_to_add = cu->list_in_scope;
21893 }
21894 }
21895 break;
21896 case DW_TAG_formal_parameter:
21897 /* If we are inside a function, mark this as an argument. If
21898 not, we might be looking at an argument to an inlined function
21899 when we do not have enough information to show inlined frames;
21900 pretend it's a local variable in that case so that the user can
21901 still see it. */
21902 if (context_stack_depth > 0
21903 && context_stack[context_stack_depth - 1].name != NULL)
21904 SYMBOL_IS_ARGUMENT (sym) = 1;
21905 attr = dwarf2_attr (die, DW_AT_location, cu);
21906 if (attr)
21907 {
21908 var_decode_location (attr, sym, cu);
21909 }
21910 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21911 if (attr)
21912 {
21913 dwarf2_const_value (attr, sym, cu);
21914 }
21915
21916 list_to_add = cu->list_in_scope;
21917 break;
21918 case DW_TAG_unspecified_parameters:
21919 /* From varargs functions; gdb doesn't seem to have any
21920 interest in this information, so just ignore it for now.
21921 (FIXME?) */
21922 break;
21923 case DW_TAG_template_type_param:
21924 suppress_add = 1;
21925 /* Fall through. */
21926 case DW_TAG_class_type:
21927 case DW_TAG_interface_type:
21928 case DW_TAG_structure_type:
21929 case DW_TAG_union_type:
21930 case DW_TAG_set_type:
21931 case DW_TAG_enumeration_type:
21932 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21933 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
21934
21935 {
21936 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
21937 really ever be static objects: otherwise, if you try
21938 to, say, break of a class's method and you're in a file
21939 which doesn't mention that class, it won't work unless
21940 the check for all static symbols in lookup_symbol_aux
21941 saves you. See the OtherFileClass tests in
21942 gdb.c++/namespace.exp. */
21943
21944 if (!suppress_add)
21945 {
21946 list_to_add = (cu->list_in_scope == &file_symbols
21947 && cu->language == language_cplus
21948 ? &global_symbols : cu->list_in_scope);
21949
21950 /* The semantics of C++ state that "struct foo {
21951 ... }" also defines a typedef for "foo". */
21952 if (cu->language == language_cplus
21953 || cu->language == language_ada
21954 || cu->language == language_d
21955 || cu->language == language_rust)
21956 {
21957 /* The symbol's name is already allocated along
21958 with this objfile, so we don't need to
21959 duplicate it for the type. */
21960 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
21961 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
21962 }
21963 }
21964 }
21965 break;
21966 case DW_TAG_typedef:
21967 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21968 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21969 list_to_add = cu->list_in_scope;
21970 break;
21971 case DW_TAG_base_type:
21972 case DW_TAG_subrange_type:
21973 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21974 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21975 list_to_add = cu->list_in_scope;
21976 break;
21977 case DW_TAG_enumerator:
21978 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21979 if (attr)
21980 {
21981 dwarf2_const_value (attr, sym, cu);
21982 }
21983 {
21984 /* NOTE: carlton/2003-11-10: See comment above in the
21985 DW_TAG_class_type, etc. block. */
21986
21987 list_to_add = (cu->list_in_scope == &file_symbols
21988 && cu->language == language_cplus
21989 ? &global_symbols : cu->list_in_scope);
21990 }
21991 break;
21992 case DW_TAG_imported_declaration:
21993 case DW_TAG_namespace:
21994 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21995 list_to_add = &global_symbols;
21996 break;
21997 case DW_TAG_module:
21998 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21999 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
22000 list_to_add = &global_symbols;
22001 break;
22002 case DW_TAG_common_block:
22003 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
22004 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
22005 add_symbol_to_list (sym, cu->list_in_scope);
22006 break;
22007 default:
22008 /* Not a tag we recognize. Hopefully we aren't processing
22009 trash data, but since we must specifically ignore things
22010 we don't recognize, there is nothing else we should do at
22011 this point. */
22012 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
22013 dwarf_tag_name (die->tag));
22014 break;
22015 }
22016
22017 if (suppress_add)
22018 {
22019 sym->hash_next = objfile->template_symbols;
22020 objfile->template_symbols = sym;
22021 list_to_add = NULL;
22022 }
22023
22024 if (list_to_add != NULL)
22025 add_symbol_to_list (sym, list_to_add);
22026
22027 /* For the benefit of old versions of GCC, check for anonymous
22028 namespaces based on the demangled name. */
22029 if (!cu->processing_has_namespace_info
22030 && cu->language == language_cplus)
22031 cp_scan_for_anonymous_namespaces (sym, objfile);
22032 }
22033 return (sym);
22034 }
22035
22036 /* Given an attr with a DW_FORM_dataN value in host byte order,
22037 zero-extend it as appropriate for the symbol's type. The DWARF
22038 standard (v4) is not entirely clear about the meaning of using
22039 DW_FORM_dataN for a constant with a signed type, where the type is
22040 wider than the data. The conclusion of a discussion on the DWARF
22041 list was that this is unspecified. We choose to always zero-extend
22042 because that is the interpretation long in use by GCC. */
22043
22044 static gdb_byte *
22045 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
22046 struct dwarf2_cu *cu, LONGEST *value, int bits)
22047 {
22048 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22049 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
22050 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
22051 LONGEST l = DW_UNSND (attr);
22052
22053 if (bits < sizeof (*value) * 8)
22054 {
22055 l &= ((LONGEST) 1 << bits) - 1;
22056 *value = l;
22057 }
22058 else if (bits == sizeof (*value) * 8)
22059 *value = l;
22060 else
22061 {
22062 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
22063 store_unsigned_integer (bytes, bits / 8, byte_order, l);
22064 return bytes;
22065 }
22066
22067 return NULL;
22068 }
22069
22070 /* Read a constant value from an attribute. Either set *VALUE, or if
22071 the value does not fit in *VALUE, set *BYTES - either already
22072 allocated on the objfile obstack, or newly allocated on OBSTACK,
22073 or, set *BATON, if we translated the constant to a location
22074 expression. */
22075
22076 static void
22077 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
22078 const char *name, struct obstack *obstack,
22079 struct dwarf2_cu *cu,
22080 LONGEST *value, const gdb_byte **bytes,
22081 struct dwarf2_locexpr_baton **baton)
22082 {
22083 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22084 struct comp_unit_head *cu_header = &cu->header;
22085 struct dwarf_block *blk;
22086 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
22087 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22088
22089 *value = 0;
22090 *bytes = NULL;
22091 *baton = NULL;
22092
22093 switch (attr->form)
22094 {
22095 case DW_FORM_addr:
22096 case DW_FORM_GNU_addr_index:
22097 {
22098 gdb_byte *data;
22099
22100 if (TYPE_LENGTH (type) != cu_header->addr_size)
22101 dwarf2_const_value_length_mismatch_complaint (name,
22102 cu_header->addr_size,
22103 TYPE_LENGTH (type));
22104 /* Symbols of this form are reasonably rare, so we just
22105 piggyback on the existing location code rather than writing
22106 a new implementation of symbol_computed_ops. */
22107 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
22108 (*baton)->per_cu = cu->per_cu;
22109 gdb_assert ((*baton)->per_cu);
22110
22111 (*baton)->size = 2 + cu_header->addr_size;
22112 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
22113 (*baton)->data = data;
22114
22115 data[0] = DW_OP_addr;
22116 store_unsigned_integer (&data[1], cu_header->addr_size,
22117 byte_order, DW_ADDR (attr));
22118 data[cu_header->addr_size + 1] = DW_OP_stack_value;
22119 }
22120 break;
22121 case DW_FORM_string:
22122 case DW_FORM_strp:
22123 case DW_FORM_GNU_str_index:
22124 case DW_FORM_GNU_strp_alt:
22125 /* DW_STRING is already allocated on the objfile obstack, point
22126 directly to it. */
22127 *bytes = (const gdb_byte *) DW_STRING (attr);
22128 break;
22129 case DW_FORM_block1:
22130 case DW_FORM_block2:
22131 case DW_FORM_block4:
22132 case DW_FORM_block:
22133 case DW_FORM_exprloc:
22134 case DW_FORM_data16:
22135 blk = DW_BLOCK (attr);
22136 if (TYPE_LENGTH (type) != blk->size)
22137 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
22138 TYPE_LENGTH (type));
22139 *bytes = blk->data;
22140 break;
22141
22142 /* The DW_AT_const_value attributes are supposed to carry the
22143 symbol's value "represented as it would be on the target
22144 architecture." By the time we get here, it's already been
22145 converted to host endianness, so we just need to sign- or
22146 zero-extend it as appropriate. */
22147 case DW_FORM_data1:
22148 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
22149 break;
22150 case DW_FORM_data2:
22151 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
22152 break;
22153 case DW_FORM_data4:
22154 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
22155 break;
22156 case DW_FORM_data8:
22157 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
22158 break;
22159
22160 case DW_FORM_sdata:
22161 case DW_FORM_implicit_const:
22162 *value = DW_SND (attr);
22163 break;
22164
22165 case DW_FORM_udata:
22166 *value = DW_UNSND (attr);
22167 break;
22168
22169 default:
22170 complaint (&symfile_complaints,
22171 _("unsupported const value attribute form: '%s'"),
22172 dwarf_form_name (attr->form));
22173 *value = 0;
22174 break;
22175 }
22176 }
22177
22178
22179 /* Copy constant value from an attribute to a symbol. */
22180
22181 static void
22182 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
22183 struct dwarf2_cu *cu)
22184 {
22185 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22186 LONGEST value;
22187 const gdb_byte *bytes;
22188 struct dwarf2_locexpr_baton *baton;
22189
22190 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
22191 SYMBOL_PRINT_NAME (sym),
22192 &objfile->objfile_obstack, cu,
22193 &value, &bytes, &baton);
22194
22195 if (baton != NULL)
22196 {
22197 SYMBOL_LOCATION_BATON (sym) = baton;
22198 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
22199 }
22200 else if (bytes != NULL)
22201 {
22202 SYMBOL_VALUE_BYTES (sym) = bytes;
22203 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
22204 }
22205 else
22206 {
22207 SYMBOL_VALUE (sym) = value;
22208 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
22209 }
22210 }
22211
22212 /* Return the type of the die in question using its DW_AT_type attribute. */
22213
22214 static struct type *
22215 die_type (struct die_info *die, struct dwarf2_cu *cu)
22216 {
22217 struct attribute *type_attr;
22218
22219 type_attr = dwarf2_attr (die, DW_AT_type, cu);
22220 if (!type_attr)
22221 {
22222 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22223 /* A missing DW_AT_type represents a void type. */
22224 return objfile_type (objfile)->builtin_void;
22225 }
22226
22227 return lookup_die_type (die, type_attr, cu);
22228 }
22229
22230 /* True iff CU's producer generates GNAT Ada auxiliary information
22231 that allows to find parallel types through that information instead
22232 of having to do expensive parallel lookups by type name. */
22233
22234 static int
22235 need_gnat_info (struct dwarf2_cu *cu)
22236 {
22237 /* Assume that the Ada compiler was GNAT, which always produces
22238 the auxiliary information. */
22239 return (cu->language == language_ada);
22240 }
22241
22242 /* Return the auxiliary type of the die in question using its
22243 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
22244 attribute is not present. */
22245
22246 static struct type *
22247 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
22248 {
22249 struct attribute *type_attr;
22250
22251 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
22252 if (!type_attr)
22253 return NULL;
22254
22255 return lookup_die_type (die, type_attr, cu);
22256 }
22257
22258 /* If DIE has a descriptive_type attribute, then set the TYPE's
22259 descriptive type accordingly. */
22260
22261 static void
22262 set_descriptive_type (struct type *type, struct die_info *die,
22263 struct dwarf2_cu *cu)
22264 {
22265 struct type *descriptive_type = die_descriptive_type (die, cu);
22266
22267 if (descriptive_type)
22268 {
22269 ALLOCATE_GNAT_AUX_TYPE (type);
22270 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
22271 }
22272 }
22273
22274 /* Return the containing type of the die in question using its
22275 DW_AT_containing_type attribute. */
22276
22277 static struct type *
22278 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
22279 {
22280 struct attribute *type_attr;
22281 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22282
22283 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
22284 if (!type_attr)
22285 error (_("Dwarf Error: Problem turning containing type into gdb type "
22286 "[in module %s]"), objfile_name (objfile));
22287
22288 return lookup_die_type (die, type_attr, cu);
22289 }
22290
22291 /* Return an error marker type to use for the ill formed type in DIE/CU. */
22292
22293 static struct type *
22294 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
22295 {
22296 struct dwarf2_per_objfile *dwarf2_per_objfile
22297 = cu->per_cu->dwarf2_per_objfile;
22298 struct objfile *objfile = dwarf2_per_objfile->objfile;
22299 char *message, *saved;
22300
22301 message = xstrprintf (_("<unknown type in %s, CU %s, DIE %s>"),
22302 objfile_name (objfile),
22303 sect_offset_str (cu->header.sect_off),
22304 sect_offset_str (die->sect_off));
22305 saved = (char *) obstack_copy0 (&objfile->objfile_obstack,
22306 message, strlen (message));
22307 xfree (message);
22308
22309 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
22310 }
22311
22312 /* Look up the type of DIE in CU using its type attribute ATTR.
22313 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
22314 DW_AT_containing_type.
22315 If there is no type substitute an error marker. */
22316
22317 static struct type *
22318 lookup_die_type (struct die_info *die, const struct attribute *attr,
22319 struct dwarf2_cu *cu)
22320 {
22321 struct dwarf2_per_objfile *dwarf2_per_objfile
22322 = cu->per_cu->dwarf2_per_objfile;
22323 struct objfile *objfile = dwarf2_per_objfile->objfile;
22324 struct type *this_type;
22325
22326 gdb_assert (attr->name == DW_AT_type
22327 || attr->name == DW_AT_GNAT_descriptive_type
22328 || attr->name == DW_AT_containing_type);
22329
22330 /* First see if we have it cached. */
22331
22332 if (attr->form == DW_FORM_GNU_ref_alt)
22333 {
22334 struct dwarf2_per_cu_data *per_cu;
22335 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
22336
22337 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
22338 dwarf2_per_objfile);
22339 this_type = get_die_type_at_offset (sect_off, per_cu);
22340 }
22341 else if (attr_form_is_ref (attr))
22342 {
22343 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
22344
22345 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
22346 }
22347 else if (attr->form == DW_FORM_ref_sig8)
22348 {
22349 ULONGEST signature = DW_SIGNATURE (attr);
22350
22351 return get_signatured_type (die, signature, cu);
22352 }
22353 else
22354 {
22355 complaint (&symfile_complaints,
22356 _("Dwarf Error: Bad type attribute %s in DIE"
22357 " at %s [in module %s]"),
22358 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
22359 objfile_name (objfile));
22360 return build_error_marker_type (cu, die);
22361 }
22362
22363 /* If not cached we need to read it in. */
22364
22365 if (this_type == NULL)
22366 {
22367 struct die_info *type_die = NULL;
22368 struct dwarf2_cu *type_cu = cu;
22369
22370 if (attr_form_is_ref (attr))
22371 type_die = follow_die_ref (die, attr, &type_cu);
22372 if (type_die == NULL)
22373 return build_error_marker_type (cu, die);
22374 /* If we find the type now, it's probably because the type came
22375 from an inter-CU reference and the type's CU got expanded before
22376 ours. */
22377 this_type = read_type_die (type_die, type_cu);
22378 }
22379
22380 /* If we still don't have a type use an error marker. */
22381
22382 if (this_type == NULL)
22383 return build_error_marker_type (cu, die);
22384
22385 return this_type;
22386 }
22387
22388 /* Return the type in DIE, CU.
22389 Returns NULL for invalid types.
22390
22391 This first does a lookup in die_type_hash,
22392 and only reads the die in if necessary.
22393
22394 NOTE: This can be called when reading in partial or full symbols. */
22395
22396 static struct type *
22397 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
22398 {
22399 struct type *this_type;
22400
22401 this_type = get_die_type (die, cu);
22402 if (this_type)
22403 return this_type;
22404
22405 return read_type_die_1 (die, cu);
22406 }
22407
22408 /* Read the type in DIE, CU.
22409 Returns NULL for invalid types. */
22410
22411 static struct type *
22412 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
22413 {
22414 struct type *this_type = NULL;
22415
22416 switch (die->tag)
22417 {
22418 case DW_TAG_class_type:
22419 case DW_TAG_interface_type:
22420 case DW_TAG_structure_type:
22421 case DW_TAG_union_type:
22422 this_type = read_structure_type (die, cu);
22423 break;
22424 case DW_TAG_enumeration_type:
22425 this_type = read_enumeration_type (die, cu);
22426 break;
22427 case DW_TAG_subprogram:
22428 case DW_TAG_subroutine_type:
22429 case DW_TAG_inlined_subroutine:
22430 this_type = read_subroutine_type (die, cu);
22431 break;
22432 case DW_TAG_array_type:
22433 this_type = read_array_type (die, cu);
22434 break;
22435 case DW_TAG_set_type:
22436 this_type = read_set_type (die, cu);
22437 break;
22438 case DW_TAG_pointer_type:
22439 this_type = read_tag_pointer_type (die, cu);
22440 break;
22441 case DW_TAG_ptr_to_member_type:
22442 this_type = read_tag_ptr_to_member_type (die, cu);
22443 break;
22444 case DW_TAG_reference_type:
22445 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22446 break;
22447 case DW_TAG_rvalue_reference_type:
22448 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
22449 break;
22450 case DW_TAG_const_type:
22451 this_type = read_tag_const_type (die, cu);
22452 break;
22453 case DW_TAG_volatile_type:
22454 this_type = read_tag_volatile_type (die, cu);
22455 break;
22456 case DW_TAG_restrict_type:
22457 this_type = read_tag_restrict_type (die, cu);
22458 break;
22459 case DW_TAG_string_type:
22460 this_type = read_tag_string_type (die, cu);
22461 break;
22462 case DW_TAG_typedef:
22463 this_type = read_typedef (die, cu);
22464 break;
22465 case DW_TAG_subrange_type:
22466 this_type = read_subrange_type (die, cu);
22467 break;
22468 case DW_TAG_base_type:
22469 this_type = read_base_type (die, cu);
22470 break;
22471 case DW_TAG_unspecified_type:
22472 this_type = read_unspecified_type (die, cu);
22473 break;
22474 case DW_TAG_namespace:
22475 this_type = read_namespace_type (die, cu);
22476 break;
22477 case DW_TAG_module:
22478 this_type = read_module_type (die, cu);
22479 break;
22480 case DW_TAG_atomic_type:
22481 this_type = read_tag_atomic_type (die, cu);
22482 break;
22483 default:
22484 complaint (&symfile_complaints,
22485 _("unexpected tag in read_type_die: '%s'"),
22486 dwarf_tag_name (die->tag));
22487 break;
22488 }
22489
22490 return this_type;
22491 }
22492
22493 /* See if we can figure out if the class lives in a namespace. We do
22494 this by looking for a member function; its demangled name will
22495 contain namespace info, if there is any.
22496 Return the computed name or NULL.
22497 Space for the result is allocated on the objfile's obstack.
22498 This is the full-die version of guess_partial_die_structure_name.
22499 In this case we know DIE has no useful parent. */
22500
22501 static char *
22502 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22503 {
22504 struct die_info *spec_die;
22505 struct dwarf2_cu *spec_cu;
22506 struct die_info *child;
22507 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22508
22509 spec_cu = cu;
22510 spec_die = die_specification (die, &spec_cu);
22511 if (spec_die != NULL)
22512 {
22513 die = spec_die;
22514 cu = spec_cu;
22515 }
22516
22517 for (child = die->child;
22518 child != NULL;
22519 child = child->sibling)
22520 {
22521 if (child->tag == DW_TAG_subprogram)
22522 {
22523 const char *linkage_name = dw2_linkage_name (child, cu);
22524
22525 if (linkage_name != NULL)
22526 {
22527 char *actual_name
22528 = language_class_name_from_physname (cu->language_defn,
22529 linkage_name);
22530 char *name = NULL;
22531
22532 if (actual_name != NULL)
22533 {
22534 const char *die_name = dwarf2_name (die, cu);
22535
22536 if (die_name != NULL
22537 && strcmp (die_name, actual_name) != 0)
22538 {
22539 /* Strip off the class name from the full name.
22540 We want the prefix. */
22541 int die_name_len = strlen (die_name);
22542 int actual_name_len = strlen (actual_name);
22543
22544 /* Test for '::' as a sanity check. */
22545 if (actual_name_len > die_name_len + 2
22546 && actual_name[actual_name_len
22547 - die_name_len - 1] == ':')
22548 name = (char *) obstack_copy0 (
22549 &objfile->per_bfd->storage_obstack,
22550 actual_name, actual_name_len - die_name_len - 2);
22551 }
22552 }
22553 xfree (actual_name);
22554 return name;
22555 }
22556 }
22557 }
22558
22559 return NULL;
22560 }
22561
22562 /* GCC might emit a nameless typedef that has a linkage name. Determine the
22563 prefix part in such case. See
22564 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22565
22566 static const char *
22567 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22568 {
22569 struct attribute *attr;
22570 const char *base;
22571
22572 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22573 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22574 return NULL;
22575
22576 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
22577 return NULL;
22578
22579 attr = dw2_linkage_name_attr (die, cu);
22580 if (attr == NULL || DW_STRING (attr) == NULL)
22581 return NULL;
22582
22583 /* dwarf2_name had to be already called. */
22584 gdb_assert (DW_STRING_IS_CANONICAL (attr));
22585
22586 /* Strip the base name, keep any leading namespaces/classes. */
22587 base = strrchr (DW_STRING (attr), ':');
22588 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
22589 return "";
22590
22591 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22592 return (char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
22593 DW_STRING (attr),
22594 &base[-1] - DW_STRING (attr));
22595 }
22596
22597 /* Return the name of the namespace/class that DIE is defined within,
22598 or "" if we can't tell. The caller should not xfree the result.
22599
22600 For example, if we're within the method foo() in the following
22601 code:
22602
22603 namespace N {
22604 class C {
22605 void foo () {
22606 }
22607 };
22608 }
22609
22610 then determine_prefix on foo's die will return "N::C". */
22611
22612 static const char *
22613 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
22614 {
22615 struct dwarf2_per_objfile *dwarf2_per_objfile
22616 = cu->per_cu->dwarf2_per_objfile;
22617 struct die_info *parent, *spec_die;
22618 struct dwarf2_cu *spec_cu;
22619 struct type *parent_type;
22620 const char *retval;
22621
22622 if (cu->language != language_cplus
22623 && cu->language != language_fortran && cu->language != language_d
22624 && cu->language != language_rust)
22625 return "";
22626
22627 retval = anonymous_struct_prefix (die, cu);
22628 if (retval)
22629 return retval;
22630
22631 /* We have to be careful in the presence of DW_AT_specification.
22632 For example, with GCC 3.4, given the code
22633
22634 namespace N {
22635 void foo() {
22636 // Definition of N::foo.
22637 }
22638 }
22639
22640 then we'll have a tree of DIEs like this:
22641
22642 1: DW_TAG_compile_unit
22643 2: DW_TAG_namespace // N
22644 3: DW_TAG_subprogram // declaration of N::foo
22645 4: DW_TAG_subprogram // definition of N::foo
22646 DW_AT_specification // refers to die #3
22647
22648 Thus, when processing die #4, we have to pretend that we're in
22649 the context of its DW_AT_specification, namely the contex of die
22650 #3. */
22651 spec_cu = cu;
22652 spec_die = die_specification (die, &spec_cu);
22653 if (spec_die == NULL)
22654 parent = die->parent;
22655 else
22656 {
22657 parent = spec_die->parent;
22658 cu = spec_cu;
22659 }
22660
22661 if (parent == NULL)
22662 return "";
22663 else if (parent->building_fullname)
22664 {
22665 const char *name;
22666 const char *parent_name;
22667
22668 /* It has been seen on RealView 2.2 built binaries,
22669 DW_TAG_template_type_param types actually _defined_ as
22670 children of the parent class:
22671
22672 enum E {};
22673 template class <class Enum> Class{};
22674 Class<enum E> class_e;
22675
22676 1: DW_TAG_class_type (Class)
22677 2: DW_TAG_enumeration_type (E)
22678 3: DW_TAG_enumerator (enum1:0)
22679 3: DW_TAG_enumerator (enum2:1)
22680 ...
22681 2: DW_TAG_template_type_param
22682 DW_AT_type DW_FORM_ref_udata (E)
22683
22684 Besides being broken debug info, it can put GDB into an
22685 infinite loop. Consider:
22686
22687 When we're building the full name for Class<E>, we'll start
22688 at Class, and go look over its template type parameters,
22689 finding E. We'll then try to build the full name of E, and
22690 reach here. We're now trying to build the full name of E,
22691 and look over the parent DIE for containing scope. In the
22692 broken case, if we followed the parent DIE of E, we'd again
22693 find Class, and once again go look at its template type
22694 arguments, etc., etc. Simply don't consider such parent die
22695 as source-level parent of this die (it can't be, the language
22696 doesn't allow it), and break the loop here. */
22697 name = dwarf2_name (die, cu);
22698 parent_name = dwarf2_name (parent, cu);
22699 complaint (&symfile_complaints,
22700 _("template param type '%s' defined within parent '%s'"),
22701 name ? name : "<unknown>",
22702 parent_name ? parent_name : "<unknown>");
22703 return "";
22704 }
22705 else
22706 switch (parent->tag)
22707 {
22708 case DW_TAG_namespace:
22709 parent_type = read_type_die (parent, cu);
22710 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22711 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22712 Work around this problem here. */
22713 if (cu->language == language_cplus
22714 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
22715 return "";
22716 /* We give a name to even anonymous namespaces. */
22717 return TYPE_TAG_NAME (parent_type);
22718 case DW_TAG_class_type:
22719 case DW_TAG_interface_type:
22720 case DW_TAG_structure_type:
22721 case DW_TAG_union_type:
22722 case DW_TAG_module:
22723 parent_type = read_type_die (parent, cu);
22724 if (TYPE_TAG_NAME (parent_type) != NULL)
22725 return TYPE_TAG_NAME (parent_type);
22726 else
22727 /* An anonymous structure is only allowed non-static data
22728 members; no typedefs, no member functions, et cetera.
22729 So it does not need a prefix. */
22730 return "";
22731 case DW_TAG_compile_unit:
22732 case DW_TAG_partial_unit:
22733 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22734 if (cu->language == language_cplus
22735 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
22736 && die->child != NULL
22737 && (die->tag == DW_TAG_class_type
22738 || die->tag == DW_TAG_structure_type
22739 || die->tag == DW_TAG_union_type))
22740 {
22741 char *name = guess_full_die_structure_name (die, cu);
22742 if (name != NULL)
22743 return name;
22744 }
22745 return "";
22746 case DW_TAG_enumeration_type:
22747 parent_type = read_type_die (parent, cu);
22748 if (TYPE_DECLARED_CLASS (parent_type))
22749 {
22750 if (TYPE_TAG_NAME (parent_type) != NULL)
22751 return TYPE_TAG_NAME (parent_type);
22752 return "";
22753 }
22754 /* Fall through. */
22755 default:
22756 return determine_prefix (parent, cu);
22757 }
22758 }
22759
22760 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
22761 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
22762 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
22763 an obconcat, otherwise allocate storage for the result. The CU argument is
22764 used to determine the language and hence, the appropriate separator. */
22765
22766 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
22767
22768 static char *
22769 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
22770 int physname, struct dwarf2_cu *cu)
22771 {
22772 const char *lead = "";
22773 const char *sep;
22774
22775 if (suffix == NULL || suffix[0] == '\0'
22776 || prefix == NULL || prefix[0] == '\0')
22777 sep = "";
22778 else if (cu->language == language_d)
22779 {
22780 /* For D, the 'main' function could be defined in any module, but it
22781 should never be prefixed. */
22782 if (strcmp (suffix, "D main") == 0)
22783 {
22784 prefix = "";
22785 sep = "";
22786 }
22787 else
22788 sep = ".";
22789 }
22790 else if (cu->language == language_fortran && physname)
22791 {
22792 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
22793 DW_AT_MIPS_linkage_name is preferred and used instead. */
22794
22795 lead = "__";
22796 sep = "_MOD_";
22797 }
22798 else
22799 sep = "::";
22800
22801 if (prefix == NULL)
22802 prefix = "";
22803 if (suffix == NULL)
22804 suffix = "";
22805
22806 if (obs == NULL)
22807 {
22808 char *retval
22809 = ((char *)
22810 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
22811
22812 strcpy (retval, lead);
22813 strcat (retval, prefix);
22814 strcat (retval, sep);
22815 strcat (retval, suffix);
22816 return retval;
22817 }
22818 else
22819 {
22820 /* We have an obstack. */
22821 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
22822 }
22823 }
22824
22825 /* Return sibling of die, NULL if no sibling. */
22826
22827 static struct die_info *
22828 sibling_die (struct die_info *die)
22829 {
22830 return die->sibling;
22831 }
22832
22833 /* Get name of a die, return NULL if not found. */
22834
22835 static const char *
22836 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
22837 struct obstack *obstack)
22838 {
22839 if (name && cu->language == language_cplus)
22840 {
22841 std::string canon_name = cp_canonicalize_string (name);
22842
22843 if (!canon_name.empty ())
22844 {
22845 if (canon_name != name)
22846 name = (const char *) obstack_copy0 (obstack,
22847 canon_name.c_str (),
22848 canon_name.length ());
22849 }
22850 }
22851
22852 return name;
22853 }
22854
22855 /* Get name of a die, return NULL if not found.
22856 Anonymous namespaces are converted to their magic string. */
22857
22858 static const char *
22859 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
22860 {
22861 struct attribute *attr;
22862 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22863
22864 attr = dwarf2_attr (die, DW_AT_name, cu);
22865 if ((!attr || !DW_STRING (attr))
22866 && die->tag != DW_TAG_namespace
22867 && die->tag != DW_TAG_class_type
22868 && die->tag != DW_TAG_interface_type
22869 && die->tag != DW_TAG_structure_type
22870 && die->tag != DW_TAG_union_type)
22871 return NULL;
22872
22873 switch (die->tag)
22874 {
22875 case DW_TAG_compile_unit:
22876 case DW_TAG_partial_unit:
22877 /* Compilation units have a DW_AT_name that is a filename, not
22878 a source language identifier. */
22879 case DW_TAG_enumeration_type:
22880 case DW_TAG_enumerator:
22881 /* These tags always have simple identifiers already; no need
22882 to canonicalize them. */
22883 return DW_STRING (attr);
22884
22885 case DW_TAG_namespace:
22886 if (attr != NULL && DW_STRING (attr) != NULL)
22887 return DW_STRING (attr);
22888 return CP_ANONYMOUS_NAMESPACE_STR;
22889
22890 case DW_TAG_class_type:
22891 case DW_TAG_interface_type:
22892 case DW_TAG_structure_type:
22893 case DW_TAG_union_type:
22894 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
22895 structures or unions. These were of the form "._%d" in GCC 4.1,
22896 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
22897 and GCC 4.4. We work around this problem by ignoring these. */
22898 if (attr && DW_STRING (attr)
22899 && (startswith (DW_STRING (attr), "._")
22900 || startswith (DW_STRING (attr), "<anonymous")))
22901 return NULL;
22902
22903 /* GCC might emit a nameless typedef that has a linkage name. See
22904 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22905 if (!attr || DW_STRING (attr) == NULL)
22906 {
22907 char *demangled = NULL;
22908
22909 attr = dw2_linkage_name_attr (die, cu);
22910 if (attr == NULL || DW_STRING (attr) == NULL)
22911 return NULL;
22912
22913 /* Avoid demangling DW_STRING (attr) the second time on a second
22914 call for the same DIE. */
22915 if (!DW_STRING_IS_CANONICAL (attr))
22916 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
22917
22918 if (demangled)
22919 {
22920 const char *base;
22921
22922 /* FIXME: we already did this for the partial symbol... */
22923 DW_STRING (attr)
22924 = ((const char *)
22925 obstack_copy0 (&objfile->per_bfd->storage_obstack,
22926 demangled, strlen (demangled)));
22927 DW_STRING_IS_CANONICAL (attr) = 1;
22928 xfree (demangled);
22929
22930 /* Strip any leading namespaces/classes, keep only the base name.
22931 DW_AT_name for named DIEs does not contain the prefixes. */
22932 base = strrchr (DW_STRING (attr), ':');
22933 if (base && base > DW_STRING (attr) && base[-1] == ':')
22934 return &base[1];
22935 else
22936 return DW_STRING (attr);
22937 }
22938 }
22939 break;
22940
22941 default:
22942 break;
22943 }
22944
22945 if (!DW_STRING_IS_CANONICAL (attr))
22946 {
22947 DW_STRING (attr)
22948 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
22949 &objfile->per_bfd->storage_obstack);
22950 DW_STRING_IS_CANONICAL (attr) = 1;
22951 }
22952 return DW_STRING (attr);
22953 }
22954
22955 /* Return the die that this die in an extension of, or NULL if there
22956 is none. *EXT_CU is the CU containing DIE on input, and the CU
22957 containing the return value on output. */
22958
22959 static struct die_info *
22960 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
22961 {
22962 struct attribute *attr;
22963
22964 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
22965 if (attr == NULL)
22966 return NULL;
22967
22968 return follow_die_ref (die, attr, ext_cu);
22969 }
22970
22971 /* Convert a DIE tag into its string name. */
22972
22973 static const char *
22974 dwarf_tag_name (unsigned tag)
22975 {
22976 const char *name = get_DW_TAG_name (tag);
22977
22978 if (name == NULL)
22979 return "DW_TAG_<unknown>";
22980
22981 return name;
22982 }
22983
22984 /* Convert a DWARF attribute code into its string name. */
22985
22986 static const char *
22987 dwarf_attr_name (unsigned attr)
22988 {
22989 const char *name;
22990
22991 #ifdef MIPS /* collides with DW_AT_HP_block_index */
22992 if (attr == DW_AT_MIPS_fde)
22993 return "DW_AT_MIPS_fde";
22994 #else
22995 if (attr == DW_AT_HP_block_index)
22996 return "DW_AT_HP_block_index";
22997 #endif
22998
22999 name = get_DW_AT_name (attr);
23000
23001 if (name == NULL)
23002 return "DW_AT_<unknown>";
23003
23004 return name;
23005 }
23006
23007 /* Convert a DWARF value form code into its string name. */
23008
23009 static const char *
23010 dwarf_form_name (unsigned form)
23011 {
23012 const char *name = get_DW_FORM_name (form);
23013
23014 if (name == NULL)
23015 return "DW_FORM_<unknown>";
23016
23017 return name;
23018 }
23019
23020 static const char *
23021 dwarf_bool_name (unsigned mybool)
23022 {
23023 if (mybool)
23024 return "TRUE";
23025 else
23026 return "FALSE";
23027 }
23028
23029 /* Convert a DWARF type code into its string name. */
23030
23031 static const char *
23032 dwarf_type_encoding_name (unsigned enc)
23033 {
23034 const char *name = get_DW_ATE_name (enc);
23035
23036 if (name == NULL)
23037 return "DW_ATE_<unknown>";
23038
23039 return name;
23040 }
23041
23042 static void
23043 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
23044 {
23045 unsigned int i;
23046
23047 print_spaces (indent, f);
23048 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
23049 dwarf_tag_name (die->tag), die->abbrev,
23050 sect_offset_str (die->sect_off));
23051
23052 if (die->parent != NULL)
23053 {
23054 print_spaces (indent, f);
23055 fprintf_unfiltered (f, " parent at offset: %s\n",
23056 sect_offset_str (die->parent->sect_off));
23057 }
23058
23059 print_spaces (indent, f);
23060 fprintf_unfiltered (f, " has children: %s\n",
23061 dwarf_bool_name (die->child != NULL));
23062
23063 print_spaces (indent, f);
23064 fprintf_unfiltered (f, " attributes:\n");
23065
23066 for (i = 0; i < die->num_attrs; ++i)
23067 {
23068 print_spaces (indent, f);
23069 fprintf_unfiltered (f, " %s (%s) ",
23070 dwarf_attr_name (die->attrs[i].name),
23071 dwarf_form_name (die->attrs[i].form));
23072
23073 switch (die->attrs[i].form)
23074 {
23075 case DW_FORM_addr:
23076 case DW_FORM_GNU_addr_index:
23077 fprintf_unfiltered (f, "address: ");
23078 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
23079 break;
23080 case DW_FORM_block2:
23081 case DW_FORM_block4:
23082 case DW_FORM_block:
23083 case DW_FORM_block1:
23084 fprintf_unfiltered (f, "block: size %s",
23085 pulongest (DW_BLOCK (&die->attrs[i])->size));
23086 break;
23087 case DW_FORM_exprloc:
23088 fprintf_unfiltered (f, "expression: size %s",
23089 pulongest (DW_BLOCK (&die->attrs[i])->size));
23090 break;
23091 case DW_FORM_data16:
23092 fprintf_unfiltered (f, "constant of 16 bytes");
23093 break;
23094 case DW_FORM_ref_addr:
23095 fprintf_unfiltered (f, "ref address: ");
23096 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
23097 break;
23098 case DW_FORM_GNU_ref_alt:
23099 fprintf_unfiltered (f, "alt ref address: ");
23100 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
23101 break;
23102 case DW_FORM_ref1:
23103 case DW_FORM_ref2:
23104 case DW_FORM_ref4:
23105 case DW_FORM_ref8:
23106 case DW_FORM_ref_udata:
23107 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
23108 (long) (DW_UNSND (&die->attrs[i])));
23109 break;
23110 case DW_FORM_data1:
23111 case DW_FORM_data2:
23112 case DW_FORM_data4:
23113 case DW_FORM_data8:
23114 case DW_FORM_udata:
23115 case DW_FORM_sdata:
23116 fprintf_unfiltered (f, "constant: %s",
23117 pulongest (DW_UNSND (&die->attrs[i])));
23118 break;
23119 case DW_FORM_sec_offset:
23120 fprintf_unfiltered (f, "section offset: %s",
23121 pulongest (DW_UNSND (&die->attrs[i])));
23122 break;
23123 case DW_FORM_ref_sig8:
23124 fprintf_unfiltered (f, "signature: %s",
23125 hex_string (DW_SIGNATURE (&die->attrs[i])));
23126 break;
23127 case DW_FORM_string:
23128 case DW_FORM_strp:
23129 case DW_FORM_line_strp:
23130 case DW_FORM_GNU_str_index:
23131 case DW_FORM_GNU_strp_alt:
23132 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
23133 DW_STRING (&die->attrs[i])
23134 ? DW_STRING (&die->attrs[i]) : "",
23135 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
23136 break;
23137 case DW_FORM_flag:
23138 if (DW_UNSND (&die->attrs[i]))
23139 fprintf_unfiltered (f, "flag: TRUE");
23140 else
23141 fprintf_unfiltered (f, "flag: FALSE");
23142 break;
23143 case DW_FORM_flag_present:
23144 fprintf_unfiltered (f, "flag: TRUE");
23145 break;
23146 case DW_FORM_indirect:
23147 /* The reader will have reduced the indirect form to
23148 the "base form" so this form should not occur. */
23149 fprintf_unfiltered (f,
23150 "unexpected attribute form: DW_FORM_indirect");
23151 break;
23152 case DW_FORM_implicit_const:
23153 fprintf_unfiltered (f, "constant: %s",
23154 plongest (DW_SND (&die->attrs[i])));
23155 break;
23156 default:
23157 fprintf_unfiltered (f, "unsupported attribute form: %d.",
23158 die->attrs[i].form);
23159 break;
23160 }
23161 fprintf_unfiltered (f, "\n");
23162 }
23163 }
23164
23165 static void
23166 dump_die_for_error (struct die_info *die)
23167 {
23168 dump_die_shallow (gdb_stderr, 0, die);
23169 }
23170
23171 static void
23172 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
23173 {
23174 int indent = level * 4;
23175
23176 gdb_assert (die != NULL);
23177
23178 if (level >= max_level)
23179 return;
23180
23181 dump_die_shallow (f, indent, die);
23182
23183 if (die->child != NULL)
23184 {
23185 print_spaces (indent, f);
23186 fprintf_unfiltered (f, " Children:");
23187 if (level + 1 < max_level)
23188 {
23189 fprintf_unfiltered (f, "\n");
23190 dump_die_1 (f, level + 1, max_level, die->child);
23191 }
23192 else
23193 {
23194 fprintf_unfiltered (f,
23195 " [not printed, max nesting level reached]\n");
23196 }
23197 }
23198
23199 if (die->sibling != NULL && level > 0)
23200 {
23201 dump_die_1 (f, level, max_level, die->sibling);
23202 }
23203 }
23204
23205 /* This is called from the pdie macro in gdbinit.in.
23206 It's not static so gcc will keep a copy callable from gdb. */
23207
23208 void
23209 dump_die (struct die_info *die, int max_level)
23210 {
23211 dump_die_1 (gdb_stdlog, 0, max_level, die);
23212 }
23213
23214 static void
23215 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
23216 {
23217 void **slot;
23218
23219 slot = htab_find_slot_with_hash (cu->die_hash, die,
23220 to_underlying (die->sect_off),
23221 INSERT);
23222
23223 *slot = die;
23224 }
23225
23226 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
23227 required kind. */
23228
23229 static sect_offset
23230 dwarf2_get_ref_die_offset (const struct attribute *attr)
23231 {
23232 if (attr_form_is_ref (attr))
23233 return (sect_offset) DW_UNSND (attr);
23234
23235 complaint (&symfile_complaints,
23236 _("unsupported die ref attribute form: '%s'"),
23237 dwarf_form_name (attr->form));
23238 return {};
23239 }
23240
23241 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
23242 * the value held by the attribute is not constant. */
23243
23244 static LONGEST
23245 dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
23246 {
23247 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
23248 return DW_SND (attr);
23249 else if (attr->form == DW_FORM_udata
23250 || attr->form == DW_FORM_data1
23251 || attr->form == DW_FORM_data2
23252 || attr->form == DW_FORM_data4
23253 || attr->form == DW_FORM_data8)
23254 return DW_UNSND (attr);
23255 else
23256 {
23257 /* For DW_FORM_data16 see attr_form_is_constant. */
23258 complaint (&symfile_complaints,
23259 _("Attribute value is not a constant (%s)"),
23260 dwarf_form_name (attr->form));
23261 return default_value;
23262 }
23263 }
23264
23265 /* Follow reference or signature attribute ATTR of SRC_DIE.
23266 On entry *REF_CU is the CU of SRC_DIE.
23267 On exit *REF_CU is the CU of the result. */
23268
23269 static struct die_info *
23270 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
23271 struct dwarf2_cu **ref_cu)
23272 {
23273 struct die_info *die;
23274
23275 if (attr_form_is_ref (attr))
23276 die = follow_die_ref (src_die, attr, ref_cu);
23277 else if (attr->form == DW_FORM_ref_sig8)
23278 die = follow_die_sig (src_die, attr, ref_cu);
23279 else
23280 {
23281 dump_die_for_error (src_die);
23282 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
23283 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
23284 }
23285
23286 return die;
23287 }
23288
23289 /* Follow reference OFFSET.
23290 On entry *REF_CU is the CU of the source die referencing OFFSET.
23291 On exit *REF_CU is the CU of the result.
23292 Returns NULL if OFFSET is invalid. */
23293
23294 static struct die_info *
23295 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
23296 struct dwarf2_cu **ref_cu)
23297 {
23298 struct die_info temp_die;
23299 struct dwarf2_cu *target_cu, *cu = *ref_cu;
23300 struct dwarf2_per_objfile *dwarf2_per_objfile
23301 = cu->per_cu->dwarf2_per_objfile;
23302 struct objfile *objfile = dwarf2_per_objfile->objfile;
23303
23304 gdb_assert (cu->per_cu != NULL);
23305
23306 target_cu = cu;
23307
23308 if (cu->per_cu->is_debug_types)
23309 {
23310 /* .debug_types CUs cannot reference anything outside their CU.
23311 If they need to, they have to reference a signatured type via
23312 DW_FORM_ref_sig8. */
23313 if (!offset_in_cu_p (&cu->header, sect_off))
23314 return NULL;
23315 }
23316 else if (offset_in_dwz != cu->per_cu->is_dwz
23317 || !offset_in_cu_p (&cu->header, sect_off))
23318 {
23319 struct dwarf2_per_cu_data *per_cu;
23320
23321 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
23322 dwarf2_per_objfile);
23323
23324 /* If necessary, add it to the queue and load its DIEs. */
23325 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
23326 load_full_comp_unit (per_cu, cu->language);
23327
23328 target_cu = per_cu->cu;
23329 }
23330 else if (cu->dies == NULL)
23331 {
23332 /* We're loading full DIEs during partial symbol reading. */
23333 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
23334 load_full_comp_unit (cu->per_cu, language_minimal);
23335 }
23336
23337 *ref_cu = target_cu;
23338 temp_die.sect_off = sect_off;
23339 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
23340 &temp_die,
23341 to_underlying (sect_off));
23342 }
23343
23344 /* Follow reference attribute ATTR of SRC_DIE.
23345 On entry *REF_CU is the CU of SRC_DIE.
23346 On exit *REF_CU is the CU of the result. */
23347
23348 static struct die_info *
23349 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
23350 struct dwarf2_cu **ref_cu)
23351 {
23352 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
23353 struct dwarf2_cu *cu = *ref_cu;
23354 struct die_info *die;
23355
23356 die = follow_die_offset (sect_off,
23357 (attr->form == DW_FORM_GNU_ref_alt
23358 || cu->per_cu->is_dwz),
23359 ref_cu);
23360 if (!die)
23361 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
23362 "at %s [in module %s]"),
23363 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
23364 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
23365
23366 return die;
23367 }
23368
23369 /* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
23370 Returned value is intended for DW_OP_call*. Returned
23371 dwarf2_locexpr_baton->data has lifetime of
23372 PER_CU->DWARF2_PER_OBJFILE->OBJFILE. */
23373
23374 struct dwarf2_locexpr_baton
23375 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
23376 struct dwarf2_per_cu_data *per_cu,
23377 CORE_ADDR (*get_frame_pc) (void *baton),
23378 void *baton)
23379 {
23380 struct dwarf2_cu *cu;
23381 struct die_info *die;
23382 struct attribute *attr;
23383 struct dwarf2_locexpr_baton retval;
23384 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
23385 struct dwarf2_per_objfile *dwarf2_per_objfile
23386 = get_dwarf2_per_objfile (objfile);
23387
23388 if (per_cu->cu == NULL)
23389 load_cu (per_cu);
23390 cu = per_cu->cu;
23391 if (cu == NULL)
23392 {
23393 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23394 Instead just throw an error, not much else we can do. */
23395 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23396 sect_offset_str (sect_off), objfile_name (objfile));
23397 }
23398
23399 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23400 if (!die)
23401 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23402 sect_offset_str (sect_off), objfile_name (objfile));
23403
23404 attr = dwarf2_attr (die, DW_AT_location, cu);
23405 if (!attr)
23406 {
23407 /* DWARF: "If there is no such attribute, then there is no effect.".
23408 DATA is ignored if SIZE is 0. */
23409
23410 retval.data = NULL;
23411 retval.size = 0;
23412 }
23413 else if (attr_form_is_section_offset (attr))
23414 {
23415 struct dwarf2_loclist_baton loclist_baton;
23416 CORE_ADDR pc = (*get_frame_pc) (baton);
23417 size_t size;
23418
23419 fill_in_loclist_baton (cu, &loclist_baton, attr);
23420
23421 retval.data = dwarf2_find_location_expression (&loclist_baton,
23422 &size, pc);
23423 retval.size = size;
23424 }
23425 else
23426 {
23427 if (!attr_form_is_block (attr))
23428 error (_("Dwarf Error: DIE at %s referenced in module %s "
23429 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
23430 sect_offset_str (sect_off), objfile_name (objfile));
23431
23432 retval.data = DW_BLOCK (attr)->data;
23433 retval.size = DW_BLOCK (attr)->size;
23434 }
23435 retval.per_cu = cu->per_cu;
23436
23437 age_cached_comp_units (dwarf2_per_objfile);
23438
23439 return retval;
23440 }
23441
23442 /* Like dwarf2_fetch_die_loc_sect_off, but take a CU
23443 offset. */
23444
23445 struct dwarf2_locexpr_baton
23446 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
23447 struct dwarf2_per_cu_data *per_cu,
23448 CORE_ADDR (*get_frame_pc) (void *baton),
23449 void *baton)
23450 {
23451 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
23452
23453 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
23454 }
23455
23456 /* Write a constant of a given type as target-ordered bytes into
23457 OBSTACK. */
23458
23459 static const gdb_byte *
23460 write_constant_as_bytes (struct obstack *obstack,
23461 enum bfd_endian byte_order,
23462 struct type *type,
23463 ULONGEST value,
23464 LONGEST *len)
23465 {
23466 gdb_byte *result;
23467
23468 *len = TYPE_LENGTH (type);
23469 result = (gdb_byte *) obstack_alloc (obstack, *len);
23470 store_unsigned_integer (result, *len, byte_order, value);
23471
23472 return result;
23473 }
23474
23475 /* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
23476 pointer to the constant bytes and set LEN to the length of the
23477 data. If memory is needed, allocate it on OBSTACK. If the DIE
23478 does not have a DW_AT_const_value, return NULL. */
23479
23480 const gdb_byte *
23481 dwarf2_fetch_constant_bytes (sect_offset sect_off,
23482 struct dwarf2_per_cu_data *per_cu,
23483 struct obstack *obstack,
23484 LONGEST *len)
23485 {
23486 struct dwarf2_cu *cu;
23487 struct die_info *die;
23488 struct attribute *attr;
23489 const gdb_byte *result = NULL;
23490 struct type *type;
23491 LONGEST value;
23492 enum bfd_endian byte_order;
23493 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
23494
23495 if (per_cu->cu == NULL)
23496 load_cu (per_cu);
23497 cu = per_cu->cu;
23498 if (cu == NULL)
23499 {
23500 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23501 Instead just throw an error, not much else we can do. */
23502 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23503 sect_offset_str (sect_off), objfile_name (objfile));
23504 }
23505
23506 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23507 if (!die)
23508 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23509 sect_offset_str (sect_off), objfile_name (objfile));
23510
23511 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23512 if (attr == NULL)
23513 return NULL;
23514
23515 byte_order = (bfd_big_endian (objfile->obfd)
23516 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23517
23518 switch (attr->form)
23519 {
23520 case DW_FORM_addr:
23521 case DW_FORM_GNU_addr_index:
23522 {
23523 gdb_byte *tem;
23524
23525 *len = cu->header.addr_size;
23526 tem = (gdb_byte *) obstack_alloc (obstack, *len);
23527 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
23528 result = tem;
23529 }
23530 break;
23531 case DW_FORM_string:
23532 case DW_FORM_strp:
23533 case DW_FORM_GNU_str_index:
23534 case DW_FORM_GNU_strp_alt:
23535 /* DW_STRING is already allocated on the objfile obstack, point
23536 directly to it. */
23537 result = (const gdb_byte *) DW_STRING (attr);
23538 *len = strlen (DW_STRING (attr));
23539 break;
23540 case DW_FORM_block1:
23541 case DW_FORM_block2:
23542 case DW_FORM_block4:
23543 case DW_FORM_block:
23544 case DW_FORM_exprloc:
23545 case DW_FORM_data16:
23546 result = DW_BLOCK (attr)->data;
23547 *len = DW_BLOCK (attr)->size;
23548 break;
23549
23550 /* The DW_AT_const_value attributes are supposed to carry the
23551 symbol's value "represented as it would be on the target
23552 architecture." By the time we get here, it's already been
23553 converted to host endianness, so we just need to sign- or
23554 zero-extend it as appropriate. */
23555 case DW_FORM_data1:
23556 type = die_type (die, cu);
23557 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23558 if (result == NULL)
23559 result = write_constant_as_bytes (obstack, byte_order,
23560 type, value, len);
23561 break;
23562 case DW_FORM_data2:
23563 type = die_type (die, cu);
23564 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23565 if (result == NULL)
23566 result = write_constant_as_bytes (obstack, byte_order,
23567 type, value, len);
23568 break;
23569 case DW_FORM_data4:
23570 type = die_type (die, cu);
23571 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23572 if (result == NULL)
23573 result = write_constant_as_bytes (obstack, byte_order,
23574 type, value, len);
23575 break;
23576 case DW_FORM_data8:
23577 type = die_type (die, cu);
23578 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23579 if (result == NULL)
23580 result = write_constant_as_bytes (obstack, byte_order,
23581 type, value, len);
23582 break;
23583
23584 case DW_FORM_sdata:
23585 case DW_FORM_implicit_const:
23586 type = die_type (die, cu);
23587 result = write_constant_as_bytes (obstack, byte_order,
23588 type, DW_SND (attr), len);
23589 break;
23590
23591 case DW_FORM_udata:
23592 type = die_type (die, cu);
23593 result = write_constant_as_bytes (obstack, byte_order,
23594 type, DW_UNSND (attr), len);
23595 break;
23596
23597 default:
23598 complaint (&symfile_complaints,
23599 _("unsupported const value attribute form: '%s'"),
23600 dwarf_form_name (attr->form));
23601 break;
23602 }
23603
23604 return result;
23605 }
23606
23607 /* Return the type of the die at OFFSET in PER_CU. Return NULL if no
23608 valid type for this die is found. */
23609
23610 struct type *
23611 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
23612 struct dwarf2_per_cu_data *per_cu)
23613 {
23614 struct dwarf2_cu *cu;
23615 struct die_info *die;
23616
23617 if (per_cu->cu == NULL)
23618 load_cu (per_cu);
23619 cu = per_cu->cu;
23620 if (!cu)
23621 return NULL;
23622
23623 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23624 if (!die)
23625 return NULL;
23626
23627 return die_type (die, cu);
23628 }
23629
23630 /* Return the type of the DIE at DIE_OFFSET in the CU named by
23631 PER_CU. */
23632
23633 struct type *
23634 dwarf2_get_die_type (cu_offset die_offset,
23635 struct dwarf2_per_cu_data *per_cu)
23636 {
23637 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
23638 return get_die_type_at_offset (die_offset_sect, per_cu);
23639 }
23640
23641 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
23642 On entry *REF_CU is the CU of SRC_DIE.
23643 On exit *REF_CU is the CU of the result.
23644 Returns NULL if the referenced DIE isn't found. */
23645
23646 static struct die_info *
23647 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23648 struct dwarf2_cu **ref_cu)
23649 {
23650 struct die_info temp_die;
23651 struct dwarf2_cu *sig_cu;
23652 struct die_info *die;
23653
23654 /* While it might be nice to assert sig_type->type == NULL here,
23655 we can get here for DW_AT_imported_declaration where we need
23656 the DIE not the type. */
23657
23658 /* If necessary, add it to the queue and load its DIEs. */
23659
23660 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
23661 read_signatured_type (sig_type);
23662
23663 sig_cu = sig_type->per_cu.cu;
23664 gdb_assert (sig_cu != NULL);
23665 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23666 temp_die.sect_off = sig_type->type_offset_in_section;
23667 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
23668 to_underlying (temp_die.sect_off));
23669 if (die)
23670 {
23671 struct dwarf2_per_objfile *dwarf2_per_objfile
23672 = (*ref_cu)->per_cu->dwarf2_per_objfile;
23673
23674 /* For .gdb_index version 7 keep track of included TUs.
23675 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
23676 if (dwarf2_per_objfile->index_table != NULL
23677 && dwarf2_per_objfile->index_table->version <= 7)
23678 {
23679 VEC_safe_push (dwarf2_per_cu_ptr,
23680 (*ref_cu)->per_cu->imported_symtabs,
23681 sig_cu->per_cu);
23682 }
23683
23684 *ref_cu = sig_cu;
23685 return die;
23686 }
23687
23688 return NULL;
23689 }
23690
23691 /* Follow signatured type referenced by ATTR in SRC_DIE.
23692 On entry *REF_CU is the CU of SRC_DIE.
23693 On exit *REF_CU is the CU of the result.
23694 The result is the DIE of the type.
23695 If the referenced type cannot be found an error is thrown. */
23696
23697 static struct die_info *
23698 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
23699 struct dwarf2_cu **ref_cu)
23700 {
23701 ULONGEST signature = DW_SIGNATURE (attr);
23702 struct signatured_type *sig_type;
23703 struct die_info *die;
23704
23705 gdb_assert (attr->form == DW_FORM_ref_sig8);
23706
23707 sig_type = lookup_signatured_type (*ref_cu, signature);
23708 /* sig_type will be NULL if the signatured type is missing from
23709 the debug info. */
23710 if (sig_type == NULL)
23711 {
23712 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23713 " from DIE at %s [in module %s]"),
23714 hex_string (signature), sect_offset_str (src_die->sect_off),
23715 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
23716 }
23717
23718 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23719 if (die == NULL)
23720 {
23721 dump_die_for_error (src_die);
23722 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
23723 " from DIE at %s [in module %s]"),
23724 hex_string (signature), sect_offset_str (src_die->sect_off),
23725 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
23726 }
23727
23728 return die;
23729 }
23730
23731 /* Get the type specified by SIGNATURE referenced in DIE/CU,
23732 reading in and processing the type unit if necessary. */
23733
23734 static struct type *
23735 get_signatured_type (struct die_info *die, ULONGEST signature,
23736 struct dwarf2_cu *cu)
23737 {
23738 struct dwarf2_per_objfile *dwarf2_per_objfile
23739 = cu->per_cu->dwarf2_per_objfile;
23740 struct signatured_type *sig_type;
23741 struct dwarf2_cu *type_cu;
23742 struct die_info *type_die;
23743 struct type *type;
23744
23745 sig_type = lookup_signatured_type (cu, signature);
23746 /* sig_type will be NULL if the signatured type is missing from
23747 the debug info. */
23748 if (sig_type == NULL)
23749 {
23750 complaint (&symfile_complaints,
23751 _("Dwarf Error: Cannot find signatured DIE %s referenced"
23752 " from DIE at %s [in module %s]"),
23753 hex_string (signature), sect_offset_str (die->sect_off),
23754 objfile_name (dwarf2_per_objfile->objfile));
23755 return build_error_marker_type (cu, die);
23756 }
23757
23758 /* If we already know the type we're done. */
23759 if (sig_type->type != NULL)
23760 return sig_type->type;
23761
23762 type_cu = cu;
23763 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
23764 if (type_die != NULL)
23765 {
23766 /* N.B. We need to call get_die_type to ensure only one type for this DIE
23767 is created. This is important, for example, because for c++ classes
23768 we need TYPE_NAME set which is only done by new_symbol. Blech. */
23769 type = read_type_die (type_die, type_cu);
23770 if (type == NULL)
23771 {
23772 complaint (&symfile_complaints,
23773 _("Dwarf Error: Cannot build signatured type %s"
23774 " referenced from DIE at %s [in module %s]"),
23775 hex_string (signature), sect_offset_str (die->sect_off),
23776 objfile_name (dwarf2_per_objfile->objfile));
23777 type = build_error_marker_type (cu, die);
23778 }
23779 }
23780 else
23781 {
23782 complaint (&symfile_complaints,
23783 _("Dwarf Error: Problem reading signatured DIE %s referenced"
23784 " from DIE at %s [in module %s]"),
23785 hex_string (signature), sect_offset_str (die->sect_off),
23786 objfile_name (dwarf2_per_objfile->objfile));
23787 type = build_error_marker_type (cu, die);
23788 }
23789 sig_type->type = type;
23790
23791 return type;
23792 }
23793
23794 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
23795 reading in and processing the type unit if necessary. */
23796
23797 static struct type *
23798 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
23799 struct dwarf2_cu *cu) /* ARI: editCase function */
23800 {
23801 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
23802 if (attr_form_is_ref (attr))
23803 {
23804 struct dwarf2_cu *type_cu = cu;
23805 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
23806
23807 return read_type_die (type_die, type_cu);
23808 }
23809 else if (attr->form == DW_FORM_ref_sig8)
23810 {
23811 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
23812 }
23813 else
23814 {
23815 struct dwarf2_per_objfile *dwarf2_per_objfile
23816 = cu->per_cu->dwarf2_per_objfile;
23817
23818 complaint (&symfile_complaints,
23819 _("Dwarf Error: DW_AT_signature has bad form %s in DIE"
23820 " at %s [in module %s]"),
23821 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
23822 objfile_name (dwarf2_per_objfile->objfile));
23823 return build_error_marker_type (cu, die);
23824 }
23825 }
23826
23827 /* Load the DIEs associated with type unit PER_CU into memory. */
23828
23829 static void
23830 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
23831 {
23832 struct signatured_type *sig_type;
23833
23834 /* Caller is responsible for ensuring type_unit_groups don't get here. */
23835 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
23836
23837 /* We have the per_cu, but we need the signatured_type.
23838 Fortunately this is an easy translation. */
23839 gdb_assert (per_cu->is_debug_types);
23840 sig_type = (struct signatured_type *) per_cu;
23841
23842 gdb_assert (per_cu->cu == NULL);
23843
23844 read_signatured_type (sig_type);
23845
23846 gdb_assert (per_cu->cu != NULL);
23847 }
23848
23849 /* die_reader_func for read_signatured_type.
23850 This is identical to load_full_comp_unit_reader,
23851 but is kept separate for now. */
23852
23853 static void
23854 read_signatured_type_reader (const struct die_reader_specs *reader,
23855 const gdb_byte *info_ptr,
23856 struct die_info *comp_unit_die,
23857 int has_children,
23858 void *data)
23859 {
23860 struct dwarf2_cu *cu = reader->cu;
23861
23862 gdb_assert (cu->die_hash == NULL);
23863 cu->die_hash =
23864 htab_create_alloc_ex (cu->header.length / 12,
23865 die_hash,
23866 die_eq,
23867 NULL,
23868 &cu->comp_unit_obstack,
23869 hashtab_obstack_allocate,
23870 dummy_obstack_deallocate);
23871
23872 if (has_children)
23873 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
23874 &info_ptr, comp_unit_die);
23875 cu->dies = comp_unit_die;
23876 /* comp_unit_die is not stored in die_hash, no need. */
23877
23878 /* We try not to read any attributes in this function, because not
23879 all CUs needed for references have been loaded yet, and symbol
23880 table processing isn't initialized. But we have to set the CU language,
23881 or we won't be able to build types correctly.
23882 Similarly, if we do not read the producer, we can not apply
23883 producer-specific interpretation. */
23884 prepare_one_comp_unit (cu, cu->dies, language_minimal);
23885 }
23886
23887 /* Read in a signatured type and build its CU and DIEs.
23888 If the type is a stub for the real type in a DWO file,
23889 read in the real type from the DWO file as well. */
23890
23891 static void
23892 read_signatured_type (struct signatured_type *sig_type)
23893 {
23894 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
23895
23896 gdb_assert (per_cu->is_debug_types);
23897 gdb_assert (per_cu->cu == NULL);
23898
23899 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
23900 read_signatured_type_reader, NULL);
23901 sig_type->per_cu.tu_read = 1;
23902 }
23903
23904 /* Decode simple location descriptions.
23905 Given a pointer to a dwarf block that defines a location, compute
23906 the location and return the value.
23907
23908 NOTE drow/2003-11-18: This function is called in two situations
23909 now: for the address of static or global variables (partial symbols
23910 only) and for offsets into structures which are expected to be
23911 (more or less) constant. The partial symbol case should go away,
23912 and only the constant case should remain. That will let this
23913 function complain more accurately. A few special modes are allowed
23914 without complaint for global variables (for instance, global
23915 register values and thread-local values).
23916
23917 A location description containing no operations indicates that the
23918 object is optimized out. The return value is 0 for that case.
23919 FIXME drow/2003-11-16: No callers check for this case any more; soon all
23920 callers will only want a very basic result and this can become a
23921 complaint.
23922
23923 Note that stack[0] is unused except as a default error return. */
23924
23925 static CORE_ADDR
23926 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
23927 {
23928 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
23929 size_t i;
23930 size_t size = blk->size;
23931 const gdb_byte *data = blk->data;
23932 CORE_ADDR stack[64];
23933 int stacki;
23934 unsigned int bytes_read, unsnd;
23935 gdb_byte op;
23936
23937 i = 0;
23938 stacki = 0;
23939 stack[stacki] = 0;
23940 stack[++stacki] = 0;
23941
23942 while (i < size)
23943 {
23944 op = data[i++];
23945 switch (op)
23946 {
23947 case DW_OP_lit0:
23948 case DW_OP_lit1:
23949 case DW_OP_lit2:
23950 case DW_OP_lit3:
23951 case DW_OP_lit4:
23952 case DW_OP_lit5:
23953 case DW_OP_lit6:
23954 case DW_OP_lit7:
23955 case DW_OP_lit8:
23956 case DW_OP_lit9:
23957 case DW_OP_lit10:
23958 case DW_OP_lit11:
23959 case DW_OP_lit12:
23960 case DW_OP_lit13:
23961 case DW_OP_lit14:
23962 case DW_OP_lit15:
23963 case DW_OP_lit16:
23964 case DW_OP_lit17:
23965 case DW_OP_lit18:
23966 case DW_OP_lit19:
23967 case DW_OP_lit20:
23968 case DW_OP_lit21:
23969 case DW_OP_lit22:
23970 case DW_OP_lit23:
23971 case DW_OP_lit24:
23972 case DW_OP_lit25:
23973 case DW_OP_lit26:
23974 case DW_OP_lit27:
23975 case DW_OP_lit28:
23976 case DW_OP_lit29:
23977 case DW_OP_lit30:
23978 case DW_OP_lit31:
23979 stack[++stacki] = op - DW_OP_lit0;
23980 break;
23981
23982 case DW_OP_reg0:
23983 case DW_OP_reg1:
23984 case DW_OP_reg2:
23985 case DW_OP_reg3:
23986 case DW_OP_reg4:
23987 case DW_OP_reg5:
23988 case DW_OP_reg6:
23989 case DW_OP_reg7:
23990 case DW_OP_reg8:
23991 case DW_OP_reg9:
23992 case DW_OP_reg10:
23993 case DW_OP_reg11:
23994 case DW_OP_reg12:
23995 case DW_OP_reg13:
23996 case DW_OP_reg14:
23997 case DW_OP_reg15:
23998 case DW_OP_reg16:
23999 case DW_OP_reg17:
24000 case DW_OP_reg18:
24001 case DW_OP_reg19:
24002 case DW_OP_reg20:
24003 case DW_OP_reg21:
24004 case DW_OP_reg22:
24005 case DW_OP_reg23:
24006 case DW_OP_reg24:
24007 case DW_OP_reg25:
24008 case DW_OP_reg26:
24009 case DW_OP_reg27:
24010 case DW_OP_reg28:
24011 case DW_OP_reg29:
24012 case DW_OP_reg30:
24013 case DW_OP_reg31:
24014 stack[++stacki] = op - DW_OP_reg0;
24015 if (i < size)
24016 dwarf2_complex_location_expr_complaint ();
24017 break;
24018
24019 case DW_OP_regx:
24020 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
24021 i += bytes_read;
24022 stack[++stacki] = unsnd;
24023 if (i < size)
24024 dwarf2_complex_location_expr_complaint ();
24025 break;
24026
24027 case DW_OP_addr:
24028 stack[++stacki] = read_address (objfile->obfd, &data[i],
24029 cu, &bytes_read);
24030 i += bytes_read;
24031 break;
24032
24033 case DW_OP_const1u:
24034 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
24035 i += 1;
24036 break;
24037
24038 case DW_OP_const1s:
24039 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
24040 i += 1;
24041 break;
24042
24043 case DW_OP_const2u:
24044 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
24045 i += 2;
24046 break;
24047
24048 case DW_OP_const2s:
24049 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
24050 i += 2;
24051 break;
24052
24053 case DW_OP_const4u:
24054 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
24055 i += 4;
24056 break;
24057
24058 case DW_OP_const4s:
24059 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
24060 i += 4;
24061 break;
24062
24063 case DW_OP_const8u:
24064 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
24065 i += 8;
24066 break;
24067
24068 case DW_OP_constu:
24069 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
24070 &bytes_read);
24071 i += bytes_read;
24072 break;
24073
24074 case DW_OP_consts:
24075 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
24076 i += bytes_read;
24077 break;
24078
24079 case DW_OP_dup:
24080 stack[stacki + 1] = stack[stacki];
24081 stacki++;
24082 break;
24083
24084 case DW_OP_plus:
24085 stack[stacki - 1] += stack[stacki];
24086 stacki--;
24087 break;
24088
24089 case DW_OP_plus_uconst:
24090 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
24091 &bytes_read);
24092 i += bytes_read;
24093 break;
24094
24095 case DW_OP_minus:
24096 stack[stacki - 1] -= stack[stacki];
24097 stacki--;
24098 break;
24099
24100 case DW_OP_deref:
24101 /* If we're not the last op, then we definitely can't encode
24102 this using GDB's address_class enum. This is valid for partial
24103 global symbols, although the variable's address will be bogus
24104 in the psymtab. */
24105 if (i < size)
24106 dwarf2_complex_location_expr_complaint ();
24107 break;
24108
24109 case DW_OP_GNU_push_tls_address:
24110 case DW_OP_form_tls_address:
24111 /* The top of the stack has the offset from the beginning
24112 of the thread control block at which the variable is located. */
24113 /* Nothing should follow this operator, so the top of stack would
24114 be returned. */
24115 /* This is valid for partial global symbols, but the variable's
24116 address will be bogus in the psymtab. Make it always at least
24117 non-zero to not look as a variable garbage collected by linker
24118 which have DW_OP_addr 0. */
24119 if (i < size)
24120 dwarf2_complex_location_expr_complaint ();
24121 stack[stacki]++;
24122 break;
24123
24124 case DW_OP_GNU_uninit:
24125 break;
24126
24127 case DW_OP_GNU_addr_index:
24128 case DW_OP_GNU_const_index:
24129 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
24130 &bytes_read);
24131 i += bytes_read;
24132 break;
24133
24134 default:
24135 {
24136 const char *name = get_DW_OP_name (op);
24137
24138 if (name)
24139 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
24140 name);
24141 else
24142 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
24143 op);
24144 }
24145
24146 return (stack[stacki]);
24147 }
24148
24149 /* Enforce maximum stack depth of SIZE-1 to avoid writing
24150 outside of the allocated space. Also enforce minimum>0. */
24151 if (stacki >= ARRAY_SIZE (stack) - 1)
24152 {
24153 complaint (&symfile_complaints,
24154 _("location description stack overflow"));
24155 return 0;
24156 }
24157
24158 if (stacki <= 0)
24159 {
24160 complaint (&symfile_complaints,
24161 _("location description stack underflow"));
24162 return 0;
24163 }
24164 }
24165 return (stack[stacki]);
24166 }
24167
24168 /* memory allocation interface */
24169
24170 static struct dwarf_block *
24171 dwarf_alloc_block (struct dwarf2_cu *cu)
24172 {
24173 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
24174 }
24175
24176 static struct die_info *
24177 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
24178 {
24179 struct die_info *die;
24180 size_t size = sizeof (struct die_info);
24181
24182 if (num_attrs > 1)
24183 size += (num_attrs - 1) * sizeof (struct attribute);
24184
24185 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
24186 memset (die, 0, sizeof (struct die_info));
24187 return (die);
24188 }
24189
24190 \f
24191 /* Macro support. */
24192
24193 /* Return file name relative to the compilation directory of file number I in
24194 *LH's file name table. The result is allocated using xmalloc; the caller is
24195 responsible for freeing it. */
24196
24197 static char *
24198 file_file_name (int file, struct line_header *lh)
24199 {
24200 /* Is the file number a valid index into the line header's file name
24201 table? Remember that file numbers start with one, not zero. */
24202 if (1 <= file && file <= lh->file_names.size ())
24203 {
24204 const file_entry &fe = lh->file_names[file - 1];
24205
24206 if (!IS_ABSOLUTE_PATH (fe.name))
24207 {
24208 const char *dir = fe.include_dir (lh);
24209 if (dir != NULL)
24210 return concat (dir, SLASH_STRING, fe.name, (char *) NULL);
24211 }
24212 return xstrdup (fe.name);
24213 }
24214 else
24215 {
24216 /* The compiler produced a bogus file number. We can at least
24217 record the macro definitions made in the file, even if we
24218 won't be able to find the file by name. */
24219 char fake_name[80];
24220
24221 xsnprintf (fake_name, sizeof (fake_name),
24222 "<bad macro file number %d>", file);
24223
24224 complaint (&symfile_complaints,
24225 _("bad file number in macro information (%d)"),
24226 file);
24227
24228 return xstrdup (fake_name);
24229 }
24230 }
24231
24232 /* Return the full name of file number I in *LH's file name table.
24233 Use COMP_DIR as the name of the current directory of the
24234 compilation. The result is allocated using xmalloc; the caller is
24235 responsible for freeing it. */
24236 static char *
24237 file_full_name (int file, struct line_header *lh, const char *comp_dir)
24238 {
24239 /* Is the file number a valid index into the line header's file name
24240 table? Remember that file numbers start with one, not zero. */
24241 if (1 <= file && file <= lh->file_names.size ())
24242 {
24243 char *relative = file_file_name (file, lh);
24244
24245 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
24246 return relative;
24247 return reconcat (relative, comp_dir, SLASH_STRING,
24248 relative, (char *) NULL);
24249 }
24250 else
24251 return file_file_name (file, lh);
24252 }
24253
24254
24255 static struct macro_source_file *
24256 macro_start_file (int file, int line,
24257 struct macro_source_file *current_file,
24258 struct line_header *lh)
24259 {
24260 /* File name relative to the compilation directory of this source file. */
24261 char *file_name = file_file_name (file, lh);
24262
24263 if (! current_file)
24264 {
24265 /* Note: We don't create a macro table for this compilation unit
24266 at all until we actually get a filename. */
24267 struct macro_table *macro_table = get_macro_table ();
24268
24269 /* If we have no current file, then this must be the start_file
24270 directive for the compilation unit's main source file. */
24271 current_file = macro_set_main (macro_table, file_name);
24272 macro_define_special (macro_table);
24273 }
24274 else
24275 current_file = macro_include (current_file, line, file_name);
24276
24277 xfree (file_name);
24278
24279 return current_file;
24280 }
24281
24282 static const char *
24283 consume_improper_spaces (const char *p, const char *body)
24284 {
24285 if (*p == ' ')
24286 {
24287 complaint (&symfile_complaints,
24288 _("macro definition contains spaces "
24289 "in formal argument list:\n`%s'"),
24290 body);
24291
24292 while (*p == ' ')
24293 p++;
24294 }
24295
24296 return p;
24297 }
24298
24299
24300 static void
24301 parse_macro_definition (struct macro_source_file *file, int line,
24302 const char *body)
24303 {
24304 const char *p;
24305
24306 /* The body string takes one of two forms. For object-like macro
24307 definitions, it should be:
24308
24309 <macro name> " " <definition>
24310
24311 For function-like macro definitions, it should be:
24312
24313 <macro name> "() " <definition>
24314 or
24315 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
24316
24317 Spaces may appear only where explicitly indicated, and in the
24318 <definition>.
24319
24320 The Dwarf 2 spec says that an object-like macro's name is always
24321 followed by a space, but versions of GCC around March 2002 omit
24322 the space when the macro's definition is the empty string.
24323
24324 The Dwarf 2 spec says that there should be no spaces between the
24325 formal arguments in a function-like macro's formal argument list,
24326 but versions of GCC around March 2002 include spaces after the
24327 commas. */
24328
24329
24330 /* Find the extent of the macro name. The macro name is terminated
24331 by either a space or null character (for an object-like macro) or
24332 an opening paren (for a function-like macro). */
24333 for (p = body; *p; p++)
24334 if (*p == ' ' || *p == '(')
24335 break;
24336
24337 if (*p == ' ' || *p == '\0')
24338 {
24339 /* It's an object-like macro. */
24340 int name_len = p - body;
24341 char *name = savestring (body, name_len);
24342 const char *replacement;
24343
24344 if (*p == ' ')
24345 replacement = body + name_len + 1;
24346 else
24347 {
24348 dwarf2_macro_malformed_definition_complaint (body);
24349 replacement = body + name_len;
24350 }
24351
24352 macro_define_object (file, line, name, replacement);
24353
24354 xfree (name);
24355 }
24356 else if (*p == '(')
24357 {
24358 /* It's a function-like macro. */
24359 char *name = savestring (body, p - body);
24360 int argc = 0;
24361 int argv_size = 1;
24362 char **argv = XNEWVEC (char *, argv_size);
24363
24364 p++;
24365
24366 p = consume_improper_spaces (p, body);
24367
24368 /* Parse the formal argument list. */
24369 while (*p && *p != ')')
24370 {
24371 /* Find the extent of the current argument name. */
24372 const char *arg_start = p;
24373
24374 while (*p && *p != ',' && *p != ')' && *p != ' ')
24375 p++;
24376
24377 if (! *p || p == arg_start)
24378 dwarf2_macro_malformed_definition_complaint (body);
24379 else
24380 {
24381 /* Make sure argv has room for the new argument. */
24382 if (argc >= argv_size)
24383 {
24384 argv_size *= 2;
24385 argv = XRESIZEVEC (char *, argv, argv_size);
24386 }
24387
24388 argv[argc++] = savestring (arg_start, p - arg_start);
24389 }
24390
24391 p = consume_improper_spaces (p, body);
24392
24393 /* Consume the comma, if present. */
24394 if (*p == ',')
24395 {
24396 p++;
24397
24398 p = consume_improper_spaces (p, body);
24399 }
24400 }
24401
24402 if (*p == ')')
24403 {
24404 p++;
24405
24406 if (*p == ' ')
24407 /* Perfectly formed definition, no complaints. */
24408 macro_define_function (file, line, name,
24409 argc, (const char **) argv,
24410 p + 1);
24411 else if (*p == '\0')
24412 {
24413 /* Complain, but do define it. */
24414 dwarf2_macro_malformed_definition_complaint (body);
24415 macro_define_function (file, line, name,
24416 argc, (const char **) argv,
24417 p);
24418 }
24419 else
24420 /* Just complain. */
24421 dwarf2_macro_malformed_definition_complaint (body);
24422 }
24423 else
24424 /* Just complain. */
24425 dwarf2_macro_malformed_definition_complaint (body);
24426
24427 xfree (name);
24428 {
24429 int i;
24430
24431 for (i = 0; i < argc; i++)
24432 xfree (argv[i]);
24433 }
24434 xfree (argv);
24435 }
24436 else
24437 dwarf2_macro_malformed_definition_complaint (body);
24438 }
24439
24440 /* Skip some bytes from BYTES according to the form given in FORM.
24441 Returns the new pointer. */
24442
24443 static const gdb_byte *
24444 skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
24445 enum dwarf_form form,
24446 unsigned int offset_size,
24447 struct dwarf2_section_info *section)
24448 {
24449 unsigned int bytes_read;
24450
24451 switch (form)
24452 {
24453 case DW_FORM_data1:
24454 case DW_FORM_flag:
24455 ++bytes;
24456 break;
24457
24458 case DW_FORM_data2:
24459 bytes += 2;
24460 break;
24461
24462 case DW_FORM_data4:
24463 bytes += 4;
24464 break;
24465
24466 case DW_FORM_data8:
24467 bytes += 8;
24468 break;
24469
24470 case DW_FORM_data16:
24471 bytes += 16;
24472 break;
24473
24474 case DW_FORM_string:
24475 read_direct_string (abfd, bytes, &bytes_read);
24476 bytes += bytes_read;
24477 break;
24478
24479 case DW_FORM_sec_offset:
24480 case DW_FORM_strp:
24481 case DW_FORM_GNU_strp_alt:
24482 bytes += offset_size;
24483 break;
24484
24485 case DW_FORM_block:
24486 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
24487 bytes += bytes_read;
24488 break;
24489
24490 case DW_FORM_block1:
24491 bytes += 1 + read_1_byte (abfd, bytes);
24492 break;
24493 case DW_FORM_block2:
24494 bytes += 2 + read_2_bytes (abfd, bytes);
24495 break;
24496 case DW_FORM_block4:
24497 bytes += 4 + read_4_bytes (abfd, bytes);
24498 break;
24499
24500 case DW_FORM_sdata:
24501 case DW_FORM_udata:
24502 case DW_FORM_GNU_addr_index:
24503 case DW_FORM_GNU_str_index:
24504 bytes = gdb_skip_leb128 (bytes, buffer_end);
24505 if (bytes == NULL)
24506 {
24507 dwarf2_section_buffer_overflow_complaint (section);
24508 return NULL;
24509 }
24510 break;
24511
24512 case DW_FORM_implicit_const:
24513 break;
24514
24515 default:
24516 {
24517 complaint (&symfile_complaints,
24518 _("invalid form 0x%x in `%s'"),
24519 form, get_section_name (section));
24520 return NULL;
24521 }
24522 }
24523
24524 return bytes;
24525 }
24526
24527 /* A helper for dwarf_decode_macros that handles skipping an unknown
24528 opcode. Returns an updated pointer to the macro data buffer; or,
24529 on error, issues a complaint and returns NULL. */
24530
24531 static const gdb_byte *
24532 skip_unknown_opcode (unsigned int opcode,
24533 const gdb_byte **opcode_definitions,
24534 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
24535 bfd *abfd,
24536 unsigned int offset_size,
24537 struct dwarf2_section_info *section)
24538 {
24539 unsigned int bytes_read, i;
24540 unsigned long arg;
24541 const gdb_byte *defn;
24542
24543 if (opcode_definitions[opcode] == NULL)
24544 {
24545 complaint (&symfile_complaints,
24546 _("unrecognized DW_MACFINO opcode 0x%x"),
24547 opcode);
24548 return NULL;
24549 }
24550
24551 defn = opcode_definitions[opcode];
24552 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
24553 defn += bytes_read;
24554
24555 for (i = 0; i < arg; ++i)
24556 {
24557 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
24558 (enum dwarf_form) defn[i], offset_size,
24559 section);
24560 if (mac_ptr == NULL)
24561 {
24562 /* skip_form_bytes already issued the complaint. */
24563 return NULL;
24564 }
24565 }
24566
24567 return mac_ptr;
24568 }
24569
24570 /* A helper function which parses the header of a macro section.
24571 If the macro section is the extended (for now called "GNU") type,
24572 then this updates *OFFSET_SIZE. Returns a pointer to just after
24573 the header, or issues a complaint and returns NULL on error. */
24574
24575 static const gdb_byte *
24576 dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
24577 bfd *abfd,
24578 const gdb_byte *mac_ptr,
24579 unsigned int *offset_size,
24580 int section_is_gnu)
24581 {
24582 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
24583
24584 if (section_is_gnu)
24585 {
24586 unsigned int version, flags;
24587
24588 version = read_2_bytes (abfd, mac_ptr);
24589 if (version != 4 && version != 5)
24590 {
24591 complaint (&symfile_complaints,
24592 _("unrecognized version `%d' in .debug_macro section"),
24593 version);
24594 return NULL;
24595 }
24596 mac_ptr += 2;
24597
24598 flags = read_1_byte (abfd, mac_ptr);
24599 ++mac_ptr;
24600 *offset_size = (flags & 1) ? 8 : 4;
24601
24602 if ((flags & 2) != 0)
24603 /* We don't need the line table offset. */
24604 mac_ptr += *offset_size;
24605
24606 /* Vendor opcode descriptions. */
24607 if ((flags & 4) != 0)
24608 {
24609 unsigned int i, count;
24610
24611 count = read_1_byte (abfd, mac_ptr);
24612 ++mac_ptr;
24613 for (i = 0; i < count; ++i)
24614 {
24615 unsigned int opcode, bytes_read;
24616 unsigned long arg;
24617
24618 opcode = read_1_byte (abfd, mac_ptr);
24619 ++mac_ptr;
24620 opcode_definitions[opcode] = mac_ptr;
24621 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24622 mac_ptr += bytes_read;
24623 mac_ptr += arg;
24624 }
24625 }
24626 }
24627
24628 return mac_ptr;
24629 }
24630
24631 /* A helper for dwarf_decode_macros that handles the GNU extensions,
24632 including DW_MACRO_import. */
24633
24634 static void
24635 dwarf_decode_macro_bytes (struct dwarf2_per_objfile *dwarf2_per_objfile,
24636 bfd *abfd,
24637 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
24638 struct macro_source_file *current_file,
24639 struct line_header *lh,
24640 struct dwarf2_section_info *section,
24641 int section_is_gnu, int section_is_dwz,
24642 unsigned int offset_size,
24643 htab_t include_hash)
24644 {
24645 struct objfile *objfile = dwarf2_per_objfile->objfile;
24646 enum dwarf_macro_record_type macinfo_type;
24647 int at_commandline;
24648 const gdb_byte *opcode_definitions[256];
24649
24650 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24651 &offset_size, section_is_gnu);
24652 if (mac_ptr == NULL)
24653 {
24654 /* We already issued a complaint. */
24655 return;
24656 }
24657
24658 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
24659 GDB is still reading the definitions from command line. First
24660 DW_MACINFO_start_file will need to be ignored as it was already executed
24661 to create CURRENT_FILE for the main source holding also the command line
24662 definitions. On first met DW_MACINFO_start_file this flag is reset to
24663 normally execute all the remaining DW_MACINFO_start_file macinfos. */
24664
24665 at_commandline = 1;
24666
24667 do
24668 {
24669 /* Do we at least have room for a macinfo type byte? */
24670 if (mac_ptr >= mac_end)
24671 {
24672 dwarf2_section_buffer_overflow_complaint (section);
24673 break;
24674 }
24675
24676 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
24677 mac_ptr++;
24678
24679 /* Note that we rely on the fact that the corresponding GNU and
24680 DWARF constants are the same. */
24681 DIAGNOSTIC_PUSH
24682 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
24683 switch (macinfo_type)
24684 {
24685 /* A zero macinfo type indicates the end of the macro
24686 information. */
24687 case 0:
24688 break;
24689
24690 case DW_MACRO_define:
24691 case DW_MACRO_undef:
24692 case DW_MACRO_define_strp:
24693 case DW_MACRO_undef_strp:
24694 case DW_MACRO_define_sup:
24695 case DW_MACRO_undef_sup:
24696 {
24697 unsigned int bytes_read;
24698 int line;
24699 const char *body;
24700 int is_define;
24701
24702 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24703 mac_ptr += bytes_read;
24704
24705 if (macinfo_type == DW_MACRO_define
24706 || macinfo_type == DW_MACRO_undef)
24707 {
24708 body = read_direct_string (abfd, mac_ptr, &bytes_read);
24709 mac_ptr += bytes_read;
24710 }
24711 else
24712 {
24713 LONGEST str_offset;
24714
24715 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
24716 mac_ptr += offset_size;
24717
24718 if (macinfo_type == DW_MACRO_define_sup
24719 || macinfo_type == DW_MACRO_undef_sup
24720 || section_is_dwz)
24721 {
24722 struct dwz_file *dwz
24723 = dwarf2_get_dwz_file (dwarf2_per_objfile);
24724
24725 body = read_indirect_string_from_dwz (objfile,
24726 dwz, str_offset);
24727 }
24728 else
24729 body = read_indirect_string_at_offset (dwarf2_per_objfile,
24730 abfd, str_offset);
24731 }
24732
24733 is_define = (macinfo_type == DW_MACRO_define
24734 || macinfo_type == DW_MACRO_define_strp
24735 || macinfo_type == DW_MACRO_define_sup);
24736 if (! current_file)
24737 {
24738 /* DWARF violation as no main source is present. */
24739 complaint (&symfile_complaints,
24740 _("debug info with no main source gives macro %s "
24741 "on line %d: %s"),
24742 is_define ? _("definition") : _("undefinition"),
24743 line, body);
24744 break;
24745 }
24746 if ((line == 0 && !at_commandline)
24747 || (line != 0 && at_commandline))
24748 complaint (&symfile_complaints,
24749 _("debug info gives %s macro %s with %s line %d: %s"),
24750 at_commandline ? _("command-line") : _("in-file"),
24751 is_define ? _("definition") : _("undefinition"),
24752 line == 0 ? _("zero") : _("non-zero"), line, body);
24753
24754 if (is_define)
24755 parse_macro_definition (current_file, line, body);
24756 else
24757 {
24758 gdb_assert (macinfo_type == DW_MACRO_undef
24759 || macinfo_type == DW_MACRO_undef_strp
24760 || macinfo_type == DW_MACRO_undef_sup);
24761 macro_undef (current_file, line, body);
24762 }
24763 }
24764 break;
24765
24766 case DW_MACRO_start_file:
24767 {
24768 unsigned int bytes_read;
24769 int line, file;
24770
24771 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24772 mac_ptr += bytes_read;
24773 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24774 mac_ptr += bytes_read;
24775
24776 if ((line == 0 && !at_commandline)
24777 || (line != 0 && at_commandline))
24778 complaint (&symfile_complaints,
24779 _("debug info gives source %d included "
24780 "from %s at %s line %d"),
24781 file, at_commandline ? _("command-line") : _("file"),
24782 line == 0 ? _("zero") : _("non-zero"), line);
24783
24784 if (at_commandline)
24785 {
24786 /* This DW_MACRO_start_file was executed in the
24787 pass one. */
24788 at_commandline = 0;
24789 }
24790 else
24791 current_file = macro_start_file (file, line, current_file, lh);
24792 }
24793 break;
24794
24795 case DW_MACRO_end_file:
24796 if (! current_file)
24797 complaint (&symfile_complaints,
24798 _("macro debug info has an unmatched "
24799 "`close_file' directive"));
24800 else
24801 {
24802 current_file = current_file->included_by;
24803 if (! current_file)
24804 {
24805 enum dwarf_macro_record_type next_type;
24806
24807 /* GCC circa March 2002 doesn't produce the zero
24808 type byte marking the end of the compilation
24809 unit. Complain if it's not there, but exit no
24810 matter what. */
24811
24812 /* Do we at least have room for a macinfo type byte? */
24813 if (mac_ptr >= mac_end)
24814 {
24815 dwarf2_section_buffer_overflow_complaint (section);
24816 return;
24817 }
24818
24819 /* We don't increment mac_ptr here, so this is just
24820 a look-ahead. */
24821 next_type
24822 = (enum dwarf_macro_record_type) read_1_byte (abfd,
24823 mac_ptr);
24824 if (next_type != 0)
24825 complaint (&symfile_complaints,
24826 _("no terminating 0-type entry for "
24827 "macros in `.debug_macinfo' section"));
24828
24829 return;
24830 }
24831 }
24832 break;
24833
24834 case DW_MACRO_import:
24835 case DW_MACRO_import_sup:
24836 {
24837 LONGEST offset;
24838 void **slot;
24839 bfd *include_bfd = abfd;
24840 struct dwarf2_section_info *include_section = section;
24841 const gdb_byte *include_mac_end = mac_end;
24842 int is_dwz = section_is_dwz;
24843 const gdb_byte *new_mac_ptr;
24844
24845 offset = read_offset_1 (abfd, mac_ptr, offset_size);
24846 mac_ptr += offset_size;
24847
24848 if (macinfo_type == DW_MACRO_import_sup)
24849 {
24850 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
24851
24852 dwarf2_read_section (objfile, &dwz->macro);
24853
24854 include_section = &dwz->macro;
24855 include_bfd = get_section_bfd_owner (include_section);
24856 include_mac_end = dwz->macro.buffer + dwz->macro.size;
24857 is_dwz = 1;
24858 }
24859
24860 new_mac_ptr = include_section->buffer + offset;
24861 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
24862
24863 if (*slot != NULL)
24864 {
24865 /* This has actually happened; see
24866 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
24867 complaint (&symfile_complaints,
24868 _("recursive DW_MACRO_import in "
24869 ".debug_macro section"));
24870 }
24871 else
24872 {
24873 *slot = (void *) new_mac_ptr;
24874
24875 dwarf_decode_macro_bytes (dwarf2_per_objfile,
24876 include_bfd, new_mac_ptr,
24877 include_mac_end, current_file, lh,
24878 section, section_is_gnu, is_dwz,
24879 offset_size, include_hash);
24880
24881 htab_remove_elt (include_hash, (void *) new_mac_ptr);
24882 }
24883 }
24884 break;
24885
24886 case DW_MACINFO_vendor_ext:
24887 if (!section_is_gnu)
24888 {
24889 unsigned int bytes_read;
24890
24891 /* This reads the constant, but since we don't recognize
24892 any vendor extensions, we ignore it. */
24893 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24894 mac_ptr += bytes_read;
24895 read_direct_string (abfd, mac_ptr, &bytes_read);
24896 mac_ptr += bytes_read;
24897
24898 /* We don't recognize any vendor extensions. */
24899 break;
24900 }
24901 /* FALLTHROUGH */
24902
24903 default:
24904 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
24905 mac_ptr, mac_end, abfd, offset_size,
24906 section);
24907 if (mac_ptr == NULL)
24908 return;
24909 break;
24910 }
24911 DIAGNOSTIC_POP
24912 } while (macinfo_type != 0);
24913 }
24914
24915 static void
24916 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
24917 int section_is_gnu)
24918 {
24919 struct dwarf2_per_objfile *dwarf2_per_objfile
24920 = cu->per_cu->dwarf2_per_objfile;
24921 struct objfile *objfile = dwarf2_per_objfile->objfile;
24922 struct line_header *lh = cu->line_header;
24923 bfd *abfd;
24924 const gdb_byte *mac_ptr, *mac_end;
24925 struct macro_source_file *current_file = 0;
24926 enum dwarf_macro_record_type macinfo_type;
24927 unsigned int offset_size = cu->header.offset_size;
24928 const gdb_byte *opcode_definitions[256];
24929 void **slot;
24930 struct dwarf2_section_info *section;
24931 const char *section_name;
24932
24933 if (cu->dwo_unit != NULL)
24934 {
24935 if (section_is_gnu)
24936 {
24937 section = &cu->dwo_unit->dwo_file->sections.macro;
24938 section_name = ".debug_macro.dwo";
24939 }
24940 else
24941 {
24942 section = &cu->dwo_unit->dwo_file->sections.macinfo;
24943 section_name = ".debug_macinfo.dwo";
24944 }
24945 }
24946 else
24947 {
24948 if (section_is_gnu)
24949 {
24950 section = &dwarf2_per_objfile->macro;
24951 section_name = ".debug_macro";
24952 }
24953 else
24954 {
24955 section = &dwarf2_per_objfile->macinfo;
24956 section_name = ".debug_macinfo";
24957 }
24958 }
24959
24960 dwarf2_read_section (objfile, section);
24961 if (section->buffer == NULL)
24962 {
24963 complaint (&symfile_complaints, _("missing %s section"), section_name);
24964 return;
24965 }
24966 abfd = get_section_bfd_owner (section);
24967
24968 /* First pass: Find the name of the base filename.
24969 This filename is needed in order to process all macros whose definition
24970 (or undefinition) comes from the command line. These macros are defined
24971 before the first DW_MACINFO_start_file entry, and yet still need to be
24972 associated to the base file.
24973
24974 To determine the base file name, we scan the macro definitions until we
24975 reach the first DW_MACINFO_start_file entry. We then initialize
24976 CURRENT_FILE accordingly so that any macro definition found before the
24977 first DW_MACINFO_start_file can still be associated to the base file. */
24978
24979 mac_ptr = section->buffer + offset;
24980 mac_end = section->buffer + section->size;
24981
24982 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24983 &offset_size, section_is_gnu);
24984 if (mac_ptr == NULL)
24985 {
24986 /* We already issued a complaint. */
24987 return;
24988 }
24989
24990 do
24991 {
24992 /* Do we at least have room for a macinfo type byte? */
24993 if (mac_ptr >= mac_end)
24994 {
24995 /* Complaint is printed during the second pass as GDB will probably
24996 stop the first pass earlier upon finding
24997 DW_MACINFO_start_file. */
24998 break;
24999 }
25000
25001 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
25002 mac_ptr++;
25003
25004 /* Note that we rely on the fact that the corresponding GNU and
25005 DWARF constants are the same. */
25006 DIAGNOSTIC_PUSH
25007 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
25008 switch (macinfo_type)
25009 {
25010 /* A zero macinfo type indicates the end of the macro
25011 information. */
25012 case 0:
25013 break;
25014
25015 case DW_MACRO_define:
25016 case DW_MACRO_undef:
25017 /* Only skip the data by MAC_PTR. */
25018 {
25019 unsigned int bytes_read;
25020
25021 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25022 mac_ptr += bytes_read;
25023 read_direct_string (abfd, mac_ptr, &bytes_read);
25024 mac_ptr += bytes_read;
25025 }
25026 break;
25027
25028 case DW_MACRO_start_file:
25029 {
25030 unsigned int bytes_read;
25031 int line, file;
25032
25033 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25034 mac_ptr += bytes_read;
25035 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25036 mac_ptr += bytes_read;
25037
25038 current_file = macro_start_file (file, line, current_file, lh);
25039 }
25040 break;
25041
25042 case DW_MACRO_end_file:
25043 /* No data to skip by MAC_PTR. */
25044 break;
25045
25046 case DW_MACRO_define_strp:
25047 case DW_MACRO_undef_strp:
25048 case DW_MACRO_define_sup:
25049 case DW_MACRO_undef_sup:
25050 {
25051 unsigned int bytes_read;
25052
25053 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25054 mac_ptr += bytes_read;
25055 mac_ptr += offset_size;
25056 }
25057 break;
25058
25059 case DW_MACRO_import:
25060 case DW_MACRO_import_sup:
25061 /* Note that, according to the spec, a transparent include
25062 chain cannot call DW_MACRO_start_file. So, we can just
25063 skip this opcode. */
25064 mac_ptr += offset_size;
25065 break;
25066
25067 case DW_MACINFO_vendor_ext:
25068 /* Only skip the data by MAC_PTR. */
25069 if (!section_is_gnu)
25070 {
25071 unsigned int bytes_read;
25072
25073 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25074 mac_ptr += bytes_read;
25075 read_direct_string (abfd, mac_ptr, &bytes_read);
25076 mac_ptr += bytes_read;
25077 }
25078 /* FALLTHROUGH */
25079
25080 default:
25081 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
25082 mac_ptr, mac_end, abfd, offset_size,
25083 section);
25084 if (mac_ptr == NULL)
25085 return;
25086 break;
25087 }
25088 DIAGNOSTIC_POP
25089 } while (macinfo_type != 0 && current_file == NULL);
25090
25091 /* Second pass: Process all entries.
25092
25093 Use the AT_COMMAND_LINE flag to determine whether we are still processing
25094 command-line macro definitions/undefinitions. This flag is unset when we
25095 reach the first DW_MACINFO_start_file entry. */
25096
25097 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
25098 htab_eq_pointer,
25099 NULL, xcalloc, xfree));
25100 mac_ptr = section->buffer + offset;
25101 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
25102 *slot = (void *) mac_ptr;
25103 dwarf_decode_macro_bytes (dwarf2_per_objfile,
25104 abfd, mac_ptr, mac_end,
25105 current_file, lh, section,
25106 section_is_gnu, 0, offset_size,
25107 include_hash.get ());
25108 }
25109
25110 /* Check if the attribute's form is a DW_FORM_block*
25111 if so return true else false. */
25112
25113 static int
25114 attr_form_is_block (const struct attribute *attr)
25115 {
25116 return (attr == NULL ? 0 :
25117 attr->form == DW_FORM_block1
25118 || attr->form == DW_FORM_block2
25119 || attr->form == DW_FORM_block4
25120 || attr->form == DW_FORM_block
25121 || attr->form == DW_FORM_exprloc);
25122 }
25123
25124 /* Return non-zero if ATTR's value is a section offset --- classes
25125 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
25126 You may use DW_UNSND (attr) to retrieve such offsets.
25127
25128 Section 7.5.4, "Attribute Encodings", explains that no attribute
25129 may have a value that belongs to more than one of these classes; it
25130 would be ambiguous if we did, because we use the same forms for all
25131 of them. */
25132
25133 static int
25134 attr_form_is_section_offset (const struct attribute *attr)
25135 {
25136 return (attr->form == DW_FORM_data4
25137 || attr->form == DW_FORM_data8
25138 || attr->form == DW_FORM_sec_offset);
25139 }
25140
25141 /* Return non-zero if ATTR's value falls in the 'constant' class, or
25142 zero otherwise. When this function returns true, you can apply
25143 dwarf2_get_attr_constant_value to it.
25144
25145 However, note that for some attributes you must check
25146 attr_form_is_section_offset before using this test. DW_FORM_data4
25147 and DW_FORM_data8 are members of both the constant class, and of
25148 the classes that contain offsets into other debug sections
25149 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
25150 that, if an attribute's can be either a constant or one of the
25151 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
25152 taken as section offsets, not constants.
25153
25154 DW_FORM_data16 is not considered as dwarf2_get_attr_constant_value
25155 cannot handle that. */
25156
25157 static int
25158 attr_form_is_constant (const struct attribute *attr)
25159 {
25160 switch (attr->form)
25161 {
25162 case DW_FORM_sdata:
25163 case DW_FORM_udata:
25164 case DW_FORM_data1:
25165 case DW_FORM_data2:
25166 case DW_FORM_data4:
25167 case DW_FORM_data8:
25168 case DW_FORM_implicit_const:
25169 return 1;
25170 default:
25171 return 0;
25172 }
25173 }
25174
25175
25176 /* DW_ADDR is always stored already as sect_offset; despite for the forms
25177 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
25178
25179 static int
25180 attr_form_is_ref (const struct attribute *attr)
25181 {
25182 switch (attr->form)
25183 {
25184 case DW_FORM_ref_addr:
25185 case DW_FORM_ref1:
25186 case DW_FORM_ref2:
25187 case DW_FORM_ref4:
25188 case DW_FORM_ref8:
25189 case DW_FORM_ref_udata:
25190 case DW_FORM_GNU_ref_alt:
25191 return 1;
25192 default:
25193 return 0;
25194 }
25195 }
25196
25197 /* Return the .debug_loc section to use for CU.
25198 For DWO files use .debug_loc.dwo. */
25199
25200 static struct dwarf2_section_info *
25201 cu_debug_loc_section (struct dwarf2_cu *cu)
25202 {
25203 struct dwarf2_per_objfile *dwarf2_per_objfile
25204 = cu->per_cu->dwarf2_per_objfile;
25205
25206 if (cu->dwo_unit)
25207 {
25208 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
25209
25210 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
25211 }
25212 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
25213 : &dwarf2_per_objfile->loc);
25214 }
25215
25216 /* A helper function that fills in a dwarf2_loclist_baton. */
25217
25218 static void
25219 fill_in_loclist_baton (struct dwarf2_cu *cu,
25220 struct dwarf2_loclist_baton *baton,
25221 const struct attribute *attr)
25222 {
25223 struct dwarf2_per_objfile *dwarf2_per_objfile
25224 = cu->per_cu->dwarf2_per_objfile;
25225 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
25226
25227 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
25228
25229 baton->per_cu = cu->per_cu;
25230 gdb_assert (baton->per_cu);
25231 /* We don't know how long the location list is, but make sure we
25232 don't run off the edge of the section. */
25233 baton->size = section->size - DW_UNSND (attr);
25234 baton->data = section->buffer + DW_UNSND (attr);
25235 baton->base_address = cu->base_address;
25236 baton->from_dwo = cu->dwo_unit != NULL;
25237 }
25238
25239 static void
25240 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
25241 struct dwarf2_cu *cu, int is_block)
25242 {
25243 struct dwarf2_per_objfile *dwarf2_per_objfile
25244 = cu->per_cu->dwarf2_per_objfile;
25245 struct objfile *objfile = dwarf2_per_objfile->objfile;
25246 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
25247
25248 if (attr_form_is_section_offset (attr)
25249 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
25250 the section. If so, fall through to the complaint in the
25251 other branch. */
25252 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
25253 {
25254 struct dwarf2_loclist_baton *baton;
25255
25256 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
25257
25258 fill_in_loclist_baton (cu, baton, attr);
25259
25260 if (cu->base_known == 0)
25261 complaint (&symfile_complaints,
25262 _("Location list used without "
25263 "specifying the CU base address."));
25264
25265 SYMBOL_ACLASS_INDEX (sym) = (is_block
25266 ? dwarf2_loclist_block_index
25267 : dwarf2_loclist_index);
25268 SYMBOL_LOCATION_BATON (sym) = baton;
25269 }
25270 else
25271 {
25272 struct dwarf2_locexpr_baton *baton;
25273
25274 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
25275 baton->per_cu = cu->per_cu;
25276 gdb_assert (baton->per_cu);
25277
25278 if (attr_form_is_block (attr))
25279 {
25280 /* Note that we're just copying the block's data pointer
25281 here, not the actual data. We're still pointing into the
25282 info_buffer for SYM's objfile; right now we never release
25283 that buffer, but when we do clean up properly this may
25284 need to change. */
25285 baton->size = DW_BLOCK (attr)->size;
25286 baton->data = DW_BLOCK (attr)->data;
25287 }
25288 else
25289 {
25290 dwarf2_invalid_attrib_class_complaint ("location description",
25291 SYMBOL_NATURAL_NAME (sym));
25292 baton->size = 0;
25293 }
25294
25295 SYMBOL_ACLASS_INDEX (sym) = (is_block
25296 ? dwarf2_locexpr_block_index
25297 : dwarf2_locexpr_index);
25298 SYMBOL_LOCATION_BATON (sym) = baton;
25299 }
25300 }
25301
25302 /* Return the OBJFILE associated with the compilation unit CU. If CU
25303 came from a separate debuginfo file, then the master objfile is
25304 returned. */
25305
25306 struct objfile *
25307 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
25308 {
25309 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
25310
25311 /* Return the master objfile, so that we can report and look up the
25312 correct file containing this variable. */
25313 if (objfile->separate_debug_objfile_backlink)
25314 objfile = objfile->separate_debug_objfile_backlink;
25315
25316 return objfile;
25317 }
25318
25319 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
25320 (CU_HEADERP is unused in such case) or prepare a temporary copy at
25321 CU_HEADERP first. */
25322
25323 static const struct comp_unit_head *
25324 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
25325 struct dwarf2_per_cu_data *per_cu)
25326 {
25327 const gdb_byte *info_ptr;
25328
25329 if (per_cu->cu)
25330 return &per_cu->cu->header;
25331
25332 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
25333
25334 memset (cu_headerp, 0, sizeof (*cu_headerp));
25335 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
25336 rcuh_kind::COMPILE);
25337
25338 return cu_headerp;
25339 }
25340
25341 /* Return the address size given in the compilation unit header for CU. */
25342
25343 int
25344 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
25345 {
25346 struct comp_unit_head cu_header_local;
25347 const struct comp_unit_head *cu_headerp;
25348
25349 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25350
25351 return cu_headerp->addr_size;
25352 }
25353
25354 /* Return the offset size given in the compilation unit header for CU. */
25355
25356 int
25357 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
25358 {
25359 struct comp_unit_head cu_header_local;
25360 const struct comp_unit_head *cu_headerp;
25361
25362 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25363
25364 return cu_headerp->offset_size;
25365 }
25366
25367 /* See its dwarf2loc.h declaration. */
25368
25369 int
25370 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
25371 {
25372 struct comp_unit_head cu_header_local;
25373 const struct comp_unit_head *cu_headerp;
25374
25375 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25376
25377 if (cu_headerp->version == 2)
25378 return cu_headerp->addr_size;
25379 else
25380 return cu_headerp->offset_size;
25381 }
25382
25383 /* Return the text offset of the CU. The returned offset comes from
25384 this CU's objfile. If this objfile came from a separate debuginfo
25385 file, then the offset may be different from the corresponding
25386 offset in the parent objfile. */
25387
25388 CORE_ADDR
25389 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
25390 {
25391 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
25392
25393 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
25394 }
25395
25396 /* Return DWARF version number of PER_CU. */
25397
25398 short
25399 dwarf2_version (struct dwarf2_per_cu_data *per_cu)
25400 {
25401 return per_cu->dwarf_version;
25402 }
25403
25404 /* Locate the .debug_info compilation unit from CU's objfile which contains
25405 the DIE at OFFSET. Raises an error on failure. */
25406
25407 static struct dwarf2_per_cu_data *
25408 dwarf2_find_containing_comp_unit (sect_offset sect_off,
25409 unsigned int offset_in_dwz,
25410 struct dwarf2_per_objfile *dwarf2_per_objfile)
25411 {
25412 struct dwarf2_per_cu_data *this_cu;
25413 int low, high;
25414 const sect_offset *cu_off;
25415
25416 low = 0;
25417 high = dwarf2_per_objfile->n_comp_units - 1;
25418 while (high > low)
25419 {
25420 struct dwarf2_per_cu_data *mid_cu;
25421 int mid = low + (high - low) / 2;
25422
25423 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
25424 cu_off = &mid_cu->sect_off;
25425 if (mid_cu->is_dwz > offset_in_dwz
25426 || (mid_cu->is_dwz == offset_in_dwz && *cu_off >= sect_off))
25427 high = mid;
25428 else
25429 low = mid + 1;
25430 }
25431 gdb_assert (low == high);
25432 this_cu = dwarf2_per_objfile->all_comp_units[low];
25433 cu_off = &this_cu->sect_off;
25434 if (this_cu->is_dwz != offset_in_dwz || *cu_off > sect_off)
25435 {
25436 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
25437 error (_("Dwarf Error: could not find partial DIE containing "
25438 "offset %s [in module %s]"),
25439 sect_offset_str (sect_off),
25440 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
25441
25442 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
25443 <= sect_off);
25444 return dwarf2_per_objfile->all_comp_units[low-1];
25445 }
25446 else
25447 {
25448 this_cu = dwarf2_per_objfile->all_comp_units[low];
25449 if (low == dwarf2_per_objfile->n_comp_units - 1
25450 && sect_off >= this_cu->sect_off + this_cu->length)
25451 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
25452 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
25453 return this_cu;
25454 }
25455 }
25456
25457 /* Initialize dwarf2_cu CU, owned by PER_CU. */
25458
25459 dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
25460 : per_cu (per_cu_),
25461 mark (0),
25462 has_loclist (0),
25463 checked_producer (0),
25464 producer_is_gxx_lt_4_6 (0),
25465 producer_is_gcc_lt_4_3 (0),
25466 producer_is_icc_lt_14 (0),
25467 processing_has_namespace_info (0)
25468 {
25469 per_cu->cu = this;
25470 }
25471
25472 /* Destroy a dwarf2_cu. */
25473
25474 dwarf2_cu::~dwarf2_cu ()
25475 {
25476 per_cu->cu = NULL;
25477 }
25478
25479 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
25480
25481 static void
25482 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
25483 enum language pretend_language)
25484 {
25485 struct attribute *attr;
25486
25487 /* Set the language we're debugging. */
25488 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
25489 if (attr)
25490 set_cu_language (DW_UNSND (attr), cu);
25491 else
25492 {
25493 cu->language = pretend_language;
25494 cu->language_defn = language_def (cu->language);
25495 }
25496
25497 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
25498 }
25499
25500 /* Free all cached compilation units. */
25501
25502 static void
25503 free_cached_comp_units (void *data)
25504 {
25505 struct dwarf2_per_objfile *dwarf2_per_objfile
25506 = (struct dwarf2_per_objfile *) data;
25507
25508 dwarf2_per_objfile->free_cached_comp_units ();
25509 }
25510
25511 /* Increase the age counter on each cached compilation unit, and free
25512 any that are too old. */
25513
25514 static void
25515 age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
25516 {
25517 struct dwarf2_per_cu_data *per_cu, **last_chain;
25518
25519 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
25520 per_cu = dwarf2_per_objfile->read_in_chain;
25521 while (per_cu != NULL)
25522 {
25523 per_cu->cu->last_used ++;
25524 if (per_cu->cu->last_used <= dwarf_max_cache_age)
25525 dwarf2_mark (per_cu->cu);
25526 per_cu = per_cu->cu->read_in_chain;
25527 }
25528
25529 per_cu = dwarf2_per_objfile->read_in_chain;
25530 last_chain = &dwarf2_per_objfile->read_in_chain;
25531 while (per_cu != NULL)
25532 {
25533 struct dwarf2_per_cu_data *next_cu;
25534
25535 next_cu = per_cu->cu->read_in_chain;
25536
25537 if (!per_cu->cu->mark)
25538 {
25539 delete per_cu->cu;
25540 *last_chain = next_cu;
25541 }
25542 else
25543 last_chain = &per_cu->cu->read_in_chain;
25544
25545 per_cu = next_cu;
25546 }
25547 }
25548
25549 /* Remove a single compilation unit from the cache. */
25550
25551 static void
25552 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
25553 {
25554 struct dwarf2_per_cu_data *per_cu, **last_chain;
25555 struct dwarf2_per_objfile *dwarf2_per_objfile
25556 = target_per_cu->dwarf2_per_objfile;
25557
25558 per_cu = dwarf2_per_objfile->read_in_chain;
25559 last_chain = &dwarf2_per_objfile->read_in_chain;
25560 while (per_cu != NULL)
25561 {
25562 struct dwarf2_per_cu_data *next_cu;
25563
25564 next_cu = per_cu->cu->read_in_chain;
25565
25566 if (per_cu == target_per_cu)
25567 {
25568 delete per_cu->cu;
25569 per_cu->cu = NULL;
25570 *last_chain = next_cu;
25571 break;
25572 }
25573 else
25574 last_chain = &per_cu->cu->read_in_chain;
25575
25576 per_cu = next_cu;
25577 }
25578 }
25579
25580 /* Release all extra memory associated with OBJFILE. */
25581
25582 void
25583 dwarf2_free_objfile (struct objfile *objfile)
25584 {
25585 struct dwarf2_per_objfile *dwarf2_per_objfile
25586 = get_dwarf2_per_objfile (objfile);
25587
25588 delete dwarf2_per_objfile;
25589 }
25590
25591 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
25592 We store these in a hash table separate from the DIEs, and preserve them
25593 when the DIEs are flushed out of cache.
25594
25595 The CU "per_cu" pointer is needed because offset alone is not enough to
25596 uniquely identify the type. A file may have multiple .debug_types sections,
25597 or the type may come from a DWO file. Furthermore, while it's more logical
25598 to use per_cu->section+offset, with Fission the section with the data is in
25599 the DWO file but we don't know that section at the point we need it.
25600 We have to use something in dwarf2_per_cu_data (or the pointer to it)
25601 because we can enter the lookup routine, get_die_type_at_offset, from
25602 outside this file, and thus won't necessarily have PER_CU->cu.
25603 Fortunately, PER_CU is stable for the life of the objfile. */
25604
25605 struct dwarf2_per_cu_offset_and_type
25606 {
25607 const struct dwarf2_per_cu_data *per_cu;
25608 sect_offset sect_off;
25609 struct type *type;
25610 };
25611
25612 /* Hash function for a dwarf2_per_cu_offset_and_type. */
25613
25614 static hashval_t
25615 per_cu_offset_and_type_hash (const void *item)
25616 {
25617 const struct dwarf2_per_cu_offset_and_type *ofs
25618 = (const struct dwarf2_per_cu_offset_and_type *) item;
25619
25620 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
25621 }
25622
25623 /* Equality function for a dwarf2_per_cu_offset_and_type. */
25624
25625 static int
25626 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
25627 {
25628 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
25629 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
25630 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
25631 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
25632
25633 return (ofs_lhs->per_cu == ofs_rhs->per_cu
25634 && ofs_lhs->sect_off == ofs_rhs->sect_off);
25635 }
25636
25637 /* Set the type associated with DIE to TYPE. Save it in CU's hash
25638 table if necessary. For convenience, return TYPE.
25639
25640 The DIEs reading must have careful ordering to:
25641 * Not cause infite loops trying to read in DIEs as a prerequisite for
25642 reading current DIE.
25643 * Not trying to dereference contents of still incompletely read in types
25644 while reading in other DIEs.
25645 * Enable referencing still incompletely read in types just by a pointer to
25646 the type without accessing its fields.
25647
25648 Therefore caller should follow these rules:
25649 * Try to fetch any prerequisite types we may need to build this DIE type
25650 before building the type and calling set_die_type.
25651 * After building type call set_die_type for current DIE as soon as
25652 possible before fetching more types to complete the current type.
25653 * Make the type as complete as possible before fetching more types. */
25654
25655 static struct type *
25656 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
25657 {
25658 struct dwarf2_per_objfile *dwarf2_per_objfile
25659 = cu->per_cu->dwarf2_per_objfile;
25660 struct dwarf2_per_cu_offset_and_type **slot, ofs;
25661 struct objfile *objfile = dwarf2_per_objfile->objfile;
25662 struct attribute *attr;
25663 struct dynamic_prop prop;
25664
25665 /* For Ada types, make sure that the gnat-specific data is always
25666 initialized (if not already set). There are a few types where
25667 we should not be doing so, because the type-specific area is
25668 already used to hold some other piece of info (eg: TYPE_CODE_FLT
25669 where the type-specific area is used to store the floatformat).
25670 But this is not a problem, because the gnat-specific information
25671 is actually not needed for these types. */
25672 if (need_gnat_info (cu)
25673 && TYPE_CODE (type) != TYPE_CODE_FUNC
25674 && TYPE_CODE (type) != TYPE_CODE_FLT
25675 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
25676 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
25677 && TYPE_CODE (type) != TYPE_CODE_METHOD
25678 && !HAVE_GNAT_AUX_INFO (type))
25679 INIT_GNAT_SPECIFIC (type);
25680
25681 /* Read DW_AT_allocated and set in type. */
25682 attr = dwarf2_attr (die, DW_AT_allocated, cu);
25683 if (attr_form_is_block (attr))
25684 {
25685 if (attr_to_dynamic_prop (attr, die, cu, &prop))
25686 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
25687 }
25688 else if (attr != NULL)
25689 {
25690 complaint (&symfile_complaints,
25691 _("DW_AT_allocated has the wrong form (%s) at DIE %s"),
25692 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
25693 sect_offset_str (die->sect_off));
25694 }
25695
25696 /* Read DW_AT_associated and set in type. */
25697 attr = dwarf2_attr (die, DW_AT_associated, cu);
25698 if (attr_form_is_block (attr))
25699 {
25700 if (attr_to_dynamic_prop (attr, die, cu, &prop))
25701 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
25702 }
25703 else if (attr != NULL)
25704 {
25705 complaint (&symfile_complaints,
25706 _("DW_AT_associated has the wrong form (%s) at DIE %s"),
25707 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
25708 sect_offset_str (die->sect_off));
25709 }
25710
25711 /* Read DW_AT_data_location and set in type. */
25712 attr = dwarf2_attr (die, DW_AT_data_location, cu);
25713 if (attr_to_dynamic_prop (attr, die, cu, &prop))
25714 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
25715
25716 if (dwarf2_per_objfile->die_type_hash == NULL)
25717 {
25718 dwarf2_per_objfile->die_type_hash =
25719 htab_create_alloc_ex (127,
25720 per_cu_offset_and_type_hash,
25721 per_cu_offset_and_type_eq,
25722 NULL,
25723 &objfile->objfile_obstack,
25724 hashtab_obstack_allocate,
25725 dummy_obstack_deallocate);
25726 }
25727
25728 ofs.per_cu = cu->per_cu;
25729 ofs.sect_off = die->sect_off;
25730 ofs.type = type;
25731 slot = (struct dwarf2_per_cu_offset_and_type **)
25732 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
25733 if (*slot)
25734 complaint (&symfile_complaints,
25735 _("A problem internal to GDB: DIE %s has type already set"),
25736 sect_offset_str (die->sect_off));
25737 *slot = XOBNEW (&objfile->objfile_obstack,
25738 struct dwarf2_per_cu_offset_and_type);
25739 **slot = ofs;
25740 return type;
25741 }
25742
25743 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
25744 or return NULL if the die does not have a saved type. */
25745
25746 static struct type *
25747 get_die_type_at_offset (sect_offset sect_off,
25748 struct dwarf2_per_cu_data *per_cu)
25749 {
25750 struct dwarf2_per_cu_offset_and_type *slot, ofs;
25751 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
25752
25753 if (dwarf2_per_objfile->die_type_hash == NULL)
25754 return NULL;
25755
25756 ofs.per_cu = per_cu;
25757 ofs.sect_off = sect_off;
25758 slot = ((struct dwarf2_per_cu_offset_and_type *)
25759 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
25760 if (slot)
25761 return slot->type;
25762 else
25763 return NULL;
25764 }
25765
25766 /* Look up the type for DIE in CU in die_type_hash,
25767 or return NULL if DIE does not have a saved type. */
25768
25769 static struct type *
25770 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
25771 {
25772 return get_die_type_at_offset (die->sect_off, cu->per_cu);
25773 }
25774
25775 /* Add a dependence relationship from CU to REF_PER_CU. */
25776
25777 static void
25778 dwarf2_add_dependence (struct dwarf2_cu *cu,
25779 struct dwarf2_per_cu_data *ref_per_cu)
25780 {
25781 void **slot;
25782
25783 if (cu->dependencies == NULL)
25784 cu->dependencies
25785 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
25786 NULL, &cu->comp_unit_obstack,
25787 hashtab_obstack_allocate,
25788 dummy_obstack_deallocate);
25789
25790 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
25791 if (*slot == NULL)
25792 *slot = ref_per_cu;
25793 }
25794
25795 /* Subroutine of dwarf2_mark to pass to htab_traverse.
25796 Set the mark field in every compilation unit in the
25797 cache that we must keep because we are keeping CU. */
25798
25799 static int
25800 dwarf2_mark_helper (void **slot, void *data)
25801 {
25802 struct dwarf2_per_cu_data *per_cu;
25803
25804 per_cu = (struct dwarf2_per_cu_data *) *slot;
25805
25806 /* cu->dependencies references may not yet have been ever read if QUIT aborts
25807 reading of the chain. As such dependencies remain valid it is not much
25808 useful to track and undo them during QUIT cleanups. */
25809 if (per_cu->cu == NULL)
25810 return 1;
25811
25812 if (per_cu->cu->mark)
25813 return 1;
25814 per_cu->cu->mark = 1;
25815
25816 if (per_cu->cu->dependencies != NULL)
25817 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
25818
25819 return 1;
25820 }
25821
25822 /* Set the mark field in CU and in every other compilation unit in the
25823 cache that we must keep because we are keeping CU. */
25824
25825 static void
25826 dwarf2_mark (struct dwarf2_cu *cu)
25827 {
25828 if (cu->mark)
25829 return;
25830 cu->mark = 1;
25831 if (cu->dependencies != NULL)
25832 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
25833 }
25834
25835 static void
25836 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
25837 {
25838 while (per_cu)
25839 {
25840 per_cu->cu->mark = 0;
25841 per_cu = per_cu->cu->read_in_chain;
25842 }
25843 }
25844
25845 /* Trivial hash function for partial_die_info: the hash value of a DIE
25846 is its offset in .debug_info for this objfile. */
25847
25848 static hashval_t
25849 partial_die_hash (const void *item)
25850 {
25851 const struct partial_die_info *part_die
25852 = (const struct partial_die_info *) item;
25853
25854 return to_underlying (part_die->sect_off);
25855 }
25856
25857 /* Trivial comparison function for partial_die_info structures: two DIEs
25858 are equal if they have the same offset. */
25859
25860 static int
25861 partial_die_eq (const void *item_lhs, const void *item_rhs)
25862 {
25863 const struct partial_die_info *part_die_lhs
25864 = (const struct partial_die_info *) item_lhs;
25865 const struct partial_die_info *part_die_rhs
25866 = (const struct partial_die_info *) item_rhs;
25867
25868 return part_die_lhs->sect_off == part_die_rhs->sect_off;
25869 }
25870
25871 static struct cmd_list_element *set_dwarf_cmdlist;
25872 static struct cmd_list_element *show_dwarf_cmdlist;
25873
25874 static void
25875 set_dwarf_cmd (const char *args, int from_tty)
25876 {
25877 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
25878 gdb_stdout);
25879 }
25880
25881 static void
25882 show_dwarf_cmd (const char *args, int from_tty)
25883 {
25884 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
25885 }
25886
25887 /* The "save gdb-index" command. */
25888
25889 /* Write SIZE bytes from the buffer pointed to by DATA to FILE, with
25890 error checking. */
25891
25892 static void
25893 file_write (FILE *file, const void *data, size_t size)
25894 {
25895 if (fwrite (data, 1, size, file) != size)
25896 error (_("couldn't data write to file"));
25897 }
25898
25899 /* Write the contents of VEC to FILE, with error checking. */
25900
25901 template<typename Elem, typename Alloc>
25902 static void
25903 file_write (FILE *file, const std::vector<Elem, Alloc> &vec)
25904 {
25905 file_write (file, vec.data (), vec.size () * sizeof (vec[0]));
25906 }
25907
25908 /* In-memory buffer to prepare data to be written later to a file. */
25909 class data_buf
25910 {
25911 public:
25912 /* Copy DATA to the end of the buffer. */
25913 template<typename T>
25914 void append_data (const T &data)
25915 {
25916 std::copy (reinterpret_cast<const gdb_byte *> (&data),
25917 reinterpret_cast<const gdb_byte *> (&data + 1),
25918 grow (sizeof (data)));
25919 }
25920
25921 /* Copy CSTR (a zero-terminated string) to the end of buffer. The
25922 terminating zero is appended too. */
25923 void append_cstr0 (const char *cstr)
25924 {
25925 const size_t size = strlen (cstr) + 1;
25926 std::copy (cstr, cstr + size, grow (size));
25927 }
25928
25929 /* Store INPUT as ULEB128 to the end of buffer. */
25930 void append_unsigned_leb128 (ULONGEST input)
25931 {
25932 for (;;)
25933 {
25934 gdb_byte output = input & 0x7f;
25935 input >>= 7;
25936 if (input)
25937 output |= 0x80;
25938 append_data (output);
25939 if (input == 0)
25940 break;
25941 }
25942 }
25943
25944 /* Accept a host-format integer in VAL and append it to the buffer
25945 as a target-format integer which is LEN bytes long. */
25946 void append_uint (size_t len, bfd_endian byte_order, ULONGEST val)
25947 {
25948 ::store_unsigned_integer (grow (len), len, byte_order, val);
25949 }
25950
25951 /* Return the size of the buffer. */
25952 size_t size () const
25953 {
25954 return m_vec.size ();
25955 }
25956
25957 /* Return true iff the buffer is empty. */
25958 bool empty () const
25959 {
25960 return m_vec.empty ();
25961 }
25962
25963 /* Write the buffer to FILE. */
25964 void file_write (FILE *file) const
25965 {
25966 ::file_write (file, m_vec);
25967 }
25968
25969 private:
25970 /* Grow SIZE bytes at the end of the buffer. Returns a pointer to
25971 the start of the new block. */
25972 gdb_byte *grow (size_t size)
25973 {
25974 m_vec.resize (m_vec.size () + size);
25975 return &*m_vec.end () - size;
25976 }
25977
25978 gdb::byte_vector m_vec;
25979 };
25980
25981 /* An entry in the symbol table. */
25982 struct symtab_index_entry
25983 {
25984 /* The name of the symbol. */
25985 const char *name;
25986 /* The offset of the name in the constant pool. */
25987 offset_type index_offset;
25988 /* A sorted vector of the indices of all the CUs that hold an object
25989 of this name. */
25990 std::vector<offset_type> cu_indices;
25991 };
25992
25993 /* The symbol table. This is a power-of-2-sized hash table. */
25994 struct mapped_symtab
25995 {
25996 mapped_symtab ()
25997 {
25998 data.resize (1024);
25999 }
26000
26001 offset_type n_elements = 0;
26002 std::vector<symtab_index_entry> data;
26003 };
26004
26005 /* Find a slot in SYMTAB for the symbol NAME. Returns a reference to
26006 the slot.
26007
26008 Function is used only during write_hash_table so no index format backward
26009 compatibility is needed. */
26010
26011 static symtab_index_entry &
26012 find_slot (struct mapped_symtab *symtab, const char *name)
26013 {
26014 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
26015
26016 index = hash & (symtab->data.size () - 1);
26017 step = ((hash * 17) & (symtab->data.size () - 1)) | 1;
26018
26019 for (;;)
26020 {
26021 if (symtab->data[index].name == NULL
26022 || strcmp (name, symtab->data[index].name) == 0)
26023 return symtab->data[index];
26024 index = (index + step) & (symtab->data.size () - 1);
26025 }
26026 }
26027
26028 /* Expand SYMTAB's hash table. */
26029
26030 static void
26031 hash_expand (struct mapped_symtab *symtab)
26032 {
26033 auto old_entries = std::move (symtab->data);
26034
26035 symtab->data.clear ();
26036 symtab->data.resize (old_entries.size () * 2);
26037
26038 for (auto &it : old_entries)
26039 if (it.name != NULL)
26040 {
26041 auto &ref = find_slot (symtab, it.name);
26042 ref = std::move (it);
26043 }
26044 }
26045
26046 /* Add an entry to SYMTAB. NAME is the name of the symbol.
26047 CU_INDEX is the index of the CU in which the symbol appears.
26048 IS_STATIC is one if the symbol is static, otherwise zero (global). */
26049
26050 static void
26051 add_index_entry (struct mapped_symtab *symtab, const char *name,
26052 int is_static, gdb_index_symbol_kind kind,
26053 offset_type cu_index)
26054 {
26055 offset_type cu_index_and_attrs;
26056
26057 ++symtab->n_elements;
26058 if (4 * symtab->n_elements / 3 >= symtab->data.size ())
26059 hash_expand (symtab);
26060
26061 symtab_index_entry &slot = find_slot (symtab, name);
26062 if (slot.name == NULL)
26063 {
26064 slot.name = name;
26065 /* index_offset is set later. */
26066 }
26067
26068 cu_index_and_attrs = 0;
26069 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
26070 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
26071 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
26072
26073 /* We don't want to record an index value twice as we want to avoid the
26074 duplication.
26075 We process all global symbols and then all static symbols
26076 (which would allow us to avoid the duplication by only having to check
26077 the last entry pushed), but a symbol could have multiple kinds in one CU.
26078 To keep things simple we don't worry about the duplication here and
26079 sort and uniqufy the list after we've processed all symbols. */
26080 slot.cu_indices.push_back (cu_index_and_attrs);
26081 }
26082
26083 /* Sort and remove duplicates of all symbols' cu_indices lists. */
26084
26085 static void
26086 uniquify_cu_indices (struct mapped_symtab *symtab)
26087 {
26088 for (auto &entry : symtab->data)
26089 {
26090 if (entry.name != NULL && !entry.cu_indices.empty ())
26091 {
26092 auto &cu_indices = entry.cu_indices;
26093 std::sort (cu_indices.begin (), cu_indices.end ());
26094 auto from = std::unique (cu_indices.begin (), cu_indices.end ());
26095 cu_indices.erase (from, cu_indices.end ());
26096 }
26097 }
26098 }
26099
26100 /* A form of 'const char *' suitable for container keys. Only the
26101 pointer is stored. The strings themselves are compared, not the
26102 pointers. */
26103 class c_str_view
26104 {
26105 public:
26106 c_str_view (const char *cstr)
26107 : m_cstr (cstr)
26108 {}
26109
26110 bool operator== (const c_str_view &other) const
26111 {
26112 return strcmp (m_cstr, other.m_cstr) == 0;
26113 }
26114
26115 /* Return the underlying C string. Note, the returned string is
26116 only a reference with lifetime of this object. */
26117 const char *c_str () const
26118 {
26119 return m_cstr;
26120 }
26121
26122 private:
26123 friend class c_str_view_hasher;
26124 const char *const m_cstr;
26125 };
26126
26127 /* A std::unordered_map::hasher for c_str_view that uses the right
26128 hash function for strings in a mapped index. */
26129 class c_str_view_hasher
26130 {
26131 public:
26132 size_t operator () (const c_str_view &x) const
26133 {
26134 return mapped_index_string_hash (INT_MAX, x.m_cstr);
26135 }
26136 };
26137
26138 /* A std::unordered_map::hasher for std::vector<>. */
26139 template<typename T>
26140 class vector_hasher
26141 {
26142 public:
26143 size_t operator () (const std::vector<T> &key) const
26144 {
26145 return iterative_hash (key.data (),
26146 sizeof (key.front ()) * key.size (), 0);
26147 }
26148 };
26149
26150 /* Write the mapped hash table SYMTAB to the data buffer OUTPUT, with
26151 constant pool entries going into the data buffer CPOOL. */
26152
26153 static void
26154 write_hash_table (mapped_symtab *symtab, data_buf &output, data_buf &cpool)
26155 {
26156 {
26157 /* Elements are sorted vectors of the indices of all the CUs that
26158 hold an object of this name. */
26159 std::unordered_map<std::vector<offset_type>, offset_type,
26160 vector_hasher<offset_type>>
26161 symbol_hash_table;
26162
26163 /* We add all the index vectors to the constant pool first, to
26164 ensure alignment is ok. */
26165 for (symtab_index_entry &entry : symtab->data)
26166 {
26167 if (entry.name == NULL)
26168 continue;
26169 gdb_assert (entry.index_offset == 0);
26170
26171 /* Finding before inserting is faster than always trying to
26172 insert, because inserting always allocates a node, does the
26173 lookup, and then destroys the new node if another node
26174 already had the same key. C++17 try_emplace will avoid
26175 this. */
26176 const auto found
26177 = symbol_hash_table.find (entry.cu_indices);
26178 if (found != symbol_hash_table.end ())
26179 {
26180 entry.index_offset = found->second;
26181 continue;
26182 }
26183
26184 symbol_hash_table.emplace (entry.cu_indices, cpool.size ());
26185 entry.index_offset = cpool.size ();
26186 cpool.append_data (MAYBE_SWAP (entry.cu_indices.size ()));
26187 for (const auto index : entry.cu_indices)
26188 cpool.append_data (MAYBE_SWAP (index));
26189 }
26190 }
26191
26192 /* Now write out the hash table. */
26193 std::unordered_map<c_str_view, offset_type, c_str_view_hasher> str_table;
26194 for (const auto &entry : symtab->data)
26195 {
26196 offset_type str_off, vec_off;
26197
26198 if (entry.name != NULL)
26199 {
26200 const auto insertpair = str_table.emplace (entry.name, cpool.size ());
26201 if (insertpair.second)
26202 cpool.append_cstr0 (entry.name);
26203 str_off = insertpair.first->second;
26204 vec_off = entry.index_offset;
26205 }
26206 else
26207 {
26208 /* While 0 is a valid constant pool index, it is not valid
26209 to have 0 for both offsets. */
26210 str_off = 0;
26211 vec_off = 0;
26212 }
26213
26214 output.append_data (MAYBE_SWAP (str_off));
26215 output.append_data (MAYBE_SWAP (vec_off));
26216 }
26217 }
26218
26219 typedef std::unordered_map<partial_symtab *, unsigned int> psym_index_map;
26220
26221 /* Helper struct for building the address table. */
26222 struct addrmap_index_data
26223 {
26224 addrmap_index_data (data_buf &addr_vec_, psym_index_map &cu_index_htab_)
26225 : addr_vec (addr_vec_), cu_index_htab (cu_index_htab_)
26226 {}
26227
26228 struct objfile *objfile;
26229 data_buf &addr_vec;
26230 psym_index_map &cu_index_htab;
26231
26232 /* Non-zero if the previous_* fields are valid.
26233 We can't write an entry until we see the next entry (since it is only then
26234 that we know the end of the entry). */
26235 int previous_valid;
26236 /* Index of the CU in the table of all CUs in the index file. */
26237 unsigned int previous_cu_index;
26238 /* Start address of the CU. */
26239 CORE_ADDR previous_cu_start;
26240 };
26241
26242 /* Write an address entry to ADDR_VEC. */
26243
26244 static void
26245 add_address_entry (struct objfile *objfile, data_buf &addr_vec,
26246 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
26247 {
26248 CORE_ADDR baseaddr;
26249
26250 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
26251
26252 addr_vec.append_uint (8, BFD_ENDIAN_LITTLE, start - baseaddr);
26253 addr_vec.append_uint (8, BFD_ENDIAN_LITTLE, end - baseaddr);
26254 addr_vec.append_data (MAYBE_SWAP (cu_index));
26255 }
26256
26257 /* Worker function for traversing an addrmap to build the address table. */
26258
26259 static int
26260 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
26261 {
26262 struct addrmap_index_data *data = (struct addrmap_index_data *) datap;
26263 struct partial_symtab *pst = (struct partial_symtab *) obj;
26264
26265 if (data->previous_valid)
26266 add_address_entry (data->objfile, data->addr_vec,
26267 data->previous_cu_start, start_addr,
26268 data->previous_cu_index);
26269
26270 data->previous_cu_start = start_addr;
26271 if (pst != NULL)
26272 {
26273 const auto it = data->cu_index_htab.find (pst);
26274 gdb_assert (it != data->cu_index_htab.cend ());
26275 data->previous_cu_index = it->second;
26276 data->previous_valid = 1;
26277 }
26278 else
26279 data->previous_valid = 0;
26280
26281 return 0;
26282 }
26283
26284 /* Write OBJFILE's address map to ADDR_VEC.
26285 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
26286 in the index file. */
26287
26288 static void
26289 write_address_map (struct objfile *objfile, data_buf &addr_vec,
26290 psym_index_map &cu_index_htab)
26291 {
26292 struct addrmap_index_data addrmap_index_data (addr_vec, cu_index_htab);
26293
26294 /* When writing the address table, we have to cope with the fact that
26295 the addrmap iterator only provides the start of a region; we have to
26296 wait until the next invocation to get the start of the next region. */
26297
26298 addrmap_index_data.objfile = objfile;
26299 addrmap_index_data.previous_valid = 0;
26300
26301 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
26302 &addrmap_index_data);
26303
26304 /* It's highly unlikely the last entry (end address = 0xff...ff)
26305 is valid, but we should still handle it.
26306 The end address is recorded as the start of the next region, but that
26307 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
26308 anyway. */
26309 if (addrmap_index_data.previous_valid)
26310 add_address_entry (objfile, addr_vec,
26311 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
26312 addrmap_index_data.previous_cu_index);
26313 }
26314
26315 /* Return the symbol kind of PSYM. */
26316
26317 static gdb_index_symbol_kind
26318 symbol_kind (struct partial_symbol *psym)
26319 {
26320 domain_enum domain = PSYMBOL_DOMAIN (psym);
26321 enum address_class aclass = PSYMBOL_CLASS (psym);
26322
26323 switch (domain)
26324 {
26325 case VAR_DOMAIN:
26326 switch (aclass)
26327 {
26328 case LOC_BLOCK:
26329 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
26330 case LOC_TYPEDEF:
26331 return GDB_INDEX_SYMBOL_KIND_TYPE;
26332 case LOC_COMPUTED:
26333 case LOC_CONST_BYTES:
26334 case LOC_OPTIMIZED_OUT:
26335 case LOC_STATIC:
26336 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
26337 case LOC_CONST:
26338 /* Note: It's currently impossible to recognize psyms as enum values
26339 short of reading the type info. For now punt. */
26340 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
26341 default:
26342 /* There are other LOC_FOO values that one might want to classify
26343 as variables, but dwarf2read.c doesn't currently use them. */
26344 return GDB_INDEX_SYMBOL_KIND_OTHER;
26345 }
26346 case STRUCT_DOMAIN:
26347 return GDB_INDEX_SYMBOL_KIND_TYPE;
26348 default:
26349 return GDB_INDEX_SYMBOL_KIND_OTHER;
26350 }
26351 }
26352
26353 /* Add a list of partial symbols to SYMTAB. */
26354
26355 static void
26356 write_psymbols (struct mapped_symtab *symtab,
26357 std::unordered_set<partial_symbol *> &psyms_seen,
26358 struct partial_symbol **psymp,
26359 int count,
26360 offset_type cu_index,
26361 int is_static)
26362 {
26363 for (; count-- > 0; ++psymp)
26364 {
26365 struct partial_symbol *psym = *psymp;
26366
26367 if (SYMBOL_LANGUAGE (psym) == language_ada)
26368 error (_("Ada is not currently supported by the index"));
26369
26370 /* Only add a given psymbol once. */
26371 if (psyms_seen.insert (psym).second)
26372 {
26373 gdb_index_symbol_kind kind = symbol_kind (psym);
26374
26375 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
26376 is_static, kind, cu_index);
26377 }
26378 }
26379 }
26380
26381 /* A helper struct used when iterating over debug_types. */
26382 struct signatured_type_index_data
26383 {
26384 signatured_type_index_data (data_buf &types_list_,
26385 std::unordered_set<partial_symbol *> &psyms_seen_)
26386 : types_list (types_list_), psyms_seen (psyms_seen_)
26387 {}
26388
26389 struct objfile *objfile;
26390 struct mapped_symtab *symtab;
26391 data_buf &types_list;
26392 std::unordered_set<partial_symbol *> &psyms_seen;
26393 int cu_index;
26394 };
26395
26396 /* A helper function that writes a single signatured_type to an
26397 obstack. */
26398
26399 static int
26400 write_one_signatured_type (void **slot, void *d)
26401 {
26402 struct signatured_type_index_data *info
26403 = (struct signatured_type_index_data *) d;
26404 struct signatured_type *entry = (struct signatured_type *) *slot;
26405 struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
26406
26407 write_psymbols (info->symtab,
26408 info->psyms_seen,
26409 &info->objfile->global_psymbols[psymtab->globals_offset],
26410 psymtab->n_global_syms, info->cu_index,
26411 0);
26412 write_psymbols (info->symtab,
26413 info->psyms_seen,
26414 &info->objfile->static_psymbols[psymtab->statics_offset],
26415 psymtab->n_static_syms, info->cu_index,
26416 1);
26417
26418 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE,
26419 to_underlying (entry->per_cu.sect_off));
26420 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE,
26421 to_underlying (entry->type_offset_in_tu));
26422 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE, entry->signature);
26423
26424 ++info->cu_index;
26425
26426 return 1;
26427 }
26428
26429 /* Recurse into all "included" dependencies and count their symbols as
26430 if they appeared in this psymtab. */
26431
26432 static void
26433 recursively_count_psymbols (struct partial_symtab *psymtab,
26434 size_t &psyms_seen)
26435 {
26436 for (int i = 0; i < psymtab->number_of_dependencies; ++i)
26437 if (psymtab->dependencies[i]->user != NULL)
26438 recursively_count_psymbols (psymtab->dependencies[i],
26439 psyms_seen);
26440
26441 psyms_seen += psymtab->n_global_syms;
26442 psyms_seen += psymtab->n_static_syms;
26443 }
26444
26445 /* Recurse into all "included" dependencies and write their symbols as
26446 if they appeared in this psymtab. */
26447
26448 static void
26449 recursively_write_psymbols (struct objfile *objfile,
26450 struct partial_symtab *psymtab,
26451 struct mapped_symtab *symtab,
26452 std::unordered_set<partial_symbol *> &psyms_seen,
26453 offset_type cu_index)
26454 {
26455 int i;
26456
26457 for (i = 0; i < psymtab->number_of_dependencies; ++i)
26458 if (psymtab->dependencies[i]->user != NULL)
26459 recursively_write_psymbols (objfile, psymtab->dependencies[i],
26460 symtab, psyms_seen, cu_index);
26461
26462 write_psymbols (symtab,
26463 psyms_seen,
26464 &objfile->global_psymbols[psymtab->globals_offset],
26465 psymtab->n_global_syms, cu_index,
26466 0);
26467 write_psymbols (symtab,
26468 psyms_seen,
26469 &objfile->static_psymbols[psymtab->statics_offset],
26470 psymtab->n_static_syms, cu_index,
26471 1);
26472 }
26473
26474 /* DWARF-5 .debug_names builder. */
26475 class debug_names
26476 {
26477 public:
26478 debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile, bool is_dwarf64,
26479 bfd_endian dwarf5_byte_order)
26480 : m_dwarf5_byte_order (dwarf5_byte_order),
26481 m_dwarf32 (dwarf5_byte_order),
26482 m_dwarf64 (dwarf5_byte_order),
26483 m_dwarf (is_dwarf64
26484 ? static_cast<dwarf &> (m_dwarf64)
26485 : static_cast<dwarf &> (m_dwarf32)),
26486 m_name_table_string_offs (m_dwarf.name_table_string_offs),
26487 m_name_table_entry_offs (m_dwarf.name_table_entry_offs),
26488 m_debugstrlookup (dwarf2_per_objfile)
26489 {}
26490
26491 int dwarf5_offset_size () const
26492 {
26493 const bool dwarf5_is_dwarf64 = &m_dwarf == &m_dwarf64;
26494 return dwarf5_is_dwarf64 ? 8 : 4;
26495 }
26496
26497 /* Is this symbol from DW_TAG_compile_unit or DW_TAG_type_unit? */
26498 enum class unit_kind { cu, tu };
26499
26500 /* Insert one symbol. */
26501 void insert (const partial_symbol *psym, int cu_index, bool is_static,
26502 unit_kind kind)
26503 {
26504 const int dwarf_tag = psymbol_tag (psym);
26505 if (dwarf_tag == 0)
26506 return;
26507 const char *const name = SYMBOL_SEARCH_NAME (psym);
26508 const auto insertpair
26509 = m_name_to_value_set.emplace (c_str_view (name),
26510 std::set<symbol_value> ());
26511 std::set<symbol_value> &value_set = insertpair.first->second;
26512 value_set.emplace (symbol_value (dwarf_tag, cu_index, is_static, kind));
26513 }
26514
26515 /* Build all the tables. All symbols must be already inserted.
26516 This function does not call file_write, caller has to do it
26517 afterwards. */
26518 void build ()
26519 {
26520 /* Verify the build method has not be called twice. */
26521 gdb_assert (m_abbrev_table.empty ());
26522 const size_t name_count = m_name_to_value_set.size ();
26523 m_bucket_table.resize
26524 (std::pow (2, std::ceil (std::log2 (name_count * 4 / 3))));
26525 m_hash_table.reserve (name_count);
26526 m_name_table_string_offs.reserve (name_count);
26527 m_name_table_entry_offs.reserve (name_count);
26528
26529 /* Map each hash of symbol to its name and value. */
26530 struct hash_it_pair
26531 {
26532 uint32_t hash;
26533 decltype (m_name_to_value_set)::const_iterator it;
26534 };
26535 std::vector<std::forward_list<hash_it_pair>> bucket_hash;
26536 bucket_hash.resize (m_bucket_table.size ());
26537 for (decltype (m_name_to_value_set)::const_iterator it
26538 = m_name_to_value_set.cbegin ();
26539 it != m_name_to_value_set.cend ();
26540 ++it)
26541 {
26542 const char *const name = it->first.c_str ();
26543 const uint32_t hash = dwarf5_djb_hash (name);
26544 hash_it_pair hashitpair;
26545 hashitpair.hash = hash;
26546 hashitpair.it = it;
26547 auto &slot = bucket_hash[hash % bucket_hash.size()];
26548 slot.push_front (std::move (hashitpair));
26549 }
26550 for (size_t bucket_ix = 0; bucket_ix < bucket_hash.size (); ++bucket_ix)
26551 {
26552 const std::forward_list<hash_it_pair> &hashitlist
26553 = bucket_hash[bucket_ix];
26554 if (hashitlist.empty ())
26555 continue;
26556 uint32_t &bucket_slot = m_bucket_table[bucket_ix];
26557 /* The hashes array is indexed starting at 1. */
26558 store_unsigned_integer (reinterpret_cast<gdb_byte *> (&bucket_slot),
26559 sizeof (bucket_slot), m_dwarf5_byte_order,
26560 m_hash_table.size () + 1);
26561 for (const hash_it_pair &hashitpair : hashitlist)
26562 {
26563 m_hash_table.push_back (0);
26564 store_unsigned_integer (reinterpret_cast<gdb_byte *>
26565 (&m_hash_table.back ()),
26566 sizeof (m_hash_table.back ()),
26567 m_dwarf5_byte_order, hashitpair.hash);
26568 const c_str_view &name = hashitpair.it->first;
26569 const std::set<symbol_value> &value_set = hashitpair.it->second;
26570 m_name_table_string_offs.push_back_reorder
26571 (m_debugstrlookup.lookup (name.c_str ()));
26572 m_name_table_entry_offs.push_back_reorder (m_entry_pool.size ());
26573 gdb_assert (!value_set.empty ());
26574 for (const symbol_value &value : value_set)
26575 {
26576 int &idx = m_indexkey_to_idx[index_key (value.dwarf_tag,
26577 value.is_static,
26578 value.kind)];
26579 if (idx == 0)
26580 {
26581 idx = m_idx_next++;
26582 m_abbrev_table.append_unsigned_leb128 (idx);
26583 m_abbrev_table.append_unsigned_leb128 (value.dwarf_tag);
26584 m_abbrev_table.append_unsigned_leb128
26585 (value.kind == unit_kind::cu ? DW_IDX_compile_unit
26586 : DW_IDX_type_unit);
26587 m_abbrev_table.append_unsigned_leb128 (DW_FORM_udata);
26588 m_abbrev_table.append_unsigned_leb128 (value.is_static
26589 ? DW_IDX_GNU_internal
26590 : DW_IDX_GNU_external);
26591 m_abbrev_table.append_unsigned_leb128 (DW_FORM_flag_present);
26592
26593 /* Terminate attributes list. */
26594 m_abbrev_table.append_unsigned_leb128 (0);
26595 m_abbrev_table.append_unsigned_leb128 (0);
26596 }
26597
26598 m_entry_pool.append_unsigned_leb128 (idx);
26599 m_entry_pool.append_unsigned_leb128 (value.cu_index);
26600 }
26601
26602 /* Terminate the list of CUs. */
26603 m_entry_pool.append_unsigned_leb128 (0);
26604 }
26605 }
26606 gdb_assert (m_hash_table.size () == name_count);
26607
26608 /* Terminate tags list. */
26609 m_abbrev_table.append_unsigned_leb128 (0);
26610 }
26611
26612 /* Return .debug_names bucket count. This must be called only after
26613 calling the build method. */
26614 uint32_t bucket_count () const
26615 {
26616 /* Verify the build method has been already called. */
26617 gdb_assert (!m_abbrev_table.empty ());
26618 const uint32_t retval = m_bucket_table.size ();
26619
26620 /* Check for overflow. */
26621 gdb_assert (retval == m_bucket_table.size ());
26622 return retval;
26623 }
26624
26625 /* Return .debug_names names count. This must be called only after
26626 calling the build method. */
26627 uint32_t name_count () const
26628 {
26629 /* Verify the build method has been already called. */
26630 gdb_assert (!m_abbrev_table.empty ());
26631 const uint32_t retval = m_hash_table.size ();
26632
26633 /* Check for overflow. */
26634 gdb_assert (retval == m_hash_table.size ());
26635 return retval;
26636 }
26637
26638 /* Return number of bytes of .debug_names abbreviation table. This
26639 must be called only after calling the build method. */
26640 uint32_t abbrev_table_bytes () const
26641 {
26642 gdb_assert (!m_abbrev_table.empty ());
26643 return m_abbrev_table.size ();
26644 }
26645
26646 /* Recurse into all "included" dependencies and store their symbols
26647 as if they appeared in this psymtab. */
26648 void recursively_write_psymbols
26649 (struct objfile *objfile,
26650 struct partial_symtab *psymtab,
26651 std::unordered_set<partial_symbol *> &psyms_seen,
26652 int cu_index)
26653 {
26654 for (int i = 0; i < psymtab->number_of_dependencies; ++i)
26655 if (psymtab->dependencies[i]->user != NULL)
26656 recursively_write_psymbols (objfile, psymtab->dependencies[i],
26657 psyms_seen, cu_index);
26658
26659 write_psymbols (psyms_seen,
26660 &objfile->global_psymbols[psymtab->globals_offset],
26661 psymtab->n_global_syms, cu_index, false, unit_kind::cu);
26662 write_psymbols (psyms_seen,
26663 &objfile->static_psymbols[psymtab->statics_offset],
26664 psymtab->n_static_syms, cu_index, true, unit_kind::cu);
26665 }
26666
26667 /* Return number of bytes the .debug_names section will have. This
26668 must be called only after calling the build method. */
26669 size_t bytes () const
26670 {
26671 /* Verify the build method has been already called. */
26672 gdb_assert (!m_abbrev_table.empty ());
26673 size_t expected_bytes = 0;
26674 expected_bytes += m_bucket_table.size () * sizeof (m_bucket_table[0]);
26675 expected_bytes += m_hash_table.size () * sizeof (m_hash_table[0]);
26676 expected_bytes += m_name_table_string_offs.bytes ();
26677 expected_bytes += m_name_table_entry_offs.bytes ();
26678 expected_bytes += m_abbrev_table.size ();
26679 expected_bytes += m_entry_pool.size ();
26680 return expected_bytes;
26681 }
26682
26683 /* Write .debug_names to FILE_NAMES and .debug_str addition to
26684 FILE_STR. This must be called only after calling the build
26685 method. */
26686 void file_write (FILE *file_names, FILE *file_str) const
26687 {
26688 /* Verify the build method has been already called. */
26689 gdb_assert (!m_abbrev_table.empty ());
26690 ::file_write (file_names, m_bucket_table);
26691 ::file_write (file_names, m_hash_table);
26692 m_name_table_string_offs.file_write (file_names);
26693 m_name_table_entry_offs.file_write (file_names);
26694 m_abbrev_table.file_write (file_names);
26695 m_entry_pool.file_write (file_names);
26696 m_debugstrlookup.file_write (file_str);
26697 }
26698
26699 /* A helper user data for write_one_signatured_type. */
26700 class write_one_signatured_type_data
26701 {
26702 public:
26703 write_one_signatured_type_data (debug_names &nametable_,
26704 signatured_type_index_data &&info_)
26705 : nametable (nametable_), info (std::move (info_))
26706 {}
26707 debug_names &nametable;
26708 struct signatured_type_index_data info;
26709 };
26710
26711 /* A helper function to pass write_one_signatured_type to
26712 htab_traverse_noresize. */
26713 static int
26714 write_one_signatured_type (void **slot, void *d)
26715 {
26716 write_one_signatured_type_data *data = (write_one_signatured_type_data *) d;
26717 struct signatured_type_index_data *info = &data->info;
26718 struct signatured_type *entry = (struct signatured_type *) *slot;
26719
26720 data->nametable.write_one_signatured_type (entry, info);
26721
26722 return 1;
26723 }
26724
26725 private:
26726
26727 /* Storage for symbol names mapping them to their .debug_str section
26728 offsets. */
26729 class debug_str_lookup
26730 {
26731 public:
26732
26733 /* Object costructor to be called for current DWARF2_PER_OBJFILE.
26734 All .debug_str section strings are automatically stored. */
26735 debug_str_lookup (struct dwarf2_per_objfile *dwarf2_per_objfile)
26736 : m_abfd (dwarf2_per_objfile->objfile->obfd),
26737 m_dwarf2_per_objfile (dwarf2_per_objfile)
26738 {
26739 dwarf2_read_section (dwarf2_per_objfile->objfile,
26740 &dwarf2_per_objfile->str);
26741 if (dwarf2_per_objfile->str.buffer == NULL)
26742 return;
26743 for (const gdb_byte *data = dwarf2_per_objfile->str.buffer;
26744 data < (dwarf2_per_objfile->str.buffer
26745 + dwarf2_per_objfile->str.size);)
26746 {
26747 const char *const s = reinterpret_cast<const char *> (data);
26748 const auto insertpair
26749 = m_str_table.emplace (c_str_view (s),
26750 data - dwarf2_per_objfile->str.buffer);
26751 if (!insertpair.second)
26752 complaint (&symfile_complaints,
26753 _("Duplicate string \"%s\" in "
26754 ".debug_str section [in module %s]"),
26755 s, bfd_get_filename (m_abfd));
26756 data += strlen (s) + 1;
26757 }
26758 }
26759
26760 /* Return offset of symbol name S in the .debug_str section. Add
26761 such symbol to the section's end if it does not exist there
26762 yet. */
26763 size_t lookup (const char *s)
26764 {
26765 const auto it = m_str_table.find (c_str_view (s));
26766 if (it != m_str_table.end ())
26767 return it->second;
26768 const size_t offset = (m_dwarf2_per_objfile->str.size
26769 + m_str_add_buf.size ());
26770 m_str_table.emplace (c_str_view (s), offset);
26771 m_str_add_buf.append_cstr0 (s);
26772 return offset;
26773 }
26774
26775 /* Append the end of the .debug_str section to FILE. */
26776 void file_write (FILE *file) const
26777 {
26778 m_str_add_buf.file_write (file);
26779 }
26780
26781 private:
26782 std::unordered_map<c_str_view, size_t, c_str_view_hasher> m_str_table;
26783 bfd *const m_abfd;
26784 struct dwarf2_per_objfile *m_dwarf2_per_objfile;
26785
26786 /* Data to add at the end of .debug_str for new needed symbol names. */
26787 data_buf m_str_add_buf;
26788 };
26789
26790 /* Container to map used DWARF tags to their .debug_names abbreviation
26791 tags. */
26792 class index_key
26793 {
26794 public:
26795 index_key (int dwarf_tag_, bool is_static_, unit_kind kind_)
26796 : dwarf_tag (dwarf_tag_), is_static (is_static_), kind (kind_)
26797 {
26798 }
26799
26800 bool
26801 operator== (const index_key &other) const
26802 {
26803 return (dwarf_tag == other.dwarf_tag && is_static == other.is_static
26804 && kind == other.kind);
26805 }
26806
26807 const int dwarf_tag;
26808 const bool is_static;
26809 const unit_kind kind;
26810 };
26811
26812 /* Provide std::unordered_map::hasher for index_key. */
26813 class index_key_hasher
26814 {
26815 public:
26816 size_t
26817 operator () (const index_key &key) const
26818 {
26819 return (std::hash<int>() (key.dwarf_tag) << 1) | key.is_static;
26820 }
26821 };
26822
26823 /* Parameters of one symbol entry. */
26824 class symbol_value
26825 {
26826 public:
26827 const int dwarf_tag, cu_index;
26828 const bool is_static;
26829 const unit_kind kind;
26830
26831 symbol_value (int dwarf_tag_, int cu_index_, bool is_static_,
26832 unit_kind kind_)
26833 : dwarf_tag (dwarf_tag_), cu_index (cu_index_), is_static (is_static_),
26834 kind (kind_)
26835 {}
26836
26837 bool
26838 operator< (const symbol_value &other) const
26839 {
26840 #define X(n) \
26841 do \
26842 { \
26843 if (n < other.n) \
26844 return true; \
26845 if (n > other.n) \
26846 return false; \
26847 } \
26848 while (0)
26849 X (dwarf_tag);
26850 X (is_static);
26851 X (kind);
26852 X (cu_index);
26853 #undef X
26854 return false;
26855 }
26856 };
26857
26858 /* Abstract base class to unify DWARF-32 and DWARF-64 name table
26859 output. */
26860 class offset_vec
26861 {
26862 protected:
26863 const bfd_endian dwarf5_byte_order;
26864 public:
26865 explicit offset_vec (bfd_endian dwarf5_byte_order_)
26866 : dwarf5_byte_order (dwarf5_byte_order_)
26867 {}
26868
26869 /* Call std::vector::reserve for NELEM elements. */
26870 virtual void reserve (size_t nelem) = 0;
26871
26872 /* Call std::vector::push_back with store_unsigned_integer byte
26873 reordering for ELEM. */
26874 virtual void push_back_reorder (size_t elem) = 0;
26875
26876 /* Return expected output size in bytes. */
26877 virtual size_t bytes () const = 0;
26878
26879 /* Write name table to FILE. */
26880 virtual void file_write (FILE *file) const = 0;
26881 };
26882
26883 /* Template to unify DWARF-32 and DWARF-64 output. */
26884 template<typename OffsetSize>
26885 class offset_vec_tmpl : public offset_vec
26886 {
26887 public:
26888 explicit offset_vec_tmpl (bfd_endian dwarf5_byte_order_)
26889 : offset_vec (dwarf5_byte_order_)
26890 {}
26891
26892 /* Implement offset_vec::reserve. */
26893 void reserve (size_t nelem) override
26894 {
26895 m_vec.reserve (nelem);
26896 }
26897
26898 /* Implement offset_vec::push_back_reorder. */
26899 void push_back_reorder (size_t elem) override
26900 {
26901 m_vec.push_back (elem);
26902 /* Check for overflow. */
26903 gdb_assert (m_vec.back () == elem);
26904 store_unsigned_integer (reinterpret_cast<gdb_byte *> (&m_vec.back ()),
26905 sizeof (m_vec.back ()), dwarf5_byte_order, elem);
26906 }
26907
26908 /* Implement offset_vec::bytes. */
26909 size_t bytes () const override
26910 {
26911 return m_vec.size () * sizeof (m_vec[0]);
26912 }
26913
26914 /* Implement offset_vec::file_write. */
26915 void file_write (FILE *file) const override
26916 {
26917 ::file_write (file, m_vec);
26918 }
26919
26920 private:
26921 std::vector<OffsetSize> m_vec;
26922 };
26923
26924 /* Base class to unify DWARF-32 and DWARF-64 .debug_names output
26925 respecting name table width. */
26926 class dwarf
26927 {
26928 public:
26929 offset_vec &name_table_string_offs, &name_table_entry_offs;
26930
26931 dwarf (offset_vec &name_table_string_offs_,
26932 offset_vec &name_table_entry_offs_)
26933 : name_table_string_offs (name_table_string_offs_),
26934 name_table_entry_offs (name_table_entry_offs_)
26935 {
26936 }
26937 };
26938
26939 /* Template to unify DWARF-32 and DWARF-64 .debug_names output
26940 respecting name table width. */
26941 template<typename OffsetSize>
26942 class dwarf_tmpl : public dwarf
26943 {
26944 public:
26945 explicit dwarf_tmpl (bfd_endian dwarf5_byte_order_)
26946 : dwarf (m_name_table_string_offs, m_name_table_entry_offs),
26947 m_name_table_string_offs (dwarf5_byte_order_),
26948 m_name_table_entry_offs (dwarf5_byte_order_)
26949 {}
26950
26951 private:
26952 offset_vec_tmpl<OffsetSize> m_name_table_string_offs;
26953 offset_vec_tmpl<OffsetSize> m_name_table_entry_offs;
26954 };
26955
26956 /* Try to reconstruct original DWARF tag for given partial_symbol.
26957 This function is not DWARF-5 compliant but it is sufficient for
26958 GDB as a DWARF-5 index consumer. */
26959 static int psymbol_tag (const struct partial_symbol *psym)
26960 {
26961 domain_enum domain = PSYMBOL_DOMAIN (psym);
26962 enum address_class aclass = PSYMBOL_CLASS (psym);
26963
26964 switch (domain)
26965 {
26966 case VAR_DOMAIN:
26967 switch (aclass)
26968 {
26969 case LOC_BLOCK:
26970 return DW_TAG_subprogram;
26971 case LOC_TYPEDEF:
26972 return DW_TAG_typedef;
26973 case LOC_COMPUTED:
26974 case LOC_CONST_BYTES:
26975 case LOC_OPTIMIZED_OUT:
26976 case LOC_STATIC:
26977 return DW_TAG_variable;
26978 case LOC_CONST:
26979 /* Note: It's currently impossible to recognize psyms as enum values
26980 short of reading the type info. For now punt. */
26981 return DW_TAG_variable;
26982 default:
26983 /* There are other LOC_FOO values that one might want to classify
26984 as variables, but dwarf2read.c doesn't currently use them. */
26985 return DW_TAG_variable;
26986 }
26987 case STRUCT_DOMAIN:
26988 return DW_TAG_structure_type;
26989 default:
26990 return 0;
26991 }
26992 }
26993
26994 /* Call insert for all partial symbols and mark them in PSYMS_SEEN. */
26995 void write_psymbols (std::unordered_set<partial_symbol *> &psyms_seen,
26996 struct partial_symbol **psymp, int count, int cu_index,
26997 bool is_static, unit_kind kind)
26998 {
26999 for (; count-- > 0; ++psymp)
27000 {
27001 struct partial_symbol *psym = *psymp;
27002
27003 if (SYMBOL_LANGUAGE (psym) == language_ada)
27004 error (_("Ada is not currently supported by the index"));
27005
27006 /* Only add a given psymbol once. */
27007 if (psyms_seen.insert (psym).second)
27008 insert (psym, cu_index, is_static, kind);
27009 }
27010 }
27011
27012 /* A helper function that writes a single signatured_type
27013 to a debug_names. */
27014 void
27015 write_one_signatured_type (struct signatured_type *entry,
27016 struct signatured_type_index_data *info)
27017 {
27018 struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
27019
27020 write_psymbols (info->psyms_seen,
27021 &info->objfile->global_psymbols[psymtab->globals_offset],
27022 psymtab->n_global_syms, info->cu_index, false,
27023 unit_kind::tu);
27024 write_psymbols (info->psyms_seen,
27025 &info->objfile->static_psymbols[psymtab->statics_offset],
27026 psymtab->n_static_syms, info->cu_index, true,
27027 unit_kind::tu);
27028
27029 info->types_list.append_uint (dwarf5_offset_size (), m_dwarf5_byte_order,
27030 to_underlying (entry->per_cu.sect_off));
27031
27032 ++info->cu_index;
27033 }
27034
27035 /* Store value of each symbol. */
27036 std::unordered_map<c_str_view, std::set<symbol_value>, c_str_view_hasher>
27037 m_name_to_value_set;
27038
27039 /* Tables of DWARF-5 .debug_names. They are in object file byte
27040 order. */
27041 std::vector<uint32_t> m_bucket_table;
27042 std::vector<uint32_t> m_hash_table;
27043
27044 const bfd_endian m_dwarf5_byte_order;
27045 dwarf_tmpl<uint32_t> m_dwarf32;
27046 dwarf_tmpl<uint64_t> m_dwarf64;
27047 dwarf &m_dwarf;
27048 offset_vec &m_name_table_string_offs, &m_name_table_entry_offs;
27049 debug_str_lookup m_debugstrlookup;
27050
27051 /* Map each used .debug_names abbreviation tag parameter to its
27052 index value. */
27053 std::unordered_map<index_key, int, index_key_hasher> m_indexkey_to_idx;
27054
27055 /* Next unused .debug_names abbreviation tag for
27056 m_indexkey_to_idx. */
27057 int m_idx_next = 1;
27058
27059 /* .debug_names abbreviation table. */
27060 data_buf m_abbrev_table;
27061
27062 /* .debug_names entry pool. */
27063 data_buf m_entry_pool;
27064 };
27065
27066 /* Return iff any of the needed offsets does not fit into 32-bit
27067 .debug_names section. */
27068
27069 static bool
27070 check_dwarf64_offsets (struct dwarf2_per_objfile *dwarf2_per_objfile)
27071 {
27072 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
27073 {
27074 const dwarf2_per_cu_data &per_cu = *dwarf2_per_objfile->all_comp_units[i];
27075
27076 if (to_underlying (per_cu.sect_off) >= (static_cast<uint64_t> (1) << 32))
27077 return true;
27078 }
27079 for (int i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
27080 {
27081 const signatured_type &sigtype = *dwarf2_per_objfile->all_type_units[i];
27082 const dwarf2_per_cu_data &per_cu = sigtype.per_cu;
27083
27084 if (to_underlying (per_cu.sect_off) >= (static_cast<uint64_t> (1) << 32))
27085 return true;
27086 }
27087 return false;
27088 }
27089
27090 /* The psyms_seen set is potentially going to be largish (~40k
27091 elements when indexing a -g3 build of GDB itself). Estimate the
27092 number of elements in order to avoid too many rehashes, which
27093 require rebuilding buckets and thus many trips to
27094 malloc/free. */
27095
27096 static size_t
27097 psyms_seen_size (struct dwarf2_per_objfile *dwarf2_per_objfile)
27098 {
27099 size_t psyms_count = 0;
27100 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
27101 {
27102 struct dwarf2_per_cu_data *per_cu
27103 = dwarf2_per_objfile->all_comp_units[i];
27104 struct partial_symtab *psymtab = per_cu->v.psymtab;
27105
27106 if (psymtab != NULL && psymtab->user == NULL)
27107 recursively_count_psymbols (psymtab, psyms_count);
27108 }
27109 /* Generating an index for gdb itself shows a ratio of
27110 TOTAL_SEEN_SYMS/UNIQUE_SYMS or ~5. 4 seems like a good bet. */
27111 return psyms_count / 4;
27112 }
27113
27114 /* Write new .gdb_index section for OBJFILE into OUT_FILE.
27115 Return how many bytes were expected to be written into OUT_FILE. */
27116
27117 static size_t
27118 write_gdbindex (struct dwarf2_per_objfile *dwarf2_per_objfile, FILE *out_file)
27119 {
27120 struct objfile *objfile = dwarf2_per_objfile->objfile;
27121 mapped_symtab symtab;
27122 data_buf cu_list;
27123
27124 /* While we're scanning CU's create a table that maps a psymtab pointer
27125 (which is what addrmap records) to its index (which is what is recorded
27126 in the index file). This will later be needed to write the address
27127 table. */
27128 psym_index_map cu_index_htab;
27129 cu_index_htab.reserve (dwarf2_per_objfile->n_comp_units);
27130
27131 /* The CU list is already sorted, so we don't need to do additional
27132 work here. Also, the debug_types entries do not appear in
27133 all_comp_units, but only in their own hash table. */
27134
27135 std::unordered_set<partial_symbol *> psyms_seen
27136 (psyms_seen_size (dwarf2_per_objfile));
27137 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
27138 {
27139 struct dwarf2_per_cu_data *per_cu
27140 = dwarf2_per_objfile->all_comp_units[i];
27141 struct partial_symtab *psymtab = per_cu->v.psymtab;
27142
27143 /* CU of a shared file from 'dwz -m' may be unused by this main file.
27144 It may be referenced from a local scope but in such case it does not
27145 need to be present in .gdb_index. */
27146 if (psymtab == NULL)
27147 continue;
27148
27149 if (psymtab->user == NULL)
27150 recursively_write_psymbols (objfile, psymtab, &symtab,
27151 psyms_seen, i);
27152
27153 const auto insertpair = cu_index_htab.emplace (psymtab, i);
27154 gdb_assert (insertpair.second);
27155
27156 cu_list.append_uint (8, BFD_ENDIAN_LITTLE,
27157 to_underlying (per_cu->sect_off));
27158 cu_list.append_uint (8, BFD_ENDIAN_LITTLE, per_cu->length);
27159 }
27160
27161 /* Dump the address map. */
27162 data_buf addr_vec;
27163 write_address_map (objfile, addr_vec, cu_index_htab);
27164
27165 /* Write out the .debug_type entries, if any. */
27166 data_buf types_cu_list;
27167 if (dwarf2_per_objfile->signatured_types)
27168 {
27169 signatured_type_index_data sig_data (types_cu_list,
27170 psyms_seen);
27171
27172 sig_data.objfile = objfile;
27173 sig_data.symtab = &symtab;
27174 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
27175 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
27176 write_one_signatured_type, &sig_data);
27177 }
27178
27179 /* Now that we've processed all symbols we can shrink their cu_indices
27180 lists. */
27181 uniquify_cu_indices (&symtab);
27182
27183 data_buf symtab_vec, constant_pool;
27184 write_hash_table (&symtab, symtab_vec, constant_pool);
27185
27186 data_buf contents;
27187 const offset_type size_of_contents = 6 * sizeof (offset_type);
27188 offset_type total_len = size_of_contents;
27189
27190 /* The version number. */
27191 contents.append_data (MAYBE_SWAP (8));
27192
27193 /* The offset of the CU list from the start of the file. */
27194 contents.append_data (MAYBE_SWAP (total_len));
27195 total_len += cu_list.size ();
27196
27197 /* The offset of the types CU list from the start of the file. */
27198 contents.append_data (MAYBE_SWAP (total_len));
27199 total_len += types_cu_list.size ();
27200
27201 /* The offset of the address table from the start of the file. */
27202 contents.append_data (MAYBE_SWAP (total_len));
27203 total_len += addr_vec.size ();
27204
27205 /* The offset of the symbol table from the start of the file. */
27206 contents.append_data (MAYBE_SWAP (total_len));
27207 total_len += symtab_vec.size ();
27208
27209 /* The offset of the constant pool from the start of the file. */
27210 contents.append_data (MAYBE_SWAP (total_len));
27211 total_len += constant_pool.size ();
27212
27213 gdb_assert (contents.size () == size_of_contents);
27214
27215 contents.file_write (out_file);
27216 cu_list.file_write (out_file);
27217 types_cu_list.file_write (out_file);
27218 addr_vec.file_write (out_file);
27219 symtab_vec.file_write (out_file);
27220 constant_pool.file_write (out_file);
27221
27222 return total_len;
27223 }
27224
27225 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
27226 static const gdb_byte dwarf5_gdb_augmentation[] = { 'G', 'D', 'B', 0 };
27227
27228 /* Write a new .debug_names section for OBJFILE into OUT_FILE, write
27229 needed addition to .debug_str section to OUT_FILE_STR. Return how
27230 many bytes were expected to be written into OUT_FILE. */
27231
27232 static size_t
27233 write_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
27234 FILE *out_file, FILE *out_file_str)
27235 {
27236 const bool dwarf5_is_dwarf64 = check_dwarf64_offsets (dwarf2_per_objfile);
27237 struct objfile *objfile = dwarf2_per_objfile->objfile;
27238 const enum bfd_endian dwarf5_byte_order
27239 = gdbarch_byte_order (get_objfile_arch (objfile));
27240
27241 /* The CU list is already sorted, so we don't need to do additional
27242 work here. Also, the debug_types entries do not appear in
27243 all_comp_units, but only in their own hash table. */
27244 data_buf cu_list;
27245 debug_names nametable (dwarf2_per_objfile, dwarf5_is_dwarf64,
27246 dwarf5_byte_order);
27247 std::unordered_set<partial_symbol *>
27248 psyms_seen (psyms_seen_size (dwarf2_per_objfile));
27249 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
27250 {
27251 const dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->all_comp_units[i];
27252 partial_symtab *psymtab = per_cu->v.psymtab;
27253
27254 /* CU of a shared file from 'dwz -m' may be unused by this main
27255 file. It may be referenced from a local scope but in such
27256 case it does not need to be present in .debug_names. */
27257 if (psymtab == NULL)
27258 continue;
27259
27260 if (psymtab->user == NULL)
27261 nametable.recursively_write_psymbols (objfile, psymtab, psyms_seen, i);
27262
27263 cu_list.append_uint (nametable.dwarf5_offset_size (), dwarf5_byte_order,
27264 to_underlying (per_cu->sect_off));
27265 }
27266
27267 /* Write out the .debug_type entries, if any. */
27268 data_buf types_cu_list;
27269 if (dwarf2_per_objfile->signatured_types)
27270 {
27271 debug_names::write_one_signatured_type_data sig_data (nametable,
27272 signatured_type_index_data (types_cu_list, psyms_seen));
27273
27274 sig_data.info.objfile = objfile;
27275 /* It is used only for gdb_index. */
27276 sig_data.info.symtab = nullptr;
27277 sig_data.info.cu_index = 0;
27278 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
27279 debug_names::write_one_signatured_type,
27280 &sig_data);
27281 }
27282
27283 nametable.build ();
27284
27285 /* No addr_vec - DWARF-5 uses .debug_aranges generated by GCC. */
27286
27287 const offset_type bytes_of_header
27288 = ((dwarf5_is_dwarf64 ? 12 : 4)
27289 + 2 + 2 + 7 * 4
27290 + sizeof (dwarf5_gdb_augmentation));
27291 size_t expected_bytes = 0;
27292 expected_bytes += bytes_of_header;
27293 expected_bytes += cu_list.size ();
27294 expected_bytes += types_cu_list.size ();
27295 expected_bytes += nametable.bytes ();
27296 data_buf header;
27297
27298 if (!dwarf5_is_dwarf64)
27299 {
27300 const uint64_t size64 = expected_bytes - 4;
27301 gdb_assert (size64 < 0xfffffff0);
27302 header.append_uint (4, dwarf5_byte_order, size64);
27303 }
27304 else
27305 {
27306 header.append_uint (4, dwarf5_byte_order, 0xffffffff);
27307 header.append_uint (8, dwarf5_byte_order, expected_bytes - 12);
27308 }
27309
27310 /* The version number. */
27311 header.append_uint (2, dwarf5_byte_order, 5);
27312
27313 /* Padding. */
27314 header.append_uint (2, dwarf5_byte_order, 0);
27315
27316 /* comp_unit_count - The number of CUs in the CU list. */
27317 header.append_uint (4, dwarf5_byte_order, dwarf2_per_objfile->n_comp_units);
27318
27319 /* local_type_unit_count - The number of TUs in the local TU
27320 list. */
27321 header.append_uint (4, dwarf5_byte_order, dwarf2_per_objfile->n_type_units);
27322
27323 /* foreign_type_unit_count - The number of TUs in the foreign TU
27324 list. */
27325 header.append_uint (4, dwarf5_byte_order, 0);
27326
27327 /* bucket_count - The number of hash buckets in the hash lookup
27328 table. */
27329 header.append_uint (4, dwarf5_byte_order, nametable.bucket_count ());
27330
27331 /* name_count - The number of unique names in the index. */
27332 header.append_uint (4, dwarf5_byte_order, nametable.name_count ());
27333
27334 /* abbrev_table_size - The size in bytes of the abbreviations
27335 table. */
27336 header.append_uint (4, dwarf5_byte_order, nametable.abbrev_table_bytes ());
27337
27338 /* augmentation_string_size - The size in bytes of the augmentation
27339 string. This value is rounded up to a multiple of 4. */
27340 static_assert (sizeof (dwarf5_gdb_augmentation) % 4 == 0, "");
27341 header.append_uint (4, dwarf5_byte_order, sizeof (dwarf5_gdb_augmentation));
27342 header.append_data (dwarf5_gdb_augmentation);
27343
27344 gdb_assert (header.size () == bytes_of_header);
27345
27346 header.file_write (out_file);
27347 cu_list.file_write (out_file);
27348 types_cu_list.file_write (out_file);
27349 nametable.file_write (out_file, out_file_str);
27350
27351 return expected_bytes;
27352 }
27353
27354 /* Assert that FILE's size is EXPECTED_SIZE. Assumes file's seek
27355 position is at the end of the file. */
27356
27357 static void
27358 assert_file_size (FILE *file, const char *filename, size_t expected_size)
27359 {
27360 const auto file_size = ftell (file);
27361 if (file_size == -1)
27362 error (_("Can't get `%s' size"), filename);
27363 gdb_assert (file_size == expected_size);
27364 }
27365
27366 /* Create an index file for OBJFILE in the directory DIR. */
27367
27368 static void
27369 write_psymtabs_to_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
27370 const char *dir,
27371 dw_index_kind index_kind)
27372 {
27373 struct objfile *objfile = dwarf2_per_objfile->objfile;
27374
27375 if (dwarf2_per_objfile->using_index)
27376 error (_("Cannot use an index to create the index"));
27377
27378 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
27379 error (_("Cannot make an index when the file has multiple .debug_types sections"));
27380
27381 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
27382 return;
27383
27384 struct stat st;
27385 if (stat (objfile_name (objfile), &st) < 0)
27386 perror_with_name (objfile_name (objfile));
27387
27388 std::string filename (std::string (dir) + SLASH_STRING
27389 + lbasename (objfile_name (objfile))
27390 + (index_kind == dw_index_kind::DEBUG_NAMES
27391 ? INDEX5_SUFFIX : INDEX4_SUFFIX));
27392
27393 FILE *out_file = gdb_fopen_cloexec (filename.c_str (), "wb").release ();
27394 if (!out_file)
27395 error (_("Can't open `%s' for writing"), filename.c_str ());
27396
27397 /* Order matters here; we want FILE to be closed before FILENAME is
27398 unlinked, because on MS-Windows one cannot delete a file that is
27399 still open. (Don't call anything here that might throw until
27400 file_closer is created.) */
27401 gdb::unlinker unlink_file (filename.c_str ());
27402 gdb_file_up close_out_file (out_file);
27403
27404 if (index_kind == dw_index_kind::DEBUG_NAMES)
27405 {
27406 std::string filename_str (std::string (dir) + SLASH_STRING
27407 + lbasename (objfile_name (objfile))
27408 + DEBUG_STR_SUFFIX);
27409 FILE *out_file_str
27410 = gdb_fopen_cloexec (filename_str.c_str (), "wb").release ();
27411 if (!out_file_str)
27412 error (_("Can't open `%s' for writing"), filename_str.c_str ());
27413 gdb::unlinker unlink_file_str (filename_str.c_str ());
27414 gdb_file_up close_out_file_str (out_file_str);
27415
27416 const size_t total_len
27417 = write_debug_names (dwarf2_per_objfile, out_file, out_file_str);
27418 assert_file_size (out_file, filename.c_str (), total_len);
27419
27420 /* We want to keep the file .debug_str file too. */
27421 unlink_file_str.keep ();
27422 }
27423 else
27424 {
27425 const size_t total_len
27426 = write_gdbindex (dwarf2_per_objfile, out_file);
27427 assert_file_size (out_file, filename.c_str (), total_len);
27428 }
27429
27430 /* We want to keep the file. */
27431 unlink_file.keep ();
27432 }
27433
27434 /* Implementation of the `save gdb-index' command.
27435
27436 Note that the .gdb_index file format used by this command is
27437 documented in the GDB manual. Any changes here must be documented
27438 there. */
27439
27440 static void
27441 save_gdb_index_command (const char *arg, int from_tty)
27442 {
27443 struct objfile *objfile;
27444 const char dwarf5space[] = "-dwarf-5 ";
27445 dw_index_kind index_kind = dw_index_kind::GDB_INDEX;
27446
27447 if (!arg)
27448 arg = "";
27449
27450 arg = skip_spaces (arg);
27451 if (strncmp (arg, dwarf5space, strlen (dwarf5space)) == 0)
27452 {
27453 index_kind = dw_index_kind::DEBUG_NAMES;
27454 arg += strlen (dwarf5space);
27455 arg = skip_spaces (arg);
27456 }
27457
27458 if (!*arg)
27459 error (_("usage: save gdb-index [-dwarf-5] DIRECTORY"));
27460
27461 ALL_OBJFILES (objfile)
27462 {
27463 struct stat st;
27464
27465 /* If the objfile does not correspond to an actual file, skip it. */
27466 if (stat (objfile_name (objfile), &st) < 0)
27467 continue;
27468
27469 struct dwarf2_per_objfile *dwarf2_per_objfile
27470 = get_dwarf2_per_objfile (objfile);
27471
27472 if (dwarf2_per_objfile != NULL)
27473 {
27474 TRY
27475 {
27476 write_psymtabs_to_index (dwarf2_per_objfile, arg, index_kind);
27477 }
27478 CATCH (except, RETURN_MASK_ERROR)
27479 {
27480 exception_fprintf (gdb_stderr, except,
27481 _("Error while writing index for `%s': "),
27482 objfile_name (objfile));
27483 }
27484 END_CATCH
27485 }
27486
27487 }
27488 }
27489
27490 \f
27491
27492 int dwarf_always_disassemble;
27493
27494 static void
27495 show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
27496 struct cmd_list_element *c, const char *value)
27497 {
27498 fprintf_filtered (file,
27499 _("Whether to always disassemble "
27500 "DWARF expressions is %s.\n"),
27501 value);
27502 }
27503
27504 static void
27505 show_check_physname (struct ui_file *file, int from_tty,
27506 struct cmd_list_element *c, const char *value)
27507 {
27508 fprintf_filtered (file,
27509 _("Whether to check \"physname\" is %s.\n"),
27510 value);
27511 }
27512
27513 void
27514 _initialize_dwarf2_read (void)
27515 {
27516 struct cmd_list_element *c;
27517
27518 dwarf2_objfile_data_key = register_objfile_data ();
27519
27520 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
27521 Set DWARF specific variables.\n\
27522 Configure DWARF variables such as the cache size"),
27523 &set_dwarf_cmdlist, "maintenance set dwarf ",
27524 0/*allow-unknown*/, &maintenance_set_cmdlist);
27525
27526 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
27527 Show DWARF specific variables\n\
27528 Show DWARF variables such as the cache size"),
27529 &show_dwarf_cmdlist, "maintenance show dwarf ",
27530 0/*allow-unknown*/, &maintenance_show_cmdlist);
27531
27532 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
27533 &dwarf_max_cache_age, _("\
27534 Set the upper bound on the age of cached DWARF compilation units."), _("\
27535 Show the upper bound on the age of cached DWARF compilation units."), _("\
27536 A higher limit means that cached compilation units will be stored\n\
27537 in memory longer, and more total memory will be used. Zero disables\n\
27538 caching, which can slow down startup."),
27539 NULL,
27540 show_dwarf_max_cache_age,
27541 &set_dwarf_cmdlist,
27542 &show_dwarf_cmdlist);
27543
27544 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
27545 &dwarf_always_disassemble, _("\
27546 Set whether `info address' always disassembles DWARF expressions."), _("\
27547 Show whether `info address' always disassembles DWARF expressions."), _("\
27548 When enabled, DWARF expressions are always printed in an assembly-like\n\
27549 syntax. When disabled, expressions will be printed in a more\n\
27550 conversational style, when possible."),
27551 NULL,
27552 show_dwarf_always_disassemble,
27553 &set_dwarf_cmdlist,
27554 &show_dwarf_cmdlist);
27555
27556 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
27557 Set debugging of the DWARF reader."), _("\
27558 Show debugging of the DWARF reader."), _("\
27559 When enabled (non-zero), debugging messages are printed during DWARF\n\
27560 reading and symtab expansion. A value of 1 (one) provides basic\n\
27561 information. A value greater than 1 provides more verbose information."),
27562 NULL,
27563 NULL,
27564 &setdebuglist, &showdebuglist);
27565
27566 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
27567 Set debugging of the DWARF DIE reader."), _("\
27568 Show debugging of the DWARF DIE reader."), _("\
27569 When enabled (non-zero), DIEs are dumped after they are read in.\n\
27570 The value is the maximum depth to print."),
27571 NULL,
27572 NULL,
27573 &setdebuglist, &showdebuglist);
27574
27575 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
27576 Set debugging of the dwarf line reader."), _("\
27577 Show debugging of the dwarf line reader."), _("\
27578 When enabled (non-zero), line number entries are dumped as they are read in.\n\
27579 A value of 1 (one) provides basic information.\n\
27580 A value greater than 1 provides more verbose information."),
27581 NULL,
27582 NULL,
27583 &setdebuglist, &showdebuglist);
27584
27585 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
27586 Set cross-checking of \"physname\" code against demangler."), _("\
27587 Show cross-checking of \"physname\" code against demangler."), _("\
27588 When enabled, GDB's internal \"physname\" code is checked against\n\
27589 the demangler."),
27590 NULL, show_check_physname,
27591 &setdebuglist, &showdebuglist);
27592
27593 add_setshow_boolean_cmd ("use-deprecated-index-sections",
27594 no_class, &use_deprecated_index_sections, _("\
27595 Set whether to use deprecated gdb_index sections."), _("\
27596 Show whether to use deprecated gdb_index sections."), _("\
27597 When enabled, deprecated .gdb_index sections are used anyway.\n\
27598 Normally they are ignored either because of a missing feature or\n\
27599 performance issue.\n\
27600 Warning: This option must be enabled before gdb reads the file."),
27601 NULL,
27602 NULL,
27603 &setlist, &showlist);
27604
27605 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
27606 _("\
27607 Save a gdb-index file.\n\
27608 Usage: save gdb-index [-dwarf-5] DIRECTORY\n\
27609 \n\
27610 No options create one file with .gdb-index extension for pre-DWARF-5\n\
27611 compatible .gdb_index section. With -dwarf-5 creates two files with\n\
27612 extension .debug_names and .debug_str for DWARF-5 .debug_names section."),
27613 &save_cmdlist);
27614 set_cmd_completer (c, filename_completer);
27615
27616 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
27617 &dwarf2_locexpr_funcs);
27618 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
27619 &dwarf2_loclist_funcs);
27620
27621 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
27622 &dwarf2_block_frame_base_locexpr_funcs);
27623 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
27624 &dwarf2_block_frame_base_loclist_funcs);
27625
27626 #if GDB_SELF_TEST
27627 selftests::register_test ("dw2_expand_symtabs_matching",
27628 selftests::dw2_expand_symtabs_matching::run_test);
27629 #endif
27630 }
This page took 0.558652 seconds and 3 git commands to generate.