ld: Add module information substream to PDB files
[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 /* Add a new stream to the PDB archive, and return its BFD. */
45 static bfd *
46 add_stream (bfd *pdb, const char *name, uint16_t *stream_num)
47 {
48 bfd *stream;
49 uint16_t num;
50
51 stream = bfd_create (name ? name : "", pdb);
52 if (!stream)
53 return NULL;
54
55 if (!bfd_make_writable (stream))
56 {
57 bfd_close (stream);
58 return false;
59 }
60
61 if (!pdb->archive_head)
62 {
63 bfd_set_archive_head (pdb, stream);
64 num = 0;
65 }
66 else
67 {
68 bfd *b = pdb->archive_head;
69
70 num = 1;
71
72 while (b->archive_next)
73 {
74 num++;
75 b = b->archive_next;
76 }
77
78 b->archive_next = stream;
79 }
80
81 if (stream_num)
82 *stream_num = num;
83
84 return stream;
85 }
86
87 /* Stream 0 ought to be a copy of the MSF directory from the last
88 time the PDB file was written. Because we don't do incremental
89 writes this isn't applicable to us, but we fill it with a dummy
90 value so as not to confuse radare. */
91 static bool
92 create_old_directory_stream (bfd *pdb)
93 {
94 bfd *stream;
95 char buf[sizeof (uint32_t)];
96
97 stream = add_stream (pdb, NULL, NULL);
98 if (!stream)
99 return false;
100
101 bfd_putl32 (0, buf);
102
103 return bfd_bwrite (buf, sizeof (uint32_t), stream) == sizeof (uint32_t);
104 }
105
106 /* Calculate the hash of a given string. */
107 static uint32_t
108 calc_hash (const char *data, size_t len)
109 {
110 uint32_t hash = 0;
111
112 while (len >= 4)
113 {
114 hash ^= data[0];
115 hash ^= data[1] << 8;
116 hash ^= data[2] << 16;
117 hash ^= data[3] << 24;
118
119 data += 4;
120 len -= 4;
121 }
122
123 if (len >= 2)
124 {
125 hash ^= data[0];
126 hash ^= data[1] << 8;
127
128 data += 2;
129 len -= 2;
130 }
131
132 if (len != 0)
133 hash ^= *data;
134
135 hash |= 0x20202020;
136 hash ^= (hash >> 11);
137
138 return hash ^ (hash >> 16);
139 }
140
141 /* Stream 1 is the PDB info stream - see
142 https://llvm.org/docs/PDB/PdbStream.html. */
143 static bool
144 populate_info_stream (bfd *pdb, bfd *info_stream, const unsigned char *guid)
145 {
146 bool ret = false;
147 struct pdb_stream_70 h;
148 uint32_t num_entries, num_buckets;
149 uint32_t names_length, stream_num;
150 char int_buf[sizeof (uint32_t)];
151
152 struct hash_entry
153 {
154 uint32_t offset;
155 uint32_t value;
156 };
157
158 struct hash_entry **buckets = NULL;
159
160 /* Write header. */
161
162 bfd_putl32 (PDB_STREAM_VERSION_VC70, &h.version);
163 bfd_putl32 (time (NULL), &h.signature);
164 bfd_putl32 (1, &h.age);
165
166 bfd_putl32 (bfd_getb32 (guid), h.guid);
167 bfd_putl16 (bfd_getb16 (&guid[4]), &h.guid[4]);
168 bfd_putl16 (bfd_getb16 (&guid[6]), &h.guid[6]);
169 memcpy (&h.guid[8], &guid[8], 8);
170
171 if (bfd_bwrite (&h, sizeof (h), info_stream) != sizeof (h))
172 return false;
173
174 /* Write hash list of named streams. This is a "rollover" hash, i.e.
175 if a bucket is filled an entry gets placed in the next free
176 slot. */
177
178 num_entries = 0;
179 for (bfd *b = pdb->archive_head; b; b = b->archive_next)
180 {
181 if (strcmp (b->filename, ""))
182 num_entries++;
183 }
184
185 num_buckets = num_entries * 2;
186
187 names_length = 0;
188 stream_num = 0;
189
190 if (num_buckets > 0)
191 {
192 buckets = xmalloc (sizeof (struct hash_entry *) * num_buckets);
193 memset (buckets, 0, sizeof (struct hash_entry *) * num_buckets);
194
195 for (bfd *b = pdb->archive_head; b; b = b->archive_next)
196 {
197 if (strcmp (b->filename, ""))
198 {
199 size_t len = strlen (b->filename);
200 uint32_t hash = (uint16_t) calc_hash (b->filename, len);
201 uint32_t bucket_num = hash % num_buckets;
202
203 while (buckets[bucket_num])
204 {
205 bucket_num++;
206
207 if (bucket_num == num_buckets)
208 bucket_num = 0;
209 }
210
211 buckets[bucket_num] = xmalloc (sizeof (struct hash_entry));
212
213 buckets[bucket_num]->offset = names_length;
214 buckets[bucket_num]->value = stream_num;
215
216 names_length += len + 1;
217 }
218
219 stream_num++;
220 }
221 }
222
223 /* Write the strings list - the hash keys are indexes into this. */
224
225 bfd_putl32 (names_length, int_buf);
226
227 if (bfd_bwrite (int_buf, sizeof (uint32_t), info_stream) !=
228 sizeof (uint32_t))
229 goto end;
230
231 for (bfd *b = pdb->archive_head; b; b = b->archive_next)
232 {
233 if (!strcmp (b->filename, ""))
234 continue;
235
236 size_t len = strlen (b->filename) + 1;
237
238 if (bfd_bwrite (b->filename, len, info_stream) != len)
239 goto end;
240 }
241
242 /* Write the number of entries and buckets. */
243
244 bfd_putl32 (num_entries, int_buf);
245
246 if (bfd_bwrite (int_buf, sizeof (uint32_t), info_stream) !=
247 sizeof (uint32_t))
248 goto end;
249
250 bfd_putl32 (num_buckets, int_buf);
251
252 if (bfd_bwrite (int_buf, sizeof (uint32_t), info_stream) !=
253 sizeof (uint32_t))
254 goto end;
255
256 /* Write the present bitmap. */
257
258 bfd_putl32 ((num_buckets + 31) / 32, int_buf);
259
260 if (bfd_bwrite (int_buf, sizeof (uint32_t), info_stream) !=
261 sizeof (uint32_t))
262 goto end;
263
264 for (unsigned int i = 0; i < num_buckets; i += 32)
265 {
266 uint32_t v = 0;
267
268 for (unsigned int j = 0; j < 32; j++)
269 {
270 if (i + j >= num_buckets)
271 break;
272
273 if (buckets[i + j])
274 v |= 1 << j;
275 }
276
277 bfd_putl32 (v, int_buf);
278
279 if (bfd_bwrite (int_buf, sizeof (uint32_t), info_stream) !=
280 sizeof (uint32_t))
281 goto end;
282 }
283
284 /* Write the (empty) deleted bitmap. */
285
286 bfd_putl32 (0, int_buf);
287
288 if (bfd_bwrite (int_buf, sizeof (uint32_t), info_stream) !=
289 sizeof (uint32_t))
290 goto end;
291
292 /* Write the buckets. */
293
294 for (unsigned int i = 0; i < num_buckets; i++)
295 {
296 if (buckets[i])
297 {
298 bfd_putl32 (buckets[i]->offset, int_buf);
299
300 if (bfd_bwrite (int_buf, sizeof (uint32_t), info_stream) !=
301 sizeof (uint32_t))
302 goto end;
303
304 bfd_putl32 (buckets[i]->value, int_buf);
305
306 if (bfd_bwrite (int_buf, sizeof (uint32_t), info_stream) !=
307 sizeof (uint32_t))
308 goto end;
309 }
310 }
311
312 bfd_putl32 (0, int_buf);
313
314 if (bfd_bwrite (int_buf, sizeof (uint32_t), info_stream) !=
315 sizeof (uint32_t))
316 goto end;
317
318 bfd_putl32 (PDB_STREAM_VERSION_VC140, int_buf);
319
320 if (bfd_bwrite (int_buf, sizeof (uint32_t), info_stream) !=
321 sizeof (uint32_t))
322 goto end;
323
324 ret = true;
325
326 end:
327 for (unsigned int i = 0; i < num_buckets; i++)
328 {
329 if (buckets[i])
330 free (buckets[i]);
331 }
332
333 free (buckets);
334
335 return ret;
336 }
337
338 /* Stream 2 is the type information (TPI) stream, and stream 4 is
339 the ID information (IPI) stream. They differ only in which records
340 go in which stream. */
341 static bool
342 create_type_stream (bfd *pdb)
343 {
344 bfd *stream;
345 struct pdb_tpi_stream_header h;
346
347 stream = add_stream (pdb, NULL, NULL);
348 if (!stream)
349 return false;
350
351 bfd_putl32 (TPI_STREAM_VERSION_80, &h.version);
352 bfd_putl32 (sizeof (h), &h.header_size);
353 bfd_putl32 (TPI_FIRST_INDEX, &h.type_index_begin);
354 bfd_putl32 (TPI_FIRST_INDEX, &h.type_index_end);
355 bfd_putl32 (0, &h.type_record_bytes);
356 bfd_putl16 (0xffff, &h.hash_stream_index);
357 bfd_putl16 (0xffff, &h.hash_aux_stream_index);
358 bfd_putl32 (4, &h.hash_key_size);
359 bfd_putl32 (0x3ffff, &h.num_hash_buckets);
360 bfd_putl32 (0, &h.hash_value_buffer_offset);
361 bfd_putl32 (0, &h.hash_value_buffer_length);
362 bfd_putl32 (0, &h.index_offset_buffer_offset);
363 bfd_putl32 (0, &h.index_offset_buffer_length);
364 bfd_putl32 (0, &h.hash_adj_buffer_offset);
365 bfd_putl32 (0, &h.hash_adj_buffer_length);
366
367 if (bfd_bwrite (&h, sizeof (h), stream) != sizeof (h))
368 return false;
369
370 return true;
371 }
372
373 /* Return the PE architecture number for the image. */
374 static uint16_t
375 get_arch_number (bfd *abfd)
376 {
377 if (abfd->arch_info->arch != bfd_arch_i386)
378 return 0;
379
380 if (abfd->arch_info->mach & bfd_mach_x86_64)
381 return IMAGE_FILE_MACHINE_AMD64;
382
383 return IMAGE_FILE_MACHINE_I386;
384 }
385
386 /* Populate the module stream, which consists of the transformed .debug$S
387 data for each object file. */
388 static bool
389 populate_module_stream (bfd *stream, uint32_t *sym_byte_size)
390 {
391 uint8_t int_buf[sizeof (uint32_t)];
392
393 *sym_byte_size = sizeof (uint32_t);
394
395 /* Write the signature. */
396
397 bfd_putl32 (CV_SIGNATURE_C13, int_buf);
398
399 if (bfd_bwrite (int_buf, sizeof (uint32_t), stream) != sizeof (uint32_t))
400 return false;
401
402 /* Write the global refs size. */
403
404 bfd_putl32 (0, int_buf);
405
406 if (bfd_bwrite (int_buf, sizeof (uint32_t), stream) != sizeof (uint32_t))
407 return false;
408
409 return true;
410 }
411
412 /* Create the module info substream within the DBI. */
413 static bool
414 create_module_info_substream (bfd *abfd, bfd *pdb, void **data,
415 uint32_t *size)
416 {
417 uint8_t *ptr;
418
419 static const char linker_fn[] = "* Linker *";
420
421 *size = 0;
422
423 for (bfd *in = coff_data (abfd)->link_info->input_bfds; in;
424 in = in->link.next)
425 {
426 size_t len = sizeof (struct module_info);
427
428 if (!strcmp (bfd_get_filename (in), "dll stuff"))
429 {
430 len += sizeof (linker_fn); /* Object name. */
431 len++; /* Empty module name. */
432 }
433 else if (in->my_archive)
434 {
435 char *name = lrealpath (bfd_get_filename (in));
436
437 len += strlen (name) + 1; /* Object name. */
438
439 free (name);
440
441 name = lrealpath (bfd_get_filename (in->my_archive));
442
443 len += strlen (name) + 1; /* Archive name. */
444
445 free (name);
446 }
447 else
448 {
449 char *name = lrealpath (bfd_get_filename (in));
450 size_t name_len = strlen (name) + 1;
451
452 len += name_len; /* Object name. */
453 len += name_len; /* And again as the archive name. */
454
455 free (name);
456 }
457
458 if (len % 4)
459 len += 4 - (len % 4);
460
461 *size += len;
462 }
463
464 *data = xmalloc (*size);
465
466 ptr = *data;
467
468 for (bfd *in = coff_data (abfd)->link_info->input_bfds; in;
469 in = in->link.next)
470 {
471 struct module_info *mod = (struct module_info *) ptr;
472 uint16_t stream_num;
473 bfd *stream;
474 uint32_t sym_byte_size;
475 uint8_t *start = ptr;
476
477 stream = add_stream (pdb, NULL, &stream_num);
478
479 if (!stream)
480 {
481 free (*data);
482 return false;
483 }
484
485 if (!populate_module_stream (stream, &sym_byte_size))
486 {
487 free (*data);
488 return false;
489 }
490
491 bfd_putl32 (0, &mod->unused1);
492
493 /* These are dummy values - MSVC copies the first section contribution
494 entry here, but doesn't seem to use it for anything. */
495 bfd_putl16 (0xffff, &mod->sc.section);
496 bfd_putl16 (0, &mod->sc.padding1);
497 bfd_putl32 (0, &mod->sc.offset);
498 bfd_putl32 (0xffffffff, &mod->sc.size);
499 bfd_putl32 (0, &mod->sc.characteristics);
500 bfd_putl16 (0xffff, &mod->sc.module_index);
501 bfd_putl16 (0, &mod->sc.padding2);
502 bfd_putl32 (0, &mod->sc.data_crc);
503 bfd_putl32 (0, &mod->sc.reloc_crc);
504
505 bfd_putl16 (0, &mod->flags);
506 bfd_putl16 (stream_num, &mod->module_sym_stream);
507 bfd_putl32 (sym_byte_size, &mod->sym_byte_size);
508 bfd_putl32 (0, &mod->c11_byte_size);
509 bfd_putl32 (0, &mod->c13_byte_size);
510 bfd_putl16 (0, &mod->source_file_count);
511 bfd_putl16 (0, &mod->padding);
512 bfd_putl32 (0, &mod->unused2);
513 bfd_putl32 (0, &mod->source_file_name_index);
514 bfd_putl32 (0, &mod->pdb_file_path_name_index);
515
516 ptr += sizeof (struct module_info);
517
518 if (!strcmp (bfd_get_filename (in), "dll stuff"))
519 {
520 /* Object name. */
521 memcpy (ptr, linker_fn, sizeof (linker_fn));
522 ptr += sizeof (linker_fn);
523
524 /* Empty module name. */
525 *ptr = 0;
526 ptr++;
527 }
528 else if (in->my_archive)
529 {
530 char *name = lrealpath (bfd_get_filename (in));
531 size_t name_len = strlen (name) + 1;
532
533 /* Object name. */
534 memcpy (ptr, name, name_len);
535 ptr += name_len;
536
537 free (name);
538
539 name = lrealpath (bfd_get_filename (in->my_archive));
540 name_len = strlen (name) + 1;
541
542 /* Archive name. */
543 memcpy (ptr, name, name_len);
544 ptr += name_len;
545
546 free (name);
547 }
548 else
549 {
550 char *name = lrealpath (bfd_get_filename (in));
551 size_t name_len = strlen (name) + 1;
552
553 /* Object name. */
554 memcpy (ptr, name, name_len);
555 ptr += name_len;
556
557 /* Object name again as archive name. */
558 memcpy (ptr, name, name_len);
559 ptr += name_len;
560
561 free (name);
562 }
563
564 /* Pad to next four-byte boundary. */
565
566 if ((ptr - start) % 4)
567 {
568 memset (ptr, 0, 4 - ((ptr - start) % 4));
569 ptr += 4 - ((ptr - start) % 4);
570 }
571 }
572
573 return true;
574 }
575
576 /* Return the index of a given output section. */
577 static uint16_t
578 find_section_number (bfd *abfd, asection *sect)
579 {
580 uint16_t i = 1;
581
582 for (asection *s = abfd->sections; s; s = s->next)
583 {
584 if (s == sect)
585 return i;
586
587 /* Empty sections aren't output. */
588 if (s->size != 0)
589 i++;
590 }
591
592 return 0;
593 }
594
595 /* Stream 4 is the debug information (DBI) stream. */
596 static bool
597 populate_dbi_stream (bfd *stream, bfd *abfd, bfd *pdb,
598 uint16_t section_header_stream_num,
599 uint16_t sym_rec_stream_num,
600 uint16_t publics_stream_num)
601 {
602 struct pdb_dbi_stream_header h;
603 struct optional_dbg_header opt;
604 void *mod_info;
605 uint32_t mod_info_size;
606
607 if (!create_module_info_substream (abfd, pdb, &mod_info, &mod_info_size))
608 return false;
609
610 bfd_putl32 (0xffffffff, &h.version_signature);
611 bfd_putl32 (DBI_STREAM_VERSION_70, &h.version_header);
612 bfd_putl32 (1, &h.age);
613 bfd_putl16 (0xffff, &h.global_stream_index);
614 bfd_putl16 (0x8e1d, &h.build_number); // MSVC 14.29
615 bfd_putl16 (publics_stream_num, &h.public_stream_index);
616 bfd_putl16 (0, &h.pdb_dll_version);
617 bfd_putl16 (sym_rec_stream_num, &h.sym_record_stream);
618 bfd_putl16 (0, &h.pdb_dll_rbld);
619 bfd_putl32 (mod_info_size, &h.mod_info_size);
620 bfd_putl32 (0, &h.section_contribution_size);
621 bfd_putl32 (0, &h.section_map_size);
622 bfd_putl32 (0, &h.source_info_size);
623 bfd_putl32 (0, &h.type_server_map_size);
624 bfd_putl32 (0, &h.mfc_type_server_index);
625 bfd_putl32 (sizeof (opt), &h.optional_dbg_header_size);
626 bfd_putl32 (0, &h.ec_substream_size);
627 bfd_putl16 (0, &h.flags);
628 bfd_putl16 (get_arch_number (abfd), &h.machine);
629 bfd_putl32 (0, &h.padding);
630
631 if (bfd_bwrite (&h, sizeof (h), stream) != sizeof (h))
632 {
633 free (mod_info);
634 return false;
635 }
636
637 if (bfd_bwrite (mod_info, mod_info_size, stream) != mod_info_size)
638 {
639 free (mod_info);
640 return false;
641 }
642
643 free (mod_info);
644
645 bfd_putl16 (0xffff, &opt.fpo_stream);
646 bfd_putl16 (0xffff, &opt.exception_stream);
647 bfd_putl16 (0xffff, &opt.fixup_stream);
648 bfd_putl16 (0xffff, &opt.omap_to_src_stream);
649 bfd_putl16 (0xffff, &opt.omap_from_src_stream);
650 bfd_putl16 (section_header_stream_num, &opt.section_header_stream);
651 bfd_putl16 (0xffff, &opt.token_map_stream);
652 bfd_putl16 (0xffff, &opt.xdata_stream);
653 bfd_putl16 (0xffff, &opt.pdata_stream);
654 bfd_putl16 (0xffff, &opt.new_fpo_stream);
655 bfd_putl16 (0xffff, &opt.orig_section_header_stream);
656
657 if (bfd_bwrite (&opt, sizeof (opt), stream) != sizeof (opt))
658 return false;
659
660 return true;
661 }
662
663 /* Used as parameter to qsort, to sort publics by hash. */
664 static int
665 public_compare_hash (const void *s1, const void *s2)
666 {
667 const struct public *p1 = *(const struct public **) s1;
668 const struct public *p2 = *(const struct public **) s2;
669
670 if (p1->hash < p2->hash)
671 return -1;
672 if (p1->hash > p2->hash)
673 return 1;
674
675 return 0;
676 }
677
678 /* Used as parameter to qsort, to sort publics by address. */
679 static int
680 public_compare_addr (const void *s1, const void *s2)
681 {
682 const struct public *p1 = *(const struct public **) s1;
683 const struct public *p2 = *(const struct public **) s2;
684
685 if (p1->section < p2->section)
686 return -1;
687 if (p1->section > p2->section)
688 return 1;
689
690 if (p1->address < p2->address)
691 return -1;
692 if (p1->address > p2->address)
693 return 1;
694
695 return 0;
696 }
697
698 /* The publics stream is a hash map of S_PUB32 records, which are stored
699 in the symbol record stream. Each S_PUB32 entry represents a symbol
700 from the point of view of the linker: a section index, an offset within
701 the section, and a mangled name. Compare with S_GDATA32 and S_GPROC32,
702 which are the same thing but generated by the compiler. */
703 static bool
704 populate_publics_stream (bfd *stream, bfd *abfd, bfd *sym_rec_stream)
705 {
706 struct publics_header header;
707 struct globals_hash_header hash_header;
708 const unsigned int num_buckets = 4096;
709 unsigned int num_entries = 0, filled_buckets = 0;
710 unsigned int buckets_size, sym_hash_size;
711 char int_buf[sizeof (uint32_t)];
712 struct public *publics_head = NULL, *publics_tail = NULL;
713 struct public **buckets;
714 struct public **sorted = NULL;
715 bool ret = false;
716
717 buckets = xmalloc (sizeof (struct public *) * num_buckets);
718 memset (buckets, 0, sizeof (struct public *) * num_buckets);
719
720 /* Loop through the global symbols in our input files, and write S_PUB32
721 records in the symbol record stream for those that make it into the
722 final image. */
723 for (bfd *in = coff_data (abfd)->link_info->input_bfds; in;
724 in = in->link.next)
725 {
726 for (unsigned int i = 0; i < in->symcount; i++)
727 {
728 struct bfd_symbol *sym = in->outsymbols[i];
729
730 if (sym->flags & BSF_GLOBAL)
731 {
732 struct pubsym ps;
733 uint16_t record_length;
734 const char *name = sym->name;
735 size_t name_len = strlen (name);
736 struct public *p = xmalloc (sizeof (struct public));
737 unsigned int padding = 0;
738 uint16_t section;
739 uint32_t flags = 0;
740
741 section =
742 find_section_number (abfd, sym->section->output_section);
743
744 if (section == 0)
745 continue;
746
747 p->next = NULL;
748 p->offset = bfd_tell (sym_rec_stream);
749 p->hash = calc_hash (name, name_len) % num_buckets;
750 p->section = section;
751 p->address = sym->section->output_offset + sym->value;
752
753 record_length = sizeof (struct pubsym) + name_len + 1;
754
755 if (record_length % 4)
756 padding = 4 - (record_length % 4);
757
758 /* Assume that all global symbols in executable sections
759 are functions. */
760 if (sym->section->flags & SEC_CODE)
761 flags = PUBSYM_FUNCTION;
762
763 bfd_putl16 (record_length + padding - sizeof (uint16_t),
764 &ps.record_length);
765 bfd_putl16 (S_PUB32, &ps.record_type);
766 bfd_putl32 (flags, &ps.flags);
767 bfd_putl32 (p->address, &ps.offset);
768 bfd_putl16 (p->section, &ps.section);
769
770 if (bfd_bwrite (&ps, sizeof (struct pubsym), sym_rec_stream) !=
771 sizeof (struct pubsym))
772 goto end;
773
774 if (bfd_bwrite (name, name_len + 1, sym_rec_stream) !=
775 name_len + 1)
776 goto end;
777
778 for (unsigned int j = 0; j < padding; j++)
779 {
780 uint8_t b = 0;
781
782 if (bfd_bwrite (&b, sizeof (uint8_t), sym_rec_stream) !=
783 sizeof (uint8_t))
784 goto end;
785 }
786
787 if (!publics_head)
788 publics_head = p;
789 else
790 publics_tail->next = p;
791
792 publics_tail = p;
793 num_entries++;
794 }
795 }
796 }
797
798
799 if (num_entries > 0)
800 {
801 /* Create an array of pointers, sorted by hash value. */
802
803 sorted = xmalloc (sizeof (struct public *) * num_entries);
804
805 struct public *p = publics_head;
806 for (unsigned int i = 0; i < num_entries; i++)
807 {
808 sorted[i] = p;
809 p = p->next;
810 }
811
812 qsort (sorted, num_entries, sizeof (struct public *),
813 public_compare_hash);
814
815 /* Populate the buckets. */
816
817 for (unsigned int i = 0; i < num_entries; i++)
818 {
819 if (!buckets[sorted[i]->hash])
820 {
821 buckets[sorted[i]->hash] = sorted[i];
822 filled_buckets++;
823 }
824
825 sorted[i]->index = i;
826 }
827 }
828
829 buckets_size = num_buckets / 8;
830 buckets_size += sizeof (uint32_t);
831 buckets_size += filled_buckets * sizeof (uint32_t);
832
833 sym_hash_size = sizeof (hash_header);
834 sym_hash_size += num_entries * sizeof (struct hash_record);
835 sym_hash_size += buckets_size;
836
837 /* Output the publics header. */
838
839 bfd_putl32 (sym_hash_size, &header.sym_hash_size);
840 bfd_putl32 (num_entries * sizeof (uint32_t), &header.addr_map_size);
841 bfd_putl32 (0, &header.num_thunks);
842 bfd_putl32 (0, &header.thunks_size);
843 bfd_putl32 (0, &header.thunk_table);
844 bfd_putl32 (0, &header.thunk_table_offset);
845 bfd_putl32 (0, &header.num_sects);
846
847 if (bfd_bwrite (&header, sizeof (header), stream) != sizeof (header))
848 goto end;
849
850 /* Output the global hash header. */
851
852 bfd_putl32 (GLOBALS_HASH_SIGNATURE, &hash_header.signature);
853 bfd_putl32 (GLOBALS_HASH_VERSION_70, &hash_header.version);
854 bfd_putl32 (num_entries * sizeof (struct hash_record),
855 &hash_header.entries_size);
856 bfd_putl32 (buckets_size, &hash_header.buckets_size);
857
858 if (bfd_bwrite (&hash_header, sizeof (hash_header), stream) !=
859 sizeof (hash_header))
860 goto end;
861
862 /* Write the entries in hash order. */
863
864 for (unsigned int i = 0; i < num_entries; i++)
865 {
866 struct hash_record hr;
867
868 bfd_putl32 (sorted[i]->offset + 1, &hr.offset);
869 bfd_putl32 (1, &hr.reference);
870
871 if (bfd_bwrite (&hr, sizeof (hr), stream) != sizeof (hr))
872 goto end;
873 }
874
875 /* Write the bitmap for filled and unfilled buckets. */
876
877 for (unsigned int i = 0; i < num_buckets; i += 8)
878 {
879 uint8_t v = 0;
880
881 for (unsigned int j = 0; j < 8; j++)
882 {
883 if (buckets[i + j])
884 v |= 1 << j;
885 }
886
887 if (bfd_bwrite (&v, sizeof (v), stream) != sizeof (v))
888 goto end;
889 }
890
891 /* Add a 4-byte gap. */
892
893 bfd_putl32 (0, int_buf);
894
895 if (bfd_bwrite (int_buf, sizeof (uint32_t), stream) != sizeof (uint32_t))
896 goto end;
897
898 /* Write the bucket offsets. */
899
900 for (unsigned int i = 0; i < num_buckets; i++)
901 {
902 if (buckets[i])
903 {
904 /* 0xc is size of internal hash_record structure in
905 Microsoft's parser. */
906 bfd_putl32 (buckets[i]->index * 0xc, int_buf);
907
908 if (bfd_bwrite (int_buf, sizeof (uint32_t), stream) !=
909 sizeof (uint32_t))
910 goto end;
911 }
912 }
913
914 /* Write the address map: offsets into the symbol record stream of
915 S_PUB32 records, ordered by address. */
916
917 if (num_entries > 0)
918 {
919 qsort (sorted, num_entries, sizeof (struct public *),
920 public_compare_addr);
921
922 for (unsigned int i = 0; i < num_entries; i++)
923 {
924 bfd_putl32 (sorted[i]->offset, int_buf);
925
926 if (bfd_bwrite (int_buf, sizeof (uint32_t), stream) !=
927 sizeof (uint32_t))
928 goto end;
929 }
930 }
931
932 ret = true;
933
934 end:
935 free (buckets);
936
937 while (publics_head)
938 {
939 struct public *p = publics_head->next;
940
941 free (publics_head);
942 publics_head = p;
943 }
944
945 free (sorted);
946
947 return ret;
948 }
949
950 /* The section header stream contains a copy of the section headers
951 from the PE file, in the same format. */
952 static bool
953 create_section_header_stream (bfd *pdb, bfd *abfd, uint16_t *num)
954 {
955 bfd *stream;
956 unsigned int section_count;
957 file_ptr scn_base;
958 size_t len;
959 char *buf;
960
961 stream = add_stream (pdb, NULL, num);
962 if (!stream)
963 return false;
964
965 section_count = abfd->section_count;
966
967 /* Empty sections aren't output. */
968 for (asection *sect = abfd->sections; sect; sect = sect->next)
969 {
970 if (sect->size == 0)
971 section_count--;
972 }
973
974 if (section_count == 0)
975 return true;
976
977 /* Copy section table from output - it's already been written at this
978 point. */
979
980 scn_base = bfd_coff_filhsz (abfd) + bfd_coff_aoutsz (abfd);
981
982 bfd_seek (abfd, scn_base, SEEK_SET);
983
984 len = section_count * sizeof (struct external_scnhdr);
985 buf = xmalloc (len);
986
987 if (bfd_bread (buf, len, abfd) != len)
988 {
989 free (buf);
990 return false;
991 }
992
993 if (bfd_bwrite (buf, len, stream) != len)
994 {
995 free (buf);
996 return false;
997 }
998
999 free (buf);
1000
1001 return true;
1002 }
1003
1004 /* Create a PDB debugging file for the PE image file abfd with the build ID
1005 guid, stored at pdb_name. */
1006 bool
1007 create_pdb_file (bfd *abfd, const char *pdb_name, const unsigned char *guid)
1008 {
1009 bfd *pdb;
1010 bool ret = false;
1011 bfd *info_stream, *dbi_stream, *names_stream, *sym_rec_stream,
1012 *publics_stream;
1013 uint16_t section_header_stream_num, sym_rec_stream_num, publics_stream_num;
1014
1015 pdb = bfd_openw (pdb_name, "pdb");
1016 if (!pdb)
1017 {
1018 einfo (_("%P: warning: cannot create PDB file: %E\n"));
1019 return false;
1020 }
1021
1022 bfd_set_format (pdb, bfd_archive);
1023
1024 if (!create_old_directory_stream (pdb))
1025 {
1026 einfo (_("%P: warning: cannot create old directory stream "
1027 "in PDB file: %E\n"));
1028 goto end;
1029 }
1030
1031 info_stream = add_stream (pdb, NULL, NULL);
1032
1033 if (!info_stream)
1034 {
1035 einfo (_("%P: warning: cannot create info stream "
1036 "in PDB file: %E\n"));
1037 goto end;
1038 }
1039
1040 if (!create_type_stream (pdb))
1041 {
1042 einfo (_("%P: warning: cannot create TPI stream "
1043 "in PDB file: %E\n"));
1044 goto end;
1045 }
1046
1047 dbi_stream = add_stream (pdb, NULL, NULL);
1048
1049 if (!dbi_stream)
1050 {
1051 einfo (_("%P: warning: cannot create DBI stream "
1052 "in PDB file: %E\n"));
1053 goto end;
1054 }
1055
1056 if (!create_type_stream (pdb))
1057 {
1058 einfo (_("%P: warning: cannot create IPI stream "
1059 "in PDB file: %E\n"));
1060 goto end;
1061 }
1062
1063 names_stream = add_stream (pdb, "/names", NULL);
1064
1065 if (!names_stream)
1066 {
1067 einfo (_("%P: warning: cannot create /names stream "
1068 "in PDB file: %E\n"));
1069 goto end;
1070 }
1071
1072 sym_rec_stream = add_stream (pdb, NULL, &sym_rec_stream_num);
1073
1074 if (!sym_rec_stream)
1075 {
1076 einfo (_("%P: warning: cannot create symbol record stream "
1077 "in PDB file: %E\n"));
1078 goto end;
1079 }
1080
1081 publics_stream = add_stream (pdb, NULL, &publics_stream_num);
1082
1083 if (!publics_stream)
1084 {
1085 einfo (_("%P: warning: cannot create publics stream "
1086 "in PDB file: %E\n"));
1087 goto end;
1088 }
1089
1090 if (!create_section_header_stream (pdb, abfd, &section_header_stream_num))
1091 {
1092 einfo (_("%P: warning: cannot create section header stream "
1093 "in PDB file: %E\n"));
1094 goto end;
1095 }
1096
1097 if (!populate_dbi_stream (dbi_stream, abfd, pdb, section_header_stream_num,
1098 sym_rec_stream_num, publics_stream_num))
1099 {
1100 einfo (_("%P: warning: cannot populate DBI stream "
1101 "in PDB file: %E\n"));
1102 goto end;
1103 }
1104
1105 if (!populate_publics_stream (publics_stream, abfd, sym_rec_stream))
1106 {
1107 einfo (_("%P: warning: cannot populate publics stream "
1108 "in PDB file: %E\n"));
1109 goto end;
1110 }
1111
1112 if (!populate_info_stream (pdb, info_stream, guid))
1113 {
1114 einfo (_("%P: warning: cannot populate info stream "
1115 "in PDB file: %E\n"));
1116 goto end;
1117 }
1118
1119 ret = true;
1120
1121 end:
1122 bfd_close (pdb);
1123
1124 return ret;
1125 }