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"
34 #include "dwarf2/read.h"
35 #include "dwarf2/dwz.h"
36 #include "gdb/gdb-index.h"
40 #include "dwarf2/tag.h"
44 #include <forward_list>
46 #include <unordered_map>
47 #include <unordered_set>
49 /* Ensure only legit values are used. */
50 #define DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE(cu_index, value) \
52 gdb_assert ((unsigned int) (value) <= 1); \
53 GDB_INDEX_SYMBOL_STATIC_SET_VALUE((cu_index), (value)); \
56 /* Ensure only legit values are used. */
57 #define DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE(cu_index, value) \
59 gdb_assert ((value) >= GDB_INDEX_SYMBOL_KIND_TYPE \
60 && (value) <= GDB_INDEX_SYMBOL_KIND_OTHER); \
61 GDB_INDEX_SYMBOL_KIND_SET_VALUE((cu_index), (value)); \
64 /* Ensure we don't use more than the allotted number of bits for the CU. */
65 #define DW2_GDB_INDEX_CU_SET_VALUE(cu_index, value) \
67 gdb_assert (((value) & ~GDB_INDEX_CU_MASK) == 0); \
68 GDB_INDEX_CU_SET_VALUE((cu_index), (value)); \
71 /* The "save gdb-index" command. */
73 /* Write SIZE bytes from the buffer pointed to by DATA to FILE, with
77 file_write (FILE *file
, const void *data
, size_t size
)
79 if (fwrite (data
, 1, size
, file
) != size
)
80 error (_("couldn't data write to file"));
83 /* Write the contents of VEC to FILE, with error checking. */
85 template<typename Elem
, typename Alloc
>
87 file_write (FILE *file
, const std::vector
<Elem
, Alloc
> &vec
)
90 file_write (file
, vec
.data (), vec
.size () * sizeof (vec
[0]));
93 /* In-memory buffer to prepare data to be written later to a file. */
97 /* Copy ARRAY to the end of the buffer. */
98 void append_array (gdb::array_view
<const gdb_byte
> array
)
100 std::copy (array
.begin (), array
.end (), grow (array
.size ()));
103 /* Copy CSTR (a zero-terminated string) to the end of buffer. The
104 terminating zero is appended too. */
105 void append_cstr0 (const char *cstr
)
107 const size_t size
= strlen (cstr
) + 1;
108 std::copy (cstr
, cstr
+ size
, grow (size
));
111 /* Store INPUT as ULEB128 to the end of buffer. */
112 void append_unsigned_leb128 (ULONGEST input
)
116 gdb_byte output
= input
& 0x7f;
120 m_vec
.push_back (output
);
126 /* Accept a host-format integer in VAL and append it to the buffer
127 as a target-format integer which is LEN bytes long. */
128 void append_uint (size_t len
, bfd_endian byte_order
, ULONGEST val
)
130 ::store_unsigned_integer (grow (len
), len
, byte_order
, val
);
133 /* Copy VALUE to the end of the buffer, little-endian. */
134 void append_offset (offset_type value
)
136 append_uint (sizeof (value
), BFD_ENDIAN_LITTLE
, value
);
139 /* Return the size of the buffer. */
142 return m_vec
.size ();
145 /* Return true iff the buffer is empty. */
148 return m_vec
.empty ();
151 /* Write the buffer to FILE. */
152 void file_write (FILE *file
) const
154 ::file_write (file
, m_vec
);
158 /* Grow SIZE bytes at the end of the buffer. Returns a pointer to
159 the start of the new block. */
160 gdb_byte
*grow (size_t size
)
162 m_vec
.resize (m_vec
.size () + size
);
163 return &*(m_vec
.end () - size
);
166 gdb::byte_vector m_vec
;
169 /* An entry in the symbol table. */
170 struct symtab_index_entry
172 /* The name of the symbol. */
174 /* The offset of the name in the constant pool. */
175 offset_type index_offset
;
176 /* A sorted vector of the indices of all the CUs that hold an object
178 std::vector
<offset_type
> cu_indices
;
180 /* Minimize CU_INDICES, sorting them and removing duplicates as
185 /* The symbol table. This is a power-of-2-sized hash table. */
193 /* Minimize each entry in the symbol table, removing duplicates. */
196 for (symtab_index_entry
&item
: data
)
200 offset_type n_elements
= 0;
201 std::vector
<symtab_index_entry
> data
;
203 /* Temporary storage for names. */
204 auto_obstack m_string_obstack
;
207 /* Find a slot in SYMTAB for the symbol NAME. Returns a reference to
210 Function is used only during write_hash_table so no index format backward
211 compatibility is needed. */
213 static symtab_index_entry
&
214 find_slot (struct mapped_symtab
*symtab
, const char *name
)
216 offset_type index
, step
, hash
= mapped_index_string_hash (INT_MAX
, name
);
218 index
= hash
& (symtab
->data
.size () - 1);
219 step
= ((hash
* 17) & (symtab
->data
.size () - 1)) | 1;
223 if (symtab
->data
[index
].name
== NULL
224 || strcmp (name
, symtab
->data
[index
].name
) == 0)
225 return symtab
->data
[index
];
226 index
= (index
+ step
) & (symtab
->data
.size () - 1);
230 /* Expand SYMTAB's hash table. */
233 hash_expand (struct mapped_symtab
*symtab
)
235 auto old_entries
= std::move (symtab
->data
);
237 symtab
->data
.clear ();
238 symtab
->data
.resize (old_entries
.size () * 2);
240 for (auto &it
: old_entries
)
243 auto &ref
= find_slot (symtab
, it
.name
);
244 ref
= std::move (it
);
248 /* Add an entry to SYMTAB. NAME is the name of the symbol.
249 CU_INDEX is the index of the CU in which the symbol appears.
250 IS_STATIC is one if the symbol is static, otherwise zero (global). */
253 add_index_entry (struct mapped_symtab
*symtab
, const char *name
,
254 int is_static
, gdb_index_symbol_kind kind
,
255 offset_type cu_index
)
257 offset_type cu_index_and_attrs
;
259 ++symtab
->n_elements
;
260 if (4 * symtab
->n_elements
/ 3 >= symtab
->data
.size ())
261 hash_expand (symtab
);
263 symtab_index_entry
&slot
= find_slot (symtab
, name
);
264 if (slot
.name
== NULL
)
267 /* index_offset is set later. */
270 cu_index_and_attrs
= 0;
271 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs
, cu_index
);
272 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs
, is_static
);
273 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs
, kind
);
275 /* We don't want to record an index value twice as we want to avoid the
277 We process all global symbols and then all static symbols
278 (which would allow us to avoid the duplication by only having to check
279 the last entry pushed), but a symbol could have multiple kinds in one CU.
280 To keep things simple we don't worry about the duplication here and
281 sort and uniquify the list after we've processed all symbols. */
282 slot
.cu_indices
.push_back (cu_index_and_attrs
);
285 /* See symtab_index_entry. */
288 symtab_index_entry::minimize ()
290 if (name
== nullptr || cu_indices
.empty ())
293 std::sort (cu_indices
.begin (), cu_indices
.end ());
294 auto from
= std::unique (cu_indices
.begin (), cu_indices
.end ());
295 cu_indices
.erase (from
, cu_indices
.end ());
297 /* We don't want to enter a variable or type more than once, so
298 remove any such duplicates from the list as well. When doing
299 this, we want to keep the entry from the first CU -- but this is
300 implicit due to the sort. This choice is done because it's
301 similar to what gdb historically did for partial symbols. */
302 std::unordered_set
<offset_type
> seen
;
303 from
= std::remove_if (cu_indices
.begin (), cu_indices
.end (),
304 [&] (offset_type val
)
306 gdb_index_symbol_kind kind
= GDB_INDEX_SYMBOL_KIND_VALUE (val
);
307 if (kind
!= GDB_INDEX_SYMBOL_KIND_TYPE
308 && kind
!= GDB_INDEX_SYMBOL_KIND_VARIABLE
)
311 val
&= ~GDB_INDEX_CU_MASK
;
312 return !seen
.insert (val
).second
;
314 cu_indices
.erase (from
, cu_indices
.end ());
317 /* A form of 'const char *' suitable for container keys. Only the
318 pointer is stored. The strings themselves are compared, not the
323 c_str_view (const char *cstr
)
327 bool operator== (const c_str_view
&other
) const
329 return strcmp (m_cstr
, other
.m_cstr
) == 0;
332 /* Return the underlying C string. Note, the returned string is
333 only a reference with lifetime of this object. */
334 const char *c_str () const
340 friend class c_str_view_hasher
;
341 const char *const m_cstr
;
344 /* A std::unordered_map::hasher for c_str_view that uses the right
345 hash function for strings in a mapped index. */
346 class c_str_view_hasher
349 size_t operator () (const c_str_view
&x
) const
351 return mapped_index_string_hash (INT_MAX
, x
.m_cstr
);
355 /* A std::unordered_map::hasher for std::vector<>. */
360 size_t operator () (const std::vector
<T
> &key
) const
362 return iterative_hash (key
.data (),
363 sizeof (key
.front ()) * key
.size (), 0);
367 /* Write the mapped hash table SYMTAB to the data buffer OUTPUT, with
368 constant pool entries going into the data buffer CPOOL. */
371 write_hash_table (mapped_symtab
*symtab
, data_buf
&output
, data_buf
&cpool
)
374 /* Elements are sorted vectors of the indices of all the CUs that
375 hold an object of this name. */
376 std::unordered_map
<std::vector
<offset_type
>, offset_type
,
377 vector_hasher
<offset_type
>>
380 /* We add all the index vectors to the constant pool first, to
381 ensure alignment is ok. */
382 for (symtab_index_entry
&entry
: symtab
->data
)
384 if (entry
.name
== NULL
)
386 gdb_assert (entry
.index_offset
== 0);
388 /* Finding before inserting is faster than always trying to
389 insert, because inserting always allocates a node, does the
390 lookup, and then destroys the new node if another node
391 already had the same key. C++17 try_emplace will avoid
394 = symbol_hash_table
.find (entry
.cu_indices
);
395 if (found
!= symbol_hash_table
.end ())
397 entry
.index_offset
= found
->second
;
401 symbol_hash_table
.emplace (entry
.cu_indices
, cpool
.size ());
402 entry
.index_offset
= cpool
.size ();
403 cpool
.append_offset (entry
.cu_indices
.size ());
404 for (const auto index
: entry
.cu_indices
)
405 cpool
.append_offset (index
);
409 /* Now write out the hash table. */
410 std::unordered_map
<c_str_view
, offset_type
, c_str_view_hasher
> str_table
;
411 for (const auto &entry
: symtab
->data
)
413 offset_type str_off
, vec_off
;
415 if (entry
.name
!= NULL
)
417 const auto insertpair
= str_table
.emplace (entry
.name
, cpool
.size ());
418 if (insertpair
.second
)
419 cpool
.append_cstr0 (entry
.name
);
420 str_off
= insertpair
.first
->second
;
421 vec_off
= entry
.index_offset
;
425 /* While 0 is a valid constant pool index, it is not valid
426 to have 0 for both offsets. */
431 output
.append_offset (str_off
);
432 output
.append_offset (vec_off
);
436 typedef std::unordered_map
<dwarf2_per_cu_data
*, unsigned int> cu_index_map
;
438 /* Helper struct for building the address table. */
439 struct addrmap_index_data
441 addrmap_index_data (data_buf
&addr_vec_
, cu_index_map
&cu_index_htab_
)
442 : addr_vec (addr_vec_
),
443 cu_index_htab (cu_index_htab_
)
447 cu_index_map
&cu_index_htab
;
449 int operator() (CORE_ADDR start_addr
, void *obj
);
451 /* True if the previous_* fields are valid.
452 We can't write an entry until we see the next entry (since it is only then
453 that we know the end of the entry). */
454 bool previous_valid
= false;
455 /* Index of the CU in the table of all CUs in the index file. */
456 unsigned int previous_cu_index
= 0;
457 /* Start address of the CU. */
458 CORE_ADDR previous_cu_start
= 0;
461 /* Write an address entry to ADDR_VEC. */
464 add_address_entry (data_buf
&addr_vec
,
465 CORE_ADDR start
, CORE_ADDR end
, unsigned int cu_index
)
467 addr_vec
.append_uint (8, BFD_ENDIAN_LITTLE
, start
);
468 addr_vec
.append_uint (8, BFD_ENDIAN_LITTLE
, end
);
469 addr_vec
.append_offset (cu_index
);
472 /* Worker function for traversing an addrmap to build the address table. */
475 addrmap_index_data::operator() (CORE_ADDR start_addr
, void *obj
)
477 dwarf2_per_cu_data
*per_cu
= (dwarf2_per_cu_data
*) obj
;
480 add_address_entry (addr_vec
,
481 previous_cu_start
, start_addr
,
484 previous_cu_start
= start_addr
;
487 const auto it
= cu_index_htab
.find (per_cu
);
488 gdb_assert (it
!= cu_index_htab
.cend ());
489 previous_cu_index
= it
->second
;
490 previous_valid
= true;
493 previous_valid
= false;
498 /* Write PER_BFD's address map to ADDR_VEC.
499 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
500 in the index file. */
503 write_address_map (struct addrmap
*addrmap
, data_buf
&addr_vec
,
504 cu_index_map
&cu_index_htab
)
506 struct addrmap_index_data
addrmap_index_data (addr_vec
, cu_index_htab
);
508 addrmap
->foreach (addrmap_index_data
);
510 /* It's highly unlikely the last entry (end address = 0xff...ff)
511 is valid, but we should still handle it.
512 The end address is recorded as the start of the next region, but that
513 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
515 if (addrmap_index_data
.previous_valid
)
516 add_address_entry (addr_vec
,
517 addrmap_index_data
.previous_cu_start
, (CORE_ADDR
) -1,
518 addrmap_index_data
.previous_cu_index
);
521 /* DWARF-5 .debug_names builder. */
525 debug_names (dwarf2_per_objfile
*per_objfile
, bool is_dwarf64
,
526 bfd_endian dwarf5_byte_order
)
527 : m_dwarf5_byte_order (dwarf5_byte_order
),
528 m_dwarf32 (dwarf5_byte_order
),
529 m_dwarf64 (dwarf5_byte_order
),
531 ? static_cast<dwarf
&> (m_dwarf64
)
532 : static_cast<dwarf
&> (m_dwarf32
)),
533 m_name_table_string_offs (m_dwarf
.name_table_string_offs
),
534 m_name_table_entry_offs (m_dwarf
.name_table_entry_offs
),
535 m_debugstrlookup (per_objfile
)
538 int dwarf5_offset_size () const
540 const bool dwarf5_is_dwarf64
= &m_dwarf
== &m_dwarf64
;
541 return dwarf5_is_dwarf64
? 8 : 4;
544 /* Is this symbol from DW_TAG_compile_unit or DW_TAG_type_unit? */
545 enum class unit_kind
{ cu
, tu
};
547 /* Insert one symbol. */
548 void insert (const cooked_index_entry
*entry
)
550 const auto it
= m_cu_index_htab
.find (entry
->per_cu
);
551 gdb_assert (it
!= m_cu_index_htab
.cend ());
552 const char *name
= entry
->full_name (&m_string_obstack
);
554 /* This is incorrect but it mirrors gdb's historical behavior; and
555 because the current .debug_names generation is also incorrect,
556 it seems better to follow what was done before, rather than
557 introduce a mismatch between the newer and older gdb. */
558 dwarf_tag tag
= entry
->tag
;
559 if (tag
!= DW_TAG_typedef
&& tag_is_type (tag
))
560 tag
= DW_TAG_structure_type
;
561 else if (tag
== DW_TAG_enumerator
|| tag
== DW_TAG_constant
)
562 tag
= DW_TAG_variable
;
564 int cu_index
= it
->second
;
565 bool is_static
= (entry
->flags
& IS_STATIC
) != 0;
566 unit_kind kind
= (entry
->per_cu
->is_debug_types
570 if (entry
->per_cu
->lang () == language_ada
)
572 /* We want to ensure that the Ada main function's name appears
573 verbatim in the index. However, this name will be of the
574 form "_ada_mumble", and will be rewritten by ada_decode.
575 So, recognize it specially here and add it to the index by
577 if (strcmp (main_name (), name
) == 0)
579 const auto insertpair
580 = m_name_to_value_set
.emplace (c_str_view (name
),
581 std::set
<symbol_value
> ());
582 std::set
<symbol_value
> &value_set
= insertpair
.first
->second
;
583 value_set
.emplace (symbol_value (tag
, cu_index
, is_static
, kind
));
586 /* In order for the index to work when read back into gdb, it
587 has to supply a funny form of the name: it should be the
588 encoded name, with any suffixes stripped. Using the
589 ordinary encoded name will not work properly with the
590 searching logic in find_name_components_bounds; nor will
591 using the decoded name. Furthermore, an Ada "verbatim"
592 name (of the form "<MumBle>") must be entered without the
593 angle brackets. Note that the current index is unusual,
594 see PR symtab/24820 for details. */
595 std::string decoded
= ada_decode (name
);
596 if (decoded
[0] == '<')
597 name
= (char *) obstack_copy0 (&m_string_obstack
,
598 decoded
.c_str () + 1,
599 decoded
.length () - 2);
601 name
= obstack_strdup (&m_string_obstack
,
602 ada_encode (decoded
.c_str ()));
605 const auto insertpair
606 = m_name_to_value_set
.emplace (c_str_view (name
),
607 std::set
<symbol_value
> ());
608 std::set
<symbol_value
> &value_set
= insertpair
.first
->second
;
609 value_set
.emplace (symbol_value (tag
, cu_index
, is_static
, kind
));
612 /* Build all the tables. All symbols must be already inserted.
613 This function does not call file_write, caller has to do it
617 /* Verify the build method has not be called twice. */
618 gdb_assert (m_abbrev_table
.empty ());
619 const size_t name_count
= m_name_to_value_set
.size ();
620 m_bucket_table
.resize
621 (std::pow (2, std::ceil (std::log2 (name_count
* 4 / 3))));
622 m_hash_table
.reserve (name_count
);
623 m_name_table_string_offs
.reserve (name_count
);
624 m_name_table_entry_offs
.reserve (name_count
);
626 /* Map each hash of symbol to its name and value. */
630 decltype (m_name_to_value_set
)::const_iterator it
;
632 std::vector
<std::forward_list
<hash_it_pair
>> bucket_hash
;
633 bucket_hash
.resize (m_bucket_table
.size ());
634 for (decltype (m_name_to_value_set
)::const_iterator it
635 = m_name_to_value_set
.cbegin ();
636 it
!= m_name_to_value_set
.cend ();
639 const char *const name
= it
->first
.c_str ();
640 const uint32_t hash
= dwarf5_djb_hash (name
);
641 hash_it_pair hashitpair
;
642 hashitpair
.hash
= hash
;
644 auto &slot
= bucket_hash
[hash
% bucket_hash
.size()];
645 slot
.push_front (std::move (hashitpair
));
647 for (size_t bucket_ix
= 0; bucket_ix
< bucket_hash
.size (); ++bucket_ix
)
649 const std::forward_list
<hash_it_pair
> &hashitlist
650 = bucket_hash
[bucket_ix
];
651 if (hashitlist
.empty ())
653 uint32_t &bucket_slot
= m_bucket_table
[bucket_ix
];
654 /* The hashes array is indexed starting at 1. */
655 store_unsigned_integer (reinterpret_cast<gdb_byte
*> (&bucket_slot
),
656 sizeof (bucket_slot
), m_dwarf5_byte_order
,
657 m_hash_table
.size () + 1);
658 for (const hash_it_pair
&hashitpair
: hashitlist
)
660 m_hash_table
.push_back (0);
661 store_unsigned_integer (reinterpret_cast<gdb_byte
*>
662 (&m_hash_table
.back ()),
663 sizeof (m_hash_table
.back ()),
664 m_dwarf5_byte_order
, hashitpair
.hash
);
665 const c_str_view
&name
= hashitpair
.it
->first
;
666 const std::set
<symbol_value
> &value_set
= hashitpair
.it
->second
;
667 m_name_table_string_offs
.push_back_reorder
668 (m_debugstrlookup
.lookup (name
.c_str ()));
669 m_name_table_entry_offs
.push_back_reorder (m_entry_pool
.size ());
670 gdb_assert (!value_set
.empty ());
671 for (const symbol_value
&value
: value_set
)
673 int &idx
= m_indexkey_to_idx
[index_key (value
.dwarf_tag
,
679 m_abbrev_table
.append_unsigned_leb128 (idx
);
680 m_abbrev_table
.append_unsigned_leb128 (value
.dwarf_tag
);
681 m_abbrev_table
.append_unsigned_leb128
682 (value
.kind
== unit_kind::cu
? DW_IDX_compile_unit
684 m_abbrev_table
.append_unsigned_leb128 (DW_FORM_udata
);
685 m_abbrev_table
.append_unsigned_leb128 (value
.is_static
686 ? DW_IDX_GNU_internal
687 : DW_IDX_GNU_external
);
688 m_abbrev_table
.append_unsigned_leb128 (DW_FORM_flag_present
);
690 /* Terminate attributes list. */
691 m_abbrev_table
.append_unsigned_leb128 (0);
692 m_abbrev_table
.append_unsigned_leb128 (0);
695 m_entry_pool
.append_unsigned_leb128 (idx
);
696 m_entry_pool
.append_unsigned_leb128 (value
.cu_index
);
699 /* Terminate the list of CUs. */
700 m_entry_pool
.append_unsigned_leb128 (0);
703 gdb_assert (m_hash_table
.size () == name_count
);
705 /* Terminate tags list. */
706 m_abbrev_table
.append_unsigned_leb128 (0);
709 /* Return .debug_names bucket count. This must be called only after
710 calling the build method. */
711 uint32_t bucket_count () const
713 /* Verify the build method has been already called. */
714 gdb_assert (!m_abbrev_table
.empty ());
715 const uint32_t retval
= m_bucket_table
.size ();
717 /* Check for overflow. */
718 gdb_assert (retval
== m_bucket_table
.size ());
722 /* Return .debug_names names count. This must be called only after
723 calling the build method. */
724 uint32_t name_count () const
726 /* Verify the build method has been already called. */
727 gdb_assert (!m_abbrev_table
.empty ());
728 const uint32_t retval
= m_hash_table
.size ();
730 /* Check for overflow. */
731 gdb_assert (retval
== m_hash_table
.size ());
735 /* Return number of bytes of .debug_names abbreviation table. This
736 must be called only after calling the build method. */
737 uint32_t abbrev_table_bytes () const
739 gdb_assert (!m_abbrev_table
.empty ());
740 return m_abbrev_table
.size ();
743 /* Return number of bytes the .debug_names section will have. This
744 must be called only after calling the build method. */
745 size_t bytes () const
747 /* Verify the build method has been already called. */
748 gdb_assert (!m_abbrev_table
.empty ());
749 size_t expected_bytes
= 0;
750 expected_bytes
+= m_bucket_table
.size () * sizeof (m_bucket_table
[0]);
751 expected_bytes
+= m_hash_table
.size () * sizeof (m_hash_table
[0]);
752 expected_bytes
+= m_name_table_string_offs
.bytes ();
753 expected_bytes
+= m_name_table_entry_offs
.bytes ();
754 expected_bytes
+= m_abbrev_table
.size ();
755 expected_bytes
+= m_entry_pool
.size ();
756 return expected_bytes
;
759 /* Write .debug_names to FILE_NAMES and .debug_str addition to
760 FILE_STR. This must be called only after calling the build
762 void file_write (FILE *file_names
, FILE *file_str
) const
764 /* Verify the build method has been already called. */
765 gdb_assert (!m_abbrev_table
.empty ());
766 ::file_write (file_names
, m_bucket_table
);
767 ::file_write (file_names
, m_hash_table
);
768 m_name_table_string_offs
.file_write (file_names
);
769 m_name_table_entry_offs
.file_write (file_names
);
770 m_abbrev_table
.file_write (file_names
);
771 m_entry_pool
.file_write (file_names
);
772 m_debugstrlookup
.file_write (file_str
);
775 void add_cu (dwarf2_per_cu_data
*per_cu
, offset_type index
)
777 m_cu_index_htab
.emplace (per_cu
, index
);
782 /* Storage for symbol names mapping them to their .debug_str section
784 class debug_str_lookup
788 /* Object constructor to be called for current DWARF2_PER_OBJFILE.
789 All .debug_str section strings are automatically stored. */
790 debug_str_lookup (dwarf2_per_objfile
*per_objfile
)
791 : m_abfd (per_objfile
->objfile
->obfd
.get ()),
792 m_per_objfile (per_objfile
)
794 per_objfile
->per_bfd
->str
.read (per_objfile
->objfile
);
795 if (per_objfile
->per_bfd
->str
.buffer
== NULL
)
797 for (const gdb_byte
*data
= per_objfile
->per_bfd
->str
.buffer
;
798 data
< (per_objfile
->per_bfd
->str
.buffer
799 + per_objfile
->per_bfd
->str
.size
);)
801 const char *const s
= reinterpret_cast<const char *> (data
);
802 const auto insertpair
803 = m_str_table
.emplace (c_str_view (s
),
804 data
- per_objfile
->per_bfd
->str
.buffer
);
805 if (!insertpair
.second
)
806 complaint (_("Duplicate string \"%s\" in "
807 ".debug_str section [in module %s]"),
808 s
, bfd_get_filename (m_abfd
));
809 data
+= strlen (s
) + 1;
813 /* Return offset of symbol name S in the .debug_str section. Add
814 such symbol to the section's end if it does not exist there
816 size_t lookup (const char *s
)
818 const auto it
= m_str_table
.find (c_str_view (s
));
819 if (it
!= m_str_table
.end ())
821 const size_t offset
= (m_per_objfile
->per_bfd
->str
.size
822 + m_str_add_buf
.size ());
823 m_str_table
.emplace (c_str_view (s
), offset
);
824 m_str_add_buf
.append_cstr0 (s
);
828 /* Append the end of the .debug_str section to FILE. */
829 void file_write (FILE *file
) const
831 m_str_add_buf
.file_write (file
);
835 std::unordered_map
<c_str_view
, size_t, c_str_view_hasher
> m_str_table
;
837 dwarf2_per_objfile
*m_per_objfile
;
839 /* Data to add at the end of .debug_str for new needed symbol names. */
840 data_buf m_str_add_buf
;
843 /* Container to map used DWARF tags to their .debug_names abbreviation
848 index_key (int dwarf_tag_
, bool is_static_
, unit_kind kind_
)
849 : dwarf_tag (dwarf_tag_
), is_static (is_static_
), kind (kind_
)
854 operator== (const index_key
&other
) const
856 return (dwarf_tag
== other
.dwarf_tag
&& is_static
== other
.is_static
857 && kind
== other
.kind
);
861 const bool is_static
;
862 const unit_kind kind
;
865 /* Provide std::unordered_map::hasher for index_key. */
866 class index_key_hasher
870 operator () (const index_key
&key
) const
872 return (std::hash
<int>() (key
.dwarf_tag
) << 1) | key
.is_static
;
876 /* Parameters of one symbol entry. */
880 const int dwarf_tag
, cu_index
;
881 const bool is_static
;
882 const unit_kind kind
;
884 symbol_value (int dwarf_tag_
, int cu_index_
, bool is_static_
,
886 : dwarf_tag (dwarf_tag_
), cu_index (cu_index_
), is_static (is_static_
),
891 operator< (const symbol_value
&other
) const
911 /* Abstract base class to unify DWARF-32 and DWARF-64 name table
916 const bfd_endian dwarf5_byte_order
;
918 explicit offset_vec (bfd_endian dwarf5_byte_order_
)
919 : dwarf5_byte_order (dwarf5_byte_order_
)
922 /* Call std::vector::reserve for NELEM elements. */
923 virtual void reserve (size_t nelem
) = 0;
925 /* Call std::vector::push_back with store_unsigned_integer byte
926 reordering for ELEM. */
927 virtual void push_back_reorder (size_t elem
) = 0;
929 /* Return expected output size in bytes. */
930 virtual size_t bytes () const = 0;
932 /* Write name table to FILE. */
933 virtual void file_write (FILE *file
) const = 0;
936 /* Template to unify DWARF-32 and DWARF-64 output. */
937 template<typename OffsetSize
>
938 class offset_vec_tmpl
: public offset_vec
941 explicit offset_vec_tmpl (bfd_endian dwarf5_byte_order_
)
942 : offset_vec (dwarf5_byte_order_
)
945 /* Implement offset_vec::reserve. */
946 void reserve (size_t nelem
) override
948 m_vec
.reserve (nelem
);
951 /* Implement offset_vec::push_back_reorder. */
952 void push_back_reorder (size_t elem
) override
954 m_vec
.push_back (elem
);
955 /* Check for overflow. */
956 gdb_assert (m_vec
.back () == elem
);
957 store_unsigned_integer (reinterpret_cast<gdb_byte
*> (&m_vec
.back ()),
958 sizeof (m_vec
.back ()), dwarf5_byte_order
, elem
);
961 /* Implement offset_vec::bytes. */
962 size_t bytes () const override
964 return m_vec
.size () * sizeof (m_vec
[0]);
967 /* Implement offset_vec::file_write. */
968 void file_write (FILE *file
) const override
970 ::file_write (file
, m_vec
);
974 std::vector
<OffsetSize
> m_vec
;
977 /* Base class to unify DWARF-32 and DWARF-64 .debug_names output
978 respecting name table width. */
982 offset_vec
&name_table_string_offs
, &name_table_entry_offs
;
984 dwarf (offset_vec
&name_table_string_offs_
,
985 offset_vec
&name_table_entry_offs_
)
986 : name_table_string_offs (name_table_string_offs_
),
987 name_table_entry_offs (name_table_entry_offs_
)
992 /* Template to unify DWARF-32 and DWARF-64 .debug_names output
993 respecting name table width. */
994 template<typename OffsetSize
>
995 class dwarf_tmpl
: public dwarf
998 explicit dwarf_tmpl (bfd_endian dwarf5_byte_order_
)
999 : dwarf (m_name_table_string_offs
, m_name_table_entry_offs
),
1000 m_name_table_string_offs (dwarf5_byte_order_
),
1001 m_name_table_entry_offs (dwarf5_byte_order_
)
1005 offset_vec_tmpl
<OffsetSize
> m_name_table_string_offs
;
1006 offset_vec_tmpl
<OffsetSize
> m_name_table_entry_offs
;
1009 /* Store value of each symbol. */
1010 std::unordered_map
<c_str_view
, std::set
<symbol_value
>, c_str_view_hasher
>
1011 m_name_to_value_set
;
1013 /* Tables of DWARF-5 .debug_names. They are in object file byte
1015 std::vector
<uint32_t> m_bucket_table
;
1016 std::vector
<uint32_t> m_hash_table
;
1018 const bfd_endian m_dwarf5_byte_order
;
1019 dwarf_tmpl
<uint32_t> m_dwarf32
;
1020 dwarf_tmpl
<uint64_t> m_dwarf64
;
1022 offset_vec
&m_name_table_string_offs
, &m_name_table_entry_offs
;
1023 debug_str_lookup m_debugstrlookup
;
1025 /* Map each used .debug_names abbreviation tag parameter to its
1027 std::unordered_map
<index_key
, int, index_key_hasher
> m_indexkey_to_idx
;
1029 /* Next unused .debug_names abbreviation tag for
1030 m_indexkey_to_idx. */
1033 /* .debug_names abbreviation table. */
1034 data_buf m_abbrev_table
;
1036 /* .debug_names entry pool. */
1037 data_buf m_entry_pool
;
1039 /* Temporary storage for Ada names. */
1040 auto_obstack m_string_obstack
;
1042 cu_index_map m_cu_index_htab
;
1045 /* Return iff any of the needed offsets does not fit into 32-bit
1046 .debug_names section. */
1049 check_dwarf64_offsets (dwarf2_per_objfile
*per_objfile
)
1051 for (const auto &per_cu
: per_objfile
->per_bfd
->all_units
)
1053 if (to_underlying (per_cu
->sect_off
)
1054 >= (static_cast<uint64_t> (1) << 32))
1060 /* Assert that FILE's size is EXPECTED_SIZE. Assumes file's seek
1061 position is at the end of the file. */
1064 assert_file_size (FILE *file
, size_t expected_size
)
1066 const auto file_size
= ftell (file
);
1067 if (file_size
== -1)
1068 perror_with_name (("ftell"));
1069 gdb_assert (file_size
== expected_size
);
1072 /* Write a gdb index file to OUT_FILE from all the sections passed as
1076 write_gdbindex_1 (FILE *out_file
,
1077 const data_buf
&cu_list
,
1078 const data_buf
&types_cu_list
,
1079 const data_buf
&addr_vec
,
1080 const data_buf
&symtab_vec
,
1081 const data_buf
&constant_pool
)
1084 const offset_type size_of_header
= 6 * sizeof (offset_type
);
1085 offset_type total_len
= size_of_header
;
1087 /* The version number. */
1088 contents
.append_offset (8);
1090 /* The offset of the CU list from the start of the file. */
1091 contents
.append_offset (total_len
);
1092 total_len
+= cu_list
.size ();
1094 /* The offset of the types CU list from the start of the file. */
1095 contents
.append_offset (total_len
);
1096 total_len
+= types_cu_list
.size ();
1098 /* The offset of the address table from the start of the file. */
1099 contents
.append_offset (total_len
);
1100 total_len
+= addr_vec
.size ();
1102 /* The offset of the symbol table from the start of the file. */
1103 contents
.append_offset (total_len
);
1104 total_len
+= symtab_vec
.size ();
1106 /* The offset of the constant pool from the start of the file. */
1107 contents
.append_offset (total_len
);
1108 total_len
+= constant_pool
.size ();
1110 gdb_assert (contents
.size () == size_of_header
);
1112 contents
.file_write (out_file
);
1113 cu_list
.file_write (out_file
);
1114 types_cu_list
.file_write (out_file
);
1115 addr_vec
.file_write (out_file
);
1116 symtab_vec
.file_write (out_file
);
1117 constant_pool
.file_write (out_file
);
1119 assert_file_size (out_file
, total_len
);
1122 /* Write the contents of the internal "cooked" index. */
1125 write_cooked_index (cooked_index_vector
*table
,
1126 const cu_index_map
&cu_index_htab
,
1127 struct mapped_symtab
*symtab
)
1129 const char *main_for_ada
= main_name ();
1131 for (const cooked_index_entry
*entry
: table
->all_entries ())
1133 const auto it
= cu_index_htab
.find (entry
->per_cu
);
1134 gdb_assert (it
!= cu_index_htab
.cend ());
1136 const char *name
= entry
->full_name (&symtab
->m_string_obstack
);
1138 if (entry
->per_cu
->lang () == language_ada
)
1140 /* We want to ensure that the Ada main function's name
1141 appears verbatim in the index. However, this name will
1142 be of the form "_ada_mumble", and will be rewritten by
1143 ada_decode. So, recognize it specially here and add it
1144 to the index by hand. */
1145 if (entry
->tag
== DW_TAG_subprogram
1146 && strcmp (main_for_ada
, name
) == 0)
1148 /* Leave it alone. */
1152 /* In order for the index to work when read back into
1153 gdb, it has to use the encoded name, with any
1154 suffixes stripped. */
1155 std::string encoded
= ada_encode (name
, false);
1156 name
= obstack_strdup (&symtab
->m_string_obstack
,
1160 else if (entry
->per_cu
->lang () == language_cplus
1161 && (entry
->flags
& IS_LINKAGE
) != 0)
1163 /* GDB never put C++ linkage names into .gdb_index. The
1164 theory here is that a linkage name will normally be in
1165 the minimal symbols anyway, so including it in the index
1166 is usually redundant -- and the cases where it would not
1167 be redundant are rare and not worth supporting. */
1170 else if ((entry
->flags
& IS_TYPE_DECLARATION
) != 0)
1172 /* Don't add type declarations to the index. */
1176 gdb_index_symbol_kind kind
;
1177 if (entry
->tag
== DW_TAG_subprogram
)
1178 kind
= GDB_INDEX_SYMBOL_KIND_FUNCTION
;
1179 else if (entry
->tag
== DW_TAG_variable
1180 || entry
->tag
== DW_TAG_constant
1181 || entry
->tag
== DW_TAG_enumerator
)
1182 kind
= GDB_INDEX_SYMBOL_KIND_VARIABLE
;
1183 else if (entry
->tag
== DW_TAG_module
1184 || entry
->tag
== DW_TAG_common_block
)
1185 kind
= GDB_INDEX_SYMBOL_KIND_OTHER
;
1187 kind
= GDB_INDEX_SYMBOL_KIND_TYPE
;
1189 add_index_entry (symtab
, name
, (entry
->flags
& IS_STATIC
) != 0,
1194 /* Write contents of a .gdb_index section for OBJFILE into OUT_FILE.
1195 If OBJFILE has an associated dwz file, write contents of a .gdb_index
1196 section for that dwz file into DWZ_OUT_FILE. If OBJFILE does not have an
1197 associated dwz file, DWZ_OUT_FILE must be NULL. */
1200 write_gdbindex (dwarf2_per_objfile
*per_objfile
,
1201 cooked_index_vector
*table
,
1202 FILE *out_file
, FILE *dwz_out_file
)
1204 mapped_symtab symtab
;
1205 data_buf objfile_cu_list
;
1206 data_buf dwz_cu_list
;
1208 /* While we're scanning CU's create a table that maps a dwarf2_per_cu_data
1209 (which is what addrmap records) to its index (which is what is recorded
1210 in the index file). This will later be needed to write the address
1212 cu_index_map cu_index_htab
;
1213 cu_index_htab
.reserve (per_objfile
->per_bfd
->all_units
.size ());
1215 /* Store out the .debug_type CUs, if any. */
1216 data_buf types_cu_list
;
1218 /* The CU list is already sorted, so we don't need to do additional
1219 work here. Also, the debug_types entries do not appear in
1220 all_units, but only in their own hash table. */
1223 int types_counter
= 0;
1224 for (int i
= 0; i
< per_objfile
->per_bfd
->all_units
.size (); ++i
)
1226 dwarf2_per_cu_data
*per_cu
1227 = per_objfile
->per_bfd
->all_units
[i
].get ();
1229 int &this_counter
= per_cu
->is_debug_types
? types_counter
: counter
;
1231 const auto insertpair
= cu_index_htab
.emplace (per_cu
, this_counter
);
1232 gdb_assert (insertpair
.second
);
1234 /* The all_units list contains CUs read from the objfile as well as
1235 from the eventual dwz file. We need to place the entry in the
1236 corresponding index. */
1237 data_buf
&cu_list
= (per_cu
->is_debug_types
1239 : per_cu
->is_dwz
? dwz_cu_list
: objfile_cu_list
);
1240 cu_list
.append_uint (8, BFD_ENDIAN_LITTLE
,
1241 to_underlying (per_cu
->sect_off
));
1242 if (per_cu
->is_debug_types
)
1244 signatured_type
*sig_type
= (signatured_type
*) per_cu
;
1245 cu_list
.append_uint (8, BFD_ENDIAN_LITTLE
,
1246 to_underlying (sig_type
->type_offset_in_tu
));
1247 cu_list
.append_uint (8, BFD_ENDIAN_LITTLE
,
1248 sig_type
->signature
);
1251 cu_list
.append_uint (8, BFD_ENDIAN_LITTLE
, per_cu
->length ());
1256 write_cooked_index (table
, cu_index_htab
, &symtab
);
1258 /* Dump the address map. */
1260 for (auto map
: table
->get_addrmaps ())
1261 write_address_map (map
, addr_vec
, cu_index_htab
);
1263 /* Now that we've processed all symbols we can shrink their cu_indices
1267 data_buf symtab_vec
, constant_pool
;
1268 if (symtab
.n_elements
== 0)
1269 symtab
.data
.resize (0);
1271 write_hash_table (&symtab
, symtab_vec
, constant_pool
);
1273 write_gdbindex_1(out_file
, objfile_cu_list
, types_cu_list
, addr_vec
,
1274 symtab_vec
, constant_pool
);
1276 if (dwz_out_file
!= NULL
)
1277 write_gdbindex_1 (dwz_out_file
, dwz_cu_list
, {}, {}, {}, {});
1279 gdb_assert (dwz_cu_list
.empty ());
1282 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
1283 static const gdb_byte dwarf5_gdb_augmentation
[] = { 'G', 'D', 'B', 0 };
1285 /* Write a new .debug_names section for OBJFILE into OUT_FILE, write
1286 needed addition to .debug_str section to OUT_FILE_STR. Return how
1287 many bytes were expected to be written into OUT_FILE. */
1290 write_debug_names (dwarf2_per_objfile
*per_objfile
,
1291 cooked_index_vector
*table
,
1292 FILE *out_file
, FILE *out_file_str
)
1294 const bool dwarf5_is_dwarf64
= check_dwarf64_offsets (per_objfile
);
1295 struct objfile
*objfile
= per_objfile
->objfile
;
1296 const enum bfd_endian dwarf5_byte_order
1297 = gdbarch_byte_order (objfile
->arch ());
1299 /* The CU list is already sorted, so we don't need to do additional
1300 work here. Also, the debug_types entries do not appear in
1301 all_units, but only in their own hash table. */
1303 data_buf types_cu_list
;
1304 debug_names
nametable (per_objfile
, dwarf5_is_dwarf64
, dwarf5_byte_order
);
1306 int types_counter
= 0;
1307 for (int i
= 0; i
< per_objfile
->per_bfd
->all_units
.size (); ++i
)
1309 dwarf2_per_cu_data
*per_cu
1310 = per_objfile
->per_bfd
->all_units
[i
].get ();
1312 int &this_counter
= per_cu
->is_debug_types
? types_counter
: counter
;
1313 data_buf
&this_list
= per_cu
->is_debug_types
? types_cu_list
: cu_list
;
1315 nametable
.add_cu (per_cu
, this_counter
);
1316 this_list
.append_uint (nametable
.dwarf5_offset_size (),
1318 to_underlying (per_cu
->sect_off
));
1322 /* Verify that all units are represented. */
1323 gdb_assert (counter
== per_objfile
->per_bfd
->all_comp_units
.size ());
1324 gdb_assert (types_counter
== per_objfile
->per_bfd
->all_type_units
.size ());
1326 for (const cooked_index_entry
*entry
: table
->all_entries ())
1327 nametable
.insert (entry
);
1331 /* No addr_vec - DWARF-5 uses .debug_aranges generated by GCC. */
1333 const offset_type bytes_of_header
1334 = ((dwarf5_is_dwarf64
? 12 : 4)
1336 + sizeof (dwarf5_gdb_augmentation
));
1337 size_t expected_bytes
= 0;
1338 expected_bytes
+= bytes_of_header
;
1339 expected_bytes
+= cu_list
.size ();
1340 expected_bytes
+= types_cu_list
.size ();
1341 expected_bytes
+= nametable
.bytes ();
1344 if (!dwarf5_is_dwarf64
)
1346 const uint64_t size64
= expected_bytes
- 4;
1347 gdb_assert (size64
< 0xfffffff0);
1348 header
.append_uint (4, dwarf5_byte_order
, size64
);
1352 header
.append_uint (4, dwarf5_byte_order
, 0xffffffff);
1353 header
.append_uint (8, dwarf5_byte_order
, expected_bytes
- 12);
1356 /* The version number. */
1357 header
.append_uint (2, dwarf5_byte_order
, 5);
1360 header
.append_uint (2, dwarf5_byte_order
, 0);
1362 /* comp_unit_count - The number of CUs in the CU list. */
1363 header
.append_uint (4, dwarf5_byte_order
, counter
);
1365 /* local_type_unit_count - The number of TUs in the local TU
1367 header
.append_uint (4, dwarf5_byte_order
, types_counter
);
1369 /* foreign_type_unit_count - The number of TUs in the foreign TU
1371 header
.append_uint (4, dwarf5_byte_order
, 0);
1373 /* bucket_count - The number of hash buckets in the hash lookup
1375 header
.append_uint (4, dwarf5_byte_order
, nametable
.bucket_count ());
1377 /* name_count - The number of unique names in the index. */
1378 header
.append_uint (4, dwarf5_byte_order
, nametable
.name_count ());
1380 /* abbrev_table_size - The size in bytes of the abbreviations
1382 header
.append_uint (4, dwarf5_byte_order
, nametable
.abbrev_table_bytes ());
1384 /* augmentation_string_size - The size in bytes of the augmentation
1385 string. This value is rounded up to a multiple of 4. */
1386 static_assert (sizeof (dwarf5_gdb_augmentation
) % 4 == 0, "");
1387 header
.append_uint (4, dwarf5_byte_order
, sizeof (dwarf5_gdb_augmentation
));
1388 header
.append_array (dwarf5_gdb_augmentation
);
1390 gdb_assert (header
.size () == bytes_of_header
);
1392 header
.file_write (out_file
);
1393 cu_list
.file_write (out_file
);
1394 types_cu_list
.file_write (out_file
);
1395 nametable
.file_write (out_file
, out_file_str
);
1397 assert_file_size (out_file
, expected_bytes
);
1400 /* This represents an index file being written (work-in-progress).
1402 The data is initially written to a temporary file. When the finalize method
1403 is called, the file is closed and moved to its final location.
1405 On failure (if this object is being destroyed with having called finalize),
1406 the temporary file is closed and deleted. */
1408 struct index_wip_file
1410 index_wip_file (const char *dir
, const char *basename
,
1413 filename
= (std::string (dir
) + SLASH_STRING
+ basename
1416 filename_temp
= make_temp_filename (filename
);
1418 scoped_fd out_file_fd
= gdb_mkostemp_cloexec (filename_temp
.data (),
1420 if (out_file_fd
.get () == -1)
1421 perror_with_name (("mkstemp"));
1423 out_file
= out_file_fd
.to_file ("wb");
1425 if (out_file
== nullptr)
1426 error (_("Can't open `%s' for writing"), filename_temp
.data ());
1428 unlink_file
.emplace (filename_temp
.data ());
1433 /* We want to keep the file. */
1434 unlink_file
->keep ();
1436 /* Close and move the str file in place. */
1437 unlink_file
.reset ();
1438 if (rename (filename_temp
.data (), filename
.c_str ()) != 0)
1439 perror_with_name (("rename"));
1442 std::string filename
;
1443 gdb::char_vector filename_temp
;
1445 /* Order matters here; we want FILE to be closed before
1446 FILENAME_TEMP is unlinked, because on MS-Windows one cannot
1447 delete a file that is still open. So, we wrap the unlinker in an
1448 optional and emplace it once we know the file name. */
1449 gdb::optional
<gdb::unlinker
> unlink_file
;
1451 gdb_file_up out_file
;
1454 /* See dwarf-index-write.h. */
1457 write_dwarf_index (dwarf2_per_objfile
*per_objfile
, const char *dir
,
1458 const char *basename
, const char *dwz_basename
,
1459 dw_index_kind index_kind
)
1461 struct objfile
*objfile
= per_objfile
->objfile
;
1463 if (per_objfile
->per_bfd
->index_table
== nullptr)
1464 error (_("No debugging symbols"));
1465 cooked_index_vector
*table
1466 = per_objfile
->per_bfd
->index_table
->index_for_writing ();
1468 if (per_objfile
->per_bfd
->types
.size () > 1)
1469 error (_("Cannot make an index when the file has multiple .debug_types sections"));
1472 gdb_assert ((objfile
->flags
& OBJF_NOT_FILENAME
) == 0);
1474 const char *index_suffix
= (index_kind
== dw_index_kind::DEBUG_NAMES
1475 ? INDEX5_SUFFIX
: INDEX4_SUFFIX
);
1477 index_wip_file
objfile_index_wip (dir
, basename
, index_suffix
);
1478 gdb::optional
<index_wip_file
> dwz_index_wip
;
1480 if (dwz_basename
!= NULL
)
1481 dwz_index_wip
.emplace (dir
, dwz_basename
, index_suffix
);
1483 if (index_kind
== dw_index_kind::DEBUG_NAMES
)
1485 index_wip_file
str_wip_file (dir
, basename
, DEBUG_STR_SUFFIX
);
1487 write_debug_names (per_objfile
, table
, objfile_index_wip
.out_file
.get (),
1488 str_wip_file
.out_file
.get ());
1490 str_wip_file
.finalize ();
1493 write_gdbindex (per_objfile
, table
, objfile_index_wip
.out_file
.get (),
1494 (dwz_index_wip
.has_value ()
1495 ? dwz_index_wip
->out_file
.get () : NULL
));
1497 objfile_index_wip
.finalize ();
1499 if (dwz_index_wip
.has_value ())
1500 dwz_index_wip
->finalize ();
1503 /* Implementation of the `save gdb-index' command.
1505 Note that the .gdb_index file format used by this command is
1506 documented in the GDB manual. Any changes here must be documented
1510 save_gdb_index_command (const char *arg
, int from_tty
)
1512 const char dwarf5space
[] = "-dwarf-5 ";
1513 dw_index_kind index_kind
= dw_index_kind::GDB_INDEX
;
1518 arg
= skip_spaces (arg
);
1519 if (strncmp (arg
, dwarf5space
, strlen (dwarf5space
)) == 0)
1521 index_kind
= dw_index_kind::DEBUG_NAMES
;
1522 arg
+= strlen (dwarf5space
);
1523 arg
= skip_spaces (arg
);
1527 error (_("usage: save gdb-index [-dwarf-5] DIRECTORY"));
1529 for (objfile
*objfile
: current_program_space
->objfiles ())
1531 /* If the objfile does not correspond to an actual file, skip it. */
1532 if ((objfile
->flags
& OBJF_NOT_FILENAME
) != 0)
1535 dwarf2_per_objfile
*per_objfile
= get_dwarf2_per_objfile (objfile
);
1537 if (per_objfile
!= NULL
)
1541 const char *basename
= lbasename (objfile_name (objfile
));
1542 const dwz_file
*dwz
= dwarf2_get_dwz_file (per_objfile
->per_bfd
);
1543 const char *dwz_basename
= NULL
;
1546 dwz_basename
= lbasename (dwz
->filename ());
1548 write_dwarf_index (per_objfile
, arg
, basename
, dwz_basename
,
1551 catch (const gdb_exception_error
&except
)
1553 exception_fprintf (gdb_stderr
, except
,
1554 _("Error while writing index for `%s': "),
1555 objfile_name (objfile
));
1562 void _initialize_dwarf_index_write ();
1564 _initialize_dwarf_index_write ()
1566 cmd_list_element
*c
= add_cmd ("gdb-index", class_files
,
1567 save_gdb_index_command
, _("\
1568 Save a gdb-index file.\n\
1569 Usage: save gdb-index [-dwarf-5] DIRECTORY\n\
1571 No options create one file with .gdb-index extension for pre-DWARF-5\n\
1572 compatible .gdb_index section. With -dwarf-5 creates two files with\n\
1573 extension .debug_names and .debug_str for DWARF-5 .debug_names section."),
1575 set_cmd_completer (c
, filename_completer
);