1 /* DWARF index writing support for GDB.
3 Copyright (C) 1994-2020 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"
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 DATA to the end of the buffer. */
99 void append_data (const T
&data
)
101 std::copy (reinterpret_cast<const gdb_byte
*> (&data
),
102 reinterpret_cast<const gdb_byte
*> (&data
+ 1),
103 grow (sizeof (data
)));
106 /* Copy CSTR (a zero-terminated string) to the end of buffer. The
107 terminating zero is appended too. */
108 void append_cstr0 (const char *cstr
)
110 const size_t size
= strlen (cstr
) + 1;
111 std::copy (cstr
, cstr
+ size
, grow (size
));
114 /* Store INPUT as ULEB128 to the end of buffer. */
115 void append_unsigned_leb128 (ULONGEST input
)
119 gdb_byte output
= input
& 0x7f;
123 append_data (output
);
129 /* Accept a host-format integer in VAL and append it to the buffer
130 as a target-format integer which is LEN bytes long. */
131 void append_uint (size_t len
, bfd_endian byte_order
, ULONGEST val
)
133 ::store_unsigned_integer (grow (len
), len
, byte_order
, val
);
136 /* Return the size of the buffer. */
139 return m_vec
.size ();
142 /* Return true iff the buffer is empty. */
145 return m_vec
.empty ();
148 /* Write the buffer to FILE. */
149 void file_write (FILE *file
) const
151 ::file_write (file
, m_vec
);
155 /* Grow SIZE bytes at the end of the buffer. Returns a pointer to
156 the start of the new block. */
157 gdb_byte
*grow (size_t size
)
159 m_vec
.resize (m_vec
.size () + size
);
160 return &*(m_vec
.end () - size
);
163 gdb::byte_vector m_vec
;
166 /* An entry in the symbol table. */
167 struct symtab_index_entry
169 /* The name of the symbol. */
171 /* The offset of the name in the constant pool. */
172 offset_type index_offset
;
173 /* A sorted vector of the indices of all the CUs that hold an object
175 std::vector
<offset_type
> cu_indices
;
178 /* The symbol table. This is a power-of-2-sized hash table. */
186 offset_type n_elements
= 0;
187 std::vector
<symtab_index_entry
> data
;
189 /* Temporary storage for Ada names. */
190 auto_obstack m_string_obstack
;
193 /* Find a slot in SYMTAB for the symbol NAME. Returns a reference to
196 Function is used only during write_hash_table so no index format backward
197 compatibility is needed. */
199 static symtab_index_entry
&
200 find_slot (struct mapped_symtab
*symtab
, const char *name
)
202 offset_type index
, step
, hash
= mapped_index_string_hash (INT_MAX
, name
);
204 index
= hash
& (symtab
->data
.size () - 1);
205 step
= ((hash
* 17) & (symtab
->data
.size () - 1)) | 1;
209 if (symtab
->data
[index
].name
== NULL
210 || strcmp (name
, symtab
->data
[index
].name
) == 0)
211 return symtab
->data
[index
];
212 index
= (index
+ step
) & (symtab
->data
.size () - 1);
216 /* Expand SYMTAB's hash table. */
219 hash_expand (struct mapped_symtab
*symtab
)
221 auto old_entries
= std::move (symtab
->data
);
223 symtab
->data
.clear ();
224 symtab
->data
.resize (old_entries
.size () * 2);
226 for (auto &it
: old_entries
)
229 auto &ref
= find_slot (symtab
, it
.name
);
230 ref
= std::move (it
);
234 /* Add an entry to SYMTAB. NAME is the name of the symbol.
235 CU_INDEX is the index of the CU in which the symbol appears.
236 IS_STATIC is one if the symbol is static, otherwise zero (global). */
239 add_index_entry (struct mapped_symtab
*symtab
, const char *name
,
240 int is_static
, gdb_index_symbol_kind kind
,
241 offset_type cu_index
)
243 offset_type cu_index_and_attrs
;
245 ++symtab
->n_elements
;
246 if (4 * symtab
->n_elements
/ 3 >= symtab
->data
.size ())
247 hash_expand (symtab
);
249 symtab_index_entry
&slot
= find_slot (symtab
, name
);
250 if (slot
.name
== NULL
)
253 /* index_offset is set later. */
256 cu_index_and_attrs
= 0;
257 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs
, cu_index
);
258 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs
, is_static
);
259 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs
, kind
);
261 /* We don't want to record an index value twice as we want to avoid the
263 We process all global symbols and then all static symbols
264 (which would allow us to avoid the duplication by only having to check
265 the last entry pushed), but a symbol could have multiple kinds in one CU.
266 To keep things simple we don't worry about the duplication here and
267 sort and uniquify the list after we've processed all symbols. */
268 slot
.cu_indices
.push_back (cu_index_and_attrs
);
271 /* Sort and remove duplicates of all symbols' cu_indices lists. */
274 uniquify_cu_indices (struct mapped_symtab
*symtab
)
276 for (auto &entry
: symtab
->data
)
278 if (entry
.name
!= NULL
&& !entry
.cu_indices
.empty ())
280 auto &cu_indices
= entry
.cu_indices
;
281 std::sort (cu_indices
.begin (), cu_indices
.end ());
282 auto from
= std::unique (cu_indices
.begin (), cu_indices
.end ());
283 cu_indices
.erase (from
, cu_indices
.end ());
288 /* A form of 'const char *' suitable for container keys. Only the
289 pointer is stored. The strings themselves are compared, not the
294 c_str_view (const char *cstr
)
298 bool operator== (const c_str_view
&other
) const
300 return strcmp (m_cstr
, other
.m_cstr
) == 0;
303 /* Return the underlying C string. Note, the returned string is
304 only a reference with lifetime of this object. */
305 const char *c_str () const
311 friend class c_str_view_hasher
;
312 const char *const m_cstr
;
315 /* A std::unordered_map::hasher for c_str_view that uses the right
316 hash function for strings in a mapped index. */
317 class c_str_view_hasher
320 size_t operator () (const c_str_view
&x
) const
322 return mapped_index_string_hash (INT_MAX
, x
.m_cstr
);
326 /* A std::unordered_map::hasher for std::vector<>. */
331 size_t operator () (const std::vector
<T
> &key
) const
333 return iterative_hash (key
.data (),
334 sizeof (key
.front ()) * key
.size (), 0);
338 /* Write the mapped hash table SYMTAB to the data buffer OUTPUT, with
339 constant pool entries going into the data buffer CPOOL. */
342 write_hash_table (mapped_symtab
*symtab
, data_buf
&output
, data_buf
&cpool
)
345 /* Elements are sorted vectors of the indices of all the CUs that
346 hold an object of this name. */
347 std::unordered_map
<std::vector
<offset_type
>, offset_type
,
348 vector_hasher
<offset_type
>>
351 /* We add all the index vectors to the constant pool first, to
352 ensure alignment is ok. */
353 for (symtab_index_entry
&entry
: symtab
->data
)
355 if (entry
.name
== NULL
)
357 gdb_assert (entry
.index_offset
== 0);
359 /* Finding before inserting is faster than always trying to
360 insert, because inserting always allocates a node, does the
361 lookup, and then destroys the new node if another node
362 already had the same key. C++17 try_emplace will avoid
365 = symbol_hash_table
.find (entry
.cu_indices
);
366 if (found
!= symbol_hash_table
.end ())
368 entry
.index_offset
= found
->second
;
372 symbol_hash_table
.emplace (entry
.cu_indices
, cpool
.size ());
373 entry
.index_offset
= cpool
.size ();
374 cpool
.append_data (MAYBE_SWAP (entry
.cu_indices
.size ()));
375 for (const auto index
: entry
.cu_indices
)
376 cpool
.append_data (MAYBE_SWAP (index
));
380 /* Now write out the hash table. */
381 std::unordered_map
<c_str_view
, offset_type
, c_str_view_hasher
> str_table
;
382 for (const auto &entry
: symtab
->data
)
384 offset_type str_off
, vec_off
;
386 if (entry
.name
!= NULL
)
388 const auto insertpair
= str_table
.emplace (entry
.name
, cpool
.size ());
389 if (insertpair
.second
)
390 cpool
.append_cstr0 (entry
.name
);
391 str_off
= insertpair
.first
->second
;
392 vec_off
= entry
.index_offset
;
396 /* While 0 is a valid constant pool index, it is not valid
397 to have 0 for both offsets. */
402 output
.append_data (MAYBE_SWAP (str_off
));
403 output
.append_data (MAYBE_SWAP (vec_off
));
407 typedef std::unordered_map
<partial_symtab
*, unsigned int> psym_index_map
;
409 /* Helper struct for building the address table. */
410 struct addrmap_index_data
412 addrmap_index_data (data_buf
&addr_vec_
, psym_index_map
&cu_index_htab_
)
413 : addr_vec (addr_vec_
), cu_index_htab (cu_index_htab_
)
416 struct objfile
*objfile
;
418 psym_index_map
&cu_index_htab
;
420 /* Non-zero if the previous_* fields are valid.
421 We can't write an entry until we see the next entry (since it is only then
422 that we know the end of the entry). */
424 /* Index of the CU in the table of all CUs in the index file. */
425 unsigned int previous_cu_index
;
426 /* Start address of the CU. */
427 CORE_ADDR previous_cu_start
;
430 /* Write an address entry to ADDR_VEC. */
433 add_address_entry (struct objfile
*objfile
, data_buf
&addr_vec
,
434 CORE_ADDR start
, CORE_ADDR end
, unsigned int cu_index
)
436 addr_vec
.append_uint (8, BFD_ENDIAN_LITTLE
, start
);
437 addr_vec
.append_uint (8, BFD_ENDIAN_LITTLE
, end
);
438 addr_vec
.append_data (MAYBE_SWAP (cu_index
));
441 /* Worker function for traversing an addrmap to build the address table. */
444 add_address_entry_worker (void *datap
, CORE_ADDR start_addr
, void *obj
)
446 struct addrmap_index_data
*data
= (struct addrmap_index_data
*) datap
;
447 partial_symtab
*pst
= (partial_symtab
*) obj
;
449 if (data
->previous_valid
)
450 add_address_entry (data
->objfile
, data
->addr_vec
,
451 data
->previous_cu_start
, start_addr
,
452 data
->previous_cu_index
);
454 data
->previous_cu_start
= start_addr
;
457 const auto it
= data
->cu_index_htab
.find (pst
);
458 gdb_assert (it
!= data
->cu_index_htab
.cend ());
459 data
->previous_cu_index
= it
->second
;
460 data
->previous_valid
= 1;
463 data
->previous_valid
= 0;
468 /* Write OBJFILE's address map to ADDR_VEC.
469 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
470 in the index file. */
473 write_address_map (struct objfile
*objfile
, data_buf
&addr_vec
,
474 psym_index_map
&cu_index_htab
)
476 struct addrmap_index_data
addrmap_index_data (addr_vec
, cu_index_htab
);
478 /* When writing the address table, we have to cope with the fact that
479 the addrmap iterator only provides the start of a region; we have to
480 wait until the next invocation to get the start of the next region. */
482 addrmap_index_data
.objfile
= objfile
;
483 addrmap_index_data
.previous_valid
= 0;
485 addrmap_foreach (objfile
->partial_symtabs
->psymtabs_addrmap
,
486 add_address_entry_worker
, &addrmap_index_data
);
488 /* It's highly unlikely the last entry (end address = 0xff...ff)
489 is valid, but we should still handle it.
490 The end address is recorded as the start of the next region, but that
491 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
493 if (addrmap_index_data
.previous_valid
)
494 add_address_entry (objfile
, addr_vec
,
495 addrmap_index_data
.previous_cu_start
, (CORE_ADDR
) -1,
496 addrmap_index_data
.previous_cu_index
);
499 /* Return the symbol kind of PSYM. */
501 static gdb_index_symbol_kind
502 symbol_kind (struct partial_symbol
*psym
)
504 domain_enum domain
= psym
->domain
;
505 enum address_class aclass
= psym
->aclass
;
513 return GDB_INDEX_SYMBOL_KIND_FUNCTION
;
515 return GDB_INDEX_SYMBOL_KIND_TYPE
;
517 case LOC_CONST_BYTES
:
518 case LOC_OPTIMIZED_OUT
:
520 return GDB_INDEX_SYMBOL_KIND_VARIABLE
;
522 /* Note: It's currently impossible to recognize psyms as enum values
523 short of reading the type info. For now punt. */
524 return GDB_INDEX_SYMBOL_KIND_VARIABLE
;
526 /* There are other LOC_FOO values that one might want to classify
527 as variables, but dwarf2read.c doesn't currently use them. */
528 return GDB_INDEX_SYMBOL_KIND_OTHER
;
531 return GDB_INDEX_SYMBOL_KIND_TYPE
;
533 return GDB_INDEX_SYMBOL_KIND_OTHER
;
537 /* Add a list of partial symbols to SYMTAB. */
540 write_psymbols (struct mapped_symtab
*symtab
,
541 std::unordered_set
<partial_symbol
*> &psyms_seen
,
542 struct partial_symbol
**psymp
,
544 offset_type cu_index
,
547 for (; count
-- > 0; ++psymp
)
549 struct partial_symbol
*psym
= *psymp
;
550 const char *name
= psym
->ginfo
.search_name ();
552 if (psym
->ginfo
.language () == language_ada
)
554 /* We want to ensure that the Ada main function's name appears
555 verbatim in the index. However, this name will be of the
556 form "_ada_mumble", and will be rewritten by ada_decode.
557 So, recognize it specially here and add it to the index by
559 if (strcmp (main_name (), name
) == 0)
561 gdb_index_symbol_kind kind
= symbol_kind (psym
);
563 add_index_entry (symtab
, name
, is_static
, kind
, cu_index
);
566 /* In order for the index to work when read back into gdb, it
567 has to supply a funny form of the name: it should be the
568 encoded name, with any suffixes stripped. Using the
569 ordinary encoded name will not work properly with the
570 searching logic in find_name_components_bounds; nor will
571 using the decoded name. Furthermore, an Ada "verbatim"
572 name (of the form "<MumBle>") must be entered without the
573 angle brackets. Note that the current index is unusual,
574 see PR symtab/24820 for details. */
575 std::string decoded
= ada_decode (name
);
576 if (decoded
[0] == '<')
577 name
= (char *) obstack_copy0 (&symtab
->m_string_obstack
,
578 decoded
.c_str () + 1,
579 decoded
.length () - 2);
581 name
= obstack_strdup (&symtab
->m_string_obstack
,
582 ada_encode (decoded
.c_str ()));
585 /* Only add a given psymbol once. */
586 if (psyms_seen
.insert (psym
).second
)
588 gdb_index_symbol_kind kind
= symbol_kind (psym
);
590 add_index_entry (symtab
, name
, is_static
, kind
, cu_index
);
595 /* A helper struct used when iterating over debug_types. */
596 struct signatured_type_index_data
598 signatured_type_index_data (data_buf
&types_list_
,
599 std::unordered_set
<partial_symbol
*> &psyms_seen_
)
600 : types_list (types_list_
), psyms_seen (psyms_seen_
)
603 struct objfile
*objfile
;
604 struct mapped_symtab
*symtab
;
605 data_buf
&types_list
;
606 std::unordered_set
<partial_symbol
*> &psyms_seen
;
610 /* A helper function that writes a single signatured_type to an
614 write_one_signatured_type (void **slot
, void *d
)
616 struct signatured_type_index_data
*info
617 = (struct signatured_type_index_data
*) d
;
618 struct signatured_type
*entry
= (struct signatured_type
*) *slot
;
619 partial_symtab
*psymtab
= entry
->per_cu
.v
.psymtab
;
621 write_psymbols (info
->symtab
,
623 (info
->objfile
->partial_symtabs
->global_psymbols
.data ()
624 + psymtab
->globals_offset
),
625 psymtab
->n_global_syms
, info
->cu_index
,
627 write_psymbols (info
->symtab
,
629 (info
->objfile
->partial_symtabs
->static_psymbols
.data ()
630 + psymtab
->statics_offset
),
631 psymtab
->n_static_syms
, info
->cu_index
,
634 info
->types_list
.append_uint (8, BFD_ENDIAN_LITTLE
,
635 to_underlying (entry
->per_cu
.sect_off
));
636 info
->types_list
.append_uint (8, BFD_ENDIAN_LITTLE
,
637 to_underlying (entry
->type_offset_in_tu
));
638 info
->types_list
.append_uint (8, BFD_ENDIAN_LITTLE
, entry
->signature
);
645 /* Recurse into all "included" dependencies and count their symbols as
646 if they appeared in this psymtab. */
649 recursively_count_psymbols (partial_symtab
*psymtab
,
652 for (int i
= 0; i
< psymtab
->number_of_dependencies
; ++i
)
653 if (psymtab
->dependencies
[i
]->user
!= NULL
)
654 recursively_count_psymbols (psymtab
->dependencies
[i
],
657 psyms_seen
+= psymtab
->n_global_syms
;
658 psyms_seen
+= psymtab
->n_static_syms
;
661 /* Recurse into all "included" dependencies and write their symbols as
662 if they appeared in this psymtab. */
665 recursively_write_psymbols (struct objfile
*objfile
,
666 partial_symtab
*psymtab
,
667 struct mapped_symtab
*symtab
,
668 std::unordered_set
<partial_symbol
*> &psyms_seen
,
669 offset_type cu_index
)
673 for (i
= 0; i
< psymtab
->number_of_dependencies
; ++i
)
674 if (psymtab
->dependencies
[i
]->user
!= NULL
)
675 recursively_write_psymbols (objfile
,
676 psymtab
->dependencies
[i
],
677 symtab
, psyms_seen
, cu_index
);
679 write_psymbols (symtab
,
681 (objfile
->partial_symtabs
->global_psymbols
.data ()
682 + psymtab
->globals_offset
),
683 psymtab
->n_global_syms
, cu_index
,
685 write_psymbols (symtab
,
687 (objfile
->partial_symtabs
->static_psymbols
.data ()
688 + psymtab
->statics_offset
),
689 psymtab
->n_static_syms
, cu_index
,
693 /* DWARF-5 .debug_names builder. */
697 debug_names (dwarf2_per_objfile
*per_objfile
, bool is_dwarf64
,
698 bfd_endian dwarf5_byte_order
)
699 : m_dwarf5_byte_order (dwarf5_byte_order
),
700 m_dwarf32 (dwarf5_byte_order
),
701 m_dwarf64 (dwarf5_byte_order
),
703 ? static_cast<dwarf
&> (m_dwarf64
)
704 : static_cast<dwarf
&> (m_dwarf32
)),
705 m_name_table_string_offs (m_dwarf
.name_table_string_offs
),
706 m_name_table_entry_offs (m_dwarf
.name_table_entry_offs
),
707 m_debugstrlookup (per_objfile
)
710 int dwarf5_offset_size () const
712 const bool dwarf5_is_dwarf64
= &m_dwarf
== &m_dwarf64
;
713 return dwarf5_is_dwarf64
? 8 : 4;
716 /* Is this symbol from DW_TAG_compile_unit or DW_TAG_type_unit? */
717 enum class unit_kind
{ cu
, tu
};
719 /* Insert one symbol. */
720 void insert (const partial_symbol
*psym
, int cu_index
, bool is_static
,
723 const int dwarf_tag
= psymbol_tag (psym
);
726 const char *name
= psym
->ginfo
.search_name ();
728 if (psym
->ginfo
.language () == language_ada
)
730 /* We want to ensure that the Ada main function's name appears
731 verbatim in the index. However, this name will be of the
732 form "_ada_mumble", and will be rewritten by ada_decode.
733 So, recognize it specially here and add it to the index by
735 if (strcmp (main_name (), name
) == 0)
737 const auto insertpair
738 = m_name_to_value_set
.emplace (c_str_view (name
),
739 std::set
<symbol_value
> ());
740 std::set
<symbol_value
> &value_set
= insertpair
.first
->second
;
741 value_set
.emplace (symbol_value (dwarf_tag
, cu_index
, is_static
,
745 /* In order for the index to work when read back into gdb, it
746 has to supply a funny form of the name: it should be the
747 encoded name, with any suffixes stripped. Using the
748 ordinary encoded name will not work properly with the
749 searching logic in find_name_components_bounds; nor will
750 using the decoded name. Furthermore, an Ada "verbatim"
751 name (of the form "<MumBle>") must be entered without the
752 angle brackets. Note that the current index is unusual,
753 see PR symtab/24820 for details. */
754 std::string decoded
= ada_decode (name
);
755 if (decoded
[0] == '<')
756 name
= (char *) obstack_copy0 (&m_string_obstack
,
757 decoded
.c_str () + 1,
758 decoded
.length () - 2);
760 name
= obstack_strdup (&m_string_obstack
,
761 ada_encode (decoded
.c_str ()));
764 const auto insertpair
765 = m_name_to_value_set
.emplace (c_str_view (name
),
766 std::set
<symbol_value
> ());
767 std::set
<symbol_value
> &value_set
= insertpair
.first
->second
;
768 value_set
.emplace (symbol_value (dwarf_tag
, cu_index
, is_static
, kind
));
771 /* Build all the tables. All symbols must be already inserted.
772 This function does not call file_write, caller has to do it
776 /* Verify the build method has not be called twice. */
777 gdb_assert (m_abbrev_table
.empty ());
778 const size_t name_count
= m_name_to_value_set
.size ();
779 m_bucket_table
.resize
780 (std::pow (2, std::ceil (std::log2 (name_count
* 4 / 3))));
781 m_hash_table
.reserve (name_count
);
782 m_name_table_string_offs
.reserve (name_count
);
783 m_name_table_entry_offs
.reserve (name_count
);
785 /* Map each hash of symbol to its name and value. */
789 decltype (m_name_to_value_set
)::const_iterator it
;
791 std::vector
<std::forward_list
<hash_it_pair
>> bucket_hash
;
792 bucket_hash
.resize (m_bucket_table
.size ());
793 for (decltype (m_name_to_value_set
)::const_iterator it
794 = m_name_to_value_set
.cbegin ();
795 it
!= m_name_to_value_set
.cend ();
798 const char *const name
= it
->first
.c_str ();
799 const uint32_t hash
= dwarf5_djb_hash (name
);
800 hash_it_pair hashitpair
;
801 hashitpair
.hash
= hash
;
803 auto &slot
= bucket_hash
[hash
% bucket_hash
.size()];
804 slot
.push_front (std::move (hashitpair
));
806 for (size_t bucket_ix
= 0; bucket_ix
< bucket_hash
.size (); ++bucket_ix
)
808 const std::forward_list
<hash_it_pair
> &hashitlist
809 = bucket_hash
[bucket_ix
];
810 if (hashitlist
.empty ())
812 uint32_t &bucket_slot
= m_bucket_table
[bucket_ix
];
813 /* The hashes array is indexed starting at 1. */
814 store_unsigned_integer (reinterpret_cast<gdb_byte
*> (&bucket_slot
),
815 sizeof (bucket_slot
), m_dwarf5_byte_order
,
816 m_hash_table
.size () + 1);
817 for (const hash_it_pair
&hashitpair
: hashitlist
)
819 m_hash_table
.push_back (0);
820 store_unsigned_integer (reinterpret_cast<gdb_byte
*>
821 (&m_hash_table
.back ()),
822 sizeof (m_hash_table
.back ()),
823 m_dwarf5_byte_order
, hashitpair
.hash
);
824 const c_str_view
&name
= hashitpair
.it
->first
;
825 const std::set
<symbol_value
> &value_set
= hashitpair
.it
->second
;
826 m_name_table_string_offs
.push_back_reorder
827 (m_debugstrlookup
.lookup (name
.c_str ()));
828 m_name_table_entry_offs
.push_back_reorder (m_entry_pool
.size ());
829 gdb_assert (!value_set
.empty ());
830 for (const symbol_value
&value
: value_set
)
832 int &idx
= m_indexkey_to_idx
[index_key (value
.dwarf_tag
,
838 m_abbrev_table
.append_unsigned_leb128 (idx
);
839 m_abbrev_table
.append_unsigned_leb128 (value
.dwarf_tag
);
840 m_abbrev_table
.append_unsigned_leb128
841 (value
.kind
== unit_kind::cu
? DW_IDX_compile_unit
843 m_abbrev_table
.append_unsigned_leb128 (DW_FORM_udata
);
844 m_abbrev_table
.append_unsigned_leb128 (value
.is_static
845 ? DW_IDX_GNU_internal
846 : DW_IDX_GNU_external
);
847 m_abbrev_table
.append_unsigned_leb128 (DW_FORM_flag_present
);
849 /* Terminate attributes list. */
850 m_abbrev_table
.append_unsigned_leb128 (0);
851 m_abbrev_table
.append_unsigned_leb128 (0);
854 m_entry_pool
.append_unsigned_leb128 (idx
);
855 m_entry_pool
.append_unsigned_leb128 (value
.cu_index
);
858 /* Terminate the list of CUs. */
859 m_entry_pool
.append_unsigned_leb128 (0);
862 gdb_assert (m_hash_table
.size () == name_count
);
864 /* Terminate tags list. */
865 m_abbrev_table
.append_unsigned_leb128 (0);
868 /* Return .debug_names bucket count. This must be called only after
869 calling the build method. */
870 uint32_t bucket_count () const
872 /* Verify the build method has been already called. */
873 gdb_assert (!m_abbrev_table
.empty ());
874 const uint32_t retval
= m_bucket_table
.size ();
876 /* Check for overflow. */
877 gdb_assert (retval
== m_bucket_table
.size ());
881 /* Return .debug_names names count. This must be called only after
882 calling the build method. */
883 uint32_t name_count () const
885 /* Verify the build method has been already called. */
886 gdb_assert (!m_abbrev_table
.empty ());
887 const uint32_t retval
= m_hash_table
.size ();
889 /* Check for overflow. */
890 gdb_assert (retval
== m_hash_table
.size ());
894 /* Return number of bytes of .debug_names abbreviation table. This
895 must be called only after calling the build method. */
896 uint32_t abbrev_table_bytes () const
898 gdb_assert (!m_abbrev_table
.empty ());
899 return m_abbrev_table
.size ();
902 /* Recurse into all "included" dependencies and store their symbols
903 as if they appeared in this psymtab. */
904 void recursively_write_psymbols
905 (struct objfile
*objfile
,
906 partial_symtab
*psymtab
,
907 std::unordered_set
<partial_symbol
*> &psyms_seen
,
910 for (int i
= 0; i
< psymtab
->number_of_dependencies
; ++i
)
911 if (psymtab
->dependencies
[i
]->user
!= NULL
)
912 recursively_write_psymbols
913 (objfile
, psymtab
->dependencies
[i
], psyms_seen
, cu_index
);
915 write_psymbols (psyms_seen
,
916 (objfile
->partial_symtabs
->global_psymbols
.data ()
917 + psymtab
->globals_offset
),
918 psymtab
->n_global_syms
, cu_index
, false, unit_kind::cu
);
919 write_psymbols (psyms_seen
,
920 (objfile
->partial_symtabs
->static_psymbols
.data ()
921 + psymtab
->statics_offset
),
922 psymtab
->n_static_syms
, cu_index
, true, unit_kind::cu
);
925 /* Return number of bytes the .debug_names section will have. This
926 must be called only after calling the build method. */
927 size_t bytes () const
929 /* Verify the build method has been already called. */
930 gdb_assert (!m_abbrev_table
.empty ());
931 size_t expected_bytes
= 0;
932 expected_bytes
+= m_bucket_table
.size () * sizeof (m_bucket_table
[0]);
933 expected_bytes
+= m_hash_table
.size () * sizeof (m_hash_table
[0]);
934 expected_bytes
+= m_name_table_string_offs
.bytes ();
935 expected_bytes
+= m_name_table_entry_offs
.bytes ();
936 expected_bytes
+= m_abbrev_table
.size ();
937 expected_bytes
+= m_entry_pool
.size ();
938 return expected_bytes
;
941 /* Write .debug_names to FILE_NAMES and .debug_str addition to
942 FILE_STR. This must be called only after calling the build
944 void file_write (FILE *file_names
, FILE *file_str
) const
946 /* Verify the build method has been already called. */
947 gdb_assert (!m_abbrev_table
.empty ());
948 ::file_write (file_names
, m_bucket_table
);
949 ::file_write (file_names
, m_hash_table
);
950 m_name_table_string_offs
.file_write (file_names
);
951 m_name_table_entry_offs
.file_write (file_names
);
952 m_abbrev_table
.file_write (file_names
);
953 m_entry_pool
.file_write (file_names
);
954 m_debugstrlookup
.file_write (file_str
);
957 /* A helper user data for write_one_signatured_type. */
958 class write_one_signatured_type_data
961 write_one_signatured_type_data (debug_names
&nametable_
,
962 signatured_type_index_data
&&info_
)
963 : nametable (nametable_
), info (std::move (info_
))
965 debug_names
&nametable
;
966 struct signatured_type_index_data info
;
969 /* A helper function to pass write_one_signatured_type to
970 htab_traverse_noresize. */
972 write_one_signatured_type (void **slot
, void *d
)
974 write_one_signatured_type_data
*data
= (write_one_signatured_type_data
*) d
;
975 struct signatured_type_index_data
*info
= &data
->info
;
976 struct signatured_type
*entry
= (struct signatured_type
*) *slot
;
978 data
->nametable
.write_one_signatured_type (entry
, info
);
985 /* Storage for symbol names mapping them to their .debug_str section
987 class debug_str_lookup
991 /* Object constructor to be called for current DWARF2_PER_OBJFILE.
992 All .debug_str section strings are automatically stored. */
993 debug_str_lookup (dwarf2_per_objfile
*per_objfile
)
994 : m_abfd (per_objfile
->objfile
->obfd
),
995 m_per_objfile (per_objfile
)
997 per_objfile
->per_bfd
->str
.read (per_objfile
->objfile
);
998 if (per_objfile
->per_bfd
->str
.buffer
== NULL
)
1000 for (const gdb_byte
*data
= per_objfile
->per_bfd
->str
.buffer
;
1001 data
< (per_objfile
->per_bfd
->str
.buffer
1002 + per_objfile
->per_bfd
->str
.size
);)
1004 const char *const s
= reinterpret_cast<const char *> (data
);
1005 const auto insertpair
1006 = m_str_table
.emplace (c_str_view (s
),
1007 data
- per_objfile
->per_bfd
->str
.buffer
);
1008 if (!insertpair
.second
)
1009 complaint (_("Duplicate string \"%s\" in "
1010 ".debug_str section [in module %s]"),
1011 s
, bfd_get_filename (m_abfd
));
1012 data
+= strlen (s
) + 1;
1016 /* Return offset of symbol name S in the .debug_str section. Add
1017 such symbol to the section's end if it does not exist there
1019 size_t lookup (const char *s
)
1021 const auto it
= m_str_table
.find (c_str_view (s
));
1022 if (it
!= m_str_table
.end ())
1024 const size_t offset
= (m_per_objfile
->per_bfd
->str
.size
1025 + m_str_add_buf
.size ());
1026 m_str_table
.emplace (c_str_view (s
), offset
);
1027 m_str_add_buf
.append_cstr0 (s
);
1031 /* Append the end of the .debug_str section to FILE. */
1032 void file_write (FILE *file
) const
1034 m_str_add_buf
.file_write (file
);
1038 std::unordered_map
<c_str_view
, size_t, c_str_view_hasher
> m_str_table
;
1040 dwarf2_per_objfile
*m_per_objfile
;
1042 /* Data to add at the end of .debug_str for new needed symbol names. */
1043 data_buf m_str_add_buf
;
1046 /* Container to map used DWARF tags to their .debug_names abbreviation
1051 index_key (int dwarf_tag_
, bool is_static_
, unit_kind kind_
)
1052 : dwarf_tag (dwarf_tag_
), is_static (is_static_
), kind (kind_
)
1057 operator== (const index_key
&other
) const
1059 return (dwarf_tag
== other
.dwarf_tag
&& is_static
== other
.is_static
1060 && kind
== other
.kind
);
1063 const int dwarf_tag
;
1064 const bool is_static
;
1065 const unit_kind kind
;
1068 /* Provide std::unordered_map::hasher for index_key. */
1069 class index_key_hasher
1073 operator () (const index_key
&key
) const
1075 return (std::hash
<int>() (key
.dwarf_tag
) << 1) | key
.is_static
;
1079 /* Parameters of one symbol entry. */
1083 const int dwarf_tag
, cu_index
;
1084 const bool is_static
;
1085 const unit_kind kind
;
1087 symbol_value (int dwarf_tag_
, int cu_index_
, bool is_static_
,
1089 : dwarf_tag (dwarf_tag_
), cu_index (cu_index_
), is_static (is_static_
),
1094 operator< (const symbol_value
&other
) const
1114 /* Abstract base class to unify DWARF-32 and DWARF-64 name table
1119 const bfd_endian dwarf5_byte_order
;
1121 explicit offset_vec (bfd_endian dwarf5_byte_order_
)
1122 : dwarf5_byte_order (dwarf5_byte_order_
)
1125 /* Call std::vector::reserve for NELEM elements. */
1126 virtual void reserve (size_t nelem
) = 0;
1128 /* Call std::vector::push_back with store_unsigned_integer byte
1129 reordering for ELEM. */
1130 virtual void push_back_reorder (size_t elem
) = 0;
1132 /* Return expected output size in bytes. */
1133 virtual size_t bytes () const = 0;
1135 /* Write name table to FILE. */
1136 virtual void file_write (FILE *file
) const = 0;
1139 /* Template to unify DWARF-32 and DWARF-64 output. */
1140 template<typename OffsetSize
>
1141 class offset_vec_tmpl
: public offset_vec
1144 explicit offset_vec_tmpl (bfd_endian dwarf5_byte_order_
)
1145 : offset_vec (dwarf5_byte_order_
)
1148 /* Implement offset_vec::reserve. */
1149 void reserve (size_t nelem
) override
1151 m_vec
.reserve (nelem
);
1154 /* Implement offset_vec::push_back_reorder. */
1155 void push_back_reorder (size_t elem
) override
1157 m_vec
.push_back (elem
);
1158 /* Check for overflow. */
1159 gdb_assert (m_vec
.back () == elem
);
1160 store_unsigned_integer (reinterpret_cast<gdb_byte
*> (&m_vec
.back ()),
1161 sizeof (m_vec
.back ()), dwarf5_byte_order
, elem
);
1164 /* Implement offset_vec::bytes. */
1165 size_t bytes () const override
1167 return m_vec
.size () * sizeof (m_vec
[0]);
1170 /* Implement offset_vec::file_write. */
1171 void file_write (FILE *file
) const override
1173 ::file_write (file
, m_vec
);
1177 std::vector
<OffsetSize
> m_vec
;
1180 /* Base class to unify DWARF-32 and DWARF-64 .debug_names output
1181 respecting name table width. */
1185 offset_vec
&name_table_string_offs
, &name_table_entry_offs
;
1187 dwarf (offset_vec
&name_table_string_offs_
,
1188 offset_vec
&name_table_entry_offs_
)
1189 : name_table_string_offs (name_table_string_offs_
),
1190 name_table_entry_offs (name_table_entry_offs_
)
1195 /* Template to unify DWARF-32 and DWARF-64 .debug_names output
1196 respecting name table width. */
1197 template<typename OffsetSize
>
1198 class dwarf_tmpl
: public dwarf
1201 explicit dwarf_tmpl (bfd_endian dwarf5_byte_order_
)
1202 : dwarf (m_name_table_string_offs
, m_name_table_entry_offs
),
1203 m_name_table_string_offs (dwarf5_byte_order_
),
1204 m_name_table_entry_offs (dwarf5_byte_order_
)
1208 offset_vec_tmpl
<OffsetSize
> m_name_table_string_offs
;
1209 offset_vec_tmpl
<OffsetSize
> m_name_table_entry_offs
;
1212 /* Try to reconstruct original DWARF tag for given partial_symbol.
1213 This function is not DWARF-5 compliant but it is sufficient for
1214 GDB as a DWARF-5 index consumer. */
1215 static int psymbol_tag (const struct partial_symbol
*psym
)
1217 domain_enum domain
= psym
->domain
;
1218 enum address_class aclass
= psym
->aclass
;
1226 return DW_TAG_subprogram
;
1228 return DW_TAG_typedef
;
1230 case LOC_CONST_BYTES
:
1231 case LOC_OPTIMIZED_OUT
:
1233 return DW_TAG_variable
;
1235 /* Note: It's currently impossible to recognize psyms as enum values
1236 short of reading the type info. For now punt. */
1237 return DW_TAG_variable
;
1239 /* There are other LOC_FOO values that one might want to classify
1240 as variables, but dwarf2read.c doesn't currently use them. */
1241 return DW_TAG_variable
;
1244 return DW_TAG_structure_type
;
1246 return DW_TAG_module
;
1252 /* Call insert for all partial symbols and mark them in PSYMS_SEEN. */
1253 void write_psymbols (std::unordered_set
<partial_symbol
*> &psyms_seen
,
1254 struct partial_symbol
**psymp
, int count
, int cu_index
,
1255 bool is_static
, unit_kind kind
)
1257 for (; count
-- > 0; ++psymp
)
1259 struct partial_symbol
*psym
= *psymp
;
1261 /* Only add a given psymbol once. */
1262 if (psyms_seen
.insert (psym
).second
)
1263 insert (psym
, cu_index
, is_static
, kind
);
1267 /* A helper function that writes a single signatured_type
1268 to a debug_names. */
1270 write_one_signatured_type (struct signatured_type
*entry
,
1271 struct signatured_type_index_data
*info
)
1273 partial_symtab
*psymtab
= entry
->per_cu
.v
.psymtab
;
1275 write_psymbols (info
->psyms_seen
,
1276 (info
->objfile
->partial_symtabs
->global_psymbols
.data ()
1277 + psymtab
->globals_offset
),
1278 psymtab
->n_global_syms
, info
->cu_index
, false,
1280 write_psymbols (info
->psyms_seen
,
1281 (info
->objfile
->partial_symtabs
->static_psymbols
.data ()
1282 + psymtab
->statics_offset
),
1283 psymtab
->n_static_syms
, info
->cu_index
, true,
1286 info
->types_list
.append_uint (dwarf5_offset_size (), m_dwarf5_byte_order
,
1287 to_underlying (entry
->per_cu
.sect_off
));
1292 /* Store value of each symbol. */
1293 std::unordered_map
<c_str_view
, std::set
<symbol_value
>, c_str_view_hasher
>
1294 m_name_to_value_set
;
1296 /* Tables of DWARF-5 .debug_names. They are in object file byte
1298 std::vector
<uint32_t> m_bucket_table
;
1299 std::vector
<uint32_t> m_hash_table
;
1301 const bfd_endian m_dwarf5_byte_order
;
1302 dwarf_tmpl
<uint32_t> m_dwarf32
;
1303 dwarf_tmpl
<uint64_t> m_dwarf64
;
1305 offset_vec
&m_name_table_string_offs
, &m_name_table_entry_offs
;
1306 debug_str_lookup m_debugstrlookup
;
1308 /* Map each used .debug_names abbreviation tag parameter to its
1310 std::unordered_map
<index_key
, int, index_key_hasher
> m_indexkey_to_idx
;
1312 /* Next unused .debug_names abbreviation tag for
1313 m_indexkey_to_idx. */
1316 /* .debug_names abbreviation table. */
1317 data_buf m_abbrev_table
;
1319 /* .debug_names entry pool. */
1320 data_buf m_entry_pool
;
1322 /* Temporary storage for Ada names. */
1323 auto_obstack m_string_obstack
;
1326 /* Return iff any of the needed offsets does not fit into 32-bit
1327 .debug_names section. */
1330 check_dwarf64_offsets (dwarf2_per_objfile
*per_objfile
)
1332 for (dwarf2_per_cu_data
*per_cu
: per_objfile
->per_bfd
->all_comp_units
)
1334 if (to_underlying (per_cu
->sect_off
) >= (static_cast<uint64_t> (1) << 32))
1337 for (const signatured_type
*sigtype
: per_objfile
->per_bfd
->all_type_units
)
1339 const dwarf2_per_cu_data
&per_cu
= sigtype
->per_cu
;
1341 if (to_underlying (per_cu
.sect_off
) >= (static_cast<uint64_t> (1) << 32))
1347 /* The psyms_seen set is potentially going to be largish (~40k
1348 elements when indexing a -g3 build of GDB itself). Estimate the
1349 number of elements in order to avoid too many rehashes, which
1350 require rebuilding buckets and thus many trips to
1354 psyms_seen_size (dwarf2_per_objfile
*per_objfile
)
1356 size_t psyms_count
= 0;
1357 for (dwarf2_per_cu_data
*per_cu
: per_objfile
->per_bfd
->all_comp_units
)
1359 partial_symtab
*psymtab
= per_cu
->v
.psymtab
;
1361 if (psymtab
!= NULL
&& psymtab
->user
== NULL
)
1362 recursively_count_psymbols (psymtab
, psyms_count
);
1364 /* Generating an index for gdb itself shows a ratio of
1365 TOTAL_SEEN_SYMS/UNIQUE_SYMS or ~5. 4 seems like a good bet. */
1366 return psyms_count
/ 4;
1369 /* Assert that FILE's size is EXPECTED_SIZE. Assumes file's seek
1370 position is at the end of the file. */
1373 assert_file_size (FILE *file
, size_t expected_size
)
1375 const auto file_size
= ftell (file
);
1376 if (file_size
== -1)
1377 perror_with_name (("ftell"));
1378 gdb_assert (file_size
== expected_size
);
1381 /* Write a gdb index file to OUT_FILE from all the sections passed as
1385 write_gdbindex_1 (FILE *out_file
,
1386 const data_buf
&cu_list
,
1387 const data_buf
&types_cu_list
,
1388 const data_buf
&addr_vec
,
1389 const data_buf
&symtab_vec
,
1390 const data_buf
&constant_pool
)
1393 const offset_type size_of_header
= 6 * sizeof (offset_type
);
1394 offset_type total_len
= size_of_header
;
1396 /* The version number. */
1397 contents
.append_data (MAYBE_SWAP (8));
1399 /* The offset of the CU list from the start of the file. */
1400 contents
.append_data (MAYBE_SWAP (total_len
));
1401 total_len
+= cu_list
.size ();
1403 /* The offset of the types CU list from the start of the file. */
1404 contents
.append_data (MAYBE_SWAP (total_len
));
1405 total_len
+= types_cu_list
.size ();
1407 /* The offset of the address table from the start of the file. */
1408 contents
.append_data (MAYBE_SWAP (total_len
));
1409 total_len
+= addr_vec
.size ();
1411 /* The offset of the symbol table from the start of the file. */
1412 contents
.append_data (MAYBE_SWAP (total_len
));
1413 total_len
+= symtab_vec
.size ();
1415 /* The offset of the constant pool from the start of the file. */
1416 contents
.append_data (MAYBE_SWAP (total_len
));
1417 total_len
+= constant_pool
.size ();
1419 gdb_assert (contents
.size () == size_of_header
);
1421 contents
.file_write (out_file
);
1422 cu_list
.file_write (out_file
);
1423 types_cu_list
.file_write (out_file
);
1424 addr_vec
.file_write (out_file
);
1425 symtab_vec
.file_write (out_file
);
1426 constant_pool
.file_write (out_file
);
1428 assert_file_size (out_file
, total_len
);
1431 /* Write contents of a .gdb_index section for OBJFILE into OUT_FILE.
1432 If OBJFILE has an associated dwz file, write contents of a .gdb_index
1433 section for that dwz file into DWZ_OUT_FILE. If OBJFILE does not have an
1434 associated dwz file, DWZ_OUT_FILE must be NULL. */
1437 write_gdbindex (dwarf2_per_objfile
*per_objfile
, FILE *out_file
,
1440 struct objfile
*objfile
= per_objfile
->objfile
;
1441 mapped_symtab symtab
;
1442 data_buf objfile_cu_list
;
1443 data_buf dwz_cu_list
;
1445 /* While we're scanning CU's create a table that maps a psymtab pointer
1446 (which is what addrmap records) to its index (which is what is recorded
1447 in the index file). This will later be needed to write the address
1449 psym_index_map cu_index_htab
;
1450 cu_index_htab
.reserve (per_objfile
->per_bfd
->all_comp_units
.size ());
1452 /* The CU list is already sorted, so we don't need to do additional
1453 work here. Also, the debug_types entries do not appear in
1454 all_comp_units, but only in their own hash table. */
1456 std::unordered_set
<partial_symbol
*> psyms_seen
1457 (psyms_seen_size (per_objfile
));
1458 for (int i
= 0; i
< per_objfile
->per_bfd
->all_comp_units
.size (); ++i
)
1460 dwarf2_per_cu_data
*per_cu
= per_objfile
->per_bfd
->all_comp_units
[i
];
1461 partial_symtab
*psymtab
= per_cu
->v
.psymtab
;
1463 if (psymtab
!= NULL
)
1465 if (psymtab
->user
== NULL
)
1466 recursively_write_psymbols (objfile
, psymtab
, &symtab
,
1469 const auto insertpair
= cu_index_htab
.emplace (psymtab
, i
);
1470 gdb_assert (insertpair
.second
);
1473 /* The all_comp_units list contains CUs read from the objfile as well as
1474 from the eventual dwz file. We need to place the entry in the
1475 corresponding index. */
1476 data_buf
&cu_list
= per_cu
->is_dwz
? dwz_cu_list
: objfile_cu_list
;
1477 cu_list
.append_uint (8, BFD_ENDIAN_LITTLE
,
1478 to_underlying (per_cu
->sect_off
));
1479 cu_list
.append_uint (8, BFD_ENDIAN_LITTLE
, per_cu
->length
);
1482 /* Dump the address map. */
1484 write_address_map (objfile
, addr_vec
, cu_index_htab
);
1486 /* Write out the .debug_type entries, if any. */
1487 data_buf types_cu_list
;
1488 if (per_objfile
->per_bfd
->signatured_types
)
1490 signatured_type_index_data
sig_data (types_cu_list
,
1493 sig_data
.objfile
= objfile
;
1494 sig_data
.symtab
= &symtab
;
1495 sig_data
.cu_index
= per_objfile
->per_bfd
->all_comp_units
.size ();
1496 htab_traverse_noresize (per_objfile
->per_bfd
->signatured_types
.get (),
1497 write_one_signatured_type
, &sig_data
);
1500 /* Now that we've processed all symbols we can shrink their cu_indices
1502 uniquify_cu_indices (&symtab
);
1504 data_buf symtab_vec
, constant_pool
;
1505 write_hash_table (&symtab
, symtab_vec
, constant_pool
);
1507 write_gdbindex_1(out_file
, objfile_cu_list
, types_cu_list
, addr_vec
,
1508 symtab_vec
, constant_pool
);
1510 if (dwz_out_file
!= NULL
)
1511 write_gdbindex_1 (dwz_out_file
, dwz_cu_list
, {}, {}, {}, {});
1513 gdb_assert (dwz_cu_list
.empty ());
1516 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
1517 static const gdb_byte dwarf5_gdb_augmentation
[] = { 'G', 'D', 'B', 0 };
1519 /* Write a new .debug_names section for OBJFILE into OUT_FILE, write
1520 needed addition to .debug_str section to OUT_FILE_STR. Return how
1521 many bytes were expected to be written into OUT_FILE. */
1524 write_debug_names (dwarf2_per_objfile
*per_objfile
,
1525 FILE *out_file
, FILE *out_file_str
)
1527 const bool dwarf5_is_dwarf64
= check_dwarf64_offsets (per_objfile
);
1528 struct objfile
*objfile
= per_objfile
->objfile
;
1529 const enum bfd_endian dwarf5_byte_order
1530 = gdbarch_byte_order (objfile
->arch ());
1532 /* The CU list is already sorted, so we don't need to do additional
1533 work here. Also, the debug_types entries do not appear in
1534 all_comp_units, but only in their own hash table. */
1536 debug_names
nametable (per_objfile
, dwarf5_is_dwarf64
, dwarf5_byte_order
);
1537 std::unordered_set
<partial_symbol
*>
1538 psyms_seen (psyms_seen_size (per_objfile
));
1539 for (int i
= 0; i
< per_objfile
->per_bfd
->all_comp_units
.size (); ++i
)
1541 const dwarf2_per_cu_data
*per_cu
= per_objfile
->per_bfd
->all_comp_units
[i
];
1542 partial_symtab
*psymtab
= per_cu
->v
.psymtab
;
1544 /* CU of a shared file from 'dwz -m' may be unused by this main
1545 file. It may be referenced from a local scope but in such
1546 case it does not need to be present in .debug_names. */
1547 if (psymtab
== NULL
)
1550 if (psymtab
->user
== NULL
)
1551 nametable
.recursively_write_psymbols (objfile
, psymtab
, psyms_seen
, i
);
1553 cu_list
.append_uint (nametable
.dwarf5_offset_size (), dwarf5_byte_order
,
1554 to_underlying (per_cu
->sect_off
));
1557 /* Write out the .debug_type entries, if any. */
1558 data_buf types_cu_list
;
1559 if (per_objfile
->per_bfd
->signatured_types
)
1561 debug_names::write_one_signatured_type_data
sig_data (nametable
,
1562 signatured_type_index_data (types_cu_list
, psyms_seen
));
1564 sig_data
.info
.objfile
= objfile
;
1565 /* It is used only for gdb_index. */
1566 sig_data
.info
.symtab
= nullptr;
1567 sig_data
.info
.cu_index
= 0;
1568 htab_traverse_noresize (per_objfile
->per_bfd
->signatured_types
.get (),
1569 debug_names::write_one_signatured_type
,
1575 /* No addr_vec - DWARF-5 uses .debug_aranges generated by GCC. */
1577 const offset_type bytes_of_header
1578 = ((dwarf5_is_dwarf64
? 12 : 4)
1580 + sizeof (dwarf5_gdb_augmentation
));
1581 size_t expected_bytes
= 0;
1582 expected_bytes
+= bytes_of_header
;
1583 expected_bytes
+= cu_list
.size ();
1584 expected_bytes
+= types_cu_list
.size ();
1585 expected_bytes
+= nametable
.bytes ();
1588 if (!dwarf5_is_dwarf64
)
1590 const uint64_t size64
= expected_bytes
- 4;
1591 gdb_assert (size64
< 0xfffffff0);
1592 header
.append_uint (4, dwarf5_byte_order
, size64
);
1596 header
.append_uint (4, dwarf5_byte_order
, 0xffffffff);
1597 header
.append_uint (8, dwarf5_byte_order
, expected_bytes
- 12);
1600 /* The version number. */
1601 header
.append_uint (2, dwarf5_byte_order
, 5);
1604 header
.append_uint (2, dwarf5_byte_order
, 0);
1606 /* comp_unit_count - The number of CUs in the CU list. */
1607 header
.append_uint (4, dwarf5_byte_order
,
1608 per_objfile
->per_bfd
->all_comp_units
.size ());
1610 /* local_type_unit_count - The number of TUs in the local TU
1612 header
.append_uint (4, dwarf5_byte_order
,
1613 per_objfile
->per_bfd
->all_type_units
.size ());
1615 /* foreign_type_unit_count - The number of TUs in the foreign TU
1617 header
.append_uint (4, dwarf5_byte_order
, 0);
1619 /* bucket_count - The number of hash buckets in the hash lookup
1621 header
.append_uint (4, dwarf5_byte_order
, nametable
.bucket_count ());
1623 /* name_count - The number of unique names in the index. */
1624 header
.append_uint (4, dwarf5_byte_order
, nametable
.name_count ());
1626 /* abbrev_table_size - The size in bytes of the abbreviations
1628 header
.append_uint (4, dwarf5_byte_order
, nametable
.abbrev_table_bytes ());
1630 /* augmentation_string_size - The size in bytes of the augmentation
1631 string. This value is rounded up to a multiple of 4. */
1632 static_assert (sizeof (dwarf5_gdb_augmentation
) % 4 == 0, "");
1633 header
.append_uint (4, dwarf5_byte_order
, sizeof (dwarf5_gdb_augmentation
));
1634 header
.append_data (dwarf5_gdb_augmentation
);
1636 gdb_assert (header
.size () == bytes_of_header
);
1638 header
.file_write (out_file
);
1639 cu_list
.file_write (out_file
);
1640 types_cu_list
.file_write (out_file
);
1641 nametable
.file_write (out_file
, out_file_str
);
1643 assert_file_size (out_file
, expected_bytes
);
1646 /* This represents an index file being written (work-in-progress).
1648 The data is initially written to a temporary file. When the finalize method
1649 is called, the file is closed and moved to its final location.
1651 On failure (if this object is being destroyed with having called finalize),
1652 the temporary file is closed and deleted. */
1654 struct index_wip_file
1656 index_wip_file (const char *dir
, const char *basename
,
1659 filename
= (std::string (dir
) + SLASH_STRING
+ basename
1662 filename_temp
= make_temp_filename (filename
);
1664 scoped_fd
out_file_fd (gdb_mkostemp_cloexec (filename_temp
.data (),
1666 if (out_file_fd
.get () == -1)
1667 perror_with_name (("mkstemp"));
1669 out_file
= out_file_fd
.to_file ("wb");
1671 if (out_file
== nullptr)
1672 error (_("Can't open `%s' for writing"), filename_temp
.data ());
1674 unlink_file
.emplace (filename_temp
.data ());
1679 /* We want to keep the file. */
1680 unlink_file
->keep ();
1682 /* Close and move the str file in place. */
1683 unlink_file
.reset ();
1684 if (rename (filename_temp
.data (), filename
.c_str ()) != 0)
1685 perror_with_name (("rename"));
1688 std::string filename
;
1689 gdb::char_vector filename_temp
;
1691 /* Order matters here; we want FILE to be closed before
1692 FILENAME_TEMP is unlinked, because on MS-Windows one cannot
1693 delete a file that is still open. So, we wrap the unlinker in an
1694 optional and emplace it once we know the file name. */
1695 gdb::optional
<gdb::unlinker
> unlink_file
;
1697 gdb_file_up out_file
;
1700 /* See dwarf-index-write.h. */
1703 write_psymtabs_to_index (dwarf2_per_objfile
*per_objfile
, const char *dir
,
1704 const char *basename
, const char *dwz_basename
,
1705 dw_index_kind index_kind
)
1707 struct objfile
*objfile
= per_objfile
->objfile
;
1709 if (per_objfile
->per_bfd
->using_index
)
1710 error (_("Cannot use an index to create the index"));
1712 if (per_objfile
->per_bfd
->types
.size () > 1)
1713 error (_("Cannot make an index when the file has multiple .debug_types sections"));
1715 if (!objfile
->partial_symtabs
->psymtabs
1716 || !objfile
->partial_symtabs
->psymtabs_addrmap
)
1720 if (stat (objfile_name (objfile
), &st
) < 0)
1721 perror_with_name (objfile_name (objfile
));
1723 const char *index_suffix
= (index_kind
== dw_index_kind::DEBUG_NAMES
1724 ? INDEX5_SUFFIX
: INDEX4_SUFFIX
);
1726 index_wip_file
objfile_index_wip (dir
, basename
, index_suffix
);
1727 gdb::optional
<index_wip_file
> dwz_index_wip
;
1729 if (dwz_basename
!= NULL
)
1730 dwz_index_wip
.emplace (dir
, dwz_basename
, index_suffix
);
1732 if (index_kind
== dw_index_kind::DEBUG_NAMES
)
1734 index_wip_file
str_wip_file (dir
, basename
, DEBUG_STR_SUFFIX
);
1736 write_debug_names (per_objfile
, objfile_index_wip
.out_file
.get (),
1737 str_wip_file
.out_file
.get ());
1739 str_wip_file
.finalize ();
1742 write_gdbindex (per_objfile
, objfile_index_wip
.out_file
.get (),
1743 (dwz_index_wip
.has_value ()
1744 ? dwz_index_wip
->out_file
.get () : NULL
));
1746 objfile_index_wip
.finalize ();
1748 if (dwz_index_wip
.has_value ())
1749 dwz_index_wip
->finalize ();
1752 /* Implementation of the `save gdb-index' command.
1754 Note that the .gdb_index file format used by this command is
1755 documented in the GDB manual. Any changes here must be documented
1759 save_gdb_index_command (const char *arg
, int from_tty
)
1761 const char dwarf5space
[] = "-dwarf-5 ";
1762 dw_index_kind index_kind
= dw_index_kind::GDB_INDEX
;
1767 arg
= skip_spaces (arg
);
1768 if (strncmp (arg
, dwarf5space
, strlen (dwarf5space
)) == 0)
1770 index_kind
= dw_index_kind::DEBUG_NAMES
;
1771 arg
+= strlen (dwarf5space
);
1772 arg
= skip_spaces (arg
);
1776 error (_("usage: save gdb-index [-dwarf-5] DIRECTORY"));
1778 for (objfile
*objfile
: current_program_space
->objfiles ())
1782 /* If the objfile does not correspond to an actual file, skip it. */
1783 if (stat (objfile_name (objfile
), &st
) < 0)
1786 dwarf2_per_objfile
*per_objfile
= get_dwarf2_per_objfile (objfile
);
1788 if (per_objfile
!= NULL
)
1792 const char *basename
= lbasename (objfile_name (objfile
));
1793 const dwz_file
*dwz
= dwarf2_get_dwz_file (per_objfile
->per_bfd
);
1794 const char *dwz_basename
= NULL
;
1797 dwz_basename
= lbasename (dwz
->filename ());
1799 write_psymtabs_to_index (per_objfile
, arg
, basename
, dwz_basename
,
1802 catch (const gdb_exception_error
&except
)
1804 exception_fprintf (gdb_stderr
, except
,
1805 _("Error while writing index for `%s': "),
1806 objfile_name (objfile
));
1813 void _initialize_dwarf_index_write ();
1815 _initialize_dwarf_index_write ()
1817 cmd_list_element
*c
= add_cmd ("gdb-index", class_files
,
1818 save_gdb_index_command
, _("\
1819 Save a gdb-index file.\n\
1820 Usage: save gdb-index [-dwarf-5] DIRECTORY\n\
1822 No options create one file with .gdb-index extension for pre-DWARF-5\n\
1823 compatible .gdb_index section. With -dwarf-5 creates two files with\n\
1824 extension .debug_names and .debug_str for DWARF-5 .debug_names section."),
1826 set_cmd_completer (c
, filename_completer
);