1 /* DWARF index writing support for GDB.
3 Copyright (C) 1994-2023 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/>. */
22 #include "dwarf2/index-write.h"
25 #include "cli/cli-decode.h"
26 #include "gdbsupport/byte-vector.h"
27 #include "gdbsupport/filestuff.h"
28 #include "gdbsupport/gdb_unlinker.h"
29 #include "gdbsupport/pathstuff.h"
30 #include "gdbsupport/scoped_fd.h"
31 #include "complaints.h"
32 #include "dwarf2/index-common.h"
33 #include "dwarf2/cooked-index.h"
35 #include "dwarf2/read.h"
36 #include "dwarf2/dwz.h"
37 #include "gdb/gdb-index.h"
41 #include "dwarf2/tag.h"
42 #include "gdbsupport/gdb_tilde_expand.h"
46 #include <forward_list>
48 #include <unordered_map>
49 #include <unordered_set>
51 /* Ensure only legit values are used. */
52 #define DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE(cu_index, value) \
54 gdb_assert ((unsigned int) (value) <= 1); \
55 GDB_INDEX_SYMBOL_STATIC_SET_VALUE((cu_index), (value)); \
58 /* Ensure only legit values are used. */
59 #define DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE(cu_index, value) \
61 gdb_assert ((value) >= GDB_INDEX_SYMBOL_KIND_TYPE \
62 && (value) <= GDB_INDEX_SYMBOL_KIND_OTHER); \
63 GDB_INDEX_SYMBOL_KIND_SET_VALUE((cu_index), (value)); \
66 /* Ensure we don't use more than the allotted number of bits for the CU. */
67 #define DW2_GDB_INDEX_CU_SET_VALUE(cu_index, value) \
69 gdb_assert (((value) & ~GDB_INDEX_CU_MASK) == 0); \
70 GDB_INDEX_CU_SET_VALUE((cu_index), (value)); \
73 /* The "save gdb-index" command. */
75 /* Write SIZE bytes from the buffer pointed to by DATA to FILE, with
79 file_write (FILE *file
, const void *data
, size_t size
)
81 if (fwrite (data
, 1, size
, file
) != size
)
82 error (_("couldn't data write to file"));
85 /* Write the contents of VEC to FILE, with error checking. */
87 template<typename Elem
, typename Alloc
>
89 file_write (FILE *file
, const std::vector
<Elem
, Alloc
> &vec
)
92 file_write (file
, vec
.data (), vec
.size () * sizeof (vec
[0]));
95 /* In-memory buffer to prepare data to be written later to a file. */
99 /* Copy ARRAY to the end of the buffer. */
100 void append_array (gdb::array_view
<const gdb_byte
> array
)
102 std::copy (array
.begin (), array
.end (), grow (array
.size ()));
105 /* Copy CSTR (a zero-terminated string) to the end of buffer. The
106 terminating zero is appended too. */
107 void append_cstr0 (const char *cstr
)
109 const size_t size
= strlen (cstr
) + 1;
110 std::copy (cstr
, cstr
+ size
, grow (size
));
113 /* Store INPUT as ULEB128 to the end of buffer. */
114 void append_unsigned_leb128 (ULONGEST input
)
118 gdb_byte output
= input
& 0x7f;
122 m_vec
.push_back (output
);
128 /* Accept a host-format integer in VAL and append it to the buffer
129 as a target-format integer which is LEN bytes long. */
130 void append_uint (size_t len
, bfd_endian byte_order
, ULONGEST val
)
132 ::store_unsigned_integer (grow (len
), len
, byte_order
, val
);
135 /* Copy VALUE to the end of the buffer, little-endian. */
136 void append_offset (offset_type value
)
138 append_uint (sizeof (value
), BFD_ENDIAN_LITTLE
, value
);
141 /* Return the size of the buffer. */
142 virtual size_t size () const
144 return m_vec
.size ();
147 /* Return true iff the buffer is empty. */
150 return m_vec
.empty ();
153 /* Write the buffer to FILE. */
154 void file_write (FILE *file
) const
156 ::file_write (file
, m_vec
);
160 /* Grow SIZE bytes at the end of the buffer. Returns a pointer to
161 the start of the new block. */
162 gdb_byte
*grow (size_t size
)
164 m_vec
.resize (m_vec
.size () + size
);
165 return &*(m_vec
.end () - size
);
168 gdb::byte_vector m_vec
;
171 /* An entry in the symbol table. */
172 struct symtab_index_entry
174 /* The name of the symbol. */
176 /* The offset of the name in the constant pool. */
177 offset_type index_offset
;
178 /* A sorted vector of the indices of all the CUs that hold an object
180 std::vector
<offset_type
> cu_indices
;
182 /* Minimize CU_INDICES, sorting them and removing duplicates as
187 /* The symbol table. This is a power-of-2-sized hash table. */
192 m_data
.resize (1024);
195 /* If there are no elements in the symbol table, then reduce the table
196 size to zero. Otherwise call symtab_index_entry::minimize each entry
197 in the symbol table. */
201 if (m_element_count
== 0)
204 for (symtab_index_entry
&item
: m_data
)
208 /* Add an entry to SYMTAB. NAME is the name of the symbol. CU_INDEX is
209 the index of the CU in which the symbol appears. IS_STATIC is one if
210 the symbol is static, otherwise zero (global). */
212 void add_index_entry (const char *name
, int is_static
,
213 gdb_index_symbol_kind kind
, offset_type cu_index
);
215 /* When entries are originally added into the data hash the order will
216 vary based on the number of worker threads GDB is configured to use.
217 This function will rebuild the hash such that the final layout will be
218 deterministic regardless of the number of worker threads used. */
222 /* Access the obstack. */
223 struct obstack
*obstack ()
224 { return &m_string_obstack
; }
228 /* Find a slot in SYMTAB for the symbol NAME. Returns a reference to
231 Function is used only during write_hash_table so no index format
232 backward compatibility is needed. */
234 symtab_index_entry
&find_slot (const char *name
);
236 /* Expand SYMTAB's hash table. */
240 /* Return true if the hash table in data needs to grow. */
242 bool hash_needs_expanding () const
243 { return 4 * m_element_count
/ 3 >= m_data
.size (); }
245 /* A vector that is used as a hash table. */
246 std::vector
<symtab_index_entry
> m_data
;
248 /* The number of elements stored in the m_data hash. */
249 offset_type m_element_count
= 0;
251 /* Temporary storage for names. */
252 auto_obstack m_string_obstack
;
255 using iterator
= decltype (m_data
)::iterator
;
256 using const_iterator
= decltype (m_data
)::const_iterator
;
259 { return m_data
.begin (); }
262 { return m_data
.end (); }
264 const_iterator
cbegin ()
265 { return m_data
.cbegin (); }
267 const_iterator
cend ()
268 { return m_data
.cend (); }
271 /* See class definition. */
274 mapped_symtab::find_slot (const char *name
)
276 offset_type index
, step
, hash
= mapped_index_string_hash (INT_MAX
, name
);
278 index
= hash
& (m_data
.size () - 1);
279 step
= ((hash
* 17) & (m_data
.size () - 1)) | 1;
283 if (m_data
[index
].name
== NULL
284 || strcmp (name
, m_data
[index
].name
) == 0)
285 return m_data
[index
];
286 index
= (index
+ step
) & (m_data
.size () - 1);
290 /* See class definition. */
293 mapped_symtab::hash_expand ()
295 auto old_entries
= std::move (m_data
);
297 gdb_assert (m_data
.size () == 0);
298 m_data
.resize (old_entries
.size () * 2);
300 for (auto &it
: old_entries
)
303 auto &ref
= this->find_slot (it
.name
);
304 ref
= std::move (it
);
308 /* See mapped_symtab class declaration. */
310 void mapped_symtab::sort ()
312 /* Move contents out of this->data vector. */
313 std::vector
<symtab_index_entry
> original_data
= std::move (m_data
);
315 /* Restore the size of m_data, this will avoid having to expand the hash
316 table (and rehash all elements) when we reinsert after sorting.
317 However, we do reset the element count, this allows for some sanity
318 checking asserts during the reinsert phase. */
319 gdb_assert (m_data
.size () == 0);
320 m_data
.resize (original_data
.size ());
323 /* Remove empty entries from ORIGINAL_DATA, this makes sorting quicker. */
324 auto it
= std::remove_if (original_data
.begin (), original_data
.end (),
325 [] (const symtab_index_entry
&entry
) -> bool
327 return entry
.name
== nullptr;
329 original_data
.erase (it
, original_data
.end ());
331 /* Sort the existing contents. */
332 std::sort (original_data
.begin (), original_data
.end (),
333 [] (const symtab_index_entry
&a
,
334 const symtab_index_entry
&b
) -> bool
336 /* Return true if A is before B. */
337 gdb_assert (a
.name
!= nullptr);
338 gdb_assert (b
.name
!= nullptr);
340 return strcmp (a
.name
, b
.name
) < 0;
343 /* Re-insert each item from the sorted list. */
344 for (auto &entry
: original_data
)
346 /* We know that ORIGINAL_DATA contains no duplicates, this data was
347 taken from a hash table that de-duplicated entries for us, so
348 count this as a new item.
350 As we retained the original size of m_data (see above) then we
351 should never need to grow m_data_ during this re-insertion phase,
354 gdb_assert (!this->hash_needs_expanding ());
357 symtab_index_entry
&slot
= this->find_slot (entry
.name
);
359 /* As discussed above, we should not find duplicates. */
360 gdb_assert (slot
.name
== nullptr);
362 /* Move this item into the slot we found. */
363 slot
= std::move (entry
);
367 /* See class definition. */
370 mapped_symtab::add_index_entry (const char *name
, int is_static
,
371 gdb_index_symbol_kind kind
,
372 offset_type cu_index
)
374 symtab_index_entry
*slot
= &this->find_slot (name
);
375 if (slot
->name
== NULL
)
377 /* This is a new element in the hash table. */
378 ++this->m_element_count
;
380 /* We might need to grow the hash table. */
381 if (this->hash_needs_expanding ())
383 this->hash_expand ();
385 /* This element will have a different slot in the new table. */
386 slot
= &this->find_slot (name
);
388 /* But it should still be a new element in the hash table. */
389 gdb_assert (slot
->name
== nullptr);
393 /* index_offset is set later. */
396 offset_type cu_index_and_attrs
= 0;
397 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs
, cu_index
);
398 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs
, is_static
);
399 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs
, kind
);
401 /* We don't want to record an index value twice as we want to avoid the
403 We process all global symbols and then all static symbols
404 (which would allow us to avoid the duplication by only having to check
405 the last entry pushed), but a symbol could have multiple kinds in one CU.
406 To keep things simple we don't worry about the duplication here and
407 sort and uniquify the list after we've processed all symbols. */
408 slot
->cu_indices
.push_back (cu_index_and_attrs
);
411 /* See symtab_index_entry. */
414 symtab_index_entry::minimize ()
416 if (name
== nullptr || cu_indices
.empty ())
419 std::sort (cu_indices
.begin (), cu_indices
.end ());
420 auto from
= std::unique (cu_indices
.begin (), cu_indices
.end ());
421 cu_indices
.erase (from
, cu_indices
.end ());
423 /* We don't want to enter a type more than once, so
424 remove any such duplicates from the list as well. When doing
425 this, we want to keep the entry from the first CU -- but this is
426 implicit due to the sort. This choice is done because it's
427 similar to what gdb historically did for partial symbols. */
428 std::unordered_set
<offset_type
> seen
;
429 from
= std::remove_if (cu_indices
.begin (), cu_indices
.end (),
430 [&] (offset_type val
)
432 gdb_index_symbol_kind kind
= GDB_INDEX_SYMBOL_KIND_VALUE (val
);
433 if (kind
!= GDB_INDEX_SYMBOL_KIND_TYPE
)
436 val
&= ~GDB_INDEX_CU_MASK
;
437 return !seen
.insert (val
).second
;
439 cu_indices
.erase (from
, cu_indices
.end ());
442 /* A form of 'const char *' suitable for container keys. Only the
443 pointer is stored. The strings themselves are compared, not the
448 c_str_view (const char *cstr
)
452 bool operator== (const c_str_view
&other
) const
454 return strcmp (m_cstr
, other
.m_cstr
) == 0;
457 bool operator< (const c_str_view
&other
) const
459 return strcmp (m_cstr
, other
.m_cstr
) < 0;
462 /* Return the underlying C string. Note, the returned string is
463 only a reference with lifetime of this object. */
464 const char *c_str () const
470 friend class c_str_view_hasher
;
471 const char *const m_cstr
;
474 /* A std::unordered_map::hasher for c_str_view that uses the right
475 hash function for strings in a mapped index. */
476 class c_str_view_hasher
479 size_t operator () (const c_str_view
&x
) const
481 return mapped_index_string_hash (INT_MAX
, x
.m_cstr
);
485 /* A std::unordered_map::hasher for std::vector<>. */
490 size_t operator () (const std::vector
<T
> &key
) const
492 return iterative_hash (key
.data (),
493 sizeof (key
.front ()) * key
.size (), 0);
497 /* Write the mapped hash table SYMTAB to the data buffer OUTPUT, with
498 constant pool entries going into the data buffer CPOOL. */
501 write_hash_table (mapped_symtab
*symtab
, data_buf
&output
, data_buf
&cpool
)
504 /* Elements are sorted vectors of the indices of all the CUs that
505 hold an object of this name. */
506 std::unordered_map
<std::vector
<offset_type
>, offset_type
,
507 vector_hasher
<offset_type
>>
510 /* We add all the index vectors to the constant pool first, to
511 ensure alignment is ok. */
512 for (symtab_index_entry
&entry
: *symtab
)
514 if (entry
.name
== NULL
)
516 gdb_assert (entry
.index_offset
== 0);
518 auto [iter
, inserted
]
519 = symbol_hash_table
.try_emplace (entry
.cu_indices
,
521 entry
.index_offset
= iter
->second
;
524 /* Newly inserted. */
525 cpool
.append_offset (entry
.cu_indices
.size ());
526 for (const auto index
: entry
.cu_indices
)
527 cpool
.append_offset (index
);
532 /* Now write out the hash table. */
533 std::unordered_map
<c_str_view
, offset_type
, c_str_view_hasher
> str_table
;
534 for (const auto &entry
: *symtab
)
536 offset_type str_off
, vec_off
;
538 if (entry
.name
!= NULL
)
540 const auto insertpair
= str_table
.emplace (entry
.name
, cpool
.size ());
541 if (insertpair
.second
)
542 cpool
.append_cstr0 (entry
.name
);
543 str_off
= insertpair
.first
->second
;
544 vec_off
= entry
.index_offset
;
548 /* While 0 is a valid constant pool index, it is not valid
549 to have 0 for both offsets. */
554 output
.append_offset (str_off
);
555 output
.append_offset (vec_off
);
560 = std::unordered_map
<const dwarf2_per_cu_data
*, unsigned int>;
562 /* Helper struct for building the address table. */
563 struct addrmap_index_data
565 addrmap_index_data (data_buf
&addr_vec_
, cu_index_map
&cu_index_htab_
)
566 : addr_vec (addr_vec_
),
567 cu_index_htab (cu_index_htab_
)
571 cu_index_map
&cu_index_htab
;
573 int operator() (CORE_ADDR start_addr
, const void *obj
);
575 /* True if the previous_* fields are valid.
576 We can't write an entry until we see the next entry (since it is only then
577 that we know the end of the entry). */
578 bool previous_valid
= false;
579 /* Index of the CU in the table of all CUs in the index file. */
580 unsigned int previous_cu_index
= 0;
581 /* Start address of the CU. */
582 CORE_ADDR previous_cu_start
= 0;
585 /* Write an address entry to ADDR_VEC. */
588 add_address_entry (data_buf
&addr_vec
,
589 CORE_ADDR start
, CORE_ADDR end
, unsigned int cu_index
)
591 addr_vec
.append_uint (8, BFD_ENDIAN_LITTLE
, start
);
592 addr_vec
.append_uint (8, BFD_ENDIAN_LITTLE
, end
);
593 addr_vec
.append_offset (cu_index
);
596 /* Worker function for traversing an addrmap to build the address table. */
599 addrmap_index_data::operator() (CORE_ADDR start_addr
, const void *obj
)
601 const dwarf2_per_cu_data
*per_cu
602 = static_cast<const dwarf2_per_cu_data
*> (obj
);
605 add_address_entry (addr_vec
,
606 previous_cu_start
, start_addr
,
609 previous_cu_start
= start_addr
;
612 const auto it
= cu_index_htab
.find (per_cu
);
613 gdb_assert (it
!= cu_index_htab
.cend ());
614 previous_cu_index
= it
->second
;
615 previous_valid
= true;
618 previous_valid
= false;
623 /* Write PER_BFD's address map to ADDR_VEC.
624 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
625 in the index file. */
628 write_address_map (const addrmap
*addrmap
, data_buf
&addr_vec
,
629 cu_index_map
&cu_index_htab
)
631 struct addrmap_index_data
addrmap_index_data (addr_vec
, cu_index_htab
);
633 addrmap
->foreach (addrmap_index_data
);
635 /* It's highly unlikely the last entry (end address = 0xff...ff)
636 is valid, but we should still handle it.
637 The end address is recorded as the start of the next region, but that
638 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
640 if (addrmap_index_data
.previous_valid
)
641 add_address_entry (addr_vec
,
642 addrmap_index_data
.previous_cu_start
, (CORE_ADDR
) -1,
643 addrmap_index_data
.previous_cu_index
);
646 /* DWARF-5 .debug_names builder. */
650 debug_names (dwarf2_per_bfd
*per_bfd
, bool is_dwarf64
,
651 bfd_endian dwarf5_byte_order
)
652 : m_dwarf5_byte_order (dwarf5_byte_order
),
653 m_dwarf32 (dwarf5_byte_order
),
654 m_dwarf64 (dwarf5_byte_order
),
656 ? static_cast<dwarf
&> (m_dwarf64
)
657 : static_cast<dwarf
&> (m_dwarf32
)),
658 m_name_table_string_offs (m_dwarf
.name_table_string_offs
),
659 m_name_table_entry_offs (m_dwarf
.name_table_entry_offs
),
660 m_debugstrlookup (per_bfd
)
663 int dwarf5_offset_size () const
665 const bool dwarf5_is_dwarf64
= &m_dwarf
== &m_dwarf64
;
666 return dwarf5_is_dwarf64
? 8 : 4;
669 /* Is this symbol from DW_TAG_compile_unit or DW_TAG_type_unit? */
670 enum class unit_kind
{ cu
, tu
};
672 /* Insert one symbol. */
673 void insert (const cooked_index_entry
*entry
)
675 const auto it
= m_cu_index_htab
.find (entry
->per_cu
);
676 gdb_assert (it
!= m_cu_index_htab
.cend ());
677 const char *name
= entry
->full_name (&m_string_obstack
);
679 /* This is incorrect but it mirrors gdb's historical behavior; and
680 because the current .debug_names generation is also incorrect,
681 it seems better to follow what was done before, rather than
682 introduce a mismatch between the newer and older gdb. */
683 dwarf_tag tag
= entry
->tag
;
684 if (tag
!= DW_TAG_typedef
&& tag_is_type (tag
))
685 tag
= DW_TAG_structure_type
;
686 else if (tag
== DW_TAG_enumerator
|| tag
== DW_TAG_constant
)
687 tag
= DW_TAG_variable
;
689 int cu_index
= it
->second
;
690 bool is_static
= (entry
->flags
& IS_STATIC
) != 0;
691 unit_kind kind
= (entry
->per_cu
->is_debug_types
695 if (entry
->per_cu
->lang () == language_ada
)
697 /* We want to ensure that the Ada main function's name appears
698 verbatim in the index. However, this name will be of the
699 form "_ada_mumble", and will be rewritten by ada_decode.
700 So, recognize it specially here and add it to the index by
702 if (strcmp (main_name (), name
) == 0)
704 const auto insertpair
705 = m_name_to_value_set
.emplace (c_str_view (name
),
706 std::set
<symbol_value
> ());
707 std::set
<symbol_value
> &value_set
= insertpair
.first
->second
;
708 value_set
.emplace (symbol_value (tag
, cu_index
, is_static
, kind
));
711 /* In order for the index to work when read back into gdb, it
712 has to supply a funny form of the name: it should be the
713 encoded name, with any suffixes stripped. Using the
714 ordinary encoded name will not work properly with the
715 searching logic in find_name_components_bounds; nor will
716 using the decoded name. Furthermore, an Ada "verbatim"
717 name (of the form "<MumBle>") must be entered without the
718 angle brackets. Note that the current index is unusual,
719 see PR symtab/24820 for details. */
720 std::string decoded
= ada_decode (name
);
721 if (decoded
[0] == '<')
722 name
= (char *) obstack_copy0 (&m_string_obstack
,
723 decoded
.c_str () + 1,
724 decoded
.length () - 2);
726 name
= obstack_strdup (&m_string_obstack
,
727 ada_encode (decoded
.c_str ()));
730 const auto insertpair
731 = m_name_to_value_set
.emplace (c_str_view (name
),
732 std::set
<symbol_value
> ());
733 std::set
<symbol_value
> &value_set
= insertpair
.first
->second
;
734 value_set
.emplace (symbol_value (tag
, cu_index
, is_static
, kind
));
737 /* Build all the tables. All symbols must be already inserted.
738 This function does not call file_write, caller has to do it
742 /* Verify the build method has not be called twice. */
743 gdb_assert (m_abbrev_table
.empty ());
744 const size_t name_count
= m_name_to_value_set
.size ();
745 m_bucket_table
.resize
746 (std::pow (2, std::ceil (std::log2 (name_count
* 4 / 3))));
747 m_hash_table
.reserve (name_count
);
748 m_name_table_string_offs
.reserve (name_count
);
749 m_name_table_entry_offs
.reserve (name_count
);
751 /* Map each hash of symbol to its name and value. */
755 decltype (m_name_to_value_set
)::const_iterator it
;
757 std::vector
<std::forward_list
<hash_it_pair
>> bucket_hash
;
758 bucket_hash
.resize (m_bucket_table
.size ());
759 for (decltype (m_name_to_value_set
)::const_iterator it
760 = m_name_to_value_set
.cbegin ();
761 it
!= m_name_to_value_set
.cend ();
764 const char *const name
= it
->first
.c_str ();
765 const uint32_t hash
= dwarf5_djb_hash (name
);
766 hash_it_pair hashitpair
;
767 hashitpair
.hash
= hash
;
769 auto &slot
= bucket_hash
[hash
% bucket_hash
.size()];
770 slot
.push_front (std::move (hashitpair
));
772 for (size_t bucket_ix
= 0; bucket_ix
< bucket_hash
.size (); ++bucket_ix
)
774 std::forward_list
<hash_it_pair
> &hashitlist
= bucket_hash
[bucket_ix
];
775 if (hashitlist
.empty ())
778 /* Sort the items within each bucket. This ensures that the
779 generated index files will be the same no matter the order in
780 which symbols were added into the index. */
781 hashitlist
.sort ([] (const hash_it_pair
&a
, const hash_it_pair
&b
)
783 return a
.it
->first
< b
.it
->first
;
786 uint32_t &bucket_slot
= m_bucket_table
[bucket_ix
];
787 /* The hashes array is indexed starting at 1. */
788 store_unsigned_integer (reinterpret_cast<gdb_byte
*> (&bucket_slot
),
789 sizeof (bucket_slot
), m_dwarf5_byte_order
,
790 m_hash_table
.size () + 1);
791 for (const hash_it_pair
&hashitpair
: hashitlist
)
793 m_hash_table
.push_back (0);
794 store_unsigned_integer (reinterpret_cast<gdb_byte
*>
795 (&m_hash_table
.back ()),
796 sizeof (m_hash_table
.back ()),
797 m_dwarf5_byte_order
, hashitpair
.hash
);
798 const c_str_view
&name
= hashitpair
.it
->first
;
799 const std::set
<symbol_value
> &value_set
= hashitpair
.it
->second
;
800 m_name_table_string_offs
.push_back_reorder
801 (m_debugstrlookup
.lookup (name
.c_str ()));
802 m_name_table_entry_offs
.push_back_reorder (m_entry_pool
.size ());
803 gdb_assert (!value_set
.empty ());
804 for (const symbol_value
&value
: value_set
)
806 int &idx
= m_indexkey_to_idx
[index_key (value
.dwarf_tag
,
812 m_abbrev_table
.append_unsigned_leb128 (idx
);
813 m_abbrev_table
.append_unsigned_leb128 (value
.dwarf_tag
);
814 m_abbrev_table
.append_unsigned_leb128
815 (value
.kind
== unit_kind::cu
? DW_IDX_compile_unit
817 m_abbrev_table
.append_unsigned_leb128 (DW_FORM_udata
);
818 m_abbrev_table
.append_unsigned_leb128 (value
.is_static
819 ? DW_IDX_GNU_internal
820 : DW_IDX_GNU_external
);
821 m_abbrev_table
.append_unsigned_leb128 (DW_FORM_flag_present
);
823 /* Terminate attributes list. */
824 m_abbrev_table
.append_unsigned_leb128 (0);
825 m_abbrev_table
.append_unsigned_leb128 (0);
828 m_entry_pool
.append_unsigned_leb128 (idx
);
829 m_entry_pool
.append_unsigned_leb128 (value
.cu_index
);
832 /* Terminate the list of CUs. */
833 m_entry_pool
.append_unsigned_leb128 (0);
836 gdb_assert (m_hash_table
.size () == name_count
);
838 /* Terminate tags list. */
839 m_abbrev_table
.append_unsigned_leb128 (0);
842 /* Return .debug_names bucket count. This must be called only after
843 calling the build method. */
844 uint32_t bucket_count () const
846 /* Verify the build method has been already called. */
847 gdb_assert (!m_abbrev_table
.empty ());
848 const uint32_t retval
= m_bucket_table
.size ();
850 /* Check for overflow. */
851 gdb_assert (retval
== m_bucket_table
.size ());
855 /* Return .debug_names names count. This must be called only after
856 calling the build method. */
857 uint32_t name_count () const
859 /* Verify the build method has been already called. */
860 gdb_assert (!m_abbrev_table
.empty ());
861 const uint32_t retval
= m_hash_table
.size ();
863 /* Check for overflow. */
864 gdb_assert (retval
== m_hash_table
.size ());
868 /* Return number of bytes of .debug_names abbreviation table. This
869 must be called only after calling the build method. */
870 uint32_t abbrev_table_bytes () const
872 gdb_assert (!m_abbrev_table
.empty ());
873 return m_abbrev_table
.size ();
876 /* Return number of bytes the .debug_names section will have. This
877 must be called only after calling the build method. */
878 size_t bytes () const
880 /* Verify the build method has been already called. */
881 gdb_assert (!m_abbrev_table
.empty ());
882 size_t expected_bytes
= 0;
883 expected_bytes
+= m_bucket_table
.size () * sizeof (m_bucket_table
[0]);
884 expected_bytes
+= m_hash_table
.size () * sizeof (m_hash_table
[0]);
885 expected_bytes
+= m_name_table_string_offs
.bytes ();
886 expected_bytes
+= m_name_table_entry_offs
.bytes ();
887 expected_bytes
+= m_abbrev_table
.size ();
888 expected_bytes
+= m_entry_pool
.size ();
889 return expected_bytes
;
892 /* Write .debug_names to FILE_NAMES and .debug_str addition to
893 FILE_STR. This must be called only after calling the build
895 void file_write (FILE *file_names
, FILE *file_str
) const
897 /* Verify the build method has been already called. */
898 gdb_assert (!m_abbrev_table
.empty ());
899 ::file_write (file_names
, m_bucket_table
);
900 ::file_write (file_names
, m_hash_table
);
901 m_name_table_string_offs
.file_write (file_names
);
902 m_name_table_entry_offs
.file_write (file_names
);
903 m_abbrev_table
.file_write (file_names
);
904 m_entry_pool
.file_write (file_names
);
905 m_debugstrlookup
.file_write (file_str
);
908 void add_cu (dwarf2_per_cu_data
*per_cu
, offset_type index
)
910 m_cu_index_htab
.emplace (per_cu
, index
);
915 /* Storage for symbol names mapping them to their .debug_str section
917 class debug_str_lookup
921 /* Object constructor to be called for current DWARF2_PER_BFD.
922 All .debug_str section strings are automatically stored. */
923 debug_str_lookup (dwarf2_per_bfd
*per_bfd
)
924 : m_abfd (per_bfd
->obfd
),
927 gdb_assert (per_bfd
->str
.readin
);
928 if (per_bfd
->str
.buffer
== NULL
)
930 for (const gdb_byte
*data
= per_bfd
->str
.buffer
;
931 data
< (per_bfd
->str
.buffer
932 + per_bfd
->str
.size
);)
934 const char *const s
= reinterpret_cast<const char *> (data
);
935 const auto insertpair
936 = m_str_table
.emplace (c_str_view (s
),
937 data
- per_bfd
->str
.buffer
);
938 if (!insertpair
.second
)
939 complaint (_("Duplicate string \"%s\" in "
940 ".debug_str section [in module %s]"),
941 s
, bfd_get_filename (m_abfd
));
942 data
+= strlen (s
) + 1;
946 /* Return offset of symbol name S in the .debug_str section. Add
947 such symbol to the section's end if it does not exist there
949 size_t lookup (const char *s
)
951 const auto it
= m_str_table
.find (c_str_view (s
));
952 if (it
!= m_str_table
.end ())
954 const size_t offset
= (m_per_bfd
->str
.size
955 + m_str_add_buf
.size ());
956 m_str_table
.emplace (c_str_view (s
), offset
);
957 m_str_add_buf
.append_cstr0 (s
);
961 /* Append the end of the .debug_str section to FILE. */
962 void file_write (FILE *file
) const
964 m_str_add_buf
.file_write (file
);
968 std::unordered_map
<c_str_view
, size_t, c_str_view_hasher
> m_str_table
;
970 dwarf2_per_bfd
*m_per_bfd
;
972 /* Data to add at the end of .debug_str for new needed symbol names. */
973 data_buf m_str_add_buf
;
976 /* Container to map used DWARF tags to their .debug_names abbreviation
981 index_key (int dwarf_tag_
, bool is_static_
, unit_kind kind_
)
982 : dwarf_tag (dwarf_tag_
), is_static (is_static_
), kind (kind_
)
987 operator== (const index_key
&other
) const
989 return (dwarf_tag
== other
.dwarf_tag
&& is_static
== other
.is_static
990 && kind
== other
.kind
);
994 const bool is_static
;
995 const unit_kind kind
;
998 /* Provide std::unordered_map::hasher for index_key. */
999 class index_key_hasher
1003 operator () (const index_key
&key
) const
1005 return (std::hash
<int>() (key
.dwarf_tag
) << 1) | key
.is_static
;
1009 /* Parameters of one symbol entry. */
1013 const int dwarf_tag
, cu_index
;
1014 const bool is_static
;
1015 const unit_kind kind
;
1017 symbol_value (int dwarf_tag_
, int cu_index_
, bool is_static_
,
1019 : dwarf_tag (dwarf_tag_
), cu_index (cu_index_
), is_static (is_static_
),
1024 operator< (const symbol_value
&other
) const
1044 /* Abstract base class to unify DWARF-32 and DWARF-64 name table
1049 const bfd_endian dwarf5_byte_order
;
1051 explicit offset_vec (bfd_endian dwarf5_byte_order_
)
1052 : dwarf5_byte_order (dwarf5_byte_order_
)
1055 /* Call std::vector::reserve for NELEM elements. */
1056 virtual void reserve (size_t nelem
) = 0;
1058 /* Call std::vector::push_back with store_unsigned_integer byte
1059 reordering for ELEM. */
1060 virtual void push_back_reorder (size_t elem
) = 0;
1062 /* Return expected output size in bytes. */
1063 virtual size_t bytes () const = 0;
1065 /* Write name table to FILE. */
1066 virtual void file_write (FILE *file
) const = 0;
1069 /* Template to unify DWARF-32 and DWARF-64 output. */
1070 template<typename OffsetSize
>
1071 class offset_vec_tmpl
: public offset_vec
1074 explicit offset_vec_tmpl (bfd_endian dwarf5_byte_order_
)
1075 : offset_vec (dwarf5_byte_order_
)
1078 /* Implement offset_vec::reserve. */
1079 void reserve (size_t nelem
) override
1081 m_vec
.reserve (nelem
);
1084 /* Implement offset_vec::push_back_reorder. */
1085 void push_back_reorder (size_t elem
) override
1087 m_vec
.push_back (elem
);
1088 /* Check for overflow. */
1089 gdb_assert (m_vec
.back () == elem
);
1090 store_unsigned_integer (reinterpret_cast<gdb_byte
*> (&m_vec
.back ()),
1091 sizeof (m_vec
.back ()), dwarf5_byte_order
, elem
);
1094 /* Implement offset_vec::bytes. */
1095 size_t bytes () const override
1097 return m_vec
.size () * sizeof (m_vec
[0]);
1100 /* Implement offset_vec::file_write. */
1101 void file_write (FILE *file
) const override
1103 ::file_write (file
, m_vec
);
1107 std::vector
<OffsetSize
> m_vec
;
1110 /* Base class to unify DWARF-32 and DWARF-64 .debug_names output
1111 respecting name table width. */
1115 offset_vec
&name_table_string_offs
, &name_table_entry_offs
;
1117 dwarf (offset_vec
&name_table_string_offs_
,
1118 offset_vec
&name_table_entry_offs_
)
1119 : name_table_string_offs (name_table_string_offs_
),
1120 name_table_entry_offs (name_table_entry_offs_
)
1125 /* Template to unify DWARF-32 and DWARF-64 .debug_names output
1126 respecting name table width. */
1127 template<typename OffsetSize
>
1128 class dwarf_tmpl
: public dwarf
1131 explicit dwarf_tmpl (bfd_endian dwarf5_byte_order_
)
1132 : dwarf (m_name_table_string_offs
, m_name_table_entry_offs
),
1133 m_name_table_string_offs (dwarf5_byte_order_
),
1134 m_name_table_entry_offs (dwarf5_byte_order_
)
1138 offset_vec_tmpl
<OffsetSize
> m_name_table_string_offs
;
1139 offset_vec_tmpl
<OffsetSize
> m_name_table_entry_offs
;
1142 /* Store value of each symbol. */
1143 std::unordered_map
<c_str_view
, std::set
<symbol_value
>, c_str_view_hasher
>
1144 m_name_to_value_set
;
1146 /* Tables of DWARF-5 .debug_names. They are in object file byte
1148 std::vector
<uint32_t> m_bucket_table
;
1149 std::vector
<uint32_t> m_hash_table
;
1151 const bfd_endian m_dwarf5_byte_order
;
1152 dwarf_tmpl
<uint32_t> m_dwarf32
;
1153 dwarf_tmpl
<uint64_t> m_dwarf64
;
1155 offset_vec
&m_name_table_string_offs
, &m_name_table_entry_offs
;
1156 debug_str_lookup m_debugstrlookup
;
1158 /* Map each used .debug_names abbreviation tag parameter to its
1160 std::unordered_map
<index_key
, int, index_key_hasher
> m_indexkey_to_idx
;
1162 /* Next unused .debug_names abbreviation tag for
1163 m_indexkey_to_idx. */
1166 /* .debug_names abbreviation table. */
1167 data_buf m_abbrev_table
;
1169 /* .debug_names entry pool. */
1170 data_buf m_entry_pool
;
1172 /* Temporary storage for Ada names. */
1173 auto_obstack m_string_obstack
;
1175 cu_index_map m_cu_index_htab
;
1178 /* Return iff any of the needed offsets does not fit into 32-bit
1179 .debug_names section. */
1182 check_dwarf64_offsets (dwarf2_per_bfd
*per_bfd
)
1184 for (const auto &per_cu
: per_bfd
->all_units
)
1186 if (to_underlying (per_cu
->sect_off
)
1187 >= (static_cast<uint64_t> (1) << 32))
1193 /* Assert that FILE's size is EXPECTED_SIZE. Assumes file's seek
1194 position is at the end of the file. */
1197 assert_file_size (FILE *file
, size_t expected_size
)
1199 const auto file_size
= ftell (file
);
1200 if (file_size
== -1)
1201 perror_with_name (("ftell"));
1202 gdb_assert (file_size
== expected_size
);
1205 /* Write a gdb index file to OUT_FILE from all the sections passed as
1209 write_gdbindex_1 (FILE *out_file
,
1210 const data_buf
&cu_list
,
1211 const data_buf
&types_cu_list
,
1212 const data_buf
&addr_vec
,
1213 const data_buf
&symtab_vec
,
1214 const data_buf
&constant_pool
,
1215 const data_buf
&shortcuts
)
1218 const offset_type size_of_header
= 7 * sizeof (offset_type
);
1219 uint64_t total_len
= size_of_header
;
1221 /* The version number. */
1222 contents
.append_offset (9);
1224 /* The offset of the CU list from the start of the file. */
1225 contents
.append_offset (total_len
);
1226 total_len
+= cu_list
.size ();
1228 /* The offset of the types CU list from the start of the file. */
1229 contents
.append_offset (total_len
);
1230 total_len
+= types_cu_list
.size ();
1232 /* The offset of the address table from the start of the file. */
1233 contents
.append_offset (total_len
);
1234 total_len
+= addr_vec
.size ();
1236 /* The offset of the symbol table from the start of the file. */
1237 contents
.append_offset (total_len
);
1238 total_len
+= symtab_vec
.size ();
1240 /* The offset of the shortcut table from the start of the file. */
1241 contents
.append_offset (total_len
);
1242 total_len
+= shortcuts
.size ();
1244 /* The offset of the constant pool from the start of the file. */
1245 contents
.append_offset (total_len
);
1246 total_len
+= constant_pool
.size ();
1248 gdb_assert (contents
.size () == size_of_header
);
1250 /* The maximum size of an index file is limited by the maximum value
1251 capable of being represented by 'offset_type'. Throw an error if
1252 that length has been exceeded. */
1253 size_t max_size
= ~(offset_type
) 0;
1254 if (total_len
> max_size
)
1255 error (_("gdb-index maximum file size of %zu exceeded"), max_size
);
1257 if (out_file
== nullptr)
1260 contents
.file_write (out_file
);
1261 cu_list
.file_write (out_file
);
1262 types_cu_list
.file_write (out_file
);
1263 addr_vec
.file_write (out_file
);
1264 symtab_vec
.file_write (out_file
);
1265 shortcuts
.file_write (out_file
);
1266 constant_pool
.file_write (out_file
);
1268 assert_file_size (out_file
, total_len
);
1271 /* Write the contents of the internal "cooked" index. */
1274 write_cooked_index (cooked_index
*table
,
1275 const cu_index_map
&cu_index_htab
,
1276 struct mapped_symtab
*symtab
)
1278 for (const cooked_index_entry
*entry
: table
->all_entries ())
1280 const auto it
= cu_index_htab
.find (entry
->per_cu
);
1281 gdb_assert (it
!= cu_index_htab
.cend ());
1283 const char *name
= entry
->full_name (symtab
->obstack ());
1285 if (entry
->per_cu
->lang () == language_ada
)
1287 /* In order for the index to work when read back into
1288 gdb, it has to use the encoded name, with any
1289 suffixes stripped. */
1290 std::string encoded
= ada_encode (name
, false);
1291 name
= obstack_strdup (symtab
->obstack (), encoded
.c_str ());
1293 else if (entry
->per_cu
->lang () == language_cplus
1294 && (entry
->flags
& IS_LINKAGE
) != 0)
1296 /* GDB never put C++ linkage names into .gdb_index. The
1297 theory here is that a linkage name will normally be in
1298 the minimal symbols anyway, so including it in the index
1299 is usually redundant -- and the cases where it would not
1300 be redundant are rare and not worth supporting. */
1303 else if ((entry
->flags
& IS_TYPE_DECLARATION
) != 0)
1305 /* Don't add type declarations to the index. */
1309 gdb_index_symbol_kind kind
;
1310 if (entry
->tag
== DW_TAG_subprogram
)
1311 kind
= GDB_INDEX_SYMBOL_KIND_FUNCTION
;
1312 else if (entry
->tag
== DW_TAG_variable
1313 || entry
->tag
== DW_TAG_constant
1314 || entry
->tag
== DW_TAG_enumerator
)
1315 kind
= GDB_INDEX_SYMBOL_KIND_VARIABLE
;
1316 else if (entry
->tag
== DW_TAG_module
1317 || entry
->tag
== DW_TAG_common_block
)
1318 kind
= GDB_INDEX_SYMBOL_KIND_OTHER
;
1320 kind
= GDB_INDEX_SYMBOL_KIND_TYPE
;
1322 symtab
->add_index_entry (name
, (entry
->flags
& IS_STATIC
) != 0,
1327 /* Write shortcut information. */
1330 write_shortcuts_table (cooked_index
*table
, data_buf
&shortcuts
,
1333 const auto main_info
= table
->get_main ();
1334 size_t main_name_offset
= 0;
1335 dwarf_source_language dw_lang
= (dwarf_source_language
) 0;
1337 if (main_info
!= nullptr)
1339 dw_lang
= main_info
->per_cu
->dw_lang ();
1343 auto_obstack obstack
;
1344 const auto main_name
= main_info
->full_name (&obstack
, true);
1346 main_name_offset
= cpool
.size ();
1347 cpool
.append_cstr0 (main_name
);
1351 shortcuts
.append_offset (dw_lang
);
1352 shortcuts
.append_offset (main_name_offset
);
1355 /* Write contents of a .gdb_index section for OBJFILE into OUT_FILE.
1356 If OBJFILE has an associated dwz file, write contents of a .gdb_index
1357 section for that dwz file into DWZ_OUT_FILE. If OBJFILE does not have an
1358 associated dwz file, DWZ_OUT_FILE must be NULL. */
1361 write_gdbindex (dwarf2_per_bfd
*per_bfd
, cooked_index
*table
,
1362 FILE *out_file
, FILE *dwz_out_file
)
1364 mapped_symtab symtab
;
1365 data_buf objfile_cu_list
;
1366 data_buf dwz_cu_list
;
1368 /* While we're scanning CU's create a table that maps a dwarf2_per_cu_data
1369 (which is what addrmap records) to its index (which is what is recorded
1370 in the index file). This will later be needed to write the address
1372 cu_index_map cu_index_htab
;
1373 cu_index_htab
.reserve (per_bfd
->all_units
.size ());
1375 /* Store out the .debug_type CUs, if any. */
1376 data_buf types_cu_list
;
1378 /* The CU list is already sorted, so we don't need to do additional
1382 for (int i
= 0; i
< per_bfd
->all_units
.size (); ++i
)
1384 dwarf2_per_cu_data
*per_cu
= per_bfd
->all_units
[i
].get ();
1386 const auto insertpair
= cu_index_htab
.emplace (per_cu
, counter
);
1387 gdb_assert (insertpair
.second
);
1389 /* See enhancement PR symtab/30838. */
1390 gdb_assert (!(per_cu
->is_dwz
&& per_cu
->is_debug_types
));
1392 /* The all_units list contains CUs read from the objfile as well as
1393 from the eventual dwz file. We need to place the entry in the
1394 corresponding index. */
1395 data_buf
&cu_list
= (per_cu
->is_debug_types
1397 : per_cu
->is_dwz
? dwz_cu_list
: objfile_cu_list
);
1398 cu_list
.append_uint (8, BFD_ENDIAN_LITTLE
,
1399 to_underlying (per_cu
->sect_off
));
1400 if (per_cu
->is_debug_types
)
1402 signatured_type
*sig_type
= (signatured_type
*) per_cu
;
1403 cu_list
.append_uint (8, BFD_ENDIAN_LITTLE
,
1404 to_underlying (sig_type
->type_offset_in_tu
));
1405 cu_list
.append_uint (8, BFD_ENDIAN_LITTLE
,
1406 sig_type
->signature
);
1409 cu_list
.append_uint (8, BFD_ENDIAN_LITTLE
, per_cu
->length ());
1414 write_cooked_index (table
, cu_index_htab
, &symtab
);
1416 /* Dump the address map. */
1418 for (auto map
: table
->get_addrmaps ())
1419 write_address_map (map
, addr_vec
, cu_index_htab
);
1421 /* Ensure symbol hash is built domestically. */
1424 /* Now that we've processed all symbols we can shrink their cu_indices
1428 data_buf symtab_vec
, constant_pool
;
1430 write_hash_table (&symtab
, symtab_vec
, constant_pool
);
1433 write_shortcuts_table (table
, shortcuts
, constant_pool
);
1435 write_gdbindex_1 (out_file
, objfile_cu_list
, types_cu_list
, addr_vec
,
1436 symtab_vec
, constant_pool
, shortcuts
);
1438 if (dwz_out_file
!= NULL
)
1439 write_gdbindex_1 (dwz_out_file
, dwz_cu_list
, {}, {}, {}, {}, {});
1441 gdb_assert (dwz_cu_list
.empty ());
1444 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
1445 static const gdb_byte dwarf5_gdb_augmentation
[] = { 'G', 'D', 'B', 0 };
1447 /* Write a new .debug_names section for OBJFILE into OUT_FILE, write
1448 needed addition to .debug_str section to OUT_FILE_STR. Return how
1449 many bytes were expected to be written into OUT_FILE. */
1452 write_debug_names (dwarf2_per_bfd
*per_bfd
, cooked_index
*table
,
1453 FILE *out_file
, FILE *out_file_str
)
1455 const bool dwarf5_is_dwarf64
= check_dwarf64_offsets (per_bfd
);
1456 const enum bfd_endian dwarf5_byte_order
1457 = bfd_big_endian (per_bfd
->obfd
) ? BFD_ENDIAN_BIG
: BFD_ENDIAN_LITTLE
;
1459 /* The CU list is already sorted, so we don't need to do additional
1460 work here. Also, the debug_types entries do not appear in
1461 all_units, but only in their own hash table. */
1463 data_buf types_cu_list
;
1464 debug_names
nametable (per_bfd
, dwarf5_is_dwarf64
, dwarf5_byte_order
);
1466 int types_counter
= 0;
1467 for (int i
= 0; i
< per_bfd
->all_units
.size (); ++i
)
1469 dwarf2_per_cu_data
*per_cu
= per_bfd
->all_units
[i
].get ();
1471 int &this_counter
= per_cu
->is_debug_types
? types_counter
: counter
;
1472 data_buf
&this_list
= per_cu
->is_debug_types
? types_cu_list
: cu_list
;
1474 nametable
.add_cu (per_cu
, this_counter
);
1475 this_list
.append_uint (nametable
.dwarf5_offset_size (),
1477 to_underlying (per_cu
->sect_off
));
1481 /* Verify that all units are represented. */
1482 gdb_assert (counter
== per_bfd
->all_comp_units
.size ());
1483 gdb_assert (types_counter
== per_bfd
->all_type_units
.size ());
1485 for (const cooked_index_entry
*entry
: table
->all_entries ())
1486 nametable
.insert (entry
);
1490 /* No addr_vec - DWARF-5 uses .debug_aranges generated by GCC. */
1492 const offset_type bytes_of_header
1493 = ((dwarf5_is_dwarf64
? 12 : 4)
1495 + sizeof (dwarf5_gdb_augmentation
));
1496 size_t expected_bytes
= 0;
1497 expected_bytes
+= bytes_of_header
;
1498 expected_bytes
+= cu_list
.size ();
1499 expected_bytes
+= types_cu_list
.size ();
1500 expected_bytes
+= nametable
.bytes ();
1503 if (!dwarf5_is_dwarf64
)
1505 const uint64_t size64
= expected_bytes
- 4;
1506 gdb_assert (size64
< 0xfffffff0);
1507 header
.append_uint (4, dwarf5_byte_order
, size64
);
1511 header
.append_uint (4, dwarf5_byte_order
, 0xffffffff);
1512 header
.append_uint (8, dwarf5_byte_order
, expected_bytes
- 12);
1515 /* The version number. */
1516 header
.append_uint (2, dwarf5_byte_order
, 5);
1519 header
.append_uint (2, dwarf5_byte_order
, 0);
1521 /* comp_unit_count - The number of CUs in the CU list. */
1522 header
.append_uint (4, dwarf5_byte_order
, counter
);
1524 /* local_type_unit_count - The number of TUs in the local TU
1526 header
.append_uint (4, dwarf5_byte_order
, types_counter
);
1528 /* foreign_type_unit_count - The number of TUs in the foreign TU
1530 header
.append_uint (4, dwarf5_byte_order
, 0);
1532 /* bucket_count - The number of hash buckets in the hash lookup
1534 header
.append_uint (4, dwarf5_byte_order
, nametable
.bucket_count ());
1536 /* name_count - The number of unique names in the index. */
1537 header
.append_uint (4, dwarf5_byte_order
, nametable
.name_count ());
1539 /* abbrev_table_size - The size in bytes of the abbreviations
1541 header
.append_uint (4, dwarf5_byte_order
, nametable
.abbrev_table_bytes ());
1543 /* augmentation_string_size - The size in bytes of the augmentation
1544 string. This value is rounded up to a multiple of 4. */
1545 static_assert (sizeof (dwarf5_gdb_augmentation
) % 4 == 0, "");
1546 header
.append_uint (4, dwarf5_byte_order
, sizeof (dwarf5_gdb_augmentation
));
1547 header
.append_array (dwarf5_gdb_augmentation
);
1549 gdb_assert (header
.size () == bytes_of_header
);
1551 header
.file_write (out_file
);
1552 cu_list
.file_write (out_file
);
1553 types_cu_list
.file_write (out_file
);
1554 nametable
.file_write (out_file
, out_file_str
);
1556 assert_file_size (out_file
, expected_bytes
);
1559 /* This represents an index file being written (work-in-progress).
1561 The data is initially written to a temporary file. When the finalize method
1562 is called, the file is closed and moved to its final location.
1564 On failure (if this object is being destroyed with having called finalize),
1565 the temporary file is closed and deleted. */
1567 struct index_wip_file
1569 index_wip_file (const char *dir
, const char *basename
,
1572 /* Validate DIR is a valid directory. */
1574 if (stat (dir
, &buf
) == -1)
1575 perror_with_name (string_printf (_("`%s'"), dir
).c_str ());
1576 if ((buf
.st_mode
& S_IFDIR
) != S_IFDIR
)
1577 error (_("`%s': Is not a directory."), dir
);
1579 filename
= (std::string (dir
) + SLASH_STRING
+ basename
1582 filename_temp
= make_temp_filename (filename
);
1584 scoped_fd out_file_fd
= gdb_mkostemp_cloexec (filename_temp
.data (),
1586 if (out_file_fd
.get () == -1)
1587 perror_with_name (string_printf (_("couldn't open `%s'"),
1588 filename_temp
.data ()).c_str ());
1590 out_file
= out_file_fd
.to_file ("wb");
1592 if (out_file
== nullptr)
1593 error (_("Can't open `%s' for writing"), filename_temp
.data ());
1595 unlink_file
.emplace (filename_temp
.data ());
1600 /* We want to keep the file. */
1601 unlink_file
->keep ();
1603 /* Close and move the str file in place. */
1604 unlink_file
.reset ();
1605 if (rename (filename_temp
.data (), filename
.c_str ()) != 0)
1606 perror_with_name (("rename"));
1609 std::string filename
;
1610 gdb::char_vector filename_temp
;
1612 /* Order matters here; we want FILE to be closed before
1613 FILENAME_TEMP is unlinked, because on MS-Windows one cannot
1614 delete a file that is still open. So, we wrap the unlinker in an
1615 optional and emplace it once we know the file name. */
1616 std::optional
<gdb::unlinker
> unlink_file
;
1618 gdb_file_up out_file
;
1621 /* See dwarf-index-write.h. */
1624 write_dwarf_index (dwarf2_per_bfd
*per_bfd
, const char *dir
,
1625 const char *basename
, const char *dwz_basename
,
1626 dw_index_kind index_kind
)
1628 if (per_bfd
->index_table
== nullptr)
1629 error (_("No debugging symbols"));
1630 cooked_index
*table
= per_bfd
->index_table
->index_for_writing ();
1632 if (per_bfd
->types
.size () > 1)
1633 error (_("Cannot make an index when the file has multiple .debug_types sections"));
1635 const char *index_suffix
= (index_kind
== dw_index_kind::DEBUG_NAMES
1636 ? INDEX5_SUFFIX
: INDEX4_SUFFIX
);
1638 index_wip_file
objfile_index_wip (dir
, basename
, index_suffix
);
1639 std::optional
<index_wip_file
> dwz_index_wip
;
1641 if (dwz_basename
!= NULL
)
1642 dwz_index_wip
.emplace (dir
, dwz_basename
, index_suffix
);
1644 if (index_kind
== dw_index_kind::DEBUG_NAMES
)
1646 index_wip_file
str_wip_file (dir
, basename
, DEBUG_STR_SUFFIX
);
1648 write_debug_names (per_bfd
, table
, objfile_index_wip
.out_file
.get (),
1649 str_wip_file
.out_file
.get ());
1651 str_wip_file
.finalize ();
1654 write_gdbindex (per_bfd
, table
, objfile_index_wip
.out_file
.get (),
1655 (dwz_index_wip
.has_value ()
1656 ? dwz_index_wip
->out_file
.get () : NULL
));
1658 objfile_index_wip
.finalize ();
1660 if (dwz_index_wip
.has_value ())
1661 dwz_index_wip
->finalize ();
1664 /* Options structure for the 'save gdb-index' command. */
1666 struct save_gdb_index_options
1668 bool dwarf_5
= false;
1671 /* The option_def list for the 'save gdb-index' command. */
1673 static const gdb::option::option_def save_gdb_index_options_defs
[] = {
1674 gdb::option::boolean_option_def
<save_gdb_index_options
> {
1676 [] (save_gdb_index_options
*opt
) { return &opt
->dwarf_5
; },
1677 nullptr, /* show_cmd_cb */
1678 nullptr /* set_doc */
1682 /* Create an options_def_group for the 'save gdb-index' command. */
1684 static gdb::option::option_def_group
1685 make_gdb_save_index_options_def_group (save_gdb_index_options
*opts
)
1687 return {{save_gdb_index_options_defs
}, opts
};
1690 /* Completer for the "save gdb-index" command. */
1693 gdb_save_index_cmd_completer (struct cmd_list_element
*ignore
,
1694 completion_tracker
&tracker
,
1695 const char *text
, const char *word
)
1697 auto grp
= make_gdb_save_index_options_def_group (nullptr);
1698 if (gdb::option::complete_options
1699 (tracker
, &text
, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND
, grp
))
1702 word
= advance_to_filename_complete_word_point (tracker
, text
);
1703 filename_completer (ignore
, tracker
, text
, word
);
1706 /* Implementation of the `save gdb-index' command.
1708 Note that the .gdb_index file format used by this command is
1709 documented in the GDB manual. Any changes here must be documented
1713 save_gdb_index_command (const char *args
, int from_tty
)
1715 save_gdb_index_options opts
;
1716 const auto group
= make_gdb_save_index_options_def_group (&opts
);
1717 gdb::option::process_options
1718 (&args
, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND
, group
);
1720 if (args
== nullptr || *args
== '\0')
1721 error (_("usage: save gdb-index [-dwarf-5] DIRECTORY"));
1723 std::string
directory (gdb_tilde_expand (args
));
1724 dw_index_kind index_kind
1725 = (opts
.dwarf_5
? dw_index_kind::DEBUG_NAMES
: dw_index_kind::GDB_INDEX
);
1727 for (objfile
*objfile
: current_program_space
->objfiles ())
1729 /* If the objfile does not correspond to an actual file, skip it. */
1730 if ((objfile
->flags
& OBJF_NOT_FILENAME
) != 0)
1733 dwarf2_per_objfile
*per_objfile
= get_dwarf2_per_objfile (objfile
);
1735 if (per_objfile
!= NULL
)
1739 const char *basename
= lbasename (objfile_name (objfile
));
1740 const dwz_file
*dwz
= dwarf2_get_dwz_file (per_objfile
->per_bfd
);
1741 const char *dwz_basename
= NULL
;
1744 dwz_basename
= lbasename (dwz
->filename ());
1746 write_dwarf_index (per_objfile
->per_bfd
, directory
.c_str (),
1747 basename
, dwz_basename
, index_kind
);
1749 catch (const gdb_exception_error
&except
)
1751 exception_fprintf (gdb_stderr
, except
,
1752 _("Error while writing index for `%s': "),
1753 objfile_name (objfile
));
1761 #include "gdbsupport/selftest.h"
1763 namespace selftests
{
1765 class pretend_data_buf
: public data_buf
1768 /* Set the pretend size. */
1769 void set_pretend_size (size_t s
) {
1773 /* Override size method of data_buf, returning the pretend size instead. */
1774 size_t size () const override
{
1775 return m_pretend_size
;
1779 size_t m_pretend_size
= 0;
1785 pretend_data_buf cu_list
;
1786 pretend_data_buf types_cu_list
;
1787 pretend_data_buf addr_vec
;
1788 pretend_data_buf symtab_vec
;
1789 pretend_data_buf constant_pool
;
1790 pretend_data_buf short_cuts
;
1792 const size_t size_of_header
= 7 * sizeof (offset_type
);
1794 /* Test that an overly large index will throw an error. */
1795 symtab_vec
.set_pretend_size (~(offset_type
)0 - size_of_header
);
1796 constant_pool
.set_pretend_size (1);
1798 bool saw_exception
= false;
1801 write_gdbindex_1 (nullptr, cu_list
, types_cu_list
, addr_vec
,
1802 symtab_vec
, constant_pool
, short_cuts
);
1804 catch (const gdb_exception_error
&e
)
1806 SELF_CHECK (e
.reason
== RETURN_ERROR
);
1807 SELF_CHECK (e
.error
== GENERIC_ERROR
);
1808 SELF_CHECK (e
.message
->find (_("gdb-index maximum file size of"))
1809 != std::string::npos
);
1810 SELF_CHECK (e
.message
->find (_("exceeded")) != std::string::npos
);
1811 saw_exception
= true;
1813 SELF_CHECK (saw_exception
);
1815 /* Test that the largest possible index will not throw an error. */
1816 constant_pool
.set_pretend_size (0);
1818 saw_exception
= false;
1821 write_gdbindex_1 (nullptr, cu_list
, types_cu_list
, addr_vec
,
1822 symtab_vec
, constant_pool
, short_cuts
);
1824 catch (const gdb_exception_error
&e
)
1826 saw_exception
= true;
1828 SELF_CHECK (!saw_exception
);
1831 } /* selftests namespace. */
1834 void _initialize_dwarf_index_write ();
1836 _initialize_dwarf_index_write ()
1839 selftests::register_test ("gdb_index", selftests::gdb_index
);
1842 cmd_list_element
*c
= add_cmd ("gdb-index", class_files
,
1843 save_gdb_index_command
, _("\
1844 Save a gdb-index file.\n\
1845 Usage: save gdb-index [-dwarf-5] DIRECTORY\n\
1847 No options create one file with .gdb-index extension for pre-DWARF-5\n\
1848 compatible .gdb_index section. With -dwarf-5 creates two files with\n\
1849 extension .debug_names and .debug_str for DWARF-5 .debug_names section."),
1851 set_cmd_completer_handle_brkchars (c
, gdb_save_index_cmd_completer
);