ld: Add minimal pdb generation
[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 /* Stream 4 is the debug information (DBI) stream. */
387 static bool
388 populate_dbi_stream (bfd *stream, bfd *abfd)
389 {
390 struct pdb_dbi_stream_header h;
391 struct optional_dbg_header opt;
392
393 bfd_putl32 (0xffffffff, &h.version_signature);
394 bfd_putl32 (DBI_STREAM_VERSION_70, &h.version_header);
395 bfd_putl32 (1, &h.age);
396 bfd_putl16 (0xffff, &h.global_stream_index);
397 bfd_putl16 (0x8e1d, &h.build_number); // MSVC 14.29
398 bfd_putl16 (0xffff, &h.public_stream_index);
399 bfd_putl16 (0, &h.pdb_dll_version);
400 bfd_putl16 (0xffff, &h.sym_record_stream);
401 bfd_putl16 (0, &h.pdb_dll_rbld);
402 bfd_putl32 (0, &h.mod_info_size);
403 bfd_putl32 (0, &h.section_contribution_size);
404 bfd_putl32 (0, &h.section_map_size);
405 bfd_putl32 (0, &h.source_info_size);
406 bfd_putl32 (0, &h.type_server_map_size);
407 bfd_putl32 (0, &h.mfc_type_server_index);
408 bfd_putl32 (sizeof (opt), &h.optional_dbg_header_size);
409 bfd_putl32 (0, &h.ec_substream_size);
410 bfd_putl16 (0, &h.flags);
411 bfd_putl16 (get_arch_number (abfd), &h.machine);
412 bfd_putl32 (0, &h.padding);
413
414 if (bfd_bwrite (&h, sizeof (h), stream) != sizeof (h))
415 return false;
416
417 bfd_putl16 (0xffff, &opt.fpo_stream);
418 bfd_putl16 (0xffff, &opt.exception_stream);
419 bfd_putl16 (0xffff, &opt.fixup_stream);
420 bfd_putl16 (0xffff, &opt.omap_to_src_stream);
421 bfd_putl16 (0xffff, &opt.omap_from_src_stream);
422 bfd_putl16 (0xffff, &opt.section_header_stream);
423 bfd_putl16 (0xffff, &opt.token_map_stream);
424 bfd_putl16 (0xffff, &opt.xdata_stream);
425 bfd_putl16 (0xffff, &opt.pdata_stream);
426 bfd_putl16 (0xffff, &opt.new_fpo_stream);
427 bfd_putl16 (0xffff, &opt.orig_section_header_stream);
428
429 if (bfd_bwrite (&opt, sizeof (opt), stream) != sizeof (opt))
430 return false;
431
432 return true;
433 }
434
435 /* Create a PDB debugging file for the PE image file abfd with the build ID
436 guid, stored at pdb_name. */
437 bool
438 create_pdb_file (bfd *abfd, const char *pdb_name, const unsigned char *guid)
439 {
440 bfd *pdb;
441 bool ret = false;
442 bfd *info_stream, *dbi_stream, *names_stream;
443
444 pdb = bfd_openw (pdb_name, "pdb");
445 if (!pdb)
446 {
447 einfo (_("%P: warning: cannot create PDB file: %s\n"),
448 bfd_errmsg (bfd_get_error ()));
449 return false;
450 }
451
452 bfd_set_format (pdb, bfd_archive);
453
454 if (!create_old_directory_stream (pdb))
455 {
456 einfo (_("%P: warning: cannot create old directory stream "
457 "in PDB file: %s\n"), bfd_errmsg (bfd_get_error ()));
458 goto end;
459 }
460
461 info_stream = add_stream (pdb, NULL, NULL);
462
463 if (!info_stream)
464 {
465 einfo (_("%P: warning: cannot create info stream "
466 "in PDB file: %s\n"), bfd_errmsg (bfd_get_error ()));
467 goto end;
468 }
469
470 if (!create_type_stream (pdb))
471 {
472 einfo (_("%P: warning: cannot create TPI stream "
473 "in PDB file: %s\n"), bfd_errmsg (bfd_get_error ()));
474 goto end;
475 }
476
477 dbi_stream = add_stream (pdb, NULL, NULL);
478
479 if (!dbi_stream)
480 {
481 einfo (_("%P: warning: cannot create DBI stream "
482 "in PDB file: %s\n"), bfd_errmsg (bfd_get_error ()));
483 goto end;
484 }
485
486 if (!create_type_stream (pdb))
487 {
488 einfo (_("%P: warning: cannot create IPI stream "
489 "in PDB file: %s\n"), bfd_errmsg (bfd_get_error ()));
490 goto end;
491 }
492
493 names_stream = add_stream (pdb, "/names", NULL);
494
495 if (!names_stream)
496 {
497 einfo (_("%P: warning: cannot create /names stream "
498 "in PDB file: %s\n"), bfd_errmsg (bfd_get_error ()));
499 goto end;
500 }
501
502 if (!populate_dbi_stream (dbi_stream, abfd))
503 {
504 einfo (_("%P: warning: cannot populate DBI stream "
505 "in PDB file: %s\n"), bfd_errmsg (bfd_get_error ()));
506 goto end;
507 }
508
509 if (!populate_info_stream (pdb, info_stream, guid))
510 {
511 einfo (_("%P: warning: cannot populate info stream "
512 "in PDB file: %s\n"), bfd_errmsg (bfd_get_error ()));
513 goto end;
514 }
515
516 ret = true;
517
518 end:
519 bfd_close (pdb);
520
521 return ret;
522 }