Handle unaligned mapping of .gdb_index
[deliverable/binutils-gdb.git] / gdb / dwarf2 / index-write.c
CommitLineData
cd4fb1b2
SM
1/* DWARF index writing support for GDB.
2
3666a048 3 Copyright (C) 1994-2021 Free Software Foundation, Inc.
cd4fb1b2
SM
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
82ca8957 22#include "dwarf2/index-write.h"
23baa4cc 23
cd4fb1b2
SM
24#include "addrmap.h"
25#include "cli/cli-decode.h"
268a13a5
TT
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"
cd4fb1b2 31#include "complaints.h"
82ca8957 32#include "dwarf2/index-common.h"
cd4fb1b2 33#include "dwarf2.h"
82ca8957 34#include "dwarf2/read.h"
9fda78b6 35#include "dwarf2/dwz.h"
cd4fb1b2
SM
36#include "gdb/gdb-index.h"
37#include "gdbcmd.h"
38#include "objfiles.h"
39#include "psympriv.h"
3b00ef10 40#include "ada-lang.h"
cd4fb1b2 41
4de283e4
TT
42#include <algorithm>
43#include <cmath>
159ed7d9 44#include <forward_list>
4de283e4
TT
45#include <set>
46#include <unordered_map>
47#include <unordered_set>
48
cd4fb1b2
SM
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 \
dda83cd7 60 && (value) <= GDB_INDEX_SYMBOL_KIND_OTHER); \
cd4fb1b2
SM
61 GDB_INDEX_SYMBOL_KIND_SET_VALUE((cu_index), (value)); \
62 } while (0)
63
85102364 64/* Ensure we don't use more than the allotted number of bits for the CU. */
cd4fb1b2
SM
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{
1f88d0c8
SM
89 if (!vec.empty ())
90 file_write (file, vec.data (), vec.size () * sizeof (vec[0]));
cd4fb1b2
SM
91}
92
93/* In-memory buffer to prepare data to be written later to a file. */
94class data_buf
95{
96public:
42c2c694
TT
97 /* Copy ARRAY to the end of the buffer. */
98 void append_array (gdb::array_view<const gdb_byte> array)
cd4fb1b2 99 {
42c2c694 100 std::copy (array.begin (), array.end (), grow (array.size ()));
cd4fb1b2
SM
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;
42c2c694 120 m_vec.push_back (output);
cd4fb1b2
SM
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
42c2c694
TT
133 /* Copy VALUE to the end of the buffer, little-endian. */
134 void append_offset (offset_type value)
135 {
136 append_uint (sizeof (value), BFD_ENDIAN_LITTLE, value);
137 }
138
cd4fb1b2
SM
139 /* Return the size of the buffer. */
140 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);
b4be9bfd 163 return &*(m_vec.end () - size);
cd4fb1b2
SM
164 }
165
166 gdb::byte_vector m_vec;
167};
168
169/* An entry in the symbol table. */
170struct symtab_index_entry
171{
172 /* The name of the symbol. */
173 const char *name;
174 /* The offset of the name in the constant pool. */
175 offset_type index_offset;
176 /* A sorted vector of the indices of all the CUs that hold an object
177 of this name. */
178 std::vector<offset_type> cu_indices;
179};
180
181/* The symbol table. This is a power-of-2-sized hash table. */
182struct mapped_symtab
183{
184 mapped_symtab ()
185 {
186 data.resize (1024);
187 }
188
189 offset_type n_elements = 0;
190 std::vector<symtab_index_entry> data;
7ab96794
TV
191
192 /* Temporary storage for Ada names. */
193 auto_obstack m_string_obstack;
cd4fb1b2
SM
194};
195
196/* Find a slot in SYMTAB for the symbol NAME. Returns a reference to
197 the slot.
198
199 Function is used only during write_hash_table so no index format backward
200 compatibility is needed. */
201
202static symtab_index_entry &
203find_slot (struct mapped_symtab *symtab, const char *name)
204{
205 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
206
207 index = hash & (symtab->data.size () - 1);
208 step = ((hash * 17) & (symtab->data.size () - 1)) | 1;
209
210 for (;;)
211 {
212 if (symtab->data[index].name == NULL
213 || strcmp (name, symtab->data[index].name) == 0)
214 return symtab->data[index];
215 index = (index + step) & (symtab->data.size () - 1);
216 }
217}
218
219/* Expand SYMTAB's hash table. */
220
221static void
222hash_expand (struct mapped_symtab *symtab)
223{
224 auto old_entries = std::move (symtab->data);
225
226 symtab->data.clear ();
227 symtab->data.resize (old_entries.size () * 2);
228
229 for (auto &it : old_entries)
230 if (it.name != NULL)
231 {
232 auto &ref = find_slot (symtab, it.name);
233 ref = std::move (it);
234 }
235}
236
237/* Add an entry to SYMTAB. NAME is the name of the symbol.
238 CU_INDEX is the index of the CU in which the symbol appears.
239 IS_STATIC is one if the symbol is static, otherwise zero (global). */
240
241static void
242add_index_entry (struct mapped_symtab *symtab, const char *name,
243 int is_static, gdb_index_symbol_kind kind,
244 offset_type cu_index)
245{
246 offset_type cu_index_and_attrs;
247
248 ++symtab->n_elements;
249 if (4 * symtab->n_elements / 3 >= symtab->data.size ())
250 hash_expand (symtab);
251
252 symtab_index_entry &slot = find_slot (symtab, name);
253 if (slot.name == NULL)
254 {
255 slot.name = name;
256 /* index_offset is set later. */
257 }
258
259 cu_index_and_attrs = 0;
260 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
261 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
262 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
263
264 /* We don't want to record an index value twice as we want to avoid the
265 duplication.
266 We process all global symbols and then all static symbols
267 (which would allow us to avoid the duplication by only having to check
268 the last entry pushed), but a symbol could have multiple kinds in one CU.
269 To keep things simple we don't worry about the duplication here and
85102364 270 sort and uniquify the list after we've processed all symbols. */
cd4fb1b2
SM
271 slot.cu_indices.push_back (cu_index_and_attrs);
272}
273
274/* Sort and remove duplicates of all symbols' cu_indices lists. */
275
276static void
277uniquify_cu_indices (struct mapped_symtab *symtab)
278{
279 for (auto &entry : symtab->data)
280 {
281 if (entry.name != NULL && !entry.cu_indices.empty ())
282 {
283 auto &cu_indices = entry.cu_indices;
284 std::sort (cu_indices.begin (), cu_indices.end ());
285 auto from = std::unique (cu_indices.begin (), cu_indices.end ());
286 cu_indices.erase (from, cu_indices.end ());
287 }
288 }
289}
290
291/* A form of 'const char *' suitable for container keys. Only the
292 pointer is stored. The strings themselves are compared, not the
293 pointers. */
294class c_str_view
295{
296public:
297 c_str_view (const char *cstr)
298 : m_cstr (cstr)
299 {}
300
301 bool operator== (const c_str_view &other) const
302 {
303 return strcmp (m_cstr, other.m_cstr) == 0;
304 }
305
306 /* Return the underlying C string. Note, the returned string is
307 only a reference with lifetime of this object. */
308 const char *c_str () const
309 {
310 return m_cstr;
311 }
312
313private:
314 friend class c_str_view_hasher;
315 const char *const m_cstr;
316};
317
318/* A std::unordered_map::hasher for c_str_view that uses the right
319 hash function for strings in a mapped index. */
320class c_str_view_hasher
321{
322public:
323 size_t operator () (const c_str_view &x) const
324 {
325 return mapped_index_string_hash (INT_MAX, x.m_cstr);
326 }
327};
328
329/* A std::unordered_map::hasher for std::vector<>. */
330template<typename T>
331class vector_hasher
332{
333public:
334 size_t operator () (const std::vector<T> &key) const
335 {
336 return iterative_hash (key.data (),
337 sizeof (key.front ()) * key.size (), 0);
338 }
339};
340
341/* Write the mapped hash table SYMTAB to the data buffer OUTPUT, with
342 constant pool entries going into the data buffer CPOOL. */
343
344static void
345write_hash_table (mapped_symtab *symtab, data_buf &output, data_buf &cpool)
346{
347 {
348 /* Elements are sorted vectors of the indices of all the CUs that
349 hold an object of this name. */
350 std::unordered_map<std::vector<offset_type>, offset_type,
351 vector_hasher<offset_type>>
352 symbol_hash_table;
353
354 /* We add all the index vectors to the constant pool first, to
355 ensure alignment is ok. */
356 for (symtab_index_entry &entry : symtab->data)
357 {
358 if (entry.name == NULL)
359 continue;
360 gdb_assert (entry.index_offset == 0);
361
362 /* Finding before inserting is faster than always trying to
363 insert, because inserting always allocates a node, does the
364 lookup, and then destroys the new node if another node
365 already had the same key. C++17 try_emplace will avoid
366 this. */
367 const auto found
368 = symbol_hash_table.find (entry.cu_indices);
369 if (found != symbol_hash_table.end ())
370 {
371 entry.index_offset = found->second;
372 continue;
373 }
374
375 symbol_hash_table.emplace (entry.cu_indices, cpool.size ());
376 entry.index_offset = cpool.size ();
42c2c694 377 cpool.append_offset (entry.cu_indices.size ());
cd4fb1b2 378 for (const auto index : entry.cu_indices)
42c2c694 379 cpool.append_offset (index);
cd4fb1b2
SM
380 }
381 }
382
383 /* Now write out the hash table. */
384 std::unordered_map<c_str_view, offset_type, c_str_view_hasher> str_table;
385 for (const auto &entry : symtab->data)
386 {
387 offset_type str_off, vec_off;
388
389 if (entry.name != NULL)
390 {
391 const auto insertpair = str_table.emplace (entry.name, cpool.size ());
392 if (insertpair.second)
393 cpool.append_cstr0 (entry.name);
394 str_off = insertpair.first->second;
395 vec_off = entry.index_offset;
396 }
397 else
398 {
399 /* While 0 is a valid constant pool index, it is not valid
400 to have 0 for both offsets. */
401 str_off = 0;
402 vec_off = 0;
403 }
404
42c2c694
TT
405 output.append_offset (str_off);
406 output.append_offset (vec_off);
cd4fb1b2
SM
407 }
408}
409
edfe0a0c 410typedef std::unordered_map<partial_symtab *, unsigned int> psym_index_map;
cd4fb1b2
SM
411
412/* Helper struct for building the address table. */
413struct addrmap_index_data
414{
415 addrmap_index_data (data_buf &addr_vec_, psym_index_map &cu_index_htab_)
416 : addr_vec (addr_vec_), cu_index_htab (cu_index_htab_)
417 {}
418
cd4fb1b2
SM
419 data_buf &addr_vec;
420 psym_index_map &cu_index_htab;
421
422 /* Non-zero if the previous_* fields are valid.
423 We can't write an entry until we see the next entry (since it is only then
424 that we know the end of the entry). */
425 int previous_valid;
426 /* Index of the CU in the table of all CUs in the index file. */
427 unsigned int previous_cu_index;
428 /* Start address of the CU. */
429 CORE_ADDR previous_cu_start;
430};
431
432/* Write an address entry to ADDR_VEC. */
433
434static void
79cc99f6 435add_address_entry (data_buf &addr_vec,
cd4fb1b2
SM
436 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
437{
79748972
TT
438 addr_vec.append_uint (8, BFD_ENDIAN_LITTLE, start);
439 addr_vec.append_uint (8, BFD_ENDIAN_LITTLE, end);
42c2c694 440 addr_vec.append_offset (cu_index);
cd4fb1b2
SM
441}
442
443/* Worker function for traversing an addrmap to build the address table. */
444
445static int
446add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
447{
448 struct addrmap_index_data *data = (struct addrmap_index_data *) datap;
edfe0a0c 449 partial_symtab *pst = (partial_symtab *) obj;
cd4fb1b2
SM
450
451 if (data->previous_valid)
79cc99f6 452 add_address_entry (data->addr_vec,
cd4fb1b2
SM
453 data->previous_cu_start, start_addr,
454 data->previous_cu_index);
455
456 data->previous_cu_start = start_addr;
457 if (pst != NULL)
458 {
459 const auto it = data->cu_index_htab.find (pst);
460 gdb_assert (it != data->cu_index_htab.cend ());
461 data->previous_cu_index = it->second;
462 data->previous_valid = 1;
463 }
464 else
465 data->previous_valid = 0;
466
467 return 0;
468}
469
79cc99f6 470/* Write PER_BFD's address map to ADDR_VEC.
cd4fb1b2
SM
471 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
472 in the index file. */
473
474static void
79cc99f6 475write_address_map (dwarf2_per_bfd *per_bfd, data_buf &addr_vec,
cd4fb1b2
SM
476 psym_index_map &cu_index_htab)
477{
478 struct addrmap_index_data addrmap_index_data (addr_vec, cu_index_htab);
479
480 /* When writing the address table, we have to cope with the fact that
481 the addrmap iterator only provides the start of a region; we have to
482 wait until the next invocation to get the start of the next region. */
483
cd4fb1b2
SM
484 addrmap_index_data.previous_valid = 0;
485
79cc99f6 486 addrmap_foreach (per_bfd->partial_symtabs->psymtabs_addrmap,
d320c2b5 487 add_address_entry_worker, &addrmap_index_data);
cd4fb1b2
SM
488
489 /* It's highly unlikely the last entry (end address = 0xff...ff)
490 is valid, but we should still handle it.
491 The end address is recorded as the start of the next region, but that
492 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
493 anyway. */
494 if (addrmap_index_data.previous_valid)
79cc99f6 495 add_address_entry (addr_vec,
cd4fb1b2
SM
496 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
497 addrmap_index_data.previous_cu_index);
498}
499
500/* Return the symbol kind of PSYM. */
501
502static gdb_index_symbol_kind
503symbol_kind (struct partial_symbol *psym)
504{
8a6d4234
TT
505 domain_enum domain = psym->domain;
506 enum address_class aclass = psym->aclass;
cd4fb1b2
SM
507
508 switch (domain)
509 {
510 case VAR_DOMAIN:
511 switch (aclass)
512 {
513 case LOC_BLOCK:
514 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
515 case LOC_TYPEDEF:
516 return GDB_INDEX_SYMBOL_KIND_TYPE;
517 case LOC_COMPUTED:
518 case LOC_CONST_BYTES:
519 case LOC_OPTIMIZED_OUT:
520 case LOC_STATIC:
521 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
522 case LOC_CONST:
523 /* Note: It's currently impossible to recognize psyms as enum values
524 short of reading the type info. For now punt. */
525 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
526 default:
527 /* There are other LOC_FOO values that one might want to classify
528 as variables, but dwarf2read.c doesn't currently use them. */
529 return GDB_INDEX_SYMBOL_KIND_OTHER;
530 }
531 case STRUCT_DOMAIN:
532 return GDB_INDEX_SYMBOL_KIND_TYPE;
533 default:
534 return GDB_INDEX_SYMBOL_KIND_OTHER;
535 }
536}
537
538/* Add a list of partial symbols to SYMTAB. */
539
540static void
541write_psymbols (struct mapped_symtab *symtab,
542 std::unordered_set<partial_symbol *> &psyms_seen,
932539d7 543 const std::vector<partial_symbol *> &symbols,
cd4fb1b2
SM
544 offset_type cu_index,
545 int is_static)
546{
932539d7 547 for (partial_symbol *psym : symbols)
cd4fb1b2 548 {
7ab96794 549 const char *name = psym->ginfo.search_name ();
cd4fb1b2 550
c1b5c1eb 551 if (psym->ginfo.language () == language_ada)
7ab96794
TV
552 {
553 /* We want to ensure that the Ada main function's name appears
554 verbatim in the index. However, this name will be of the
555 form "_ada_mumble", and will be rewritten by ada_decode.
556 So, recognize it specially here and add it to the index by
557 hand. */
558 if (strcmp (main_name (), name) == 0)
559 {
560 gdb_index_symbol_kind kind = symbol_kind (psym);
561
562 add_index_entry (symtab, name, is_static, kind, cu_index);
563 }
564
565 /* In order for the index to work when read back into gdb, it
566 has to supply a funny form of the name: it should be the
567 encoded name, with any suffixes stripped. Using the
568 ordinary encoded name will not work properly with the
569 searching logic in find_name_components_bounds; nor will
570 using the decoded name. Furthermore, an Ada "verbatim"
571 name (of the form "<MumBle>") must be entered without the
572 angle brackets. Note that the current index is unusual,
573 see PR symtab/24820 for details. */
574 std::string decoded = ada_decode (name);
575 if (decoded[0] == '<')
576 name = (char *) obstack_copy0 (&symtab->m_string_obstack,
577 decoded.c_str () + 1,
578 decoded.length () - 2);
579 else
580 name = obstack_strdup (&symtab->m_string_obstack,
581 ada_encode (decoded.c_str ()));
582 }
cd4fb1b2
SM
583
584 /* Only add a given psymbol once. */
585 if (psyms_seen.insert (psym).second)
586 {
587 gdb_index_symbol_kind kind = symbol_kind (psym);
588
7ab96794 589 add_index_entry (symtab, name, is_static, kind, cu_index);
cd4fb1b2
SM
590 }
591 }
592}
593
594/* A helper struct used when iterating over debug_types. */
595struct signatured_type_index_data
596{
597 signatured_type_index_data (data_buf &types_list_,
dda83cd7 598 std::unordered_set<partial_symbol *> &psyms_seen_)
cd4fb1b2
SM
599 : types_list (types_list_), psyms_seen (psyms_seen_)
600 {}
601
602 struct objfile *objfile;
603 struct mapped_symtab *symtab;
604 data_buf &types_list;
605 std::unordered_set<partial_symbol *> &psyms_seen;
606 int cu_index;
607};
608
609/* A helper function that writes a single signatured_type to an
610 obstack. */
611
612static int
613write_one_signatured_type (void **slot, void *d)
614{
615 struct signatured_type_index_data *info
616 = (struct signatured_type_index_data *) d;
617 struct signatured_type *entry = (struct signatured_type *) *slot;
edfe0a0c 618 partial_symtab *psymtab = entry->per_cu.v.psymtab;
cd4fb1b2 619
2bd3e4b8
TV
620 if (psymtab == nullptr)
621 {
622 /* We can end up here when processing a skeleton CU referring to a
623 .dwo file that hasn't been found. There's not much we can do in
624 such a case, so skip this CU. */
625 return 1;
626 }
627
932539d7
TT
628 write_psymbols (info->symtab, info->psyms_seen,
629 psymtab->global_psymbols, info->cu_index,
cd4fb1b2 630 0);
932539d7
TT
631 write_psymbols (info->symtab, info->psyms_seen,
632 psymtab->static_psymbols, info->cu_index,
cd4fb1b2
SM
633 1);
634
635 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE,
636 to_underlying (entry->per_cu.sect_off));
637 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE,
638 to_underlying (entry->type_offset_in_tu));
639 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE, entry->signature);
640
641 ++info->cu_index;
642
643 return 1;
644}
645
646/* Recurse into all "included" dependencies and count their symbols as
647 if they appeared in this psymtab. */
648
649static void
edfe0a0c 650recursively_count_psymbols (partial_symtab *psymtab,
cd4fb1b2
SM
651 size_t &psyms_seen)
652{
653 for (int i = 0; i < psymtab->number_of_dependencies; ++i)
654 if (psymtab->dependencies[i]->user != NULL)
edfe0a0c 655 recursively_count_psymbols (psymtab->dependencies[i],
cd4fb1b2
SM
656 psyms_seen);
657
932539d7
TT
658 psyms_seen += psymtab->global_psymbols.size ();
659 psyms_seen += psymtab->static_psymbols.size ();
cd4fb1b2
SM
660}
661
662/* Recurse into all "included" dependencies and write their symbols as
663 if they appeared in this psymtab. */
664
665static void
666recursively_write_psymbols (struct objfile *objfile,
edfe0a0c 667 partial_symtab *psymtab,
cd4fb1b2
SM
668 struct mapped_symtab *symtab,
669 std::unordered_set<partial_symbol *> &psyms_seen,
670 offset_type cu_index)
671{
672 int i;
673
674 for (i = 0; i < psymtab->number_of_dependencies; ++i)
675 if (psymtab->dependencies[i]->user != NULL)
891813be 676 recursively_write_psymbols (objfile,
edfe0a0c 677 psymtab->dependencies[i],
cd4fb1b2
SM
678 symtab, psyms_seen, cu_index);
679
932539d7
TT
680 write_psymbols (symtab, psyms_seen,
681 psymtab->global_psymbols, cu_index,
cd4fb1b2 682 0);
932539d7
TT
683 write_psymbols (symtab, psyms_seen,
684 psymtab->static_psymbols, cu_index,
cd4fb1b2
SM
685 1);
686}
687
688/* DWARF-5 .debug_names builder. */
689class debug_names
690{
691public:
976ca316 692 debug_names (dwarf2_per_objfile *per_objfile, bool is_dwarf64,
cd4fb1b2
SM
693 bfd_endian dwarf5_byte_order)
694 : m_dwarf5_byte_order (dwarf5_byte_order),
695 m_dwarf32 (dwarf5_byte_order),
696 m_dwarf64 (dwarf5_byte_order),
697 m_dwarf (is_dwarf64
698 ? static_cast<dwarf &> (m_dwarf64)
699 : static_cast<dwarf &> (m_dwarf32)),
700 m_name_table_string_offs (m_dwarf.name_table_string_offs),
701 m_name_table_entry_offs (m_dwarf.name_table_entry_offs),
976ca316 702 m_debugstrlookup (per_objfile)
cd4fb1b2
SM
703 {}
704
705 int dwarf5_offset_size () const
706 {
707 const bool dwarf5_is_dwarf64 = &m_dwarf == &m_dwarf64;
708 return dwarf5_is_dwarf64 ? 8 : 4;
709 }
710
711 /* Is this symbol from DW_TAG_compile_unit or DW_TAG_type_unit? */
712 enum class unit_kind { cu, tu };
713
714 /* Insert one symbol. */
715 void insert (const partial_symbol *psym, int cu_index, bool is_static,
716 unit_kind kind)
717 {
718 const int dwarf_tag = psymbol_tag (psym);
719 if (dwarf_tag == 0)
720 return;
c9d95fa3 721 const char *name = psym->ginfo.search_name ();
3b00ef10 722
c1b5c1eb 723 if (psym->ginfo.language () == language_ada)
3b00ef10
TT
724 {
725 /* We want to ensure that the Ada main function's name appears
726 verbatim in the index. However, this name will be of the
727 form "_ada_mumble", and will be rewritten by ada_decode.
728 So, recognize it specially here and add it to the index by
729 hand. */
730 if (strcmp (main_name (), name) == 0)
731 {
732 const auto insertpair
733 = m_name_to_value_set.emplace (c_str_view (name),
734 std::set<symbol_value> ());
735 std::set<symbol_value> &value_set = insertpair.first->second;
736 value_set.emplace (symbol_value (dwarf_tag, cu_index, is_static,
737 kind));
738 }
739
740 /* In order for the index to work when read back into gdb, it
741 has to supply a funny form of the name: it should be the
742 encoded name, with any suffixes stripped. Using the
743 ordinary encoded name will not work properly with the
744 searching logic in find_name_components_bounds; nor will
745 using the decoded name. Furthermore, an Ada "verbatim"
746 name (of the form "<MumBle>") must be entered without the
747 angle brackets. Note that the current index is unusual,
748 see PR symtab/24820 for details. */
f945dedf 749 std::string decoded = ada_decode (name);
3b00ef10
TT
750 if (decoded[0] == '<')
751 name = (char *) obstack_copy0 (&m_string_obstack,
f945dedf
CB
752 decoded.c_str () + 1,
753 decoded.length () - 2);
3b00ef10 754 else
f945dedf
CB
755 name = obstack_strdup (&m_string_obstack,
756 ada_encode (decoded.c_str ()));
3b00ef10
TT
757 }
758
cd4fb1b2
SM
759 const auto insertpair
760 = m_name_to_value_set.emplace (c_str_view (name),
761 std::set<symbol_value> ());
762 std::set<symbol_value> &value_set = insertpair.first->second;
763 value_set.emplace (symbol_value (dwarf_tag, cu_index, is_static, kind));
764 }
765
766 /* Build all the tables. All symbols must be already inserted.
767 This function does not call file_write, caller has to do it
768 afterwards. */
769 void build ()
770 {
771 /* Verify the build method has not be called twice. */
772 gdb_assert (m_abbrev_table.empty ());
773 const size_t name_count = m_name_to_value_set.size ();
774 m_bucket_table.resize
775 (std::pow (2, std::ceil (std::log2 (name_count * 4 / 3))));
776 m_hash_table.reserve (name_count);
777 m_name_table_string_offs.reserve (name_count);
778 m_name_table_entry_offs.reserve (name_count);
779
780 /* Map each hash of symbol to its name and value. */
781 struct hash_it_pair
782 {
783 uint32_t hash;
784 decltype (m_name_to_value_set)::const_iterator it;
785 };
786 std::vector<std::forward_list<hash_it_pair>> bucket_hash;
787 bucket_hash.resize (m_bucket_table.size ());
788 for (decltype (m_name_to_value_set)::const_iterator it
789 = m_name_to_value_set.cbegin ();
790 it != m_name_to_value_set.cend ();
791 ++it)
792 {
793 const char *const name = it->first.c_str ();
794 const uint32_t hash = dwarf5_djb_hash (name);
795 hash_it_pair hashitpair;
796 hashitpair.hash = hash;
797 hashitpair.it = it;
798 auto &slot = bucket_hash[hash % bucket_hash.size()];
799 slot.push_front (std::move (hashitpair));
800 }
801 for (size_t bucket_ix = 0; bucket_ix < bucket_hash.size (); ++bucket_ix)
802 {
803 const std::forward_list<hash_it_pair> &hashitlist
804 = bucket_hash[bucket_ix];
805 if (hashitlist.empty ())
806 continue;
807 uint32_t &bucket_slot = m_bucket_table[bucket_ix];
808 /* The hashes array is indexed starting at 1. */
809 store_unsigned_integer (reinterpret_cast<gdb_byte *> (&bucket_slot),
810 sizeof (bucket_slot), m_dwarf5_byte_order,
811 m_hash_table.size () + 1);
812 for (const hash_it_pair &hashitpair : hashitlist)
813 {
814 m_hash_table.push_back (0);
815 store_unsigned_integer (reinterpret_cast<gdb_byte *>
816 (&m_hash_table.back ()),
817 sizeof (m_hash_table.back ()),
818 m_dwarf5_byte_order, hashitpair.hash);
819 const c_str_view &name = hashitpair.it->first;
820 const std::set<symbol_value> &value_set = hashitpair.it->second;
821 m_name_table_string_offs.push_back_reorder
822 (m_debugstrlookup.lookup (name.c_str ()));
823 m_name_table_entry_offs.push_back_reorder (m_entry_pool.size ());
824 gdb_assert (!value_set.empty ());
825 for (const symbol_value &value : value_set)
826 {
827 int &idx = m_indexkey_to_idx[index_key (value.dwarf_tag,
828 value.is_static,
829 value.kind)];
830 if (idx == 0)
831 {
832 idx = m_idx_next++;
833 m_abbrev_table.append_unsigned_leb128 (idx);
834 m_abbrev_table.append_unsigned_leb128 (value.dwarf_tag);
835 m_abbrev_table.append_unsigned_leb128
836 (value.kind == unit_kind::cu ? DW_IDX_compile_unit
837 : DW_IDX_type_unit);
838 m_abbrev_table.append_unsigned_leb128 (DW_FORM_udata);
839 m_abbrev_table.append_unsigned_leb128 (value.is_static
840 ? DW_IDX_GNU_internal
841 : DW_IDX_GNU_external);
842 m_abbrev_table.append_unsigned_leb128 (DW_FORM_flag_present);
843
844 /* Terminate attributes list. */
845 m_abbrev_table.append_unsigned_leb128 (0);
846 m_abbrev_table.append_unsigned_leb128 (0);
847 }
848
849 m_entry_pool.append_unsigned_leb128 (idx);
850 m_entry_pool.append_unsigned_leb128 (value.cu_index);
851 }
852
853 /* Terminate the list of CUs. */
854 m_entry_pool.append_unsigned_leb128 (0);
855 }
856 }
857 gdb_assert (m_hash_table.size () == name_count);
858
859 /* Terminate tags list. */
860 m_abbrev_table.append_unsigned_leb128 (0);
861 }
862
863 /* Return .debug_names bucket count. This must be called only after
864 calling the build method. */
865 uint32_t bucket_count () const
866 {
867 /* Verify the build method has been already called. */
868 gdb_assert (!m_abbrev_table.empty ());
869 const uint32_t retval = m_bucket_table.size ();
870
871 /* Check for overflow. */
872 gdb_assert (retval == m_bucket_table.size ());
873 return retval;
874 }
875
876 /* Return .debug_names names count. This must be called only after
877 calling the build method. */
878 uint32_t name_count () const
879 {
880 /* Verify the build method has been already called. */
881 gdb_assert (!m_abbrev_table.empty ());
882 const uint32_t retval = m_hash_table.size ();
883
884 /* Check for overflow. */
885 gdb_assert (retval == m_hash_table.size ());
886 return retval;
887 }
888
889 /* Return number of bytes of .debug_names abbreviation table. This
890 must be called only after calling the build method. */
891 uint32_t abbrev_table_bytes () const
892 {
893 gdb_assert (!m_abbrev_table.empty ());
894 return m_abbrev_table.size ();
895 }
896
897 /* Recurse into all "included" dependencies and store their symbols
898 as if they appeared in this psymtab. */
899 void recursively_write_psymbols
900 (struct objfile *objfile,
edfe0a0c 901 partial_symtab *psymtab,
cd4fb1b2
SM
902 std::unordered_set<partial_symbol *> &psyms_seen,
903 int cu_index)
904 {
905 for (int i = 0; i < psymtab->number_of_dependencies; ++i)
906 if (psymtab->dependencies[i]->user != NULL)
891813be 907 recursively_write_psymbols
edfe0a0c 908 (objfile, psymtab->dependencies[i], psyms_seen, cu_index);
cd4fb1b2 909
932539d7
TT
910 write_psymbols (psyms_seen, psymtab->global_psymbols,
911 cu_index, false, unit_kind::cu);
912 write_psymbols (psyms_seen, psymtab->static_psymbols,
913 cu_index, true, unit_kind::cu);
cd4fb1b2
SM
914 }
915
916 /* Return number of bytes the .debug_names section will have. This
917 must be called only after calling the build method. */
918 size_t bytes () const
919 {
920 /* Verify the build method has been already called. */
921 gdb_assert (!m_abbrev_table.empty ());
922 size_t expected_bytes = 0;
923 expected_bytes += m_bucket_table.size () * sizeof (m_bucket_table[0]);
924 expected_bytes += m_hash_table.size () * sizeof (m_hash_table[0]);
925 expected_bytes += m_name_table_string_offs.bytes ();
926 expected_bytes += m_name_table_entry_offs.bytes ();
927 expected_bytes += m_abbrev_table.size ();
928 expected_bytes += m_entry_pool.size ();
929 return expected_bytes;
930 }
931
932 /* Write .debug_names to FILE_NAMES and .debug_str addition to
933 FILE_STR. This must be called only after calling the build
934 method. */
935 void file_write (FILE *file_names, FILE *file_str) const
936 {
937 /* Verify the build method has been already called. */
938 gdb_assert (!m_abbrev_table.empty ());
939 ::file_write (file_names, m_bucket_table);
940 ::file_write (file_names, m_hash_table);
941 m_name_table_string_offs.file_write (file_names);
942 m_name_table_entry_offs.file_write (file_names);
943 m_abbrev_table.file_write (file_names);
944 m_entry_pool.file_write (file_names);
945 m_debugstrlookup.file_write (file_str);
946 }
947
948 /* A helper user data for write_one_signatured_type. */
949 class write_one_signatured_type_data
950 {
951 public:
952 write_one_signatured_type_data (debug_names &nametable_,
dda83cd7 953 signatured_type_index_data &&info_)
cd4fb1b2
SM
954 : nametable (nametable_), info (std::move (info_))
955 {}
956 debug_names &nametable;
957 struct signatured_type_index_data info;
958 };
959
960 /* A helper function to pass write_one_signatured_type to
961 htab_traverse_noresize. */
962 static int
963 write_one_signatured_type (void **slot, void *d)
964 {
965 write_one_signatured_type_data *data = (write_one_signatured_type_data *) d;
966 struct signatured_type_index_data *info = &data->info;
967 struct signatured_type *entry = (struct signatured_type *) *slot;
968
969 data->nametable.write_one_signatured_type (entry, info);
970
971 return 1;
972 }
973
974private:
975
976 /* Storage for symbol names mapping them to their .debug_str section
977 offsets. */
978 class debug_str_lookup
979 {
980 public:
981
30baf67b 982 /* Object constructor to be called for current DWARF2_PER_OBJFILE.
cd4fb1b2 983 All .debug_str section strings are automatically stored. */
976ca316
SM
984 debug_str_lookup (dwarf2_per_objfile *per_objfile)
985 : m_abfd (per_objfile->objfile->obfd),
986 m_per_objfile (per_objfile)
cd4fb1b2 987 {
976ca316
SM
988 per_objfile->per_bfd->str.read (per_objfile->objfile);
989 if (per_objfile->per_bfd->str.buffer == NULL)
cd4fb1b2 990 return;
976ca316
SM
991 for (const gdb_byte *data = per_objfile->per_bfd->str.buffer;
992 data < (per_objfile->per_bfd->str.buffer
993 + per_objfile->per_bfd->str.size);)
cd4fb1b2
SM
994 {
995 const char *const s = reinterpret_cast<const char *> (data);
996 const auto insertpair
997 = m_str_table.emplace (c_str_view (s),
976ca316 998 data - per_objfile->per_bfd->str.buffer);
cd4fb1b2 999 if (!insertpair.second)
b98664d3 1000 complaint (_("Duplicate string \"%s\" in "
cd4fb1b2
SM
1001 ".debug_str section [in module %s]"),
1002 s, bfd_get_filename (m_abfd));
1003 data += strlen (s) + 1;
1004 }
1005 }
1006
1007 /* Return offset of symbol name S in the .debug_str section. Add
1008 such symbol to the section's end if it does not exist there
1009 yet. */
1010 size_t lookup (const char *s)
1011 {
1012 const auto it = m_str_table.find (c_str_view (s));
1013 if (it != m_str_table.end ())
1014 return it->second;
976ca316 1015 const size_t offset = (m_per_objfile->per_bfd->str.size
cd4fb1b2
SM
1016 + m_str_add_buf.size ());
1017 m_str_table.emplace (c_str_view (s), offset);
1018 m_str_add_buf.append_cstr0 (s);
1019 return offset;
1020 }
1021
1022 /* Append the end of the .debug_str section to FILE. */
1023 void file_write (FILE *file) const
1024 {
1025 m_str_add_buf.file_write (file);
1026 }
1027
1028 private:
1029 std::unordered_map<c_str_view, size_t, c_str_view_hasher> m_str_table;
1030 bfd *const m_abfd;
976ca316 1031 dwarf2_per_objfile *m_per_objfile;
cd4fb1b2
SM
1032
1033 /* Data to add at the end of .debug_str for new needed symbol names. */
1034 data_buf m_str_add_buf;
1035 };
1036
1037 /* Container to map used DWARF tags to their .debug_names abbreviation
1038 tags. */
1039 class index_key
1040 {
1041 public:
1042 index_key (int dwarf_tag_, bool is_static_, unit_kind kind_)
1043 : dwarf_tag (dwarf_tag_), is_static (is_static_), kind (kind_)
1044 {
1045 }
1046
1047 bool
1048 operator== (const index_key &other) const
1049 {
1050 return (dwarf_tag == other.dwarf_tag && is_static == other.is_static
1051 && kind == other.kind);
1052 }
1053
1054 const int dwarf_tag;
1055 const bool is_static;
1056 const unit_kind kind;
1057 };
1058
1059 /* Provide std::unordered_map::hasher for index_key. */
1060 class index_key_hasher
1061 {
1062 public:
1063 size_t
1064 operator () (const index_key &key) const
1065 {
1066 return (std::hash<int>() (key.dwarf_tag) << 1) | key.is_static;
1067 }
1068 };
1069
1070 /* Parameters of one symbol entry. */
1071 class symbol_value
1072 {
1073 public:
1074 const int dwarf_tag, cu_index;
1075 const bool is_static;
1076 const unit_kind kind;
1077
1078 symbol_value (int dwarf_tag_, int cu_index_, bool is_static_,
1079 unit_kind kind_)
1080 : dwarf_tag (dwarf_tag_), cu_index (cu_index_), is_static (is_static_),
dda83cd7 1081 kind (kind_)
cd4fb1b2
SM
1082 {}
1083
1084 bool
1085 operator< (const symbol_value &other) const
1086 {
1087#define X(n) \
1088 do \
1089 { \
1090 if (n < other.n) \
1091 return true; \
1092 if (n > other.n) \
1093 return false; \
1094 } \
1095 while (0)
1096 X (dwarf_tag);
1097 X (is_static);
1098 X (kind);
1099 X (cu_index);
1100#undef X
1101 return false;
1102 }
1103 };
1104
1105 /* Abstract base class to unify DWARF-32 and DWARF-64 name table
1106 output. */
1107 class offset_vec
1108 {
1109 protected:
1110 const bfd_endian dwarf5_byte_order;
1111 public:
1112 explicit offset_vec (bfd_endian dwarf5_byte_order_)
1113 : dwarf5_byte_order (dwarf5_byte_order_)
1114 {}
1115
1116 /* Call std::vector::reserve for NELEM elements. */
1117 virtual void reserve (size_t nelem) = 0;
1118
1119 /* Call std::vector::push_back with store_unsigned_integer byte
1120 reordering for ELEM. */
1121 virtual void push_back_reorder (size_t elem) = 0;
1122
1123 /* Return expected output size in bytes. */
1124 virtual size_t bytes () const = 0;
1125
1126 /* Write name table to FILE. */
1127 virtual void file_write (FILE *file) const = 0;
1128 };
1129
1130 /* Template to unify DWARF-32 and DWARF-64 output. */
1131 template<typename OffsetSize>
1132 class offset_vec_tmpl : public offset_vec
1133 {
1134 public:
1135 explicit offset_vec_tmpl (bfd_endian dwarf5_byte_order_)
1136 : offset_vec (dwarf5_byte_order_)
1137 {}
1138
1139 /* Implement offset_vec::reserve. */
1140 void reserve (size_t nelem) override
1141 {
1142 m_vec.reserve (nelem);
1143 }
1144
1145 /* Implement offset_vec::push_back_reorder. */
1146 void push_back_reorder (size_t elem) override
1147 {
1148 m_vec.push_back (elem);
1149 /* Check for overflow. */
1150 gdb_assert (m_vec.back () == elem);
1151 store_unsigned_integer (reinterpret_cast<gdb_byte *> (&m_vec.back ()),
1152 sizeof (m_vec.back ()), dwarf5_byte_order, elem);
1153 }
1154
1155 /* Implement offset_vec::bytes. */
1156 size_t bytes () const override
1157 {
1158 return m_vec.size () * sizeof (m_vec[0]);
1159 }
1160
1161 /* Implement offset_vec::file_write. */
1162 void file_write (FILE *file) const override
1163 {
1164 ::file_write (file, m_vec);
1165 }
1166
1167 private:
1168 std::vector<OffsetSize> m_vec;
1169 };
1170
1171 /* Base class to unify DWARF-32 and DWARF-64 .debug_names output
1172 respecting name table width. */
1173 class dwarf
1174 {
1175 public:
1176 offset_vec &name_table_string_offs, &name_table_entry_offs;
1177
1178 dwarf (offset_vec &name_table_string_offs_,
1179 offset_vec &name_table_entry_offs_)
1180 : name_table_string_offs (name_table_string_offs_),
1181 name_table_entry_offs (name_table_entry_offs_)
1182 {
1183 }
1184 };
1185
1186 /* Template to unify DWARF-32 and DWARF-64 .debug_names output
1187 respecting name table width. */
1188 template<typename OffsetSize>
1189 class dwarf_tmpl : public dwarf
1190 {
1191 public:
1192 explicit dwarf_tmpl (bfd_endian dwarf5_byte_order_)
1193 : dwarf (m_name_table_string_offs, m_name_table_entry_offs),
1194 m_name_table_string_offs (dwarf5_byte_order_),
1195 m_name_table_entry_offs (dwarf5_byte_order_)
1196 {}
1197
1198 private:
1199 offset_vec_tmpl<OffsetSize> m_name_table_string_offs;
1200 offset_vec_tmpl<OffsetSize> m_name_table_entry_offs;
1201 };
1202
1203 /* Try to reconstruct original DWARF tag for given partial_symbol.
1204 This function is not DWARF-5 compliant but it is sufficient for
1205 GDB as a DWARF-5 index consumer. */
1206 static int psymbol_tag (const struct partial_symbol *psym)
1207 {
8a6d4234
TT
1208 domain_enum domain = psym->domain;
1209 enum address_class aclass = psym->aclass;
cd4fb1b2
SM
1210
1211 switch (domain)
1212 {
1213 case VAR_DOMAIN:
1214 switch (aclass)
1215 {
1216 case LOC_BLOCK:
1217 return DW_TAG_subprogram;
1218 case LOC_TYPEDEF:
1219 return DW_TAG_typedef;
1220 case LOC_COMPUTED:
1221 case LOC_CONST_BYTES:
1222 case LOC_OPTIMIZED_OUT:
1223 case LOC_STATIC:
1224 return DW_TAG_variable;
1225 case LOC_CONST:
1226 /* Note: It's currently impossible to recognize psyms as enum values
1227 short of reading the type info. For now punt. */
1228 return DW_TAG_variable;
1229 default:
1230 /* There are other LOC_FOO values that one might want to classify
1231 as variables, but dwarf2read.c doesn't currently use them. */
1232 return DW_TAG_variable;
1233 }
1234 case STRUCT_DOMAIN:
1235 return DW_TAG_structure_type;
7666722f
TV
1236 case MODULE_DOMAIN:
1237 return DW_TAG_module;
cd4fb1b2
SM
1238 default:
1239 return 0;
1240 }
1241 }
1242
1243 /* Call insert for all partial symbols and mark them in PSYMS_SEEN. */
1244 void write_psymbols (std::unordered_set<partial_symbol *> &psyms_seen,
932539d7
TT
1245 const std::vector<partial_symbol *> &symbols,
1246 int cu_index, bool is_static, unit_kind kind)
cd4fb1b2 1247 {
932539d7 1248 for (partial_symbol *psym : symbols)
cd4fb1b2 1249 {
cd4fb1b2
SM
1250 /* Only add a given psymbol once. */
1251 if (psyms_seen.insert (psym).second)
1252 insert (psym, cu_index, is_static, kind);
1253 }
1254 }
1255
1256 /* A helper function that writes a single signatured_type
1257 to a debug_names. */
1258 void
1259 write_one_signatured_type (struct signatured_type *entry,
1260 struct signatured_type_index_data *info)
1261 {
edfe0a0c 1262 partial_symtab *psymtab = entry->per_cu.v.psymtab;
cd4fb1b2 1263
932539d7
TT
1264 write_psymbols (info->psyms_seen, psymtab->global_psymbols,
1265 info->cu_index, false, unit_kind::tu);
1266 write_psymbols (info->psyms_seen, psymtab->static_psymbols,
1267 info->cu_index, true, unit_kind::tu);
cd4fb1b2
SM
1268
1269 info->types_list.append_uint (dwarf5_offset_size (), m_dwarf5_byte_order,
1270 to_underlying (entry->per_cu.sect_off));
1271
1272 ++info->cu_index;
1273 }
1274
1275 /* Store value of each symbol. */
1276 std::unordered_map<c_str_view, std::set<symbol_value>, c_str_view_hasher>
1277 m_name_to_value_set;
1278
1279 /* Tables of DWARF-5 .debug_names. They are in object file byte
1280 order. */
1281 std::vector<uint32_t> m_bucket_table;
1282 std::vector<uint32_t> m_hash_table;
1283
1284 const bfd_endian m_dwarf5_byte_order;
1285 dwarf_tmpl<uint32_t> m_dwarf32;
1286 dwarf_tmpl<uint64_t> m_dwarf64;
1287 dwarf &m_dwarf;
1288 offset_vec &m_name_table_string_offs, &m_name_table_entry_offs;
1289 debug_str_lookup m_debugstrlookup;
1290
1291 /* Map each used .debug_names abbreviation tag parameter to its
1292 index value. */
1293 std::unordered_map<index_key, int, index_key_hasher> m_indexkey_to_idx;
1294
1295 /* Next unused .debug_names abbreviation tag for
1296 m_indexkey_to_idx. */
1297 int m_idx_next = 1;
1298
1299 /* .debug_names abbreviation table. */
1300 data_buf m_abbrev_table;
1301
1302 /* .debug_names entry pool. */
1303 data_buf m_entry_pool;
3b00ef10
TT
1304
1305 /* Temporary storage for Ada names. */
1306 auto_obstack m_string_obstack;
cd4fb1b2
SM
1307};
1308
1309/* Return iff any of the needed offsets does not fit into 32-bit
1310 .debug_names section. */
1311
1312static bool
976ca316 1313check_dwarf64_offsets (dwarf2_per_objfile *per_objfile)
cd4fb1b2 1314{
976ca316 1315 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
cd4fb1b2 1316 {
b76e467d 1317 if (to_underlying (per_cu->sect_off) >= (static_cast<uint64_t> (1) << 32))
cd4fb1b2
SM
1318 return true;
1319 }
976ca316 1320 for (const signatured_type *sigtype : per_objfile->per_bfd->all_type_units)
cd4fb1b2 1321 {
b2bdb8cf 1322 const dwarf2_per_cu_data &per_cu = sigtype->per_cu;
cd4fb1b2
SM
1323
1324 if (to_underlying (per_cu.sect_off) >= (static_cast<uint64_t> (1) << 32))
1325 return true;
1326 }
1327 return false;
1328}
1329
1330/* The psyms_seen set is potentially going to be largish (~40k
1331 elements when indexing a -g3 build of GDB itself). Estimate the
1332 number of elements in order to avoid too many rehashes, which
1333 require rebuilding buckets and thus many trips to
1334 malloc/free. */
1335
1336static size_t
976ca316 1337psyms_seen_size (dwarf2_per_objfile *per_objfile)
cd4fb1b2
SM
1338{
1339 size_t psyms_count = 0;
976ca316 1340 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
cd4fb1b2 1341 {
edfe0a0c 1342 partial_symtab *psymtab = per_cu->v.psymtab;
cd4fb1b2
SM
1343
1344 if (psymtab != NULL && psymtab->user == NULL)
1345 recursively_count_psymbols (psymtab, psyms_count);
1346 }
1347 /* Generating an index for gdb itself shows a ratio of
1348 TOTAL_SEEN_SYMS/UNIQUE_SYMS or ~5. 4 seems like a good bet. */
1349 return psyms_count / 4;
1350}
1351
c4973306
SM
1352/* Assert that FILE's size is EXPECTED_SIZE. Assumes file's seek
1353 position is at the end of the file. */
cd4fb1b2 1354
c4973306
SM
1355static void
1356assert_file_size (FILE *file, size_t expected_size)
1357{
1358 const auto file_size = ftell (file);
1359 if (file_size == -1)
1360 perror_with_name (("ftell"));
1361 gdb_assert (file_size == expected_size);
1362}
1363
1364/* Write a gdb index file to OUT_FILE from all the sections passed as
1365 arguments. */
1366
1367static void
1368write_gdbindex_1 (FILE *out_file,
1369 const data_buf &cu_list,
1370 const data_buf &types_cu_list,
1371 const data_buf &addr_vec,
1372 const data_buf &symtab_vec,
1373 const data_buf &constant_pool)
1374{
1375 data_buf contents;
1376 const offset_type size_of_header = 6 * sizeof (offset_type);
1377 offset_type total_len = size_of_header;
1378
1379 /* The version number. */
42c2c694 1380 contents.append_offset (8);
c4973306
SM
1381
1382 /* The offset of the CU list from the start of the file. */
42c2c694 1383 contents.append_offset (total_len);
c4973306
SM
1384 total_len += cu_list.size ();
1385
1386 /* The offset of the types CU list from the start of the file. */
42c2c694 1387 contents.append_offset (total_len);
c4973306
SM
1388 total_len += types_cu_list.size ();
1389
1390 /* The offset of the address table from the start of the file. */
42c2c694 1391 contents.append_offset (total_len);
c4973306
SM
1392 total_len += addr_vec.size ();
1393
1394 /* The offset of the symbol table from the start of the file. */
42c2c694 1395 contents.append_offset (total_len);
c4973306
SM
1396 total_len += symtab_vec.size ();
1397
1398 /* The offset of the constant pool from the start of the file. */
42c2c694 1399 contents.append_offset (total_len);
c4973306
SM
1400 total_len += constant_pool.size ();
1401
1402 gdb_assert (contents.size () == size_of_header);
1403
1404 contents.file_write (out_file);
1405 cu_list.file_write (out_file);
1406 types_cu_list.file_write (out_file);
1407 addr_vec.file_write (out_file);
1408 symtab_vec.file_write (out_file);
1409 constant_pool.file_write (out_file);
1410
1411 assert_file_size (out_file, total_len);
1412}
1413
1414/* Write contents of a .gdb_index section for OBJFILE into OUT_FILE.
1415 If OBJFILE has an associated dwz file, write contents of a .gdb_index
1416 section for that dwz file into DWZ_OUT_FILE. If OBJFILE does not have an
1417 associated dwz file, DWZ_OUT_FILE must be NULL. */
1418
1419static void
976ca316 1420write_gdbindex (dwarf2_per_objfile *per_objfile, FILE *out_file,
c4973306 1421 FILE *dwz_out_file)
cd4fb1b2 1422{
976ca316 1423 struct objfile *objfile = per_objfile->objfile;
cd4fb1b2 1424 mapped_symtab symtab;
c4973306
SM
1425 data_buf objfile_cu_list;
1426 data_buf dwz_cu_list;
cd4fb1b2
SM
1427
1428 /* While we're scanning CU's create a table that maps a psymtab pointer
1429 (which is what addrmap records) to its index (which is what is recorded
1430 in the index file). This will later be needed to write the address
1431 table. */
1432 psym_index_map cu_index_htab;
976ca316 1433 cu_index_htab.reserve (per_objfile->per_bfd->all_comp_units.size ());
cd4fb1b2
SM
1434
1435 /* The CU list is already sorted, so we don't need to do additional
1436 work here. Also, the debug_types entries do not appear in
1437 all_comp_units, but only in their own hash table. */
1438
1439 std::unordered_set<partial_symbol *> psyms_seen
976ca316
SM
1440 (psyms_seen_size (per_objfile));
1441 for (int i = 0; i < per_objfile->per_bfd->all_comp_units.size (); ++i)
cd4fb1b2 1442 {
976ca316 1443 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->all_comp_units[i];
edfe0a0c 1444 partial_symtab *psymtab = per_cu->v.psymtab;
cd4fb1b2 1445
efba5c23
TV
1446 if (psymtab != NULL)
1447 {
1448 if (psymtab->user == NULL)
1449 recursively_write_psymbols (objfile, psymtab, &symtab,
1450 psyms_seen, i);
1451
1452 const auto insertpair = cu_index_htab.emplace (psymtab, i);
1453 gdb_assert (insertpair.second);
1454 }
cd4fb1b2 1455
c4973306
SM
1456 /* The all_comp_units list contains CUs read from the objfile as well as
1457 from the eventual dwz file. We need to place the entry in the
1458 corresponding index. */
1459 data_buf &cu_list = per_cu->is_dwz ? dwz_cu_list : objfile_cu_list;
cd4fb1b2
SM
1460 cu_list.append_uint (8, BFD_ENDIAN_LITTLE,
1461 to_underlying (per_cu->sect_off));
1462 cu_list.append_uint (8, BFD_ENDIAN_LITTLE, per_cu->length);
1463 }
1464
1465 /* Dump the address map. */
1466 data_buf addr_vec;
79cc99f6 1467 write_address_map (per_objfile->per_bfd, addr_vec, cu_index_htab);
cd4fb1b2
SM
1468
1469 /* Write out the .debug_type entries, if any. */
1470 data_buf types_cu_list;
976ca316 1471 if (per_objfile->per_bfd->signatured_types)
cd4fb1b2
SM
1472 {
1473 signatured_type_index_data sig_data (types_cu_list,
1474 psyms_seen);
1475
1476 sig_data.objfile = objfile;
1477 sig_data.symtab = &symtab;
976ca316
SM
1478 sig_data.cu_index = per_objfile->per_bfd->all_comp_units.size ();
1479 htab_traverse_noresize (per_objfile->per_bfd->signatured_types.get (),
cd4fb1b2
SM
1480 write_one_signatured_type, &sig_data);
1481 }
1482
1483 /* Now that we've processed all symbols we can shrink their cu_indices
1484 lists. */
1485 uniquify_cu_indices (&symtab);
1486
1487 data_buf symtab_vec, constant_pool;
1488 write_hash_table (&symtab, symtab_vec, constant_pool);
1489
c4973306
SM
1490 write_gdbindex_1(out_file, objfile_cu_list, types_cu_list, addr_vec,
1491 symtab_vec, constant_pool);
cd4fb1b2 1492
c4973306
SM
1493 if (dwz_out_file != NULL)
1494 write_gdbindex_1 (dwz_out_file, dwz_cu_list, {}, {}, {}, {});
1495 else
1496 gdb_assert (dwz_cu_list.empty ());
cd4fb1b2
SM
1497}
1498
1499/* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
1500static const gdb_byte dwarf5_gdb_augmentation[] = { 'G', 'D', 'B', 0 };
1501
1502/* Write a new .debug_names section for OBJFILE into OUT_FILE, write
1503 needed addition to .debug_str section to OUT_FILE_STR. Return how
1504 many bytes were expected to be written into OUT_FILE. */
1505
c4973306 1506static void
976ca316 1507write_debug_names (dwarf2_per_objfile *per_objfile,
cd4fb1b2
SM
1508 FILE *out_file, FILE *out_file_str)
1509{
976ca316
SM
1510 const bool dwarf5_is_dwarf64 = check_dwarf64_offsets (per_objfile);
1511 struct objfile *objfile = per_objfile->objfile;
cd4fb1b2 1512 const enum bfd_endian dwarf5_byte_order
08feed99 1513 = gdbarch_byte_order (objfile->arch ());
cd4fb1b2
SM
1514
1515 /* The CU list is already sorted, so we don't need to do additional
1516 work here. Also, the debug_types entries do not appear in
1517 all_comp_units, but only in their own hash table. */
1518 data_buf cu_list;
976ca316 1519 debug_names nametable (per_objfile, dwarf5_is_dwarf64, dwarf5_byte_order);
cd4fb1b2 1520 std::unordered_set<partial_symbol *>
976ca316
SM
1521 psyms_seen (psyms_seen_size (per_objfile));
1522 for (int i = 0; i < per_objfile->per_bfd->all_comp_units.size (); ++i)
cd4fb1b2 1523 {
976ca316 1524 const dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->all_comp_units[i];
edfe0a0c 1525 partial_symtab *psymtab = per_cu->v.psymtab;
cd4fb1b2
SM
1526
1527 /* CU of a shared file from 'dwz -m' may be unused by this main
1528 file. It may be referenced from a local scope but in such
1529 case it does not need to be present in .debug_names. */
1530 if (psymtab == NULL)
1531 continue;
1532
1533 if (psymtab->user == NULL)
1534 nametable.recursively_write_psymbols (objfile, psymtab, psyms_seen, i);
1535
1536 cu_list.append_uint (nametable.dwarf5_offset_size (), dwarf5_byte_order,
1537 to_underlying (per_cu->sect_off));
1538 }
1539
1540 /* Write out the .debug_type entries, if any. */
1541 data_buf types_cu_list;
976ca316 1542 if (per_objfile->per_bfd->signatured_types)
cd4fb1b2
SM
1543 {
1544 debug_names::write_one_signatured_type_data sig_data (nametable,
1545 signatured_type_index_data (types_cu_list, psyms_seen));
1546
1547 sig_data.info.objfile = objfile;
1548 /* It is used only for gdb_index. */
1549 sig_data.info.symtab = nullptr;
1550 sig_data.info.cu_index = 0;
976ca316 1551 htab_traverse_noresize (per_objfile->per_bfd->signatured_types.get (),
cd4fb1b2
SM
1552 debug_names::write_one_signatured_type,
1553 &sig_data);
1554 }
1555
1556 nametable.build ();
1557
1558 /* No addr_vec - DWARF-5 uses .debug_aranges generated by GCC. */
1559
1560 const offset_type bytes_of_header
1561 = ((dwarf5_is_dwarf64 ? 12 : 4)
1562 + 2 + 2 + 7 * 4
1563 + sizeof (dwarf5_gdb_augmentation));
1564 size_t expected_bytes = 0;
1565 expected_bytes += bytes_of_header;
1566 expected_bytes += cu_list.size ();
1567 expected_bytes += types_cu_list.size ();
1568 expected_bytes += nametable.bytes ();
1569 data_buf header;
1570
1571 if (!dwarf5_is_dwarf64)
1572 {
1573 const uint64_t size64 = expected_bytes - 4;
1574 gdb_assert (size64 < 0xfffffff0);
1575 header.append_uint (4, dwarf5_byte_order, size64);
1576 }
1577 else
1578 {
1579 header.append_uint (4, dwarf5_byte_order, 0xffffffff);
1580 header.append_uint (8, dwarf5_byte_order, expected_bytes - 12);
1581 }
1582
1583 /* The version number. */
1584 header.append_uint (2, dwarf5_byte_order, 5);
1585
1586 /* Padding. */
1587 header.append_uint (2, dwarf5_byte_order, 0);
1588
1589 /* comp_unit_count - The number of CUs in the CU list. */
b76e467d 1590 header.append_uint (4, dwarf5_byte_order,
976ca316 1591 per_objfile->per_bfd->all_comp_units.size ());
cd4fb1b2
SM
1592
1593 /* local_type_unit_count - The number of TUs in the local TU
1594 list. */
b2bdb8cf 1595 header.append_uint (4, dwarf5_byte_order,
976ca316 1596 per_objfile->per_bfd->all_type_units.size ());
cd4fb1b2
SM
1597
1598 /* foreign_type_unit_count - The number of TUs in the foreign TU
1599 list. */
1600 header.append_uint (4, dwarf5_byte_order, 0);
1601
1602 /* bucket_count - The number of hash buckets in the hash lookup
1603 table. */
1604 header.append_uint (4, dwarf5_byte_order, nametable.bucket_count ());
1605
1606 /* name_count - The number of unique names in the index. */
1607 header.append_uint (4, dwarf5_byte_order, nametable.name_count ());
1608
1609 /* abbrev_table_size - The size in bytes of the abbreviations
1610 table. */
1611 header.append_uint (4, dwarf5_byte_order, nametable.abbrev_table_bytes ());
1612
1613 /* augmentation_string_size - The size in bytes of the augmentation
1614 string. This value is rounded up to a multiple of 4. */
1615 static_assert (sizeof (dwarf5_gdb_augmentation) % 4 == 0, "");
1616 header.append_uint (4, dwarf5_byte_order, sizeof (dwarf5_gdb_augmentation));
42c2c694 1617 header.append_array (dwarf5_gdb_augmentation);
cd4fb1b2
SM
1618
1619 gdb_assert (header.size () == bytes_of_header);
1620
1621 header.file_write (out_file);
1622 cu_list.file_write (out_file);
1623 types_cu_list.file_write (out_file);
1624 nametable.file_write (out_file, out_file_str);
1625
c4973306 1626 assert_file_size (out_file, expected_bytes);
cd4fb1b2
SM
1627}
1628
c4973306 1629/* This represents an index file being written (work-in-progress).
cd4fb1b2 1630
c4973306
SM
1631 The data is initially written to a temporary file. When the finalize method
1632 is called, the file is closed and moved to its final location.
1633
1634 On failure (if this object is being destroyed with having called finalize),
1635 the temporary file is closed and deleted. */
1636
1637struct index_wip_file
cd4fb1b2 1638{
c4973306
SM
1639 index_wip_file (const char *dir, const char *basename,
1640 const char *suffix)
1641 {
1642 filename = (std::string (dir) + SLASH_STRING + basename
1643 + suffix);
1644
1645 filename_temp = make_temp_filename (filename);
1646
1647 scoped_fd out_file_fd (gdb_mkostemp_cloexec (filename_temp.data (),
1648 O_BINARY));
1649 if (out_file_fd.get () == -1)
1650 perror_with_name (("mkstemp"));
1651
1652 out_file = out_file_fd.to_file ("wb");
1653
1654 if (out_file == nullptr)
1655 error (_("Can't open `%s' for writing"), filename_temp.data ());
1656
1657 unlink_file.emplace (filename_temp.data ());
1658 }
1659
1660 void finalize ()
1661 {
1662 /* We want to keep the file. */
1663 unlink_file->keep ();
1664
1665 /* Close and move the str file in place. */
1666 unlink_file.reset ();
1667 if (rename (filename_temp.data (), filename.c_str ()) != 0)
1668 perror_with_name (("rename"));
1669 }
1670
1671 std::string filename;
1672 gdb::char_vector filename_temp;
1673
1674 /* Order matters here; we want FILE to be closed before
1675 FILENAME_TEMP is unlinked, because on MS-Windows one cannot
1676 delete a file that is still open. So, we wrap the unlinker in an
1677 optional and emplace it once we know the file name. */
1678 gdb::optional<gdb::unlinker> unlink_file;
1679
1680 gdb_file_up out_file;
1681};
cd4fb1b2 1682
87d6a7aa 1683/* See dwarf-index-write.h. */
cd4fb1b2 1684
87d6a7aa 1685void
976ca316
SM
1686write_psymtabs_to_index (dwarf2_per_objfile *per_objfile, const char *dir,
1687 const char *basename, const char *dwz_basename,
cd4fb1b2
SM
1688 dw_index_kind index_kind)
1689{
79cc99f6 1690 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
976ca316 1691 struct objfile *objfile = per_objfile->objfile;
cd4fb1b2 1692
976ca316 1693 if (per_objfile->per_bfd->using_index)
cd4fb1b2
SM
1694 error (_("Cannot use an index to create the index"));
1695
976ca316 1696 if (per_objfile->per_bfd->types.size () > 1)
cd4fb1b2
SM
1697 error (_("Cannot make an index when the file has multiple .debug_types sections"));
1698
da314dd3
TT
1699 if (per_bfd->partial_symtabs == nullptr
1700 || !per_bfd->partial_symtabs->psymtabs
79cc99f6 1701 || !per_bfd->partial_symtabs->psymtabs_addrmap)
cd4fb1b2
SM
1702 return;
1703
1704 struct stat st;
1705 if (stat (objfile_name (objfile), &st) < 0)
1706 perror_with_name (objfile_name (objfile));
1707
c4973306
SM
1708 const char *index_suffix = (index_kind == dw_index_kind::DEBUG_NAMES
1709 ? INDEX5_SUFFIX : INDEX4_SUFFIX);
cd4fb1b2 1710
c4973306
SM
1711 index_wip_file objfile_index_wip (dir, basename, index_suffix);
1712 gdb::optional<index_wip_file> dwz_index_wip;
cd4fb1b2 1713
c4973306
SM
1714 if (dwz_basename != NULL)
1715 dwz_index_wip.emplace (dir, dwz_basename, index_suffix);
cd4fb1b2
SM
1716
1717 if (index_kind == dw_index_kind::DEBUG_NAMES)
1718 {
c4973306
SM
1719 index_wip_file str_wip_file (dir, basename, DEBUG_STR_SUFFIX);
1720
976ca316 1721 write_debug_names (per_objfile, objfile_index_wip.out_file.get (),
c4973306
SM
1722 str_wip_file.out_file.get ());
1723
1724 str_wip_file.finalize ();
cd4fb1b2
SM
1725 }
1726 else
976ca316 1727 write_gdbindex (per_objfile, objfile_index_wip.out_file.get (),
c4973306
SM
1728 (dwz_index_wip.has_value ()
1729 ? dwz_index_wip->out_file.get () : NULL));
cd4fb1b2 1730
c4973306 1731 objfile_index_wip.finalize ();
87d6a7aa 1732
c4973306
SM
1733 if (dwz_index_wip.has_value ())
1734 dwz_index_wip->finalize ();
cd4fb1b2
SM
1735}
1736
1737/* Implementation of the `save gdb-index' command.
1738
1739 Note that the .gdb_index file format used by this command is
1740 documented in the GDB manual. Any changes here must be documented
1741 there. */
1742
1743static void
1744save_gdb_index_command (const char *arg, int from_tty)
1745{
cd4fb1b2
SM
1746 const char dwarf5space[] = "-dwarf-5 ";
1747 dw_index_kind index_kind = dw_index_kind::GDB_INDEX;
1748
1749 if (!arg)
1750 arg = "";
1751
1752 arg = skip_spaces (arg);
1753 if (strncmp (arg, dwarf5space, strlen (dwarf5space)) == 0)
1754 {
1755 index_kind = dw_index_kind::DEBUG_NAMES;
1756 arg += strlen (dwarf5space);
1757 arg = skip_spaces (arg);
1758 }
1759
1760 if (!*arg)
1761 error (_("usage: save gdb-index [-dwarf-5] DIRECTORY"));
1762
2030c079 1763 for (objfile *objfile : current_program_space->objfiles ())
aed57c53
TT
1764 {
1765 struct stat st;
cd4fb1b2 1766
aed57c53
TT
1767 /* If the objfile does not correspond to an actual file, skip it. */
1768 if (stat (objfile_name (objfile), &st) < 0)
1769 continue;
cd4fb1b2 1770
976ca316 1771 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
cd4fb1b2 1772
976ca316 1773 if (per_objfile != NULL)
aed57c53 1774 {
a70b8144 1775 try
aed57c53
TT
1776 {
1777 const char *basename = lbasename (objfile_name (objfile));
976ca316 1778 const dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
c4973306
SM
1779 const char *dwz_basename = NULL;
1780
1781 if (dwz != NULL)
1782 dwz_basename = lbasename (dwz->filename ());
1783
976ca316
SM
1784 write_psymtabs_to_index (per_objfile, arg, basename, dwz_basename,
1785 index_kind);
aed57c53 1786 }
230d2906 1787 catch (const gdb_exception_error &except)
aed57c53
TT
1788 {
1789 exception_fprintf (gdb_stderr, except,
1790 _("Error while writing index for `%s': "),
1791 objfile_name (objfile));
1792 }
aed57c53 1793 }
cd4fb1b2 1794
aed57c53 1795 }
cd4fb1b2
SM
1796}
1797
6c265988 1798void _initialize_dwarf_index_write ();
cd4fb1b2
SM
1799void
1800_initialize_dwarf_index_write ()
1801{
1802 cmd_list_element *c = add_cmd ("gdb-index", class_files,
1803 save_gdb_index_command, _("\
1804Save a gdb-index file.\n\
1805Usage: save gdb-index [-dwarf-5] DIRECTORY\n\
1806\n\
1807No options create one file with .gdb-index extension for pre-DWARF-5\n\
1808compatible .gdb_index section. With -dwarf-5 creates two files with\n\
1809extension .debug_names and .debug_str for DWARF-5 .debug_names section."),
1810 &save_cmdlist);
1811 set_cmd_completer (c, filename_completer);
1812}
This page took 0.401055 seconds and 4 git commands to generate.