Remove OBJF_REORDERED
[binutils-gdb.git] / gdb / dwarf2 / read-gdb-index.c
1 /* Reading code for .gdb_index
2
3 Copyright (C) 2023 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20 #include "defs.h"
21 #include "read-gdb-index.h"
22
23 #include "cli/cli-cmds.h"
24 #include "complaints.h"
25 #include "dwz.h"
26 #include "gdb/gdb-index.h"
27 #include "gdbsupport/gdb-checked-static-cast.h"
28 #include "mapped-index.h"
29 #include "read.h"
30
31 /* When true, do not reject deprecated .gdb_index sections. */
32 static bool use_deprecated_index_sections = false;
33
34 /* This is a view into the index that converts from bytes to an
35 offset_type, and allows indexing. Unaligned bytes are specifically
36 allowed here, and handled via unpacking. */
37
38 class offset_view
39 {
40 public:
41 offset_view () = default;
42
43 explicit offset_view (gdb::array_view<const gdb_byte> bytes)
44 : m_bytes (bytes)
45 {
46 }
47
48 /* Extract the INDEXth offset_type from the array. */
49 offset_type operator[] (size_t index) const
50 {
51 const gdb_byte *bytes = &m_bytes[index * sizeof (offset_type)];
52 return (offset_type) extract_unsigned_integer (bytes,
53 sizeof (offset_type),
54 BFD_ENDIAN_LITTLE);
55 }
56
57 /* Return the number of offset_types in this array. */
58 size_t size () const
59 {
60 return m_bytes.size () / sizeof (offset_type);
61 }
62
63 /* Return true if this view is empty. */
64 bool empty () const
65 {
66 return m_bytes.empty ();
67 }
68
69 private:
70 /* The underlying bytes. */
71 gdb::array_view<const gdb_byte> m_bytes;
72 };
73
74 /* A description of .gdb_index index. The file format is described in
75 a comment by the code that writes the index. */
76
77 struct mapped_gdb_index final : public mapped_index_base
78 {
79 /* Index data format version. */
80 int version = 0;
81
82 /* The address table data. */
83 gdb::array_view<const gdb_byte> address_table;
84
85 /* The symbol table, implemented as a hash table. */
86 offset_view symbol_table;
87
88 /* A pointer to the constant pool. */
89 gdb::array_view<const gdb_byte> constant_pool;
90
91 /* Return the index into the constant pool of the name of the IDXth
92 symbol in the symbol table. */
93 offset_type symbol_name_index (offset_type idx) const
94 {
95 return symbol_table[2 * idx];
96 }
97
98 /* Return the index into the constant pool of the CU vector of the
99 IDXth symbol in the symbol table. */
100 offset_type symbol_vec_index (offset_type idx) const
101 {
102 return symbol_table[2 * idx + 1];
103 }
104
105 bool symbol_name_slot_invalid (offset_type idx) const override
106 {
107 return (symbol_name_index (idx) == 0
108 && symbol_vec_index (idx) == 0);
109 }
110
111 /* Convenience method to get at the name of the symbol at IDX in the
112 symbol table. */
113 const char *symbol_name_at
114 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
115 {
116 return (const char *) (this->constant_pool.data ()
117 + symbol_name_index (idx));
118 }
119
120 size_t symbol_name_count () const override
121 { return this->symbol_table.size () / 2; }
122
123 quick_symbol_functions_up make_quick_functions () const override;
124
125 bool version_check () const override
126 {
127 return version >= 8;
128 }
129 };
130
131 struct dwarf2_gdb_index : public dwarf2_base_index_functions
132 {
133 /* This dumps minimal information about the index.
134 It is called via "mt print objfiles".
135 One use is to verify .gdb_index has been loaded by the
136 gdb.dwarf2/gdb-index.exp testcase. */
137 void dump (struct objfile *objfile) override;
138
139 void expand_matching_symbols
140 (struct objfile *,
141 const lookup_name_info &lookup_name,
142 domain_enum domain,
143 int global,
144 symbol_compare_ftype *ordered_compare) override;
145
146 bool expand_symtabs_matching
147 (struct objfile *objfile,
148 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
149 const lookup_name_info *lookup_name,
150 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
151 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
152 block_search_flags search_flags,
153 domain_enum domain,
154 enum search_domain kind) override;
155 };
156
157 /* This dumps minimal information about the index.
158 It is called via "mt print objfiles".
159 One use is to verify .gdb_index has been loaded by the
160 gdb.dwarf2/gdb-index.exp testcase. */
161
162 void
163 dwarf2_gdb_index::dump (struct objfile *objfile)
164 {
165 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
166
167 mapped_gdb_index *index = (gdb::checked_static_cast<mapped_gdb_index *>
168 (per_objfile->per_bfd->index_table.get ()));
169 gdb_printf (".gdb_index: version %d\n", index->version);
170 gdb_printf ("\n");
171 }
172
173 /* Struct used to manage iterating over all CUs looking for a symbol. */
174
175 struct dw2_symtab_iterator
176 {
177 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
178 dwarf2_per_objfile *per_objfile;
179 /* If set, only look for symbols that match that block. Valid values are
180 GLOBAL_BLOCK and STATIC_BLOCK. */
181 gdb::optional<block_enum> block_index;
182 /* The kind of symbol we're looking for. */
183 domain_enum domain;
184 /* The list of CUs from the index entry of the symbol,
185 or NULL if not found. */
186 offset_view vec;
187 /* The next element in VEC to look at. */
188 int next;
189 /* The number of elements in VEC, or zero if there is no match. */
190 int length;
191 /* Have we seen a global version of the symbol?
192 If so we can ignore all further global instances.
193 This is to work around gold/15646, inefficient gold-generated
194 indices. */
195 int global_seen;
196 };
197
198 /* Initialize the index symtab iterator ITER, offset_type NAMEI variant. */
199
200 static void
201 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
202 dwarf2_per_objfile *per_objfile,
203 gdb::optional<block_enum> block_index,
204 domain_enum domain, offset_type namei,
205 mapped_gdb_index &index)
206 {
207 iter->per_objfile = per_objfile;
208 iter->block_index = block_index;
209 iter->domain = domain;
210 iter->next = 0;
211 iter->global_seen = 0;
212 iter->vec = {};
213 iter->length = 0;
214
215 gdb_assert (!index.symbol_name_slot_invalid (namei));
216 offset_type vec_idx = index.symbol_vec_index (namei);
217
218 iter->vec = offset_view (index.constant_pool.slice (vec_idx));
219 iter->length = iter->vec[0];
220 }
221
222 /* Return the next matching CU or NULL if there are no more. */
223
224 static struct dwarf2_per_cu_data *
225 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter,
226 mapped_gdb_index &index)
227 {
228 dwarf2_per_objfile *per_objfile = iter->per_objfile;
229
230 for ( ; iter->next < iter->length; ++iter->next)
231 {
232 offset_type cu_index_and_attrs = iter->vec[iter->next + 1];
233 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
234 gdb_index_symbol_kind symbol_kind =
235 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
236 /* Only check the symbol attributes if they're present.
237 Indices prior to version 7 don't record them,
238 and indices >= 7 may elide them for certain symbols
239 (gold does this). */
240 int attrs_valid = (index.version >= 7
241 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
242
243 /* Don't crash on bad data. */
244 if (cu_index >= per_objfile->per_bfd->all_units.size ())
245 {
246 complaint (_(".gdb_index entry has bad CU index"
247 " [in module %s]"), objfile_name (per_objfile->objfile));
248 continue;
249 }
250
251 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (cu_index);
252
253 /* Skip if already read in. */
254 if (per_objfile->symtab_set_p (per_cu))
255 continue;
256
257 /* Check static vs global. */
258 if (attrs_valid)
259 {
260 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
261
262 if (iter->block_index.has_value ())
263 {
264 bool want_static = *iter->block_index == STATIC_BLOCK;
265
266 if (is_static != want_static)
267 continue;
268 }
269
270 /* Work around gold/15646. */
271 if (!is_static
272 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
273 {
274 if (iter->global_seen)
275 continue;
276
277 iter->global_seen = 1;
278 }
279 }
280
281 /* Only check the symbol's kind if it has one. */
282 if (attrs_valid)
283 {
284 switch (iter->domain)
285 {
286 case VAR_DOMAIN:
287 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
288 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
289 /* Some types are also in VAR_DOMAIN. */
290 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
291 continue;
292 break;
293 case STRUCT_DOMAIN:
294 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
295 continue;
296 break;
297 case LABEL_DOMAIN:
298 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
299 continue;
300 break;
301 case MODULE_DOMAIN:
302 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
303 continue;
304 break;
305 default:
306 break;
307 }
308 }
309
310 ++iter->next;
311 return per_cu;
312 }
313
314 return NULL;
315 }
316
317 void
318 dwarf2_gdb_index::expand_matching_symbols
319 (struct objfile *objfile,
320 const lookup_name_info &name, domain_enum domain,
321 int global,
322 symbol_compare_ftype *ordered_compare)
323 {
324 /* Used for Ada. */
325 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
326
327 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
328
329 mapped_gdb_index &index
330 = *(gdb::checked_static_cast<mapped_gdb_index *>
331 (per_objfile->per_bfd->index_table.get ()));
332
333 const char *match_name = name.ada ().lookup_name ().c_str ();
334 auto matcher = [&] (const char *symname)
335 {
336 if (ordered_compare == nullptr)
337 return true;
338 return ordered_compare (symname, match_name) == 0;
339 };
340
341 dw2_expand_symtabs_matching_symbol (index, name, matcher,
342 [&] (offset_type namei)
343 {
344 struct dw2_symtab_iterator iter;
345 struct dwarf2_per_cu_data *per_cu;
346
347 dw2_symtab_iter_init (&iter, per_objfile, block_kind, domain, namei,
348 index);
349 while ((per_cu = dw2_symtab_iter_next (&iter, index)) != NULL)
350 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
351 nullptr);
352 return true;
353 }, per_objfile);
354 }
355
356 /* Helper for dw2_expand_matching symtabs. Called on each symbol
357 matched, to expand corresponding CUs that were marked. IDX is the
358 index of the symbol name that matched. */
359
360 static bool
361 dw2_expand_marked_cus
362 (dwarf2_per_objfile *per_objfile, offset_type idx,
363 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
364 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
365 block_search_flags search_flags,
366 search_domain kind)
367 {
368 offset_type vec_len, vec_idx;
369 bool global_seen = false;
370 mapped_gdb_index &index
371 = *(gdb::checked_static_cast<mapped_gdb_index *>
372 (per_objfile->per_bfd->index_table.get ()));
373
374 offset_view vec (index.constant_pool.slice (index.symbol_vec_index (idx)));
375 vec_len = vec[0];
376 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
377 {
378 offset_type cu_index_and_attrs = vec[vec_idx + 1];
379 /* This value is only valid for index versions >= 7. */
380 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
381 gdb_index_symbol_kind symbol_kind =
382 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
383 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
384 /* Only check the symbol attributes if they're present.
385 Indices prior to version 7 don't record them,
386 and indices >= 7 may elide them for certain symbols
387 (gold does this). */
388 int attrs_valid =
389 (index.version >= 7
390 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
391
392 /* Work around gold/15646. */
393 if (attrs_valid
394 && !is_static
395 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
396 {
397 if (global_seen)
398 continue;
399
400 global_seen = true;
401 }
402
403 /* Only check the symbol's kind if it has one. */
404 if (attrs_valid)
405 {
406 if (is_static)
407 {
408 if ((search_flags & SEARCH_STATIC_BLOCK) == 0)
409 continue;
410 }
411 else
412 {
413 if ((search_flags & SEARCH_GLOBAL_BLOCK) == 0)
414 continue;
415 }
416
417 switch (kind)
418 {
419 case VARIABLES_DOMAIN:
420 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
421 continue;
422 break;
423 case FUNCTIONS_DOMAIN:
424 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
425 continue;
426 break;
427 case TYPES_DOMAIN:
428 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
429 continue;
430 break;
431 case MODULES_DOMAIN:
432 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
433 continue;
434 break;
435 default:
436 break;
437 }
438 }
439
440 /* Don't crash on bad data. */
441 if (cu_index >= per_objfile->per_bfd->all_units.size ())
442 {
443 complaint (_(".gdb_index entry has bad CU index"
444 " [in module %s]"), objfile_name (per_objfile->objfile));
445 continue;
446 }
447
448 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (cu_index);
449 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
450 expansion_notify))
451 return false;
452 }
453
454 return true;
455 }
456
457 bool
458 dwarf2_gdb_index::expand_symtabs_matching
459 (struct objfile *objfile,
460 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
461 const lookup_name_info *lookup_name,
462 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
463 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
464 block_search_flags search_flags,
465 domain_enum domain,
466 enum search_domain kind)
467 {
468 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
469
470 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
471
472 /* This invariant is documented in quick-functions.h. */
473 gdb_assert (lookup_name != nullptr || symbol_matcher == nullptr);
474 if (lookup_name == nullptr)
475 {
476 for (dwarf2_per_cu_data *per_cu
477 : all_units_range (per_objfile->per_bfd))
478 {
479 QUIT;
480
481 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile,
482 file_matcher,
483 expansion_notify))
484 return false;
485 }
486 return true;
487 }
488
489 mapped_gdb_index &index
490 = *(gdb::checked_static_cast<mapped_gdb_index *>
491 (per_objfile->per_bfd->index_table.get ()));
492
493 bool result
494 = dw2_expand_symtabs_matching_symbol (index, *lookup_name,
495 symbol_matcher,
496 [&] (offset_type idx)
497 {
498 if (!dw2_expand_marked_cus (per_objfile, idx, file_matcher,
499 expansion_notify, search_flags, kind))
500 return false;
501 return true;
502 }, per_objfile);
503
504 return result;
505 }
506
507 quick_symbol_functions_up
508 mapped_gdb_index::make_quick_functions () const
509 {
510 return quick_symbol_functions_up (new dwarf2_gdb_index);
511 }
512
513 /* A helper function that reads the .gdb_index from BUFFER and fills
514 in MAP. FILENAME is the name of the file containing the data;
515 it is used for error reporting. DEPRECATED_OK is true if it is
516 ok to use deprecated sections.
517
518 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
519 out parameters that are filled in with information about the CU and
520 TU lists in the section.
521
522 Returns true if all went well, false otherwise. */
523
524 static bool
525 read_gdb_index_from_buffer (const char *filename,
526 bool deprecated_ok,
527 gdb::array_view<const gdb_byte> buffer,
528 mapped_gdb_index *map,
529 const gdb_byte **cu_list,
530 offset_type *cu_list_elements,
531 const gdb_byte **types_list,
532 offset_type *types_list_elements)
533 {
534 const gdb_byte *addr = &buffer[0];
535 offset_view metadata (buffer);
536
537 /* Version check. */
538 offset_type version = metadata[0];
539 /* Versions earlier than 3 emitted every copy of a psymbol. This
540 causes the index to behave very poorly for certain requests. Version 3
541 contained incomplete addrmap. So, it seems better to just ignore such
542 indices. */
543 if (version < 4)
544 {
545 static int warning_printed = 0;
546 if (!warning_printed)
547 {
548 warning (_("Skipping obsolete .gdb_index section in %s."),
549 filename);
550 warning_printed = 1;
551 }
552 return 0;
553 }
554 /* Index version 4 uses a different hash function than index version
555 5 and later.
556
557 Versions earlier than 6 did not emit psymbols for inlined
558 functions. Using these files will cause GDB not to be able to
559 set breakpoints on inlined functions by name, so we ignore these
560 indices unless the user has done
561 "set use-deprecated-index-sections on". */
562 if (version < 6 && !deprecated_ok)
563 {
564 static int warning_printed = 0;
565 if (!warning_printed)
566 {
567 warning (_("\
568 Skipping deprecated .gdb_index section in %s.\n\
569 Do \"set use-deprecated-index-sections on\" before the file is read\n\
570 to use the section anyway."),
571 filename);
572 warning_printed = 1;
573 }
574 return 0;
575 }
576 /* Version 7 indices generated by gold refer to the CU for a symbol instead
577 of the TU (for symbols coming from TUs),
578 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
579 Plus gold-generated indices can have duplicate entries for global symbols,
580 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
581 These are just performance bugs, and we can't distinguish gdb-generated
582 indices from gold-generated ones, so issue no warning here. */
583
584 /* Indexes with higher version than the one supported by GDB may be no
585 longer backward compatible. */
586 if (version > 8)
587 return 0;
588
589 map->version = version;
590
591 int i = 1;
592 *cu_list = addr + metadata[i];
593 *cu_list_elements = (metadata[i + 1] - metadata[i]) / 8;
594 ++i;
595
596 *types_list = addr + metadata[i];
597 *types_list_elements = (metadata[i + 1] - metadata[i]) / 8;
598 ++i;
599
600 const gdb_byte *address_table = addr + metadata[i];
601 const gdb_byte *address_table_end = addr + metadata[i + 1];
602 map->address_table
603 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
604 ++i;
605
606 const gdb_byte *symbol_table = addr + metadata[i];
607 const gdb_byte *symbol_table_end = addr + metadata[i + 1];
608 map->symbol_table
609 = offset_view (gdb::array_view<const gdb_byte> (symbol_table,
610 symbol_table_end));
611
612 ++i;
613 map->constant_pool = buffer.slice (metadata[i]);
614
615 if (map->constant_pool.empty () && !map->symbol_table.empty ())
616 {
617 /* An empty constant pool implies that all symbol table entries are
618 empty. Make map->symbol_table.empty () == true. */
619 map->symbol_table
620 = offset_view (gdb::array_view<const gdb_byte> (symbol_table,
621 symbol_table));
622 }
623
624 return 1;
625 }
626
627 /* A helper for create_cus_from_gdb_index that handles a given list of
628 CUs. */
629
630 static void
631 create_cus_from_gdb_index_list (dwarf2_per_bfd *per_bfd,
632 const gdb_byte *cu_list, offset_type n_elements,
633 struct dwarf2_section_info *section,
634 int is_dwz)
635 {
636 for (offset_type i = 0; i < n_elements; i += 2)
637 {
638 gdb_static_assert (sizeof (ULONGEST) >= 8);
639
640 sect_offset sect_off
641 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
642 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
643 cu_list += 2 * 8;
644
645 dwarf2_per_cu_data_up per_cu
646 = create_cu_from_index_list (per_bfd, section, is_dwz, sect_off,
647 length);
648 per_bfd->all_units.push_back (std::move (per_cu));
649 }
650 }
651
652 /* Read the CU list from the mapped index, and use it to create all
653 the CU objects for PER_BFD. */
654
655 static void
656 create_cus_from_gdb_index (dwarf2_per_bfd *per_bfd,
657 const gdb_byte *cu_list, offset_type cu_list_elements,
658 const gdb_byte *dwz_list, offset_type dwz_elements)
659 {
660 gdb_assert (per_bfd->all_units.empty ());
661 per_bfd->all_units.reserve ((cu_list_elements + dwz_elements) / 2);
662
663 create_cus_from_gdb_index_list (per_bfd, cu_list, cu_list_elements,
664 &per_bfd->info, 0);
665
666 if (dwz_elements == 0)
667 return;
668
669 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
670 create_cus_from_gdb_index_list (per_bfd, dwz_list, dwz_elements,
671 &dwz->info, 1);
672 }
673
674 /* Create the signatured type hash table from the index. */
675
676 static void
677 create_signatured_type_table_from_gdb_index
678 (dwarf2_per_bfd *per_bfd, struct dwarf2_section_info *section,
679 const gdb_byte *bytes, offset_type elements)
680 {
681 htab_up sig_types_hash = allocate_signatured_type_table ();
682
683 for (offset_type i = 0; i < elements; i += 3)
684 {
685 signatured_type_up sig_type;
686 ULONGEST signature;
687 void **slot;
688 cu_offset type_offset_in_tu;
689
690 gdb_static_assert (sizeof (ULONGEST) >= 8);
691 sect_offset sect_off
692 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
693 type_offset_in_tu
694 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
695 BFD_ENDIAN_LITTLE);
696 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
697 bytes += 3 * 8;
698
699 sig_type = per_bfd->allocate_signatured_type (signature);
700 sig_type->type_offset_in_tu = type_offset_in_tu;
701 sig_type->section = section;
702 sig_type->sect_off = sect_off;
703
704 slot = htab_find_slot (sig_types_hash.get (), sig_type.get (), INSERT);
705 *slot = sig_type.get ();
706
707 per_bfd->all_units.emplace_back (sig_type.release ());
708 }
709
710 per_bfd->signatured_types = std::move (sig_types_hash);
711 }
712
713 /* Read the address map data from the mapped GDB index, and use it to
714 populate the index_addrmap. */
715
716 static void
717 create_addrmap_from_gdb_index (dwarf2_per_objfile *per_objfile,
718 mapped_gdb_index *index)
719 {
720 struct objfile *objfile = per_objfile->objfile;
721 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
722 struct gdbarch *gdbarch = objfile->arch ();
723 const gdb_byte *iter, *end;
724 CORE_ADDR baseaddr;
725
726 addrmap_mutable mutable_map;
727
728 iter = index->address_table.data ();
729 end = iter + index->address_table.size ();
730
731 baseaddr = objfile->text_section_offset ();
732
733 while (iter < end)
734 {
735 ULONGEST hi, lo, cu_index;
736 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
737 iter += 8;
738 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
739 iter += 8;
740 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
741 iter += 4;
742
743 if (lo > hi)
744 {
745 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
746 hex_string (lo), hex_string (hi));
747 continue;
748 }
749
750 if (cu_index >= per_bfd->all_units.size ())
751 {
752 complaint (_(".gdb_index address table has invalid CU number %u"),
753 (unsigned) cu_index);
754 continue;
755 }
756
757 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
758 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
759 mutable_map.set_empty (lo, hi - 1, per_bfd->get_cu (cu_index));
760 }
761
762 per_bfd->index_addrmap
763 = new (&per_bfd->obstack) addrmap_fixed (&per_bfd->obstack, &mutable_map);
764 }
765
766 /* See read-gdb-index.h. */
767
768 int
769 dwarf2_read_gdb_index
770 (dwarf2_per_objfile *per_objfile,
771 get_gdb_index_contents_ftype get_gdb_index_contents,
772 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
773 {
774 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
775 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
776 struct dwz_file *dwz;
777 struct objfile *objfile = per_objfile->objfile;
778 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
779
780 gdb::array_view<const gdb_byte> main_index_contents
781 = get_gdb_index_contents (objfile, per_bfd);
782
783 if (main_index_contents.empty ())
784 return 0;
785
786 std::unique_ptr<mapped_gdb_index> map (new mapped_gdb_index);
787 if (!read_gdb_index_from_buffer (objfile_name (objfile),
788 use_deprecated_index_sections,
789 main_index_contents, map.get (), &cu_list,
790 &cu_list_elements, &types_list,
791 &types_list_elements))
792 return 0;
793
794 /* Don't use the index if it's empty. */
795 if (map->symbol_table.empty ())
796 return 0;
797
798 /* If there is a .dwz file, read it so we can get its CU list as
799 well. */
800 dwz = dwarf2_get_dwz_file (per_bfd);
801 if (dwz != NULL)
802 {
803 mapped_gdb_index dwz_map;
804 const gdb_byte *dwz_types_ignore;
805 offset_type dwz_types_elements_ignore;
806
807 gdb::array_view<const gdb_byte> dwz_index_content
808 = get_gdb_index_contents_dwz (objfile, dwz);
809
810 if (dwz_index_content.empty ())
811 return 0;
812
813 if (!read_gdb_index_from_buffer (bfd_get_filename (dwz->dwz_bfd.get ()),
814 1, dwz_index_content, &dwz_map,
815 &dwz_list, &dwz_list_elements,
816 &dwz_types_ignore,
817 &dwz_types_elements_ignore))
818 {
819 warning (_("could not read '.gdb_index' section from %s; skipping"),
820 bfd_get_filename (dwz->dwz_bfd.get ()));
821 return 0;
822 }
823 }
824
825 create_cus_from_gdb_index (per_bfd, cu_list, cu_list_elements, dwz_list,
826 dwz_list_elements);
827
828 if (types_list_elements)
829 {
830 /* We can only handle a single .debug_types when we have an
831 index. */
832 if (per_bfd->types.size () > 1)
833 {
834 per_bfd->all_units.clear ();
835 return 0;
836 }
837
838 dwarf2_section_info *section
839 = (per_bfd->types.size () == 1
840 ? &per_bfd->types[0]
841 : &per_bfd->info);
842
843 create_signatured_type_table_from_gdb_index (per_bfd, section, types_list,
844 types_list_elements);
845 }
846
847 finalize_all_units (per_bfd);
848
849 create_addrmap_from_gdb_index (per_objfile, map.get ());
850
851 per_bfd->index_table = std::move (map);
852 per_bfd->quick_file_names_table =
853 create_quick_file_names_table (per_bfd->all_units.size ());
854
855 return 1;
856 }
857
858 void _initialize_read_gdb_index ();
859
860 void
861 _initialize_read_gdb_index ()
862 {
863 add_setshow_boolean_cmd ("use-deprecated-index-sections",
864 no_class, &use_deprecated_index_sections, _("\
865 Set whether to use deprecated gdb_index sections."), _("\
866 Show whether to use deprecated gdb_index sections."), _("\
867 When enabled, deprecated .gdb_index sections are used anyway.\n\
868 Normally they are ignored either because of a missing feature or\n\
869 performance issue.\n\
870 Warning: This option must be enabled before gdb reads the file."),
871 NULL,
872 NULL,
873 &setlist, &showlist);
874 }