ld: Generate PDB string table
[binutils-gdb.git] / ld / pdb.c
1 /* Support for generating PDB CodeView debugging files.
2 Copyright (C) 2022 Free Software Foundation, Inc.
3
4 This file is part of the GNU Binutils.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
20
21 #include "pdb.h"
22 #include "bfdlink.h"
23 #include "ld.h"
24 #include "ldmisc.h"
25 #include "libbfd.h"
26 #include "libiberty.h"
27 #include "coff/i386.h"
28 #include "coff/external.h"
29 #include "coff/internal.h"
30 #include "coff/pe.h"
31 #include "libcoff.h"
32 #include <time.h>
33
34 struct public
35 {
36 struct public *next;
37 uint32_t offset;
38 uint32_t hash;
39 unsigned int index;
40 uint16_t section;
41 uint32_t address;
42 };
43
44 struct string
45 {
46 struct string *next;
47 uint32_t hash;
48 uint32_t offset;
49 size_t len;
50 char s[];
51 };
52
53 struct string_table
54 {
55 struct string *strings_head;
56 struct string *strings_tail;
57 uint32_t strings_len;
58 htab_t hashmap;
59 };
60
61 /* Add a new stream to the PDB archive, and return its BFD. */
62 static bfd *
63 add_stream (bfd *pdb, const char *name, uint16_t *stream_num)
64 {
65 bfd *stream;
66 uint16_t num;
67
68 stream = bfd_create (name ? name : "", pdb);
69 if (!stream)
70 return NULL;
71
72 if (!bfd_make_writable (stream))
73 {
74 bfd_close (stream);
75 return false;
76 }
77
78 if (!pdb->archive_head)
79 {
80 bfd_set_archive_head (pdb, stream);
81 num = 0;
82 }
83 else
84 {
85 bfd *b = pdb->archive_head;
86
87 num = 1;
88
89 while (b->archive_next)
90 {
91 num++;
92 b = b->archive_next;
93 }
94
95 b->archive_next = stream;
96 }
97
98 if (stream_num)
99 *stream_num = num;
100
101 return stream;
102 }
103
104 /* Stream 0 ought to be a copy of the MSF directory from the last
105 time the PDB file was written. Because we don't do incremental
106 writes this isn't applicable to us, but we fill it with a dummy
107 value so as not to confuse radare. */
108 static bool
109 create_old_directory_stream (bfd *pdb)
110 {
111 bfd *stream;
112 char buf[sizeof (uint32_t)];
113
114 stream = add_stream (pdb, NULL, NULL);
115 if (!stream)
116 return false;
117
118 bfd_putl32 (0, buf);
119
120 return bfd_bwrite (buf, sizeof (uint32_t), stream) == sizeof (uint32_t);
121 }
122
123 /* Calculate the hash of a given string. */
124 static uint32_t
125 calc_hash (const char *data, size_t len)
126 {
127 uint32_t hash = 0;
128
129 while (len >= 4)
130 {
131 hash ^= data[0];
132 hash ^= data[1] << 8;
133 hash ^= data[2] << 16;
134 hash ^= data[3] << 24;
135
136 data += 4;
137 len -= 4;
138 }
139
140 if (len >= 2)
141 {
142 hash ^= data[0];
143 hash ^= data[1] << 8;
144
145 data += 2;
146 len -= 2;
147 }
148
149 if (len != 0)
150 hash ^= *data;
151
152 hash |= 0x20202020;
153 hash ^= (hash >> 11);
154
155 return hash ^ (hash >> 16);
156 }
157
158 /* Stream 1 is the PDB info stream - see
159 https://llvm.org/docs/PDB/PdbStream.html. */
160 static bool
161 populate_info_stream (bfd *pdb, bfd *info_stream, const unsigned char *guid)
162 {
163 bool ret = false;
164 struct pdb_stream_70 h;
165 uint32_t num_entries, num_buckets;
166 uint32_t names_length, stream_num;
167 char int_buf[sizeof (uint32_t)];
168
169 struct hash_entry
170 {
171 uint32_t offset;
172 uint32_t value;
173 };
174
175 struct hash_entry **buckets = NULL;
176
177 /* Write header. */
178
179 bfd_putl32 (PDB_STREAM_VERSION_VC70, &h.version);
180 bfd_putl32 (time (NULL), &h.signature);
181 bfd_putl32 (1, &h.age);
182
183 bfd_putl32 (bfd_getb32 (guid), h.guid);
184 bfd_putl16 (bfd_getb16 (&guid[4]), &h.guid[4]);
185 bfd_putl16 (bfd_getb16 (&guid[6]), &h.guid[6]);
186 memcpy (&h.guid[8], &guid[8], 8);
187
188 if (bfd_bwrite (&h, sizeof (h), info_stream) != sizeof (h))
189 return false;
190
191 /* Write hash list of named streams. This is a "rollover" hash, i.e.
192 if a bucket is filled an entry gets placed in the next free
193 slot. */
194
195 num_entries = 0;
196 for (bfd *b = pdb->archive_head; b; b = b->archive_next)
197 {
198 if (strcmp (b->filename, ""))
199 num_entries++;
200 }
201
202 num_buckets = num_entries * 2;
203
204 names_length = 0;
205 stream_num = 0;
206
207 if (num_buckets > 0)
208 {
209 buckets = xmalloc (sizeof (struct hash_entry *) * num_buckets);
210 memset (buckets, 0, sizeof (struct hash_entry *) * num_buckets);
211
212 for (bfd *b = pdb->archive_head; b; b = b->archive_next)
213 {
214 if (strcmp (b->filename, ""))
215 {
216 size_t len = strlen (b->filename);
217 uint32_t hash = (uint16_t) calc_hash (b->filename, len);
218 uint32_t bucket_num = hash % num_buckets;
219
220 while (buckets[bucket_num])
221 {
222 bucket_num++;
223
224 if (bucket_num == num_buckets)
225 bucket_num = 0;
226 }
227
228 buckets[bucket_num] = xmalloc (sizeof (struct hash_entry));
229
230 buckets[bucket_num]->offset = names_length;
231 buckets[bucket_num]->value = stream_num;
232
233 names_length += len + 1;
234 }
235
236 stream_num++;
237 }
238 }
239
240 /* Write the strings list - the hash keys are indexes into this. */
241
242 bfd_putl32 (names_length, int_buf);
243
244 if (bfd_bwrite (int_buf, sizeof (uint32_t), info_stream) !=
245 sizeof (uint32_t))
246 goto end;
247
248 for (bfd *b = pdb->archive_head; b; b = b->archive_next)
249 {
250 if (!strcmp (b->filename, ""))
251 continue;
252
253 size_t len = strlen (b->filename) + 1;
254
255 if (bfd_bwrite (b->filename, len, info_stream) != len)
256 goto end;
257 }
258
259 /* Write the number of entries and buckets. */
260
261 bfd_putl32 (num_entries, int_buf);
262
263 if (bfd_bwrite (int_buf, sizeof (uint32_t), info_stream) !=
264 sizeof (uint32_t))
265 goto end;
266
267 bfd_putl32 (num_buckets, int_buf);
268
269 if (bfd_bwrite (int_buf, sizeof (uint32_t), info_stream) !=
270 sizeof (uint32_t))
271 goto end;
272
273 /* Write the present bitmap. */
274
275 bfd_putl32 ((num_buckets + 31) / 32, int_buf);
276
277 if (bfd_bwrite (int_buf, sizeof (uint32_t), info_stream) !=
278 sizeof (uint32_t))
279 goto end;
280
281 for (unsigned int i = 0; i < num_buckets; i += 32)
282 {
283 uint32_t v = 0;
284
285 for (unsigned int j = 0; j < 32; j++)
286 {
287 if (i + j >= num_buckets)
288 break;
289
290 if (buckets[i + j])
291 v |= 1 << j;
292 }
293
294 bfd_putl32 (v, int_buf);
295
296 if (bfd_bwrite (int_buf, sizeof (uint32_t), info_stream) !=
297 sizeof (uint32_t))
298 goto end;
299 }
300
301 /* Write the (empty) deleted bitmap. */
302
303 bfd_putl32 (0, int_buf);
304
305 if (bfd_bwrite (int_buf, sizeof (uint32_t), info_stream) !=
306 sizeof (uint32_t))
307 goto end;
308
309 /* Write the buckets. */
310
311 for (unsigned int i = 0; i < num_buckets; i++)
312 {
313 if (buckets[i])
314 {
315 bfd_putl32 (buckets[i]->offset, int_buf);
316
317 if (bfd_bwrite (int_buf, sizeof (uint32_t), info_stream) !=
318 sizeof (uint32_t))
319 goto end;
320
321 bfd_putl32 (buckets[i]->value, int_buf);
322
323 if (bfd_bwrite (int_buf, sizeof (uint32_t), info_stream) !=
324 sizeof (uint32_t))
325 goto end;
326 }
327 }
328
329 bfd_putl32 (0, int_buf);
330
331 if (bfd_bwrite (int_buf, sizeof (uint32_t), info_stream) !=
332 sizeof (uint32_t))
333 goto end;
334
335 bfd_putl32 (PDB_STREAM_VERSION_VC140, int_buf);
336
337 if (bfd_bwrite (int_buf, sizeof (uint32_t), info_stream) !=
338 sizeof (uint32_t))
339 goto end;
340
341 ret = true;
342
343 end:
344 for (unsigned int i = 0; i < num_buckets; i++)
345 {
346 if (buckets[i])
347 free (buckets[i]);
348 }
349
350 free (buckets);
351
352 return ret;
353 }
354
355 /* Stream 2 is the type information (TPI) stream, and stream 4 is
356 the ID information (IPI) stream. They differ only in which records
357 go in which stream. */
358 static bool
359 create_type_stream (bfd *pdb)
360 {
361 bfd *stream;
362 struct pdb_tpi_stream_header h;
363
364 stream = add_stream (pdb, NULL, NULL);
365 if (!stream)
366 return false;
367
368 bfd_putl32 (TPI_STREAM_VERSION_80, &h.version);
369 bfd_putl32 (sizeof (h), &h.header_size);
370 bfd_putl32 (TPI_FIRST_INDEX, &h.type_index_begin);
371 bfd_putl32 (TPI_FIRST_INDEX, &h.type_index_end);
372 bfd_putl32 (0, &h.type_record_bytes);
373 bfd_putl16 (0xffff, &h.hash_stream_index);
374 bfd_putl16 (0xffff, &h.hash_aux_stream_index);
375 bfd_putl32 (4, &h.hash_key_size);
376 bfd_putl32 (0x3ffff, &h.num_hash_buckets);
377 bfd_putl32 (0, &h.hash_value_buffer_offset);
378 bfd_putl32 (0, &h.hash_value_buffer_length);
379 bfd_putl32 (0, &h.index_offset_buffer_offset);
380 bfd_putl32 (0, &h.index_offset_buffer_length);
381 bfd_putl32 (0, &h.hash_adj_buffer_offset);
382 bfd_putl32 (0, &h.hash_adj_buffer_length);
383
384 if (bfd_bwrite (&h, sizeof (h), stream) != sizeof (h))
385 return false;
386
387 return true;
388 }
389
390 /* Return the PE architecture number for the image. */
391 static uint16_t
392 get_arch_number (bfd *abfd)
393 {
394 if (abfd->arch_info->arch != bfd_arch_i386)
395 return 0;
396
397 if (abfd->arch_info->mach & bfd_mach_x86_64)
398 return IMAGE_FILE_MACHINE_AMD64;
399
400 return IMAGE_FILE_MACHINE_I386;
401 }
402
403 /* Add a string to the strings table, if it's not already there. */
404 static void
405 add_string (char *str, size_t len, struct string_table *strings)
406 {
407 uint32_t hash = calc_hash (str, len);
408 void **slot;
409
410 slot = htab_find_slot_with_hash (strings->hashmap, str, hash, INSERT);
411
412 if (!*slot)
413 {
414 struct string *s;
415
416 *slot = xmalloc (offsetof (struct string, s) + len);
417
418 s = (struct string *) *slot;
419
420 s->next = NULL;
421 s->hash = hash;
422 s->offset = strings->strings_len;
423 s->len = len;
424 memcpy (s->s, str, len);
425
426 if (strings->strings_tail)
427 strings->strings_tail->next = s;
428 else
429 strings->strings_head = s;
430
431 strings->strings_tail = s;
432
433 strings->strings_len += len + 1;
434 }
435 }
436
437 /* Return the hash of an entry in the string table. */
438 static hashval_t
439 hash_string_table_entry (const void *p)
440 {
441 const struct string *s = (const struct string *) p;
442
443 return s->hash;
444 }
445
446 /* Compare an entry in the string table with a string. */
447 static int
448 eq_string_table_entry (const void *a, const void *b)
449 {
450 const struct string *s1 = (const struct string *) a;
451 const char *s2 = (const char *) b;
452 size_t s2_len = strlen (s2);
453
454 if (s2_len != s1->len)
455 return 0;
456
457 return memcmp (s1->s, s2, s2_len) == 0;
458 }
459
460 /* Parse the string table within the .debug$S section. */
461 static void
462 parse_string_table (bfd_byte *data, size_t size,
463 struct string_table *strings)
464 {
465 while (true)
466 {
467 size_t len = strnlen ((char *) data, size);
468
469 add_string ((char *) data, len, strings);
470
471 data += len + 1;
472
473 if (size <= len + 1)
474 break;
475
476 size -= len + 1;
477 }
478 }
479
480 /* Parse the .debug$S section within an object file. */
481 static bool
482 handle_debugs_section (asection *s, bfd *mod, struct string_table *strings)
483 {
484 bfd_byte *data = NULL;
485 size_t off;
486
487 if (!bfd_get_full_section_contents (mod, s, &data))
488 return false;
489
490 if (!data)
491 return false;
492
493 if (bfd_getl32 (data) != CV_SIGNATURE_C13)
494 {
495 free (data);
496 return true;
497 }
498
499 off = sizeof (uint32_t);
500
501 while (off + sizeof (uint32_t) <= s->size)
502 {
503 uint32_t type, size;
504
505 type = bfd_getl32 (data + off);
506
507 off += sizeof (uint32_t);
508
509 if (off + sizeof (uint32_t) > s->size)
510 {
511 free (data);
512 bfd_set_error (bfd_error_bad_value);
513 return false;
514 }
515
516 size = bfd_getl32 (data + off);
517
518 off += sizeof (uint32_t);
519
520 if (off + size > s->size)
521 {
522 free (data);
523 bfd_set_error (bfd_error_bad_value);
524 return false;
525 }
526
527 switch (type)
528 {
529 case DEBUG_S_STRINGTABLE:
530 parse_string_table (data + off, size, strings);
531
532 break;
533 }
534
535 off += size;
536
537 if (off % sizeof (uint32_t))
538 off += sizeof (uint32_t) - (off % sizeof (uint32_t));
539 }
540
541 free (data);
542
543 return true;
544 }
545
546 /* Populate the module stream, which consists of the transformed .debug$S
547 data for each object file. */
548 static bool
549 populate_module_stream (bfd *stream, bfd *mod, uint32_t *sym_byte_size,
550 struct string_table *strings)
551 {
552 uint8_t int_buf[sizeof (uint32_t)];
553
554 *sym_byte_size = sizeof (uint32_t);
555
556 /* Process .debug$S section(s). */
557
558 for (asection *s = mod->sections; s; s = s->next)
559 {
560 if (!strcmp (s->name, ".debug$S") && s->size >= sizeof (uint32_t))
561 {
562 if (!handle_debugs_section (s, mod, strings))
563 return false;
564 }
565 }
566
567 /* Write the signature. */
568
569 bfd_putl32 (CV_SIGNATURE_C13, int_buf);
570
571 if (bfd_bwrite (int_buf, sizeof (uint32_t), stream) != sizeof (uint32_t))
572 return false;
573
574 /* Write the global refs size. */
575
576 bfd_putl32 (0, int_buf);
577
578 if (bfd_bwrite (int_buf, sizeof (uint32_t), stream) != sizeof (uint32_t))
579 return false;
580
581 return true;
582 }
583
584 /* Create the module info substream within the DBI. */
585 static bool
586 create_module_info_substream (bfd *abfd, bfd *pdb, void **data,
587 uint32_t *size, struct string_table *strings)
588 {
589 uint8_t *ptr;
590
591 static const char linker_fn[] = "* Linker *";
592
593 *size = 0;
594
595 for (bfd *in = coff_data (abfd)->link_info->input_bfds; in;
596 in = in->link.next)
597 {
598 size_t len = sizeof (struct module_info);
599
600 if (!strcmp (bfd_get_filename (in), "dll stuff"))
601 {
602 len += sizeof (linker_fn); /* Object name. */
603 len++; /* Empty module name. */
604 }
605 else if (in->my_archive)
606 {
607 char *name = lrealpath (bfd_get_filename (in));
608
609 len += strlen (name) + 1; /* Object name. */
610
611 free (name);
612
613 name = lrealpath (bfd_get_filename (in->my_archive));
614
615 len += strlen (name) + 1; /* Archive name. */
616
617 free (name);
618 }
619 else
620 {
621 char *name = lrealpath (bfd_get_filename (in));
622 size_t name_len = strlen (name) + 1;
623
624 len += name_len; /* Object name. */
625 len += name_len; /* And again as the archive name. */
626
627 free (name);
628 }
629
630 if (len % 4)
631 len += 4 - (len % 4);
632
633 *size += len;
634 }
635
636 *data = xmalloc (*size);
637
638 ptr = *data;
639
640 for (bfd *in = coff_data (abfd)->link_info->input_bfds; in;
641 in = in->link.next)
642 {
643 struct module_info *mod = (struct module_info *) ptr;
644 uint16_t stream_num;
645 bfd *stream;
646 uint32_t sym_byte_size;
647 uint8_t *start = ptr;
648
649 stream = add_stream (pdb, NULL, &stream_num);
650
651 if (!stream)
652 {
653 free (*data);
654 return false;
655 }
656
657 if (!populate_module_stream (stream, in, &sym_byte_size,
658 strings))
659 {
660 free (*data);
661 return false;
662 }
663
664 bfd_putl32 (0, &mod->unused1);
665
666 /* These are dummy values - MSVC copies the first section contribution
667 entry here, but doesn't seem to use it for anything. */
668 bfd_putl16 (0xffff, &mod->sc.section);
669 bfd_putl16 (0, &mod->sc.padding1);
670 bfd_putl32 (0, &mod->sc.offset);
671 bfd_putl32 (0xffffffff, &mod->sc.size);
672 bfd_putl32 (0, &mod->sc.characteristics);
673 bfd_putl16 (0xffff, &mod->sc.module_index);
674 bfd_putl16 (0, &mod->sc.padding2);
675 bfd_putl32 (0, &mod->sc.data_crc);
676 bfd_putl32 (0, &mod->sc.reloc_crc);
677
678 bfd_putl16 (0, &mod->flags);
679 bfd_putl16 (stream_num, &mod->module_sym_stream);
680 bfd_putl32 (sym_byte_size, &mod->sym_byte_size);
681 bfd_putl32 (0, &mod->c11_byte_size);
682 bfd_putl32 (0, &mod->c13_byte_size);
683 bfd_putl16 (0, &mod->source_file_count);
684 bfd_putl16 (0, &mod->padding);
685 bfd_putl32 (0, &mod->unused2);
686 bfd_putl32 (0, &mod->source_file_name_index);
687 bfd_putl32 (0, &mod->pdb_file_path_name_index);
688
689 ptr += sizeof (struct module_info);
690
691 if (!strcmp (bfd_get_filename (in), "dll stuff"))
692 {
693 /* Object name. */
694 memcpy (ptr, linker_fn, sizeof (linker_fn));
695 ptr += sizeof (linker_fn);
696
697 /* Empty module name. */
698 *ptr = 0;
699 ptr++;
700 }
701 else if (in->my_archive)
702 {
703 char *name = lrealpath (bfd_get_filename (in));
704 size_t name_len = strlen (name) + 1;
705
706 /* Object name. */
707 memcpy (ptr, name, name_len);
708 ptr += name_len;
709
710 free (name);
711
712 name = lrealpath (bfd_get_filename (in->my_archive));
713 name_len = strlen (name) + 1;
714
715 /* Archive name. */
716 memcpy (ptr, name, name_len);
717 ptr += name_len;
718
719 free (name);
720 }
721 else
722 {
723 char *name = lrealpath (bfd_get_filename (in));
724 size_t name_len = strlen (name) + 1;
725
726 /* Object name. */
727 memcpy (ptr, name, name_len);
728 ptr += name_len;
729
730 /* Object name again as archive name. */
731 memcpy (ptr, name, name_len);
732 ptr += name_len;
733
734 free (name);
735 }
736
737 /* Pad to next four-byte boundary. */
738
739 if ((ptr - start) % 4)
740 {
741 memset (ptr, 0, 4 - ((ptr - start) % 4));
742 ptr += 4 - ((ptr - start) % 4);
743 }
744 }
745
746 return true;
747 }
748
749 /* Return the index of a given output section. */
750 static uint16_t
751 find_section_number (bfd *abfd, asection *sect)
752 {
753 uint16_t i = 1;
754
755 for (asection *s = abfd->sections; s; s = s->next)
756 {
757 if (s == sect)
758 return i;
759
760 /* Empty sections aren't output. */
761 if (s->size != 0)
762 i++;
763 }
764
765 return 0;
766 }
767
768 /* Create the substream which maps addresses in the image file to locations
769 in the original object files. */
770 static bool
771 create_section_contrib_substream (bfd *abfd, void **data, uint32_t *size)
772 {
773 unsigned int num_sc = 0;
774 struct section_contribution *sc;
775 uint16_t mod_index;
776 char *sect_flags;
777 file_ptr offset;
778
779 for (bfd *in = coff_data (abfd)->link_info->input_bfds; in;
780 in = in->link.next)
781 {
782 for (asection *s = in->sections; s; s = s->next)
783 {
784 if (s->size == 0 || discarded_section (s))
785 continue;
786
787 num_sc++;
788 }
789 }
790
791 *size = sizeof (uint32_t) + (num_sc * sizeof (struct section_contribution));
792 *data = xmalloc (*size);
793
794 bfd_putl32 (SECTION_CONTRIB_VERSION_60, *data);
795
796 /* Read characteristics of outputted sections. */
797
798 sect_flags = xmalloc (sizeof (uint32_t) * abfd->section_count);
799
800 offset = bfd_coff_filhsz (abfd) + bfd_coff_aoutsz (abfd);
801 offset += offsetof (struct external_scnhdr, s_flags);
802
803 for (unsigned int i = 0; i < abfd->section_count; i++)
804 {
805 bfd_seek (abfd, offset, SEEK_SET);
806
807 if (bfd_bread (sect_flags + (i * sizeof (uint32_t)), sizeof (uint32_t),
808 abfd) != sizeof (uint32_t))
809 {
810 free (*data);
811 free (sect_flags);
812 return false;
813 }
814
815 offset += sizeof (struct external_scnhdr);
816 }
817
818 sc =
819 (struct section_contribution *) ((uint8_t *) *data + sizeof (uint32_t));
820
821 mod_index = 0;
822 for (bfd *in = coff_data (abfd)->link_info->input_bfds; in;
823 in = in->link.next)
824 {
825 for (asection *s = in->sections; s; s = s->next)
826 {
827 uint16_t sect_num;
828
829 if (s->size == 0 || discarded_section (s))
830 continue;
831
832 sect_num = find_section_number (abfd, s->output_section);
833
834 memcpy (&sc->characteristics,
835 sect_flags + ((sect_num - 1) * sizeof (uint32_t)),
836 sizeof (uint32_t));
837
838 bfd_putl16 (sect_num, &sc->section);
839 bfd_putl16 (0, &sc->padding1);
840 bfd_putl32 (s->output_offset, &sc->offset);
841 bfd_putl32 (s->size, &sc->size);
842 bfd_putl16 (mod_index, &sc->module_index);
843 bfd_putl16 (0, &sc->padding2);
844 bfd_putl32 (0, &sc->data_crc);
845 bfd_putl32 (0, &sc->reloc_crc);
846
847 sc++;
848 }
849
850 mod_index++;
851 }
852
853 free (sect_flags);
854
855 return true;
856 }
857
858 /* Stream 4 is the debug information (DBI) stream. */
859 static bool
860 populate_dbi_stream (bfd *stream, bfd *abfd, bfd *pdb,
861 uint16_t section_header_stream_num,
862 uint16_t sym_rec_stream_num,
863 uint16_t publics_stream_num,
864 struct string_table *strings)
865 {
866 struct pdb_dbi_stream_header h;
867 struct optional_dbg_header opt;
868 void *mod_info, *sc;
869 uint32_t mod_info_size, sc_size;
870
871 if (!create_module_info_substream (abfd, pdb, &mod_info, &mod_info_size,
872 strings))
873 return false;
874
875 if (!create_section_contrib_substream (abfd, &sc, &sc_size))
876 {
877 free (mod_info);
878 return false;
879 }
880
881 bfd_putl32 (0xffffffff, &h.version_signature);
882 bfd_putl32 (DBI_STREAM_VERSION_70, &h.version_header);
883 bfd_putl32 (1, &h.age);
884 bfd_putl16 (0xffff, &h.global_stream_index);
885 bfd_putl16 (0x8e1d, &h.build_number); // MSVC 14.29
886 bfd_putl16 (publics_stream_num, &h.public_stream_index);
887 bfd_putl16 (0, &h.pdb_dll_version);
888 bfd_putl16 (sym_rec_stream_num, &h.sym_record_stream);
889 bfd_putl16 (0, &h.pdb_dll_rbld);
890 bfd_putl32 (mod_info_size, &h.mod_info_size);
891 bfd_putl32 (sc_size, &h.section_contribution_size);
892 bfd_putl32 (0, &h.section_map_size);
893 bfd_putl32 (0, &h.source_info_size);
894 bfd_putl32 (0, &h.type_server_map_size);
895 bfd_putl32 (0, &h.mfc_type_server_index);
896 bfd_putl32 (sizeof (opt), &h.optional_dbg_header_size);
897 bfd_putl32 (0, &h.ec_substream_size);
898 bfd_putl16 (0, &h.flags);
899 bfd_putl16 (get_arch_number (abfd), &h.machine);
900 bfd_putl32 (0, &h.padding);
901
902 if (bfd_bwrite (&h, sizeof (h), stream) != sizeof (h))
903 {
904 free (sc);
905 free (mod_info);
906 return false;
907 }
908
909 if (bfd_bwrite (mod_info, mod_info_size, stream) != mod_info_size)
910 {
911 free (sc);
912 free (mod_info);
913 return false;
914 }
915
916 free (mod_info);
917
918 if (bfd_bwrite (sc, sc_size, stream) != sc_size)
919 {
920 free (sc);
921 return false;
922 }
923
924 free (sc);
925
926 bfd_putl16 (0xffff, &opt.fpo_stream);
927 bfd_putl16 (0xffff, &opt.exception_stream);
928 bfd_putl16 (0xffff, &opt.fixup_stream);
929 bfd_putl16 (0xffff, &opt.omap_to_src_stream);
930 bfd_putl16 (0xffff, &opt.omap_from_src_stream);
931 bfd_putl16 (section_header_stream_num, &opt.section_header_stream);
932 bfd_putl16 (0xffff, &opt.token_map_stream);
933 bfd_putl16 (0xffff, &opt.xdata_stream);
934 bfd_putl16 (0xffff, &opt.pdata_stream);
935 bfd_putl16 (0xffff, &opt.new_fpo_stream);
936 bfd_putl16 (0xffff, &opt.orig_section_header_stream);
937
938 if (bfd_bwrite (&opt, sizeof (opt), stream) != sizeof (opt))
939 return false;
940
941 return true;
942 }
943
944 /* Used as parameter to qsort, to sort publics by hash. */
945 static int
946 public_compare_hash (const void *s1, const void *s2)
947 {
948 const struct public *p1 = *(const struct public **) s1;
949 const struct public *p2 = *(const struct public **) s2;
950
951 if (p1->hash < p2->hash)
952 return -1;
953 if (p1->hash > p2->hash)
954 return 1;
955
956 return 0;
957 }
958
959 /* Used as parameter to qsort, to sort publics by address. */
960 static int
961 public_compare_addr (const void *s1, const void *s2)
962 {
963 const struct public *p1 = *(const struct public **) s1;
964 const struct public *p2 = *(const struct public **) s2;
965
966 if (p1->section < p2->section)
967 return -1;
968 if (p1->section > p2->section)
969 return 1;
970
971 if (p1->address < p2->address)
972 return -1;
973 if (p1->address > p2->address)
974 return 1;
975
976 return 0;
977 }
978
979 /* The publics stream is a hash map of S_PUB32 records, which are stored
980 in the symbol record stream. Each S_PUB32 entry represents a symbol
981 from the point of view of the linker: a section index, an offset within
982 the section, and a mangled name. Compare with S_GDATA32 and S_GPROC32,
983 which are the same thing but generated by the compiler. */
984 static bool
985 populate_publics_stream (bfd *stream, bfd *abfd, bfd *sym_rec_stream)
986 {
987 struct publics_header header;
988 struct globals_hash_header hash_header;
989 const unsigned int num_buckets = 4096;
990 unsigned int num_entries = 0, filled_buckets = 0;
991 unsigned int buckets_size, sym_hash_size;
992 char int_buf[sizeof (uint32_t)];
993 struct public *publics_head = NULL, *publics_tail = NULL;
994 struct public **buckets;
995 struct public **sorted = NULL;
996 bool ret = false;
997
998 buckets = xmalloc (sizeof (struct public *) * num_buckets);
999 memset (buckets, 0, sizeof (struct public *) * num_buckets);
1000
1001 /* Loop through the global symbols in our input files, and write S_PUB32
1002 records in the symbol record stream for those that make it into the
1003 final image. */
1004 for (bfd *in = coff_data (abfd)->link_info->input_bfds; in;
1005 in = in->link.next)
1006 {
1007 for (unsigned int i = 0; i < in->symcount; i++)
1008 {
1009 struct bfd_symbol *sym = in->outsymbols[i];
1010
1011 if (sym->flags & BSF_GLOBAL)
1012 {
1013 struct pubsym ps;
1014 uint16_t record_length;
1015 const char *name = sym->name;
1016 size_t name_len = strlen (name);
1017 struct public *p = xmalloc (sizeof (struct public));
1018 unsigned int padding = 0;
1019 uint16_t section;
1020 uint32_t flags = 0;
1021
1022 section =
1023 find_section_number (abfd, sym->section->output_section);
1024
1025 if (section == 0)
1026 continue;
1027
1028 p->next = NULL;
1029 p->offset = bfd_tell (sym_rec_stream);
1030 p->hash = calc_hash (name, name_len) % num_buckets;
1031 p->section = section;
1032 p->address = sym->section->output_offset + sym->value;
1033
1034 record_length = sizeof (struct pubsym) + name_len + 1;
1035
1036 if (record_length % 4)
1037 padding = 4 - (record_length % 4);
1038
1039 /* Assume that all global symbols in executable sections
1040 are functions. */
1041 if (sym->section->flags & SEC_CODE)
1042 flags = PUBSYM_FUNCTION;
1043
1044 bfd_putl16 (record_length + padding - sizeof (uint16_t),
1045 &ps.record_length);
1046 bfd_putl16 (S_PUB32, &ps.record_type);
1047 bfd_putl32 (flags, &ps.flags);
1048 bfd_putl32 (p->address, &ps.offset);
1049 bfd_putl16 (p->section, &ps.section);
1050
1051 if (bfd_bwrite (&ps, sizeof (struct pubsym), sym_rec_stream) !=
1052 sizeof (struct pubsym))
1053 goto end;
1054
1055 if (bfd_bwrite (name, name_len + 1, sym_rec_stream) !=
1056 name_len + 1)
1057 goto end;
1058
1059 for (unsigned int j = 0; j < padding; j++)
1060 {
1061 uint8_t b = 0;
1062
1063 if (bfd_bwrite (&b, sizeof (uint8_t), sym_rec_stream) !=
1064 sizeof (uint8_t))
1065 goto end;
1066 }
1067
1068 if (!publics_head)
1069 publics_head = p;
1070 else
1071 publics_tail->next = p;
1072
1073 publics_tail = p;
1074 num_entries++;
1075 }
1076 }
1077 }
1078
1079
1080 if (num_entries > 0)
1081 {
1082 /* Create an array of pointers, sorted by hash value. */
1083
1084 sorted = xmalloc (sizeof (struct public *) * num_entries);
1085
1086 struct public *p = publics_head;
1087 for (unsigned int i = 0; i < num_entries; i++)
1088 {
1089 sorted[i] = p;
1090 p = p->next;
1091 }
1092
1093 qsort (sorted, num_entries, sizeof (struct public *),
1094 public_compare_hash);
1095
1096 /* Populate the buckets. */
1097
1098 for (unsigned int i = 0; i < num_entries; i++)
1099 {
1100 if (!buckets[sorted[i]->hash])
1101 {
1102 buckets[sorted[i]->hash] = sorted[i];
1103 filled_buckets++;
1104 }
1105
1106 sorted[i]->index = i;
1107 }
1108 }
1109
1110 buckets_size = num_buckets / 8;
1111 buckets_size += sizeof (uint32_t);
1112 buckets_size += filled_buckets * sizeof (uint32_t);
1113
1114 sym_hash_size = sizeof (hash_header);
1115 sym_hash_size += num_entries * sizeof (struct hash_record);
1116 sym_hash_size += buckets_size;
1117
1118 /* Output the publics header. */
1119
1120 bfd_putl32 (sym_hash_size, &header.sym_hash_size);
1121 bfd_putl32 (num_entries * sizeof (uint32_t), &header.addr_map_size);
1122 bfd_putl32 (0, &header.num_thunks);
1123 bfd_putl32 (0, &header.thunks_size);
1124 bfd_putl32 (0, &header.thunk_table);
1125 bfd_putl32 (0, &header.thunk_table_offset);
1126 bfd_putl32 (0, &header.num_sects);
1127
1128 if (bfd_bwrite (&header, sizeof (header), stream) != sizeof (header))
1129 goto end;
1130
1131 /* Output the global hash header. */
1132
1133 bfd_putl32 (GLOBALS_HASH_SIGNATURE, &hash_header.signature);
1134 bfd_putl32 (GLOBALS_HASH_VERSION_70, &hash_header.version);
1135 bfd_putl32 (num_entries * sizeof (struct hash_record),
1136 &hash_header.entries_size);
1137 bfd_putl32 (buckets_size, &hash_header.buckets_size);
1138
1139 if (bfd_bwrite (&hash_header, sizeof (hash_header), stream) !=
1140 sizeof (hash_header))
1141 goto end;
1142
1143 /* Write the entries in hash order. */
1144
1145 for (unsigned int i = 0; i < num_entries; i++)
1146 {
1147 struct hash_record hr;
1148
1149 bfd_putl32 (sorted[i]->offset + 1, &hr.offset);
1150 bfd_putl32 (1, &hr.reference);
1151
1152 if (bfd_bwrite (&hr, sizeof (hr), stream) != sizeof (hr))
1153 goto end;
1154 }
1155
1156 /* Write the bitmap for filled and unfilled buckets. */
1157
1158 for (unsigned int i = 0; i < num_buckets; i += 8)
1159 {
1160 uint8_t v = 0;
1161
1162 for (unsigned int j = 0; j < 8; j++)
1163 {
1164 if (buckets[i + j])
1165 v |= 1 << j;
1166 }
1167
1168 if (bfd_bwrite (&v, sizeof (v), stream) != sizeof (v))
1169 goto end;
1170 }
1171
1172 /* Add a 4-byte gap. */
1173
1174 bfd_putl32 (0, int_buf);
1175
1176 if (bfd_bwrite (int_buf, sizeof (uint32_t), stream) != sizeof (uint32_t))
1177 goto end;
1178
1179 /* Write the bucket offsets. */
1180
1181 for (unsigned int i = 0; i < num_buckets; i++)
1182 {
1183 if (buckets[i])
1184 {
1185 /* 0xc is size of internal hash_record structure in
1186 Microsoft's parser. */
1187 bfd_putl32 (buckets[i]->index * 0xc, int_buf);
1188
1189 if (bfd_bwrite (int_buf, sizeof (uint32_t), stream) !=
1190 sizeof (uint32_t))
1191 goto end;
1192 }
1193 }
1194
1195 /* Write the address map: offsets into the symbol record stream of
1196 S_PUB32 records, ordered by address. */
1197
1198 if (num_entries > 0)
1199 {
1200 qsort (sorted, num_entries, sizeof (struct public *),
1201 public_compare_addr);
1202
1203 for (unsigned int i = 0; i < num_entries; i++)
1204 {
1205 bfd_putl32 (sorted[i]->offset, int_buf);
1206
1207 if (bfd_bwrite (int_buf, sizeof (uint32_t), stream) !=
1208 sizeof (uint32_t))
1209 goto end;
1210 }
1211 }
1212
1213 ret = true;
1214
1215 end:
1216 free (buckets);
1217
1218 while (publics_head)
1219 {
1220 struct public *p = publics_head->next;
1221
1222 free (publics_head);
1223 publics_head = p;
1224 }
1225
1226 free (sorted);
1227
1228 return ret;
1229 }
1230
1231 /* The section header stream contains a copy of the section headers
1232 from the PE file, in the same format. */
1233 static bool
1234 create_section_header_stream (bfd *pdb, bfd *abfd, uint16_t *num)
1235 {
1236 bfd *stream;
1237 unsigned int section_count;
1238 file_ptr scn_base;
1239 size_t len;
1240 char *buf;
1241
1242 stream = add_stream (pdb, NULL, num);
1243 if (!stream)
1244 return false;
1245
1246 section_count = abfd->section_count;
1247
1248 /* Empty sections aren't output. */
1249 for (asection *sect = abfd->sections; sect; sect = sect->next)
1250 {
1251 if (sect->size == 0)
1252 section_count--;
1253 }
1254
1255 if (section_count == 0)
1256 return true;
1257
1258 /* Copy section table from output - it's already been written at this
1259 point. */
1260
1261 scn_base = bfd_coff_filhsz (abfd) + bfd_coff_aoutsz (abfd);
1262
1263 bfd_seek (abfd, scn_base, SEEK_SET);
1264
1265 len = section_count * sizeof (struct external_scnhdr);
1266 buf = xmalloc (len);
1267
1268 if (bfd_bread (buf, len, abfd) != len)
1269 {
1270 free (buf);
1271 return false;
1272 }
1273
1274 if (bfd_bwrite (buf, len, stream) != len)
1275 {
1276 free (buf);
1277 return false;
1278 }
1279
1280 free (buf);
1281
1282 return true;
1283 }
1284
1285 /* Populate the "/names" named stream, which contains the string table. */
1286 static bool
1287 populate_names_stream (bfd *stream, struct string_table *strings)
1288 {
1289 char int_buf[sizeof (uint32_t)];
1290 struct string_table_header h;
1291 uint32_t num_strings = 0, num_buckets;
1292 struct string **buckets;
1293
1294 bfd_putl32 (STRING_TABLE_SIGNATURE, &h.signature);
1295 bfd_putl32 (STRING_TABLE_VERSION, &h.version);
1296
1297 if (bfd_bwrite (&h, sizeof (h), stream) != sizeof (h))
1298 return false;
1299
1300 bfd_putl32 (strings->strings_len, int_buf);
1301
1302 if (bfd_bwrite (int_buf, sizeof (uint32_t), stream) != sizeof (uint32_t))
1303 return false;
1304
1305 int_buf[0] = 0;
1306
1307 if (bfd_bwrite (int_buf, 1, stream) != 1)
1308 return false;
1309
1310 for (struct string *s = strings->strings_head; s; s = s->next)
1311 {
1312 if (bfd_bwrite (s->s, s->len, stream) != s->len)
1313 return false;
1314
1315 if (bfd_bwrite (int_buf, 1, stream) != 1)
1316 return false;
1317
1318 num_strings++;
1319 }
1320
1321 num_buckets = num_strings * 2;
1322
1323 buckets = xmalloc (sizeof (struct string *) * num_buckets);
1324 memset (buckets, 0, sizeof (struct string *) * num_buckets);
1325
1326 for (struct string *s = strings->strings_head; s; s = s->next)
1327 {
1328 uint32_t bucket_num = s->hash % num_buckets;
1329
1330 while (buckets[bucket_num])
1331 {
1332 bucket_num++;
1333
1334 if (bucket_num == num_buckets)
1335 bucket_num = 0;
1336 }
1337
1338 buckets[bucket_num] = s;
1339 }
1340
1341 bfd_putl32 (num_buckets, int_buf);
1342
1343 if (bfd_bwrite (int_buf, sizeof (uint32_t), stream) != sizeof (uint32_t))
1344 {
1345 free (buckets);
1346 return false;
1347 }
1348
1349 for (unsigned int i = 0; i < num_buckets; i++)
1350 {
1351 if (buckets[i])
1352 bfd_putl32 (buckets[i]->offset, int_buf);
1353 else
1354 bfd_putl32 (0, int_buf);
1355
1356 if (bfd_bwrite (int_buf, sizeof (uint32_t), stream) !=
1357 sizeof (uint32_t))
1358 {
1359 free (buckets);
1360 return false;
1361 }
1362 }
1363
1364 free (buckets);
1365
1366 bfd_putl32 (num_strings, int_buf);
1367
1368 if (bfd_bwrite (int_buf, sizeof (uint32_t), stream) != sizeof (uint32_t))
1369 return false;
1370
1371 return true;
1372 }
1373
1374 /* Create a PDB debugging file for the PE image file abfd with the build ID
1375 guid, stored at pdb_name. */
1376 bool
1377 create_pdb_file (bfd *abfd, const char *pdb_name, const unsigned char *guid)
1378 {
1379 bfd *pdb;
1380 bool ret = false;
1381 bfd *info_stream, *dbi_stream, *names_stream, *sym_rec_stream,
1382 *publics_stream;
1383 uint16_t section_header_stream_num, sym_rec_stream_num, publics_stream_num;
1384 struct string_table strings;
1385
1386 pdb = bfd_openw (pdb_name, "pdb");
1387 if (!pdb)
1388 {
1389 einfo (_("%P: warning: cannot create PDB file: %E\n"));
1390 return false;
1391 }
1392
1393 strings.strings_head = NULL;
1394 strings.strings_tail = NULL;
1395 strings.strings_len = 1;
1396 strings.hashmap = htab_create_alloc (0, hash_string_table_entry,
1397 eq_string_table_entry, free,
1398 xcalloc, free);
1399
1400 bfd_set_format (pdb, bfd_archive);
1401
1402 if (!create_old_directory_stream (pdb))
1403 {
1404 einfo (_("%P: warning: cannot create old directory stream "
1405 "in PDB file: %E\n"));
1406 goto end;
1407 }
1408
1409 info_stream = add_stream (pdb, NULL, NULL);
1410
1411 if (!info_stream)
1412 {
1413 einfo (_("%P: warning: cannot create info stream "
1414 "in PDB file: %E\n"));
1415 goto end;
1416 }
1417
1418 if (!create_type_stream (pdb))
1419 {
1420 einfo (_("%P: warning: cannot create TPI stream "
1421 "in PDB file: %E\n"));
1422 goto end;
1423 }
1424
1425 dbi_stream = add_stream (pdb, NULL, NULL);
1426
1427 if (!dbi_stream)
1428 {
1429 einfo (_("%P: warning: cannot create DBI stream "
1430 "in PDB file: %E\n"));
1431 goto end;
1432 }
1433
1434 if (!create_type_stream (pdb))
1435 {
1436 einfo (_("%P: warning: cannot create IPI stream "
1437 "in PDB file: %E\n"));
1438 goto end;
1439 }
1440
1441 names_stream = add_stream (pdb, "/names", NULL);
1442
1443 if (!names_stream)
1444 {
1445 einfo (_("%P: warning: cannot create /names stream "
1446 "in PDB file: %E\n"));
1447 goto end;
1448 }
1449
1450 sym_rec_stream = add_stream (pdb, NULL, &sym_rec_stream_num);
1451
1452 if (!sym_rec_stream)
1453 {
1454 einfo (_("%P: warning: cannot create symbol record stream "
1455 "in PDB file: %E\n"));
1456 goto end;
1457 }
1458
1459 publics_stream = add_stream (pdb, NULL, &publics_stream_num);
1460
1461 if (!publics_stream)
1462 {
1463 einfo (_("%P: warning: cannot create publics stream "
1464 "in PDB file: %E\n"));
1465 goto end;
1466 }
1467
1468 if (!create_section_header_stream (pdb, abfd, &section_header_stream_num))
1469 {
1470 einfo (_("%P: warning: cannot create section header stream "
1471 "in PDB file: %E\n"));
1472 goto end;
1473 }
1474
1475 if (!populate_dbi_stream (dbi_stream, abfd, pdb, section_header_stream_num,
1476 sym_rec_stream_num, publics_stream_num,
1477 &strings))
1478 {
1479 einfo (_("%P: warning: cannot populate DBI stream "
1480 "in PDB file: %E\n"));
1481 goto end;
1482 }
1483
1484 add_string ("", 0, &strings);
1485
1486 if (!populate_names_stream (names_stream, &strings))
1487 {
1488 einfo (_("%P: warning: cannot populate names stream "
1489 "in PDB file: %E\n"));
1490 goto end;
1491 }
1492
1493 if (!populate_publics_stream (publics_stream, abfd, sym_rec_stream))
1494 {
1495 einfo (_("%P: warning: cannot populate publics stream "
1496 "in PDB file: %E\n"));
1497 goto end;
1498 }
1499
1500 if (!populate_info_stream (pdb, info_stream, guid))
1501 {
1502 einfo (_("%P: warning: cannot populate info stream "
1503 "in PDB file: %E\n"));
1504 goto end;
1505 }
1506
1507 ret = true;
1508
1509 end:
1510 bfd_close (pdb);
1511
1512 htab_delete (strings.hashmap);
1513
1514 return ret;
1515 }