Move DWARF index-related things to a separate file
[external/binutils.git] / gdb / dwarf-index-write.c
1 /* DWARF index writing support for GDB.
2
3    Copyright (C) 1994-2018 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 "common/byte-vector.h"
25 #include "common/filestuff.h"
26 #include "common/gdb_unlinker.h"
27 #include "complaints.h"
28 #include "dwarf-index-common.h"
29 #include "dwarf2.h"
30 #include "dwarf2read.h"
31 #include "gdb/gdb-index.h"
32 #include "gdbcmd.h"
33 #include "objfiles.h"
34 #include "psympriv.h"
35
36 #include <algorithm>
37 #include <set>
38 #include <unordered_map>
39 #include <unordered_set>
40
41 /* The suffix for an index file.  */
42 #define INDEX4_SUFFIX ".gdb-index"
43 #define INDEX5_SUFFIX ".debug_names"
44 #define DEBUG_STR_SUFFIX ".debug_str"
45
46 /* Ensure only legit values are used.  */
47 #define DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE(cu_index, value) \
48   do { \
49     gdb_assert ((unsigned int) (value) <= 1); \
50     GDB_INDEX_SYMBOL_STATIC_SET_VALUE((cu_index), (value)); \
51   } while (0)
52
53 /* Ensure only legit values are used.  */
54 #define DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE(cu_index, value) \
55   do { \
56     gdb_assert ((value) >= GDB_INDEX_SYMBOL_KIND_TYPE \
57                 && (value) <= GDB_INDEX_SYMBOL_KIND_OTHER); \
58     GDB_INDEX_SYMBOL_KIND_SET_VALUE((cu_index), (value)); \
59   } while (0)
60
61 /* Ensure we don't use more than the alloted nuber of bits for the CU.  */
62 #define DW2_GDB_INDEX_CU_SET_VALUE(cu_index, value) \
63   do { \
64     gdb_assert (((value) & ~GDB_INDEX_CU_MASK) == 0); \
65     GDB_INDEX_CU_SET_VALUE((cu_index), (value)); \
66   } while (0)
67
68 /* The "save gdb-index" command.  */
69
70 /* Write SIZE bytes from the buffer pointed to by DATA to FILE, with
71    error checking.  */
72
73 static void
74 file_write (FILE *file, const void *data, size_t size)
75 {
76   if (fwrite (data, 1, size, file) != size)
77     error (_("couldn't data write to file"));
78 }
79
80 /* Write the contents of VEC to FILE, with error checking.  */
81
82 template<typename Elem, typename Alloc>
83 static void
84 file_write (FILE *file, const std::vector<Elem, Alloc> &vec)
85 {
86   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   CORE_ADDR baseaddr;
430
431   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
432
433   addr_vec.append_uint (8, BFD_ENDIAN_LITTLE, start - baseaddr);
434   addr_vec.append_uint (8, BFD_ENDIAN_LITTLE, end - baseaddr);
435   addr_vec.append_data (MAYBE_SWAP (cu_index));
436 }
437
438 /* Worker function for traversing an addrmap to build the address table.  */
439
440 static int
441 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
442 {
443   struct addrmap_index_data *data = (struct addrmap_index_data *) datap;
444   struct partial_symtab *pst = (struct partial_symtab *) obj;
445
446   if (data->previous_valid)
447     add_address_entry (data->objfile, data->addr_vec,
448                        data->previous_cu_start, start_addr,
449                        data->previous_cu_index);
450
451   data->previous_cu_start = start_addr;
452   if (pst != NULL)
453     {
454       const auto it = data->cu_index_htab.find (pst);
455       gdb_assert (it != data->cu_index_htab.cend ());
456       data->previous_cu_index = it->second;
457       data->previous_valid = 1;
458     }
459   else
460     data->previous_valid = 0;
461
462   return 0;
463 }
464
465 /* Write OBJFILE's address map to ADDR_VEC.
466    CU_INDEX_HTAB is used to map addrmap entries to their CU indices
467    in the index file.  */
468
469 static void
470 write_address_map (struct objfile *objfile, data_buf &addr_vec,
471                    psym_index_map &cu_index_htab)
472 {
473   struct addrmap_index_data addrmap_index_data (addr_vec, cu_index_htab);
474
475   /* When writing the address table, we have to cope with the fact that
476      the addrmap iterator only provides the start of a region; we have to
477      wait until the next invocation to get the start of the next region.  */
478
479   addrmap_index_data.objfile = objfile;
480   addrmap_index_data.previous_valid = 0;
481
482   addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
483                    &addrmap_index_data);
484
485   /* It's highly unlikely the last entry (end address = 0xff...ff)
486      is valid, but we should still handle it.
487      The end address is recorded as the start of the next region, but that
488      doesn't work here.  To cope we pass 0xff...ff, this is a rare situation
489      anyway.  */
490   if (addrmap_index_data.previous_valid)
491     add_address_entry (objfile, addr_vec,
492                        addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
493                        addrmap_index_data.previous_cu_index);
494 }
495
496 /* Return the symbol kind of PSYM.  */
497
498 static gdb_index_symbol_kind
499 symbol_kind (struct partial_symbol *psym)
500 {
501   domain_enum domain = PSYMBOL_DOMAIN (psym);
502   enum address_class aclass = PSYMBOL_CLASS (psym);
503
504   switch (domain)
505     {
506     case VAR_DOMAIN:
507       switch (aclass)
508         {
509         case LOC_BLOCK:
510           return GDB_INDEX_SYMBOL_KIND_FUNCTION;
511         case LOC_TYPEDEF:
512           return GDB_INDEX_SYMBOL_KIND_TYPE;
513         case LOC_COMPUTED:
514         case LOC_CONST_BYTES:
515         case LOC_OPTIMIZED_OUT:
516         case LOC_STATIC:
517           return GDB_INDEX_SYMBOL_KIND_VARIABLE;
518         case LOC_CONST:
519           /* Note: It's currently impossible to recognize psyms as enum values
520              short of reading the type info.  For now punt.  */
521           return GDB_INDEX_SYMBOL_KIND_VARIABLE;
522         default:
523           /* There are other LOC_FOO values that one might want to classify
524              as variables, but dwarf2read.c doesn't currently use them.  */
525           return GDB_INDEX_SYMBOL_KIND_OTHER;
526         }
527     case STRUCT_DOMAIN:
528       return GDB_INDEX_SYMBOL_KIND_TYPE;
529     default:
530       return GDB_INDEX_SYMBOL_KIND_OTHER;
531     }
532 }
533
534 /* Add a list of partial symbols to SYMTAB.  */
535
536 static void
537 write_psymbols (struct mapped_symtab *symtab,
538                 std::unordered_set<partial_symbol *> &psyms_seen,
539                 struct partial_symbol **psymp,
540                 int count,
541                 offset_type cu_index,
542                 int is_static)
543 {
544   for (; count-- > 0; ++psymp)
545     {
546       struct partial_symbol *psym = *psymp;
547
548       if (SYMBOL_LANGUAGE (psym) == language_ada)
549         error (_("Ada is not currently supported by the index"));
550
551       /* Only add a given psymbol once.  */
552       if (psyms_seen.insert (psym).second)
553         {
554           gdb_index_symbol_kind kind = symbol_kind (psym);
555
556           add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
557                            is_static, kind, cu_index);
558         }
559     }
560 }
561
562 /* A helper struct used when iterating over debug_types.  */
563 struct signatured_type_index_data
564 {
565   signatured_type_index_data (data_buf &types_list_,
566                               std::unordered_set<partial_symbol *> &psyms_seen_)
567     : types_list (types_list_), psyms_seen (psyms_seen_)
568   {}
569
570   struct objfile *objfile;
571   struct mapped_symtab *symtab;
572   data_buf &types_list;
573   std::unordered_set<partial_symbol *> &psyms_seen;
574   int cu_index;
575 };
576
577 /* A helper function that writes a single signatured_type to an
578    obstack.  */
579
580 static int
581 write_one_signatured_type (void **slot, void *d)
582 {
583   struct signatured_type_index_data *info
584     = (struct signatured_type_index_data *) d;
585   struct signatured_type *entry = (struct signatured_type *) *slot;
586   struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
587
588   write_psymbols (info->symtab,
589                   info->psyms_seen,
590                   &info->objfile->global_psymbols[psymtab->globals_offset],
591                   psymtab->n_global_syms, info->cu_index,
592                   0);
593   write_psymbols (info->symtab,
594                   info->psyms_seen,
595                   &info->objfile->static_psymbols[psymtab->statics_offset],
596                   psymtab->n_static_syms, info->cu_index,
597                   1);
598
599   info->types_list.append_uint (8, BFD_ENDIAN_LITTLE,
600                                 to_underlying (entry->per_cu.sect_off));
601   info->types_list.append_uint (8, BFD_ENDIAN_LITTLE,
602                                 to_underlying (entry->type_offset_in_tu));
603   info->types_list.append_uint (8, BFD_ENDIAN_LITTLE, entry->signature);
604
605   ++info->cu_index;
606
607   return 1;
608 }
609
610 /* Recurse into all "included" dependencies and count their symbols as
611    if they appeared in this psymtab.  */
612
613 static void
614 recursively_count_psymbols (struct partial_symtab *psymtab,
615                             size_t &psyms_seen)
616 {
617   for (int i = 0; i < psymtab->number_of_dependencies; ++i)
618     if (psymtab->dependencies[i]->user != NULL)
619       recursively_count_psymbols (psymtab->dependencies[i],
620                                   psyms_seen);
621
622   psyms_seen += psymtab->n_global_syms;
623   psyms_seen += psymtab->n_static_syms;
624 }
625
626 /* Recurse into all "included" dependencies and write their symbols as
627    if they appeared in this psymtab.  */
628
629 static void
630 recursively_write_psymbols (struct objfile *objfile,
631                             struct partial_symtab *psymtab,
632                             struct mapped_symtab *symtab,
633                             std::unordered_set<partial_symbol *> &psyms_seen,
634                             offset_type cu_index)
635 {
636   int i;
637
638   for (i = 0; i < psymtab->number_of_dependencies; ++i)
639     if (psymtab->dependencies[i]->user != NULL)
640       recursively_write_psymbols (objfile, psymtab->dependencies[i],
641                                   symtab, psyms_seen, cu_index);
642
643   write_psymbols (symtab,
644                   psyms_seen,
645                   &objfile->global_psymbols[psymtab->globals_offset],
646                   psymtab->n_global_syms, cu_index,
647                   0);
648   write_psymbols (symtab,
649                   psyms_seen,
650                   &objfile->static_psymbols[psymtab->statics_offset],
651                   psymtab->n_static_syms, cu_index,
652                   1);
653 }
654
655 /* DWARF-5 .debug_names builder.  */
656 class debug_names
657 {
658 public:
659   debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile, bool is_dwarf64,
660                bfd_endian dwarf5_byte_order)
661     : m_dwarf5_byte_order (dwarf5_byte_order),
662       m_dwarf32 (dwarf5_byte_order),
663       m_dwarf64 (dwarf5_byte_order),
664       m_dwarf (is_dwarf64
665                ? static_cast<dwarf &> (m_dwarf64)
666                : static_cast<dwarf &> (m_dwarf32)),
667       m_name_table_string_offs (m_dwarf.name_table_string_offs),
668       m_name_table_entry_offs (m_dwarf.name_table_entry_offs),
669       m_debugstrlookup (dwarf2_per_objfile)
670   {}
671
672   int dwarf5_offset_size () const
673   {
674     const bool dwarf5_is_dwarf64 = &m_dwarf == &m_dwarf64;
675     return dwarf5_is_dwarf64 ? 8 : 4;
676   }
677
678   /* Is this symbol from DW_TAG_compile_unit or DW_TAG_type_unit?  */
679   enum class unit_kind { cu, tu };
680
681   /* Insert one symbol.  */
682   void insert (const partial_symbol *psym, int cu_index, bool is_static,
683                unit_kind kind)
684   {
685     const int dwarf_tag = psymbol_tag (psym);
686     if (dwarf_tag == 0)
687       return;
688     const char *const name = SYMBOL_SEARCH_NAME (psym);
689     const auto insertpair
690       = m_name_to_value_set.emplace (c_str_view (name),
691                                      std::set<symbol_value> ());
692     std::set<symbol_value> &value_set = insertpair.first->second;
693     value_set.emplace (symbol_value (dwarf_tag, cu_index, is_static, kind));
694   }
695
696   /* Build all the tables.  All symbols must be already inserted.
697      This function does not call file_write, caller has to do it
698      afterwards.  */
699   void build ()
700   {
701     /* Verify the build method has not be called twice.  */
702     gdb_assert (m_abbrev_table.empty ());
703     const size_t name_count = m_name_to_value_set.size ();
704     m_bucket_table.resize
705       (std::pow (2, std::ceil (std::log2 (name_count * 4 / 3))));
706     m_hash_table.reserve (name_count);
707     m_name_table_string_offs.reserve (name_count);
708     m_name_table_entry_offs.reserve (name_count);
709
710     /* Map each hash of symbol to its name and value.  */
711     struct hash_it_pair
712     {
713       uint32_t hash;
714       decltype (m_name_to_value_set)::const_iterator it;
715     };
716     std::vector<std::forward_list<hash_it_pair>> bucket_hash;
717     bucket_hash.resize (m_bucket_table.size ());
718     for (decltype (m_name_to_value_set)::const_iterator it
719            = m_name_to_value_set.cbegin ();
720          it != m_name_to_value_set.cend ();
721          ++it)
722       {
723         const char *const name = it->first.c_str ();
724         const uint32_t hash = dwarf5_djb_hash (name);
725         hash_it_pair hashitpair;
726         hashitpair.hash = hash;
727         hashitpair.it = it;
728         auto &slot = bucket_hash[hash % bucket_hash.size()];
729         slot.push_front (std::move (hashitpair));
730       }
731     for (size_t bucket_ix = 0; bucket_ix < bucket_hash.size (); ++bucket_ix)
732       {
733         const std::forward_list<hash_it_pair> &hashitlist
734           = bucket_hash[bucket_ix];
735         if (hashitlist.empty ())
736           continue;
737         uint32_t &bucket_slot = m_bucket_table[bucket_ix];
738         /* The hashes array is indexed starting at 1.  */
739         store_unsigned_integer (reinterpret_cast<gdb_byte *> (&bucket_slot),
740                                 sizeof (bucket_slot), m_dwarf5_byte_order,
741                                 m_hash_table.size () + 1);
742         for (const hash_it_pair &hashitpair : hashitlist)
743           {
744             m_hash_table.push_back (0);
745             store_unsigned_integer (reinterpret_cast<gdb_byte *>
746                                                         (&m_hash_table.back ()),
747                                     sizeof (m_hash_table.back ()),
748                                     m_dwarf5_byte_order, hashitpair.hash);
749             const c_str_view &name = hashitpair.it->first;
750             const std::set<symbol_value> &value_set = hashitpair.it->second;
751             m_name_table_string_offs.push_back_reorder
752               (m_debugstrlookup.lookup (name.c_str ()));
753             m_name_table_entry_offs.push_back_reorder (m_entry_pool.size ());
754             gdb_assert (!value_set.empty ());
755             for (const symbol_value &value : value_set)
756               {
757                 int &idx = m_indexkey_to_idx[index_key (value.dwarf_tag,
758                                                         value.is_static,
759                                                         value.kind)];
760                 if (idx == 0)
761                   {
762                     idx = m_idx_next++;
763                     m_abbrev_table.append_unsigned_leb128 (idx);
764                     m_abbrev_table.append_unsigned_leb128 (value.dwarf_tag);
765                     m_abbrev_table.append_unsigned_leb128
766                               (value.kind == unit_kind::cu ? DW_IDX_compile_unit
767                                                            : DW_IDX_type_unit);
768                     m_abbrev_table.append_unsigned_leb128 (DW_FORM_udata);
769                     m_abbrev_table.append_unsigned_leb128 (value.is_static
770                                                            ? DW_IDX_GNU_internal
771                                                            : DW_IDX_GNU_external);
772                     m_abbrev_table.append_unsigned_leb128 (DW_FORM_flag_present);
773
774                     /* Terminate attributes list.  */
775                     m_abbrev_table.append_unsigned_leb128 (0);
776                     m_abbrev_table.append_unsigned_leb128 (0);
777                   }
778
779                 m_entry_pool.append_unsigned_leb128 (idx);
780                 m_entry_pool.append_unsigned_leb128 (value.cu_index);
781               }
782
783             /* Terminate the list of CUs.  */
784             m_entry_pool.append_unsigned_leb128 (0);
785           }
786       }
787     gdb_assert (m_hash_table.size () == name_count);
788
789     /* Terminate tags list.  */
790     m_abbrev_table.append_unsigned_leb128 (0);
791   }
792
793   /* Return .debug_names bucket count.  This must be called only after
794      calling the build method.  */
795   uint32_t bucket_count () const
796   {
797     /* Verify the build method has been already called.  */
798     gdb_assert (!m_abbrev_table.empty ());
799     const uint32_t retval = m_bucket_table.size ();
800
801     /* Check for overflow.  */
802     gdb_assert (retval == m_bucket_table.size ());
803     return retval;
804   }
805
806   /* Return .debug_names names count.  This must be called only after
807      calling the build method.  */
808   uint32_t name_count () const
809   {
810     /* Verify the build method has been already called.  */
811     gdb_assert (!m_abbrev_table.empty ());
812     const uint32_t retval = m_hash_table.size ();
813
814     /* Check for overflow.  */
815     gdb_assert (retval == m_hash_table.size ());
816     return retval;
817   }
818
819   /* Return number of bytes of .debug_names abbreviation table.  This
820      must be called only after calling the build method.  */
821   uint32_t abbrev_table_bytes () const
822   {
823     gdb_assert (!m_abbrev_table.empty ());
824     return m_abbrev_table.size ();
825   }
826
827   /* Recurse into all "included" dependencies and store their symbols
828      as if they appeared in this psymtab.  */
829   void recursively_write_psymbols
830     (struct objfile *objfile,
831      struct partial_symtab *psymtab,
832      std::unordered_set<partial_symbol *> &psyms_seen,
833      int cu_index)
834   {
835     for (int i = 0; i < psymtab->number_of_dependencies; ++i)
836       if (psymtab->dependencies[i]->user != NULL)
837         recursively_write_psymbols (objfile, psymtab->dependencies[i],
838                                     psyms_seen, cu_index);
839
840     write_psymbols (psyms_seen,
841                     &objfile->global_psymbols[psymtab->globals_offset],
842                     psymtab->n_global_syms, cu_index, false, unit_kind::cu);
843     write_psymbols (psyms_seen,
844                     &objfile->static_psymbols[psymtab->statics_offset],
845                     psymtab->n_static_syms, cu_index, true, unit_kind::cu);
846   }
847
848   /* Return number of bytes the .debug_names section will have.  This
849      must be called only after calling the build method.  */
850   size_t bytes () const
851   {
852     /* Verify the build method has been already called.  */
853     gdb_assert (!m_abbrev_table.empty ());
854     size_t expected_bytes = 0;
855     expected_bytes += m_bucket_table.size () * sizeof (m_bucket_table[0]);
856     expected_bytes += m_hash_table.size () * sizeof (m_hash_table[0]);
857     expected_bytes += m_name_table_string_offs.bytes ();
858     expected_bytes += m_name_table_entry_offs.bytes ();
859     expected_bytes += m_abbrev_table.size ();
860     expected_bytes += m_entry_pool.size ();
861     return expected_bytes;
862   }
863
864   /* Write .debug_names to FILE_NAMES and .debug_str addition to
865      FILE_STR.  This must be called only after calling the build
866      method.  */
867   void file_write (FILE *file_names, FILE *file_str) const
868   {
869     /* Verify the build method has been already called.  */
870     gdb_assert (!m_abbrev_table.empty ());
871     ::file_write (file_names, m_bucket_table);
872     ::file_write (file_names, m_hash_table);
873     m_name_table_string_offs.file_write (file_names);
874     m_name_table_entry_offs.file_write (file_names);
875     m_abbrev_table.file_write (file_names);
876     m_entry_pool.file_write (file_names);
877     m_debugstrlookup.file_write (file_str);
878   }
879
880   /* A helper user data for write_one_signatured_type.  */
881   class write_one_signatured_type_data
882   {
883   public:
884     write_one_signatured_type_data (debug_names &nametable_,
885                                     signatured_type_index_data &&info_)
886     : nametable (nametable_), info (std::move (info_))
887     {}
888     debug_names &nametable;
889     struct signatured_type_index_data info;
890   };
891
892   /* A helper function to pass write_one_signatured_type to
893      htab_traverse_noresize.  */
894   static int
895   write_one_signatured_type (void **slot, void *d)
896   {
897     write_one_signatured_type_data *data = (write_one_signatured_type_data *) d;
898     struct signatured_type_index_data *info = &data->info;
899     struct signatured_type *entry = (struct signatured_type *) *slot;
900
901     data->nametable.write_one_signatured_type (entry, info);
902
903     return 1;
904   }
905
906 private:
907
908   /* Storage for symbol names mapping them to their .debug_str section
909      offsets.  */
910   class debug_str_lookup
911   {
912   public:
913
914     /* Object costructor to be called for current DWARF2_PER_OBJFILE.
915        All .debug_str section strings are automatically stored.  */
916     debug_str_lookup (struct dwarf2_per_objfile *dwarf2_per_objfile)
917       : m_abfd (dwarf2_per_objfile->objfile->obfd),
918         m_dwarf2_per_objfile (dwarf2_per_objfile)
919     {
920       dwarf2_read_section (dwarf2_per_objfile->objfile,
921                            &dwarf2_per_objfile->str);
922       if (dwarf2_per_objfile->str.buffer == NULL)
923         return;
924       for (const gdb_byte *data = dwarf2_per_objfile->str.buffer;
925            data < (dwarf2_per_objfile->str.buffer
926                    + dwarf2_per_objfile->str.size);)
927         {
928           const char *const s = reinterpret_cast<const char *> (data);
929           const auto insertpair
930             = m_str_table.emplace (c_str_view (s),
931                                    data - dwarf2_per_objfile->str.buffer);
932           if (!insertpair.second)
933             complaint (&symfile_complaints,
934                        _("Duplicate string \"%s\" in "
935                          ".debug_str section [in module %s]"),
936                        s, bfd_get_filename (m_abfd));
937           data += strlen (s) + 1;
938         }
939     }
940
941     /* Return offset of symbol name S in the .debug_str section.  Add
942        such symbol to the section's end if it does not exist there
943        yet.  */
944     size_t lookup (const char *s)
945     {
946       const auto it = m_str_table.find (c_str_view (s));
947       if (it != m_str_table.end ())
948         return it->second;
949       const size_t offset = (m_dwarf2_per_objfile->str.size
950                              + m_str_add_buf.size ());
951       m_str_table.emplace (c_str_view (s), offset);
952       m_str_add_buf.append_cstr0 (s);
953       return offset;
954     }
955
956     /* Append the end of the .debug_str section to FILE.  */
957     void file_write (FILE *file) const
958     {
959       m_str_add_buf.file_write (file);
960     }
961
962   private:
963     std::unordered_map<c_str_view, size_t, c_str_view_hasher> m_str_table;
964     bfd *const m_abfd;
965     struct dwarf2_per_objfile *m_dwarf2_per_objfile;
966
967     /* Data to add at the end of .debug_str for new needed symbol names.  */
968     data_buf m_str_add_buf;
969   };
970
971   /* Container to map used DWARF tags to their .debug_names abbreviation
972      tags.  */
973   class index_key
974   {
975   public:
976     index_key (int dwarf_tag_, bool is_static_, unit_kind kind_)
977       : dwarf_tag (dwarf_tag_), is_static (is_static_), kind (kind_)
978     {
979     }
980
981     bool
982     operator== (const index_key &other) const
983     {
984       return (dwarf_tag == other.dwarf_tag && is_static == other.is_static
985               && kind == other.kind);
986     }
987
988     const int dwarf_tag;
989     const bool is_static;
990     const unit_kind kind;
991   };
992
993   /* Provide std::unordered_map::hasher for index_key.  */
994   class index_key_hasher
995   {
996   public:
997     size_t
998     operator () (const index_key &key) const
999     {
1000       return (std::hash<int>() (key.dwarf_tag) << 1) | key.is_static;
1001     }
1002   };
1003
1004   /* Parameters of one symbol entry.  */
1005   class symbol_value
1006   {
1007   public:
1008     const int dwarf_tag, cu_index;
1009     const bool is_static;
1010     const unit_kind kind;
1011
1012     symbol_value (int dwarf_tag_, int cu_index_, bool is_static_,
1013                   unit_kind kind_)
1014       : dwarf_tag (dwarf_tag_), cu_index (cu_index_), is_static (is_static_),
1015         kind (kind_)
1016     {}
1017
1018     bool
1019     operator< (const symbol_value &other) const
1020     {
1021 #define X(n) \
1022   do \
1023     { \
1024       if (n < other.n) \
1025         return true; \
1026       if (n > other.n) \
1027         return false; \
1028     } \
1029   while (0)
1030       X (dwarf_tag);
1031       X (is_static);
1032       X (kind);
1033       X (cu_index);
1034 #undef X
1035       return false;
1036     }
1037   };
1038
1039   /* Abstract base class to unify DWARF-32 and DWARF-64 name table
1040      output.  */
1041   class offset_vec
1042   {
1043   protected:
1044     const bfd_endian dwarf5_byte_order;
1045   public:
1046     explicit offset_vec (bfd_endian dwarf5_byte_order_)
1047       : dwarf5_byte_order (dwarf5_byte_order_)
1048     {}
1049
1050     /* Call std::vector::reserve for NELEM elements.  */
1051     virtual void reserve (size_t nelem) = 0;
1052
1053     /* Call std::vector::push_back with store_unsigned_integer byte
1054        reordering for ELEM.  */
1055     virtual void push_back_reorder (size_t elem) = 0;
1056
1057     /* Return expected output size in bytes.  */
1058     virtual size_t bytes () const = 0;
1059
1060     /* Write name table to FILE.  */
1061     virtual void file_write (FILE *file) const = 0;
1062   };
1063
1064   /* Template to unify DWARF-32 and DWARF-64 output.  */
1065   template<typename OffsetSize>
1066   class offset_vec_tmpl : public offset_vec
1067   {
1068   public:
1069     explicit offset_vec_tmpl (bfd_endian dwarf5_byte_order_)
1070       : offset_vec (dwarf5_byte_order_)
1071     {}
1072
1073     /* Implement offset_vec::reserve.  */
1074     void reserve (size_t nelem) override
1075     {
1076       m_vec.reserve (nelem);
1077     }
1078
1079     /* Implement offset_vec::push_back_reorder.  */
1080     void push_back_reorder (size_t elem) override
1081     {
1082       m_vec.push_back (elem);
1083       /* Check for overflow.  */
1084       gdb_assert (m_vec.back () == elem);
1085       store_unsigned_integer (reinterpret_cast<gdb_byte *> (&m_vec.back ()),
1086                               sizeof (m_vec.back ()), dwarf5_byte_order, elem);
1087     }
1088
1089     /* Implement offset_vec::bytes.  */
1090     size_t bytes () const override
1091     {
1092       return m_vec.size () * sizeof (m_vec[0]);
1093     }
1094
1095     /* Implement offset_vec::file_write.  */
1096     void file_write (FILE *file) const override
1097     {
1098       ::file_write (file, m_vec);
1099     }
1100
1101   private:
1102     std::vector<OffsetSize> m_vec;
1103   };
1104
1105   /* Base class to unify DWARF-32 and DWARF-64 .debug_names output
1106      respecting name table width.  */
1107   class dwarf
1108   {
1109   public:
1110     offset_vec &name_table_string_offs, &name_table_entry_offs;
1111
1112     dwarf (offset_vec &name_table_string_offs_,
1113            offset_vec &name_table_entry_offs_)
1114       : name_table_string_offs (name_table_string_offs_),
1115         name_table_entry_offs (name_table_entry_offs_)
1116     {
1117     }
1118   };
1119
1120   /* Template to unify DWARF-32 and DWARF-64 .debug_names output
1121      respecting name table width.  */
1122   template<typename OffsetSize>
1123   class dwarf_tmpl : public dwarf
1124   {
1125   public:
1126     explicit dwarf_tmpl (bfd_endian dwarf5_byte_order_)
1127       : dwarf (m_name_table_string_offs, m_name_table_entry_offs),
1128         m_name_table_string_offs (dwarf5_byte_order_),
1129         m_name_table_entry_offs (dwarf5_byte_order_)
1130     {}
1131
1132   private:
1133     offset_vec_tmpl<OffsetSize> m_name_table_string_offs;
1134     offset_vec_tmpl<OffsetSize> m_name_table_entry_offs;
1135   };
1136
1137   /* Try to reconstruct original DWARF tag for given partial_symbol.
1138      This function is not DWARF-5 compliant but it is sufficient for
1139      GDB as a DWARF-5 index consumer.  */
1140   static int psymbol_tag (const struct partial_symbol *psym)
1141   {
1142     domain_enum domain = PSYMBOL_DOMAIN (psym);
1143     enum address_class aclass = PSYMBOL_CLASS (psym);
1144
1145     switch (domain)
1146       {
1147       case VAR_DOMAIN:
1148         switch (aclass)
1149           {
1150           case LOC_BLOCK:
1151             return DW_TAG_subprogram;
1152           case LOC_TYPEDEF:
1153             return DW_TAG_typedef;
1154           case LOC_COMPUTED:
1155           case LOC_CONST_BYTES:
1156           case LOC_OPTIMIZED_OUT:
1157           case LOC_STATIC:
1158             return DW_TAG_variable;
1159           case LOC_CONST:
1160             /* Note: It's currently impossible to recognize psyms as enum values
1161                short of reading the type info.  For now punt.  */
1162             return DW_TAG_variable;
1163           default:
1164             /* There are other LOC_FOO values that one might want to classify
1165                as variables, but dwarf2read.c doesn't currently use them.  */
1166             return DW_TAG_variable;
1167           }
1168       case STRUCT_DOMAIN:
1169         return DW_TAG_structure_type;
1170       default:
1171         return 0;
1172       }
1173   }
1174
1175   /* Call insert for all partial symbols and mark them in PSYMS_SEEN.  */
1176   void write_psymbols (std::unordered_set<partial_symbol *> &psyms_seen,
1177                        struct partial_symbol **psymp, int count, int cu_index,
1178                        bool is_static, unit_kind kind)
1179   {
1180     for (; count-- > 0; ++psymp)
1181       {
1182         struct partial_symbol *psym = *psymp;
1183
1184         if (SYMBOL_LANGUAGE (psym) == language_ada)
1185           error (_("Ada is not currently supported by the index"));
1186
1187         /* Only add a given psymbol once.  */
1188         if (psyms_seen.insert (psym).second)
1189           insert (psym, cu_index, is_static, kind);
1190       }
1191   }
1192
1193   /* A helper function that writes a single signatured_type
1194      to a debug_names.  */
1195   void
1196   write_one_signatured_type (struct signatured_type *entry,
1197                              struct signatured_type_index_data *info)
1198   {
1199     struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
1200
1201     write_psymbols (info->psyms_seen,
1202                     &info->objfile->global_psymbols[psymtab->globals_offset],
1203                     psymtab->n_global_syms, info->cu_index, false,
1204                     unit_kind::tu);
1205     write_psymbols (info->psyms_seen,
1206                     &info->objfile->static_psymbols[psymtab->statics_offset],
1207                     psymtab->n_static_syms, info->cu_index, true,
1208                     unit_kind::tu);
1209
1210     info->types_list.append_uint (dwarf5_offset_size (), m_dwarf5_byte_order,
1211                                   to_underlying (entry->per_cu.sect_off));
1212
1213     ++info->cu_index;
1214   }
1215
1216   /* Store value of each symbol.  */
1217   std::unordered_map<c_str_view, std::set<symbol_value>, c_str_view_hasher>
1218     m_name_to_value_set;
1219
1220   /* Tables of DWARF-5 .debug_names.  They are in object file byte
1221      order.  */
1222   std::vector<uint32_t> m_bucket_table;
1223   std::vector<uint32_t> m_hash_table;
1224
1225   const bfd_endian m_dwarf5_byte_order;
1226   dwarf_tmpl<uint32_t> m_dwarf32;
1227   dwarf_tmpl<uint64_t> m_dwarf64;
1228   dwarf &m_dwarf;
1229   offset_vec &m_name_table_string_offs, &m_name_table_entry_offs;
1230   debug_str_lookup m_debugstrlookup;
1231
1232   /* Map each used .debug_names abbreviation tag parameter to its
1233      index value.  */
1234   std::unordered_map<index_key, int, index_key_hasher> m_indexkey_to_idx;
1235
1236   /* Next unused .debug_names abbreviation tag for
1237      m_indexkey_to_idx.  */
1238   int m_idx_next = 1;
1239
1240   /* .debug_names abbreviation table.  */
1241   data_buf m_abbrev_table;
1242
1243   /* .debug_names entry pool.  */
1244   data_buf m_entry_pool;
1245 };
1246
1247 /* Return iff any of the needed offsets does not fit into 32-bit
1248    .debug_names section.  */
1249
1250 static bool
1251 check_dwarf64_offsets (struct dwarf2_per_objfile *dwarf2_per_objfile)
1252 {
1253   for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
1254     {
1255       const dwarf2_per_cu_data &per_cu = *dwarf2_per_objfile->all_comp_units[i];
1256
1257       if (to_underlying (per_cu.sect_off) >= (static_cast<uint64_t> (1) << 32))
1258         return true;
1259     }
1260   for (int i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
1261     {
1262       const signatured_type &sigtype = *dwarf2_per_objfile->all_type_units[i];
1263       const dwarf2_per_cu_data &per_cu = sigtype.per_cu;
1264
1265       if (to_underlying (per_cu.sect_off) >= (static_cast<uint64_t> (1) << 32))
1266         return true;
1267     }
1268   return false;
1269 }
1270
1271 /* The psyms_seen set is potentially going to be largish (~40k
1272    elements when indexing a -g3 build of GDB itself).  Estimate the
1273    number of elements in order to avoid too many rehashes, which
1274    require rebuilding buckets and thus many trips to
1275    malloc/free.  */
1276
1277 static size_t
1278 psyms_seen_size (struct dwarf2_per_objfile *dwarf2_per_objfile)
1279 {
1280   size_t psyms_count = 0;
1281   for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
1282     {
1283       struct dwarf2_per_cu_data *per_cu
1284         = dwarf2_per_objfile->all_comp_units[i];
1285       struct partial_symtab *psymtab = per_cu->v.psymtab;
1286
1287       if (psymtab != NULL && psymtab->user == NULL)
1288         recursively_count_psymbols (psymtab, psyms_count);
1289     }
1290   /* Generating an index for gdb itself shows a ratio of
1291      TOTAL_SEEN_SYMS/UNIQUE_SYMS or ~5.  4 seems like a good bet.  */
1292   return psyms_count / 4;
1293 }
1294
1295 /* Write new .gdb_index section for OBJFILE into OUT_FILE.
1296    Return how many bytes were expected to be written into OUT_FILE.  */
1297
1298 static size_t
1299 write_gdbindex (struct dwarf2_per_objfile *dwarf2_per_objfile, FILE *out_file)
1300 {
1301   struct objfile *objfile = dwarf2_per_objfile->objfile;
1302   mapped_symtab symtab;
1303   data_buf cu_list;
1304
1305   /* While we're scanning CU's create a table that maps a psymtab pointer
1306      (which is what addrmap records) to its index (which is what is recorded
1307      in the index file).  This will later be needed to write the address
1308      table.  */
1309   psym_index_map cu_index_htab;
1310   cu_index_htab.reserve (dwarf2_per_objfile->n_comp_units);
1311
1312   /* The CU list is already sorted, so we don't need to do additional
1313      work here.  Also, the debug_types entries do not appear in
1314      all_comp_units, but only in their own hash table.  */
1315
1316   std::unordered_set<partial_symbol *> psyms_seen
1317     (psyms_seen_size (dwarf2_per_objfile));
1318   for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
1319     {
1320       struct dwarf2_per_cu_data *per_cu
1321         = dwarf2_per_objfile->all_comp_units[i];
1322       struct partial_symtab *psymtab = per_cu->v.psymtab;
1323
1324       /* CU of a shared file from 'dwz -m' may be unused by this main file.
1325          It may be referenced from a local scope but in such case it does not
1326          need to be present in .gdb_index.  */
1327       if (psymtab == NULL)
1328         continue;
1329
1330       if (psymtab->user == NULL)
1331         recursively_write_psymbols (objfile, psymtab, &symtab,
1332                                     psyms_seen, i);
1333
1334       const auto insertpair = cu_index_htab.emplace (psymtab, i);
1335       gdb_assert (insertpair.second);
1336
1337       cu_list.append_uint (8, BFD_ENDIAN_LITTLE,
1338                            to_underlying (per_cu->sect_off));
1339       cu_list.append_uint (8, BFD_ENDIAN_LITTLE, per_cu->length);
1340     }
1341
1342   /* Dump the address map.  */
1343   data_buf addr_vec;
1344   write_address_map (objfile, addr_vec, cu_index_htab);
1345
1346   /* Write out the .debug_type entries, if any.  */
1347   data_buf types_cu_list;
1348   if (dwarf2_per_objfile->signatured_types)
1349     {
1350       signatured_type_index_data sig_data (types_cu_list,
1351                                            psyms_seen);
1352
1353       sig_data.objfile = objfile;
1354       sig_data.symtab = &symtab;
1355       sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
1356       htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
1357                               write_one_signatured_type, &sig_data);
1358     }
1359
1360   /* Now that we've processed all symbols we can shrink their cu_indices
1361      lists.  */
1362   uniquify_cu_indices (&symtab);
1363
1364   data_buf symtab_vec, constant_pool;
1365   write_hash_table (&symtab, symtab_vec, constant_pool);
1366
1367   data_buf contents;
1368   const offset_type size_of_contents = 6 * sizeof (offset_type);
1369   offset_type total_len = size_of_contents;
1370
1371   /* The version number.  */
1372   contents.append_data (MAYBE_SWAP (8));
1373
1374   /* The offset of the CU list from the start of the file.  */
1375   contents.append_data (MAYBE_SWAP (total_len));
1376   total_len += cu_list.size ();
1377
1378   /* The offset of the types CU list from the start of the file.  */
1379   contents.append_data (MAYBE_SWAP (total_len));
1380   total_len += types_cu_list.size ();
1381
1382   /* The offset of the address table from the start of the file.  */
1383   contents.append_data (MAYBE_SWAP (total_len));
1384   total_len += addr_vec.size ();
1385
1386   /* The offset of the symbol table from the start of the file.  */
1387   contents.append_data (MAYBE_SWAP (total_len));
1388   total_len += symtab_vec.size ();
1389
1390   /* The offset of the constant pool from the start of the file.  */
1391   contents.append_data (MAYBE_SWAP (total_len));
1392   total_len += constant_pool.size ();
1393
1394   gdb_assert (contents.size () == size_of_contents);
1395
1396   contents.file_write (out_file);
1397   cu_list.file_write (out_file);
1398   types_cu_list.file_write (out_file);
1399   addr_vec.file_write (out_file);
1400   symtab_vec.file_write (out_file);
1401   constant_pool.file_write (out_file);
1402
1403   return total_len;
1404 }
1405
1406 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension.  */
1407 static const gdb_byte dwarf5_gdb_augmentation[] = { 'G', 'D', 'B', 0 };
1408
1409 /* Write a new .debug_names section for OBJFILE into OUT_FILE, write
1410    needed addition to .debug_str section to OUT_FILE_STR.  Return how
1411    many bytes were expected to be written into OUT_FILE.  */
1412
1413 static size_t
1414 write_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
1415                    FILE *out_file, FILE *out_file_str)
1416 {
1417   const bool dwarf5_is_dwarf64 = check_dwarf64_offsets (dwarf2_per_objfile);
1418   struct objfile *objfile = dwarf2_per_objfile->objfile;
1419   const enum bfd_endian dwarf5_byte_order
1420     = gdbarch_byte_order (get_objfile_arch (objfile));
1421
1422   /* The CU list is already sorted, so we don't need to do additional
1423      work here.  Also, the debug_types entries do not appear in
1424      all_comp_units, but only in their own hash table.  */
1425   data_buf cu_list;
1426   debug_names nametable (dwarf2_per_objfile, dwarf5_is_dwarf64,
1427                          dwarf5_byte_order);
1428   std::unordered_set<partial_symbol *>
1429     psyms_seen (psyms_seen_size (dwarf2_per_objfile));
1430   for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
1431     {
1432       const dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->all_comp_units[i];
1433       partial_symtab *psymtab = per_cu->v.psymtab;
1434
1435       /* CU of a shared file from 'dwz -m' may be unused by this main
1436          file.  It may be referenced from a local scope but in such
1437          case it does not need to be present in .debug_names.  */
1438       if (psymtab == NULL)
1439         continue;
1440
1441       if (psymtab->user == NULL)
1442         nametable.recursively_write_psymbols (objfile, psymtab, psyms_seen, i);
1443
1444       cu_list.append_uint (nametable.dwarf5_offset_size (), dwarf5_byte_order,
1445                            to_underlying (per_cu->sect_off));
1446     }
1447
1448   /* Write out the .debug_type entries, if any.  */
1449   data_buf types_cu_list;
1450   if (dwarf2_per_objfile->signatured_types)
1451     {
1452       debug_names::write_one_signatured_type_data sig_data (nametable,
1453                         signatured_type_index_data (types_cu_list, psyms_seen));
1454
1455       sig_data.info.objfile = objfile;
1456       /* It is used only for gdb_index.  */
1457       sig_data.info.symtab = nullptr;
1458       sig_data.info.cu_index = 0;
1459       htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
1460                               debug_names::write_one_signatured_type,
1461                               &sig_data);
1462     }
1463
1464   nametable.build ();
1465
1466   /* No addr_vec - DWARF-5 uses .debug_aranges generated by GCC.  */
1467
1468   const offset_type bytes_of_header
1469     = ((dwarf5_is_dwarf64 ? 12 : 4)
1470        + 2 + 2 + 7 * 4
1471        + sizeof (dwarf5_gdb_augmentation));
1472   size_t expected_bytes = 0;
1473   expected_bytes += bytes_of_header;
1474   expected_bytes += cu_list.size ();
1475   expected_bytes += types_cu_list.size ();
1476   expected_bytes += nametable.bytes ();
1477   data_buf header;
1478
1479   if (!dwarf5_is_dwarf64)
1480     {
1481       const uint64_t size64 = expected_bytes - 4;
1482       gdb_assert (size64 < 0xfffffff0);
1483       header.append_uint (4, dwarf5_byte_order, size64);
1484     }
1485   else
1486     {
1487       header.append_uint (4, dwarf5_byte_order, 0xffffffff);
1488       header.append_uint (8, dwarf5_byte_order, expected_bytes - 12);
1489     }
1490
1491   /* The version number.  */
1492   header.append_uint (2, dwarf5_byte_order, 5);
1493
1494   /* Padding.  */
1495   header.append_uint (2, dwarf5_byte_order, 0);
1496
1497   /* comp_unit_count - The number of CUs in the CU list.  */
1498   header.append_uint (4, dwarf5_byte_order, dwarf2_per_objfile->n_comp_units);
1499
1500   /* local_type_unit_count - The number of TUs in the local TU
1501      list.  */
1502   header.append_uint (4, dwarf5_byte_order, dwarf2_per_objfile->n_type_units);
1503
1504   /* foreign_type_unit_count - The number of TUs in the foreign TU
1505      list.  */
1506   header.append_uint (4, dwarf5_byte_order, 0);
1507
1508   /* bucket_count - The number of hash buckets in the hash lookup
1509      table.  */
1510   header.append_uint (4, dwarf5_byte_order, nametable.bucket_count ());
1511
1512   /* name_count - The number of unique names in the index.  */
1513   header.append_uint (4, dwarf5_byte_order, nametable.name_count ());
1514
1515   /* abbrev_table_size - The size in bytes of the abbreviations
1516      table.  */
1517   header.append_uint (4, dwarf5_byte_order, nametable.abbrev_table_bytes ());
1518
1519   /* augmentation_string_size - The size in bytes of the augmentation
1520      string.  This value is rounded up to a multiple of 4.  */
1521   static_assert (sizeof (dwarf5_gdb_augmentation) % 4 == 0, "");
1522   header.append_uint (4, dwarf5_byte_order, sizeof (dwarf5_gdb_augmentation));
1523   header.append_data (dwarf5_gdb_augmentation);
1524
1525   gdb_assert (header.size () == bytes_of_header);
1526
1527   header.file_write (out_file);
1528   cu_list.file_write (out_file);
1529   types_cu_list.file_write (out_file);
1530   nametable.file_write (out_file, out_file_str);
1531
1532   return expected_bytes;
1533 }
1534
1535 /* Assert that FILE's size is EXPECTED_SIZE.  Assumes file's seek
1536    position is at the end of the file.  */
1537
1538 static void
1539 assert_file_size (FILE *file, const char *filename, size_t expected_size)
1540 {
1541   const auto file_size = ftell (file);
1542   if (file_size == -1)
1543     error (_("Can't get `%s' size"), filename);
1544   gdb_assert (file_size == expected_size);
1545 }
1546
1547 /* Create an index file for OBJFILE in the directory DIR.  */
1548
1549 static void
1550 write_psymtabs_to_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
1551                          const char *dir,
1552                          dw_index_kind index_kind)
1553 {
1554   struct objfile *objfile = dwarf2_per_objfile->objfile;
1555
1556   if (dwarf2_per_objfile->using_index)
1557     error (_("Cannot use an index to create the index"));
1558
1559   if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
1560     error (_("Cannot make an index when the file has multiple .debug_types sections"));
1561
1562   if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
1563     return;
1564
1565   struct stat st;
1566   if (stat (objfile_name (objfile), &st) < 0)
1567     perror_with_name (objfile_name (objfile));
1568
1569   std::string filename (std::string (dir) + SLASH_STRING
1570                         + lbasename (objfile_name (objfile))
1571                         + (index_kind == dw_index_kind::DEBUG_NAMES
1572                            ? INDEX5_SUFFIX : INDEX4_SUFFIX));
1573
1574   FILE *out_file = gdb_fopen_cloexec (filename.c_str (), "wb").release ();
1575   if (!out_file)
1576     error (_("Can't open `%s' for writing"), filename.c_str ());
1577
1578   /* Order matters here; we want FILE to be closed before FILENAME is
1579      unlinked, because on MS-Windows one cannot delete a file that is
1580      still open.  (Don't call anything here that might throw until
1581      file_closer is created.)  */
1582   gdb::unlinker unlink_file (filename.c_str ());
1583   gdb_file_up close_out_file (out_file);
1584
1585   if (index_kind == dw_index_kind::DEBUG_NAMES)
1586     {
1587       std::string filename_str (std::string (dir) + SLASH_STRING
1588                                 + lbasename (objfile_name (objfile))
1589                                 + DEBUG_STR_SUFFIX);
1590       FILE *out_file_str
1591         = gdb_fopen_cloexec (filename_str.c_str (), "wb").release ();
1592       if (!out_file_str)
1593         error (_("Can't open `%s' for writing"), filename_str.c_str ());
1594       gdb::unlinker unlink_file_str (filename_str.c_str ());
1595       gdb_file_up close_out_file_str (out_file_str);
1596
1597       const size_t total_len
1598         = write_debug_names (dwarf2_per_objfile, out_file, out_file_str);
1599       assert_file_size (out_file, filename.c_str (), total_len);
1600
1601       /* We want to keep the file .debug_str file too.  */
1602       unlink_file_str.keep ();
1603     }
1604   else
1605     {
1606       const size_t total_len
1607         = write_gdbindex (dwarf2_per_objfile, out_file);
1608       assert_file_size (out_file, filename.c_str (), total_len);
1609     }
1610
1611   /* We want to keep the file.  */
1612   unlink_file.keep ();
1613 }
1614
1615 /* Implementation of the `save gdb-index' command.
1616
1617    Note that the .gdb_index file format used by this command is
1618    documented in the GDB manual.  Any changes here must be documented
1619    there.  */
1620
1621 static void
1622 save_gdb_index_command (const char *arg, int from_tty)
1623 {
1624   struct objfile *objfile;
1625   const char dwarf5space[] = "-dwarf-5 ";
1626   dw_index_kind index_kind = dw_index_kind::GDB_INDEX;
1627
1628   if (!arg)
1629     arg = "";
1630
1631   arg = skip_spaces (arg);
1632   if (strncmp (arg, dwarf5space, strlen (dwarf5space)) == 0)
1633     {
1634       index_kind = dw_index_kind::DEBUG_NAMES;
1635       arg += strlen (dwarf5space);
1636       arg = skip_spaces (arg);
1637     }
1638
1639   if (!*arg)
1640     error (_("usage: save gdb-index [-dwarf-5] DIRECTORY"));
1641
1642   ALL_OBJFILES (objfile)
1643   {
1644     struct stat st;
1645
1646     /* If the objfile does not correspond to an actual file, skip it.  */
1647     if (stat (objfile_name (objfile), &st) < 0)
1648       continue;
1649
1650     struct dwarf2_per_objfile *dwarf2_per_objfile
1651       = get_dwarf2_per_objfile (objfile);
1652
1653     if (dwarf2_per_objfile != NULL)
1654       {
1655         TRY
1656           {
1657             write_psymtabs_to_index (dwarf2_per_objfile, arg, index_kind);
1658           }
1659         CATCH (except, RETURN_MASK_ERROR)
1660           {
1661             exception_fprintf (gdb_stderr, except,
1662                                _("Error while writing index for `%s': "),
1663                                objfile_name (objfile));
1664           }
1665         END_CATCH
1666       }
1667
1668   }
1669 }
1670
1671 void
1672 _initialize_dwarf_index_write ()
1673 {
1674   cmd_list_element *c = add_cmd ("gdb-index", class_files,
1675                                  save_gdb_index_command, _("\
1676 Save a gdb-index file.\n\
1677 Usage: save gdb-index [-dwarf-5] DIRECTORY\n\
1678 \n\
1679 No options create one file with .gdb-index extension for pre-DWARF-5\n\
1680 compatible .gdb_index section.  With -dwarf-5 creates two files with\n\
1681 extension .debug_names and .debug_str for DWARF-5 .debug_names section."),
1682                &save_cmdlist);
1683   set_cmd_completer (c, filename_completer);
1684 }