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