ld: Fix segfault in populate_publics_stream
[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 uint32_t source_file_offset;
50 size_t len;
51 char s[];
52 };
53
54 struct string_table
55 {
56 struct string *strings_head;
57 struct string *strings_tail;
58 uint32_t strings_len;
59 htab_t hashmap;
60 };
61
62 struct mod_source_files
63 {
64 uint16_t files_count;
65 struct string **files;
66 };
67
68 struct source_files_info
69 {
70 uint16_t mod_count;
71 struct mod_source_files *mods;
72 };
73
74 /* Add a new stream to the PDB archive, and return its BFD. */
75 static bfd *
76 add_stream (bfd *pdb, const char *name, uint16_t *stream_num)
77 {
78 bfd *stream;
79 uint16_t num;
80
81 stream = bfd_create (name ? name : "", pdb);
82 if (!stream)
83 return NULL;
84
85 if (!bfd_make_writable (stream))
86 {
87 bfd_close (stream);
88 return false;
89 }
90
91 if (!pdb->archive_head)
92 {
93 bfd_set_archive_head (pdb, stream);
94 num = 0;
95 }
96 else
97 {
98 bfd *b = pdb->archive_head;
99
100 num = 1;
101
102 while (b->archive_next)
103 {
104 num++;
105 b = b->archive_next;
106 }
107
108 b->archive_next = stream;
109 }
110
111 if (stream_num)
112 *stream_num = num;
113
114 return stream;
115 }
116
117 /* Stream 0 ought to be a copy of the MSF directory from the last
118 time the PDB file was written. Because we don't do incremental
119 writes this isn't applicable to us, but we fill it with a dummy
120 value so as not to confuse radare. */
121 static bool
122 create_old_directory_stream (bfd *pdb)
123 {
124 bfd *stream;
125 char buf[sizeof (uint32_t)];
126
127 stream = add_stream (pdb, NULL, NULL);
128 if (!stream)
129 return false;
130
131 bfd_putl32 (0, buf);
132
133 return bfd_bwrite (buf, sizeof (uint32_t), stream) == sizeof (uint32_t);
134 }
135
136 /* Calculate the hash of a given string. */
137 static uint32_t
138 calc_hash (const char *data, size_t len)
139 {
140 uint32_t hash = 0;
141
142 while (len >= 4)
143 {
144 hash ^= data[0];
145 hash ^= data[1] << 8;
146 hash ^= data[2] << 16;
147 hash ^= data[3] << 24;
148
149 data += 4;
150 len -= 4;
151 }
152
153 if (len >= 2)
154 {
155 hash ^= data[0];
156 hash ^= data[1] << 8;
157
158 data += 2;
159 len -= 2;
160 }
161
162 if (len != 0)
163 hash ^= *data;
164
165 hash |= 0x20202020;
166 hash ^= (hash >> 11);
167
168 return hash ^ (hash >> 16);
169 }
170
171 /* Stream 1 is the PDB info stream - see
172 https://llvm.org/docs/PDB/PdbStream.html. */
173 static bool
174 populate_info_stream (bfd *pdb, bfd *info_stream, const unsigned char *guid)
175 {
176 bool ret = false;
177 struct pdb_stream_70 h;
178 uint32_t num_entries, num_buckets;
179 uint32_t names_length, stream_num;
180 char int_buf[sizeof (uint32_t)];
181
182 struct hash_entry
183 {
184 uint32_t offset;
185 uint32_t value;
186 };
187
188 struct hash_entry **buckets = NULL;
189
190 /* Write header. */
191
192 bfd_putl32 (PDB_STREAM_VERSION_VC70, &h.version);
193 bfd_putl32 (time (NULL), &h.signature);
194 bfd_putl32 (1, &h.age);
195
196 bfd_putl32 (bfd_getb32 (guid), h.guid);
197 bfd_putl16 (bfd_getb16 (&guid[4]), &h.guid[4]);
198 bfd_putl16 (bfd_getb16 (&guid[6]), &h.guid[6]);
199 memcpy (&h.guid[8], &guid[8], 8);
200
201 if (bfd_bwrite (&h, sizeof (h), info_stream) != sizeof (h))
202 return false;
203
204 /* Write hash list of named streams. This is a "rollover" hash, i.e.
205 if a bucket is filled an entry gets placed in the next free
206 slot. */
207
208 num_entries = 0;
209 for (bfd *b = pdb->archive_head; b; b = b->archive_next)
210 {
211 if (strcmp (b->filename, ""))
212 num_entries++;
213 }
214
215 num_buckets = num_entries * 2;
216
217 names_length = 0;
218 stream_num = 0;
219
220 if (num_buckets > 0)
221 {
222 buckets = xmalloc (sizeof (struct hash_entry *) * num_buckets);
223 memset (buckets, 0, sizeof (struct hash_entry *) * num_buckets);
224
225 for (bfd *b = pdb->archive_head; b; b = b->archive_next)
226 {
227 if (strcmp (b->filename, ""))
228 {
229 size_t len = strlen (b->filename);
230 uint32_t hash = (uint16_t) calc_hash (b->filename, len);
231 uint32_t bucket_num = hash % num_buckets;
232
233 while (buckets[bucket_num])
234 {
235 bucket_num++;
236
237 if (bucket_num == num_buckets)
238 bucket_num = 0;
239 }
240
241 buckets[bucket_num] = xmalloc (sizeof (struct hash_entry));
242
243 buckets[bucket_num]->offset = names_length;
244 buckets[bucket_num]->value = stream_num;
245
246 names_length += len + 1;
247 }
248
249 stream_num++;
250 }
251 }
252
253 /* Write the strings list - the hash keys are indexes into this. */
254
255 bfd_putl32 (names_length, int_buf);
256
257 if (bfd_bwrite (int_buf, sizeof (uint32_t), info_stream) !=
258 sizeof (uint32_t))
259 goto end;
260
261 for (bfd *b = pdb->archive_head; b; b = b->archive_next)
262 {
263 if (!strcmp (b->filename, ""))
264 continue;
265
266 size_t len = strlen (b->filename) + 1;
267
268 if (bfd_bwrite (b->filename, len, info_stream) != len)
269 goto end;
270 }
271
272 /* Write the number of entries and buckets. */
273
274 bfd_putl32 (num_entries, int_buf);
275
276 if (bfd_bwrite (int_buf, sizeof (uint32_t), info_stream) !=
277 sizeof (uint32_t))
278 goto end;
279
280 bfd_putl32 (num_buckets, int_buf);
281
282 if (bfd_bwrite (int_buf, sizeof (uint32_t), info_stream) !=
283 sizeof (uint32_t))
284 goto end;
285
286 /* Write the present bitmap. */
287
288 bfd_putl32 ((num_buckets + 31) / 32, int_buf);
289
290 if (bfd_bwrite (int_buf, sizeof (uint32_t), info_stream) !=
291 sizeof (uint32_t))
292 goto end;
293
294 for (unsigned int i = 0; i < num_buckets; i += 32)
295 {
296 uint32_t v = 0;
297
298 for (unsigned int j = 0; j < 32; j++)
299 {
300 if (i + j >= num_buckets)
301 break;
302
303 if (buckets[i + j])
304 v |= 1 << j;
305 }
306
307 bfd_putl32 (v, int_buf);
308
309 if (bfd_bwrite (int_buf, sizeof (uint32_t), info_stream) !=
310 sizeof (uint32_t))
311 goto end;
312 }
313
314 /* Write the (empty) deleted bitmap. */
315
316 bfd_putl32 (0, int_buf);
317
318 if (bfd_bwrite (int_buf, sizeof (uint32_t), info_stream) !=
319 sizeof (uint32_t))
320 goto end;
321
322 /* Write the buckets. */
323
324 for (unsigned int i = 0; i < num_buckets; i++)
325 {
326 if (buckets[i])
327 {
328 bfd_putl32 (buckets[i]->offset, int_buf);
329
330 if (bfd_bwrite (int_buf, sizeof (uint32_t), info_stream) !=
331 sizeof (uint32_t))
332 goto end;
333
334 bfd_putl32 (buckets[i]->value, int_buf);
335
336 if (bfd_bwrite (int_buf, sizeof (uint32_t), info_stream) !=
337 sizeof (uint32_t))
338 goto end;
339 }
340 }
341
342 bfd_putl32 (0, int_buf);
343
344 if (bfd_bwrite (int_buf, sizeof (uint32_t), info_stream) !=
345 sizeof (uint32_t))
346 goto end;
347
348 bfd_putl32 (PDB_STREAM_VERSION_VC140, int_buf);
349
350 if (bfd_bwrite (int_buf, sizeof (uint32_t), info_stream) !=
351 sizeof (uint32_t))
352 goto end;
353
354 ret = true;
355
356 end:
357 for (unsigned int i = 0; i < num_buckets; i++)
358 {
359 if (buckets[i])
360 free (buckets[i]);
361 }
362
363 free (buckets);
364
365 return ret;
366 }
367
368 /* Stream 2 is the type information (TPI) stream, and stream 4 is
369 the ID information (IPI) stream. They differ only in which records
370 go in which stream. */
371 static bool
372 create_type_stream (bfd *pdb)
373 {
374 bfd *stream;
375 struct pdb_tpi_stream_header h;
376
377 stream = add_stream (pdb, NULL, NULL);
378 if (!stream)
379 return false;
380
381 bfd_putl32 (TPI_STREAM_VERSION_80, &h.version);
382 bfd_putl32 (sizeof (h), &h.header_size);
383 bfd_putl32 (TPI_FIRST_INDEX, &h.type_index_begin);
384 bfd_putl32 (TPI_FIRST_INDEX, &h.type_index_end);
385 bfd_putl32 (0, &h.type_record_bytes);
386 bfd_putl16 (0xffff, &h.hash_stream_index);
387 bfd_putl16 (0xffff, &h.hash_aux_stream_index);
388 bfd_putl32 (4, &h.hash_key_size);
389 bfd_putl32 (0x3ffff, &h.num_hash_buckets);
390 bfd_putl32 (0, &h.hash_value_buffer_offset);
391 bfd_putl32 (0, &h.hash_value_buffer_length);
392 bfd_putl32 (0, &h.index_offset_buffer_offset);
393 bfd_putl32 (0, &h.index_offset_buffer_length);
394 bfd_putl32 (0, &h.hash_adj_buffer_offset);
395 bfd_putl32 (0, &h.hash_adj_buffer_length);
396
397 if (bfd_bwrite (&h, sizeof (h), stream) != sizeof (h))
398 return false;
399
400 return true;
401 }
402
403 /* Return the PE architecture number for the image. */
404 static uint16_t
405 get_arch_number (bfd *abfd)
406 {
407 if (abfd->arch_info->arch != bfd_arch_i386)
408 return 0;
409
410 if (abfd->arch_info->mach & bfd_mach_x86_64)
411 return IMAGE_FILE_MACHINE_AMD64;
412
413 return IMAGE_FILE_MACHINE_I386;
414 }
415
416 /* Validate the DEBUG_S_FILECHKSMS entry within a module's .debug$S
417 section, and copy it to the module's symbol stream. */
418 static bool
419 copy_filechksms (uint8_t *data, uint32_t size, char *string_table,
420 struct string_table *strings, uint8_t *out,
421 struct mod_source_files *mod_source)
422 {
423 uint8_t *orig_data = data;
424 uint32_t orig_size = size;
425 uint16_t num_files = 0;
426 struct string **strptr;
427
428 bfd_putl32 (DEBUG_S_FILECHKSMS, out);
429 out += sizeof (uint32_t);
430
431 bfd_putl32 (size, out);
432 out += sizeof (uint32_t);
433
434 /* Calculate the number of files, and check for any overflows. */
435
436 while (size > 0)
437 {
438 struct file_checksum *fc = (struct file_checksum *) data;
439 uint8_t padding;
440 size_t len;
441
442 if (size < sizeof (struct file_checksum))
443 {
444 bfd_set_error (bfd_error_bad_value);
445 return false;
446 }
447
448 len = sizeof (struct file_checksum) + fc->checksum_length;
449
450 if (size < len)
451 {
452 bfd_set_error (bfd_error_bad_value);
453 return false;
454 }
455
456 data += len;
457 size -= len;
458
459 if (len % sizeof (uint32_t))
460 padding = sizeof (uint32_t) - (len % sizeof (uint32_t));
461 else
462 padding = 0;
463
464 if (size < padding)
465 {
466 bfd_set_error (bfd_error_bad_value);
467 return false;
468 }
469
470 num_files++;
471
472 data += padding;
473 size -= padding;
474 }
475
476 /* Add the files to mod_source, so that they'll appear in the source
477 info substream. */
478
479 strptr = NULL;
480 if (num_files > 0)
481 {
482 uint16_t new_count = num_files + mod_source->files_count;
483
484 mod_source->files = xrealloc (mod_source->files,
485 sizeof (struct string *) * new_count);
486
487 strptr = mod_source->files + mod_source->files_count;
488
489 mod_source->files_count += num_files;
490 }
491
492 /* Actually copy the data. */
493
494 data = orig_data;
495 size = orig_size;
496
497 while (size > 0)
498 {
499 struct file_checksum *fc = (struct file_checksum *) data;
500 uint32_t string_off;
501 uint8_t padding;
502 size_t len;
503 struct string *str = NULL;
504
505 string_off = bfd_getl32 (&fc->file_id);
506 len = sizeof (struct file_checksum) + fc->checksum_length;
507
508 if (len % sizeof (uint32_t))
509 padding = sizeof (uint32_t) - (len % sizeof (uint32_t));
510 else
511 padding = 0;
512
513 /* Remap the "file ID", i.e. the offset in the module's string table,
514 so it points to the right place in the main string table. */
515
516 if (string_table)
517 {
518 char *fn = string_table + string_off;
519 size_t fn_len = strlen (fn);
520 uint32_t hash = calc_hash (fn, fn_len);
521 void **slot;
522
523 slot = htab_find_slot_with_hash (strings->hashmap, fn, hash,
524 NO_INSERT);
525
526 if (slot)
527 str = (struct string *) *slot;
528 }
529
530 *strptr = str;
531 strptr++;
532
533 bfd_putl32 (str ? str->offset : 0, &fc->file_id);
534
535 memcpy (out, data, len + padding);
536
537 data += len + padding;
538 size -= len + padding;
539 out += len + padding;
540 }
541
542 return true;
543 }
544
545 /* Add a string to the strings table, if it's not already there. */
546 static void
547 add_string (char *str, size_t len, struct string_table *strings)
548 {
549 uint32_t hash = calc_hash (str, len);
550 void **slot;
551
552 slot = htab_find_slot_with_hash (strings->hashmap, str, hash, INSERT);
553
554 if (!*slot)
555 {
556 struct string *s;
557
558 *slot = xmalloc (offsetof (struct string, s) + len);
559
560 s = (struct string *) *slot;
561
562 s->next = NULL;
563 s->hash = hash;
564 s->offset = strings->strings_len;
565 s->source_file_offset = 0xffffffff;
566 s->len = len;
567 memcpy (s->s, str, len);
568
569 if (strings->strings_tail)
570 strings->strings_tail->next = s;
571 else
572 strings->strings_head = s;
573
574 strings->strings_tail = s;
575
576 strings->strings_len += len + 1;
577 }
578 }
579
580 /* Return the hash of an entry in the string table. */
581 static hashval_t
582 hash_string_table_entry (const void *p)
583 {
584 const struct string *s = (const struct string *) p;
585
586 return s->hash;
587 }
588
589 /* Compare an entry in the string table with a string. */
590 static int
591 eq_string_table_entry (const void *a, const void *b)
592 {
593 const struct string *s1 = (const struct string *) a;
594 const char *s2 = (const char *) b;
595 size_t s2_len = strlen (s2);
596
597 if (s2_len != s1->len)
598 return 0;
599
600 return memcmp (s1->s, s2, s2_len) == 0;
601 }
602
603 /* Parse the string table within the .debug$S section. */
604 static void
605 parse_string_table (bfd_byte *data, size_t size,
606 struct string_table *strings)
607 {
608 while (true)
609 {
610 size_t len = strnlen ((char *) data, size);
611
612 add_string ((char *) data, len, strings);
613
614 data += len + 1;
615
616 if (size <= len + 1)
617 break;
618
619 size -= len + 1;
620 }
621 }
622
623 /* Parse the .debug$S section within an object file. */
624 static bool
625 handle_debugs_section (asection *s, bfd *mod, struct string_table *strings,
626 uint8_t **dataptr, uint32_t *sizeptr,
627 struct mod_source_files *mod_source)
628 {
629 bfd_byte *data = NULL;
630 size_t off;
631 uint32_t c13_size = 0;
632 char *string_table = NULL;
633 uint8_t *buf, *bufptr;
634
635 if (!bfd_get_full_section_contents (mod, s, &data))
636 return false;
637
638 if (!data)
639 return false;
640
641 if (bfd_getl32 (data) != CV_SIGNATURE_C13)
642 {
643 free (data);
644 return true;
645 }
646
647 off = sizeof (uint32_t);
648
649 /* calculate size */
650
651 while (off + sizeof (uint32_t) <= s->size)
652 {
653 uint32_t type, size;
654
655 type = bfd_getl32 (data + off);
656
657 off += sizeof (uint32_t);
658
659 if (off + sizeof (uint32_t) > s->size)
660 {
661 free (data);
662 bfd_set_error (bfd_error_bad_value);
663 return false;
664 }
665
666 size = bfd_getl32 (data + off);
667
668 off += sizeof (uint32_t);
669
670 if (off + size > s->size)
671 {
672 free (data);
673 bfd_set_error (bfd_error_bad_value);
674 return false;
675 }
676
677 switch (type)
678 {
679 case DEBUG_S_FILECHKSMS:
680 c13_size += sizeof (uint32_t) + sizeof (uint32_t) + size;
681
682 if (c13_size % sizeof (uint32_t))
683 c13_size += sizeof (uint32_t) - (c13_size % sizeof (uint32_t));
684
685 break;
686
687 case DEBUG_S_STRINGTABLE:
688 parse_string_table (data + off, size, strings);
689
690 string_table = (char *) data + off;
691
692 break;
693 }
694
695 off += size;
696
697 if (off % sizeof (uint32_t))
698 off += sizeof (uint32_t) - (off % sizeof (uint32_t));
699 }
700
701 if (c13_size == 0)
702 {
703 free (data);
704 return true;
705 }
706
707 /* copy data */
708
709 buf = xmalloc (c13_size);
710 bufptr = buf;
711
712 off = sizeof (uint32_t);
713
714 while (off + sizeof (uint32_t) <= s->size)
715 {
716 uint32_t type, size;
717
718 type = bfd_getl32 (data + off);
719 off += sizeof (uint32_t);
720
721 size = bfd_getl32 (data + off);
722 off += sizeof (uint32_t);
723
724 switch (type)
725 {
726 case DEBUG_S_FILECHKSMS:
727 if (!copy_filechksms (data + off, size, string_table,
728 strings, bufptr, mod_source))
729 {
730 free (data);
731 return false;
732 }
733
734 bufptr += sizeof (uint32_t) + sizeof (uint32_t) + size;
735
736 break;
737 }
738
739 off += size;
740
741 if (off % sizeof (uint32_t))
742 off += sizeof (uint32_t) - (off % sizeof (uint32_t));
743 }
744
745 free (data);
746
747 if (*dataptr)
748 {
749 /* Append the C13 info to what's already there, if the module has
750 multiple .debug$S sections. */
751
752 *dataptr = xrealloc (*dataptr, *sizeptr + c13_size);
753 memcpy (*dataptr + *sizeptr, buf, c13_size);
754
755 free (buf);
756 }
757 else
758 {
759 *dataptr = buf;
760 }
761
762 *sizeptr += c13_size;
763
764 return true;
765 }
766
767 /* Populate the module stream, which consists of the transformed .debug$S
768 data for each object file. */
769 static bool
770 populate_module_stream (bfd *stream, bfd *mod, uint32_t *sym_byte_size,
771 struct string_table *strings,
772 uint32_t *c13_info_size,
773 struct mod_source_files *mod_source)
774 {
775 uint8_t int_buf[sizeof (uint32_t)];
776 uint8_t *c13_info = NULL;
777
778 *sym_byte_size = sizeof (uint32_t);
779 *c13_info_size = 0;
780
781 /* Process .debug$S section(s). */
782
783 for (asection *s = mod->sections; s; s = s->next)
784 {
785 if (!strcmp (s->name, ".debug$S") && s->size >= sizeof (uint32_t))
786 {
787 if (!handle_debugs_section (s, mod, strings, &c13_info,
788 c13_info_size, mod_source))
789 {
790 free (c13_info);
791 free (mod_source->files);
792 return false;
793 }
794 }
795 }
796
797 /* Write the signature. */
798
799 bfd_putl32 (CV_SIGNATURE_C13, int_buf);
800
801 if (bfd_bwrite (int_buf, sizeof (uint32_t), stream) != sizeof (uint32_t))
802 {
803 free (c13_info);
804 return false;
805 }
806
807 if (c13_info)
808 {
809 if (bfd_bwrite (c13_info, *c13_info_size, stream) != *c13_info_size)
810 {
811 free (c13_info);
812 return false;
813 }
814
815 free (c13_info);
816 }
817
818 /* Write the global refs size. */
819
820 bfd_putl32 (0, int_buf);
821
822 if (bfd_bwrite (int_buf, sizeof (uint32_t), stream) != sizeof (uint32_t))
823 return false;
824
825 return true;
826 }
827
828 /* Create the module info substream within the DBI. */
829 static bool
830 create_module_info_substream (bfd *abfd, bfd *pdb, void **data,
831 uint32_t *size, struct string_table *strings,
832 struct source_files_info *source)
833 {
834 uint8_t *ptr;
835 unsigned int mod_num;
836
837 static const char linker_fn[] = "* Linker *";
838
839 *size = 0;
840
841 for (bfd *in = coff_data (abfd)->link_info->input_bfds; in;
842 in = in->link.next)
843 {
844 size_t len = sizeof (struct module_info);
845
846 if (!strcmp (bfd_get_filename (in), "dll stuff"))
847 {
848 len += sizeof (linker_fn); /* Object name. */
849 len++; /* Empty module name. */
850 }
851 else if (in->my_archive)
852 {
853 char *name = lrealpath (bfd_get_filename (in));
854
855 len += strlen (name) + 1; /* Object name. */
856
857 free (name);
858
859 name = lrealpath (bfd_get_filename (in->my_archive));
860
861 len += strlen (name) + 1; /* Archive name. */
862
863 free (name);
864 }
865 else
866 {
867 char *name = lrealpath (bfd_get_filename (in));
868 size_t name_len = strlen (name) + 1;
869
870 len += name_len; /* Object name. */
871 len += name_len; /* And again as the archive name. */
872
873 free (name);
874 }
875
876 if (len % 4)
877 len += 4 - (len % 4);
878
879 *size += len;
880
881 source->mod_count++;
882 }
883
884 *data = xmalloc (*size);
885
886 ptr = *data;
887
888 source->mods = xmalloc (source->mod_count
889 * sizeof (struct mod_source_files));
890 memset (source->mods, 0,
891 source->mod_count * sizeof (struct mod_source_files));
892
893 mod_num = 0;
894
895 for (bfd *in = coff_data (abfd)->link_info->input_bfds; in;
896 in = in->link.next)
897 {
898 struct module_info *mod = (struct module_info *) ptr;
899 uint16_t stream_num;
900 bfd *stream;
901 uint32_t sym_byte_size, c13_info_size;
902 uint8_t *start = ptr;
903
904 stream = add_stream (pdb, NULL, &stream_num);
905
906 if (!stream)
907 {
908 for (unsigned int i = 0; i < source->mod_count; i++)
909 {
910 free (source->mods[i].files);
911 }
912
913 free (source->mods);
914 free (*data);
915 return false;
916 }
917
918 if (!populate_module_stream (stream, in, &sym_byte_size,
919 strings, &c13_info_size,
920 &source->mods[mod_num]))
921 {
922 for (unsigned int i = 0; i < source->mod_count; i++)
923 {
924 free (source->mods[i].files);
925 }
926
927 free (source->mods);
928 free (*data);
929 return false;
930 }
931
932 bfd_putl32 (0, &mod->unused1);
933
934 /* These are dummy values - MSVC copies the first section contribution
935 entry here, but doesn't seem to use it for anything. */
936 bfd_putl16 (0xffff, &mod->sc.section);
937 bfd_putl16 (0, &mod->sc.padding1);
938 bfd_putl32 (0, &mod->sc.offset);
939 bfd_putl32 (0xffffffff, &mod->sc.size);
940 bfd_putl32 (0, &mod->sc.characteristics);
941 bfd_putl16 (0xffff, &mod->sc.module_index);
942 bfd_putl16 (0, &mod->sc.padding2);
943 bfd_putl32 (0, &mod->sc.data_crc);
944 bfd_putl32 (0, &mod->sc.reloc_crc);
945
946 bfd_putl16 (0, &mod->flags);
947 bfd_putl16 (stream_num, &mod->module_sym_stream);
948 bfd_putl32 (sym_byte_size, &mod->sym_byte_size);
949 bfd_putl32 (0, &mod->c11_byte_size);
950 bfd_putl32 (c13_info_size, &mod->c13_byte_size);
951 bfd_putl16 (0, &mod->source_file_count);
952 bfd_putl16 (0, &mod->padding);
953 bfd_putl32 (0, &mod->unused2);
954 bfd_putl32 (0, &mod->source_file_name_index);
955 bfd_putl32 (0, &mod->pdb_file_path_name_index);
956
957 ptr += sizeof (struct module_info);
958
959 if (!strcmp (bfd_get_filename (in), "dll stuff"))
960 {
961 /* Object name. */
962 memcpy (ptr, linker_fn, sizeof (linker_fn));
963 ptr += sizeof (linker_fn);
964
965 /* Empty module name. */
966 *ptr = 0;
967 ptr++;
968 }
969 else if (in->my_archive)
970 {
971 char *name = lrealpath (bfd_get_filename (in));
972 size_t name_len = strlen (name) + 1;
973
974 /* Object name. */
975 memcpy (ptr, name, name_len);
976 ptr += name_len;
977
978 free (name);
979
980 name = lrealpath (bfd_get_filename (in->my_archive));
981 name_len = strlen (name) + 1;
982
983 /* Archive name. */
984 memcpy (ptr, name, name_len);
985 ptr += name_len;
986
987 free (name);
988 }
989 else
990 {
991 char *name = lrealpath (bfd_get_filename (in));
992 size_t name_len = strlen (name) + 1;
993
994 /* Object name. */
995 memcpy (ptr, name, name_len);
996 ptr += name_len;
997
998 /* Object name again as archive name. */
999 memcpy (ptr, name, name_len);
1000 ptr += name_len;
1001
1002 free (name);
1003 }
1004
1005 /* Pad to next four-byte boundary. */
1006
1007 if ((ptr - start) % 4)
1008 {
1009 memset (ptr, 0, 4 - ((ptr - start) % 4));
1010 ptr += 4 - ((ptr - start) % 4);
1011 }
1012
1013 mod_num++;
1014 }
1015
1016 return true;
1017 }
1018
1019 /* Return the index of a given output section. */
1020 static uint16_t
1021 find_section_number (bfd *abfd, asection *sect)
1022 {
1023 uint16_t i = 1;
1024
1025 for (asection *s = abfd->sections; s; s = s->next)
1026 {
1027 if (s == sect)
1028 return i;
1029
1030 /* Empty sections aren't output. */
1031 if (s->size != 0)
1032 i++;
1033 }
1034
1035 return 0;
1036 }
1037
1038 /* Create the substream which maps addresses in the image file to locations
1039 in the original object files. */
1040 static bool
1041 create_section_contrib_substream (bfd *abfd, void **data, uint32_t *size)
1042 {
1043 unsigned int num_sc = 0;
1044 struct section_contribution *sc;
1045 uint16_t mod_index;
1046 char *sect_flags;
1047 file_ptr offset;
1048
1049 for (bfd *in = coff_data (abfd)->link_info->input_bfds; in;
1050 in = in->link.next)
1051 {
1052 for (asection *s = in->sections; s; s = s->next)
1053 {
1054 if (s->size == 0 || discarded_section (s))
1055 continue;
1056
1057 num_sc++;
1058 }
1059 }
1060
1061 *size = sizeof (uint32_t) + (num_sc * sizeof (struct section_contribution));
1062 *data = xmalloc (*size);
1063
1064 bfd_putl32 (SECTION_CONTRIB_VERSION_60, *data);
1065
1066 /* Read characteristics of outputted sections. */
1067
1068 sect_flags = xmalloc (sizeof (uint32_t) * abfd->section_count);
1069
1070 offset = bfd_coff_filhsz (abfd) + bfd_coff_aoutsz (abfd);
1071 offset += offsetof (struct external_scnhdr, s_flags);
1072
1073 for (unsigned int i = 0; i < abfd->section_count; i++)
1074 {
1075 bfd_seek (abfd, offset, SEEK_SET);
1076
1077 if (bfd_bread (sect_flags + (i * sizeof (uint32_t)), sizeof (uint32_t),
1078 abfd) != sizeof (uint32_t))
1079 {
1080 free (*data);
1081 free (sect_flags);
1082 return false;
1083 }
1084
1085 offset += sizeof (struct external_scnhdr);
1086 }
1087
1088 sc =
1089 (struct section_contribution *) ((uint8_t *) *data + sizeof (uint32_t));
1090
1091 mod_index = 0;
1092 for (bfd *in = coff_data (abfd)->link_info->input_bfds; in;
1093 in = in->link.next)
1094 {
1095 for (asection *s = in->sections; s; s = s->next)
1096 {
1097 uint16_t sect_num;
1098
1099 if (s->size == 0 || discarded_section (s))
1100 continue;
1101
1102 sect_num = find_section_number (abfd, s->output_section);
1103
1104 memcpy (&sc->characteristics,
1105 sect_flags + ((sect_num - 1) * sizeof (uint32_t)),
1106 sizeof (uint32_t));
1107
1108 bfd_putl16 (sect_num, &sc->section);
1109 bfd_putl16 (0, &sc->padding1);
1110 bfd_putl32 (s->output_offset, &sc->offset);
1111 bfd_putl32 (s->size, &sc->size);
1112 bfd_putl16 (mod_index, &sc->module_index);
1113 bfd_putl16 (0, &sc->padding2);
1114 bfd_putl32 (0, &sc->data_crc);
1115 bfd_putl32 (0, &sc->reloc_crc);
1116
1117 sc++;
1118 }
1119
1120 mod_index++;
1121 }
1122
1123 free (sect_flags);
1124
1125 return true;
1126 }
1127
1128 /* The source info substream lives within the DBI stream, and lists the
1129 source files for each object file (i.e. it's derived from the
1130 DEBUG_S_FILECHKSMS parts of the .debug$S sections). This is a bit
1131 superfluous, as the filenames are also available in the C13 parts of
1132 the module streams, but MSVC relies on it to work properly. */
1133 static void
1134 create_source_info_substream (void **data, uint32_t *size,
1135 struct source_files_info *source)
1136 {
1137 uint16_t dedupe_source_files_count = 0;
1138 uint16_t source_files_count = 0;
1139 uint32_t strings_len = 0;
1140 uint8_t *ptr;
1141
1142 /* Loop through the source files, marking unique filenames. The pointers
1143 here are for entries in the main string table, and so have already
1144 been deduplicated. */
1145
1146 for (uint16_t i = 0; i < source->mod_count; i++)
1147 {
1148 for (uint16_t j = 0; j < source->mods[i].files_count; j++)
1149 {
1150 if (source->mods[i].files[j])
1151 {
1152 if (source->mods[i].files[j]->source_file_offset == 0xffffffff)
1153 {
1154 source->mods[i].files[j]->source_file_offset = strings_len;
1155 strings_len += source->mods[i].files[j]->len + 1;
1156 dedupe_source_files_count++;
1157 }
1158
1159 source_files_count++;
1160 }
1161 }
1162 }
1163
1164 *size = sizeof (uint16_t) + sizeof (uint16_t);
1165 *size += (sizeof (uint16_t) + sizeof (uint16_t)) * source->mod_count;
1166 *size += sizeof (uint32_t) * source_files_count;
1167 *size += strings_len;
1168
1169 *data = xmalloc (*size);
1170
1171 ptr = (uint8_t *) *data;
1172
1173 /* Write header (module count and source file count). */
1174
1175 bfd_putl16 (source->mod_count, ptr);
1176 ptr += sizeof (uint16_t);
1177
1178 bfd_putl16 (dedupe_source_files_count, ptr);
1179 ptr += sizeof (uint16_t);
1180
1181 /* Write "ModIndices". As the LLVM documentation puts it, "this array is
1182 present, but does not appear to be useful". */
1183
1184 for (uint16_t i = 0; i < source->mod_count; i++)
1185 {
1186 bfd_putl16 (i, ptr);
1187 ptr += sizeof (uint16_t);
1188 }
1189
1190 /* Write source file count for each module. */
1191
1192 for (uint16_t i = 0; i < source->mod_count; i++)
1193 {
1194 bfd_putl16 (source->mods[i].files_count, ptr);
1195 ptr += sizeof (uint16_t);
1196 }
1197
1198 /* For each module, write the offsets within the string table
1199 for each source file. */
1200
1201 for (uint16_t i = 0; i < source->mod_count; i++)
1202 {
1203 for (uint16_t j = 0; j < source->mods[i].files_count; j++)
1204 {
1205 if (source->mods[i].files[j])
1206 {
1207 bfd_putl32 (source->mods[i].files[j]->source_file_offset, ptr);
1208 ptr += sizeof (uint32_t);
1209 }
1210 }
1211 }
1212
1213 /* Write the string table. We set source_file_offset to a dummy value for
1214 each entry we write, so we don't write duplicate filenames. */
1215
1216 for (uint16_t i = 0; i < source->mod_count; i++)
1217 {
1218 for (uint16_t j = 0; j < source->mods[i].files_count; j++)
1219 {
1220 if (source->mods[i].files[j]
1221 && source->mods[i].files[j]->source_file_offset != 0xffffffff)
1222 {
1223 memcpy (ptr, source->mods[i].files[j]->s,
1224 source->mods[i].files[j]->len);
1225 ptr += source->mods[i].files[j]->len;
1226
1227 *ptr = 0;
1228 ptr++;
1229
1230 source->mods[i].files[j]->source_file_offset = 0xffffffff;
1231 }
1232 }
1233 }
1234 }
1235
1236 /* Stream 4 is the debug information (DBI) stream. */
1237 static bool
1238 populate_dbi_stream (bfd *stream, bfd *abfd, bfd *pdb,
1239 uint16_t section_header_stream_num,
1240 uint16_t sym_rec_stream_num,
1241 uint16_t publics_stream_num,
1242 struct string_table *strings)
1243 {
1244 struct pdb_dbi_stream_header h;
1245 struct optional_dbg_header opt;
1246 void *mod_info, *sc, *source_info;
1247 uint32_t mod_info_size, sc_size, source_info_size;
1248 struct source_files_info source;
1249
1250 source.mod_count = 0;
1251 source.mods = NULL;
1252
1253 if (!create_module_info_substream (abfd, pdb, &mod_info, &mod_info_size,
1254 strings, &source))
1255 return false;
1256
1257 if (!create_section_contrib_substream (abfd, &sc, &sc_size))
1258 {
1259 for (unsigned int i = 0; i < source.mod_count; i++)
1260 {
1261 free (source.mods[i].files);
1262 }
1263 free (source.mods);
1264
1265 free (mod_info);
1266 return false;
1267 }
1268
1269 create_source_info_substream (&source_info, &source_info_size, &source);
1270
1271 for (unsigned int i = 0; i < source.mod_count; i++)
1272 {
1273 free (source.mods[i].files);
1274 }
1275 free (source.mods);
1276
1277 bfd_putl32 (0xffffffff, &h.version_signature);
1278 bfd_putl32 (DBI_STREAM_VERSION_70, &h.version_header);
1279 bfd_putl32 (1, &h.age);
1280 bfd_putl16 (0xffff, &h.global_stream_index);
1281 bfd_putl16 (0x8e1d, &h.build_number); // MSVC 14.29
1282 bfd_putl16 (publics_stream_num, &h.public_stream_index);
1283 bfd_putl16 (0, &h.pdb_dll_version);
1284 bfd_putl16 (sym_rec_stream_num, &h.sym_record_stream);
1285 bfd_putl16 (0, &h.pdb_dll_rbld);
1286 bfd_putl32 (mod_info_size, &h.mod_info_size);
1287 bfd_putl32 (sc_size, &h.section_contribution_size);
1288 bfd_putl32 (0, &h.section_map_size);
1289 bfd_putl32 (source_info_size, &h.source_info_size);
1290 bfd_putl32 (0, &h.type_server_map_size);
1291 bfd_putl32 (0, &h.mfc_type_server_index);
1292 bfd_putl32 (sizeof (opt), &h.optional_dbg_header_size);
1293 bfd_putl32 (0, &h.ec_substream_size);
1294 bfd_putl16 (0, &h.flags);
1295 bfd_putl16 (get_arch_number (abfd), &h.machine);
1296 bfd_putl32 (0, &h.padding);
1297
1298 if (bfd_bwrite (&h, sizeof (h), stream) != sizeof (h))
1299 {
1300 free (source_info);
1301 free (sc);
1302 free (mod_info);
1303 return false;
1304 }
1305
1306 if (bfd_bwrite (mod_info, mod_info_size, stream) != mod_info_size)
1307 {
1308 free (source_info);
1309 free (sc);
1310 free (mod_info);
1311 return false;
1312 }
1313
1314 free (mod_info);
1315
1316 if (bfd_bwrite (sc, sc_size, stream) != sc_size)
1317 {
1318 free (source_info);
1319 free (sc);
1320 return false;
1321 }
1322
1323 free (sc);
1324
1325 if (bfd_bwrite (source_info, source_info_size, stream) != source_info_size)
1326 {
1327 free (source_info);
1328 return false;
1329 }
1330
1331 free (source_info);
1332
1333 bfd_putl16 (0xffff, &opt.fpo_stream);
1334 bfd_putl16 (0xffff, &opt.exception_stream);
1335 bfd_putl16 (0xffff, &opt.fixup_stream);
1336 bfd_putl16 (0xffff, &opt.omap_to_src_stream);
1337 bfd_putl16 (0xffff, &opt.omap_from_src_stream);
1338 bfd_putl16 (section_header_stream_num, &opt.section_header_stream);
1339 bfd_putl16 (0xffff, &opt.token_map_stream);
1340 bfd_putl16 (0xffff, &opt.xdata_stream);
1341 bfd_putl16 (0xffff, &opt.pdata_stream);
1342 bfd_putl16 (0xffff, &opt.new_fpo_stream);
1343 bfd_putl16 (0xffff, &opt.orig_section_header_stream);
1344
1345 if (bfd_bwrite (&opt, sizeof (opt), stream) != sizeof (opt))
1346 return false;
1347
1348 return true;
1349 }
1350
1351 /* Used as parameter to qsort, to sort publics by hash. */
1352 static int
1353 public_compare_hash (const void *s1, const void *s2)
1354 {
1355 const struct public *p1 = *(const struct public **) s1;
1356 const struct public *p2 = *(const struct public **) s2;
1357
1358 if (p1->hash < p2->hash)
1359 return -1;
1360 if (p1->hash > p2->hash)
1361 return 1;
1362
1363 return 0;
1364 }
1365
1366 /* Used as parameter to qsort, to sort publics by address. */
1367 static int
1368 public_compare_addr (const void *s1, const void *s2)
1369 {
1370 const struct public *p1 = *(const struct public **) s1;
1371 const struct public *p2 = *(const struct public **) s2;
1372
1373 if (p1->section < p2->section)
1374 return -1;
1375 if (p1->section > p2->section)
1376 return 1;
1377
1378 if (p1->address < p2->address)
1379 return -1;
1380 if (p1->address > p2->address)
1381 return 1;
1382
1383 return 0;
1384 }
1385
1386 /* The publics stream is a hash map of S_PUB32 records, which are stored
1387 in the symbol record stream. Each S_PUB32 entry represents a symbol
1388 from the point of view of the linker: a section index, an offset within
1389 the section, and a mangled name. Compare with S_GDATA32 and S_GPROC32,
1390 which are the same thing but generated by the compiler. */
1391 static bool
1392 populate_publics_stream (bfd *stream, bfd *abfd, bfd *sym_rec_stream)
1393 {
1394 struct publics_header header;
1395 struct globals_hash_header hash_header;
1396 const unsigned int num_buckets = 4096;
1397 unsigned int num_entries = 0, filled_buckets = 0;
1398 unsigned int buckets_size, sym_hash_size;
1399 char int_buf[sizeof (uint32_t)];
1400 struct public *publics_head = NULL, *publics_tail = NULL;
1401 struct public **buckets;
1402 struct public **sorted = NULL;
1403 bool ret = false;
1404
1405 buckets = xmalloc (sizeof (struct public *) * num_buckets);
1406 memset (buckets, 0, sizeof (struct public *) * num_buckets);
1407
1408 /* Loop through the global symbols in our input files, and write S_PUB32
1409 records in the symbol record stream for those that make it into the
1410 final image. */
1411 for (bfd *in = coff_data (abfd)->link_info->input_bfds; in;
1412 in = in->link.next)
1413 {
1414 if (!in->outsymbols)
1415 continue;
1416
1417 for (unsigned int i = 0; i < in->symcount; i++)
1418 {
1419 struct bfd_symbol *sym = in->outsymbols[i];
1420
1421 if (sym->flags & BSF_GLOBAL)
1422 {
1423 struct pubsym ps;
1424 uint16_t record_length;
1425 const char *name = sym->name;
1426 size_t name_len = strlen (name);
1427 struct public *p = xmalloc (sizeof (struct public));
1428 unsigned int padding = 0;
1429 uint16_t section;
1430 uint32_t flags = 0;
1431
1432 section =
1433 find_section_number (abfd, sym->section->output_section);
1434
1435 if (section == 0)
1436 continue;
1437
1438 p->next = NULL;
1439 p->offset = bfd_tell (sym_rec_stream);
1440 p->hash = calc_hash (name, name_len) % num_buckets;
1441 p->section = section;
1442 p->address = sym->section->output_offset + sym->value;
1443
1444 record_length = sizeof (struct pubsym) + name_len + 1;
1445
1446 if (record_length % 4)
1447 padding = 4 - (record_length % 4);
1448
1449 /* Assume that all global symbols in executable sections
1450 are functions. */
1451 if (sym->section->flags & SEC_CODE)
1452 flags = PUBSYM_FUNCTION;
1453
1454 bfd_putl16 (record_length + padding - sizeof (uint16_t),
1455 &ps.record_length);
1456 bfd_putl16 (S_PUB32, &ps.record_type);
1457 bfd_putl32 (flags, &ps.flags);
1458 bfd_putl32 (p->address, &ps.offset);
1459 bfd_putl16 (p->section, &ps.section);
1460
1461 if (bfd_bwrite (&ps, sizeof (struct pubsym), sym_rec_stream) !=
1462 sizeof (struct pubsym))
1463 goto end;
1464
1465 if (bfd_bwrite (name, name_len + 1, sym_rec_stream) !=
1466 name_len + 1)
1467 goto end;
1468
1469 for (unsigned int j = 0; j < padding; j++)
1470 {
1471 uint8_t b = 0;
1472
1473 if (bfd_bwrite (&b, sizeof (uint8_t), sym_rec_stream) !=
1474 sizeof (uint8_t))
1475 goto end;
1476 }
1477
1478 if (!publics_head)
1479 publics_head = p;
1480 else
1481 publics_tail->next = p;
1482
1483 publics_tail = p;
1484 num_entries++;
1485 }
1486 }
1487 }
1488
1489
1490 if (num_entries > 0)
1491 {
1492 /* Create an array of pointers, sorted by hash value. */
1493
1494 sorted = xmalloc (sizeof (struct public *) * num_entries);
1495
1496 struct public *p = publics_head;
1497 for (unsigned int i = 0; i < num_entries; i++)
1498 {
1499 sorted[i] = p;
1500 p = p->next;
1501 }
1502
1503 qsort (sorted, num_entries, sizeof (struct public *),
1504 public_compare_hash);
1505
1506 /* Populate the buckets. */
1507
1508 for (unsigned int i = 0; i < num_entries; i++)
1509 {
1510 if (!buckets[sorted[i]->hash])
1511 {
1512 buckets[sorted[i]->hash] = sorted[i];
1513 filled_buckets++;
1514 }
1515
1516 sorted[i]->index = i;
1517 }
1518 }
1519
1520 buckets_size = num_buckets / 8;
1521 buckets_size += sizeof (uint32_t);
1522 buckets_size += filled_buckets * sizeof (uint32_t);
1523
1524 sym_hash_size = sizeof (hash_header);
1525 sym_hash_size += num_entries * sizeof (struct hash_record);
1526 sym_hash_size += buckets_size;
1527
1528 /* Output the publics header. */
1529
1530 bfd_putl32 (sym_hash_size, &header.sym_hash_size);
1531 bfd_putl32 (num_entries * sizeof (uint32_t), &header.addr_map_size);
1532 bfd_putl32 (0, &header.num_thunks);
1533 bfd_putl32 (0, &header.thunks_size);
1534 bfd_putl32 (0, &header.thunk_table);
1535 bfd_putl32 (0, &header.thunk_table_offset);
1536 bfd_putl32 (0, &header.num_sects);
1537
1538 if (bfd_bwrite (&header, sizeof (header), stream) != sizeof (header))
1539 goto end;
1540
1541 /* Output the global hash header. */
1542
1543 bfd_putl32 (GLOBALS_HASH_SIGNATURE, &hash_header.signature);
1544 bfd_putl32 (GLOBALS_HASH_VERSION_70, &hash_header.version);
1545 bfd_putl32 (num_entries * sizeof (struct hash_record),
1546 &hash_header.entries_size);
1547 bfd_putl32 (buckets_size, &hash_header.buckets_size);
1548
1549 if (bfd_bwrite (&hash_header, sizeof (hash_header), stream) !=
1550 sizeof (hash_header))
1551 goto end;
1552
1553 /* Write the entries in hash order. */
1554
1555 for (unsigned int i = 0; i < num_entries; i++)
1556 {
1557 struct hash_record hr;
1558
1559 bfd_putl32 (sorted[i]->offset + 1, &hr.offset);
1560 bfd_putl32 (1, &hr.reference);
1561
1562 if (bfd_bwrite (&hr, sizeof (hr), stream) != sizeof (hr))
1563 goto end;
1564 }
1565
1566 /* Write the bitmap for filled and unfilled buckets. */
1567
1568 for (unsigned int i = 0; i < num_buckets; i += 8)
1569 {
1570 uint8_t v = 0;
1571
1572 for (unsigned int j = 0; j < 8; j++)
1573 {
1574 if (buckets[i + j])
1575 v |= 1 << j;
1576 }
1577
1578 if (bfd_bwrite (&v, sizeof (v), stream) != sizeof (v))
1579 goto end;
1580 }
1581
1582 /* Add a 4-byte gap. */
1583
1584 bfd_putl32 (0, int_buf);
1585
1586 if (bfd_bwrite (int_buf, sizeof (uint32_t), stream) != sizeof (uint32_t))
1587 goto end;
1588
1589 /* Write the bucket offsets. */
1590
1591 for (unsigned int i = 0; i < num_buckets; i++)
1592 {
1593 if (buckets[i])
1594 {
1595 /* 0xc is size of internal hash_record structure in
1596 Microsoft's parser. */
1597 bfd_putl32 (buckets[i]->index * 0xc, int_buf);
1598
1599 if (bfd_bwrite (int_buf, sizeof (uint32_t), stream) !=
1600 sizeof (uint32_t))
1601 goto end;
1602 }
1603 }
1604
1605 /* Write the address map: offsets into the symbol record stream of
1606 S_PUB32 records, ordered by address. */
1607
1608 if (num_entries > 0)
1609 {
1610 qsort (sorted, num_entries, sizeof (struct public *),
1611 public_compare_addr);
1612
1613 for (unsigned int i = 0; i < num_entries; i++)
1614 {
1615 bfd_putl32 (sorted[i]->offset, int_buf);
1616
1617 if (bfd_bwrite (int_buf, sizeof (uint32_t), stream) !=
1618 sizeof (uint32_t))
1619 goto end;
1620 }
1621 }
1622
1623 ret = true;
1624
1625 end:
1626 free (buckets);
1627
1628 while (publics_head)
1629 {
1630 struct public *p = publics_head->next;
1631
1632 free (publics_head);
1633 publics_head = p;
1634 }
1635
1636 free (sorted);
1637
1638 return ret;
1639 }
1640
1641 /* The section header stream contains a copy of the section headers
1642 from the PE file, in the same format. */
1643 static bool
1644 create_section_header_stream (bfd *pdb, bfd *abfd, uint16_t *num)
1645 {
1646 bfd *stream;
1647 unsigned int section_count;
1648 file_ptr scn_base;
1649 size_t len;
1650 char *buf;
1651
1652 stream = add_stream (pdb, NULL, num);
1653 if (!stream)
1654 return false;
1655
1656 section_count = abfd->section_count;
1657
1658 /* Empty sections aren't output. */
1659 for (asection *sect = abfd->sections; sect; sect = sect->next)
1660 {
1661 if (sect->size == 0)
1662 section_count--;
1663 }
1664
1665 if (section_count == 0)
1666 return true;
1667
1668 /* Copy section table from output - it's already been written at this
1669 point. */
1670
1671 scn_base = bfd_coff_filhsz (abfd) + bfd_coff_aoutsz (abfd);
1672
1673 bfd_seek (abfd, scn_base, SEEK_SET);
1674
1675 len = section_count * sizeof (struct external_scnhdr);
1676 buf = xmalloc (len);
1677
1678 if (bfd_bread (buf, len, abfd) != len)
1679 {
1680 free (buf);
1681 return false;
1682 }
1683
1684 if (bfd_bwrite (buf, len, stream) != len)
1685 {
1686 free (buf);
1687 return false;
1688 }
1689
1690 free (buf);
1691
1692 return true;
1693 }
1694
1695 /* Populate the "/names" named stream, which contains the string table. */
1696 static bool
1697 populate_names_stream (bfd *stream, struct string_table *strings)
1698 {
1699 char int_buf[sizeof (uint32_t)];
1700 struct string_table_header h;
1701 uint32_t num_strings = 0, num_buckets;
1702 struct string **buckets;
1703
1704 bfd_putl32 (STRING_TABLE_SIGNATURE, &h.signature);
1705 bfd_putl32 (STRING_TABLE_VERSION, &h.version);
1706
1707 if (bfd_bwrite (&h, sizeof (h), stream) != sizeof (h))
1708 return false;
1709
1710 bfd_putl32 (strings->strings_len, int_buf);
1711
1712 if (bfd_bwrite (int_buf, sizeof (uint32_t), stream) != sizeof (uint32_t))
1713 return false;
1714
1715 int_buf[0] = 0;
1716
1717 if (bfd_bwrite (int_buf, 1, stream) != 1)
1718 return false;
1719
1720 for (struct string *s = strings->strings_head; s; s = s->next)
1721 {
1722 if (bfd_bwrite (s->s, s->len, stream) != s->len)
1723 return false;
1724
1725 if (bfd_bwrite (int_buf, 1, stream) != 1)
1726 return false;
1727
1728 num_strings++;
1729 }
1730
1731 num_buckets = num_strings * 2;
1732
1733 buckets = xmalloc (sizeof (struct string *) * num_buckets);
1734 memset (buckets, 0, sizeof (struct string *) * num_buckets);
1735
1736 for (struct string *s = strings->strings_head; s; s = s->next)
1737 {
1738 uint32_t bucket_num = s->hash % num_buckets;
1739
1740 while (buckets[bucket_num])
1741 {
1742 bucket_num++;
1743
1744 if (bucket_num == num_buckets)
1745 bucket_num = 0;
1746 }
1747
1748 buckets[bucket_num] = s;
1749 }
1750
1751 bfd_putl32 (num_buckets, int_buf);
1752
1753 if (bfd_bwrite (int_buf, sizeof (uint32_t), stream) != sizeof (uint32_t))
1754 {
1755 free (buckets);
1756 return false;
1757 }
1758
1759 for (unsigned int i = 0; i < num_buckets; i++)
1760 {
1761 if (buckets[i])
1762 bfd_putl32 (buckets[i]->offset, int_buf);
1763 else
1764 bfd_putl32 (0, int_buf);
1765
1766 if (bfd_bwrite (int_buf, sizeof (uint32_t), stream) !=
1767 sizeof (uint32_t))
1768 {
1769 free (buckets);
1770 return false;
1771 }
1772 }
1773
1774 free (buckets);
1775
1776 bfd_putl32 (num_strings, int_buf);
1777
1778 if (bfd_bwrite (int_buf, sizeof (uint32_t), stream) != sizeof (uint32_t))
1779 return false;
1780
1781 return true;
1782 }
1783
1784 /* Create a PDB debugging file for the PE image file abfd with the build ID
1785 guid, stored at pdb_name. */
1786 bool
1787 create_pdb_file (bfd *abfd, const char *pdb_name, const unsigned char *guid)
1788 {
1789 bfd *pdb;
1790 bool ret = false;
1791 bfd *info_stream, *dbi_stream, *names_stream, *sym_rec_stream,
1792 *publics_stream;
1793 uint16_t section_header_stream_num, sym_rec_stream_num, publics_stream_num;
1794 struct string_table strings;
1795
1796 pdb = bfd_openw (pdb_name, "pdb");
1797 if (!pdb)
1798 {
1799 einfo (_("%P: warning: cannot create PDB file: %E\n"));
1800 return false;
1801 }
1802
1803 strings.strings_head = NULL;
1804 strings.strings_tail = NULL;
1805 strings.strings_len = 1;
1806 strings.hashmap = htab_create_alloc (0, hash_string_table_entry,
1807 eq_string_table_entry, free,
1808 xcalloc, free);
1809
1810 bfd_set_format (pdb, bfd_archive);
1811
1812 if (!create_old_directory_stream (pdb))
1813 {
1814 einfo (_("%P: warning: cannot create old directory stream "
1815 "in PDB file: %E\n"));
1816 goto end;
1817 }
1818
1819 info_stream = add_stream (pdb, NULL, NULL);
1820
1821 if (!info_stream)
1822 {
1823 einfo (_("%P: warning: cannot create info stream "
1824 "in PDB file: %E\n"));
1825 goto end;
1826 }
1827
1828 if (!create_type_stream (pdb))
1829 {
1830 einfo (_("%P: warning: cannot create TPI stream "
1831 "in PDB file: %E\n"));
1832 goto end;
1833 }
1834
1835 dbi_stream = add_stream (pdb, NULL, NULL);
1836
1837 if (!dbi_stream)
1838 {
1839 einfo (_("%P: warning: cannot create DBI stream "
1840 "in PDB file: %E\n"));
1841 goto end;
1842 }
1843
1844 if (!create_type_stream (pdb))
1845 {
1846 einfo (_("%P: warning: cannot create IPI stream "
1847 "in PDB file: %E\n"));
1848 goto end;
1849 }
1850
1851 names_stream = add_stream (pdb, "/names", NULL);
1852
1853 if (!names_stream)
1854 {
1855 einfo (_("%P: warning: cannot create /names stream "
1856 "in PDB file: %E\n"));
1857 goto end;
1858 }
1859
1860 sym_rec_stream = add_stream (pdb, NULL, &sym_rec_stream_num);
1861
1862 if (!sym_rec_stream)
1863 {
1864 einfo (_("%P: warning: cannot create symbol record stream "
1865 "in PDB file: %E\n"));
1866 goto end;
1867 }
1868
1869 publics_stream = add_stream (pdb, NULL, &publics_stream_num);
1870
1871 if (!publics_stream)
1872 {
1873 einfo (_("%P: warning: cannot create publics stream "
1874 "in PDB file: %E\n"));
1875 goto end;
1876 }
1877
1878 if (!create_section_header_stream (pdb, abfd, &section_header_stream_num))
1879 {
1880 einfo (_("%P: warning: cannot create section header stream "
1881 "in PDB file: %E\n"));
1882 goto end;
1883 }
1884
1885 if (!populate_dbi_stream (dbi_stream, abfd, pdb, section_header_stream_num,
1886 sym_rec_stream_num, publics_stream_num,
1887 &strings))
1888 {
1889 einfo (_("%P: warning: cannot populate DBI stream "
1890 "in PDB file: %E\n"));
1891 goto end;
1892 }
1893
1894 add_string ("", 0, &strings);
1895
1896 if (!populate_names_stream (names_stream, &strings))
1897 {
1898 einfo (_("%P: warning: cannot populate names stream "
1899 "in PDB file: %E\n"));
1900 goto end;
1901 }
1902
1903 if (!populate_publics_stream (publics_stream, abfd, sym_rec_stream))
1904 {
1905 einfo (_("%P: warning: cannot populate publics stream "
1906 "in PDB file: %E\n"));
1907 goto end;
1908 }
1909
1910 if (!populate_info_stream (pdb, info_stream, guid))
1911 {
1912 einfo (_("%P: warning: cannot populate info stream "
1913 "in PDB file: %E\n"));
1914 goto end;
1915 }
1916
1917 ret = true;
1918
1919 end:
1920 bfd_close (pdb);
1921
1922 htab_delete (strings.hashmap);
1923
1924 return ret;
1925 }