GDB (xrefs)
Loading...
Searching...
No Matches
index-write.c
Go to the documentation of this file.
1/* DWARF index writing support for GDB.
2
3 Copyright (C) 1994-2023 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
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.
11
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.
16
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/>. */
19
20#include "defs.h"
21
22#include "dwarf2/index-write.h"
23
24#include "addrmap.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"
33#include "dwarf2.h"
34#include "dwarf2/read.h"
35#include "dwarf2/dwz.h"
36#include "gdb/gdb-index.h"
37#include "gdbcmd.h"
38#include "objfiles.h"
39#include "ada-lang.h"
40#include "dwarf2/tag.h"
41
42#include <algorithm>
43#include <cmath>
44#include <forward_list>
45#include <set>
46#include <unordered_map>
47#include <unordered_set>
48
49/* Ensure only legit values are used. */
50#define DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE(cu_index, value) \
51 do { \
52 gdb_assert ((unsigned int) (value) <= 1); \
53 GDB_INDEX_SYMBOL_STATIC_SET_VALUE((cu_index), (value)); \
54 } while (0)
55
56/* Ensure only legit values are used. */
57#define DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE(cu_index, value) \
58 do { \
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)); \
62 } while (0)
63
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) \
66 do { \
67 gdb_assert (((value) & ~GDB_INDEX_CU_MASK) == 0); \
68 GDB_INDEX_CU_SET_VALUE((cu_index), (value)); \
69 } while (0)
70
71/* The "save gdb-index" command. */
72
73/* Write SIZE bytes from the buffer pointed to by DATA to FILE, with
74 error checking. */
75
76static void
77file_write (FILE *file, const void *data, size_t size)
78{
79 if (fwrite (data, 1, size, file) != size)
80 error (_("couldn't data write to file"));
81}
82
83/* Write the contents of VEC to FILE, with error checking. */
84
85template<typename Elem, typename Alloc>
86static void
87file_write (FILE *file, const std::vector<Elem, Alloc> &vec)
88{
89 if (!vec.empty ())
90 file_write (file, vec.data (), vec.size () * sizeof (vec[0]));
91}
92
93/* In-memory buffer to prepare data to be written later to a file. */
95{
96public:
97 /* Copy ARRAY to the end of the buffer. */
98 void append_array (gdb::array_view<const gdb_byte> array)
99 {
100 std::copy (array.begin (), array.end (), grow (array.size ()));
101 }
102
103 /* Copy CSTR (a zero-terminated string) to the end of buffer. The
104 terminating zero is appended too. */
105 void append_cstr0 (const char *cstr)
106 {
107 const size_t size = strlen (cstr) + 1;
108 std::copy (cstr, cstr + size, grow (size));
109 }
110
111 /* Store INPUT as ULEB128 to the end of buffer. */
112 void append_unsigned_leb128 (ULONGEST input)
113 {
114 for (;;)
115 {
116 gdb_byte output = input & 0x7f;
117 input >>= 7;
118 if (input)
119 output |= 0x80;
120 m_vec.push_back (output);
121 if (input == 0)
122 break;
123 }
124 }
125
126 /* Accept a host-format integer in VAL and append it to the buffer
127 as a target-format integer which is LEN bytes long. */
128 void append_uint (size_t len, bfd_endian byte_order, ULONGEST val)
129 {
130 ::store_unsigned_integer (grow (len), len, byte_order, val);
131 }
132
133 /* Copy VALUE to the end of the buffer, little-endian. */
135 {
136 append_uint (sizeof (value), BFD_ENDIAN_LITTLE, value);
137 }
138
139 /* Return the size of the buffer. */
140 virtual size_t size () const
141 {
142 return m_vec.size ();
143 }
144
145 /* Return true iff the buffer is empty. */
146 bool empty () const
147 {
148 return m_vec.empty ();
149 }
150
151 /* Write the buffer to FILE. */
152 void file_write (FILE *file) const
153 {
154 ::file_write (file, m_vec);
155 }
156
157private:
158 /* Grow SIZE bytes at the end of the buffer. Returns a pointer to
159 the start of the new block. */
160 gdb_byte *grow (size_t size)
161 {
162 m_vec.resize (m_vec.size () + size);
163 return &*(m_vec.end () - size);
164 }
165
166 gdb::byte_vector m_vec;
167};
168
169/* An entry in the symbol table. */
171{
172 /* The name of the symbol. */
173 const char *name;
174 /* The offset of the name in the constant pool. */
176 /* A sorted vector of the indices of all the CUs that hold an object
177 of this name. */
178 std::vector<offset_type> cu_indices;
179
180 /* Minimize CU_INDICES, sorting them and removing duplicates as
181 appropriate. */
182 void minimize ();
183};
184
185/* The symbol table. This is a power-of-2-sized hash table. */
187{
189 {
190 data.resize (1024);
191 }
192
193 /* Minimize each entry in the symbol table, removing duplicates. */
194 void minimize ()
195 {
196 for (symtab_index_entry &item : data)
197 item.minimize ();
198 }
199
201 std::vector<symtab_index_entry> data;
202
203 /* Temporary storage for names. */
204 auto_obstack m_string_obstack;
205};
206
207/* Find a slot in SYMTAB for the symbol NAME. Returns a reference to
208 the slot.
209
210 Function is used only during write_hash_table so no index format backward
211 compatibility is needed. */
212
213static symtab_index_entry &
214find_slot (struct mapped_symtab *symtab, const char *name)
215{
216 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
217
218 index = hash & (symtab->data.size () - 1);
219 step = ((hash * 17) & (symtab->data.size () - 1)) | 1;
220
221 for (;;)
222 {
223 if (symtab->data[index].name == NULL
224 || strcmp (name, symtab->data[index].name) == 0)
225 return symtab->data[index];
226 index = (index + step) & (symtab->data.size () - 1);
227 }
228}
229
230/* Expand SYMTAB's hash table. */
231
232static void
234{
235 auto old_entries = std::move (symtab->data);
236
237 symtab->data.clear ();
238 symtab->data.resize (old_entries.size () * 2);
239
240 for (auto &it : old_entries)
241 if (it.name != NULL)
242 {
243 auto &ref = find_slot (symtab, it.name);
244 ref = std::move (it);
245 }
246}
247
248/* Add an entry to SYMTAB. NAME is the name of the symbol.
249 CU_INDEX is the index of the CU in which the symbol appears.
250 IS_STATIC is one if the symbol is static, otherwise zero (global). */
251
252static void
254 int is_static, gdb_index_symbol_kind kind,
255 offset_type cu_index)
256{
257 offset_type cu_index_and_attrs;
258
259 ++symtab->n_elements;
260 if (4 * symtab->n_elements / 3 >= symtab->data.size ())
262
264 if (slot.name == NULL)
265 {
266 slot.name = name;
267 /* index_offset is set later. */
268 }
269
270 cu_index_and_attrs = 0;
271 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
272 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
273 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
274
275 /* We don't want to record an index value twice as we want to avoid the
276 duplication.
277 We process all global symbols and then all static symbols
278 (which would allow us to avoid the duplication by only having to check
279 the last entry pushed), but a symbol could have multiple kinds in one CU.
280 To keep things simple we don't worry about the duplication here and
281 sort and uniquify the list after we've processed all symbols. */
282 slot.cu_indices.push_back (cu_index_and_attrs);
283}
284
285/* See symtab_index_entry. */
286
287void
289{
290 if (name == nullptr || cu_indices.empty ())
291 return;
292
293 std::sort (cu_indices.begin (), cu_indices.end ());
294 auto from = std::unique (cu_indices.begin (), cu_indices.end ());
295 cu_indices.erase (from, cu_indices.end ());
296
297 /* We don't want to enter a type more than once, so
298 remove any such duplicates from the list as well. When doing
299 this, we want to keep the entry from the first CU -- but this is
300 implicit due to the sort. This choice is done because it's
301 similar to what gdb historically did for partial symbols. */
302 std::unordered_set<offset_type> seen;
303 from = std::remove_if (cu_indices.begin (), cu_indices.end (),
304 [&] (offset_type val)
305 {
306 gdb_index_symbol_kind kind = GDB_INDEX_SYMBOL_KIND_VALUE (val);
307 if (kind != GDB_INDEX_SYMBOL_KIND_TYPE)
308 return false;
309
310 val &= ~GDB_INDEX_CU_MASK;
311 return !seen.insert (val).second;
312 });
313 cu_indices.erase (from, cu_indices.end ());
314}
315
316/* A form of 'const char *' suitable for container keys. Only the
317 pointer is stored. The strings themselves are compared, not the
318 pointers. */
320{
321public:
322 c_str_view (const char *cstr)
323 : m_cstr (cstr)
324 {}
325
326 bool operator== (const c_str_view &other) const
327 {
328 return strcmp (m_cstr, other.m_cstr) == 0;
329 }
330
331 /* Return the underlying C string. Note, the returned string is
332 only a reference with lifetime of this object. */
333 const char *c_str () const
334 {
335 return m_cstr;
336 }
337
338private:
339 friend class c_str_view_hasher;
340 const char *const m_cstr;
341};
342
343/* A std::unordered_map::hasher for c_str_view that uses the right
344 hash function for strings in a mapped index. */
346{
347public:
348 size_t operator () (const c_str_view &x) const
349 {
350 return mapped_index_string_hash (INT_MAX, x.m_cstr);
351 }
352};
353
354/* A std::unordered_map::hasher for std::vector<>. */
355template<typename T>
357{
358public:
359 size_t operator () (const std::vector<T> &key) const
360 {
361 return iterative_hash (key.data (),
362 sizeof (key.front ()) * key.size (), 0);
363 }
364};
365
366/* Write the mapped hash table SYMTAB to the data buffer OUTPUT, with
367 constant pool entries going into the data buffer CPOOL. */
368
369static void
371{
372 {
373 /* Elements are sorted vectors of the indices of all the CUs that
374 hold an object of this name. */
375 std::unordered_map<std::vector<offset_type>, offset_type,
377 symbol_hash_table;
378
379 /* We add all the index vectors to the constant pool first, to
380 ensure alignment is ok. */
381 for (symtab_index_entry &entry : symtab->data)
382 {
383 if (entry.name == NULL)
384 continue;
385 gdb_assert (entry.index_offset == 0);
386
387 /* Finding before inserting is faster than always trying to
388 insert, because inserting always allocates a node, does the
389 lookup, and then destroys the new node if another node
390 already had the same key. C++17 try_emplace will avoid
391 this. */
392 const auto found
393 = symbol_hash_table.find (entry.cu_indices);
394 if (found != symbol_hash_table.end ())
395 {
396 entry.index_offset = found->second;
397 continue;
398 }
399
400 symbol_hash_table.emplace (entry.cu_indices, cpool.size ());
401 entry.index_offset = cpool.size ();
402 cpool.append_offset (entry.cu_indices.size ());
403 for (const auto index : entry.cu_indices)
404 cpool.append_offset (index);
405 }
406 }
407
408 /* Now write out the hash table. */
409 std::unordered_map<c_str_view, offset_type, c_str_view_hasher> str_table;
410 for (const auto &entry : symtab->data)
411 {
412 offset_type str_off, vec_off;
413
414 if (entry.name != NULL)
415 {
416 const auto insertpair = str_table.emplace (entry.name, cpool.size ());
417 if (insertpair.second)
418 cpool.append_cstr0 (entry.name);
419 str_off = insertpair.first->second;
420 vec_off = entry.index_offset;
421 }
422 else
423 {
424 /* While 0 is a valid constant pool index, it is not valid
425 to have 0 for both offsets. */
426 str_off = 0;
427 vec_off = 0;
428 }
429
430 output.append_offset (str_off);
431 output.append_offset (vec_off);
432 }
433}
434
436 = std::unordered_map<const dwarf2_per_cu_data *, unsigned int>;
437
438/* Helper struct for building the address table. */
441 addrmap_index_data (data_buf &addr_vec_, cu_index_map &cu_index_htab_)
442 : addr_vec (addr_vec_),
443 cu_index_htab (cu_index_htab_)
444 {}
448
449 int operator() (CORE_ADDR start_addr, const void *obj);
450
451 /* True if the previous_* fields are valid.
452 We can't write an entry until we see the next entry (since it is only then
453 that we know the end of the entry). */
454 bool previous_valid = false;
455 /* Index of the CU in the table of all CUs in the index file. */
456 unsigned int previous_cu_index = 0;
457 /* Start address of the CU. */
458 CORE_ADDR previous_cu_start = 0;
459};
460
461/* Write an address entry to ADDR_VEC. */
462
463static void
464add_address_entry (data_buf &addr_vec,
465 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
466{
467 addr_vec.append_uint (8, BFD_ENDIAN_LITTLE, start);
468 addr_vec.append_uint (8, BFD_ENDIAN_LITTLE, end);
469 addr_vec.append_offset (cu_index);
470}
471
472/* Worker function for traversing an addrmap to build the address table. */
473
474int
475addrmap_index_data::operator() (CORE_ADDR start_addr, const void *obj)
476{
477 const dwarf2_per_cu_data *per_cu
478 = static_cast<const dwarf2_per_cu_data *> (obj);
479
480 if (previous_valid)
482 previous_cu_start, start_addr,
484
485 previous_cu_start = start_addr;
486 if (per_cu != NULL)
487 {
488 const auto it = cu_index_htab.find (per_cu);
489 gdb_assert (it != cu_index_htab.cend ());
490 previous_cu_index = it->second;
491 previous_valid = true;
492 }
493 else
494 previous_valid = false;
495
496 return 0;
497}
498
499/* Write PER_BFD's address map to ADDR_VEC.
500 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
501 in the index file. */
502
503static void
504write_address_map (const addrmap *addrmap, data_buf &addr_vec,
505 cu_index_map &cu_index_htab)
506{
508
510
511 /* It's highly unlikely the last entry (end address = 0xff...ff)
512 is valid, but we should still handle it.
513 The end address is recorded as the start of the next region, but that
514 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
515 anyway. */
520}
521
522/* DWARF-5 .debug_names builder. */
523class debug_names
524{
525public:
526 debug_names (dwarf2_per_bfd *per_bfd, bool is_dwarf64,
527 bfd_endian dwarf5_byte_order)
528 : m_dwarf5_byte_order (dwarf5_byte_order),
529 m_dwarf32 (dwarf5_byte_order),
530 m_dwarf64 (dwarf5_byte_order),
531 m_dwarf (is_dwarf64
532 ? static_cast<dwarf &> (m_dwarf64)
533 : static_cast<dwarf &> (m_dwarf32)),
534 m_name_table_string_offs (m_dwarf.name_table_string_offs),
535 m_name_table_entry_offs (m_dwarf.name_table_entry_offs),
536 m_debugstrlookup (per_bfd)
537 {}
539 int dwarf5_offset_size () const
540 {
541 const bool dwarf5_is_dwarf64 = &m_dwarf == &m_dwarf64;
542 return dwarf5_is_dwarf64 ? 8 : 4;
543 }
544
545 /* Is this symbol from DW_TAG_compile_unit or DW_TAG_type_unit? */
546 enum class unit_kind { cu, tu };
547
548 /* Insert one symbol. */
549 void insert (const cooked_index_entry *entry)
550 {
551 const auto it = m_cu_index_htab.find (entry->per_cu);
552 gdb_assert (it != m_cu_index_htab.cend ());
553 const char *name = entry->full_name (&m_string_obstack);
554
555 /* This is incorrect but it mirrors gdb's historical behavior; and
556 because the current .debug_names generation is also incorrect,
557 it seems better to follow what was done before, rather than
558 introduce a mismatch between the newer and older gdb. */
559 dwarf_tag tag = entry->tag;
560 if (tag != DW_TAG_typedef && tag_is_type (tag))
561 tag = DW_TAG_structure_type;
562 else if (tag == DW_TAG_enumerator || tag == DW_TAG_constant)
563 tag = DW_TAG_variable;
564
565 int cu_index = it->second;
566 bool is_static = (entry->flags & IS_STATIC) != 0;
567 unit_kind kind = (entry->per_cu->is_debug_types
569 : unit_kind::cu);
570
571 if (entry->per_cu->lang () == language_ada)
572 {
573 /* We want to ensure that the Ada main function's name appears
574 verbatim in the index. However, this name will be of the
575 form "_ada_mumble", and will be rewritten by ada_decode.
576 So, recognize it specially here and add it to the index by
577 hand. */
578 if (strcmp (main_name (), name) == 0)
579 {
580 const auto insertpair
582 std::set<symbol_value> ());
583 std::set<symbol_value> &value_set = insertpair.first->second;
584 value_set.emplace (symbol_value (tag, cu_index, is_static, kind));
585 }
586
587 /* In order for the index to work when read back into gdb, it
588 has to supply a funny form of the name: it should be the
589 encoded name, with any suffixes stripped. Using the
590 ordinary encoded name will not work properly with the
591 searching logic in find_name_components_bounds; nor will
592 using the decoded name. Furthermore, an Ada "verbatim"
593 name (of the form "<MumBle>") must be entered without the
594 angle brackets. Note that the current index is unusual,
595 see PR symtab/24820 for details. */
596 std::string decoded = ada_decode (name);
597 if (decoded[0] == '<')
598 name = (char *) obstack_copy0 (&m_string_obstack,
599 decoded.c_str () + 1,
600 decoded.length () - 2);
601 else
602 name = obstack_strdup (&m_string_obstack,
603 ada_encode (decoded.c_str ()));
604 }
605
606 const auto insertpair
608 std::set<symbol_value> ());
609 std::set<symbol_value> &value_set = insertpair.first->second;
610 value_set.emplace (symbol_value (tag, cu_index, is_static, kind));
611 }
612
613 /* Build all the tables. All symbols must be already inserted.
614 This function does not call file_write, caller has to do it
615 afterwards. */
616 void build ()
617 {
618 /* Verify the build method has not be called twice. */
619 gdb_assert (m_abbrev_table.empty ());
620 const size_t name_count = m_name_to_value_set.size ();
621 m_bucket_table.resize
622 (std::pow (2, std::ceil (std::log2 (name_count * 4 / 3))));
623 m_hash_table.reserve (name_count);
626
627 /* Map each hash of symbol to its name and value. */
628 struct hash_it_pair
629 {
630 uint32_t hash;
631 decltype (m_name_to_value_set)::const_iterator it;
632 };
633 std::vector<std::forward_list<hash_it_pair>> bucket_hash;
634 bucket_hash.resize (m_bucket_table.size ());
635 for (decltype (m_name_to_value_set)::const_iterator it
636 = m_name_to_value_set.cbegin ();
637 it != m_name_to_value_set.cend ();
638 ++it)
639 {
640 const char *const name = it->first.c_str ();
641 const uint32_t hash = dwarf5_djb_hash (name);
642 hash_it_pair hashitpair;
643 hashitpair.hash = hash;
644 hashitpair.it = it;
645 auto &slot = bucket_hash[hash % bucket_hash.size()];
646 slot.push_front (std::move (hashitpair));
647 }
648 for (size_t bucket_ix = 0; bucket_ix < bucket_hash.size (); ++bucket_ix)
649 {
650 const std::forward_list<hash_it_pair> &hashitlist
651 = bucket_hash[bucket_ix];
652 if (hashitlist.empty ())
653 continue;
654 uint32_t &bucket_slot = m_bucket_table[bucket_ix];
655 /* The hashes array is indexed starting at 1. */
656 store_unsigned_integer (reinterpret_cast<gdb_byte *> (&bucket_slot),
657 sizeof (bucket_slot), m_dwarf5_byte_order,
658 m_hash_table.size () + 1);
659 for (const hash_it_pair &hashitpair : hashitlist)
660 {
661 m_hash_table.push_back (0);
662 store_unsigned_integer (reinterpret_cast<gdb_byte *>
663 (&m_hash_table.back ()),
664 sizeof (m_hash_table.back ()),
665 m_dwarf5_byte_order, hashitpair.hash);
666 const c_str_view &name = hashitpair.it->first;
667 const std::set<symbol_value> &value_set = hashitpair.it->second;
669 (m_debugstrlookup.lookup (name.c_str ()));
671 gdb_assert (!value_set.empty ());
672 for (const symbol_value &value : value_set)
673 {
674 int &idx = m_indexkey_to_idx[index_key (value.dwarf_tag,
675 value.is_static,
676 value.kind)];
677 if (idx == 0)
678 {
679 idx = m_idx_next++;
683 (value.kind == unit_kind::cu ? DW_IDX_compile_unit
684 : DW_IDX_type_unit);
687 ? DW_IDX_GNU_internal
688 : DW_IDX_GNU_external);
689 m_abbrev_table.append_unsigned_leb128 (DW_FORM_flag_present);
690
691 /* Terminate attributes list. */
694 }
695
698 }
699
700 /* Terminate the list of CUs. */
702 }
703 }
704 gdb_assert (m_hash_table.size () == name_count);
705
706 /* Terminate tags list. */
708 }
709
710 /* Return .debug_names bucket count. This must be called only after
711 calling the build method. */
712 uint32_t bucket_count () const
713 {
714 /* Verify the build method has been already called. */
715 gdb_assert (!m_abbrev_table.empty ());
716 const uint32_t retval = m_bucket_table.size ();
717
718 /* Check for overflow. */
719 gdb_assert (retval == m_bucket_table.size ());
720 return retval;
721 }
722
723 /* Return .debug_names names count. This must be called only after
724 calling the build method. */
725 uint32_t name_count () const
726 {
727 /* Verify the build method has been already called. */
728 gdb_assert (!m_abbrev_table.empty ());
729 const uint32_t retval = m_hash_table.size ();
730
731 /* Check for overflow. */
732 gdb_assert (retval == m_hash_table.size ());
733 return retval;
734 }
735
736 /* Return number of bytes of .debug_names abbreviation table. This
737 must be called only after calling the build method. */
738 uint32_t abbrev_table_bytes () const
739 {
740 gdb_assert (!m_abbrev_table.empty ());
741 return m_abbrev_table.size ();
742 }
743
744 /* Return number of bytes the .debug_names section will have. This
745 must be called only after calling the build method. */
746 size_t bytes () const
747 {
748 /* Verify the build method has been already called. */
749 gdb_assert (!m_abbrev_table.empty ());
750 size_t expected_bytes = 0;
751 expected_bytes += m_bucket_table.size () * sizeof (m_bucket_table[0]);
752 expected_bytes += m_hash_table.size () * sizeof (m_hash_table[0]);
753 expected_bytes += m_name_table_string_offs.bytes ();
754 expected_bytes += m_name_table_entry_offs.bytes ();
755 expected_bytes += m_abbrev_table.size ();
756 expected_bytes += m_entry_pool.size ();
757 return expected_bytes;
758 }
759
760 /* Write .debug_names to FILE_NAMES and .debug_str addition to
761 FILE_STR. This must be called only after calling the build
762 method. */
763 void file_write (FILE *file_names, FILE *file_str) const
764 {
765 /* Verify the build method has been already called. */
766 gdb_assert (!m_abbrev_table.empty ());
767 ::file_write (file_names, m_bucket_table);
768 ::file_write (file_names, m_hash_table);
771 m_abbrev_table.file_write (file_names);
772 m_entry_pool.file_write (file_names);
773 m_debugstrlookup.file_write (file_str);
774 }
776 void add_cu (dwarf2_per_cu_data *per_cu, offset_type index)
777 {
778 m_cu_index_htab.emplace (per_cu, index);
779 }
780
781private:
782
783 /* Storage for symbol names mapping them to their .debug_str section
784 offsets. */
785 class debug_str_lookup
786 {
787 public:
788
789 /* Object constructor to be called for current DWARF2_PER_BFD.
790 All .debug_str section strings are automatically stored. */
792 : m_abfd (per_bfd->obfd),
793 m_per_bfd (per_bfd)
794 {
795 gdb_assert (per_bfd->str.readin);
796 if (per_bfd->str.buffer == NULL)
797 return;
798 for (const gdb_byte *data = per_bfd->str.buffer;
799 data < (per_bfd->str.buffer
800 + per_bfd->str.size);)
801 {
802 const char *const s = reinterpret_cast<const char *> (data);
803 const auto insertpair
804 = m_str_table.emplace (c_str_view (s),
805 data - per_bfd->str.buffer);
806 if (!insertpair.second)
807 complaint (_("Duplicate string \"%s\" in "
808 ".debug_str section [in module %s]"),
809 s, bfd_get_filename (m_abfd));
810 data += strlen (s) + 1;
811 }
812 }
813
814 /* Return offset of symbol name S in the .debug_str section. Add
815 such symbol to the section's end if it does not exist there
816 yet. */
817 size_t lookup (const char *s)
818 {
819 const auto it = m_str_table.find (c_str_view (s));
820 if (it != m_str_table.end ())
821 return it->second;
822 const size_t offset = (m_per_bfd->str.size
823 + m_str_add_buf.size ());
824 m_str_table.emplace (c_str_view (s), offset);
826 return offset;
827 }
828
829 /* Append the end of the .debug_str section to FILE. */
830 void file_write (FILE *file) const
831 {
833 }
834
835 private:
836 std::unordered_map<c_str_view, size_t, c_str_view_hasher> m_str_table;
837 bfd *const m_abfd;
839
840 /* Data to add at the end of .debug_str for new needed symbol names. */
842 };
843
844 /* Container to map used DWARF tags to their .debug_names abbreviation
845 tags. */
846 class index_key
847 {
848 public:
849 index_key (int dwarf_tag_, bool is_static_, unit_kind kind_)
850 : dwarf_tag (dwarf_tag_), is_static (is_static_), kind (kind_)
851 {
852 }
853
854 bool
855 operator== (const index_key &other) const
856 {
857 return (dwarf_tag == other.dwarf_tag && is_static == other.is_static
858 && kind == other.kind);
859 }
861 const int dwarf_tag;
862 const bool is_static;
863 const unit_kind kind;
864 };
865
866 /* Provide std::unordered_map::hasher for index_key. */
867 class index_key_hasher
868 {
869 public:
870 size_t
871 operator () (const index_key &key) const
872 {
873 return (std::hash<int>() (key.dwarf_tag) << 1) | key.is_static;
874 }
875 };
876
877 /* Parameters of one symbol entry. */
878 class symbol_value
879 {
880 public:
881 const int dwarf_tag, cu_index;
882 const bool is_static;
883 const unit_kind kind;
885 symbol_value (int dwarf_tag_, int cu_index_, bool is_static_,
886 unit_kind kind_)
887 : dwarf_tag (dwarf_tag_), cu_index (cu_index_), is_static (is_static_),
888 kind (kind_)
889 {}
891 bool
892 operator< (const symbol_value &other) const
893 {
894#define X(n) \
895 do \
896 { \
897 if (n < other.n) \
898 return true; \
899 if (n > other.n) \
900 return false; \
901 } \
902 while (0)
903 X (dwarf_tag);
904 X (is_static);
905 X (kind);
906 X (cu_index);
907#undef X
908 return false;
909 }
910 };
911
912 /* Abstract base class to unify DWARF-32 and DWARF-64 name table
913 output. */
914 class offset_vec
915 {
916 protected:
917 const bfd_endian dwarf5_byte_order;
918 public:
919 explicit offset_vec (bfd_endian dwarf5_byte_order_)
920 : dwarf5_byte_order (dwarf5_byte_order_)
921 {}
922
923 /* Call std::vector::reserve for NELEM elements. */
924 virtual void reserve (size_t nelem) = 0;
925
926 /* Call std::vector::push_back with store_unsigned_integer byte
927 reordering for ELEM. */
928 virtual void push_back_reorder (size_t elem) = 0;
929
930 /* Return expected output size in bytes. */
931 virtual size_t bytes () const = 0;
932
933 /* Write name table to FILE. */
934 virtual void file_write (FILE *file) const = 0;
935 };
936
937 /* Template to unify DWARF-32 and DWARF-64 output. */
938 template<typename OffsetSize>
939 class offset_vec_tmpl : public offset_vec
940 {
941 public:
942 explicit offset_vec_tmpl (bfd_endian dwarf5_byte_order_)
943 : offset_vec (dwarf5_byte_order_)
944 {}
945
946 /* Implement offset_vec::reserve. */
947 void reserve (size_t nelem) override
948 {
949 m_vec.reserve (nelem);
950 }
951
952 /* Implement offset_vec::push_back_reorder. */
953 void push_back_reorder (size_t elem) override
954 {
955 m_vec.push_back (elem);
956 /* Check for overflow. */
957 gdb_assert (m_vec.back () == elem);
958 store_unsigned_integer (reinterpret_cast<gdb_byte *> (&m_vec.back ()),
959 sizeof (m_vec.back ()), dwarf5_byte_order, elem);
960 }
961
962 /* Implement offset_vec::bytes. */
963 size_t bytes () const override
964 {
965 return m_vec.size () * sizeof (m_vec[0]);
966 }
967
968 /* Implement offset_vec::file_write. */
969 void file_write (FILE *file) const override
970 {
971 ::file_write (file, m_vec);
972 }
973
974 private:
975 std::vector<OffsetSize> m_vec;
976 };
977
978 /* Base class to unify DWARF-32 and DWARF-64 .debug_names output
979 respecting name table width. */
980 class dwarf
981 {
982 public:
985 dwarf (offset_vec &name_table_string_offs_,
986 offset_vec &name_table_entry_offs_)
987 : name_table_string_offs (name_table_string_offs_),
988 name_table_entry_offs (name_table_entry_offs_)
989 {
990 }
991 };
992
993 /* Template to unify DWARF-32 and DWARF-64 .debug_names output
994 respecting name table width. */
995 template<typename OffsetSize>
996 class dwarf_tmpl : public dwarf
997 {
998 public:
999 explicit dwarf_tmpl (bfd_endian dwarf5_byte_order_)
1001 m_name_table_string_offs (dwarf5_byte_order_),
1002 m_name_table_entry_offs (dwarf5_byte_order_)
1003 {}
1004
1005 private:
1008 };
1009
1010 /* Store value of each symbol. */
1011 std::unordered_map<c_str_view, std::set<symbol_value>, c_str_view_hasher>
1013
1014 /* Tables of DWARF-5 .debug_names. They are in object file byte
1015 order. */
1016 std::vector<uint32_t> m_bucket_table;
1017 std::vector<uint32_t> m_hash_table;
1019 const bfd_endian m_dwarf5_byte_order;
1025
1026 /* Map each used .debug_names abbreviation tag parameter to its
1027 index value. */
1028 std::unordered_map<index_key, int, index_key_hasher> m_indexkey_to_idx;
1029
1030 /* Next unused .debug_names abbreviation tag for
1031 m_indexkey_to_idx. */
1032 int m_idx_next = 1;
1033
1034 /* .debug_names abbreviation table. */
1036
1037 /* .debug_names entry pool. */
1039
1040 /* Temporary storage for Ada names. */
1041 auto_obstack m_string_obstack;
1044};
1045
1046/* Return iff any of the needed offsets does not fit into 32-bit
1047 .debug_names section. */
1048
1049static bool
1051{
1052 for (const auto &per_cu : per_bfd->all_units)
1053 {
1054 if (to_underlying (per_cu->sect_off)
1055 >= (static_cast<uint64_t> (1) << 32))
1056 return true;
1057 }
1058 return false;
1059}
1060
1061/* Assert that FILE's size is EXPECTED_SIZE. Assumes file's seek
1062 position is at the end of the file. */
1063
1064static void
1065assert_file_size (FILE *file, size_t expected_size)
1066{
1067 const auto file_size = ftell (file);
1068 if (file_size == -1)
1069 perror_with_name (("ftell"));
1070 gdb_assert (file_size == expected_size);
1071}
1072
1073/* Write a gdb index file to OUT_FILE from all the sections passed as
1074 arguments. */
1075
1076static void
1077write_gdbindex_1 (FILE *out_file,
1078 const data_buf &cu_list,
1079 const data_buf &types_cu_list,
1080 const data_buf &addr_vec,
1081 const data_buf &symtab_vec,
1082 const data_buf &constant_pool)
1083{
1084 data_buf contents;
1085 const offset_type size_of_header = 6 * sizeof (offset_type);
1086 uint64_t total_len = size_of_header;
1087
1088 /* The version number. */
1089 contents.append_offset (8);
1090
1091 /* The offset of the CU list from the start of the file. */
1092 contents.append_offset (total_len);
1093 total_len += cu_list.size ();
1094
1095 /* The offset of the types CU list from the start of the file. */
1096 contents.append_offset (total_len);
1097 total_len += types_cu_list.size ();
1098
1099 /* The offset of the address table from the start of the file. */
1100 contents.append_offset (total_len);
1101 total_len += addr_vec.size ();
1102
1103 /* The offset of the symbol table from the start of the file. */
1104 contents.append_offset (total_len);
1105 total_len += symtab_vec.size ();
1106
1107 /* The offset of the constant pool from the start of the file. */
1108 contents.append_offset (total_len);
1109 total_len += constant_pool.size ();
1110
1111 gdb_assert (contents.size () == size_of_header);
1112
1113 /* The maximum size of an index file is limited by the maximum value
1114 capable of being represented by 'offset_type'. Throw an error if
1115 that length has been exceeded. */
1116 size_t max_size = ~(offset_type) 0;
1117 if (total_len > max_size)
1118 error (_("gdb-index maximum file size of %zu exceeded"), max_size);
1119
1120 if (out_file == nullptr)
1121 return;
1122
1123 contents.file_write (out_file);
1124 cu_list.file_write (out_file);
1125 types_cu_list.file_write (out_file);
1126 addr_vec.file_write (out_file);
1127 symtab_vec.file_write (out_file);
1128 constant_pool.file_write (out_file);
1129
1130 assert_file_size (out_file, total_len);
1131}
1132
1133/* Write the contents of the internal "cooked" index. */
1134
1135static void
1137 const cu_index_map &cu_index_htab,
1138 struct mapped_symtab *symtab)
1139{
1140 for (const cooked_index_entry *entry : table->all_entries ())
1141 {
1142 const auto it = cu_index_htab.find (entry->per_cu);
1143 gdb_assert (it != cu_index_htab.cend ());
1144
1145 const char *name = entry->full_name (&symtab->m_string_obstack);
1146
1147 if (entry->per_cu->lang () == language_ada)
1148 {
1149 /* In order for the index to work when read back into
1150 gdb, it has to use the encoded name, with any
1151 suffixes stripped. */
1152 std::string encoded = ada_encode (name, false);
1153 name = obstack_strdup (&symtab->m_string_obstack,
1154 encoded.c_str ());
1155 }
1156 else if (entry->per_cu->lang () == language_cplus
1157 && (entry->flags & IS_LINKAGE) != 0)
1158 {
1159 /* GDB never put C++ linkage names into .gdb_index. The
1160 theory here is that a linkage name will normally be in
1161 the minimal symbols anyway, so including it in the index
1162 is usually redundant -- and the cases where it would not
1163 be redundant are rare and not worth supporting. */
1164 continue;
1165 }
1166 else if ((entry->flags & IS_TYPE_DECLARATION) != 0)
1167 {
1168 /* Don't add type declarations to the index. */
1169 continue;
1170 }
1171
1172 gdb_index_symbol_kind kind;
1173 if (entry->tag == DW_TAG_subprogram)
1174 kind = GDB_INDEX_SYMBOL_KIND_FUNCTION;
1175 else if (entry->tag == DW_TAG_variable
1176 || entry->tag == DW_TAG_constant
1177 || entry->tag == DW_TAG_enumerator)
1178 kind = GDB_INDEX_SYMBOL_KIND_VARIABLE;
1179 else if (entry->tag == DW_TAG_module
1180 || entry->tag == DW_TAG_common_block)
1181 kind = GDB_INDEX_SYMBOL_KIND_OTHER;
1182 else
1183 kind = GDB_INDEX_SYMBOL_KIND_TYPE;
1184
1185 add_index_entry (symtab, name, (entry->flags & IS_STATIC) != 0,
1186 kind, it->second);
1187 }
1188}
1189
1190/* Write contents of a .gdb_index section for OBJFILE into OUT_FILE.
1191 If OBJFILE has an associated dwz file, write contents of a .gdb_index
1192 section for that dwz file into DWZ_OUT_FILE. If OBJFILE does not have an
1193 associated dwz file, DWZ_OUT_FILE must be NULL. */
1194
1195static void
1197 FILE *out_file, FILE *dwz_out_file)
1198{
1200 data_buf objfile_cu_list;
1201 data_buf dwz_cu_list;
1202
1203 /* While we're scanning CU's create a table that maps a dwarf2_per_cu_data
1204 (which is what addrmap records) to its index (which is what is recorded
1205 in the index file). This will later be needed to write the address
1206 table. */
1207 cu_index_map cu_index_htab;
1208 cu_index_htab.reserve (per_bfd->all_units.size ());
1209
1210 /* Store out the .debug_type CUs, if any. */
1211 data_buf types_cu_list;
1212
1213 /* The CU list is already sorted, so we don't need to do additional
1214 work here. */
1215
1216 int counter = 0;
1217 for (int i = 0; i < per_bfd->all_units.size (); ++i)
1218 {
1219 dwarf2_per_cu_data *per_cu = per_bfd->all_units[i].get ();
1220
1221 const auto insertpair = cu_index_htab.emplace (per_cu, counter);
1222 gdb_assert (insertpair.second);
1223
1224 /* See enhancement PR symtab/30838. */
1225 gdb_assert (!(per_cu->is_dwz && per_cu->is_debug_types));
1226
1227 /* The all_units list contains CUs read from the objfile as well as
1228 from the eventual dwz file. We need to place the entry in the
1229 corresponding index. */
1230 data_buf &cu_list = (per_cu->is_debug_types
1231 ? types_cu_list
1232 : per_cu->is_dwz ? dwz_cu_list : objfile_cu_list);
1233 cu_list.append_uint (8, BFD_ENDIAN_LITTLE,
1234 to_underlying (per_cu->sect_off));
1235 if (per_cu->is_debug_types)
1236 {
1237 signatured_type *sig_type = (signatured_type *) per_cu;
1238 cu_list.append_uint (8, BFD_ENDIAN_LITTLE,
1239 to_underlying (sig_type->type_offset_in_tu));
1240 cu_list.append_uint (8, BFD_ENDIAN_LITTLE,
1241 sig_type->signature);
1242 }
1243 else
1244 cu_list.append_uint (8, BFD_ENDIAN_LITTLE, per_cu->length ());
1245
1246 ++counter;
1247 }
1248
1249 write_cooked_index (table, cu_index_htab, &symtab);
1250
1251 /* Dump the address map. */
1252 data_buf addr_vec;
1253 for (auto map : table->get_addrmaps ())
1254 write_address_map (map, addr_vec, cu_index_htab);
1255
1256 /* Now that we've processed all symbols we can shrink their cu_indices
1257 lists. */
1258 symtab.minimize ();
1259
1260 data_buf symtab_vec, constant_pool;
1261 if (symtab.n_elements == 0)
1262 symtab.data.resize (0);
1263
1264 write_hash_table (&symtab, symtab_vec, constant_pool);
1265
1266 write_gdbindex_1(out_file, objfile_cu_list, types_cu_list, addr_vec,
1267 symtab_vec, constant_pool);
1268
1269 if (dwz_out_file != NULL)
1270 write_gdbindex_1 (dwz_out_file, dwz_cu_list, {}, {}, {}, {});
1271 else
1272 gdb_assert (dwz_cu_list.empty ());
1273}
1274
1275/* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
1276static const gdb_byte dwarf5_gdb_augmentation[] = { 'G', 'D', 'B', 0 };
1277
1278/* Write a new .debug_names section for OBJFILE into OUT_FILE, write
1279 needed addition to .debug_str section to OUT_FILE_STR. Return how
1280 many bytes were expected to be written into OUT_FILE. */
1281
1282static void
1284 FILE *out_file, FILE *out_file_str)
1285{
1286 const bool dwarf5_is_dwarf64 = check_dwarf64_offsets (per_bfd);
1287 const enum bfd_endian dwarf5_byte_order
1288 = bfd_big_endian (per_bfd->obfd) ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
1289
1290 /* The CU list is already sorted, so we don't need to do additional
1291 work here. Also, the debug_types entries do not appear in
1292 all_units, but only in their own hash table. */
1293 data_buf cu_list;
1294 data_buf types_cu_list;
1295 debug_names nametable (per_bfd, dwarf5_is_dwarf64, dwarf5_byte_order);
1296 int counter = 0;
1297 int types_counter = 0;
1298 for (int i = 0; i < per_bfd->all_units.size (); ++i)
1299 {
1300 dwarf2_per_cu_data *per_cu = per_bfd->all_units[i].get ();
1301
1302 int &this_counter = per_cu->is_debug_types ? types_counter : counter;
1303 data_buf &this_list = per_cu->is_debug_types ? types_cu_list : cu_list;
1304
1305 nametable.add_cu (per_cu, this_counter);
1306 this_list.append_uint (nametable.dwarf5_offset_size (),
1307 dwarf5_byte_order,
1308 to_underlying (per_cu->sect_off));
1309 ++this_counter;
1310 }
1311
1312 /* Verify that all units are represented. */
1313 gdb_assert (counter == per_bfd->all_comp_units.size ());
1314 gdb_assert (types_counter == per_bfd->all_type_units.size ());
1315
1316 for (const cooked_index_entry *entry : table->all_entries ())
1317 nametable.insert (entry);
1318
1319 nametable.build ();
1320
1321 /* No addr_vec - DWARF-5 uses .debug_aranges generated by GCC. */
1322
1323 const offset_type bytes_of_header
1324 = ((dwarf5_is_dwarf64 ? 12 : 4)
1325 + 2 + 2 + 7 * 4
1326 + sizeof (dwarf5_gdb_augmentation));
1327 size_t expected_bytes = 0;
1328 expected_bytes += bytes_of_header;
1329 expected_bytes += cu_list.size ();
1330 expected_bytes += types_cu_list.size ();
1331 expected_bytes += nametable.bytes ();
1332 data_buf header;
1333
1334 if (!dwarf5_is_dwarf64)
1335 {
1336 const uint64_t size64 = expected_bytes - 4;
1337 gdb_assert (size64 < 0xfffffff0);
1338 header.append_uint (4, dwarf5_byte_order, size64);
1339 }
1340 else
1341 {
1342 header.append_uint (4, dwarf5_byte_order, 0xffffffff);
1343 header.append_uint (8, dwarf5_byte_order, expected_bytes - 12);
1344 }
1345
1346 /* The version number. */
1347 header.append_uint (2, dwarf5_byte_order, 5);
1348
1349 /* Padding. */
1350 header.append_uint (2, dwarf5_byte_order, 0);
1351
1352 /* comp_unit_count - The number of CUs in the CU list. */
1353 header.append_uint (4, dwarf5_byte_order, counter);
1354
1355 /* local_type_unit_count - The number of TUs in the local TU
1356 list. */
1357 header.append_uint (4, dwarf5_byte_order, types_counter);
1358
1359 /* foreign_type_unit_count - The number of TUs in the foreign TU
1360 list. */
1361 header.append_uint (4, dwarf5_byte_order, 0);
1362
1363 /* bucket_count - The number of hash buckets in the hash lookup
1364 table. */
1365 header.append_uint (4, dwarf5_byte_order, nametable.bucket_count ());
1366
1367 /* name_count - The number of unique names in the index. */
1368 header.append_uint (4, dwarf5_byte_order, nametable.name_count ());
1369
1370 /* abbrev_table_size - The size in bytes of the abbreviations
1371 table. */
1372 header.append_uint (4, dwarf5_byte_order, nametable.abbrev_table_bytes ());
1373
1374 /* augmentation_string_size - The size in bytes of the augmentation
1375 string. This value is rounded up to a multiple of 4. */
1376 static_assert (sizeof (dwarf5_gdb_augmentation) % 4 == 0, "");
1377 header.append_uint (4, dwarf5_byte_order, sizeof (dwarf5_gdb_augmentation));
1379
1380 gdb_assert (header.size () == bytes_of_header);
1381
1382 header.file_write (out_file);
1383 cu_list.file_write (out_file);
1384 types_cu_list.file_write (out_file);
1385 nametable.file_write (out_file, out_file_str);
1386
1387 assert_file_size (out_file, expected_bytes);
1388}
1389
1390/* This represents an index file being written (work-in-progress).
1391
1392 The data is initially written to a temporary file. When the finalize method
1393 is called, the file is closed and moved to its final location.
1394
1395 On failure (if this object is being destroyed with having called finalize),
1396 the temporary file is closed and deleted. */
1398struct index_wip_file
1400 index_wip_file (const char *dir, const char *basename,
1401 const char *suffix)
1402 {
1403 filename = (std::string (dir) + SLASH_STRING + basename
1404 + suffix);
1405
1406 filename_temp = make_temp_filename (filename);
1407
1408 scoped_fd out_file_fd = gdb_mkostemp_cloexec (filename_temp.data (),
1409 O_BINARY);
1410 if (out_file_fd.get () == -1)
1411 perror_with_name (("mkstemp"));
1412
1413 out_file = out_file_fd.to_file ("wb");
1414
1415 if (out_file == nullptr)
1416 error (_("Can't open `%s' for writing"), filename_temp.data ());
1417
1418 unlink_file.emplace (filename_temp.data ());
1419 }
1421 void finalize ()
1422 {
1423 /* We want to keep the file. */
1424 unlink_file->keep ();
1425
1426 /* Close and move the str file in place. */
1427 unlink_file.reset ();
1428 if (rename (filename_temp.data (), filename.c_str ()) != 0)
1429 perror_with_name (("rename"));
1430 }
1432 std::string filename;
1433 gdb::char_vector filename_temp;
1434
1435 /* Order matters here; we want FILE to be closed before
1436 FILENAME_TEMP is unlinked, because on MS-Windows one cannot
1437 delete a file that is still open. So, we wrap the unlinker in an
1438 optional and emplace it once we know the file name. */
1439 gdb::optional<gdb::unlinker> unlink_file;
1441 gdb_file_up out_file;
1442};
1443
1444/* See dwarf-index-write.h. */
1445
1446void
1447write_dwarf_index (dwarf2_per_bfd *per_bfd, const char *dir,
1448 const char *basename, const char *dwz_basename,
1449 dw_index_kind index_kind)
1450{
1451 if (per_bfd->index_table == nullptr)
1452 error (_("No debugging symbols"));
1453 cooked_index *table = per_bfd->index_table->index_for_writing ();
1454
1455 if (per_bfd->types.size () > 1)
1456 error (_("Cannot make an index when the file has multiple .debug_types sections"));
1457
1458 const char *index_suffix = (index_kind == dw_index_kind::DEBUG_NAMES
1460
1461 index_wip_file objfile_index_wip (dir, basename, index_suffix);
1462 gdb::optional<index_wip_file> dwz_index_wip;
1463
1464 if (dwz_basename != NULL)
1465 dwz_index_wip.emplace (dir, dwz_basename, index_suffix);
1466
1467 if (index_kind == dw_index_kind::DEBUG_NAMES)
1468 {
1469 index_wip_file str_wip_file (dir, basename, DEBUG_STR_SUFFIX);
1470
1471 write_debug_names (per_bfd, table, objfile_index_wip.out_file.get (),
1472 str_wip_file.out_file.get ());
1473
1474 str_wip_file.finalize ();
1475 }
1476 else
1477 write_gdbindex (per_bfd, table, objfile_index_wip.out_file.get (),
1478 (dwz_index_wip.has_value ()
1479 ? dwz_index_wip->out_file.get () : NULL));
1480
1481 objfile_index_wip.finalize ();
1482
1483 if (dwz_index_wip.has_value ())
1484 dwz_index_wip->finalize ();
1485}
1486
1487/* Implementation of the `save gdb-index' command.
1488
1489 Note that the .gdb_index file format used by this command is
1490 documented in the GDB manual. Any changes here must be documented
1491 there. */
1492
1493static void
1494save_gdb_index_command (const char *arg, int from_tty)
1495{
1496 const char dwarf5space[] = "-dwarf-5 ";
1498
1499 if (!arg)
1500 arg = "";
1501
1502 arg = skip_spaces (arg);
1503 if (strncmp (arg, dwarf5space, strlen (dwarf5space)) == 0)
1504 {
1505 index_kind = dw_index_kind::DEBUG_NAMES;
1506 arg += strlen (dwarf5space);
1507 arg = skip_spaces (arg);
1508 }
1509
1510 if (!*arg)
1511 error (_("usage: save gdb-index [-dwarf-5] DIRECTORY"));
1512
1514 {
1515 /* If the objfile does not correspond to an actual file, skip it. */
1516 if ((objfile->flags & OBJF_NOT_FILENAME) != 0)
1517 continue;
1518
1520
1521 if (per_objfile != NULL)
1522 {
1523 try
1524 {
1525 const char *basename = lbasename (objfile_name (objfile));
1526 const dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
1527 const char *dwz_basename = NULL;
1528
1529 if (dwz != NULL)
1530 dwz_basename = lbasename (dwz->filename ());
1531
1532 write_dwarf_index (per_objfile->per_bfd, arg, basename,
1533 dwz_basename, index_kind);
1534 }
1535 catch (const gdb_exception_error &except)
1536 {
1538 _("Error while writing index for `%s': "),
1540 }
1541 }
1542
1543 }
1544}
1545
1546#if GDB_SELF_TEST
1547#include "gdbsupport/selftest.h"
1548
1549namespace selftests {
1550
1551class pretend_data_buf : public data_buf
1552{
1553public:
1554 /* Set the pretend size. */
1555 void set_pretend_size (size_t s) {
1556 m_pretend_size = s;
1557 }
1558
1559 /* Override size method of data_buf, returning the pretend size instead. */
1560 size_t size () const override {
1561 return m_pretend_size;
1562 }
1563
1564private:
1565 size_t m_pretend_size = 0;
1566};
1567
1568static void
1569gdb_index ()
1570{
1571 pretend_data_buf cu_list;
1572 pretend_data_buf types_cu_list;
1573 pretend_data_buf addr_vec;
1574 pretend_data_buf symtab_vec;
1575 pretend_data_buf constant_pool;
1576
1577 const size_t size_of_header = 6 * sizeof (offset_type);
1578
1579 /* Test that an overly large index will throw an error. */
1580 symtab_vec.set_pretend_size (~(offset_type)0 - size_of_header);
1581 constant_pool.set_pretend_size (1);
1582
1583 bool saw_exception = false;
1584 try
1585 {
1586 write_gdbindex_1 (nullptr, cu_list, types_cu_list, addr_vec,
1587 symtab_vec, constant_pool);
1588 }
1589 catch (const gdb_exception_error &e)
1590 {
1591 SELF_CHECK (e.reason == RETURN_ERROR);
1592 SELF_CHECK (e.error == GENERIC_ERROR);
1593 SELF_CHECK (e.message->find (_("gdb-index maximum file size of"))
1594 != std::string::npos);
1595 SELF_CHECK (e.message->find (_("exceeded")) != std::string::npos);
1596 saw_exception = true;
1597 }
1598 SELF_CHECK (saw_exception);
1599
1600 /* Test that the largest possible index will not throw an error. */
1601 constant_pool.set_pretend_size (0);
1602
1603 saw_exception = false;
1604 try
1605 {
1606 write_gdbindex_1 (nullptr, cu_list, types_cu_list, addr_vec,
1607 symtab_vec, constant_pool);
1608 }
1609 catch (const gdb_exception_error &e)
1610 {
1611 saw_exception = true;
1612 }
1613 SELF_CHECK (!saw_exception);
1614}
1615
1616} /* selftests namespace. */
1617#endif
1618
1620void
1622{
1623#if GDB_SELF_TEST
1624 selftests::register_test ("gdb_index", selftests::gdb_index);
1625#endif
1626
1627 cmd_list_element *c = add_cmd ("gdb-index", class_files,
1629Save a gdb-index file.\n\
1630Usage: save gdb-index [-dwarf-5] DIRECTORY\n\
1631\n\
1632No options create one file with .gdb-index extension for pre-DWARF-5\n\
1633compatible .gdb_index section. With -dwarf-5 creates two files with\n\
1634extension .debug_names and .debug_str for DWARF-5 .debug_names section."),
1635 &save_cmdlist);
1637}
const char *const name
std::string ada_decode(const char *encoded, bool wrap, bool operators)
Definition ada-lang.c:1311
std::string ada_encode(const char *decoded, bool fold)
Definition ada-lang.c:1155
struct cmd_list_element * save_cmdlist
Definition breakpoint.c:834
size_t operator()(const c_str_view &x) const
const char * c_str() const
bool operator==(const c_str_view &other) const
const char *const m_cstr
c_str_view(const char *cstr)
std::vector< const addrmap * > get_addrmaps() const
range all_entries() const
void file_write(FILE *file) const
void append_cstr0(const char *cstr)
virtual size_t size() const
void append_array(gdb::array_view< const gdb_byte > array)
Definition index-write.c:98
bool empty() const
void append_uint(size_t len, bfd_endian byte_order, ULONGEST val)
gdb::byte_vector m_vec
void append_unsigned_leb128(ULONGEST input)
gdb_byte * grow(size_t size)
void append_offset(offset_type value)
size_t lookup(const char *s)
std::unordered_map< c_str_view, size_t, c_str_view_hasher > m_str_table
debug_str_lookup(dwarf2_per_bfd *per_bfd)
void file_write(FILE *file) const
dwarf_tmpl(bfd_endian dwarf5_byte_order_)
offset_vec_tmpl< OffsetSize > m_name_table_string_offs
offset_vec_tmpl< OffsetSize > m_name_table_entry_offs
dwarf(offset_vec &name_table_string_offs_, offset_vec &name_table_entry_offs_)
offset_vec & name_table_string_offs
offset_vec & name_table_entry_offs
size_t operator()(const index_key &key) const
index_key(int dwarf_tag_, bool is_static_, unit_kind kind_)
bool operator==(const index_key &other) const
const unit_kind kind
offset_vec_tmpl(bfd_endian dwarf5_byte_order_)
void file_write(FILE *file) const override
void push_back_reorder(size_t elem) override
void reserve(size_t nelem) override
size_t bytes() const override
std::vector< OffsetSize > m_vec
const bfd_endian dwarf5_byte_order
virtual void file_write(FILE *file) const =0
virtual size_t bytes() const =0
virtual void push_back_reorder(size_t elem)=0
virtual void reserve(size_t nelem)=0
offset_vec(bfd_endian dwarf5_byte_order_)
bool operator<(const symbol_value &other) const
symbol_value(int dwarf_tag_, int cu_index_, bool is_static_, unit_kind kind_)
std::vector< uint32_t > m_hash_table
int dwarf5_offset_size() const
void file_write(FILE *file_names, FILE *file_str) const
uint32_t abbrev_table_bytes() const
data_buf m_abbrev_table
uint32_t name_count() const
offset_vec & m_name_table_entry_offs
size_t bytes() const
dwarf_tmpl< uint64_t > m_dwarf64
void add_cu(dwarf2_per_cu_data *per_cu, offset_type index)
dwarf_tmpl< uint32_t > m_dwarf32
data_buf m_entry_pool
std::unordered_map< index_key, int, index_key_hasher > m_indexkey_to_idx
offset_vec & m_name_table_string_offs
void insert(const cooked_index_entry *entry)
cu_index_map m_cu_index_htab
std::unordered_map< c_str_view, std::set< symbol_value >, c_str_view_hasher > m_name_to_value_set
debug_str_lookup m_debugstrlookup
auto_obstack m_string_obstack
dwarf & m_dwarf
debug_names(dwarf2_per_bfd *per_bfd, bool is_dwarf64, bfd_endian dwarf5_byte_order)
std::vector< uint32_t > m_bucket_table
const bfd_endian m_dwarf5_byte_order
uint32_t bucket_count() const
size_t operator()(const std::vector< T > &key) const
struct cmd_list_element * add_cmd(const char *name, enum command_class theclass, const char *doc, struct cmd_list_element **list)
Definition cli-decode.c:233
void set_cmd_completer(struct cmd_list_element *cmd, completer_ftype *completer)
Definition cli-decode.c:117
@ class_files
Definition command.h:57
#define complaint(FMT,...)
Definition complaints.h:47
void filename_completer(struct cmd_list_element *ignore, completion_tracker &tracker, const char *text, const char *word)
Definition completer.c:204
@ IS_TYPE_DECLARATION
@ IS_STATIC
@ IS_LINKAGE
static void store_unsigned_integer(gdb_byte *addr, int len, enum bfd_endian byte_order, ULONGEST val)
Definition defs.h:515
#define O_BINARY
Definition defs.h:114
@ language_ada
Definition defs.h:225
@ language_cplus
Definition defs.h:216
struct dwz_file * dwarf2_get_dwz_file(dwarf2_per_bfd *per_bfd, bool require)
Definition dwz.c:193
void exception_fprintf(struct ui_file *file, const struct gdb_exception &e, const char *prefix,...)
Definition exceptions.c:116
bfd * obfd
size_t size
Definition go32-nat.c:239
uint32_t dwarf5_djb_hash(const char *str_)
hashval_t mapped_index_string_hash(int index_version, const void *p)
#define INDEX5_SUFFIX
uint32_t offset_type
#define DEBUG_STR_SUFFIX
#define INDEX4_SUFFIX
static void write_gdbindex_1(FILE *out_file, const data_buf &cu_list, const data_buf &types_cu_list, const data_buf &addr_vec, const data_buf &symtab_vec, const data_buf &constant_pool)
#define X(n)
#define DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE(cu_index, value)
Definition index-write.c:50
static void write_debug_names(dwarf2_per_bfd *per_bfd, cooked_index *table, FILE *out_file, FILE *out_file_str)
std::unordered_map< const dwarf2_per_cu_data *, unsigned int > cu_index_map
static void assert_file_size(FILE *file, size_t expected_size)
#define DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE(cu_index, value)
Definition index-write.c:57
static void write_address_map(const addrmap *addrmap, data_buf &addr_vec, cu_index_map &cu_index_htab)
void write_dwarf_index(dwarf2_per_bfd *per_bfd, const char *dir, const char *basename, const char *dwz_basename, dw_index_kind index_kind)
static symtab_index_entry & find_slot(struct mapped_symtab *symtab, const char *name)
#define DW2_GDB_INDEX_CU_SET_VALUE(cu_index, value)
Definition index-write.c:65
static void add_address_entry(data_buf &addr_vec, CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
void _initialize_dwarf_index_write()
static const gdb_byte dwarf5_gdb_augmentation[]
static void hash_expand(struct mapped_symtab *symtab)
static void write_cooked_index(cooked_index *table, const cu_index_map &cu_index_htab, struct mapped_symtab *symtab)
static void file_write(FILE *file, const void *data, size_t size)
Definition index-write.c:77
static bool check_dwarf64_offsets(dwarf2_per_bfd *per_bfd)
static void save_gdb_index_command(const char *arg, int from_tty)
static void write_gdbindex(dwarf2_per_bfd *per_bfd, cooked_index *table, FILE *out_file, FILE *dwz_out_file)
static void add_index_entry(struct mapped_symtab *symtab, const char *name, int is_static, gdb_index_symbol_kind kind, offset_type cu_index)
static void write_hash_table(mapped_symtab *symtab, data_buf &output, data_buf &cpool)
@ OBJF_NOT_FILENAME
const char * objfile_name(const struct objfile *objfile)
Definition objfiles.c:1259
struct program_space * current_program_space
Definition progspace.c:40
dw_index_kind
Definition public.h:29
dwarf2_per_objfile * get_dwarf2_per_objfile(struct objfile *objfile)
Definition read.c:165
cu_index_map & cu_index_htab
addrmap_index_data(data_buf &addr_vec_, cu_index_map &cu_index_htab_)
int operator()(CORE_ADDR start_addr, const void *obj)
CORE_ADDR previous_cu_start
unsigned int previous_cu_index
data_buf & addr_vec
int foreach(addrmap_foreach_const_fn fn) const
Definition addrmap.h:102
dwarf2_section_info str
Definition read.h:476
bfd * obfd
Definition read.h:462
gdb::array_view< dwarf2_per_cu_data_up > all_comp_units
Definition read.h:496
std::unique_ptr< dwarf_scanner_base > index_table
Definition read.h:533
gdb::array_view< dwarf2_per_cu_data_up > all_type_units
Definition read.h:497
std::vector< dwarf2_section_info > types
Definition read.h:488
std::vector< dwarf2_per_cu_data_up > all_units
Definition read.h:492
sect_offset sect_off
Definition read.h:120
unsigned int is_dwz
Definition read.h:135
unsigned int is_debug_types
Definition read.h:132
unsigned int length() const
Definition read.h:295
struct dwarf2_per_bfd * per_bfd
Definition read.h:728
const gdb_byte * buffer
Definition section.h:115
bfd_size_type size
Definition section.h:117
Definition dwz.h:32
const char * filename() const
Definition dwz.h:38
std::string filename
gdb_file_up out_file
index_wip_file(const char *dir, const char *basename, const char *suffix)
gdb::optional< gdb::unlinker > unlink_file
gdb::char_vector filename_temp
std::vector< symtab_index_entry > data
offset_type n_elements
auto_obstack m_string_obstack
objfile_flags flags
Definition objfiles.h:724
objfiles_range objfiles()
Definition progspace.h:209
ULONGEST signature
Definition read.h:388
cu_offset type_offset_in_tu
Definition read.h:393
offset_type index_offset
const char * name
std::vector< offset_type > cu_indices
void minimize()
Definition value.h:130
const char * main_name()
Definition symtab.c:6322
static bool tag_is_type(dwarf_tag tag)
Definition tag.h:28
#define gdb_stderr
Definition utils.h:187