Correct hpux-core.c thread_section_p signature
[binutils-gdb.git] / gdb / dwarf2 / index-write.c
blob1008d19e1692c8774ebb9c4ef987a49fd1fda179
1 /* DWARF index writing support for GDB.
3 Copyright (C) 1994-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/>. */
21 #include "dwarf2/index-write.h"
23 #include "addrmap.h"
24 #include "cli/cli-decode.h"
25 #include "exceptions.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 "dwarf2/index-common.h"
32 #include "dwarf2/cooked-index.h"
33 #include "dwarf2.h"
34 #include "dwarf2/read.h"
35 #include "dwarf2/dwz.h"
36 #include "gdb/gdb-index.h"
37 #include "cli/cli-cmds.h"
38 #include "objfiles.h"
39 #include "ada-lang.h"
40 #include "dwarf2/tag.h"
41 #include "gdbsupport/gdb_tilde_expand.h"
42 #include "dwarf2/read-debug-names.h"
44 #include <algorithm>
45 #include <cmath>
46 #include <map>
47 #include <unordered_map>
48 #include <unordered_set>
50 /* Ensure only legit values are used. */
51 #define DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE(cu_index, value) \
52 do { \
53 gdb_assert ((unsigned int) (value) <= 1); \
54 GDB_INDEX_SYMBOL_STATIC_SET_VALUE((cu_index), (value)); \
55 } while (0)
57 /* Ensure only legit values are used. */
58 #define DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE(cu_index, value) \
59 do { \
60 gdb_assert ((value) >= GDB_INDEX_SYMBOL_KIND_TYPE \
61 && (value) <= GDB_INDEX_SYMBOL_KIND_OTHER); \
62 GDB_INDEX_SYMBOL_KIND_SET_VALUE((cu_index), (value)); \
63 } while (0)
65 /* Ensure we don't use more than the allotted number of bits for the CU. */
66 #define DW2_GDB_INDEX_CU_SET_VALUE(cu_index, value) \
67 do { \
68 gdb_assert (((value) & ~GDB_INDEX_CU_MASK) == 0); \
69 GDB_INDEX_CU_SET_VALUE((cu_index), (value)); \
70 } while (0)
72 /* The "save gdb-index" command. */
74 /* Write SIZE bytes from the buffer pointed to by DATA to FILE, with
75 error checking. */
77 static void
78 file_write (FILE *file, const void *data, size_t size)
80 if (fwrite (data, 1, size, file) != size)
81 error (_("couldn't data write to file"));
84 /* Write the contents of VEC to FILE, with error checking. */
86 template<typename Elem, typename Alloc>
87 static void
88 file_write (FILE *file, const std::vector<Elem, Alloc> &vec)
90 if (!vec.empty ())
91 file_write (file, vec.data (), vec.size () * sizeof (vec[0]));
94 /* In-memory buffer to prepare data to be written later to a file. */
95 class data_buf
97 public:
98 /* Copy ARRAY to the end of the buffer. */
99 void append_array (gdb::array_view<const gdb_byte> array)
101 std::copy (array.begin (), array.end (), grow (array.size ()));
104 /* Copy CSTR (a zero-terminated string) to the end of buffer. The
105 terminating zero is appended too. */
106 void append_cstr0 (const char *cstr)
108 const size_t size = strlen (cstr) + 1;
109 std::copy (cstr, cstr + size, grow (size));
112 /* Store INPUT as ULEB128 to the end of buffer. */
113 void append_unsigned_leb128 (ULONGEST input)
115 for (;;)
117 gdb_byte output = input & 0x7f;
118 input >>= 7;
119 if (input)
120 output |= 0x80;
121 m_vec.push_back (output);
122 if (input == 0)
123 break;
127 /* Accept a host-format integer in VAL and append it to the buffer
128 as a target-format integer which is LEN bytes long. */
129 void append_uint (size_t len, bfd_endian byte_order, ULONGEST val)
131 ::store_unsigned_integer (grow (len), len, byte_order, val);
134 /* Copy VALUE to the end of the buffer, little-endian. */
135 void append_offset (offset_type value)
137 append_uint (sizeof (value), BFD_ENDIAN_LITTLE, value);
140 /* Return the size of the buffer. */
141 virtual size_t size () const
143 return m_vec.size ();
146 /* Return true iff the buffer is empty. */
147 bool empty () const
149 return m_vec.empty ();
152 /* Write the buffer to FILE. */
153 void file_write (FILE *file) const
155 ::file_write (file, m_vec);
158 private:
159 /* Grow SIZE bytes at the end of the buffer. Returns a pointer to
160 the start of the new block. */
161 gdb_byte *grow (size_t size)
163 m_vec.resize (m_vec.size () + size);
164 return &*(m_vec.end () - size);
167 gdb::byte_vector m_vec;
170 /* An entry in the symbol table. */
171 struct symtab_index_entry
173 /* The name of the symbol. */
174 const char *name;
175 /* The offset of the name in the constant pool. */
176 offset_type index_offset;
177 /* A sorted vector of the indices of all the CUs that hold an object
178 of this name. */
179 std::vector<offset_type> cu_indices;
181 /* Minimize CU_INDICES, sorting them and removing duplicates as
182 appropriate. */
183 void minimize ();
186 /* The symbol table. This is a power-of-2-sized hash table. */
187 struct mapped_symtab
189 mapped_symtab ()
191 m_data.resize (1024);
194 /* If there are no elements in the symbol table, then reduce the table
195 size to zero. Otherwise call symtab_index_entry::minimize each entry
196 in the symbol table. */
198 void minimize ()
200 if (m_element_count == 0)
201 m_data.resize (0);
203 for (symtab_index_entry &item : m_data)
204 item.minimize ();
207 /* Add an entry to SYMTAB. NAME is the name of the symbol. CU_INDEX is
208 the index of the CU in which the symbol appears. IS_STATIC is one if
209 the symbol is static, otherwise zero (global). */
211 void add_index_entry (const char *name, int is_static,
212 gdb_index_symbol_kind kind, offset_type cu_index);
214 /* When entries are originally added into the data hash the order will
215 vary based on the number of worker threads GDB is configured to use.
216 This function will rebuild the hash such that the final layout will be
217 deterministic regardless of the number of worker threads used. */
219 void sort ();
221 /* Access the obstack. */
222 struct obstack *obstack ()
223 { return &m_string_obstack; }
225 private:
227 /* Find a slot in SYMTAB for the symbol NAME. Returns a reference to
228 the slot.
230 Function is used only during write_hash_table so no index format
231 backward compatibility is needed. */
233 symtab_index_entry &find_slot (const char *name);
235 /* Expand SYMTAB's hash table. */
237 void hash_expand ();
239 /* Return true if the hash table in data needs to grow. */
241 bool hash_needs_expanding () const
242 { return 4 * m_element_count / 3 >= m_data.size (); }
244 /* A vector that is used as a hash table. */
245 std::vector<symtab_index_entry> m_data;
247 /* The number of elements stored in the m_data hash. */
248 offset_type m_element_count = 0;
250 /* Temporary storage for names. */
251 auto_obstack m_string_obstack;
253 public:
254 using iterator = decltype (m_data)::iterator;
255 using const_iterator = decltype (m_data)::const_iterator;
257 iterator begin ()
258 { return m_data.begin (); }
260 iterator end ()
261 { return m_data.end (); }
263 const_iterator cbegin ()
264 { return m_data.cbegin (); }
266 const_iterator cend ()
267 { return m_data.cend (); }
270 /* See class definition. */
272 symtab_index_entry &
273 mapped_symtab::find_slot (const char *name)
275 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
277 index = hash & (m_data.size () - 1);
278 step = ((hash * 17) & (m_data.size () - 1)) | 1;
280 for (;;)
282 if (m_data[index].name == NULL
283 || strcmp (name, m_data[index].name) == 0)
284 return m_data[index];
285 index = (index + step) & (m_data.size () - 1);
289 /* See class definition. */
291 void
292 mapped_symtab::hash_expand ()
294 auto old_entries = std::move (m_data);
296 gdb_assert (m_data.size () == 0);
297 m_data.resize (old_entries.size () * 2);
299 for (auto &it : old_entries)
300 if (it.name != NULL)
302 auto &ref = this->find_slot (it.name);
303 ref = std::move (it);
307 /* See mapped_symtab class declaration. */
309 void mapped_symtab::sort ()
311 /* Move contents out of this->data vector. */
312 std::vector<symtab_index_entry> original_data = std::move (m_data);
314 /* Restore the size of m_data, this will avoid having to expand the hash
315 table (and rehash all elements) when we reinsert after sorting.
316 However, we do reset the element count, this allows for some sanity
317 checking asserts during the reinsert phase. */
318 gdb_assert (m_data.size () == 0);
319 m_data.resize (original_data.size ());
320 m_element_count = 0;
322 /* Remove empty entries from ORIGINAL_DATA, this makes sorting quicker. */
323 auto it = std::remove_if (original_data.begin (), original_data.end (),
324 [] (const symtab_index_entry &entry) -> bool
326 return entry.name == nullptr;
328 original_data.erase (it, original_data.end ());
330 /* Sort the existing contents. */
331 std::sort (original_data.begin (), original_data.end (),
332 [] (const symtab_index_entry &a,
333 const symtab_index_entry &b) -> bool
335 /* Return true if A is before B. */
336 gdb_assert (a.name != nullptr);
337 gdb_assert (b.name != nullptr);
339 return strcmp (a.name, b.name) < 0;
342 /* Re-insert each item from the sorted list. */
343 for (auto &entry : original_data)
345 /* We know that ORIGINAL_DATA contains no duplicates, this data was
346 taken from a hash table that de-duplicated entries for us, so
347 count this as a new item.
349 As we retained the original size of m_data (see above) then we
350 should never need to grow m_data_ during this re-insertion phase,
351 assert that now. */
352 ++m_element_count;
353 gdb_assert (!this->hash_needs_expanding ());
355 /* Lookup a slot. */
356 symtab_index_entry &slot = this->find_slot (entry.name);
358 /* As discussed above, we should not find duplicates. */
359 gdb_assert (slot.name == nullptr);
361 /* Move this item into the slot we found. */
362 slot = std::move (entry);
366 /* See class definition. */
368 void
369 mapped_symtab::add_index_entry (const char *name, int is_static,
370 gdb_index_symbol_kind kind,
371 offset_type cu_index)
373 symtab_index_entry *slot = &this->find_slot (name);
374 if (slot->name == NULL)
376 /* This is a new element in the hash table. */
377 ++this->m_element_count;
379 /* We might need to grow the hash table. */
380 if (this->hash_needs_expanding ())
382 this->hash_expand ();
384 /* This element will have a different slot in the new table. */
385 slot = &this->find_slot (name);
387 /* But it should still be a new element in the hash table. */
388 gdb_assert (slot->name == nullptr);
391 slot->name = name;
392 /* index_offset is set later. */
395 offset_type cu_index_and_attrs = 0;
396 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
397 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
398 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
400 /* We don't want to record an index value twice as we want to avoid the
401 duplication.
402 We process all global symbols and then all static symbols
403 (which would allow us to avoid the duplication by only having to check
404 the last entry pushed), but a symbol could have multiple kinds in one CU.
405 To keep things simple we don't worry about the duplication here and
406 sort and uniquify the list after we've processed all symbols. */
407 slot->cu_indices.push_back (cu_index_and_attrs);
410 /* See symtab_index_entry. */
412 void
413 symtab_index_entry::minimize ()
415 if (name == nullptr || cu_indices.empty ())
416 return;
418 std::sort (cu_indices.begin (), cu_indices.end ());
419 auto from = std::unique (cu_indices.begin (), cu_indices.end ());
420 cu_indices.erase (from, cu_indices.end ());
422 /* We don't want to enter a type more than once, so
423 remove any such duplicates from the list as well. When doing
424 this, we want to keep the entry from the first CU -- but this is
425 implicit due to the sort. This choice is done because it's
426 similar to what gdb historically did for partial symbols. */
427 std::unordered_set<offset_type> seen;
428 from = std::remove_if (cu_indices.begin (), cu_indices.end (),
429 [&] (offset_type val)
431 gdb_index_symbol_kind kind = GDB_INDEX_SYMBOL_KIND_VALUE (val);
432 if (kind != GDB_INDEX_SYMBOL_KIND_TYPE)
433 return false;
435 val &= ~GDB_INDEX_CU_MASK;
436 return !seen.insert (val).second;
438 cu_indices.erase (from, cu_indices.end ());
441 /* A form of 'const char *' suitable for container keys. Only the
442 pointer is stored. The strings themselves are compared, not the
443 pointers. */
444 class c_str_view
446 public:
447 c_str_view (const char *cstr)
448 : m_cstr (cstr)
451 bool operator== (const c_str_view &other) const
453 return strcmp (m_cstr, other.m_cstr) == 0;
456 bool operator< (const c_str_view &other) const
458 return strcmp (m_cstr, other.m_cstr) < 0;
461 /* Return the underlying C string. Note, the returned string is
462 only a reference with lifetime of this object. */
463 const char *c_str () const
465 return m_cstr;
468 private:
469 friend class c_str_view_hasher;
470 const char *const m_cstr;
473 /* A std::unordered_map::hasher for c_str_view that uses the right
474 hash function for strings in a mapped index. */
475 class c_str_view_hasher
477 public:
478 size_t operator () (const c_str_view &x) const
480 return mapped_index_string_hash (INT_MAX, x.m_cstr);
484 /* A std::unordered_map::hasher for std::vector<>. */
485 template<typename T>
486 class vector_hasher
488 public:
489 size_t operator () (const std::vector<T> &key) const
491 return iterative_hash (key.data (),
492 sizeof (key.front ()) * key.size (), 0);
496 /* Write the mapped hash table SYMTAB to the data buffer OUTPUT, with
497 constant pool entries going into the data buffer CPOOL. */
499 static void
500 write_hash_table (mapped_symtab *symtab, data_buf &output, data_buf &cpool)
503 /* Elements are sorted vectors of the indices of all the CUs that
504 hold an object of this name. */
505 std::unordered_map<std::vector<offset_type>, offset_type,
506 vector_hasher<offset_type>>
507 symbol_hash_table;
509 /* We add all the index vectors to the constant pool first, to
510 ensure alignment is ok. */
511 for (symtab_index_entry &entry : *symtab)
513 if (entry.name == NULL)
514 continue;
515 gdb_assert (entry.index_offset == 0);
517 auto [iter, inserted]
518 = symbol_hash_table.try_emplace (entry.cu_indices,
519 cpool.size ());
520 entry.index_offset = iter->second;
521 if (inserted)
523 /* Newly inserted. */
524 cpool.append_offset (entry.cu_indices.size ());
525 for (const auto index : entry.cu_indices)
526 cpool.append_offset (index);
531 /* Now write out the hash table. */
532 std::unordered_map<c_str_view, offset_type, c_str_view_hasher> str_table;
533 for (const auto &entry : *symtab)
535 offset_type str_off, vec_off;
537 if (entry.name != NULL)
539 const auto insertpair = str_table.emplace (entry.name, cpool.size ());
540 if (insertpair.second)
541 cpool.append_cstr0 (entry.name);
542 str_off = insertpair.first->second;
543 vec_off = entry.index_offset;
545 else
547 /* While 0 is a valid constant pool index, it is not valid
548 to have 0 for both offsets. */
549 str_off = 0;
550 vec_off = 0;
553 output.append_offset (str_off);
554 output.append_offset (vec_off);
558 using cu_index_map
559 = std::unordered_map<const dwarf2_per_cu_data *, unsigned int>;
561 /* Helper struct for building the address table. */
562 struct addrmap_index_data
564 addrmap_index_data (data_buf &addr_vec_, cu_index_map &cu_index_htab_)
565 : addr_vec (addr_vec_),
566 cu_index_htab (cu_index_htab_)
569 data_buf &addr_vec;
570 cu_index_map &cu_index_htab;
572 int operator() (CORE_ADDR start_addr, const void *obj);
574 /* True if the previous_* fields are valid.
575 We can't write an entry until we see the next entry (since it is only then
576 that we know the end of the entry). */
577 bool previous_valid = false;
578 /* Index of the CU in the table of all CUs in the index file. */
579 unsigned int previous_cu_index = 0;
580 /* Start address of the CU. */
581 CORE_ADDR previous_cu_start = 0;
584 /* Write an address entry to ADDR_VEC. */
586 static void
587 add_address_entry (data_buf &addr_vec,
588 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
590 addr_vec.append_uint (8, BFD_ENDIAN_LITTLE, start);
591 addr_vec.append_uint (8, BFD_ENDIAN_LITTLE, end);
592 addr_vec.append_offset (cu_index);
595 /* Worker function for traversing an addrmap to build the address table. */
598 addrmap_index_data::operator() (CORE_ADDR start_addr, const void *obj)
600 const dwarf2_per_cu_data *per_cu
601 = static_cast<const dwarf2_per_cu_data *> (obj);
603 if (previous_valid)
604 add_address_entry (addr_vec,
605 previous_cu_start, start_addr,
606 previous_cu_index);
608 previous_cu_start = start_addr;
609 if (per_cu != NULL)
611 const auto it = cu_index_htab.find (per_cu);
612 gdb_assert (it != cu_index_htab.cend ());
613 previous_cu_index = it->second;
614 previous_valid = true;
616 else
617 previous_valid = false;
619 return 0;
622 /* Write PER_BFD's address map to ADDR_VEC.
623 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
624 in the index file. */
626 static void
627 write_address_map (const addrmap *addrmap, data_buf &addr_vec,
628 cu_index_map &cu_index_htab)
630 struct addrmap_index_data addrmap_index_data (addr_vec, cu_index_htab);
632 addrmap->foreach (addrmap_index_data);
634 /* It's highly unlikely the last entry (end address = 0xff...ff)
635 is valid, but we should still handle it.
636 The end address is recorded as the start of the next region, but that
637 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
638 anyway. */
639 if (addrmap_index_data.previous_valid)
640 add_address_entry (addr_vec,
641 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
642 addrmap_index_data.previous_cu_index);
645 /* DWARF-5 .debug_names builder. */
646 class debug_names
648 public:
649 debug_names (dwarf2_per_bfd *per_bfd, bool is_dwarf64,
650 bfd_endian dwarf5_byte_order)
651 : m_dwarf5_byte_order (dwarf5_byte_order),
652 m_dwarf32 (dwarf5_byte_order),
653 m_dwarf64 (dwarf5_byte_order),
654 m_dwarf (is_dwarf64
655 ? static_cast<dwarf &> (m_dwarf64)
656 : static_cast<dwarf &> (m_dwarf32)),
657 m_name_table_string_offs (m_dwarf.name_table_string_offs),
658 m_name_table_entry_offs (m_dwarf.name_table_entry_offs),
659 m_debugstrlookup (per_bfd)
662 int dwarf5_offset_size () const
664 const bool dwarf5_is_dwarf64 = &m_dwarf == &m_dwarf64;
665 return dwarf5_is_dwarf64 ? 8 : 4;
668 /* Is this symbol from DW_TAG_compile_unit or DW_TAG_type_unit? */
669 enum class unit_kind { cu, tu };
671 /* Insert one symbol. */
672 void insert (const cooked_index_entry *entry)
674 /* These entries are synthesized by the reader, and so should not
675 be written. */
676 if (entry->lang == language_ada && entry->tag == DW_TAG_namespace)
677 return;
679 const auto insertpair
680 = m_name_to_value_set.try_emplace (c_str_view (entry->name));
681 entry_list &elist = insertpair.first->second;
682 elist.entries.push_back (entry);
685 /* Build all the tables. All symbols must be already inserted.
686 This function does not call file_write, caller has to do it
687 afterwards. */
688 void build ()
690 /* Verify the build method has not be called twice. */
691 gdb_assert (m_abbrev_table.empty ());
692 const size_t name_count = m_name_to_value_set.size ();
693 m_name_table_string_offs.reserve (name_count);
694 m_name_table_entry_offs.reserve (name_count);
696 /* The name table is indexed from 1. The numbers are needed here
697 so that parent entries can be handled correctly. */
698 int next_name = 1;
699 for (auto &item : m_name_to_value_set)
700 item.second.index = next_name++;
702 /* The next available abbrev number. */
703 int next_abbrev = 1;
705 for (auto &item : m_name_to_value_set)
707 const c_str_view &name = item.first;
708 entry_list &these_entries = item.second;
710 /* Sort the items within each bucket. This ensures that the
711 generated index files will be the same no matter the order in
712 which symbols were added into the index. */
713 std::sort (these_entries.entries.begin (),
714 these_entries.entries.end (),
715 [] (const cooked_index_entry *a,
716 const cooked_index_entry *b)
718 /* Sort first by CU. */
719 if (a->per_cu->index != b->per_cu->index)
720 return a->per_cu->index < b->per_cu->index;
721 /* Then by DIE in the CU. */
722 if (a->die_offset != b->die_offset)
723 return a->die_offset < b->die_offset;
724 /* We might have two entries for a DIE because
725 the linkage name is entered separately. So,
726 sort by flags. */
727 return a->flags < b->flags;
730 m_name_table_string_offs.push_back_reorder
731 (m_debugstrlookup.lookup (name.c_str ())); /* ??? */
732 m_name_table_entry_offs.push_back_reorder (m_entry_pool.size ());
734 for (const cooked_index_entry *entry : these_entries.entries)
736 unit_kind kind = (entry->per_cu->is_debug_types
737 ? unit_kind::tu
738 : unit_kind::cu);
739 /* Currently Ada parentage is synthesized by the
740 reader and so must be ignored here. */
741 const cooked_index_entry *parent = (entry->lang == language_ada
742 ? nullptr
743 : entry->get_parent ());
745 int &idx = m_indexkey_to_idx[index_key (entry->tag,
746 kind,
747 entry->flags,
748 entry->lang,
749 parent != nullptr)];
750 if (idx == 0)
752 idx = next_abbrev++;
753 m_abbrev_table.append_unsigned_leb128 (idx);
754 m_abbrev_table.append_unsigned_leb128 (entry->tag);
755 m_abbrev_table.append_unsigned_leb128
756 (kind == unit_kind::cu
757 ? DW_IDX_compile_unit
758 : DW_IDX_type_unit);
759 m_abbrev_table.append_unsigned_leb128 (DW_FORM_udata);
760 m_abbrev_table.append_unsigned_leb128 (DW_IDX_die_offset);
761 m_abbrev_table.append_unsigned_leb128 (DW_FORM_ref_addr);
762 m_abbrev_table.append_unsigned_leb128 (DW_IDX_GNU_language);
763 m_abbrev_table.append_unsigned_leb128 (DW_FORM_udata);
764 if ((entry->flags & IS_STATIC) != 0)
766 m_abbrev_table.append_unsigned_leb128 (DW_IDX_GNU_internal);
767 m_abbrev_table.append_unsigned_leb128 (DW_FORM_flag_present);
769 if ((entry->flags & IS_MAIN) != 0)
771 m_abbrev_table.append_unsigned_leb128 (DW_IDX_GNU_main);
772 m_abbrev_table.append_unsigned_leb128 (DW_FORM_flag_present);
774 if ((entry->flags & IS_LINKAGE) != 0)
776 m_abbrev_table.append_unsigned_leb128 (DW_IDX_GNU_linkage_name);
777 m_abbrev_table.append_unsigned_leb128 (DW_FORM_flag_present);
779 if (parent != nullptr)
781 m_abbrev_table.append_unsigned_leb128 (DW_IDX_parent);
782 m_abbrev_table.append_unsigned_leb128 (DW_FORM_udata);
785 /* Terminate attributes list. */
786 m_abbrev_table.append_unsigned_leb128 (0);
787 m_abbrev_table.append_unsigned_leb128 (0);
790 m_entry_pool.append_unsigned_leb128 (idx);
792 const auto it = m_cu_index_htab.find (entry->per_cu);
793 gdb_assert (it != m_cu_index_htab.cend ());
794 m_entry_pool.append_unsigned_leb128 (it->second);
796 m_entry_pool.append_uint (dwarf5_offset_size (),
797 m_dwarf5_byte_order,
798 to_underlying (entry->die_offset));
800 m_entry_pool.append_unsigned_leb128 (entry->per_cu->dw_lang ());
802 if (parent != nullptr)
804 c_str_view par_name (parent->name);
805 auto name_iter = m_name_to_value_set.find (par_name);
806 gdb_assert (name_iter != m_name_to_value_set.end ());
807 gdb_assert (name_iter->second.index != 0);
808 m_entry_pool.append_unsigned_leb128 (name_iter->second.index);
812 /* Terminate the list of entries. */
813 m_entry_pool.append_unsigned_leb128 (0);
816 /* Terminate tags list. */
817 m_abbrev_table.append_unsigned_leb128 (0);
820 /* Return .debug_names names count. This must be called only after
821 calling the build method. */
822 uint32_t name_count () const
824 /* Verify the build method has been already called. */
825 gdb_assert (!m_abbrev_table.empty ());
826 return m_name_to_value_set.size ();
829 /* Return number of bytes of .debug_names abbreviation table. This
830 must be called only after calling the build method. */
831 uint32_t abbrev_table_bytes () const
833 gdb_assert (!m_abbrev_table.empty ());
834 return m_abbrev_table.size ();
837 /* Return number of bytes the .debug_names section will have. This
838 must be called only after calling the build method. */
839 size_t bytes () const
841 /* Verify the build method has been already called. */
842 gdb_assert (!m_abbrev_table.empty ());
843 size_t expected_bytes = 0;
844 expected_bytes += m_name_table_string_offs.bytes ();
845 expected_bytes += m_name_table_entry_offs.bytes ();
846 expected_bytes += m_abbrev_table.size ();
847 expected_bytes += m_entry_pool.size ();
848 return expected_bytes;
851 /* Write .debug_names to FILE_NAMES and .debug_str addition to
852 FILE_STR. This must be called only after calling the build
853 method. */
854 void file_write (FILE *file_names, FILE *file_str) const
856 /* Verify the build method has been already called. */
857 gdb_assert (!m_abbrev_table.empty ());
858 m_name_table_string_offs.file_write (file_names);
859 m_name_table_entry_offs.file_write (file_names);
860 m_abbrev_table.file_write (file_names);
861 m_entry_pool.file_write (file_names);
862 m_debugstrlookup.file_write (file_str);
865 void add_cu (dwarf2_per_cu_data *per_cu, offset_type index)
867 m_cu_index_htab.emplace (per_cu, index);
870 private:
872 /* Storage for symbol names mapping them to their .debug_str section
873 offsets. */
874 class debug_str_lookup
876 public:
878 /* Object constructor to be called for current DWARF2_PER_BFD. */
879 debug_str_lookup (dwarf2_per_bfd *per_bfd)
880 : m_abfd (per_bfd->obfd),
881 m_per_bfd (per_bfd)
885 /* Return offset of symbol name S in the .debug_str section. Add
886 such symbol to the section's end if it does not exist there
887 yet. */
888 size_t lookup (const char *s)
890 /* Most strings will have come from the string table
891 already. */
892 const gdb_byte *b = (const gdb_byte *) s;
893 if (b >= m_per_bfd->str.buffer
894 && b < m_per_bfd->str.buffer + m_per_bfd->str.size)
895 return b - m_per_bfd->str.buffer;
897 const auto it = m_str_table.find (c_str_view (s));
898 if (it != m_str_table.end ())
899 return it->second;
900 const size_t offset = (m_per_bfd->str.size
901 + m_str_add_buf.size ());
902 m_str_table.emplace (c_str_view (s), offset);
903 m_str_add_buf.append_cstr0 (s);
904 return offset;
907 /* Append the end of the .debug_str section to FILE. */
908 void file_write (FILE *file) const
910 m_str_add_buf.file_write (file);
913 private:
914 std::unordered_map<c_str_view, size_t, c_str_view_hasher> m_str_table;
915 bfd *const m_abfd;
916 dwarf2_per_bfd *m_per_bfd;
918 /* Data to add at the end of .debug_str for new needed symbol names. */
919 data_buf m_str_add_buf;
922 /* Container to map used DWARF tags to their .debug_names abbreviation
923 tags. */
924 class index_key
926 public:
927 index_key (dwarf_tag tag_, unit_kind kind_, cooked_index_flag flags_,
928 enum language lang_, bool has_parent_)
929 : tag (tag_),
930 kind (kind_),
931 flags (flags_ & ~IS_TYPE_DECLARATION),
932 lang (lang_),
933 has_parent (has_parent_)
937 bool operator== (const index_key &other) const
939 return (tag == other.tag
940 && kind == other.kind
941 && flags == other.flags
942 && lang == other.lang
943 && has_parent == other.has_parent);
946 const dwarf_tag tag;
947 const unit_kind kind;
948 const cooked_index_flag flags;
949 const enum language lang;
950 const bool has_parent;
953 /* Provide std::unordered_map::hasher for index_key. */
954 class index_key_hasher
956 public:
957 size_t operator () (const index_key &key) const
959 return (std::hash<int>() (key.tag)
960 ^ std::hash<int>() (key.flags)
961 ^ std::hash<int>() (key.lang));
965 /* Abstract base class to unify DWARF-32 and DWARF-64 name table
966 output. */
967 class offset_vec
969 protected:
970 const bfd_endian dwarf5_byte_order;
971 public:
972 explicit offset_vec (bfd_endian dwarf5_byte_order_)
973 : dwarf5_byte_order (dwarf5_byte_order_)
976 /* Call std::vector::reserve for NELEM elements. */
977 virtual void reserve (size_t nelem) = 0;
979 /* Call std::vector::push_back with store_unsigned_integer byte
980 reordering for ELEM. */
981 virtual void push_back_reorder (size_t elem) = 0;
983 /* Return expected output size in bytes. */
984 virtual size_t bytes () const = 0;
986 /* Write name table to FILE. */
987 virtual void file_write (FILE *file) const = 0;
990 /* Template to unify DWARF-32 and DWARF-64 output. */
991 template<typename OffsetSize>
992 class offset_vec_tmpl : public offset_vec
994 public:
995 explicit offset_vec_tmpl (bfd_endian dwarf5_byte_order_)
996 : offset_vec (dwarf5_byte_order_)
999 /* Implement offset_vec::reserve. */
1000 void reserve (size_t nelem) override
1002 m_vec.reserve (nelem);
1005 /* Implement offset_vec::push_back_reorder. */
1006 void push_back_reorder (size_t elem) override
1008 m_vec.push_back (elem);
1009 /* Check for overflow. */
1010 gdb_assert (m_vec.back () == elem);
1011 store_unsigned_integer (reinterpret_cast<gdb_byte *> (&m_vec.back ()),
1012 sizeof (m_vec.back ()), dwarf5_byte_order, elem);
1015 /* Implement offset_vec::bytes. */
1016 size_t bytes () const override
1018 return m_vec.size () * sizeof (m_vec[0]);
1021 /* Implement offset_vec::file_write. */
1022 void file_write (FILE *file) const override
1024 ::file_write (file, m_vec);
1027 private:
1028 std::vector<OffsetSize> m_vec;
1031 /* Base class to unify DWARF-32 and DWARF-64 .debug_names output
1032 respecting name table width. */
1033 class dwarf
1035 public:
1036 offset_vec &name_table_string_offs, &name_table_entry_offs;
1038 dwarf (offset_vec &name_table_string_offs_,
1039 offset_vec &name_table_entry_offs_)
1040 : name_table_string_offs (name_table_string_offs_),
1041 name_table_entry_offs (name_table_entry_offs_)
1046 /* Template to unify DWARF-32 and DWARF-64 .debug_names output
1047 respecting name table width. */
1048 template<typename OffsetSize>
1049 class dwarf_tmpl : public dwarf
1051 public:
1052 explicit dwarf_tmpl (bfd_endian dwarf5_byte_order_)
1053 : dwarf (m_name_table_string_offs, m_name_table_entry_offs),
1054 m_name_table_string_offs (dwarf5_byte_order_),
1055 m_name_table_entry_offs (dwarf5_byte_order_)
1058 private:
1059 offset_vec_tmpl<OffsetSize> m_name_table_string_offs;
1060 offset_vec_tmpl<OffsetSize> m_name_table_entry_offs;
1063 struct entry_list
1065 unsigned index = 0;
1066 std::vector<const cooked_index_entry *> entries;
1069 /* Store value of each symbol. Note that we rely on the sorting
1070 behavior of map to make the output stable. */
1071 std::map<c_str_view, entry_list> m_name_to_value_set;
1073 const bfd_endian m_dwarf5_byte_order;
1074 dwarf_tmpl<uint32_t> m_dwarf32;
1075 dwarf_tmpl<uint64_t> m_dwarf64;
1076 dwarf &m_dwarf;
1077 offset_vec &m_name_table_string_offs, &m_name_table_entry_offs;
1078 debug_str_lookup m_debugstrlookup;
1080 /* Map each used .debug_names abbreviation tag parameter to its
1081 index value. */
1082 std::unordered_map<index_key, int, index_key_hasher> m_indexkey_to_idx;
1084 /* .debug_names abbreviation table. */
1085 data_buf m_abbrev_table;
1087 /* .debug_names entry pool. */
1088 data_buf m_entry_pool;
1090 /* Temporary storage for Ada names. */
1091 auto_obstack m_string_obstack;
1093 cu_index_map m_cu_index_htab;
1096 /* Return iff any of the needed offsets does not fit into 32-bit
1097 .debug_names section. */
1099 static bool
1100 check_dwarf64_offsets (dwarf2_per_bfd *per_bfd)
1102 for (const auto &per_cu : per_bfd->all_units)
1104 if (to_underlying (per_cu->sect_off)
1105 >= (static_cast<uint64_t> (1) << 32))
1106 return true;
1108 return false;
1111 /* Assert that FILE's size is EXPECTED_SIZE. Assumes file's seek
1112 position is at the end of the file. */
1114 static void
1115 assert_file_size (FILE *file, size_t expected_size)
1117 const auto file_size = ftell (file);
1118 if (file_size == -1)
1119 perror_with_name (("ftell"));
1120 gdb_assert (file_size == expected_size);
1123 /* Write a gdb index file to OUT_FILE from all the sections passed as
1124 arguments. */
1126 static void
1127 write_gdbindex_1 (FILE *out_file,
1128 const data_buf &cu_list,
1129 const data_buf &types_cu_list,
1130 const data_buf &addr_vec,
1131 const data_buf &symtab_vec,
1132 const data_buf &constant_pool,
1133 const data_buf &shortcuts)
1135 data_buf contents;
1136 const offset_type size_of_header = 7 * sizeof (offset_type);
1137 uint64_t total_len = size_of_header;
1139 /* The version number. */
1140 contents.append_offset (9);
1142 /* The offset of the CU list from the start of the file. */
1143 contents.append_offset (total_len);
1144 total_len += cu_list.size ();
1146 /* The offset of the types CU list from the start of the file. */
1147 contents.append_offset (total_len);
1148 total_len += types_cu_list.size ();
1150 /* The offset of the address table from the start of the file. */
1151 contents.append_offset (total_len);
1152 total_len += addr_vec.size ();
1154 /* The offset of the symbol table from the start of the file. */
1155 contents.append_offset (total_len);
1156 total_len += symtab_vec.size ();
1158 /* The offset of the shortcut table from the start of the file. */
1159 contents.append_offset (total_len);
1160 total_len += shortcuts.size ();
1162 /* The offset of the constant pool from the start of the file. */
1163 contents.append_offset (total_len);
1164 total_len += constant_pool.size ();
1166 gdb_assert (contents.size () == size_of_header);
1168 /* The maximum size of an index file is limited by the maximum value
1169 capable of being represented by 'offset_type'. Throw an error if
1170 that length has been exceeded. */
1171 size_t max_size = ~(offset_type) 0;
1172 if (total_len > max_size)
1173 error (_("gdb-index maximum file size of %zu exceeded"), max_size);
1175 if (out_file == nullptr)
1176 return;
1178 contents.file_write (out_file);
1179 cu_list.file_write (out_file);
1180 types_cu_list.file_write (out_file);
1181 addr_vec.file_write (out_file);
1182 symtab_vec.file_write (out_file);
1183 shortcuts.file_write (out_file);
1184 constant_pool.file_write (out_file);
1186 assert_file_size (out_file, total_len);
1189 /* Write the contents of the internal "cooked" index. */
1191 static void
1192 write_cooked_index (cooked_index *table,
1193 const cu_index_map &cu_index_htab,
1194 struct mapped_symtab *symtab)
1196 for (const cooked_index_entry *entry : table->all_entries ())
1198 const auto it = cu_index_htab.find (entry->per_cu);
1199 gdb_assert (it != cu_index_htab.cend ());
1201 const char *name = entry->full_name (symtab->obstack ());
1203 if (entry->lang == language_ada)
1205 /* In order for the index to work when read back into
1206 gdb, it has to use the encoded name, with any
1207 suffixes stripped. */
1208 std::string encoded = ada_encode (name, false);
1209 name = obstack_strdup (symtab->obstack (), encoded.c_str ());
1211 else if (entry->lang == language_cplus
1212 && (entry->flags & IS_LINKAGE) != 0)
1214 /* GDB never put C++ linkage names into .gdb_index. The
1215 theory here is that a linkage name will normally be in
1216 the minimal symbols anyway, so including it in the index
1217 is usually redundant -- and the cases where it would not
1218 be redundant are rare and not worth supporting. */
1219 continue;
1221 else if ((entry->flags & IS_TYPE_DECLARATION) != 0)
1223 /* Don't add type declarations to the index. */
1224 continue;
1227 gdb_index_symbol_kind kind;
1228 if (entry->tag == DW_TAG_subprogram
1229 || entry->tag == DW_TAG_entry_point)
1230 kind = GDB_INDEX_SYMBOL_KIND_FUNCTION;
1231 else if (entry->tag == DW_TAG_variable
1232 || entry->tag == DW_TAG_constant
1233 || entry->tag == DW_TAG_enumerator)
1234 kind = GDB_INDEX_SYMBOL_KIND_VARIABLE;
1235 else if (tag_is_type (entry->tag))
1236 kind = GDB_INDEX_SYMBOL_KIND_TYPE;
1237 else
1238 kind = GDB_INDEX_SYMBOL_KIND_OTHER;
1240 symtab->add_index_entry (name, (entry->flags & IS_STATIC) != 0,
1241 kind, it->second);
1245 /* Write shortcut information. */
1247 static void
1248 write_shortcuts_table (cooked_index *table, data_buf &shortcuts,
1249 data_buf &cpool)
1251 const auto main_info = table->get_main ();
1252 size_t main_name_offset = 0;
1253 dwarf_source_language dw_lang = (dwarf_source_language) 0;
1255 if (main_info != nullptr)
1257 dw_lang = main_info->per_cu->dw_lang ();
1259 if (dw_lang != 0)
1261 auto_obstack obstack;
1262 const auto main_name = main_info->full_name (&obstack, true);
1264 main_name_offset = cpool.size ();
1265 cpool.append_cstr0 (main_name);
1269 shortcuts.append_offset (dw_lang);
1270 shortcuts.append_offset (main_name_offset);
1273 /* Write contents of a .gdb_index section for OBJFILE into OUT_FILE.
1274 If OBJFILE has an associated dwz file, write contents of a .gdb_index
1275 section for that dwz file into DWZ_OUT_FILE. If OBJFILE does not have an
1276 associated dwz file, DWZ_OUT_FILE must be NULL. */
1278 static void
1279 write_gdbindex (dwarf2_per_bfd *per_bfd, cooked_index *table,
1280 FILE *out_file, FILE *dwz_out_file)
1282 mapped_symtab symtab;
1283 data_buf objfile_cu_list;
1284 data_buf dwz_cu_list;
1286 /* While we're scanning CU's create a table that maps a dwarf2_per_cu_data
1287 (which is what addrmap records) to its index (which is what is recorded
1288 in the index file). This will later be needed to write the address
1289 table. */
1290 cu_index_map cu_index_htab;
1291 cu_index_htab.reserve (per_bfd->all_units.size ());
1293 /* Store out the .debug_type CUs, if any. */
1294 data_buf types_cu_list;
1296 /* The CU list is already sorted, so we don't need to do additional
1297 work here. */
1299 int counter = 0;
1300 for (int i = 0; i < per_bfd->all_units.size (); ++i)
1302 dwarf2_per_cu_data *per_cu = per_bfd->all_units[i].get ();
1304 const auto insertpair = cu_index_htab.emplace (per_cu, counter);
1305 gdb_assert (insertpair.second);
1307 /* See enhancement PR symtab/30838. */
1308 gdb_assert (!(per_cu->is_dwz && per_cu->is_debug_types));
1310 /* The all_units list contains CUs read from the objfile as well as
1311 from the eventual dwz file. We need to place the entry in the
1312 corresponding index. */
1313 data_buf &cu_list = (per_cu->is_debug_types
1314 ? types_cu_list
1315 : per_cu->is_dwz ? dwz_cu_list : objfile_cu_list);
1316 cu_list.append_uint (8, BFD_ENDIAN_LITTLE,
1317 to_underlying (per_cu->sect_off));
1318 if (per_cu->is_debug_types)
1320 signatured_type *sig_type = (signatured_type *) per_cu;
1321 cu_list.append_uint (8, BFD_ENDIAN_LITTLE,
1322 to_underlying (sig_type->type_offset_in_tu));
1323 cu_list.append_uint (8, BFD_ENDIAN_LITTLE,
1324 sig_type->signature);
1326 else
1327 cu_list.append_uint (8, BFD_ENDIAN_LITTLE, per_cu->length ());
1329 ++counter;
1332 write_cooked_index (table, cu_index_htab, &symtab);
1334 /* Dump the address map. */
1335 data_buf addr_vec;
1336 for (auto map : table->get_addrmaps ())
1337 write_address_map (map, addr_vec, cu_index_htab);
1339 /* Ensure symbol hash is built domestically. */
1340 symtab.sort ();
1342 /* Now that we've processed all symbols we can shrink their cu_indices
1343 lists. */
1344 symtab.minimize ();
1346 data_buf symtab_vec, constant_pool;
1348 write_hash_table (&symtab, symtab_vec, constant_pool);
1350 data_buf shortcuts;
1351 write_shortcuts_table (table, shortcuts, constant_pool);
1353 write_gdbindex_1 (out_file, objfile_cu_list, types_cu_list, addr_vec,
1354 symtab_vec, constant_pool, shortcuts);
1356 if (dwz_out_file != NULL)
1357 write_gdbindex_1 (dwz_out_file, dwz_cu_list, {}, {}, {}, {}, {});
1358 else
1359 gdb_assert (dwz_cu_list.empty ());
1362 /* Write a new .debug_names section for OBJFILE into OUT_FILE, write
1363 needed addition to .debug_str section to OUT_FILE_STR. Return how
1364 many bytes were expected to be written into OUT_FILE. */
1366 static void
1367 write_debug_names (dwarf2_per_bfd *per_bfd, cooked_index *table,
1368 FILE *out_file, FILE *out_file_str)
1370 const bool dwarf5_is_dwarf64 = check_dwarf64_offsets (per_bfd);
1371 const enum bfd_endian dwarf5_byte_order
1372 = bfd_big_endian (per_bfd->obfd) ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
1374 /* The CU list is already sorted, so we don't need to do additional
1375 work here. Also, the debug_types entries do not appear in
1376 all_units, but only in their own hash table. */
1377 data_buf cu_list;
1378 data_buf types_cu_list;
1379 debug_names nametable (per_bfd, dwarf5_is_dwarf64, dwarf5_byte_order);
1380 int counter = 0;
1381 int types_counter = 0;
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 int &this_counter = per_cu->is_debug_types ? types_counter : counter;
1387 data_buf &this_list = per_cu->is_debug_types ? types_cu_list : cu_list;
1389 nametable.add_cu (per_cu, this_counter);
1390 this_list.append_uint (nametable.dwarf5_offset_size (),
1391 dwarf5_byte_order,
1392 to_underlying (per_cu->sect_off));
1393 ++this_counter;
1396 /* Verify that all units are represented. */
1397 gdb_assert (counter == per_bfd->all_comp_units.size ());
1398 gdb_assert (types_counter == per_bfd->all_type_units.size ());
1400 for (const cooked_index_entry *entry : table->all_entries ())
1401 nametable.insert (entry);
1403 nametable.build ();
1405 /* No addr_vec - DWARF-5 uses .debug_aranges generated by GCC. */
1407 const offset_type bytes_of_header
1408 = ((dwarf5_is_dwarf64 ? 12 : 4)
1409 + 2 + 2 + 7 * 4
1410 + sizeof (dwarf5_augmentation));
1411 size_t expected_bytes = 0;
1412 expected_bytes += bytes_of_header;
1413 expected_bytes += cu_list.size ();
1414 expected_bytes += types_cu_list.size ();
1415 expected_bytes += nametable.bytes ();
1416 data_buf header;
1418 if (!dwarf5_is_dwarf64)
1420 const uint64_t size64 = expected_bytes - 4;
1421 gdb_assert (size64 < 0xfffffff0);
1422 header.append_uint (4, dwarf5_byte_order, size64);
1424 else
1426 header.append_uint (4, dwarf5_byte_order, 0xffffffff);
1427 header.append_uint (8, dwarf5_byte_order, expected_bytes - 12);
1430 /* The version number. */
1431 header.append_uint (2, dwarf5_byte_order, 5);
1433 /* Padding. */
1434 header.append_uint (2, dwarf5_byte_order, 0);
1436 /* comp_unit_count - The number of CUs in the CU list. */
1437 header.append_uint (4, dwarf5_byte_order, counter);
1439 /* local_type_unit_count - The number of TUs in the local TU
1440 list. */
1441 header.append_uint (4, dwarf5_byte_order, types_counter);
1443 /* foreign_type_unit_count - The number of TUs in the foreign TU
1444 list. */
1445 header.append_uint (4, dwarf5_byte_order, 0);
1447 /* bucket_count - The number of hash buckets in the hash lookup
1448 table. GDB does not use the hash table, so there's also no need
1449 to write it -- plus, the hash table is broken as defined due to
1450 the lack of name canonicalization. */
1451 header.append_uint (4, dwarf5_byte_order, 0);
1453 /* name_count - The number of unique names in the index. */
1454 header.append_uint (4, dwarf5_byte_order, nametable.name_count ());
1456 /* abbrev_table_size - The size in bytes of the abbreviations
1457 table. */
1458 header.append_uint (4, dwarf5_byte_order, nametable.abbrev_table_bytes ());
1460 /* augmentation_string_size - The size in bytes of the augmentation
1461 string. This value is rounded up to a multiple of 4. */
1462 static_assert (sizeof (dwarf5_augmentation) % 4 == 0);
1463 header.append_uint (4, dwarf5_byte_order, sizeof (dwarf5_augmentation));
1464 header.append_array (dwarf5_augmentation);
1466 gdb_assert (header.size () == bytes_of_header);
1468 header.file_write (out_file);
1469 cu_list.file_write (out_file);
1470 types_cu_list.file_write (out_file);
1471 nametable.file_write (out_file, out_file_str);
1473 assert_file_size (out_file, expected_bytes);
1476 /* This represents an index file being written (work-in-progress).
1478 The data is initially written to a temporary file. When the finalize method
1479 is called, the file is closed and moved to its final location.
1481 On failure (if this object is being destroyed with having called finalize),
1482 the temporary file is closed and deleted. */
1484 struct index_wip_file
1486 index_wip_file (const char *dir, const char *basename,
1487 const char *suffix)
1489 /* Validate DIR is a valid directory. */
1490 struct stat buf;
1491 if (stat (dir, &buf) == -1)
1492 perror_with_name (string_printf (_("`%s'"), dir).c_str ());
1493 if ((buf.st_mode & S_IFDIR) != S_IFDIR)
1494 error (_("`%s': Is not a directory."), dir);
1496 filename = (std::string (dir) + SLASH_STRING + basename
1497 + suffix);
1499 filename_temp = make_temp_filename (filename);
1501 scoped_fd out_file_fd = gdb_mkostemp_cloexec (filename_temp.data (),
1502 O_BINARY);
1503 if (out_file_fd.get () == -1)
1504 perror_with_name (string_printf (_("couldn't open `%s'"),
1505 filename_temp.data ()).c_str ());
1507 out_file = out_file_fd.to_file ("wb");
1509 if (out_file == nullptr)
1510 error (_("Can't open `%s' for writing"), filename_temp.data ());
1512 unlink_file.emplace (filename_temp.data ());
1515 void finalize ()
1517 /* We want to keep the file. */
1518 unlink_file->keep ();
1520 /* Close and move the str file in place. */
1521 unlink_file.reset ();
1522 if (rename (filename_temp.data (), filename.c_str ()) != 0)
1523 perror_with_name (("rename"));
1526 std::string filename;
1527 gdb::char_vector filename_temp;
1529 /* Order matters here; we want FILE to be closed before
1530 FILENAME_TEMP is unlinked, because on MS-Windows one cannot
1531 delete a file that is still open. So, we wrap the unlinker in an
1532 optional and emplace it once we know the file name. */
1533 std::optional<gdb::unlinker> unlink_file;
1535 gdb_file_up out_file;
1538 /* See dwarf-index-write.h. */
1540 void
1541 write_dwarf_index (dwarf2_per_bfd *per_bfd, const char *dir,
1542 const char *basename, const char *dwz_basename,
1543 dw_index_kind index_kind)
1545 if (per_bfd->index_table == nullptr)
1546 error (_("No debugging symbols"));
1547 cooked_index *table = per_bfd->index_table->index_for_writing ();
1548 if (table == nullptr)
1549 error (_("Cannot use an index to create the index"));
1551 if (per_bfd->infos.size () > 1)
1552 error (_("Cannot make an index when the file has multiple .debug_info"
1553 " sections"));
1554 if (per_bfd->types.size () > 1)
1555 error (_("Cannot make an index when the file has multiple .debug_types sections"));
1557 const char *index_suffix = (index_kind == dw_index_kind::DEBUG_NAMES
1558 ? INDEX5_SUFFIX : INDEX4_SUFFIX);
1560 index_wip_file objfile_index_wip (dir, basename, index_suffix);
1561 std::optional<index_wip_file> dwz_index_wip;
1563 if (dwz_basename != NULL)
1564 dwz_index_wip.emplace (dir, dwz_basename, index_suffix);
1566 if (index_kind == dw_index_kind::DEBUG_NAMES)
1568 index_wip_file str_wip_file (dir, basename, DEBUG_STR_SUFFIX);
1570 write_debug_names (per_bfd, table, objfile_index_wip.out_file.get (),
1571 str_wip_file.out_file.get ());
1573 str_wip_file.finalize ();
1575 else
1576 write_gdbindex (per_bfd, table, objfile_index_wip.out_file.get (),
1577 (dwz_index_wip.has_value ()
1578 ? dwz_index_wip->out_file.get () : NULL));
1580 objfile_index_wip.finalize ();
1582 if (dwz_index_wip.has_value ())
1583 dwz_index_wip->finalize ();
1586 /* Options structure for the 'save gdb-index' command. */
1588 struct save_gdb_index_options
1590 bool dwarf_5 = false;
1593 /* The option_def list for the 'save gdb-index' command. */
1595 static const gdb::option::option_def save_gdb_index_options_defs[] = {
1596 gdb::option::boolean_option_def<save_gdb_index_options> {
1597 "dwarf-5",
1598 [] (save_gdb_index_options *opt) { return &opt->dwarf_5; },
1599 nullptr, /* show_cmd_cb */
1600 nullptr /* set_doc */
1604 /* Create an options_def_group for the 'save gdb-index' command. */
1606 static gdb::option::option_def_group
1607 make_gdb_save_index_options_def_group (save_gdb_index_options *opts)
1609 return {{save_gdb_index_options_defs}, opts};
1612 /* Completer for the "save gdb-index" command. */
1614 static void
1615 gdb_save_index_cmd_completer (struct cmd_list_element *ignore,
1616 completion_tracker &tracker,
1617 const char *text, const char *word)
1619 auto grp = make_gdb_save_index_options_def_group (nullptr);
1620 if (gdb::option::complete_options
1621 (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, grp))
1622 return;
1624 word = advance_to_filename_maybe_quoted_complete_word_point (tracker, text);
1625 filename_maybe_quoted_completer (ignore, tracker, text, word);
1628 /* Implementation of the `save gdb-index' command.
1630 Note that the .gdb_index file format used by this command is
1631 documented in the GDB manual. Any changes here must be documented
1632 there. */
1634 static void
1635 save_gdb_index_command (const char *args, int from_tty)
1637 save_gdb_index_options opts;
1638 const auto group = make_gdb_save_index_options_def_group (&opts);
1639 gdb::option::process_options
1640 (&args, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, group);
1642 std::string directory = extract_single_filename_arg (args);
1643 if (directory.empty ())
1644 error (_("usage: save gdb-index [-dwarf-5] DIRECTORY"));
1646 dw_index_kind index_kind
1647 = (opts.dwarf_5 ? dw_index_kind::DEBUG_NAMES : dw_index_kind::GDB_INDEX);
1649 for (objfile *objfile : current_program_space->objfiles ())
1651 /* If the objfile does not correspond to an actual file, skip it. */
1652 if ((objfile->flags & OBJF_NOT_FILENAME) != 0)
1653 continue;
1655 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
1657 if (per_objfile != NULL)
1661 const char *basename = lbasename (objfile_name (objfile));
1662 const dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
1663 const char *dwz_basename = NULL;
1665 if (dwz != NULL)
1666 dwz_basename = lbasename (dwz->filename ());
1668 write_dwarf_index (per_objfile->per_bfd, directory.c_str (),
1669 basename, dwz_basename, index_kind);
1671 catch (const gdb_exception_error &except)
1673 exception_fprintf (gdb_stderr, except,
1674 _("Error while writing index for `%s': "),
1675 objfile_name (objfile));
1682 #if GDB_SELF_TEST
1683 #include "gdbsupport/selftest.h"
1685 namespace selftests {
1687 class pretend_data_buf : public data_buf
1689 public:
1690 /* Set the pretend size. */
1691 void set_pretend_size (size_t s) {
1692 m_pretend_size = s;
1695 /* Override size method of data_buf, returning the pretend size instead. */
1696 size_t size () const override {
1697 return m_pretend_size;
1700 private:
1701 size_t m_pretend_size = 0;
1704 static void
1705 gdb_index ()
1707 pretend_data_buf cu_list;
1708 pretend_data_buf types_cu_list;
1709 pretend_data_buf addr_vec;
1710 pretend_data_buf symtab_vec;
1711 pretend_data_buf constant_pool;
1712 pretend_data_buf short_cuts;
1714 const size_t size_of_header = 7 * sizeof (offset_type);
1716 /* Test that an overly large index will throw an error. */
1717 symtab_vec.set_pretend_size (~(offset_type)0 - size_of_header);
1718 constant_pool.set_pretend_size (1);
1720 bool saw_exception = false;
1723 write_gdbindex_1 (nullptr, cu_list, types_cu_list, addr_vec,
1724 symtab_vec, constant_pool, short_cuts);
1726 catch (const gdb_exception_error &e)
1728 SELF_CHECK (e.reason == RETURN_ERROR);
1729 SELF_CHECK (e.error == GENERIC_ERROR);
1730 SELF_CHECK (e.message->find (_("gdb-index maximum file size of"))
1731 != std::string::npos);
1732 SELF_CHECK (e.message->find (_("exceeded")) != std::string::npos);
1733 saw_exception = true;
1735 SELF_CHECK (saw_exception);
1737 /* Test that the largest possible index will not throw an error. */
1738 constant_pool.set_pretend_size (0);
1740 saw_exception = false;
1743 write_gdbindex_1 (nullptr, cu_list, types_cu_list, addr_vec,
1744 symtab_vec, constant_pool, short_cuts);
1746 catch (const gdb_exception_error &e)
1748 saw_exception = true;
1750 SELF_CHECK (!saw_exception);
1753 } /* selftests namespace. */
1754 #endif
1756 void _initialize_dwarf_index_write ();
1757 void
1758 _initialize_dwarf_index_write ()
1760 #if GDB_SELF_TEST
1761 selftests::register_test ("gdb_index", selftests::gdb_index);
1762 #endif
1764 cmd_list_element *c = add_cmd ("gdb-index", class_files,
1765 save_gdb_index_command, _("\
1766 Save a gdb-index file.\n\
1767 Usage: save gdb-index [-dwarf-5] DIRECTORY\n\
1769 No options create one file with .gdb-index extension for pre-DWARF-5\n\
1770 compatible .gdb_index section. With -dwarf-5 creates two files with\n\
1771 extension .debug_names and .debug_str for DWARF-5 .debug_names section."),
1772 &save_cmdlist);
1773 set_cmd_completer_handle_brkchars (c, gdb_save_index_cmd_completer);