]> Git Repo - binutils.git/blob - ld/pdb.c
Automatic date update in version.in
[binutils.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 /* Create the substream which maps addresses in the image file to locations
596    in the original object files.  */
597 static bool
598 create_section_contrib_substream (bfd *abfd, void **data, uint32_t *size)
599 {
600   unsigned int num_sc = 0;
601   struct section_contribution *sc;
602   uint16_t mod_index;
603   char *sect_flags;
604   file_ptr offset;
605
606   for (bfd *in = coff_data (abfd)->link_info->input_bfds; in;
607        in = in->link.next)
608     {
609       for (asection *s = in->sections; s; s = s->next)
610         {
611           if (s->size == 0 || discarded_section (s))
612             continue;
613
614           num_sc++;
615         }
616     }
617
618   *size = sizeof (uint32_t) + (num_sc * sizeof (struct section_contribution));
619   *data = xmalloc (*size);
620
621   bfd_putl32 (SECTION_CONTRIB_VERSION_60, *data);
622
623   /* Read characteristics of outputted sections.  */
624
625   sect_flags = xmalloc (sizeof (uint32_t) * abfd->section_count);
626
627   offset = bfd_coff_filhsz (abfd) + bfd_coff_aoutsz (abfd);
628   offset += offsetof (struct external_scnhdr, s_flags);
629
630   for (unsigned int i = 0; i < abfd->section_count; i++)
631     {
632       bfd_seek (abfd, offset, SEEK_SET);
633
634       if (bfd_bread (sect_flags + (i * sizeof (uint32_t)), sizeof (uint32_t),
635                      abfd) != sizeof (uint32_t))
636         {
637           free (*data);
638           free (sect_flags);
639           return false;
640         }
641
642       offset += sizeof (struct external_scnhdr);
643     }
644
645   sc =
646     (struct section_contribution *) ((uint8_t *) *data + sizeof (uint32_t));
647
648   mod_index = 0;
649   for (bfd *in = coff_data (abfd)->link_info->input_bfds; in;
650        in = in->link.next)
651     {
652       for (asection *s = in->sections; s; s = s->next)
653         {
654           uint16_t sect_num;
655
656           if (s->size == 0 || discarded_section (s))
657             continue;
658
659           sect_num = find_section_number (abfd, s->output_section);
660
661           memcpy (&sc->characteristics,
662                   sect_flags + ((sect_num - 1) * sizeof (uint32_t)),
663                   sizeof (uint32_t));
664
665           bfd_putl16 (sect_num, &sc->section);
666           bfd_putl16 (0, &sc->padding1);
667           bfd_putl32 (s->output_offset, &sc->offset);
668           bfd_putl32 (s->size, &sc->size);
669           bfd_putl16 (mod_index, &sc->module_index);
670           bfd_putl16 (0, &sc->padding2);
671           bfd_putl32 (0, &sc->data_crc);
672           bfd_putl32 (0, &sc->reloc_crc);
673
674           sc++;
675         }
676
677       mod_index++;
678     }
679
680   free (sect_flags);
681
682   return true;
683 }
684
685 /* Stream 4 is the debug information (DBI) stream.  */
686 static bool
687 populate_dbi_stream (bfd *stream, bfd *abfd, bfd *pdb,
688                      uint16_t section_header_stream_num,
689                      uint16_t sym_rec_stream_num,
690                      uint16_t publics_stream_num)
691 {
692   struct pdb_dbi_stream_header h;
693   struct optional_dbg_header opt;
694   void *mod_info, *sc;
695   uint32_t mod_info_size, sc_size;
696
697   if (!create_module_info_substream (abfd, pdb, &mod_info, &mod_info_size))
698     return false;
699
700   if (!create_section_contrib_substream (abfd, &sc, &sc_size))
701     {
702       free (mod_info);
703       return false;
704     }
705
706   bfd_putl32 (0xffffffff, &h.version_signature);
707   bfd_putl32 (DBI_STREAM_VERSION_70, &h.version_header);
708   bfd_putl32 (1, &h.age);
709   bfd_putl16 (0xffff, &h.global_stream_index);
710   bfd_putl16 (0x8e1d, &h.build_number); // MSVC 14.29
711   bfd_putl16 (publics_stream_num, &h.public_stream_index);
712   bfd_putl16 (0, &h.pdb_dll_version);
713   bfd_putl16 (sym_rec_stream_num, &h.sym_record_stream);
714   bfd_putl16 (0, &h.pdb_dll_rbld);
715   bfd_putl32 (mod_info_size, &h.mod_info_size);
716   bfd_putl32 (sc_size, &h.section_contribution_size);
717   bfd_putl32 (0, &h.section_map_size);
718   bfd_putl32 (0, &h.source_info_size);
719   bfd_putl32 (0, &h.type_server_map_size);
720   bfd_putl32 (0, &h.mfc_type_server_index);
721   bfd_putl32 (sizeof (opt), &h.optional_dbg_header_size);
722   bfd_putl32 (0, &h.ec_substream_size);
723   bfd_putl16 (0, &h.flags);
724   bfd_putl16 (get_arch_number (abfd), &h.machine);
725   bfd_putl32 (0, &h.padding);
726
727   if (bfd_bwrite (&h, sizeof (h), stream) != sizeof (h))
728     {
729       free (sc);
730       free (mod_info);
731       return false;
732     }
733
734   if (bfd_bwrite (mod_info, mod_info_size, stream) != mod_info_size)
735     {
736       free (sc);
737       free (mod_info);
738       return false;
739     }
740
741   free (mod_info);
742
743   if (bfd_bwrite (sc, sc_size, stream) != sc_size)
744     {
745       free (sc);
746       return false;
747     }
748
749   free (sc);
750
751   bfd_putl16 (0xffff, &opt.fpo_stream);
752   bfd_putl16 (0xffff, &opt.exception_stream);
753   bfd_putl16 (0xffff, &opt.fixup_stream);
754   bfd_putl16 (0xffff, &opt.omap_to_src_stream);
755   bfd_putl16 (0xffff, &opt.omap_from_src_stream);
756   bfd_putl16 (section_header_stream_num, &opt.section_header_stream);
757   bfd_putl16 (0xffff, &opt.token_map_stream);
758   bfd_putl16 (0xffff, &opt.xdata_stream);
759   bfd_putl16 (0xffff, &opt.pdata_stream);
760   bfd_putl16 (0xffff, &opt.new_fpo_stream);
761   bfd_putl16 (0xffff, &opt.orig_section_header_stream);
762
763   if (bfd_bwrite (&opt, sizeof (opt), stream) != sizeof (opt))
764     return false;
765
766   return true;
767 }
768
769 /* Used as parameter to qsort, to sort publics by hash.  */
770 static int
771 public_compare_hash (const void *s1, const void *s2)
772 {
773   const struct public *p1 = *(const struct public **) s1;
774   const struct public *p2 = *(const struct public **) s2;
775
776   if (p1->hash < p2->hash)
777     return -1;
778   if (p1->hash > p2->hash)
779     return 1;
780
781   return 0;
782 }
783
784 /* Used as parameter to qsort, to sort publics by address.  */
785 static int
786 public_compare_addr (const void *s1, const void *s2)
787 {
788   const struct public *p1 = *(const struct public **) s1;
789   const struct public *p2 = *(const struct public **) s2;
790
791   if (p1->section < p2->section)
792     return -1;
793   if (p1->section > p2->section)
794     return 1;
795
796   if (p1->address < p2->address)
797     return -1;
798   if (p1->address > p2->address)
799     return 1;
800
801   return 0;
802 }
803
804 /* The publics stream is a hash map of S_PUB32 records, which are stored
805    in the symbol record stream.  Each S_PUB32 entry represents a symbol
806    from the point of view of the linker: a section index, an offset within
807    the section, and a mangled name.  Compare with S_GDATA32 and S_GPROC32,
808    which are the same thing but generated by the compiler.  */
809 static bool
810 populate_publics_stream (bfd *stream, bfd *abfd, bfd *sym_rec_stream)
811 {
812   struct publics_header header;
813   struct globals_hash_header hash_header;
814   const unsigned int num_buckets = 4096;
815   unsigned int num_entries = 0, filled_buckets = 0;
816   unsigned int buckets_size, sym_hash_size;
817   char int_buf[sizeof (uint32_t)];
818   struct public *publics_head = NULL, *publics_tail = NULL;
819   struct public **buckets;
820   struct public **sorted = NULL;
821   bool ret = false;
822
823   buckets = xmalloc (sizeof (struct public *) * num_buckets);
824   memset (buckets, 0, sizeof (struct public *) * num_buckets);
825
826   /* Loop through the global symbols in our input files, and write S_PUB32
827      records in the symbol record stream for those that make it into the
828      final image.  */
829   for (bfd *in = coff_data (abfd)->link_info->input_bfds; in;
830        in = in->link.next)
831     {
832       for (unsigned int i = 0; i < in->symcount; i++)
833         {
834           struct bfd_symbol *sym = in->outsymbols[i];
835
836           if (sym->flags & BSF_GLOBAL)
837             {
838               struct pubsym ps;
839               uint16_t record_length;
840               const char *name = sym->name;
841               size_t name_len = strlen (name);
842               struct public *p = xmalloc (sizeof (struct public));
843               unsigned int padding = 0;
844               uint16_t section;
845               uint32_t flags = 0;
846
847               section =
848                 find_section_number (abfd, sym->section->output_section);
849
850               if (section == 0)
851                 continue;
852
853               p->next = NULL;
854               p->offset = bfd_tell (sym_rec_stream);
855               p->hash = calc_hash (name, name_len) % num_buckets;
856               p->section = section;
857               p->address = sym->section->output_offset + sym->value;
858
859               record_length = sizeof (struct pubsym) + name_len + 1;
860
861               if (record_length % 4)
862                 padding = 4 - (record_length % 4);
863
864               /* Assume that all global symbols in executable sections
865                  are functions.  */
866               if (sym->section->flags & SEC_CODE)
867                 flags = PUBSYM_FUNCTION;
868
869               bfd_putl16 (record_length + padding - sizeof (uint16_t),
870                           &ps.record_length);
871               bfd_putl16 (S_PUB32, &ps.record_type);
872               bfd_putl32 (flags, &ps.flags);
873               bfd_putl32 (p->address, &ps.offset);
874               bfd_putl16 (p->section, &ps.section);
875
876               if (bfd_bwrite (&ps, sizeof (struct pubsym), sym_rec_stream) !=
877                   sizeof (struct pubsym))
878                 goto end;
879
880               if (bfd_bwrite (name, name_len + 1, sym_rec_stream) !=
881                   name_len + 1)
882                 goto end;
883
884               for (unsigned int j = 0; j < padding; j++)
885                 {
886                   uint8_t b = 0;
887
888                   if (bfd_bwrite (&b, sizeof (uint8_t), sym_rec_stream) !=
889                       sizeof (uint8_t))
890                     goto end;
891                 }
892
893               if (!publics_head)
894                 publics_head = p;
895               else
896                 publics_tail->next = p;
897
898               publics_tail = p;
899               num_entries++;
900             }
901         }
902     }
903
904
905   if (num_entries > 0)
906     {
907       /* Create an array of pointers, sorted by hash value.  */
908
909       sorted = xmalloc (sizeof (struct public *) * num_entries);
910
911       struct public *p = publics_head;
912       for (unsigned int i = 0; i < num_entries; i++)
913         {
914           sorted[i] = p;
915           p = p->next;
916         }
917
918       qsort (sorted, num_entries, sizeof (struct public *),
919              public_compare_hash);
920
921       /* Populate the buckets.  */
922
923       for (unsigned int i = 0; i < num_entries; i++)
924         {
925           if (!buckets[sorted[i]->hash])
926             {
927               buckets[sorted[i]->hash] = sorted[i];
928               filled_buckets++;
929             }
930
931           sorted[i]->index = i;
932         }
933     }
934
935   buckets_size = num_buckets / 8;
936   buckets_size += sizeof (uint32_t);
937   buckets_size += filled_buckets * sizeof (uint32_t);
938
939   sym_hash_size = sizeof (hash_header);
940   sym_hash_size += num_entries * sizeof (struct hash_record);
941   sym_hash_size += buckets_size;
942
943   /* Output the publics header.  */
944
945   bfd_putl32 (sym_hash_size, &header.sym_hash_size);
946   bfd_putl32 (num_entries * sizeof (uint32_t), &header.addr_map_size);
947   bfd_putl32 (0, &header.num_thunks);
948   bfd_putl32 (0, &header.thunks_size);
949   bfd_putl32 (0, &header.thunk_table);
950   bfd_putl32 (0, &header.thunk_table_offset);
951   bfd_putl32 (0, &header.num_sects);
952
953   if (bfd_bwrite (&header, sizeof (header), stream) != sizeof (header))
954     goto end;
955
956   /* Output the global hash header.  */
957
958   bfd_putl32 (GLOBALS_HASH_SIGNATURE, &hash_header.signature);
959   bfd_putl32 (GLOBALS_HASH_VERSION_70, &hash_header.version);
960   bfd_putl32 (num_entries * sizeof (struct hash_record),
961               &hash_header.entries_size);
962   bfd_putl32 (buckets_size, &hash_header.buckets_size);
963
964   if (bfd_bwrite (&hash_header, sizeof (hash_header), stream) !=
965       sizeof (hash_header))
966     goto end;
967
968   /* Write the entries in hash order.  */
969
970   for (unsigned int i = 0; i < num_entries; i++)
971     {
972       struct hash_record hr;
973
974       bfd_putl32 (sorted[i]->offset + 1, &hr.offset);
975       bfd_putl32 (1, &hr.reference);
976
977       if (bfd_bwrite (&hr, sizeof (hr), stream) != sizeof (hr))
978         goto end;
979     }
980
981   /* Write the bitmap for filled and unfilled buckets.  */
982
983   for (unsigned int i = 0; i < num_buckets; i += 8)
984     {
985       uint8_t v = 0;
986
987       for (unsigned int j = 0; j < 8; j++)
988         {
989           if (buckets[i + j])
990             v |= 1 << j;
991         }
992
993       if (bfd_bwrite (&v, sizeof (v), stream) != sizeof (v))
994         goto end;
995     }
996
997   /* Add a 4-byte gap.  */
998
999   bfd_putl32 (0, int_buf);
1000
1001   if (bfd_bwrite (int_buf, sizeof (uint32_t), stream) != sizeof (uint32_t))
1002     goto end;
1003
1004   /* Write the bucket offsets.  */
1005
1006   for (unsigned int i = 0; i < num_buckets; i++)
1007     {
1008       if (buckets[i])
1009         {
1010           /* 0xc is size of internal hash_record structure in
1011              Microsoft's parser.  */
1012           bfd_putl32 (buckets[i]->index * 0xc, int_buf);
1013
1014           if (bfd_bwrite (int_buf, sizeof (uint32_t), stream) !=
1015               sizeof (uint32_t))
1016             goto end;
1017         }
1018     }
1019
1020   /* Write the address map: offsets into the symbol record stream of
1021      S_PUB32 records, ordered by address.  */
1022
1023   if (num_entries > 0)
1024     {
1025       qsort (sorted, num_entries, sizeof (struct public *),
1026              public_compare_addr);
1027
1028       for (unsigned int i = 0; i < num_entries; i++)
1029         {
1030           bfd_putl32 (sorted[i]->offset, int_buf);
1031
1032           if (bfd_bwrite (int_buf, sizeof (uint32_t), stream) !=
1033               sizeof (uint32_t))
1034             goto end;
1035         }
1036     }
1037
1038   ret = true;
1039
1040 end:
1041   free (buckets);
1042
1043   while (publics_head)
1044     {
1045       struct public *p = publics_head->next;
1046
1047       free (publics_head);
1048       publics_head = p;
1049     }
1050
1051   free (sorted);
1052
1053   return ret;
1054 }
1055
1056 /* The section header stream contains a copy of the section headers
1057    from the PE file, in the same format.  */
1058 static bool
1059 create_section_header_stream (bfd *pdb, bfd *abfd, uint16_t *num)
1060 {
1061   bfd *stream;
1062   unsigned int section_count;
1063   file_ptr scn_base;
1064   size_t len;
1065   char *buf;
1066
1067   stream = add_stream (pdb, NULL, num);
1068   if (!stream)
1069     return false;
1070
1071   section_count = abfd->section_count;
1072
1073   /* Empty sections aren't output.  */
1074   for (asection *sect = abfd->sections; sect; sect = sect->next)
1075     {
1076       if (sect->size == 0)
1077         section_count--;
1078     }
1079
1080   if (section_count == 0)
1081     return true;
1082
1083   /* Copy section table from output - it's already been written at this
1084      point.  */
1085
1086   scn_base = bfd_coff_filhsz (abfd) + bfd_coff_aoutsz (abfd);
1087
1088   bfd_seek (abfd, scn_base, SEEK_SET);
1089
1090   len = section_count * sizeof (struct external_scnhdr);
1091   buf = xmalloc (len);
1092
1093   if (bfd_bread (buf, len, abfd) != len)
1094     {
1095       free (buf);
1096       return false;
1097     }
1098
1099   if (bfd_bwrite (buf, len, stream) != len)
1100     {
1101       free (buf);
1102       return false;
1103     }
1104
1105   free (buf);
1106
1107   return true;
1108 }
1109
1110 /* Create a PDB debugging file for the PE image file abfd with the build ID
1111    guid, stored at pdb_name.  */
1112 bool
1113 create_pdb_file (bfd *abfd, const char *pdb_name, const unsigned char *guid)
1114 {
1115   bfd *pdb;
1116   bool ret = false;
1117   bfd *info_stream, *dbi_stream, *names_stream, *sym_rec_stream,
1118     *publics_stream;
1119   uint16_t section_header_stream_num, sym_rec_stream_num, publics_stream_num;
1120
1121   pdb = bfd_openw (pdb_name, "pdb");
1122   if (!pdb)
1123     {
1124       einfo (_("%P: warning: cannot create PDB file: %E\n"));
1125       return false;
1126     }
1127
1128   bfd_set_format (pdb, bfd_archive);
1129
1130   if (!create_old_directory_stream (pdb))
1131     {
1132       einfo (_("%P: warning: cannot create old directory stream "
1133                "in PDB file: %E\n"));
1134       goto end;
1135     }
1136
1137   info_stream = add_stream (pdb, NULL, NULL);
1138
1139   if (!info_stream)
1140     {
1141       einfo (_("%P: warning: cannot create info stream "
1142                "in PDB file: %E\n"));
1143       goto end;
1144     }
1145
1146   if (!create_type_stream (pdb))
1147     {
1148       einfo (_("%P: warning: cannot create TPI stream "
1149                "in PDB file: %E\n"));
1150       goto end;
1151     }
1152
1153   dbi_stream = add_stream (pdb, NULL, NULL);
1154
1155   if (!dbi_stream)
1156     {
1157       einfo (_("%P: warning: cannot create DBI stream "
1158                "in PDB file: %E\n"));
1159       goto end;
1160     }
1161
1162   if (!create_type_stream (pdb))
1163     {
1164       einfo (_("%P: warning: cannot create IPI stream "
1165                "in PDB file: %E\n"));
1166       goto end;
1167     }
1168
1169   names_stream = add_stream (pdb, "/names", NULL);
1170
1171   if (!names_stream)
1172     {
1173       einfo (_("%P: warning: cannot create /names stream "
1174                "in PDB file: %E\n"));
1175       goto end;
1176     }
1177
1178   sym_rec_stream = add_stream (pdb, NULL, &sym_rec_stream_num);
1179
1180   if (!sym_rec_stream)
1181     {
1182       einfo (_("%P: warning: cannot create symbol record stream "
1183                "in PDB file: %E\n"));
1184       goto end;
1185     }
1186
1187   publics_stream = add_stream (pdb, NULL, &publics_stream_num);
1188
1189   if (!publics_stream)
1190     {
1191       einfo (_("%P: warning: cannot create publics stream "
1192                "in PDB file: %E\n"));
1193       goto end;
1194     }
1195
1196   if (!create_section_header_stream (pdb, abfd, &section_header_stream_num))
1197     {
1198       einfo (_("%P: warning: cannot create section header stream "
1199                "in PDB file: %E\n"));
1200       goto end;
1201     }
1202
1203   if (!populate_dbi_stream (dbi_stream, abfd, pdb, section_header_stream_num,
1204                             sym_rec_stream_num, publics_stream_num))
1205     {
1206       einfo (_("%P: warning: cannot populate DBI stream "
1207                "in PDB file: %E\n"));
1208       goto end;
1209     }
1210
1211   if (!populate_publics_stream (publics_stream, abfd, sym_rec_stream))
1212     {
1213       einfo (_("%P: warning: cannot populate publics stream "
1214                "in PDB file: %E\n"));
1215       goto end;
1216     }
1217
1218   if (!populate_info_stream (pdb, info_stream, guid))
1219     {
1220       einfo (_("%P: warning: cannot populate info stream "
1221                "in PDB file: %E\n"));
1222       goto end;
1223     }
1224
1225   ret = true;
1226
1227 end:
1228   bfd_close (pdb);
1229
1230   return ret;
1231 }
This page took 0.087037 seconds and 4 git commands to generate.