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