Automatic date update in version.in
[binutils-gdb.git] / gdb / dwarf2 / read-gdb-index.c
bloba8706d02c4c683a65cba4ad0d247914326929e00
1 /* Reading code for .gdb_index
3 Copyright (C) 2023-2024 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
20 #include "read-gdb-index.h"
22 #include "cli/cli-cmds.h"
23 #include "cli/cli-style.h"
24 #include "complaints.h"
25 #include "dwarf2/index-common.h"
26 #include "dwz.h"
27 #include "event-top.h"
28 #include "gdb/gdb-index.h"
29 #include "gdbsupport/gdb-checked-static-cast.h"
30 #include "mapped-index.h"
31 #include "read.h"
32 #include "extract-store-integer.h"
33 #include "cp-support.h"
34 #include "symtab.h"
35 #include "gdbsupport/selftest.h"
37 /* When true, do not reject deprecated .gdb_index sections. */
38 static bool use_deprecated_index_sections = false;
40 /* This is a view into the index that converts from bytes to an
41 offset_type, and allows indexing. Unaligned bytes are specifically
42 allowed here, and handled via unpacking. */
44 class offset_view
46 public:
47 offset_view () = default;
49 explicit offset_view (gdb::array_view<const gdb_byte> bytes)
50 : m_bytes (bytes)
54 /* Extract the INDEXth offset_type from the array. */
55 offset_type operator[] (size_t index) const
57 const gdb_byte *bytes = &m_bytes[index * sizeof (offset_type)];
58 return (offset_type) extract_unsigned_integer (bytes,
59 sizeof (offset_type),
60 BFD_ENDIAN_LITTLE);
63 /* Return the number of offset_types in this array. */
64 size_t size () const
66 return m_bytes.size () / sizeof (offset_type);
69 /* Return true if this view is empty. */
70 bool empty () const
72 return m_bytes.empty ();
75 private:
76 /* The underlying bytes. */
77 gdb::array_view<const gdb_byte> m_bytes;
80 /* An index into a (C++) symbol name component in a symbol name as
81 recorded in the mapped_index's symbol table. For each C++ symbol
82 in the symbol table, we record one entry for the start of each
83 component in the symbol in a table of name components, and then
84 sort the table, in order to be able to binary search symbol names,
85 ignoring leading namespaces, both completion and regular look up.
86 For example, for symbol "A::B::C", we'll have an entry that points
87 to "A::B::C", another that points to "B::C", and another for "C".
88 Note that function symbols in GDB index have no parameter
89 information, just the function/method names. You can convert a
90 name_component to a "const char *" using the
91 'mapped_index::symbol_name_at(offset_type)' method. */
93 struct name_component
95 /* Offset in the symbol name where the component starts. Stored as
96 a (32-bit) offset instead of a pointer to save memory and improve
97 locality on 64-bit architectures. */
98 offset_type name_offset;
100 /* The symbol's index in the symbol and constant pool tables of a
101 mapped_index. */
102 offset_type idx;
105 /* A description of .gdb_index index. The file format is described in
106 a comment by the code that writes the index. */
108 struct mapped_gdb_index : public dwarf_scanner_base
110 /* The name_component table (a sorted vector). See name_component's
111 description above. */
112 std::vector<name_component> name_components;
114 /* How NAME_COMPONENTS is sorted. */
115 enum case_sensitivity name_components_casing;
117 /* Index data format version. */
118 int version = 0;
120 /* The address table data. */
121 gdb::array_view<const gdb_byte> address_table;
123 /* The symbol table, implemented as a hash table. */
124 offset_view symbol_table;
126 /* A pointer to the constant pool. */
127 gdb::array_view<const gdb_byte> constant_pool;
129 /* The shortcut table data. */
130 gdb::array_view<const gdb_byte> shortcut_table;
132 /* An address map that maps from PC to dwarf2_per_cu_data. */
133 addrmap_fixed *index_addrmap = nullptr;
135 /* Return the index into the constant pool of the name of the IDXth
136 symbol in the symbol table. */
137 offset_type symbol_name_index (offset_type idx) const
139 return symbol_table[2 * idx];
142 /* Return the index into the constant pool of the CU vector of the
143 IDXth symbol in the symbol table. */
144 offset_type symbol_vec_index (offset_type idx) const
146 return symbol_table[2 * idx + 1];
149 /* Return whether the name at IDX in the symbol table should be
150 ignored. */
151 virtual bool symbol_name_slot_invalid (offset_type idx) const
153 return (symbol_name_index (idx) == 0
154 && symbol_vec_index (idx) == 0);
157 /* Convenience method to get at the name of the symbol at IDX in the
158 symbol table. */
159 virtual const char *symbol_name_at
160 (offset_type idx, dwarf2_per_objfile *per_objfile) const
162 return (const char *) (this->constant_pool.data ()
163 + symbol_name_index (idx));
166 virtual size_t symbol_name_count () const
167 { return this->symbol_table.size () / 2; }
169 /* Build the symbol name component sorted vector, if we haven't
170 yet. */
171 void build_name_components (dwarf2_per_objfile *per_objfile);
173 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
174 possible matches for LN_NO_PARAMS in the name component
175 vector. */
176 std::pair<std::vector<name_component>::const_iterator,
177 std::vector<name_component>::const_iterator>
178 find_name_components_bounds (const lookup_name_info &ln_no_params,
179 enum language lang,
180 dwarf2_per_objfile *per_objfile) const;
182 quick_symbol_functions_up make_quick_functions () const override;
184 bool version_check () const override
186 return version >= 8;
189 dwarf2_per_cu_data *lookup (unrelocated_addr addr) override
191 if (index_addrmap == nullptr)
192 return nullptr;
194 void *obj = index_addrmap->find (static_cast<CORE_ADDR> (addr));
195 return static_cast<dwarf2_per_cu_data *> (obj);
198 cooked_index *index_for_writing () override
199 { return nullptr; }
203 /* Starting from a search name, return the string that finds the upper
204 bound of all strings that start with SEARCH_NAME in a sorted name
205 list. Returns the empty string to indicate that the upper bound is
206 the end of the list. */
208 static std::string
209 make_sort_after_prefix_name (const char *search_name)
211 /* When looking to complete "func", we find the upper bound of all
212 symbols that start with "func" by looking for where we'd insert
213 the closest string that would follow "func" in lexicographical
214 order. Usually, that's "func"-with-last-character-incremented,
215 i.e. "fund". Mind non-ASCII characters, though. Usually those
216 will be UTF-8 multi-byte sequences, but we can't be certain.
217 Especially mind the 0xff character, which is a valid character in
218 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
219 rule out compilers allowing it in identifiers. Note that
220 conveniently, strcmp/strcasecmp are specified to compare
221 characters interpreted as unsigned char. So what we do is treat
222 the whole string as a base 256 number composed of a sequence of
223 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
224 to 0, and carries 1 to the following more-significant position.
225 If the very first character in SEARCH_NAME ends up incremented
226 and carries/overflows, then the upper bound is the end of the
227 list. The string after the empty string is also the empty
228 string.
230 Some examples of this operation:
232 SEARCH_NAME => "+1" RESULT
234 "abc" => "abd"
235 "ab\xff" => "ac"
236 "\xff" "a" "\xff" => "\xff" "b"
237 "\xff" => ""
238 "\xff\xff" => ""
239 "" => ""
241 Then, with these symbols for example:
243 func
244 func1
245 fund
247 completing "func" looks for symbols between "func" and
248 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
249 which finds "func" and "func1", but not "fund".
251 And with:
253 funcÿ (Latin1 'ÿ' [0xff])
254 funcÿ1
255 fund
257 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
258 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
260 And with:
262 ÿÿ (Latin1 'ÿ' [0xff])
263 ÿÿ1
265 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
266 the end of the list.
268 std::string after = search_name;
269 while (!after.empty () && (unsigned char) after.back () == 0xff)
270 after.pop_back ();
271 if (!after.empty ())
272 after.back () = (unsigned char) after.back () + 1;
273 return after;
276 /* See declaration. */
278 std::pair<std::vector<name_component>::const_iterator,
279 std::vector<name_component>::const_iterator>
280 mapped_gdb_index::find_name_components_bounds
281 (const lookup_name_info &lookup_name_without_params, language lang,
282 dwarf2_per_objfile *per_objfile) const
284 auto *name_cmp
285 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
287 const char *lang_name
288 = lookup_name_without_params.language_lookup_name (lang);
290 /* Comparison function object for lower_bound that matches against a
291 given symbol name. */
292 auto lookup_compare_lower = [&] (const name_component &elem,
293 const char *name)
295 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
296 const char *elem_name = elem_qualified + elem.name_offset;
297 return name_cmp (elem_name, name) < 0;
300 /* Comparison function object for upper_bound that matches against a
301 given symbol name. */
302 auto lookup_compare_upper = [&] (const char *name,
303 const name_component &elem)
305 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
306 const char *elem_name = elem_qualified + elem.name_offset;
307 return name_cmp (name, elem_name) < 0;
310 auto begin = this->name_components.begin ();
311 auto end = this->name_components.end ();
313 /* Find the lower bound. */
314 auto lower = [&] ()
316 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
317 return begin;
318 else
319 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
320 } ();
322 /* Find the upper bound. */
323 auto upper = [&] ()
325 if (lookup_name_without_params.completion_mode ())
327 /* In completion mode, we want UPPER to point past all
328 symbols names that have the same prefix. I.e., with
329 these symbols, and completing "func":
331 function << lower bound
332 function1
333 other_function << upper bound
335 We find the upper bound by looking for the insertion
336 point of "func"-with-last-character-incremented,
337 i.e. "fund". */
338 std::string after = make_sort_after_prefix_name (lang_name);
339 if (after.empty ())
340 return end;
341 return std::lower_bound (lower, end, after.c_str (),
342 lookup_compare_lower);
344 else
345 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
346 } ();
348 return {lower, upper};
351 /* See declaration. */
353 void
354 mapped_gdb_index::build_name_components (dwarf2_per_objfile *per_objfile)
356 if (!this->name_components.empty ())
357 return;
359 this->name_components_casing = case_sensitivity;
360 auto *name_cmp
361 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
363 /* The code below only knows how to break apart components of C++
364 symbol names (and other languages that use '::' as
365 namespace/module separator) and Ada symbol names. */
366 auto count = this->symbol_name_count ();
367 for (offset_type idx = 0; idx < count; idx++)
369 if (this->symbol_name_slot_invalid (idx))
370 continue;
372 const char *name = this->symbol_name_at (idx, per_objfile);
374 /* Add each name component to the name component table. */
375 unsigned int previous_len = 0;
377 if (strstr (name, "::") != nullptr)
379 for (unsigned int current_len = cp_find_first_component (name);
380 name[current_len] != '\0';
381 current_len += cp_find_first_component (name + current_len))
383 gdb_assert (name[current_len] == ':');
384 this->name_components.push_back ({previous_len, idx});
385 /* Skip the '::'. */
386 current_len += 2;
387 previous_len = current_len;
390 else
392 /* Handle the Ada encoded (aka mangled) form here. */
393 for (const char *iter = strstr (name, "__");
394 iter != nullptr;
395 iter = strstr (iter, "__"))
397 this->name_components.push_back ({previous_len, idx});
398 iter += 2;
399 previous_len = iter - name;
403 this->name_components.push_back ({previous_len, idx});
406 /* Sort name_components elements by name. */
407 auto name_comp_compare = [&] (const name_component &left,
408 const name_component &right)
410 const char *left_qualified
411 = this->symbol_name_at (left.idx, per_objfile);
412 const char *right_qualified
413 = this->symbol_name_at (right.idx, per_objfile);
415 const char *left_name = left_qualified + left.name_offset;
416 const char *right_name = right_qualified + right.name_offset;
418 return name_cmp (left_name, right_name) < 0;
421 std::sort (this->name_components.begin (),
422 this->name_components.end (),
423 name_comp_compare);
426 /* Helper for dw2_expand_symtabs_matching that works with a
427 mapped_index_base instead of the containing objfile. This is split
428 to a separate function in order to be able to unit test the
429 name_components matching using a mock mapped_index_base. For each
430 symbol name that matches, calls MATCH_CALLBACK, passing it the
431 symbol's index in the mapped_index_base symbol table. */
433 static bool
434 dw2_expand_symtabs_matching_symbol
435 (mapped_gdb_index &index,
436 const lookup_name_info &lookup_name_in,
437 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
438 gdb::function_view<bool (offset_type)> match_callback,
439 dwarf2_per_objfile *per_objfile,
440 gdb::function_view<expand_symtabs_lang_matcher_ftype> lang_matcher)
442 lookup_name_info lookup_name_without_params
443 = lookup_name_in.make_ignore_params ();
445 /* Build the symbol name component sorted vector, if we haven't
446 yet. */
447 index.build_name_components (per_objfile);
449 /* The same symbol may appear more than once in the range though.
450 E.g., if we're looking for symbols that complete "w", and we have
451 a symbol named "w1::w2", we'll find the two name components for
452 that same symbol in the range. To be sure we only call the
453 callback once per symbol, we first collect the symbol name
454 indexes that matched in a temporary vector and ignore
455 duplicates. */
456 std::vector<offset_type> matches;
458 struct name_and_matcher
460 symbol_name_matcher_ftype *matcher;
461 const char *name;
463 bool operator== (const name_and_matcher &other) const
465 return matcher == other.matcher && strcmp (name, other.name) == 0;
469 /* A vector holding all the different symbol name matchers, for all
470 languages. */
471 std::vector<name_and_matcher> matchers;
473 for (int i = 0; i < nr_languages; i++)
475 enum language lang_e = (enum language) i;
476 if (lang_matcher != nullptr && !lang_matcher (lang_e))
477 continue;
479 const language_defn *lang = language_def (lang_e);
480 symbol_name_matcher_ftype *name_matcher
481 = lang->get_symbol_name_matcher (lookup_name_without_params);
483 name_and_matcher key {
484 name_matcher,
485 lookup_name_without_params.language_lookup_name (lang_e)
488 /* Don't insert the same comparison routine more than once.
489 Note that we do this linear walk. This is not a problem in
490 practice because the number of supported languages is
491 low. */
492 if (std::find (matchers.begin (), matchers.end (), key)
493 != matchers.end ())
494 continue;
495 matchers.push_back (std::move (key));
497 auto bounds
498 = index.find_name_components_bounds (lookup_name_without_params,
499 lang_e, per_objfile);
501 /* Now for each symbol name in range, check to see if we have a name
502 match, and if so, call the MATCH_CALLBACK callback. */
504 for (; bounds.first != bounds.second; ++bounds.first)
506 const char *qualified
507 = index.symbol_name_at (bounds.first->idx, per_objfile);
509 if (!name_matcher (qualified, lookup_name_without_params, NULL)
510 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
511 continue;
513 matches.push_back (bounds.first->idx);
517 std::sort (matches.begin (), matches.end ());
519 /* Finally call the callback, once per match. */
520 ULONGEST prev = -1;
521 bool result = true;
522 for (offset_type idx : matches)
524 if (prev != idx)
526 if (!match_callback (idx))
528 result = false;
529 break;
531 prev = idx;
535 /* Above we use a type wider than idx's for 'prev', since 0 and
536 (offset_type)-1 are both possible values. */
537 static_assert (sizeof (prev) > sizeof (offset_type), "");
539 return result;
542 #if GDB_SELF_TEST
544 namespace selftests { namespace dw2_expand_symtabs_matching {
546 /* A mock .gdb_index/.debug_names-like name index table, enough to
547 exercise dw2_expand_symtabs_matching_symbol, which works with the
548 mapped_index_base interface. Builds an index from the symbol list
549 passed as parameter to the constructor. */
550 class mock_mapped_index : public mapped_gdb_index
552 public:
553 mock_mapped_index (gdb::array_view<const char *> symbols)
554 : m_symbol_table (symbols)
557 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
559 bool symbol_name_slot_invalid (offset_type idx) const override
560 { return false; }
562 /* Return the number of names in the symbol table. */
563 size_t symbol_name_count () const override
565 return m_symbol_table.size ();
568 /* Get the name of the symbol at IDX in the symbol table. */
569 const char *symbol_name_at
570 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
572 return m_symbol_table[idx];
575 quick_symbol_functions_up make_quick_functions () const override
577 return nullptr;
580 private:
581 gdb::array_view<const char *> m_symbol_table;
584 /* Convenience function that converts a NULL pointer to a "<null>"
585 string, to pass to print routines. */
587 static const char *
588 string_or_null (const char *str)
590 return str != NULL ? str : "<null>";
593 /* Check if a lookup_name_info built from
594 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
595 index. EXPECTED_LIST is the list of expected matches, in expected
596 matching order. If no match expected, then an empty list is
597 specified. Returns true on success. On failure prints a warning
598 indicating the file:line that failed, and returns false. */
600 static bool
601 check_match (const char *file, int line,
602 mock_mapped_index &mock_index,
603 const char *name, symbol_name_match_type match_type,
604 bool completion_mode,
605 std::initializer_list<const char *> expected_list,
606 dwarf2_per_objfile *per_objfile)
608 lookup_name_info lookup_name (name, match_type, completion_mode);
610 bool matched = true;
612 auto mismatch = [&] (const char *expected_str,
613 const char *got)
615 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
616 "expected=\"%s\", got=\"%s\"\n"),
617 file, line,
618 (match_type == symbol_name_match_type::FULL
619 ? "FULL" : "WILD"),
620 name, string_or_null (expected_str), string_or_null (got));
621 matched = false;
624 auto expected_it = expected_list.begin ();
625 auto expected_end = expected_list.end ();
627 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
628 nullptr,
629 [&] (offset_type idx)
631 const char *matched_name = mock_index.symbol_name_at (idx, per_objfile);
632 const char *expected_str
633 = expected_it == expected_end ? NULL : *expected_it++;
635 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
636 mismatch (expected_str, matched_name);
637 return true;
638 }, per_objfile, nullptr);
640 const char *expected_str
641 = expected_it == expected_end ? NULL : *expected_it++;
642 if (expected_str != NULL)
643 mismatch (expected_str, NULL);
645 return matched;
648 /* The symbols added to the mock mapped_index for testing (in
649 canonical form). */
650 static const char *test_symbols[] = {
651 "function",
652 "std::bar",
653 "std::zfunction",
654 "std::zfunction2",
655 "w1::w2",
656 "ns::foo<char*>",
657 "ns::foo<int>",
658 "ns::foo<long>",
659 "ns2::tmpl<int>::foo2",
660 "(anonymous namespace)::A::B::C",
662 /* These are used to check that the increment-last-char in the
663 matching algorithm for completion doesn't match "t1_fund" when
664 completing "t1_func". */
665 "t1_func",
666 "t1_func1",
667 "t1_fund",
668 "t1_fund1",
670 /* A UTF-8 name with multi-byte sequences to make sure that
671 cp-name-parser understands this as a single identifier ("função"
672 is "function" in PT). */
673 (const char *)u8"u8função",
675 /* Test a symbol name that ends with a 0xff character, which is a
676 valid character in non-UTF-8 source character sets (e.g. Latin1
677 'ÿ'), and we can't rule out compilers allowing it in identifiers.
678 We test this because the completion algorithm finds the upper
679 bound of symbols by looking for the insertion point of
680 "func"-with-last-character-incremented, i.e. "fund", and adding 1
681 to 0xff should wraparound and carry to the previous character.
682 See comments in make_sort_after_prefix_name. */
683 "yfunc\377",
685 /* Some more symbols with \377 (0xff). See above. */
686 "\377",
687 "\377\377123",
689 /* A name with all sorts of complications. Starts with "z" to make
690 it easier for the completion tests below. */
691 #define Z_SYM_NAME \
692 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
693 "::tuple<(anonymous namespace)::ui*, " \
694 "std::default_delete<(anonymous namespace)::ui>, void>"
696 Z_SYM_NAME
699 /* Returns true if the mapped_index_base::find_name_component_bounds
700 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
701 in completion mode. */
703 static bool
704 check_find_bounds_finds (mapped_gdb_index &index,
705 const char *search_name,
706 gdb::array_view<const char *> expected_syms,
707 dwarf2_per_objfile *per_objfile)
709 lookup_name_info lookup_name (search_name,
710 symbol_name_match_type::FULL, true);
712 auto bounds = index.find_name_components_bounds (lookup_name,
713 language_cplus,
714 per_objfile);
716 size_t distance = std::distance (bounds.first, bounds.second);
717 if (distance != expected_syms.size ())
718 return false;
720 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
722 auto nc_elem = bounds.first + exp_elem;
723 const char *qualified = index.symbol_name_at (nc_elem->idx, per_objfile);
724 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
725 return false;
728 return true;
731 /* Test the lower-level mapped_index::find_name_component_bounds
732 method. */
734 static void
735 test_mapped_index_find_name_component_bounds ()
737 mock_mapped_index mock_index (test_symbols);
739 mock_index.build_name_components (NULL /* per_objfile */);
741 /* Test the lower-level mapped_index::find_name_component_bounds
742 method in completion mode. */
744 static const char *expected_syms[] = {
745 "t1_func",
746 "t1_func1",
749 SELF_CHECK (check_find_bounds_finds
750 (mock_index, "t1_func", expected_syms,
751 NULL /* per_objfile */));
754 /* Check that the increment-last-char in the name matching algorithm
755 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. See
756 make_sort_after_prefix_name. */
758 static const char *expected_syms1[] = {
759 "\377",
760 "\377\377123",
762 SELF_CHECK (check_find_bounds_finds
763 (mock_index, "\377", expected_syms1, NULL /* per_objfile */));
765 static const char *expected_syms2[] = {
766 "\377\377123",
768 SELF_CHECK (check_find_bounds_finds
769 (mock_index, "\377\377", expected_syms2,
770 NULL /* per_objfile */));
774 /* Test dw2_expand_symtabs_matching_symbol. */
776 static void
777 test_dw2_expand_symtabs_matching_symbol ()
779 mock_mapped_index mock_index (test_symbols);
781 /* We let all tests run until the end even if some fails, for debug
782 convenience. */
783 bool any_mismatch = false;
785 /* Create the expected symbols list (an initializer_list). Needed
786 because lists have commas, and we need to pass them to CHECK,
787 which is a macro. */
788 #define EXPECT(...) { __VA_ARGS__ }
790 /* Wrapper for check_match that passes down the current
791 __FILE__/__LINE__. */
792 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
793 any_mismatch |= !check_match (__FILE__, __LINE__, \
794 mock_index, \
795 NAME, MATCH_TYPE, COMPLETION_MODE, \
796 EXPECTED_LIST, NULL)
798 /* Identity checks. */
799 for (const char *sym : test_symbols)
801 /* Should be able to match all existing symbols. */
802 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
803 EXPECT (sym));
805 /* Should be able to match all existing symbols with
806 parameters. */
807 std::string with_params = std::string (sym) + "(int)";
808 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
809 EXPECT (sym));
811 /* Should be able to match all existing symbols with
812 parameters and qualifiers. */
813 with_params = std::string (sym) + " ( int ) const";
814 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
815 EXPECT (sym));
817 /* This should really find sym, but cp-name-parser.y doesn't
818 know about lvalue/rvalue qualifiers yet. */
819 with_params = std::string (sym) + " ( int ) &&";
820 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
821 {});
824 /* Check that the name matching algorithm for completion doesn't get
825 confused with Latin1 'ÿ' / 0xff. See
826 make_sort_after_prefix_name. */
828 static const char str[] = "\377";
829 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
830 EXPECT ("\377", "\377\377123"));
833 /* Check that the increment-last-char in the matching algorithm for
834 completion doesn't match "t1_fund" when completing "t1_func". */
836 static const char str[] = "t1_func";
837 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
838 EXPECT ("t1_func", "t1_func1"));
841 /* Check that completion mode works at each prefix of the expected
842 symbol name. */
844 static const char str[] = "function(int)";
845 size_t len = strlen (str);
846 std::string lookup;
848 for (size_t i = 1; i < len; i++)
850 lookup.assign (str, i);
851 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
852 EXPECT ("function"));
856 /* While "w" is a prefix of both components, the match function
857 should still only be called once. */
859 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
860 EXPECT ("w1::w2"));
861 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
862 EXPECT ("w1::w2"));
865 /* Same, with a "complicated" symbol. */
867 static const char str[] = Z_SYM_NAME;
868 size_t len = strlen (str);
869 std::string lookup;
871 for (size_t i = 1; i < len; i++)
873 lookup.assign (str, i);
874 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
875 EXPECT (Z_SYM_NAME));
879 /* In FULL mode, an incomplete symbol doesn't match. */
881 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
882 {});
885 /* A complete symbol with parameters matches any overload, since the
886 index has no overload info. */
888 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
889 EXPECT ("std::zfunction", "std::zfunction2"));
890 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
891 EXPECT ("std::zfunction", "std::zfunction2"));
892 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
893 EXPECT ("std::zfunction", "std::zfunction2"));
896 /* Check that whitespace is ignored appropriately. A symbol with a
897 template argument list. */
899 static const char expected[] = "ns::foo<int>";
900 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
901 EXPECT (expected));
902 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
903 EXPECT (expected));
906 /* Check that whitespace is ignored appropriately. A symbol with a
907 template argument list that includes a pointer. */
909 static const char expected[] = "ns::foo<char*>";
910 /* Try both completion and non-completion modes. */
911 static const bool completion_mode[2] = {false, true};
912 for (size_t i = 0; i < 2; i++)
914 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
915 completion_mode[i], EXPECT (expected));
916 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
917 completion_mode[i], EXPECT (expected));
919 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
920 completion_mode[i], EXPECT (expected));
921 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
922 completion_mode[i], EXPECT (expected));
927 /* Check method qualifiers are ignored. */
928 static const char expected[] = "ns::foo<char*>";
929 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
930 symbol_name_match_type::FULL, true, EXPECT (expected));
931 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
932 symbol_name_match_type::FULL, true, EXPECT (expected));
933 CHECK_MATCH ("foo < char * > ( int ) const",
934 symbol_name_match_type::WILD, true, EXPECT (expected));
935 CHECK_MATCH ("foo < char * > ( int ) &&",
936 symbol_name_match_type::WILD, true, EXPECT (expected));
939 /* Test lookup names that don't match anything. */
941 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
942 {});
944 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
945 {});
948 /* Some wild matching tests, exercising "(anonymous namespace)",
949 which should not be confused with a parameter list. */
951 static const char *syms[] = {
952 "A::B::C",
953 "B::C",
954 "C",
955 "A :: B :: C ( int )",
956 "B :: C ( int )",
957 "C ( int )",
960 for (const char *s : syms)
962 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
963 EXPECT ("(anonymous namespace)::A::B::C"));
968 static const char expected[] = "ns2::tmpl<int>::foo2";
969 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
970 EXPECT (expected));
971 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
972 EXPECT (expected));
975 SELF_CHECK (!any_mismatch);
977 #undef EXPECT
978 #undef CHECK_MATCH
981 static void
982 run_test ()
984 test_mapped_index_find_name_component_bounds ();
985 test_dw2_expand_symtabs_matching_symbol ();
988 }} // namespace selftests::dw2_expand_symtabs_matching
990 #endif /* GDB_SELF_TEST */
992 struct dwarf2_gdb_index : public dwarf2_base_index_functions
994 /* This dumps minimal information about the index.
995 It is called via "mt print objfiles".
996 One use is to verify .gdb_index has been loaded by the
997 gdb.dwarf2/gdb-index.exp testcase. */
998 void dump (struct objfile *objfile) override;
1000 bool expand_symtabs_matching
1001 (struct objfile *objfile,
1002 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
1003 const lookup_name_info *lookup_name,
1004 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
1005 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
1006 block_search_flags search_flags,
1007 domain_search_flags domain,
1008 gdb::function_view<expand_symtabs_lang_matcher_ftype> lang_matcher)
1009 override;
1012 /* This dumps minimal information about the index.
1013 It is called via "mt print objfiles".
1014 One use is to verify .gdb_index has been loaded by the
1015 gdb.dwarf2/gdb-index.exp testcase. */
1017 void
1018 dwarf2_gdb_index::dump (struct objfile *objfile)
1020 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
1022 mapped_gdb_index *index = (gdb::checked_static_cast<mapped_gdb_index *>
1023 (per_objfile->per_bfd->index_table.get ()));
1024 gdb_printf (".gdb_index: version %d\n", index->version);
1025 gdb_printf ("\n");
1028 /* Helper for dw2_expand_matching symtabs. Called on each symbol
1029 matched, to expand corresponding CUs that were marked. IDX is the
1030 index of the symbol name that matched. */
1032 static bool
1033 dw2_expand_marked_cus
1034 (dwarf2_per_objfile *per_objfile, offset_type idx,
1035 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
1036 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
1037 block_search_flags search_flags,
1038 domain_search_flags kind,
1039 gdb::function_view<expand_symtabs_lang_matcher_ftype> lang_matcher)
1041 offset_type vec_len, vec_idx;
1042 bool global_seen = false;
1043 mapped_gdb_index &index
1044 = *(gdb::checked_static_cast<mapped_gdb_index *>
1045 (per_objfile->per_bfd->index_table.get ()));
1047 offset_view vec (index.constant_pool.slice (index.symbol_vec_index (idx)));
1048 vec_len = vec[0];
1049 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
1051 offset_type cu_index_and_attrs = vec[vec_idx + 1];
1052 /* This value is only valid for index versions >= 7. */
1053 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
1054 gdb_index_symbol_kind symbol_kind =
1055 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
1056 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
1057 /* Only check the symbol attributes if they're present.
1058 Indices prior to version 7 don't record them,
1059 and indices >= 7 may elide them for certain symbols
1060 (gold does this). */
1061 int attrs_valid =
1062 (index.version >= 7
1063 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
1065 /* Work around gold/15646. */
1066 if (attrs_valid
1067 && !is_static
1068 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
1070 if (global_seen)
1071 continue;
1073 global_seen = true;
1076 /* Only check the symbol's kind if it has one. */
1077 if (attrs_valid)
1079 if (is_static)
1081 if ((search_flags & SEARCH_STATIC_BLOCK) == 0)
1082 continue;
1084 else
1086 if ((search_flags & SEARCH_GLOBAL_BLOCK) == 0)
1087 continue;
1090 domain_search_flags mask = 0;
1091 switch (symbol_kind)
1093 case GDB_INDEX_SYMBOL_KIND_VARIABLE:
1094 mask = SEARCH_VAR_DOMAIN;
1095 break;
1096 case GDB_INDEX_SYMBOL_KIND_FUNCTION:
1097 mask = SEARCH_FUNCTION_DOMAIN;
1098 break;
1099 case GDB_INDEX_SYMBOL_KIND_TYPE:
1100 mask = SEARCH_TYPE_DOMAIN | SEARCH_STRUCT_DOMAIN;
1101 break;
1102 case GDB_INDEX_SYMBOL_KIND_OTHER:
1103 mask = SEARCH_MODULE_DOMAIN;
1104 break;
1106 if ((kind & mask) == 0)
1107 continue;
1110 /* Don't crash on bad data. */
1111 if (cu_index >= per_objfile->per_bfd->all_units.size ())
1113 complaint (_(".gdb_index entry has bad CU index"
1114 " [in module %s]"), objfile_name (per_objfile->objfile));
1115 continue;
1118 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (cu_index);
1119 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
1120 expansion_notify, lang_matcher))
1121 return false;
1124 return true;
1127 bool
1128 dwarf2_gdb_index::expand_symtabs_matching
1129 (struct objfile *objfile,
1130 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
1131 const lookup_name_info *lookup_name,
1132 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
1133 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
1134 block_search_flags search_flags,
1135 domain_search_flags domain,
1136 gdb::function_view<expand_symtabs_lang_matcher_ftype> lang_matcher)
1138 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
1140 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
1142 /* This invariant is documented in quick-functions.h. */
1143 gdb_assert (lookup_name != nullptr || symbol_matcher == nullptr);
1144 if (lookup_name == nullptr)
1146 for (dwarf2_per_cu_data *per_cu
1147 : all_units_range (per_objfile->per_bfd))
1149 QUIT;
1151 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile,
1152 file_matcher,
1153 expansion_notify,
1154 lang_matcher))
1155 return false;
1157 return true;
1160 mapped_gdb_index &index
1161 = *(gdb::checked_static_cast<mapped_gdb_index *>
1162 (per_objfile->per_bfd->index_table.get ()));
1164 bool result
1165 = dw2_expand_symtabs_matching_symbol (index, *lookup_name,
1166 symbol_matcher,
1167 [&] (offset_type idx)
1169 if (!dw2_expand_marked_cus (per_objfile, idx, file_matcher,
1170 expansion_notify, search_flags, domain,
1171 lang_matcher))
1172 return false;
1173 return true;
1174 }, per_objfile, lang_matcher);
1176 return result;
1179 quick_symbol_functions_up
1180 mapped_gdb_index::make_quick_functions () const
1182 return quick_symbol_functions_up (new dwarf2_gdb_index);
1185 /* A helper function that reads the .gdb_index from BUFFER and fills
1186 in MAP. FILENAME is the name of the file containing the data;
1187 it is used for error reporting. DEPRECATED_OK is true if it is
1188 ok to use deprecated sections.
1190 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
1191 out parameters that are filled in with information about the CU and
1192 TU lists in the section.
1194 Returns true if all went well, false otherwise. */
1196 static bool
1197 read_gdb_index_from_buffer (const char *filename,
1198 bool deprecated_ok,
1199 gdb::array_view<const gdb_byte> buffer,
1200 mapped_gdb_index *map,
1201 const gdb_byte **cu_list,
1202 offset_type *cu_list_elements,
1203 const gdb_byte **types_list,
1204 offset_type *types_list_elements)
1206 const gdb_byte *addr = &buffer[0];
1207 offset_view metadata (buffer);
1209 /* Version check. */
1210 offset_type version = metadata[0];
1211 /* Versions earlier than 3 emitted every copy of a psymbol. This
1212 causes the index to behave very poorly for certain requests. Version 3
1213 contained incomplete addrmap. So, it seems better to just ignore such
1214 indices. */
1215 if (version < 4)
1217 static int warning_printed = 0;
1218 if (!warning_printed)
1220 warning (_("Skipping obsolete .gdb_index section in %s."),
1221 filename);
1222 warning_printed = 1;
1224 return 0;
1226 /* Index version 4 uses a different hash function than index version
1227 5 and later.
1229 Versions earlier than 6 did not emit psymbols for inlined
1230 functions. Using these files will cause GDB not to be able to
1231 set breakpoints on inlined functions by name, so we ignore these
1232 indices unless the user has done
1233 "set use-deprecated-index-sections on". */
1234 if (version < 6 && !deprecated_ok)
1236 static int warning_printed = 0;
1237 if (!warning_printed)
1239 warning (_("\
1240 Skipping deprecated .gdb_index section in %s.\n\
1241 Do \"%ps\" before the file is read\n\
1242 to use the section anyway."),
1243 filename,
1244 styled_string (command_style.style (),
1245 "set use-deprecated-index-sections on"));
1246 warning_printed = 1;
1248 return 0;
1250 /* Version 7 indices generated by gold refer to the CU for a symbol instead
1251 of the TU (for symbols coming from TUs),
1252 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
1253 Plus gold-generated indices can have duplicate entries for global symbols,
1254 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
1255 These are just performance bugs, and we can't distinguish gdb-generated
1256 indices from gold-generated ones, so issue no warning here. */
1258 /* Indexes with higher version than the one supported by GDB may be no
1259 longer backward compatible. */
1260 if (version > 9)
1261 return 0;
1263 map->version = version;
1265 int i = 1;
1266 *cu_list = addr + metadata[i];
1267 *cu_list_elements = (metadata[i + 1] - metadata[i]) / 8;
1268 ++i;
1270 *types_list = addr + metadata[i];
1271 *types_list_elements = (metadata[i + 1] - metadata[i]) / 8;
1272 ++i;
1274 const gdb_byte *address_table = addr + metadata[i];
1275 const gdb_byte *address_table_end = addr + metadata[i + 1];
1276 map->address_table
1277 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
1278 ++i;
1280 const gdb_byte *symbol_table = addr + metadata[i];
1281 const gdb_byte *symbol_table_end = addr + metadata[i + 1];
1282 map->symbol_table
1283 = offset_view (gdb::array_view<const gdb_byte> (symbol_table,
1284 symbol_table_end));
1286 ++i;
1288 if (version >= 9)
1290 const gdb_byte *shortcut_table = addr + metadata[i];
1291 const gdb_byte *shortcut_table_end = addr + metadata[i + 1];
1292 map->shortcut_table
1293 = gdb::array_view<const gdb_byte> (shortcut_table, shortcut_table_end);
1294 ++i;
1297 map->constant_pool = buffer.slice (metadata[i]);
1299 if (map->constant_pool.empty () && !map->symbol_table.empty ())
1301 /* An empty constant pool implies that all symbol table entries are
1302 empty. Make map->symbol_table.empty () == true. */
1303 map->symbol_table
1304 = offset_view (gdb::array_view<const gdb_byte> (symbol_table,
1305 symbol_table));
1308 return 1;
1311 /* A helper for create_cus_from_gdb_index that handles a given list of
1312 CUs. */
1314 static void
1315 create_cus_from_gdb_index_list (dwarf2_per_bfd *per_bfd,
1316 const gdb_byte *cu_list, offset_type n_elements,
1317 struct dwarf2_section_info *section,
1318 int is_dwz)
1320 for (offset_type i = 0; i < n_elements; i += 2)
1322 static_assert (sizeof (ULONGEST) >= 8);
1324 sect_offset sect_off
1325 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
1326 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
1327 cu_list += 2 * 8;
1329 dwarf2_per_cu_data_up per_cu
1330 = create_cu_from_index_list (per_bfd, section, is_dwz, sect_off,
1331 length);
1332 per_bfd->all_units.push_back (std::move (per_cu));
1336 /* Read the CU list from the mapped index, and use it to create all
1337 the CU objects for PER_BFD. */
1339 static void
1340 create_cus_from_gdb_index (dwarf2_per_bfd *per_bfd,
1341 const gdb_byte *cu_list, offset_type cu_list_elements,
1342 const gdb_byte *dwz_list, offset_type dwz_elements)
1344 gdb_assert (per_bfd->all_units.empty ());
1345 per_bfd->all_units.reserve ((cu_list_elements + dwz_elements) / 2);
1347 create_cus_from_gdb_index_list (per_bfd, cu_list, cu_list_elements,
1348 &per_bfd->infos[0], 0);
1350 if (dwz_elements == 0)
1351 return;
1353 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
1354 create_cus_from_gdb_index_list (per_bfd, dwz_list, dwz_elements,
1355 &dwz->info, 1);
1358 /* Create the signatured type hash table from the index. */
1360 static void
1361 create_signatured_type_table_from_gdb_index
1362 (dwarf2_per_bfd *per_bfd, struct dwarf2_section_info *section,
1363 const gdb_byte *bytes, offset_type elements)
1365 htab_up sig_types_hash = allocate_signatured_type_table ();
1367 for (offset_type i = 0; i < elements; i += 3)
1369 signatured_type_up sig_type;
1370 ULONGEST signature;
1371 void **slot;
1372 cu_offset type_offset_in_tu;
1374 static_assert (sizeof (ULONGEST) >= 8);
1375 sect_offset sect_off
1376 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
1377 type_offset_in_tu
1378 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
1379 BFD_ENDIAN_LITTLE);
1380 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
1381 bytes += 3 * 8;
1383 sig_type = per_bfd->allocate_signatured_type (signature);
1384 sig_type->type_offset_in_tu = type_offset_in_tu;
1385 sig_type->section = section;
1386 sig_type->sect_off = sect_off;
1388 slot = htab_find_slot (sig_types_hash.get (), sig_type.get (), INSERT);
1389 *slot = sig_type.get ();
1391 per_bfd->all_units.emplace_back (sig_type.release ());
1394 per_bfd->signatured_types = std::move (sig_types_hash);
1397 /* Read the address map data from the mapped GDB index. */
1399 static void
1400 create_addrmap_from_gdb_index (dwarf2_per_objfile *per_objfile,
1401 mapped_gdb_index *index)
1403 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
1404 const gdb_byte *iter, *end;
1406 addrmap_mutable mutable_map;
1408 iter = index->address_table.data ();
1409 end = iter + index->address_table.size ();
1411 while (iter < end)
1413 ULONGEST hi, lo, cu_index;
1414 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1415 iter += 8;
1416 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1417 iter += 8;
1418 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
1419 iter += 4;
1421 if (lo > hi)
1423 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
1424 hex_string (lo), hex_string (hi));
1425 continue;
1428 if (cu_index >= per_bfd->all_units.size ())
1430 complaint (_(".gdb_index address table has invalid CU number %u"),
1431 (unsigned) cu_index);
1432 continue;
1435 mutable_map.set_empty (lo, hi - 1, per_bfd->get_cu (cu_index));
1438 index->index_addrmap
1439 = new (&per_bfd->obstack) addrmap_fixed (&per_bfd->obstack, &mutable_map);
1442 /* Sets the name and language of the main function from the shortcut table. */
1444 static void
1445 set_main_name_from_gdb_index (dwarf2_per_objfile *per_objfile,
1446 mapped_gdb_index *index)
1448 const auto expected_size = 2 * sizeof (offset_type);
1449 if (index->shortcut_table.size () < expected_size)
1450 /* The data in the section is not present, is corrupted or is in a version
1451 we don't know about. Regardless, we can't make use of it. */
1452 return;
1454 auto ptr = index->shortcut_table.data ();
1455 const auto dw_lang = extract_unsigned_integer (ptr, 4, BFD_ENDIAN_LITTLE);
1456 if (dw_lang >= DW_LANG_hi_user)
1458 complaint (_(".gdb_index shortcut table has invalid main language %u"),
1459 (unsigned) dw_lang);
1460 return;
1462 if (dw_lang == 0)
1464 /* Don't bother if the language for the main symbol was not known or if
1465 there was no main symbol at all when the index was built. */
1466 return;
1468 ptr += 4;
1470 const auto lang = dwarf_lang_to_enum_language (dw_lang);
1471 const auto name_offset = extract_unsigned_integer (ptr,
1472 sizeof (offset_type),
1473 BFD_ENDIAN_LITTLE);
1474 const auto name = (const char *) (index->constant_pool.data () + name_offset);
1476 set_objfile_main_name (per_objfile->objfile, name, (enum language) lang);
1479 /* See read-gdb-index.h. */
1481 bool
1482 dwarf2_read_gdb_index
1483 (dwarf2_per_objfile *per_objfile,
1484 get_gdb_index_contents_ftype get_gdb_index_contents,
1485 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
1487 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
1488 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
1489 struct dwz_file *dwz;
1490 struct objfile *objfile = per_objfile->objfile;
1491 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
1493 gdb::array_view<const gdb_byte> main_index_contents
1494 = get_gdb_index_contents (objfile, per_bfd);
1496 if (main_index_contents.empty ())
1497 return false;
1499 auto map = std::make_unique<mapped_gdb_index> ();
1500 if (!read_gdb_index_from_buffer (objfile_name (objfile),
1501 use_deprecated_index_sections,
1502 main_index_contents, map.get (), &cu_list,
1503 &cu_list_elements, &types_list,
1504 &types_list_elements))
1505 return false;
1507 /* Don't use the index if it's empty. */
1508 if (map->symbol_table.empty ())
1509 return false;
1511 /* If there is a .dwz file, read it so we can get its CU list as
1512 well. */
1513 dwz = dwarf2_get_dwz_file (per_bfd);
1514 if (dwz != NULL)
1516 mapped_gdb_index dwz_map;
1517 const gdb_byte *dwz_types_ignore;
1518 offset_type dwz_types_elements_ignore;
1520 gdb::array_view<const gdb_byte> dwz_index_content
1521 = get_gdb_index_contents_dwz (objfile, dwz);
1523 if (dwz_index_content.empty ())
1524 return false;
1526 if (!read_gdb_index_from_buffer (bfd_get_filename (dwz->dwz_bfd.get ()),
1527 1, dwz_index_content, &dwz_map,
1528 &dwz_list, &dwz_list_elements,
1529 &dwz_types_ignore,
1530 &dwz_types_elements_ignore))
1532 warning (_("could not read '.gdb_index' section from %s; skipping"),
1533 bfd_get_filename (dwz->dwz_bfd.get ()));
1534 return false;
1538 create_cus_from_gdb_index (per_bfd, cu_list, cu_list_elements, dwz_list,
1539 dwz_list_elements);
1541 if (types_list_elements)
1543 /* We can only handle a single .debug_info and .debug_types when we have
1544 an index. */
1545 if (per_bfd->infos.size () > 1
1546 || per_bfd->types.size () > 1)
1548 per_bfd->all_units.clear ();
1549 return false;
1552 dwarf2_section_info *section
1553 = (per_bfd->types.size () == 1
1554 ? &per_bfd->types[0]
1555 : &per_bfd->infos[0]);
1557 create_signatured_type_table_from_gdb_index (per_bfd, section, types_list,
1558 types_list_elements);
1561 finalize_all_units (per_bfd);
1563 create_addrmap_from_gdb_index (per_objfile, map.get ());
1565 set_main_name_from_gdb_index (per_objfile, map.get ());
1567 per_bfd->index_table = std::move (map);
1568 per_bfd->quick_file_names_table =
1569 create_quick_file_names_table (per_bfd->all_units.size ());
1571 return true;
1574 void _initialize_read_gdb_index ();
1576 void
1577 _initialize_read_gdb_index ()
1579 add_setshow_boolean_cmd ("use-deprecated-index-sections",
1580 no_class, &use_deprecated_index_sections, _("\
1581 Set whether to use deprecated gdb_index sections."), _("\
1582 Show whether to use deprecated gdb_index sections."), _("\
1583 When enabled, deprecated .gdb_index sections are used anyway.\n\
1584 Normally they are ignored either because of a missing feature or\n\
1585 performance issue.\n\
1586 Warning: This option must be enabled before gdb reads the file."),
1587 NULL,
1588 NULL,
1589 &setlist, &showlist);
1591 #if GDB_SELF_TEST
1592 selftests::register_test ("dw2_expand_symtabs_matching",
1593 selftests::dw2_expand_symtabs_matching::run_test);
1594 #endif