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