1 /* DWARF index writing support for GDB.
3 Copyright (C) 1994-2018 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/>. */
23 #include "cli/cli-decode.h"
24 #include "common/byte-vector.h"
25 #include "common/filestuff.h"
26 #include "common/gdb_unlinker.h"
27 #include "complaints.h"
28 #include "dwarf-index-common.h"
30 #include "dwarf2read.h"
31 #include "gdb/gdb-index.h"
39 #include <unordered_map>
40 #include <unordered_set>
42 /* The suffix for an index file. */
43 #define INDEX4_SUFFIX ".gdb-index"
44 #define INDEX5_SUFFIX ".debug_names"
45 #define DEBUG_STR_SUFFIX ".debug_str"
47 /* Ensure only legit values are used. */
48 #define DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE(cu_index, value) \
50 gdb_assert ((unsigned int) (value) <= 1); \
51 GDB_INDEX_SYMBOL_STATIC_SET_VALUE((cu_index), (value)); \
54 /* Ensure only legit values are used. */
55 #define DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE(cu_index, value) \
57 gdb_assert ((value) >= GDB_INDEX_SYMBOL_KIND_TYPE \
58 && (value) <= GDB_INDEX_SYMBOL_KIND_OTHER); \
59 GDB_INDEX_SYMBOL_KIND_SET_VALUE((cu_index), (value)); \
62 /* Ensure we don't use more than the alloted nuber of bits for the CU. */
63 #define DW2_GDB_INDEX_CU_SET_VALUE(cu_index, value) \
65 gdb_assert (((value) & ~GDB_INDEX_CU_MASK) == 0); \
66 GDB_INDEX_CU_SET_VALUE((cu_index), (value)); \
69 /* The "save gdb-index" command. */
71 /* Write SIZE bytes from the buffer pointed to by DATA to FILE, with
75 file_write (FILE *file
, const void *data
, size_t size
)
77 if (fwrite (data
, 1, size
, file
) != size
)
78 error (_("couldn't data write to file"));
81 /* Write the contents of VEC to FILE, with error checking. */
83 template<typename Elem
, typename Alloc
>
85 file_write (FILE *file
, const std::vector
<Elem
, Alloc
> &vec
)
87 file_write (file
, vec
.data (), vec
.size () * sizeof (vec
[0]));
90 /* In-memory buffer to prepare data to be written later to a file. */
94 /* Copy DATA to the end of the buffer. */
96 void append_data (const T
&data
)
98 std::copy (reinterpret_cast<const gdb_byte
*> (&data
),
99 reinterpret_cast<const gdb_byte
*> (&data
+ 1),
100 grow (sizeof (data
)));
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 append_data (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 /* Return the size of the buffer. */
136 return m_vec
.size ();
139 /* Return true iff the buffer is empty. */
142 return m_vec
.empty ();
145 /* Write the buffer to FILE. */
146 void file_write (FILE *file
) const
148 ::file_write (file
, m_vec
);
152 /* Grow SIZE bytes at the end of the buffer. Returns a pointer to
153 the start of the new block. */
154 gdb_byte
*grow (size_t size
)
156 m_vec
.resize (m_vec
.size () + size
);
157 return &*(m_vec
.end () - size
);
160 gdb::byte_vector m_vec
;
163 /* An entry in the symbol table. */
164 struct symtab_index_entry
166 /* The name of the symbol. */
168 /* The offset of the name in the constant pool. */
169 offset_type index_offset
;
170 /* A sorted vector of the indices of all the CUs that hold an object
172 std::vector
<offset_type
> cu_indices
;
175 /* The symbol table. This is a power-of-2-sized hash table. */
183 offset_type n_elements
= 0;
184 std::vector
<symtab_index_entry
> data
;
187 /* Find a slot in SYMTAB for the symbol NAME. Returns a reference to
190 Function is used only during write_hash_table so no index format backward
191 compatibility is needed. */
193 static symtab_index_entry
&
194 find_slot (struct mapped_symtab
*symtab
, const char *name
)
196 offset_type index
, step
, hash
= mapped_index_string_hash (INT_MAX
, name
);
198 index
= hash
& (symtab
->data
.size () - 1);
199 step
= ((hash
* 17) & (symtab
->data
.size () - 1)) | 1;
203 if (symtab
->data
[index
].name
== NULL
204 || strcmp (name
, symtab
->data
[index
].name
) == 0)
205 return symtab
->data
[index
];
206 index
= (index
+ step
) & (symtab
->data
.size () - 1);
210 /* Expand SYMTAB's hash table. */
213 hash_expand (struct mapped_symtab
*symtab
)
215 auto old_entries
= std::move (symtab
->data
);
217 symtab
->data
.clear ();
218 symtab
->data
.resize (old_entries
.size () * 2);
220 for (auto &it
: old_entries
)
223 auto &ref
= find_slot (symtab
, it
.name
);
224 ref
= std::move (it
);
228 /* Add an entry to SYMTAB. NAME is the name of the symbol.
229 CU_INDEX is the index of the CU in which the symbol appears.
230 IS_STATIC is one if the symbol is static, otherwise zero (global). */
233 add_index_entry (struct mapped_symtab
*symtab
, const char *name
,
234 int is_static
, gdb_index_symbol_kind kind
,
235 offset_type cu_index
)
237 offset_type cu_index_and_attrs
;
239 ++symtab
->n_elements
;
240 if (4 * symtab
->n_elements
/ 3 >= symtab
->data
.size ())
241 hash_expand (symtab
);
243 symtab_index_entry
&slot
= find_slot (symtab
, name
);
244 if (slot
.name
== NULL
)
247 /* index_offset is set later. */
250 cu_index_and_attrs
= 0;
251 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs
, cu_index
);
252 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs
, is_static
);
253 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs
, kind
);
255 /* We don't want to record an index value twice as we want to avoid the
257 We process all global symbols and then all static symbols
258 (which would allow us to avoid the duplication by only having to check
259 the last entry pushed), but a symbol could have multiple kinds in one CU.
260 To keep things simple we don't worry about the duplication here and
261 sort and uniqufy the list after we've processed all symbols. */
262 slot
.cu_indices
.push_back (cu_index_and_attrs
);
265 /* Sort and remove duplicates of all symbols' cu_indices lists. */
268 uniquify_cu_indices (struct mapped_symtab
*symtab
)
270 for (auto &entry
: symtab
->data
)
272 if (entry
.name
!= NULL
&& !entry
.cu_indices
.empty ())
274 auto &cu_indices
= entry
.cu_indices
;
275 std::sort (cu_indices
.begin (), cu_indices
.end ());
276 auto from
= std::unique (cu_indices
.begin (), cu_indices
.end ());
277 cu_indices
.erase (from
, cu_indices
.end ());
282 /* A form of 'const char *' suitable for container keys. Only the
283 pointer is stored. The strings themselves are compared, not the
288 c_str_view (const char *cstr
)
292 bool operator== (const c_str_view
&other
) const
294 return strcmp (m_cstr
, other
.m_cstr
) == 0;
297 /* Return the underlying C string. Note, the returned string is
298 only a reference with lifetime of this object. */
299 const char *c_str () const
305 friend class c_str_view_hasher
;
306 const char *const m_cstr
;
309 /* A std::unordered_map::hasher for c_str_view that uses the right
310 hash function for strings in a mapped index. */
311 class c_str_view_hasher
314 size_t operator () (const c_str_view
&x
) const
316 return mapped_index_string_hash (INT_MAX
, x
.m_cstr
);
320 /* A std::unordered_map::hasher for std::vector<>. */
325 size_t operator () (const std::vector
<T
> &key
) const
327 return iterative_hash (key
.data (),
328 sizeof (key
.front ()) * key
.size (), 0);
332 /* Write the mapped hash table SYMTAB to the data buffer OUTPUT, with
333 constant pool entries going into the data buffer CPOOL. */
336 write_hash_table (mapped_symtab
*symtab
, data_buf
&output
, data_buf
&cpool
)
339 /* Elements are sorted vectors of the indices of all the CUs that
340 hold an object of this name. */
341 std::unordered_map
<std::vector
<offset_type
>, offset_type
,
342 vector_hasher
<offset_type
>>
345 /* We add all the index vectors to the constant pool first, to
346 ensure alignment is ok. */
347 for (symtab_index_entry
&entry
: symtab
->data
)
349 if (entry
.name
== NULL
)
351 gdb_assert (entry
.index_offset
== 0);
353 /* Finding before inserting is faster than always trying to
354 insert, because inserting always allocates a node, does the
355 lookup, and then destroys the new node if another node
356 already had the same key. C++17 try_emplace will avoid
359 = symbol_hash_table
.find (entry
.cu_indices
);
360 if (found
!= symbol_hash_table
.end ())
362 entry
.index_offset
= found
->second
;
366 symbol_hash_table
.emplace (entry
.cu_indices
, cpool
.size ());
367 entry
.index_offset
= cpool
.size ();
368 cpool
.append_data (MAYBE_SWAP (entry
.cu_indices
.size ()));
369 for (const auto index
: entry
.cu_indices
)
370 cpool
.append_data (MAYBE_SWAP (index
));
374 /* Now write out the hash table. */
375 std::unordered_map
<c_str_view
, offset_type
, c_str_view_hasher
> str_table
;
376 for (const auto &entry
: symtab
->data
)
378 offset_type str_off
, vec_off
;
380 if (entry
.name
!= NULL
)
382 const auto insertpair
= str_table
.emplace (entry
.name
, cpool
.size ());
383 if (insertpair
.second
)
384 cpool
.append_cstr0 (entry
.name
);
385 str_off
= insertpair
.first
->second
;
386 vec_off
= entry
.index_offset
;
390 /* While 0 is a valid constant pool index, it is not valid
391 to have 0 for both offsets. */
396 output
.append_data (MAYBE_SWAP (str_off
));
397 output
.append_data (MAYBE_SWAP (vec_off
));
401 typedef std::unordered_map
<partial_symtab
*, unsigned int> psym_index_map
;
403 /* Helper struct for building the address table. */
404 struct addrmap_index_data
406 addrmap_index_data (data_buf
&addr_vec_
, psym_index_map
&cu_index_htab_
)
407 : addr_vec (addr_vec_
), cu_index_htab (cu_index_htab_
)
410 struct objfile
*objfile
;
412 psym_index_map
&cu_index_htab
;
414 /* Non-zero if the previous_* fields are valid.
415 We can't write an entry until we see the next entry (since it is only then
416 that we know the end of the entry). */
418 /* Index of the CU in the table of all CUs in the index file. */
419 unsigned int previous_cu_index
;
420 /* Start address of the CU. */
421 CORE_ADDR previous_cu_start
;
424 /* Write an address entry to ADDR_VEC. */
427 add_address_entry (struct objfile
*objfile
, data_buf
&addr_vec
,
428 CORE_ADDR start
, CORE_ADDR end
, unsigned int cu_index
)
430 addr_vec
.append_uint (8, BFD_ENDIAN_LITTLE
, start
);
431 addr_vec
.append_uint (8, BFD_ENDIAN_LITTLE
, end
);
432 addr_vec
.append_data (MAYBE_SWAP (cu_index
));
435 /* Worker function for traversing an addrmap to build the address table. */
438 add_address_entry_worker (void *datap
, CORE_ADDR start_addr
, void *obj
)
440 struct addrmap_index_data
*data
= (struct addrmap_index_data
*) datap
;
441 struct partial_symtab
*pst
= (struct partial_symtab
*) obj
;
443 if (data
->previous_valid
)
444 add_address_entry (data
->objfile
, data
->addr_vec
,
445 data
->previous_cu_start
, start_addr
,
446 data
->previous_cu_index
);
448 data
->previous_cu_start
= start_addr
;
451 const auto it
= data
->cu_index_htab
.find (pst
);
452 gdb_assert (it
!= data
->cu_index_htab
.cend ());
453 data
->previous_cu_index
= it
->second
;
454 data
->previous_valid
= 1;
457 data
->previous_valid
= 0;
462 /* Write OBJFILE's address map to ADDR_VEC.
463 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
464 in the index file. */
467 write_address_map (struct objfile
*objfile
, data_buf
&addr_vec
,
468 psym_index_map
&cu_index_htab
)
470 struct addrmap_index_data
addrmap_index_data (addr_vec
, cu_index_htab
);
472 /* When writing the address table, we have to cope with the fact that
473 the addrmap iterator only provides the start of a region; we have to
474 wait until the next invocation to get the start of the next region. */
476 addrmap_index_data
.objfile
= objfile
;
477 addrmap_index_data
.previous_valid
= 0;
479 addrmap_foreach (objfile
->psymtabs_addrmap
, add_address_entry_worker
,
480 &addrmap_index_data
);
482 /* It's highly unlikely the last entry (end address = 0xff...ff)
483 is valid, but we should still handle it.
484 The end address is recorded as the start of the next region, but that
485 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
487 if (addrmap_index_data
.previous_valid
)
488 add_address_entry (objfile
, addr_vec
,
489 addrmap_index_data
.previous_cu_start
, (CORE_ADDR
) -1,
490 addrmap_index_data
.previous_cu_index
);
493 /* Return the symbol kind of PSYM. */
495 static gdb_index_symbol_kind
496 symbol_kind (struct partial_symbol
*psym
)
498 domain_enum domain
= psym
->domain
;
499 enum address_class aclass
= psym
->aclass
;
507 return GDB_INDEX_SYMBOL_KIND_FUNCTION
;
509 return GDB_INDEX_SYMBOL_KIND_TYPE
;
511 case LOC_CONST_BYTES
:
512 case LOC_OPTIMIZED_OUT
:
514 return GDB_INDEX_SYMBOL_KIND_VARIABLE
;
516 /* Note: It's currently impossible to recognize psyms as enum values
517 short of reading the type info. For now punt. */
518 return GDB_INDEX_SYMBOL_KIND_VARIABLE
;
520 /* There are other LOC_FOO values that one might want to classify
521 as variables, but dwarf2read.c doesn't currently use them. */
522 return GDB_INDEX_SYMBOL_KIND_OTHER
;
525 return GDB_INDEX_SYMBOL_KIND_TYPE
;
527 return GDB_INDEX_SYMBOL_KIND_OTHER
;
531 /* Add a list of partial symbols to SYMTAB. */
534 write_psymbols (struct mapped_symtab
*symtab
,
535 std::unordered_set
<partial_symbol
*> &psyms_seen
,
536 struct partial_symbol
**psymp
,
538 offset_type cu_index
,
541 for (; count
-- > 0; ++psymp
)
543 struct partial_symbol
*psym
= *psymp
;
545 if (psym
->language
== language_ada
)
546 error (_("Ada is not currently supported by the index"));
548 /* Only add a given psymbol once. */
549 if (psyms_seen
.insert (psym
).second
)
551 gdb_index_symbol_kind kind
= symbol_kind (psym
);
553 add_index_entry (symtab
, symbol_search_name (psym
),
554 is_static
, kind
, cu_index
);
559 /* A helper struct used when iterating over debug_types. */
560 struct signatured_type_index_data
562 signatured_type_index_data (data_buf
&types_list_
,
563 std::unordered_set
<partial_symbol
*> &psyms_seen_
)
564 : types_list (types_list_
), psyms_seen (psyms_seen_
)
567 struct objfile
*objfile
;
568 struct mapped_symtab
*symtab
;
569 data_buf
&types_list
;
570 std::unordered_set
<partial_symbol
*> &psyms_seen
;
574 /* A helper function that writes a single signatured_type to an
578 write_one_signatured_type (void **slot
, void *d
)
580 struct signatured_type_index_data
*info
581 = (struct signatured_type_index_data
*) d
;
582 struct signatured_type
*entry
= (struct signatured_type
*) *slot
;
583 struct partial_symtab
*psymtab
= entry
->per_cu
.v
.psymtab
;
585 write_psymbols (info
->symtab
,
587 (info
->objfile
->global_psymbols
.data ()
588 + psymtab
->globals_offset
),
589 psymtab
->n_global_syms
, info
->cu_index
,
591 write_psymbols (info
->symtab
,
593 (info
->objfile
->static_psymbols
.data ()
594 + psymtab
->statics_offset
),
595 psymtab
->n_static_syms
, info
->cu_index
,
598 info
->types_list
.append_uint (8, BFD_ENDIAN_LITTLE
,
599 to_underlying (entry
->per_cu
.sect_off
));
600 info
->types_list
.append_uint (8, BFD_ENDIAN_LITTLE
,
601 to_underlying (entry
->type_offset_in_tu
));
602 info
->types_list
.append_uint (8, BFD_ENDIAN_LITTLE
, entry
->signature
);
609 /* Recurse into all "included" dependencies and count their symbols as
610 if they appeared in this psymtab. */
613 recursively_count_psymbols (struct partial_symtab
*psymtab
,
616 for (int i
= 0; i
< psymtab
->number_of_dependencies
; ++i
)
617 if (psymtab
->dependencies
[i
]->user
!= NULL
)
618 recursively_count_psymbols (psymtab
->dependencies
[i
],
621 psyms_seen
+= psymtab
->n_global_syms
;
622 psyms_seen
+= psymtab
->n_static_syms
;
625 /* Recurse into all "included" dependencies and write their symbols as
626 if they appeared in this psymtab. */
629 recursively_write_psymbols (struct objfile
*objfile
,
630 struct partial_symtab
*psymtab
,
631 struct mapped_symtab
*symtab
,
632 std::unordered_set
<partial_symbol
*> &psyms_seen
,
633 offset_type cu_index
)
637 for (i
= 0; i
< psymtab
->number_of_dependencies
; ++i
)
638 if (psymtab
->dependencies
[i
]->user
!= NULL
)
639 recursively_write_psymbols (objfile
, psymtab
->dependencies
[i
],
640 symtab
, psyms_seen
, cu_index
);
642 write_psymbols (symtab
,
644 objfile
->global_psymbols
.data () + psymtab
->globals_offset
,
645 psymtab
->n_global_syms
, cu_index
,
647 write_psymbols (symtab
,
649 objfile
->static_psymbols
.data () + psymtab
->statics_offset
,
650 psymtab
->n_static_syms
, cu_index
,
654 /* DWARF-5 .debug_names builder. */
658 debug_names (struct dwarf2_per_objfile
*dwarf2_per_objfile
, bool is_dwarf64
,
659 bfd_endian dwarf5_byte_order
)
660 : m_dwarf5_byte_order (dwarf5_byte_order
),
661 m_dwarf32 (dwarf5_byte_order
),
662 m_dwarf64 (dwarf5_byte_order
),
664 ? static_cast<dwarf
&> (m_dwarf64
)
665 : static_cast<dwarf
&> (m_dwarf32
)),
666 m_name_table_string_offs (m_dwarf
.name_table_string_offs
),
667 m_name_table_entry_offs (m_dwarf
.name_table_entry_offs
),
668 m_debugstrlookup (dwarf2_per_objfile
)
671 int dwarf5_offset_size () const
673 const bool dwarf5_is_dwarf64
= &m_dwarf
== &m_dwarf64
;
674 return dwarf5_is_dwarf64
? 8 : 4;
677 /* Is this symbol from DW_TAG_compile_unit or DW_TAG_type_unit? */
678 enum class unit_kind
{ cu
, tu
};
680 /* Insert one symbol. */
681 void insert (const partial_symbol
*psym
, int cu_index
, bool is_static
,
684 const int dwarf_tag
= psymbol_tag (psym
);
687 const char *const name
= symbol_search_name (psym
);
688 const auto insertpair
689 = m_name_to_value_set
.emplace (c_str_view (name
),
690 std::set
<symbol_value
> ());
691 std::set
<symbol_value
> &value_set
= insertpair
.first
->second
;
692 value_set
.emplace (symbol_value (dwarf_tag
, cu_index
, is_static
, kind
));
695 /* Build all the tables. All symbols must be already inserted.
696 This function does not call file_write, caller has to do it
700 /* Verify the build method has not be called twice. */
701 gdb_assert (m_abbrev_table
.empty ());
702 const size_t name_count
= m_name_to_value_set
.size ();
703 m_bucket_table
.resize
704 (std::pow (2, std::ceil (std::log2 (name_count
* 4 / 3))));
705 m_hash_table
.reserve (name_count
);
706 m_name_table_string_offs
.reserve (name_count
);
707 m_name_table_entry_offs
.reserve (name_count
);
709 /* Map each hash of symbol to its name and value. */
713 decltype (m_name_to_value_set
)::const_iterator it
;
715 std::vector
<std::forward_list
<hash_it_pair
>> bucket_hash
;
716 bucket_hash
.resize (m_bucket_table
.size ());
717 for (decltype (m_name_to_value_set
)::const_iterator it
718 = m_name_to_value_set
.cbegin ();
719 it
!= m_name_to_value_set
.cend ();
722 const char *const name
= it
->first
.c_str ();
723 const uint32_t hash
= dwarf5_djb_hash (name
);
724 hash_it_pair hashitpair
;
725 hashitpair
.hash
= hash
;
727 auto &slot
= bucket_hash
[hash
% bucket_hash
.size()];
728 slot
.push_front (std::move (hashitpair
));
730 for (size_t bucket_ix
= 0; bucket_ix
< bucket_hash
.size (); ++bucket_ix
)
732 const std::forward_list
<hash_it_pair
> &hashitlist
733 = bucket_hash
[bucket_ix
];
734 if (hashitlist
.empty ())
736 uint32_t &bucket_slot
= m_bucket_table
[bucket_ix
];
737 /* The hashes array is indexed starting at 1. */
738 store_unsigned_integer (reinterpret_cast<gdb_byte
*> (&bucket_slot
),
739 sizeof (bucket_slot
), m_dwarf5_byte_order
,
740 m_hash_table
.size () + 1);
741 for (const hash_it_pair
&hashitpair
: hashitlist
)
743 m_hash_table
.push_back (0);
744 store_unsigned_integer (reinterpret_cast<gdb_byte
*>
745 (&m_hash_table
.back ()),
746 sizeof (m_hash_table
.back ()),
747 m_dwarf5_byte_order
, hashitpair
.hash
);
748 const c_str_view
&name
= hashitpair
.it
->first
;
749 const std::set
<symbol_value
> &value_set
= hashitpair
.it
->second
;
750 m_name_table_string_offs
.push_back_reorder
751 (m_debugstrlookup
.lookup (name
.c_str ()));
752 m_name_table_entry_offs
.push_back_reorder (m_entry_pool
.size ());
753 gdb_assert (!value_set
.empty ());
754 for (const symbol_value
&value
: value_set
)
756 int &idx
= m_indexkey_to_idx
[index_key (value
.dwarf_tag
,
762 m_abbrev_table
.append_unsigned_leb128 (idx
);
763 m_abbrev_table
.append_unsigned_leb128 (value
.dwarf_tag
);
764 m_abbrev_table
.append_unsigned_leb128
765 (value
.kind
== unit_kind::cu
? DW_IDX_compile_unit
767 m_abbrev_table
.append_unsigned_leb128 (DW_FORM_udata
);
768 m_abbrev_table
.append_unsigned_leb128 (value
.is_static
769 ? DW_IDX_GNU_internal
770 : DW_IDX_GNU_external
);
771 m_abbrev_table
.append_unsigned_leb128 (DW_FORM_flag_present
);
773 /* Terminate attributes list. */
774 m_abbrev_table
.append_unsigned_leb128 (0);
775 m_abbrev_table
.append_unsigned_leb128 (0);
778 m_entry_pool
.append_unsigned_leb128 (idx
);
779 m_entry_pool
.append_unsigned_leb128 (value
.cu_index
);
782 /* Terminate the list of CUs. */
783 m_entry_pool
.append_unsigned_leb128 (0);
786 gdb_assert (m_hash_table
.size () == name_count
);
788 /* Terminate tags list. */
789 m_abbrev_table
.append_unsigned_leb128 (0);
792 /* Return .debug_names bucket count. This must be called only after
793 calling the build method. */
794 uint32_t bucket_count () const
796 /* Verify the build method has been already called. */
797 gdb_assert (!m_abbrev_table
.empty ());
798 const uint32_t retval
= m_bucket_table
.size ();
800 /* Check for overflow. */
801 gdb_assert (retval
== m_bucket_table
.size ());
805 /* Return .debug_names names count. This must be called only after
806 calling the build method. */
807 uint32_t name_count () const
809 /* Verify the build method has been already called. */
810 gdb_assert (!m_abbrev_table
.empty ());
811 const uint32_t retval
= m_hash_table
.size ();
813 /* Check for overflow. */
814 gdb_assert (retval
== m_hash_table
.size ());
818 /* Return number of bytes of .debug_names abbreviation table. This
819 must be called only after calling the build method. */
820 uint32_t abbrev_table_bytes () const
822 gdb_assert (!m_abbrev_table
.empty ());
823 return m_abbrev_table
.size ();
826 /* Recurse into all "included" dependencies and store their symbols
827 as if they appeared in this psymtab. */
828 void recursively_write_psymbols
829 (struct objfile
*objfile
,
830 struct partial_symtab
*psymtab
,
831 std::unordered_set
<partial_symbol
*> &psyms_seen
,
834 for (int i
= 0; i
< psymtab
->number_of_dependencies
; ++i
)
835 if (psymtab
->dependencies
[i
]->user
!= NULL
)
836 recursively_write_psymbols (objfile
, psymtab
->dependencies
[i
],
837 psyms_seen
, cu_index
);
839 write_psymbols (psyms_seen
,
840 objfile
->global_psymbols
.data () + psymtab
->globals_offset
,
841 psymtab
->n_global_syms
, cu_index
, false, unit_kind::cu
);
842 write_psymbols (psyms_seen
,
843 objfile
->static_psymbols
.data () + psymtab
->statics_offset
,
844 psymtab
->n_static_syms
, cu_index
, true, unit_kind::cu
);
847 /* Return number of bytes the .debug_names section will have. This
848 must be called only after calling the build method. */
849 size_t bytes () const
851 /* Verify the build method has been already called. */
852 gdb_assert (!m_abbrev_table
.empty ());
853 size_t expected_bytes
= 0;
854 expected_bytes
+= m_bucket_table
.size () * sizeof (m_bucket_table
[0]);
855 expected_bytes
+= m_hash_table
.size () * sizeof (m_hash_table
[0]);
856 expected_bytes
+= m_name_table_string_offs
.bytes ();
857 expected_bytes
+= m_name_table_entry_offs
.bytes ();
858 expected_bytes
+= m_abbrev_table
.size ();
859 expected_bytes
+= m_entry_pool
.size ();
860 return expected_bytes
;
863 /* Write .debug_names to FILE_NAMES and .debug_str addition to
864 FILE_STR. This must be called only after calling the build
866 void file_write (FILE *file_names
, FILE *file_str
) const
868 /* Verify the build method has been already called. */
869 gdb_assert (!m_abbrev_table
.empty ());
870 ::file_write (file_names
, m_bucket_table
);
871 ::file_write (file_names
, m_hash_table
);
872 m_name_table_string_offs
.file_write (file_names
);
873 m_name_table_entry_offs
.file_write (file_names
);
874 m_abbrev_table
.file_write (file_names
);
875 m_entry_pool
.file_write (file_names
);
876 m_debugstrlookup
.file_write (file_str
);
879 /* A helper user data for write_one_signatured_type. */
880 class write_one_signatured_type_data
883 write_one_signatured_type_data (debug_names
&nametable_
,
884 signatured_type_index_data
&&info_
)
885 : nametable (nametable_
), info (std::move (info_
))
887 debug_names
&nametable
;
888 struct signatured_type_index_data info
;
891 /* A helper function to pass write_one_signatured_type to
892 htab_traverse_noresize. */
894 write_one_signatured_type (void **slot
, void *d
)
896 write_one_signatured_type_data
*data
= (write_one_signatured_type_data
*) d
;
897 struct signatured_type_index_data
*info
= &data
->info
;
898 struct signatured_type
*entry
= (struct signatured_type
*) *slot
;
900 data
->nametable
.write_one_signatured_type (entry
, info
);
907 /* Storage for symbol names mapping them to their .debug_str section
909 class debug_str_lookup
913 /* Object costructor to be called for current DWARF2_PER_OBJFILE.
914 All .debug_str section strings are automatically stored. */
915 debug_str_lookup (struct dwarf2_per_objfile
*dwarf2_per_objfile
)
916 : m_abfd (dwarf2_per_objfile
->objfile
->obfd
),
917 m_dwarf2_per_objfile (dwarf2_per_objfile
)
919 dwarf2_read_section (dwarf2_per_objfile
->objfile
,
920 &dwarf2_per_objfile
->str
);
921 if (dwarf2_per_objfile
->str
.buffer
== NULL
)
923 for (const gdb_byte
*data
= dwarf2_per_objfile
->str
.buffer
;
924 data
< (dwarf2_per_objfile
->str
.buffer
925 + dwarf2_per_objfile
->str
.size
);)
927 const char *const s
= reinterpret_cast<const char *> (data
);
928 const auto insertpair
929 = m_str_table
.emplace (c_str_view (s
),
930 data
- dwarf2_per_objfile
->str
.buffer
);
931 if (!insertpair
.second
)
932 complaint (_("Duplicate string \"%s\" in "
933 ".debug_str section [in module %s]"),
934 s
, bfd_get_filename (m_abfd
));
935 data
+= strlen (s
) + 1;
939 /* Return offset of symbol name S in the .debug_str section. Add
940 such symbol to the section's end if it does not exist there
942 size_t lookup (const char *s
)
944 const auto it
= m_str_table
.find (c_str_view (s
));
945 if (it
!= m_str_table
.end ())
947 const size_t offset
= (m_dwarf2_per_objfile
->str
.size
948 + m_str_add_buf
.size ());
949 m_str_table
.emplace (c_str_view (s
), offset
);
950 m_str_add_buf
.append_cstr0 (s
);
954 /* Append the end of the .debug_str section to FILE. */
955 void file_write (FILE *file
) const
957 m_str_add_buf
.file_write (file
);
961 std::unordered_map
<c_str_view
, size_t, c_str_view_hasher
> m_str_table
;
963 struct dwarf2_per_objfile
*m_dwarf2_per_objfile
;
965 /* Data to add at the end of .debug_str for new needed symbol names. */
966 data_buf m_str_add_buf
;
969 /* Container to map used DWARF tags to their .debug_names abbreviation
974 index_key (int dwarf_tag_
, bool is_static_
, unit_kind kind_
)
975 : dwarf_tag (dwarf_tag_
), is_static (is_static_
), kind (kind_
)
980 operator== (const index_key
&other
) const
982 return (dwarf_tag
== other
.dwarf_tag
&& is_static
== other
.is_static
983 && kind
== other
.kind
);
987 const bool is_static
;
988 const unit_kind kind
;
991 /* Provide std::unordered_map::hasher for index_key. */
992 class index_key_hasher
996 operator () (const index_key
&key
) const
998 return (std::hash
<int>() (key
.dwarf_tag
) << 1) | key
.is_static
;
1002 /* Parameters of one symbol entry. */
1006 const int dwarf_tag
, cu_index
;
1007 const bool is_static
;
1008 const unit_kind kind
;
1010 symbol_value (int dwarf_tag_
, int cu_index_
, bool is_static_
,
1012 : dwarf_tag (dwarf_tag_
), cu_index (cu_index_
), is_static (is_static_
),
1017 operator< (const symbol_value
&other
) const
1037 /* Abstract base class to unify DWARF-32 and DWARF-64 name table
1042 const bfd_endian dwarf5_byte_order
;
1044 explicit offset_vec (bfd_endian dwarf5_byte_order_
)
1045 : dwarf5_byte_order (dwarf5_byte_order_
)
1048 /* Call std::vector::reserve for NELEM elements. */
1049 virtual void reserve (size_t nelem
) = 0;
1051 /* Call std::vector::push_back with store_unsigned_integer byte
1052 reordering for ELEM. */
1053 virtual void push_back_reorder (size_t elem
) = 0;
1055 /* Return expected output size in bytes. */
1056 virtual size_t bytes () const = 0;
1058 /* Write name table to FILE. */
1059 virtual void file_write (FILE *file
) const = 0;
1062 /* Template to unify DWARF-32 and DWARF-64 output. */
1063 template<typename OffsetSize
>
1064 class offset_vec_tmpl
: public offset_vec
1067 explicit offset_vec_tmpl (bfd_endian dwarf5_byte_order_
)
1068 : offset_vec (dwarf5_byte_order_
)
1071 /* Implement offset_vec::reserve. */
1072 void reserve (size_t nelem
) override
1074 m_vec
.reserve (nelem
);
1077 /* Implement offset_vec::push_back_reorder. */
1078 void push_back_reorder (size_t elem
) override
1080 m_vec
.push_back (elem
);
1081 /* Check for overflow. */
1082 gdb_assert (m_vec
.back () == elem
);
1083 store_unsigned_integer (reinterpret_cast<gdb_byte
*> (&m_vec
.back ()),
1084 sizeof (m_vec
.back ()), dwarf5_byte_order
, elem
);
1087 /* Implement offset_vec::bytes. */
1088 size_t bytes () const override
1090 return m_vec
.size () * sizeof (m_vec
[0]);
1093 /* Implement offset_vec::file_write. */
1094 void file_write (FILE *file
) const override
1096 ::file_write (file
, m_vec
);
1100 std::vector
<OffsetSize
> m_vec
;
1103 /* Base class to unify DWARF-32 and DWARF-64 .debug_names output
1104 respecting name table width. */
1108 offset_vec
&name_table_string_offs
, &name_table_entry_offs
;
1110 dwarf (offset_vec
&name_table_string_offs_
,
1111 offset_vec
&name_table_entry_offs_
)
1112 : name_table_string_offs (name_table_string_offs_
),
1113 name_table_entry_offs (name_table_entry_offs_
)
1118 /* Template to unify DWARF-32 and DWARF-64 .debug_names output
1119 respecting name table width. */
1120 template<typename OffsetSize
>
1121 class dwarf_tmpl
: public dwarf
1124 explicit dwarf_tmpl (bfd_endian dwarf5_byte_order_
)
1125 : dwarf (m_name_table_string_offs
, m_name_table_entry_offs
),
1126 m_name_table_string_offs (dwarf5_byte_order_
),
1127 m_name_table_entry_offs (dwarf5_byte_order_
)
1131 offset_vec_tmpl
<OffsetSize
> m_name_table_string_offs
;
1132 offset_vec_tmpl
<OffsetSize
> m_name_table_entry_offs
;
1135 /* Try to reconstruct original DWARF tag for given partial_symbol.
1136 This function is not DWARF-5 compliant but it is sufficient for
1137 GDB as a DWARF-5 index consumer. */
1138 static int psymbol_tag (const struct partial_symbol
*psym
)
1140 domain_enum domain
= psym
->domain
;
1141 enum address_class aclass
= psym
->aclass
;
1149 return DW_TAG_subprogram
;
1151 return DW_TAG_typedef
;
1153 case LOC_CONST_BYTES
:
1154 case LOC_OPTIMIZED_OUT
:
1156 return DW_TAG_variable
;
1158 /* Note: It's currently impossible to recognize psyms as enum values
1159 short of reading the type info. For now punt. */
1160 return DW_TAG_variable
;
1162 /* There are other LOC_FOO values that one might want to classify
1163 as variables, but dwarf2read.c doesn't currently use them. */
1164 return DW_TAG_variable
;
1167 return DW_TAG_structure_type
;
1173 /* Call insert for all partial symbols and mark them in PSYMS_SEEN. */
1174 void write_psymbols (std::unordered_set
<partial_symbol
*> &psyms_seen
,
1175 struct partial_symbol
**psymp
, int count
, int cu_index
,
1176 bool is_static
, unit_kind kind
)
1178 for (; count
-- > 0; ++psymp
)
1180 struct partial_symbol
*psym
= *psymp
;
1182 if (psym
->language
== language_ada
)
1183 error (_("Ada is not currently supported by the index"));
1185 /* Only add a given psymbol once. */
1186 if (psyms_seen
.insert (psym
).second
)
1187 insert (psym
, cu_index
, is_static
, kind
);
1191 /* A helper function that writes a single signatured_type
1192 to a debug_names. */
1194 write_one_signatured_type (struct signatured_type
*entry
,
1195 struct signatured_type_index_data
*info
)
1197 struct partial_symtab
*psymtab
= entry
->per_cu
.v
.psymtab
;
1199 write_psymbols (info
->psyms_seen
,
1200 (info
->objfile
->global_psymbols
.data ()
1201 + psymtab
->globals_offset
),
1202 psymtab
->n_global_syms
, info
->cu_index
, false,
1204 write_psymbols (info
->psyms_seen
,
1205 (info
->objfile
->static_psymbols
.data ()
1206 + psymtab
->statics_offset
),
1207 psymtab
->n_static_syms
, info
->cu_index
, true,
1210 info
->types_list
.append_uint (dwarf5_offset_size (), m_dwarf5_byte_order
,
1211 to_underlying (entry
->per_cu
.sect_off
));
1216 /* Store value of each symbol. */
1217 std::unordered_map
<c_str_view
, std::set
<symbol_value
>, c_str_view_hasher
>
1218 m_name_to_value_set
;
1220 /* Tables of DWARF-5 .debug_names. They are in object file byte
1222 std::vector
<uint32_t> m_bucket_table
;
1223 std::vector
<uint32_t> m_hash_table
;
1225 const bfd_endian m_dwarf5_byte_order
;
1226 dwarf_tmpl
<uint32_t> m_dwarf32
;
1227 dwarf_tmpl
<uint64_t> m_dwarf64
;
1229 offset_vec
&m_name_table_string_offs
, &m_name_table_entry_offs
;
1230 debug_str_lookup m_debugstrlookup
;
1232 /* Map each used .debug_names abbreviation tag parameter to its
1234 std::unordered_map
<index_key
, int, index_key_hasher
> m_indexkey_to_idx
;
1236 /* Next unused .debug_names abbreviation tag for
1237 m_indexkey_to_idx. */
1240 /* .debug_names abbreviation table. */
1241 data_buf m_abbrev_table
;
1243 /* .debug_names entry pool. */
1244 data_buf m_entry_pool
;
1247 /* Return iff any of the needed offsets does not fit into 32-bit
1248 .debug_names section. */
1251 check_dwarf64_offsets (struct dwarf2_per_objfile
*dwarf2_per_objfile
)
1253 for (dwarf2_per_cu_data
*per_cu
: dwarf2_per_objfile
->all_comp_units
)
1255 if (to_underlying (per_cu
->sect_off
) >= (static_cast<uint64_t> (1) << 32))
1258 for (const signatured_type
*sigtype
: dwarf2_per_objfile
->all_type_units
)
1260 const dwarf2_per_cu_data
&per_cu
= sigtype
->per_cu
;
1262 if (to_underlying (per_cu
.sect_off
) >= (static_cast<uint64_t> (1) << 32))
1268 /* The psyms_seen set is potentially going to be largish (~40k
1269 elements when indexing a -g3 build of GDB itself). Estimate the
1270 number of elements in order to avoid too many rehashes, which
1271 require rebuilding buckets and thus many trips to
1275 psyms_seen_size (struct dwarf2_per_objfile
*dwarf2_per_objfile
)
1277 size_t psyms_count
= 0;
1278 for (dwarf2_per_cu_data
*per_cu
: dwarf2_per_objfile
->all_comp_units
)
1280 struct partial_symtab
*psymtab
= per_cu
->v
.psymtab
;
1282 if (psymtab
!= NULL
&& psymtab
->user
== NULL
)
1283 recursively_count_psymbols (psymtab
, psyms_count
);
1285 /* Generating an index for gdb itself shows a ratio of
1286 TOTAL_SEEN_SYMS/UNIQUE_SYMS or ~5. 4 seems like a good bet. */
1287 return psyms_count
/ 4;
1290 /* Write new .gdb_index section for OBJFILE into OUT_FILE.
1291 Return how many bytes were expected to be written into OUT_FILE. */
1294 write_gdbindex (struct dwarf2_per_objfile
*dwarf2_per_objfile
, FILE *out_file
)
1296 struct objfile
*objfile
= dwarf2_per_objfile
->objfile
;
1297 mapped_symtab symtab
;
1300 /* While we're scanning CU's create a table that maps a psymtab pointer
1301 (which is what addrmap records) to its index (which is what is recorded
1302 in the index file). This will later be needed to write the address
1304 psym_index_map cu_index_htab
;
1305 cu_index_htab
.reserve (dwarf2_per_objfile
->all_comp_units
.size ());
1307 /* The CU list is already sorted, so we don't need to do additional
1308 work here. Also, the debug_types entries do not appear in
1309 all_comp_units, but only in their own hash table. */
1311 std::unordered_set
<partial_symbol
*> psyms_seen
1312 (psyms_seen_size (dwarf2_per_objfile
));
1313 for (int i
= 0; i
< dwarf2_per_objfile
->all_comp_units
.size (); ++i
)
1315 struct dwarf2_per_cu_data
*per_cu
1316 = dwarf2_per_objfile
->all_comp_units
[i
];
1317 struct partial_symtab
*psymtab
= per_cu
->v
.psymtab
;
1319 /* CU of a shared file from 'dwz -m' may be unused by this main file.
1320 It may be referenced from a local scope but in such case it does not
1321 need to be present in .gdb_index. */
1322 if (psymtab
== NULL
)
1325 if (psymtab
->user
== NULL
)
1326 recursively_write_psymbols (objfile
, psymtab
, &symtab
,
1329 const auto insertpair
= cu_index_htab
.emplace (psymtab
, i
);
1330 gdb_assert (insertpair
.second
);
1332 cu_list
.append_uint (8, BFD_ENDIAN_LITTLE
,
1333 to_underlying (per_cu
->sect_off
));
1334 cu_list
.append_uint (8, BFD_ENDIAN_LITTLE
, per_cu
->length
);
1337 /* Dump the address map. */
1339 write_address_map (objfile
, addr_vec
, cu_index_htab
);
1341 /* Write out the .debug_type entries, if any. */
1342 data_buf types_cu_list
;
1343 if (dwarf2_per_objfile
->signatured_types
)
1345 signatured_type_index_data
sig_data (types_cu_list
,
1348 sig_data
.objfile
= objfile
;
1349 sig_data
.symtab
= &symtab
;
1350 sig_data
.cu_index
= dwarf2_per_objfile
->all_comp_units
.size ();
1351 htab_traverse_noresize (dwarf2_per_objfile
->signatured_types
,
1352 write_one_signatured_type
, &sig_data
);
1355 /* Now that we've processed all symbols we can shrink their cu_indices
1357 uniquify_cu_indices (&symtab
);
1359 data_buf symtab_vec
, constant_pool
;
1360 write_hash_table (&symtab
, symtab_vec
, constant_pool
);
1363 const offset_type size_of_contents
= 6 * sizeof (offset_type
);
1364 offset_type total_len
= size_of_contents
;
1366 /* The version number. */
1367 contents
.append_data (MAYBE_SWAP (8));
1369 /* The offset of the CU list from the start of the file. */
1370 contents
.append_data (MAYBE_SWAP (total_len
));
1371 total_len
+= cu_list
.size ();
1373 /* The offset of the types CU list from the start of the file. */
1374 contents
.append_data (MAYBE_SWAP (total_len
));
1375 total_len
+= types_cu_list
.size ();
1377 /* The offset of the address table from the start of the file. */
1378 contents
.append_data (MAYBE_SWAP (total_len
));
1379 total_len
+= addr_vec
.size ();
1381 /* The offset of the symbol table from the start of the file. */
1382 contents
.append_data (MAYBE_SWAP (total_len
));
1383 total_len
+= symtab_vec
.size ();
1385 /* The offset of the constant pool from the start of the file. */
1386 contents
.append_data (MAYBE_SWAP (total_len
));
1387 total_len
+= constant_pool
.size ();
1389 gdb_assert (contents
.size () == size_of_contents
);
1391 contents
.file_write (out_file
);
1392 cu_list
.file_write (out_file
);
1393 types_cu_list
.file_write (out_file
);
1394 addr_vec
.file_write (out_file
);
1395 symtab_vec
.file_write (out_file
);
1396 constant_pool
.file_write (out_file
);
1401 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
1402 static const gdb_byte dwarf5_gdb_augmentation
[] = { 'G', 'D', 'B', 0 };
1404 /* Write a new .debug_names section for OBJFILE into OUT_FILE, write
1405 needed addition to .debug_str section to OUT_FILE_STR. Return how
1406 many bytes were expected to be written into OUT_FILE. */
1409 write_debug_names (struct dwarf2_per_objfile
*dwarf2_per_objfile
,
1410 FILE *out_file
, FILE *out_file_str
)
1412 const bool dwarf5_is_dwarf64
= check_dwarf64_offsets (dwarf2_per_objfile
);
1413 struct objfile
*objfile
= dwarf2_per_objfile
->objfile
;
1414 const enum bfd_endian dwarf5_byte_order
1415 = gdbarch_byte_order (get_objfile_arch (objfile
));
1417 /* The CU list is already sorted, so we don't need to do additional
1418 work here. Also, the debug_types entries do not appear in
1419 all_comp_units, but only in their own hash table. */
1421 debug_names
nametable (dwarf2_per_objfile
, dwarf5_is_dwarf64
,
1423 std::unordered_set
<partial_symbol
*>
1424 psyms_seen (psyms_seen_size (dwarf2_per_objfile
));
1425 for (int i
= 0; i
< dwarf2_per_objfile
->all_comp_units
.size (); ++i
)
1427 const dwarf2_per_cu_data
*per_cu
= dwarf2_per_objfile
->all_comp_units
[i
];
1428 partial_symtab
*psymtab
= per_cu
->v
.psymtab
;
1430 /* CU of a shared file from 'dwz -m' may be unused by this main
1431 file. It may be referenced from a local scope but in such
1432 case it does not need to be present in .debug_names. */
1433 if (psymtab
== NULL
)
1436 if (psymtab
->user
== NULL
)
1437 nametable
.recursively_write_psymbols (objfile
, psymtab
, psyms_seen
, i
);
1439 cu_list
.append_uint (nametable
.dwarf5_offset_size (), dwarf5_byte_order
,
1440 to_underlying (per_cu
->sect_off
));
1443 /* Write out the .debug_type entries, if any. */
1444 data_buf types_cu_list
;
1445 if (dwarf2_per_objfile
->signatured_types
)
1447 debug_names::write_one_signatured_type_data
sig_data (nametable
,
1448 signatured_type_index_data (types_cu_list
, psyms_seen
));
1450 sig_data
.info
.objfile
= objfile
;
1451 /* It is used only for gdb_index. */
1452 sig_data
.info
.symtab
= nullptr;
1453 sig_data
.info
.cu_index
= 0;
1454 htab_traverse_noresize (dwarf2_per_objfile
->signatured_types
,
1455 debug_names::write_one_signatured_type
,
1461 /* No addr_vec - DWARF-5 uses .debug_aranges generated by GCC. */
1463 const offset_type bytes_of_header
1464 = ((dwarf5_is_dwarf64
? 12 : 4)
1466 + sizeof (dwarf5_gdb_augmentation
));
1467 size_t expected_bytes
= 0;
1468 expected_bytes
+= bytes_of_header
;
1469 expected_bytes
+= cu_list
.size ();
1470 expected_bytes
+= types_cu_list
.size ();
1471 expected_bytes
+= nametable
.bytes ();
1474 if (!dwarf5_is_dwarf64
)
1476 const uint64_t size64
= expected_bytes
- 4;
1477 gdb_assert (size64
< 0xfffffff0);
1478 header
.append_uint (4, dwarf5_byte_order
, size64
);
1482 header
.append_uint (4, dwarf5_byte_order
, 0xffffffff);
1483 header
.append_uint (8, dwarf5_byte_order
, expected_bytes
- 12);
1486 /* The version number. */
1487 header
.append_uint (2, dwarf5_byte_order
, 5);
1490 header
.append_uint (2, dwarf5_byte_order
, 0);
1492 /* comp_unit_count - The number of CUs in the CU list. */
1493 header
.append_uint (4, dwarf5_byte_order
,
1494 dwarf2_per_objfile
->all_comp_units
.size ());
1496 /* local_type_unit_count - The number of TUs in the local TU
1498 header
.append_uint (4, dwarf5_byte_order
,
1499 dwarf2_per_objfile
->all_type_units
.size ());
1501 /* foreign_type_unit_count - The number of TUs in the foreign TU
1503 header
.append_uint (4, dwarf5_byte_order
, 0);
1505 /* bucket_count - The number of hash buckets in the hash lookup
1507 header
.append_uint (4, dwarf5_byte_order
, nametable
.bucket_count ());
1509 /* name_count - The number of unique names in the index. */
1510 header
.append_uint (4, dwarf5_byte_order
, nametable
.name_count ());
1512 /* abbrev_table_size - The size in bytes of the abbreviations
1514 header
.append_uint (4, dwarf5_byte_order
, nametable
.abbrev_table_bytes ());
1516 /* augmentation_string_size - The size in bytes of the augmentation
1517 string. This value is rounded up to a multiple of 4. */
1518 static_assert (sizeof (dwarf5_gdb_augmentation
) % 4 == 0, "");
1519 header
.append_uint (4, dwarf5_byte_order
, sizeof (dwarf5_gdb_augmentation
));
1520 header
.append_data (dwarf5_gdb_augmentation
);
1522 gdb_assert (header
.size () == bytes_of_header
);
1524 header
.file_write (out_file
);
1525 cu_list
.file_write (out_file
);
1526 types_cu_list
.file_write (out_file
);
1527 nametable
.file_write (out_file
, out_file_str
);
1529 return expected_bytes
;
1532 /* Assert that FILE's size is EXPECTED_SIZE. Assumes file's seek
1533 position is at the end of the file. */
1536 assert_file_size (FILE *file
, const char *filename
, size_t expected_size
)
1538 const auto file_size
= ftell (file
);
1539 if (file_size
== -1)
1540 error (_("Can't get `%s' size"), filename
);
1541 gdb_assert (file_size
== expected_size
);
1544 /* Create an index file for OBJFILE in the directory DIR. */
1547 write_psymtabs_to_index (struct dwarf2_per_objfile
*dwarf2_per_objfile
,
1549 dw_index_kind index_kind
)
1551 struct objfile
*objfile
= dwarf2_per_objfile
->objfile
;
1553 if (dwarf2_per_objfile
->using_index
)
1554 error (_("Cannot use an index to create the index"));
1556 if (VEC_length (dwarf2_section_info_def
, dwarf2_per_objfile
->types
) > 1)
1557 error (_("Cannot make an index when the file has multiple .debug_types sections"));
1559 if (!objfile
->psymtabs
|| !objfile
->psymtabs_addrmap
)
1563 if (stat (objfile_name (objfile
), &st
) < 0)
1564 perror_with_name (objfile_name (objfile
));
1566 std::string
filename (std::string (dir
) + SLASH_STRING
1567 + lbasename (objfile_name (objfile
))
1568 + (index_kind
== dw_index_kind::DEBUG_NAMES
1569 ? INDEX5_SUFFIX
: INDEX4_SUFFIX
));
1571 FILE *out_file
= gdb_fopen_cloexec (filename
.c_str (), "wb").release ();
1573 error (_("Can't open `%s' for writing"), filename
.c_str ());
1575 /* Order matters here; we want FILE to be closed before FILENAME is
1576 unlinked, because on MS-Windows one cannot delete a file that is
1577 still open. (Don't call anything here that might throw until
1578 file_closer is created.) */
1579 gdb::unlinker
unlink_file (filename
.c_str ());
1580 gdb_file_up
close_out_file (out_file
);
1582 if (index_kind
== dw_index_kind::DEBUG_NAMES
)
1584 std::string
filename_str (std::string (dir
) + SLASH_STRING
1585 + lbasename (objfile_name (objfile
))
1586 + DEBUG_STR_SUFFIX
);
1588 = gdb_fopen_cloexec (filename_str
.c_str (), "wb").release ();
1590 error (_("Can't open `%s' for writing"), filename_str
.c_str ());
1591 gdb::unlinker
unlink_file_str (filename_str
.c_str ());
1592 gdb_file_up
close_out_file_str (out_file_str
);
1594 const size_t total_len
1595 = write_debug_names (dwarf2_per_objfile
, out_file
, out_file_str
);
1596 assert_file_size (out_file
, filename
.c_str (), total_len
);
1598 /* We want to keep the file .debug_str file too. */
1599 unlink_file_str
.keep ();
1603 const size_t total_len
1604 = write_gdbindex (dwarf2_per_objfile
, out_file
);
1605 assert_file_size (out_file
, filename
.c_str (), total_len
);
1608 /* We want to keep the file. */
1609 unlink_file
.keep ();
1612 /* Implementation of the `save gdb-index' command.
1614 Note that the .gdb_index file format used by this command is
1615 documented in the GDB manual. Any changes here must be documented
1619 save_gdb_index_command (const char *arg
, int from_tty
)
1621 struct objfile
*objfile
;
1622 const char dwarf5space
[] = "-dwarf-5 ";
1623 dw_index_kind index_kind
= dw_index_kind::GDB_INDEX
;
1628 arg
= skip_spaces (arg
);
1629 if (strncmp (arg
, dwarf5space
, strlen (dwarf5space
)) == 0)
1631 index_kind
= dw_index_kind::DEBUG_NAMES
;
1632 arg
+= strlen (dwarf5space
);
1633 arg
= skip_spaces (arg
);
1637 error (_("usage: save gdb-index [-dwarf-5] DIRECTORY"));
1639 ALL_OBJFILES (objfile
)
1643 /* If the objfile does not correspond to an actual file, skip it. */
1644 if (stat (objfile_name (objfile
), &st
) < 0)
1647 struct dwarf2_per_objfile
*dwarf2_per_objfile
1648 = get_dwarf2_per_objfile (objfile
);
1650 if (dwarf2_per_objfile
!= NULL
)
1654 write_psymtabs_to_index (dwarf2_per_objfile
, arg
, index_kind
);
1656 CATCH (except
, RETURN_MASK_ERROR
)
1658 exception_fprintf (gdb_stderr
, except
,
1659 _("Error while writing index for `%s': "),
1660 objfile_name (objfile
));
1669 _initialize_dwarf_index_write ()
1671 cmd_list_element
*c
= add_cmd ("gdb-index", class_files
,
1672 save_gdb_index_command
, _("\
1673 Save a gdb-index file.\n\
1674 Usage: save gdb-index [-dwarf-5] DIRECTORY\n\
1676 No options create one file with .gdb-index extension for pre-DWARF-5\n\
1677 compatible .gdb_index section. With -dwarf-5 creates two files with\n\
1678 extension .debug_names and .debug_str for DWARF-5 .debug_names section."),
1680 set_cmd_completer (c
, filename_completer
);