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