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