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