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