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 "dwarf-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 "dwarf-index-common.h"
34 #include "dwarf2read.h"
35 #include "gdb/gdb-index.h"
44 #include <unordered_map>
45 #include <unordered_set>
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 allotted number 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
)
88 file_write (file
, vec
.data (), vec
.size () * sizeof (vec
[0]));
91 /* In-memory buffer to prepare data to be written later to a file. */
95 /* Copy DATA to the end of the buffer. */
97 void append_data (const T
&data
)
99 std::copy (reinterpret_cast<const gdb_byte
*> (&data
),
100 reinterpret_cast<const gdb_byte
*> (&data
+ 1),
101 grow (sizeof (data
)));
104 /* Copy CSTR (a zero-terminated string) to the end of buffer. The
105 terminating zero is appended too. */
106 void append_cstr0 (const char *cstr
)
108 const size_t size
= strlen (cstr
) + 1;
109 std::copy (cstr
, cstr
+ size
, grow (size
));
112 /* Store INPUT as ULEB128 to the end of buffer. */
113 void append_unsigned_leb128 (ULONGEST input
)
117 gdb_byte output
= input
& 0x7f;
121 append_data (output
);
127 /* Accept a host-format integer in VAL and append it to the buffer
128 as a target-format integer which is LEN bytes long. */
129 void append_uint (size_t len
, bfd_endian byte_order
, ULONGEST val
)
131 ::store_unsigned_integer (grow (len
), len
, byte_order
, val
);
134 /* Return the size of the buffer. */
137 return m_vec
.size ();
140 /* Return true iff the buffer is empty. */
143 return m_vec
.empty ();
146 /* Write the buffer to FILE. */
147 void file_write (FILE *file
) const
149 ::file_write (file
, m_vec
);
153 /* Grow SIZE bytes at the end of the buffer. Returns a pointer to
154 the start of the new block. */
155 gdb_byte
*grow (size_t size
)
157 m_vec
.resize (m_vec
.size () + size
);
158 return &*(m_vec
.end () - size
);
161 gdb::byte_vector m_vec
;
164 /* An entry in the symbol table. */
165 struct symtab_index_entry
167 /* The name of the symbol. */
169 /* The offset of the name in the constant pool. */
170 offset_type index_offset
;
171 /* A sorted vector of the indices of all the CUs that hold an object
173 std::vector
<offset_type
> cu_indices
;
176 /* The symbol table. This is a power-of-2-sized hash table. */
184 offset_type n_elements
= 0;
185 std::vector
<symtab_index_entry
> data
;
188 /* Find a slot in SYMTAB for the symbol NAME. Returns a reference to
191 Function is used only during write_hash_table so no index format backward
192 compatibility is needed. */
194 static symtab_index_entry
&
195 find_slot (struct mapped_symtab
*symtab
, const char *name
)
197 offset_type index
, step
, hash
= mapped_index_string_hash (INT_MAX
, name
);
199 index
= hash
& (symtab
->data
.size () - 1);
200 step
= ((hash
* 17) & (symtab
->data
.size () - 1)) | 1;
204 if (symtab
->data
[index
].name
== NULL
205 || strcmp (name
, symtab
->data
[index
].name
) == 0)
206 return symtab
->data
[index
];
207 index
= (index
+ step
) & (symtab
->data
.size () - 1);
211 /* Expand SYMTAB's hash table. */
214 hash_expand (struct mapped_symtab
*symtab
)
216 auto old_entries
= std::move (symtab
->data
);
218 symtab
->data
.clear ();
219 symtab
->data
.resize (old_entries
.size () * 2);
221 for (auto &it
: old_entries
)
224 auto &ref
= find_slot (symtab
, it
.name
);
225 ref
= std::move (it
);
229 /* Add an entry to SYMTAB. NAME is the name of the symbol.
230 CU_INDEX is the index of the CU in which the symbol appears.
231 IS_STATIC is one if the symbol is static, otherwise zero (global). */
234 add_index_entry (struct mapped_symtab
*symtab
, const char *name
,
235 int is_static
, gdb_index_symbol_kind kind
,
236 offset_type cu_index
)
238 offset_type cu_index_and_attrs
;
240 ++symtab
->n_elements
;
241 if (4 * symtab
->n_elements
/ 3 >= symtab
->data
.size ())
242 hash_expand (symtab
);
244 symtab_index_entry
&slot
= find_slot (symtab
, name
);
245 if (slot
.name
== NULL
)
248 /* index_offset is set later. */
251 cu_index_and_attrs
= 0;
252 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs
, cu_index
);
253 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs
, is_static
);
254 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs
, kind
);
256 /* We don't want to record an index value twice as we want to avoid the
258 We process all global symbols and then all static symbols
259 (which would allow us to avoid the duplication by only having to check
260 the last entry pushed), but a symbol could have multiple kinds in one CU.
261 To keep things simple we don't worry about the duplication here and
262 sort and uniquify the list after we've processed all symbols. */
263 slot
.cu_indices
.push_back (cu_index_and_attrs
);
266 /* Sort and remove duplicates of all symbols' cu_indices lists. */
269 uniquify_cu_indices (struct mapped_symtab
*symtab
)
271 for (auto &entry
: symtab
->data
)
273 if (entry
.name
!= NULL
&& !entry
.cu_indices
.empty ())
275 auto &cu_indices
= entry
.cu_indices
;
276 std::sort (cu_indices
.begin (), cu_indices
.end ());
277 auto from
= std::unique (cu_indices
.begin (), cu_indices
.end ());
278 cu_indices
.erase (from
, cu_indices
.end ());
283 /* A form of 'const char *' suitable for container keys. Only the
284 pointer is stored. The strings themselves are compared, not the
289 c_str_view (const char *cstr
)
293 bool operator== (const c_str_view
&other
) const
295 return strcmp (m_cstr
, other
.m_cstr
) == 0;
298 /* Return the underlying C string. Note, the returned string is
299 only a reference with lifetime of this object. */
300 const char *c_str () const
306 friend class c_str_view_hasher
;
307 const char *const m_cstr
;
310 /* A std::unordered_map::hasher for c_str_view that uses the right
311 hash function for strings in a mapped index. */
312 class c_str_view_hasher
315 size_t operator () (const c_str_view
&x
) const
317 return mapped_index_string_hash (INT_MAX
, x
.m_cstr
);
321 /* A std::unordered_map::hasher for std::vector<>. */
326 size_t operator () (const std::vector
<T
> &key
) const
328 return iterative_hash (key
.data (),
329 sizeof (key
.front ()) * key
.size (), 0);
333 /* Write the mapped hash table SYMTAB to the data buffer OUTPUT, with
334 constant pool entries going into the data buffer CPOOL. */
337 write_hash_table (mapped_symtab
*symtab
, data_buf
&output
, data_buf
&cpool
)
340 /* Elements are sorted vectors of the indices of all the CUs that
341 hold an object of this name. */
342 std::unordered_map
<std::vector
<offset_type
>, offset_type
,
343 vector_hasher
<offset_type
>>
346 /* We add all the index vectors to the constant pool first, to
347 ensure alignment is ok. */
348 for (symtab_index_entry
&entry
: symtab
->data
)
350 if (entry
.name
== NULL
)
352 gdb_assert (entry
.index_offset
== 0);
354 /* Finding before inserting is faster than always trying to
355 insert, because inserting always allocates a node, does the
356 lookup, and then destroys the new node if another node
357 already had the same key. C++17 try_emplace will avoid
360 = symbol_hash_table
.find (entry
.cu_indices
);
361 if (found
!= symbol_hash_table
.end ())
363 entry
.index_offset
= found
->second
;
367 symbol_hash_table
.emplace (entry
.cu_indices
, cpool
.size ());
368 entry
.index_offset
= cpool
.size ();
369 cpool
.append_data (MAYBE_SWAP (entry
.cu_indices
.size ()));
370 for (const auto index
: entry
.cu_indices
)
371 cpool
.append_data (MAYBE_SWAP (index
));
375 /* Now write out the hash table. */
376 std::unordered_map
<c_str_view
, offset_type
, c_str_view_hasher
> str_table
;
377 for (const auto &entry
: symtab
->data
)
379 offset_type str_off
, vec_off
;
381 if (entry
.name
!= NULL
)
383 const auto insertpair
= str_table
.emplace (entry
.name
, cpool
.size ());
384 if (insertpair
.second
)
385 cpool
.append_cstr0 (entry
.name
);
386 str_off
= insertpair
.first
->second
;
387 vec_off
= entry
.index_offset
;
391 /* While 0 is a valid constant pool index, it is not valid
392 to have 0 for both offsets. */
397 output
.append_data (MAYBE_SWAP (str_off
));
398 output
.append_data (MAYBE_SWAP (vec_off
));
402 typedef std::unordered_map
<dwarf2_psymtab
*, unsigned int> psym_index_map
;
404 /* Helper struct for building the address table. */
405 struct addrmap_index_data
407 addrmap_index_data (data_buf
&addr_vec_
, psym_index_map
&cu_index_htab_
)
408 : addr_vec (addr_vec_
), cu_index_htab (cu_index_htab_
)
411 struct objfile
*objfile
;
413 psym_index_map
&cu_index_htab
;
415 /* Non-zero if the previous_* fields are valid.
416 We can't write an entry until we see the next entry (since it is only then
417 that we know the end of the entry). */
419 /* Index of the CU in the table of all CUs in the index file. */
420 unsigned int previous_cu_index
;
421 /* Start address of the CU. */
422 CORE_ADDR previous_cu_start
;
425 /* Write an address entry to ADDR_VEC. */
428 add_address_entry (struct objfile
*objfile
, data_buf
&addr_vec
,
429 CORE_ADDR start
, CORE_ADDR end
, unsigned int cu_index
)
431 addr_vec
.append_uint (8, BFD_ENDIAN_LITTLE
, start
);
432 addr_vec
.append_uint (8, BFD_ENDIAN_LITTLE
, end
);
433 addr_vec
.append_data (MAYBE_SWAP (cu_index
));
436 /* Worker function for traversing an addrmap to build the address table. */
439 add_address_entry_worker (void *datap
, CORE_ADDR start_addr
, void *obj
)
441 struct addrmap_index_data
*data
= (struct addrmap_index_data
*) datap
;
442 dwarf2_psymtab
*pst
= (dwarf2_psymtab
*) obj
;
444 if (data
->previous_valid
)
445 add_address_entry (data
->objfile
, data
->addr_vec
,
446 data
->previous_cu_start
, start_addr
,
447 data
->previous_cu_index
);
449 data
->previous_cu_start
= start_addr
;
452 const auto it
= data
->cu_index_htab
.find (pst
);
453 gdb_assert (it
!= data
->cu_index_htab
.cend ());
454 data
->previous_cu_index
= it
->second
;
455 data
->previous_valid
= 1;
458 data
->previous_valid
= 0;
463 /* Write OBJFILE's address map to ADDR_VEC.
464 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
465 in the index file. */
468 write_address_map (struct objfile
*objfile
, data_buf
&addr_vec
,
469 psym_index_map
&cu_index_htab
)
471 struct addrmap_index_data
addrmap_index_data (addr_vec
, cu_index_htab
);
473 /* When writing the address table, we have to cope with the fact that
474 the addrmap iterator only provides the start of a region; we have to
475 wait until the next invocation to get the start of the next region. */
477 addrmap_index_data
.objfile
= objfile
;
478 addrmap_index_data
.previous_valid
= 0;
480 addrmap_foreach (objfile
->partial_symtabs
->psymtabs_addrmap
,
481 add_address_entry_worker
, &addrmap_index_data
);
483 /* It's highly unlikely the last entry (end address = 0xff...ff)
484 is valid, but we should still handle it.
485 The end address is recorded as the start of the next region, but that
486 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
488 if (addrmap_index_data
.previous_valid
)
489 add_address_entry (objfile
, addr_vec
,
490 addrmap_index_data
.previous_cu_start
, (CORE_ADDR
) -1,
491 addrmap_index_data
.previous_cu_index
);
494 /* Return the symbol kind of PSYM. */
496 static gdb_index_symbol_kind
497 symbol_kind (struct partial_symbol
*psym
)
499 domain_enum domain
= psym
->domain
;
500 enum address_class aclass
= psym
->aclass
;
508 return GDB_INDEX_SYMBOL_KIND_FUNCTION
;
510 return GDB_INDEX_SYMBOL_KIND_TYPE
;
512 case LOC_CONST_BYTES
:
513 case LOC_OPTIMIZED_OUT
:
515 return GDB_INDEX_SYMBOL_KIND_VARIABLE
;
517 /* Note: It's currently impossible to recognize psyms as enum values
518 short of reading the type info. For now punt. */
519 return GDB_INDEX_SYMBOL_KIND_VARIABLE
;
521 /* There are other LOC_FOO values that one might want to classify
522 as variables, but dwarf2read.c doesn't currently use them. */
523 return GDB_INDEX_SYMBOL_KIND_OTHER
;
526 return GDB_INDEX_SYMBOL_KIND_TYPE
;
528 return GDB_INDEX_SYMBOL_KIND_OTHER
;
532 /* Add a list of partial symbols to SYMTAB. */
535 write_psymbols (struct mapped_symtab
*symtab
,
536 std::unordered_set
<partial_symbol
*> &psyms_seen
,
537 struct partial_symbol
**psymp
,
539 offset_type cu_index
,
542 for (; count
-- > 0; ++psymp
)
544 struct partial_symbol
*psym
= *psymp
;
546 if (psym
->ginfo
.language () == language_ada
)
547 error (_("Ada is not currently supported by the index; "
548 "use the DWARF 5 index instead"));
550 /* Only add a given psymbol once. */
551 if (psyms_seen
.insert (psym
).second
)
553 gdb_index_symbol_kind kind
= symbol_kind (psym
);
555 add_index_entry (symtab
, psym
->ginfo
.search_name (),
556 is_static
, kind
, cu_index
);
561 /* A helper struct used when iterating over debug_types. */
562 struct signatured_type_index_data
564 signatured_type_index_data (data_buf
&types_list_
,
565 std::unordered_set
<partial_symbol
*> &psyms_seen_
)
566 : types_list (types_list_
), psyms_seen (psyms_seen_
)
569 struct objfile
*objfile
;
570 struct mapped_symtab
*symtab
;
571 data_buf
&types_list
;
572 std::unordered_set
<partial_symbol
*> &psyms_seen
;
576 /* A helper function that writes a single signatured_type to an
580 write_one_signatured_type (void **slot
, void *d
)
582 struct signatured_type_index_data
*info
583 = (struct signatured_type_index_data
*) d
;
584 struct signatured_type
*entry
= (struct signatured_type
*) *slot
;
585 dwarf2_psymtab
*psymtab
= entry
->per_cu
.v
.psymtab
;
587 write_psymbols (info
->symtab
,
589 (info
->objfile
->partial_symtabs
->global_psymbols
.data ()
590 + psymtab
->globals_offset
),
591 psymtab
->n_global_syms
, info
->cu_index
,
593 write_psymbols (info
->symtab
,
595 (info
->objfile
->partial_symtabs
->static_psymbols
.data ()
596 + psymtab
->statics_offset
),
597 psymtab
->n_static_syms
, info
->cu_index
,
600 info
->types_list
.append_uint (8, BFD_ENDIAN_LITTLE
,
601 to_underlying (entry
->per_cu
.sect_off
));
602 info
->types_list
.append_uint (8, BFD_ENDIAN_LITTLE
,
603 to_underlying (entry
->type_offset_in_tu
));
604 info
->types_list
.append_uint (8, BFD_ENDIAN_LITTLE
, entry
->signature
);
611 /* Recurse into all "included" dependencies and count their symbols as
612 if they appeared in this psymtab. */
615 recursively_count_psymbols (dwarf2_psymtab
*psymtab
,
618 for (int i
= 0; i
< psymtab
->number_of_dependencies
; ++i
)
619 if (psymtab
->dependencies
[i
]->user
!= NULL
)
620 recursively_count_psymbols ((dwarf2_psymtab
*) psymtab
->dependencies
[i
],
623 psyms_seen
+= psymtab
->n_global_syms
;
624 psyms_seen
+= psymtab
->n_static_syms
;
627 /* Recurse into all "included" dependencies and write their symbols as
628 if they appeared in this psymtab. */
631 recursively_write_psymbols (struct objfile
*objfile
,
632 dwarf2_psymtab
*psymtab
,
633 struct mapped_symtab
*symtab
,
634 std::unordered_set
<partial_symbol
*> &psyms_seen
,
635 offset_type cu_index
)
639 for (i
= 0; i
< psymtab
->number_of_dependencies
; ++i
)
640 if (psymtab
->dependencies
[i
]->user
!= NULL
)
641 recursively_write_psymbols (objfile
,
642 (dwarf2_psymtab
*) psymtab
->dependencies
[i
],
643 symtab
, psyms_seen
, cu_index
);
645 write_psymbols (symtab
,
647 (objfile
->partial_symtabs
->global_psymbols
.data ()
648 + psymtab
->globals_offset
),
649 psymtab
->n_global_syms
, cu_index
,
651 write_psymbols (symtab
,
653 (objfile
->partial_symtabs
->static_psymbols
.data ()
654 + psymtab
->statics_offset
),
655 psymtab
->n_static_syms
, cu_index
,
659 /* DWARF-5 .debug_names builder. */
663 debug_names (struct dwarf2_per_objfile
*dwarf2_per_objfile
, bool is_dwarf64
,
664 bfd_endian dwarf5_byte_order
)
665 : m_dwarf5_byte_order (dwarf5_byte_order
),
666 m_dwarf32 (dwarf5_byte_order
),
667 m_dwarf64 (dwarf5_byte_order
),
669 ? static_cast<dwarf
&> (m_dwarf64
)
670 : static_cast<dwarf
&> (m_dwarf32
)),
671 m_name_table_string_offs (m_dwarf
.name_table_string_offs
),
672 m_name_table_entry_offs (m_dwarf
.name_table_entry_offs
),
673 m_debugstrlookup (dwarf2_per_objfile
)
676 int dwarf5_offset_size () const
678 const bool dwarf5_is_dwarf64
= &m_dwarf
== &m_dwarf64
;
679 return dwarf5_is_dwarf64
? 8 : 4;
682 /* Is this symbol from DW_TAG_compile_unit or DW_TAG_type_unit? */
683 enum class unit_kind
{ cu
, tu
};
685 /* Insert one symbol. */
686 void insert (const partial_symbol
*psym
, int cu_index
, bool is_static
,
689 const int dwarf_tag
= psymbol_tag (psym
);
692 const char *name
= psym
->ginfo
.search_name ();
694 if (psym
->ginfo
.language () == language_ada
)
696 /* We want to ensure that the Ada main function's name appears
697 verbatim in the index. However, this name will be of the
698 form "_ada_mumble", and will be rewritten by ada_decode.
699 So, recognize it specially here and add it to the index by
701 if (strcmp (main_name (), name
) == 0)
703 const auto insertpair
704 = m_name_to_value_set
.emplace (c_str_view (name
),
705 std::set
<symbol_value
> ());
706 std::set
<symbol_value
> &value_set
= insertpair
.first
->second
;
707 value_set
.emplace (symbol_value (dwarf_tag
, cu_index
, is_static
,
711 /* In order for the index to work when read back into gdb, it
712 has to supply a funny form of the name: it should be the
713 encoded name, with any suffixes stripped. Using the
714 ordinary encoded name will not work properly with the
715 searching logic in find_name_components_bounds; nor will
716 using the decoded name. Furthermore, an Ada "verbatim"
717 name (of the form "<MumBle>") must be entered without the
718 angle brackets. Note that the current index is unusual,
719 see PR symtab/24820 for details. */
720 std::string decoded
= ada_decode (name
);
721 if (decoded
[0] == '<')
722 name
= (char *) obstack_copy0 (&m_string_obstack
,
723 decoded
.c_str () + 1,
724 decoded
.length () - 2);
726 name
= obstack_strdup (&m_string_obstack
,
727 ada_encode (decoded
.c_str ()));
730 const auto insertpair
731 = m_name_to_value_set
.emplace (c_str_view (name
),
732 std::set
<symbol_value
> ());
733 std::set
<symbol_value
> &value_set
= insertpair
.first
->second
;
734 value_set
.emplace (symbol_value (dwarf_tag
, cu_index
, is_static
, kind
));
737 /* Build all the tables. All symbols must be already inserted.
738 This function does not call file_write, caller has to do it
742 /* Verify the build method has not be called twice. */
743 gdb_assert (m_abbrev_table
.empty ());
744 const size_t name_count
= m_name_to_value_set
.size ();
745 m_bucket_table
.resize
746 (std::pow (2, std::ceil (std::log2 (name_count
* 4 / 3))));
747 m_hash_table
.reserve (name_count
);
748 m_name_table_string_offs
.reserve (name_count
);
749 m_name_table_entry_offs
.reserve (name_count
);
751 /* Map each hash of symbol to its name and value. */
755 decltype (m_name_to_value_set
)::const_iterator it
;
757 std::vector
<std::forward_list
<hash_it_pair
>> bucket_hash
;
758 bucket_hash
.resize (m_bucket_table
.size ());
759 for (decltype (m_name_to_value_set
)::const_iterator it
760 = m_name_to_value_set
.cbegin ();
761 it
!= m_name_to_value_set
.cend ();
764 const char *const name
= it
->first
.c_str ();
765 const uint32_t hash
= dwarf5_djb_hash (name
);
766 hash_it_pair hashitpair
;
767 hashitpair
.hash
= hash
;
769 auto &slot
= bucket_hash
[hash
% bucket_hash
.size()];
770 slot
.push_front (std::move (hashitpair
));
772 for (size_t bucket_ix
= 0; bucket_ix
< bucket_hash
.size (); ++bucket_ix
)
774 const std::forward_list
<hash_it_pair
> &hashitlist
775 = bucket_hash
[bucket_ix
];
776 if (hashitlist
.empty ())
778 uint32_t &bucket_slot
= m_bucket_table
[bucket_ix
];
779 /* The hashes array is indexed starting at 1. */
780 store_unsigned_integer (reinterpret_cast<gdb_byte
*> (&bucket_slot
),
781 sizeof (bucket_slot
), m_dwarf5_byte_order
,
782 m_hash_table
.size () + 1);
783 for (const hash_it_pair
&hashitpair
: hashitlist
)
785 m_hash_table
.push_back (0);
786 store_unsigned_integer (reinterpret_cast<gdb_byte
*>
787 (&m_hash_table
.back ()),
788 sizeof (m_hash_table
.back ()),
789 m_dwarf5_byte_order
, hashitpair
.hash
);
790 const c_str_view
&name
= hashitpair
.it
->first
;
791 const std::set
<symbol_value
> &value_set
= hashitpair
.it
->second
;
792 m_name_table_string_offs
.push_back_reorder
793 (m_debugstrlookup
.lookup (name
.c_str ()));
794 m_name_table_entry_offs
.push_back_reorder (m_entry_pool
.size ());
795 gdb_assert (!value_set
.empty ());
796 for (const symbol_value
&value
: value_set
)
798 int &idx
= m_indexkey_to_idx
[index_key (value
.dwarf_tag
,
804 m_abbrev_table
.append_unsigned_leb128 (idx
);
805 m_abbrev_table
.append_unsigned_leb128 (value
.dwarf_tag
);
806 m_abbrev_table
.append_unsigned_leb128
807 (value
.kind
== unit_kind::cu
? DW_IDX_compile_unit
809 m_abbrev_table
.append_unsigned_leb128 (DW_FORM_udata
);
810 m_abbrev_table
.append_unsigned_leb128 (value
.is_static
811 ? DW_IDX_GNU_internal
812 : DW_IDX_GNU_external
);
813 m_abbrev_table
.append_unsigned_leb128 (DW_FORM_flag_present
);
815 /* Terminate attributes list. */
816 m_abbrev_table
.append_unsigned_leb128 (0);
817 m_abbrev_table
.append_unsigned_leb128 (0);
820 m_entry_pool
.append_unsigned_leb128 (idx
);
821 m_entry_pool
.append_unsigned_leb128 (value
.cu_index
);
824 /* Terminate the list of CUs. */
825 m_entry_pool
.append_unsigned_leb128 (0);
828 gdb_assert (m_hash_table
.size () == name_count
);
830 /* Terminate tags list. */
831 m_abbrev_table
.append_unsigned_leb128 (0);
834 /* Return .debug_names bucket count. This must be called only after
835 calling the build method. */
836 uint32_t bucket_count () const
838 /* Verify the build method has been already called. */
839 gdb_assert (!m_abbrev_table
.empty ());
840 const uint32_t retval
= m_bucket_table
.size ();
842 /* Check for overflow. */
843 gdb_assert (retval
== m_bucket_table
.size ());
847 /* Return .debug_names names count. This must be called only after
848 calling the build method. */
849 uint32_t name_count () const
851 /* Verify the build method has been already called. */
852 gdb_assert (!m_abbrev_table
.empty ());
853 const uint32_t retval
= m_hash_table
.size ();
855 /* Check for overflow. */
856 gdb_assert (retval
== m_hash_table
.size ());
860 /* Return number of bytes of .debug_names abbreviation table. This
861 must be called only after calling the build method. */
862 uint32_t abbrev_table_bytes () const
864 gdb_assert (!m_abbrev_table
.empty ());
865 return m_abbrev_table
.size ();
868 /* Recurse into all "included" dependencies and store their symbols
869 as if they appeared in this psymtab. */
870 void recursively_write_psymbols
871 (struct objfile
*objfile
,
872 dwarf2_psymtab
*psymtab
,
873 std::unordered_set
<partial_symbol
*> &psyms_seen
,
876 for (int i
= 0; i
< psymtab
->number_of_dependencies
; ++i
)
877 if (psymtab
->dependencies
[i
]->user
!= NULL
)
878 recursively_write_psymbols
879 (objfile
, (dwarf2_psymtab
*) psymtab
->dependencies
[i
], psyms_seen
, cu_index
);
881 write_psymbols (psyms_seen
,
882 (objfile
->partial_symtabs
->global_psymbols
.data ()
883 + psymtab
->globals_offset
),
884 psymtab
->n_global_syms
, cu_index
, false, unit_kind::cu
);
885 write_psymbols (psyms_seen
,
886 (objfile
->partial_symtabs
->static_psymbols
.data ()
887 + psymtab
->statics_offset
),
888 psymtab
->n_static_syms
, cu_index
, true, unit_kind::cu
);
891 /* Return number of bytes the .debug_names section will have. This
892 must be called only after calling the build method. */
893 size_t bytes () const
895 /* Verify the build method has been already called. */
896 gdb_assert (!m_abbrev_table
.empty ());
897 size_t expected_bytes
= 0;
898 expected_bytes
+= m_bucket_table
.size () * sizeof (m_bucket_table
[0]);
899 expected_bytes
+= m_hash_table
.size () * sizeof (m_hash_table
[0]);
900 expected_bytes
+= m_name_table_string_offs
.bytes ();
901 expected_bytes
+= m_name_table_entry_offs
.bytes ();
902 expected_bytes
+= m_abbrev_table
.size ();
903 expected_bytes
+= m_entry_pool
.size ();
904 return expected_bytes
;
907 /* Write .debug_names to FILE_NAMES and .debug_str addition to
908 FILE_STR. This must be called only after calling the build
910 void file_write (FILE *file_names
, FILE *file_str
) const
912 /* Verify the build method has been already called. */
913 gdb_assert (!m_abbrev_table
.empty ());
914 ::file_write (file_names
, m_bucket_table
);
915 ::file_write (file_names
, m_hash_table
);
916 m_name_table_string_offs
.file_write (file_names
);
917 m_name_table_entry_offs
.file_write (file_names
);
918 m_abbrev_table
.file_write (file_names
);
919 m_entry_pool
.file_write (file_names
);
920 m_debugstrlookup
.file_write (file_str
);
923 /* A helper user data for write_one_signatured_type. */
924 class write_one_signatured_type_data
927 write_one_signatured_type_data (debug_names
&nametable_
,
928 signatured_type_index_data
&&info_
)
929 : nametable (nametable_
), info (std::move (info_
))
931 debug_names
&nametable
;
932 struct signatured_type_index_data info
;
935 /* A helper function to pass write_one_signatured_type to
936 htab_traverse_noresize. */
938 write_one_signatured_type (void **slot
, void *d
)
940 write_one_signatured_type_data
*data
= (write_one_signatured_type_data
*) d
;
941 struct signatured_type_index_data
*info
= &data
->info
;
942 struct signatured_type
*entry
= (struct signatured_type
*) *slot
;
944 data
->nametable
.write_one_signatured_type (entry
, info
);
951 /* Storage for symbol names mapping them to their .debug_str section
953 class debug_str_lookup
957 /* Object constructor to be called for current DWARF2_PER_OBJFILE.
958 All .debug_str section strings are automatically stored. */
959 debug_str_lookup (struct dwarf2_per_objfile
*dwarf2_per_objfile
)
960 : m_abfd (dwarf2_per_objfile
->objfile
->obfd
),
961 m_dwarf2_per_objfile (dwarf2_per_objfile
)
963 dwarf2_read_section (dwarf2_per_objfile
->objfile
,
964 &dwarf2_per_objfile
->str
);
965 if (dwarf2_per_objfile
->str
.buffer
== NULL
)
967 for (const gdb_byte
*data
= dwarf2_per_objfile
->str
.buffer
;
968 data
< (dwarf2_per_objfile
->str
.buffer
969 + dwarf2_per_objfile
->str
.size
);)
971 const char *const s
= reinterpret_cast<const char *> (data
);
972 const auto insertpair
973 = m_str_table
.emplace (c_str_view (s
),
974 data
- dwarf2_per_objfile
->str
.buffer
);
975 if (!insertpair
.second
)
976 complaint (_("Duplicate string \"%s\" in "
977 ".debug_str section [in module %s]"),
978 s
, bfd_get_filename (m_abfd
));
979 data
+= strlen (s
) + 1;
983 /* Return offset of symbol name S in the .debug_str section. Add
984 such symbol to the section's end if it does not exist there
986 size_t lookup (const char *s
)
988 const auto it
= m_str_table
.find (c_str_view (s
));
989 if (it
!= m_str_table
.end ())
991 const size_t offset
= (m_dwarf2_per_objfile
->str
.size
992 + m_str_add_buf
.size ());
993 m_str_table
.emplace (c_str_view (s
), offset
);
994 m_str_add_buf
.append_cstr0 (s
);
998 /* Append the end of the .debug_str section to FILE. */
999 void file_write (FILE *file
) const
1001 m_str_add_buf
.file_write (file
);
1005 std::unordered_map
<c_str_view
, size_t, c_str_view_hasher
> m_str_table
;
1007 struct dwarf2_per_objfile
*m_dwarf2_per_objfile
;
1009 /* Data to add at the end of .debug_str for new needed symbol names. */
1010 data_buf m_str_add_buf
;
1013 /* Container to map used DWARF tags to their .debug_names abbreviation
1018 index_key (int dwarf_tag_
, bool is_static_
, unit_kind kind_
)
1019 : dwarf_tag (dwarf_tag_
), is_static (is_static_
), kind (kind_
)
1024 operator== (const index_key
&other
) const
1026 return (dwarf_tag
== other
.dwarf_tag
&& is_static
== other
.is_static
1027 && kind
== other
.kind
);
1030 const int dwarf_tag
;
1031 const bool is_static
;
1032 const unit_kind kind
;
1035 /* Provide std::unordered_map::hasher for index_key. */
1036 class index_key_hasher
1040 operator () (const index_key
&key
) const
1042 return (std::hash
<int>() (key
.dwarf_tag
) << 1) | key
.is_static
;
1046 /* Parameters of one symbol entry. */
1050 const int dwarf_tag
, cu_index
;
1051 const bool is_static
;
1052 const unit_kind kind
;
1054 symbol_value (int dwarf_tag_
, int cu_index_
, bool is_static_
,
1056 : dwarf_tag (dwarf_tag_
), cu_index (cu_index_
), is_static (is_static_
),
1061 operator< (const symbol_value
&other
) const
1081 /* Abstract base class to unify DWARF-32 and DWARF-64 name table
1086 const bfd_endian dwarf5_byte_order
;
1088 explicit offset_vec (bfd_endian dwarf5_byte_order_
)
1089 : dwarf5_byte_order (dwarf5_byte_order_
)
1092 /* Call std::vector::reserve for NELEM elements. */
1093 virtual void reserve (size_t nelem
) = 0;
1095 /* Call std::vector::push_back with store_unsigned_integer byte
1096 reordering for ELEM. */
1097 virtual void push_back_reorder (size_t elem
) = 0;
1099 /* Return expected output size in bytes. */
1100 virtual size_t bytes () const = 0;
1102 /* Write name table to FILE. */
1103 virtual void file_write (FILE *file
) const = 0;
1106 /* Template to unify DWARF-32 and DWARF-64 output. */
1107 template<typename OffsetSize
>
1108 class offset_vec_tmpl
: public offset_vec
1111 explicit offset_vec_tmpl (bfd_endian dwarf5_byte_order_
)
1112 : offset_vec (dwarf5_byte_order_
)
1115 /* Implement offset_vec::reserve. */
1116 void reserve (size_t nelem
) override
1118 m_vec
.reserve (nelem
);
1121 /* Implement offset_vec::push_back_reorder. */
1122 void push_back_reorder (size_t elem
) override
1124 m_vec
.push_back (elem
);
1125 /* Check for overflow. */
1126 gdb_assert (m_vec
.back () == elem
);
1127 store_unsigned_integer (reinterpret_cast<gdb_byte
*> (&m_vec
.back ()),
1128 sizeof (m_vec
.back ()), dwarf5_byte_order
, elem
);
1131 /* Implement offset_vec::bytes. */
1132 size_t bytes () const override
1134 return m_vec
.size () * sizeof (m_vec
[0]);
1137 /* Implement offset_vec::file_write. */
1138 void file_write (FILE *file
) const override
1140 ::file_write (file
, m_vec
);
1144 std::vector
<OffsetSize
> m_vec
;
1147 /* Base class to unify DWARF-32 and DWARF-64 .debug_names output
1148 respecting name table width. */
1152 offset_vec
&name_table_string_offs
, &name_table_entry_offs
;
1154 dwarf (offset_vec
&name_table_string_offs_
,
1155 offset_vec
&name_table_entry_offs_
)
1156 : name_table_string_offs (name_table_string_offs_
),
1157 name_table_entry_offs (name_table_entry_offs_
)
1162 /* Template to unify DWARF-32 and DWARF-64 .debug_names output
1163 respecting name table width. */
1164 template<typename OffsetSize
>
1165 class dwarf_tmpl
: public dwarf
1168 explicit dwarf_tmpl (bfd_endian dwarf5_byte_order_
)
1169 : dwarf (m_name_table_string_offs
, m_name_table_entry_offs
),
1170 m_name_table_string_offs (dwarf5_byte_order_
),
1171 m_name_table_entry_offs (dwarf5_byte_order_
)
1175 offset_vec_tmpl
<OffsetSize
> m_name_table_string_offs
;
1176 offset_vec_tmpl
<OffsetSize
> m_name_table_entry_offs
;
1179 /* Try to reconstruct original DWARF tag for given partial_symbol.
1180 This function is not DWARF-5 compliant but it is sufficient for
1181 GDB as a DWARF-5 index consumer. */
1182 static int psymbol_tag (const struct partial_symbol
*psym
)
1184 domain_enum domain
= psym
->domain
;
1185 enum address_class aclass
= psym
->aclass
;
1193 return DW_TAG_subprogram
;
1195 return DW_TAG_typedef
;
1197 case LOC_CONST_BYTES
:
1198 case LOC_OPTIMIZED_OUT
:
1200 return DW_TAG_variable
;
1202 /* Note: It's currently impossible to recognize psyms as enum values
1203 short of reading the type info. For now punt. */
1204 return DW_TAG_variable
;
1206 /* There are other LOC_FOO values that one might want to classify
1207 as variables, but dwarf2read.c doesn't currently use them. */
1208 return DW_TAG_variable
;
1211 return DW_TAG_structure_type
;
1217 /* Call insert for all partial symbols and mark them in PSYMS_SEEN. */
1218 void write_psymbols (std::unordered_set
<partial_symbol
*> &psyms_seen
,
1219 struct partial_symbol
**psymp
, int count
, int cu_index
,
1220 bool is_static
, unit_kind kind
)
1222 for (; count
-- > 0; ++psymp
)
1224 struct partial_symbol
*psym
= *psymp
;
1226 /* Only add a given psymbol once. */
1227 if (psyms_seen
.insert (psym
).second
)
1228 insert (psym
, cu_index
, is_static
, kind
);
1232 /* A helper function that writes a single signatured_type
1233 to a debug_names. */
1235 write_one_signatured_type (struct signatured_type
*entry
,
1236 struct signatured_type_index_data
*info
)
1238 dwarf2_psymtab
*psymtab
= entry
->per_cu
.v
.psymtab
;
1240 write_psymbols (info
->psyms_seen
,
1241 (info
->objfile
->partial_symtabs
->global_psymbols
.data ()
1242 + psymtab
->globals_offset
),
1243 psymtab
->n_global_syms
, info
->cu_index
, false,
1245 write_psymbols (info
->psyms_seen
,
1246 (info
->objfile
->partial_symtabs
->static_psymbols
.data ()
1247 + psymtab
->statics_offset
),
1248 psymtab
->n_static_syms
, info
->cu_index
, true,
1251 info
->types_list
.append_uint (dwarf5_offset_size (), m_dwarf5_byte_order
,
1252 to_underlying (entry
->per_cu
.sect_off
));
1257 /* Store value of each symbol. */
1258 std::unordered_map
<c_str_view
, std::set
<symbol_value
>, c_str_view_hasher
>
1259 m_name_to_value_set
;
1261 /* Tables of DWARF-5 .debug_names. They are in object file byte
1263 std::vector
<uint32_t> m_bucket_table
;
1264 std::vector
<uint32_t> m_hash_table
;
1266 const bfd_endian m_dwarf5_byte_order
;
1267 dwarf_tmpl
<uint32_t> m_dwarf32
;
1268 dwarf_tmpl
<uint64_t> m_dwarf64
;
1270 offset_vec
&m_name_table_string_offs
, &m_name_table_entry_offs
;
1271 debug_str_lookup m_debugstrlookup
;
1273 /* Map each used .debug_names abbreviation tag parameter to its
1275 std::unordered_map
<index_key
, int, index_key_hasher
> m_indexkey_to_idx
;
1277 /* Next unused .debug_names abbreviation tag for
1278 m_indexkey_to_idx. */
1281 /* .debug_names abbreviation table. */
1282 data_buf m_abbrev_table
;
1284 /* .debug_names entry pool. */
1285 data_buf m_entry_pool
;
1287 /* Temporary storage for Ada names. */
1288 auto_obstack m_string_obstack
;
1291 /* Return iff any of the needed offsets does not fit into 32-bit
1292 .debug_names section. */
1295 check_dwarf64_offsets (struct dwarf2_per_objfile
*dwarf2_per_objfile
)
1297 for (dwarf2_per_cu_data
*per_cu
: dwarf2_per_objfile
->all_comp_units
)
1299 if (to_underlying (per_cu
->sect_off
) >= (static_cast<uint64_t> (1) << 32))
1302 for (const signatured_type
*sigtype
: dwarf2_per_objfile
->all_type_units
)
1304 const dwarf2_per_cu_data
&per_cu
= sigtype
->per_cu
;
1306 if (to_underlying (per_cu
.sect_off
) >= (static_cast<uint64_t> (1) << 32))
1312 /* The psyms_seen set is potentially going to be largish (~40k
1313 elements when indexing a -g3 build of GDB itself). Estimate the
1314 number of elements in order to avoid too many rehashes, which
1315 require rebuilding buckets and thus many trips to
1319 psyms_seen_size (struct dwarf2_per_objfile
*dwarf2_per_objfile
)
1321 size_t psyms_count
= 0;
1322 for (dwarf2_per_cu_data
*per_cu
: dwarf2_per_objfile
->all_comp_units
)
1324 dwarf2_psymtab
*psymtab
= per_cu
->v
.psymtab
;
1326 if (psymtab
!= NULL
&& psymtab
->user
== NULL
)
1327 recursively_count_psymbols (psymtab
, psyms_count
);
1329 /* Generating an index for gdb itself shows a ratio of
1330 TOTAL_SEEN_SYMS/UNIQUE_SYMS or ~5. 4 seems like a good bet. */
1331 return psyms_count
/ 4;
1334 /* Assert that FILE's size is EXPECTED_SIZE. Assumes file's seek
1335 position is at the end of the file. */
1338 assert_file_size (FILE *file
, size_t expected_size
)
1340 const auto file_size
= ftell (file
);
1341 if (file_size
== -1)
1342 perror_with_name (("ftell"));
1343 gdb_assert (file_size
== expected_size
);
1346 /* Write a gdb index file to OUT_FILE from all the sections passed as
1350 write_gdbindex_1 (FILE *out_file
,
1351 const data_buf
&cu_list
,
1352 const data_buf
&types_cu_list
,
1353 const data_buf
&addr_vec
,
1354 const data_buf
&symtab_vec
,
1355 const data_buf
&constant_pool
)
1358 const offset_type size_of_header
= 6 * sizeof (offset_type
);
1359 offset_type total_len
= size_of_header
;
1361 /* The version number. */
1362 contents
.append_data (MAYBE_SWAP (8));
1364 /* The offset of the CU list from the start of the file. */
1365 contents
.append_data (MAYBE_SWAP (total_len
));
1366 total_len
+= cu_list
.size ();
1368 /* The offset of the types CU list from the start of the file. */
1369 contents
.append_data (MAYBE_SWAP (total_len
));
1370 total_len
+= types_cu_list
.size ();
1372 /* The offset of the address table from the start of the file. */
1373 contents
.append_data (MAYBE_SWAP (total_len
));
1374 total_len
+= addr_vec
.size ();
1376 /* The offset of the symbol table from the start of the file. */
1377 contents
.append_data (MAYBE_SWAP (total_len
));
1378 total_len
+= symtab_vec
.size ();
1380 /* The offset of the constant pool from the start of the file. */
1381 contents
.append_data (MAYBE_SWAP (total_len
));
1382 total_len
+= constant_pool
.size ();
1384 gdb_assert (contents
.size () == size_of_header
);
1386 contents
.file_write (out_file
);
1387 cu_list
.file_write (out_file
);
1388 types_cu_list
.file_write (out_file
);
1389 addr_vec
.file_write (out_file
);
1390 symtab_vec
.file_write (out_file
);
1391 constant_pool
.file_write (out_file
);
1393 assert_file_size (out_file
, total_len
);
1396 /* Write contents of a .gdb_index section for OBJFILE into OUT_FILE.
1397 If OBJFILE has an associated dwz file, write contents of a .gdb_index
1398 section for that dwz file into DWZ_OUT_FILE. If OBJFILE does not have an
1399 associated dwz file, DWZ_OUT_FILE must be NULL. */
1402 write_gdbindex (struct dwarf2_per_objfile
*dwarf2_per_objfile
, FILE *out_file
,
1405 struct objfile
*objfile
= dwarf2_per_objfile
->objfile
;
1406 mapped_symtab symtab
;
1407 data_buf objfile_cu_list
;
1408 data_buf dwz_cu_list
;
1410 /* While we're scanning CU's create a table that maps a psymtab pointer
1411 (which is what addrmap records) to its index (which is what is recorded
1412 in the index file). This will later be needed to write the address
1414 psym_index_map cu_index_htab
;
1415 cu_index_htab
.reserve (dwarf2_per_objfile
->all_comp_units
.size ());
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 std::unordered_set
<partial_symbol
*> psyms_seen
1422 (psyms_seen_size (dwarf2_per_objfile
));
1423 for (int i
= 0; i
< dwarf2_per_objfile
->all_comp_units
.size (); ++i
)
1425 struct dwarf2_per_cu_data
*per_cu
1426 = dwarf2_per_objfile
->all_comp_units
[i
];
1427 dwarf2_psymtab
*psymtab
= per_cu
->v
.psymtab
;
1429 /* CU of a shared file from 'dwz -m' may be unused by this main file.
1430 It may be referenced from a local scope but in such case it does not
1431 need to be present in .gdb_index. */
1432 if (psymtab
== NULL
)
1435 if (psymtab
->user
== NULL
)
1436 recursively_write_psymbols (objfile
, psymtab
, &symtab
,
1439 const auto insertpair
= cu_index_htab
.emplace (psymtab
, i
);
1440 gdb_assert (insertpair
.second
);
1442 /* The all_comp_units list contains CUs read from the objfile as well as
1443 from the eventual dwz file. We need to place the entry in the
1444 corresponding index. */
1445 data_buf
&cu_list
= per_cu
->is_dwz
? dwz_cu_list
: objfile_cu_list
;
1446 cu_list
.append_uint (8, BFD_ENDIAN_LITTLE
,
1447 to_underlying (per_cu
->sect_off
));
1448 cu_list
.append_uint (8, BFD_ENDIAN_LITTLE
, per_cu
->length
);
1451 /* Dump the address map. */
1453 write_address_map (objfile
, addr_vec
, cu_index_htab
);
1455 /* Write out the .debug_type entries, if any. */
1456 data_buf types_cu_list
;
1457 if (dwarf2_per_objfile
->signatured_types
)
1459 signatured_type_index_data
sig_data (types_cu_list
,
1462 sig_data
.objfile
= objfile
;
1463 sig_data
.symtab
= &symtab
;
1464 sig_data
.cu_index
= dwarf2_per_objfile
->all_comp_units
.size ();
1465 htab_traverse_noresize (dwarf2_per_objfile
->signatured_types
,
1466 write_one_signatured_type
, &sig_data
);
1469 /* Now that we've processed all symbols we can shrink their cu_indices
1471 uniquify_cu_indices (&symtab
);
1473 data_buf symtab_vec
, constant_pool
;
1474 write_hash_table (&symtab
, symtab_vec
, constant_pool
);
1476 write_gdbindex_1(out_file
, objfile_cu_list
, types_cu_list
, addr_vec
,
1477 symtab_vec
, constant_pool
);
1479 if (dwz_out_file
!= NULL
)
1480 write_gdbindex_1 (dwz_out_file
, dwz_cu_list
, {}, {}, {}, {});
1482 gdb_assert (dwz_cu_list
.empty ());
1485 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
1486 static const gdb_byte dwarf5_gdb_augmentation
[] = { 'G', 'D', 'B', 0 };
1488 /* Write a new .debug_names section for OBJFILE into OUT_FILE, write
1489 needed addition to .debug_str section to OUT_FILE_STR. Return how
1490 many bytes were expected to be written into OUT_FILE. */
1493 write_debug_names (struct dwarf2_per_objfile
*dwarf2_per_objfile
,
1494 FILE *out_file
, FILE *out_file_str
)
1496 const bool dwarf5_is_dwarf64
= check_dwarf64_offsets (dwarf2_per_objfile
);
1497 struct objfile
*objfile
= dwarf2_per_objfile
->objfile
;
1498 const enum bfd_endian dwarf5_byte_order
1499 = gdbarch_byte_order (get_objfile_arch (objfile
));
1501 /* The CU list is already sorted, so we don't need to do additional
1502 work here. Also, the debug_types entries do not appear in
1503 all_comp_units, but only in their own hash table. */
1505 debug_names
nametable (dwarf2_per_objfile
, dwarf5_is_dwarf64
,
1507 std::unordered_set
<partial_symbol
*>
1508 psyms_seen (psyms_seen_size (dwarf2_per_objfile
));
1509 for (int i
= 0; i
< dwarf2_per_objfile
->all_comp_units
.size (); ++i
)
1511 const dwarf2_per_cu_data
*per_cu
= dwarf2_per_objfile
->all_comp_units
[i
];
1512 dwarf2_psymtab
*psymtab
= per_cu
->v
.psymtab
;
1514 /* CU of a shared file from 'dwz -m' may be unused by this main
1515 file. It may be referenced from a local scope but in such
1516 case it does not need to be present in .debug_names. */
1517 if (psymtab
== NULL
)
1520 if (psymtab
->user
== NULL
)
1521 nametable
.recursively_write_psymbols (objfile
, psymtab
, psyms_seen
, i
);
1523 cu_list
.append_uint (nametable
.dwarf5_offset_size (), dwarf5_byte_order
,
1524 to_underlying (per_cu
->sect_off
));
1527 /* Write out the .debug_type entries, if any. */
1528 data_buf types_cu_list
;
1529 if (dwarf2_per_objfile
->signatured_types
)
1531 debug_names::write_one_signatured_type_data
sig_data (nametable
,
1532 signatured_type_index_data (types_cu_list
, psyms_seen
));
1534 sig_data
.info
.objfile
= objfile
;
1535 /* It is used only for gdb_index. */
1536 sig_data
.info
.symtab
= nullptr;
1537 sig_data
.info
.cu_index
= 0;
1538 htab_traverse_noresize (dwarf2_per_objfile
->signatured_types
,
1539 debug_names::write_one_signatured_type
,
1545 /* No addr_vec - DWARF-5 uses .debug_aranges generated by GCC. */
1547 const offset_type bytes_of_header
1548 = ((dwarf5_is_dwarf64
? 12 : 4)
1550 + sizeof (dwarf5_gdb_augmentation
));
1551 size_t expected_bytes
= 0;
1552 expected_bytes
+= bytes_of_header
;
1553 expected_bytes
+= cu_list
.size ();
1554 expected_bytes
+= types_cu_list
.size ();
1555 expected_bytes
+= nametable
.bytes ();
1558 if (!dwarf5_is_dwarf64
)
1560 const uint64_t size64
= expected_bytes
- 4;
1561 gdb_assert (size64
< 0xfffffff0);
1562 header
.append_uint (4, dwarf5_byte_order
, size64
);
1566 header
.append_uint (4, dwarf5_byte_order
, 0xffffffff);
1567 header
.append_uint (8, dwarf5_byte_order
, expected_bytes
- 12);
1570 /* The version number. */
1571 header
.append_uint (2, dwarf5_byte_order
, 5);
1574 header
.append_uint (2, dwarf5_byte_order
, 0);
1576 /* comp_unit_count - The number of CUs in the CU list. */
1577 header
.append_uint (4, dwarf5_byte_order
,
1578 dwarf2_per_objfile
->all_comp_units
.size ());
1580 /* local_type_unit_count - The number of TUs in the local TU
1582 header
.append_uint (4, dwarf5_byte_order
,
1583 dwarf2_per_objfile
->all_type_units
.size ());
1585 /* foreign_type_unit_count - The number of TUs in the foreign TU
1587 header
.append_uint (4, dwarf5_byte_order
, 0);
1589 /* bucket_count - The number of hash buckets in the hash lookup
1591 header
.append_uint (4, dwarf5_byte_order
, nametable
.bucket_count ());
1593 /* name_count - The number of unique names in the index. */
1594 header
.append_uint (4, dwarf5_byte_order
, nametable
.name_count ());
1596 /* abbrev_table_size - The size in bytes of the abbreviations
1598 header
.append_uint (4, dwarf5_byte_order
, nametable
.abbrev_table_bytes ());
1600 /* augmentation_string_size - The size in bytes of the augmentation
1601 string. This value is rounded up to a multiple of 4. */
1602 static_assert (sizeof (dwarf5_gdb_augmentation
) % 4 == 0, "");
1603 header
.append_uint (4, dwarf5_byte_order
, sizeof (dwarf5_gdb_augmentation
));
1604 header
.append_data (dwarf5_gdb_augmentation
);
1606 gdb_assert (header
.size () == bytes_of_header
);
1608 header
.file_write (out_file
);
1609 cu_list
.file_write (out_file
);
1610 types_cu_list
.file_write (out_file
);
1611 nametable
.file_write (out_file
, out_file_str
);
1613 assert_file_size (out_file
, expected_bytes
);
1616 /* This represents an index file being written (work-in-progress).
1618 The data is initially written to a temporary file. When the finalize method
1619 is called, the file is closed and moved to its final location.
1621 On failure (if this object is being destroyed with having called finalize),
1622 the temporary file is closed and deleted. */
1624 struct index_wip_file
1626 index_wip_file (const char *dir
, const char *basename
,
1629 filename
= (std::string (dir
) + SLASH_STRING
+ basename
1632 filename_temp
= make_temp_filename (filename
);
1634 scoped_fd
out_file_fd (gdb_mkostemp_cloexec (filename_temp
.data (),
1636 if (out_file_fd
.get () == -1)
1637 perror_with_name (("mkstemp"));
1639 out_file
= out_file_fd
.to_file ("wb");
1641 if (out_file
== nullptr)
1642 error (_("Can't open `%s' for writing"), filename_temp
.data ());
1644 unlink_file
.emplace (filename_temp
.data ());
1649 /* We want to keep the file. */
1650 unlink_file
->keep ();
1652 /* Close and move the str file in place. */
1653 unlink_file
.reset ();
1654 if (rename (filename_temp
.data (), filename
.c_str ()) != 0)
1655 perror_with_name (("rename"));
1658 std::string filename
;
1659 gdb::char_vector filename_temp
;
1661 /* Order matters here; we want FILE to be closed before
1662 FILENAME_TEMP is unlinked, because on MS-Windows one cannot
1663 delete a file that is still open. So, we wrap the unlinker in an
1664 optional and emplace it once we know the file name. */
1665 gdb::optional
<gdb::unlinker
> unlink_file
;
1667 gdb_file_up out_file
;
1670 /* See dwarf-index-write.h. */
1673 write_psymtabs_to_index (struct dwarf2_per_objfile
*dwarf2_per_objfile
,
1674 const char *dir
, const char *basename
,
1675 const char *dwz_basename
,
1676 dw_index_kind index_kind
)
1678 struct objfile
*objfile
= dwarf2_per_objfile
->objfile
;
1680 if (dwarf2_per_objfile
->using_index
)
1681 error (_("Cannot use an index to create the index"));
1683 if (dwarf2_per_objfile
->types
.size () > 1)
1684 error (_("Cannot make an index when the file has multiple .debug_types sections"));
1686 if (!objfile
->partial_symtabs
->psymtabs
1687 || !objfile
->partial_symtabs
->psymtabs_addrmap
)
1691 if (stat (objfile_name (objfile
), &st
) < 0)
1692 perror_with_name (objfile_name (objfile
));
1694 const char *index_suffix
= (index_kind
== dw_index_kind::DEBUG_NAMES
1695 ? INDEX5_SUFFIX
: INDEX4_SUFFIX
);
1697 index_wip_file
objfile_index_wip (dir
, basename
, index_suffix
);
1698 gdb::optional
<index_wip_file
> dwz_index_wip
;
1700 if (dwz_basename
!= NULL
)
1701 dwz_index_wip
.emplace (dir
, dwz_basename
, index_suffix
);
1703 if (index_kind
== dw_index_kind::DEBUG_NAMES
)
1705 index_wip_file
str_wip_file (dir
, basename
, DEBUG_STR_SUFFIX
);
1707 write_debug_names (dwarf2_per_objfile
, objfile_index_wip
.out_file
.get (),
1708 str_wip_file
.out_file
.get ());
1710 str_wip_file
.finalize ();
1713 write_gdbindex (dwarf2_per_objfile
, objfile_index_wip
.out_file
.get (),
1714 (dwz_index_wip
.has_value ()
1715 ? dwz_index_wip
->out_file
.get () : NULL
));
1717 objfile_index_wip
.finalize ();
1719 if (dwz_index_wip
.has_value ())
1720 dwz_index_wip
->finalize ();
1723 /* Implementation of the `save gdb-index' command.
1725 Note that the .gdb_index file format used by this command is
1726 documented in the GDB manual. Any changes here must be documented
1730 save_gdb_index_command (const char *arg
, int from_tty
)
1732 const char dwarf5space
[] = "-dwarf-5 ";
1733 dw_index_kind index_kind
= dw_index_kind::GDB_INDEX
;
1738 arg
= skip_spaces (arg
);
1739 if (strncmp (arg
, dwarf5space
, strlen (dwarf5space
)) == 0)
1741 index_kind
= dw_index_kind::DEBUG_NAMES
;
1742 arg
+= strlen (dwarf5space
);
1743 arg
= skip_spaces (arg
);
1747 error (_("usage: save gdb-index [-dwarf-5] DIRECTORY"));
1749 for (objfile
*objfile
: current_program_space
->objfiles ())
1753 /* If the objfile does not correspond to an actual file, skip it. */
1754 if (stat (objfile_name (objfile
), &st
) < 0)
1757 struct dwarf2_per_objfile
*dwarf2_per_objfile
1758 = get_dwarf2_per_objfile (objfile
);
1760 if (dwarf2_per_objfile
!= NULL
)
1764 const char *basename
= lbasename (objfile_name (objfile
));
1765 const dwz_file
*dwz
= dwarf2_get_dwz_file (dwarf2_per_objfile
);
1766 const char *dwz_basename
= NULL
;
1769 dwz_basename
= lbasename (dwz
->filename ());
1771 write_psymtabs_to_index (dwarf2_per_objfile
, arg
, basename
,
1772 dwz_basename
, index_kind
);
1774 catch (const gdb_exception_error
&except
)
1776 exception_fprintf (gdb_stderr
, except
,
1777 _("Error while writing index for `%s': "),
1778 objfile_name (objfile
));
1785 void _initialize_dwarf_index_write ();
1787 _initialize_dwarf_index_write ()
1789 cmd_list_element
*c
= add_cmd ("gdb-index", class_files
,
1790 save_gdb_index_command
, _("\
1791 Save a gdb-index file.\n\
1792 Usage: save gdb-index [-dwarf-5] DIRECTORY\n\
1794 No options create one file with .gdb-index extension for pre-DWARF-5\n\
1795 compatible .gdb_index section. With -dwarf-5 creates two files with\n\
1796 extension .debug_names and .debug_str for DWARF-5 .debug_names section."),
1798 set_cmd_completer (c
, filename_completer
);