ld: Write types into IPI stream of PDB
[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 struct type_entry
75 {
76 struct type_entry *next;
77 uint32_t index;
78 uint32_t cv_hash;
79 uint8_t data[];
80 };
81
82 struct types
83 {
84 htab_t hashmap;
85 uint32_t num_types;
86 struct type_entry *first;
87 struct type_entry *last;
88 };
89
90 static const uint32_t crc_table[] =
91 {
92 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f,
93 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988,
94 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2,
95 0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7,
96 0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9,
97 0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172,
98 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, 0x42b2986c,
99 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59,
100 0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423,
101 0xcfba9599, 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924,
102 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, 0x01db7106,
103 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433,
104 0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d,
105 0x91646c97, 0xe6635c01, 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e,
106 0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950,
107 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65,
108 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7,
109 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0,
110 0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa,
111 0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
112 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81,
113 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a,
114 0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683, 0xe3630b12, 0x94643b84,
115 0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1,
116 0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb,
117 0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc,
118 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 0xd6d6a3e8, 0xa1d1937e,
119 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b,
120 0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55,
121 0x316e8eef, 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236,
122 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, 0xb2bd0b28,
123 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d,
124 0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f,
125 0x72076785, 0x05005713, 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38,
126 0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242,
127 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777,
128 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69,
129 0x616bffd3, 0x166ccf45, 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2,
130 0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc,
131 0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
132 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693,
133 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94,
134 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d
135 };
136
137 /* Add a new stream to the PDB archive, and return its BFD. */
138 static bfd *
139 add_stream (bfd *pdb, const char *name, uint16_t *stream_num)
140 {
141 bfd *stream;
142 uint16_t num;
143
144 stream = bfd_create (name ? name : "", pdb);
145 if (!stream)
146 return NULL;
147
148 if (!bfd_make_writable (stream))
149 {
150 bfd_close (stream);
151 return false;
152 }
153
154 if (!pdb->archive_head)
155 {
156 bfd_set_archive_head (pdb, stream);
157 num = 0;
158 }
159 else
160 {
161 bfd *b = pdb->archive_head;
162
163 num = 1;
164
165 while (b->archive_next)
166 {
167 num++;
168 b = b->archive_next;
169 }
170
171 b->archive_next = stream;
172 }
173
174 if (stream_num)
175 *stream_num = num;
176
177 return stream;
178 }
179
180 /* Stream 0 ought to be a copy of the MSF directory from the last
181 time the PDB file was written. Because we don't do incremental
182 writes this isn't applicable to us, but we fill it with a dummy
183 value so as not to confuse radare. */
184 static bool
185 create_old_directory_stream (bfd *pdb)
186 {
187 bfd *stream;
188 char buf[sizeof (uint32_t)];
189
190 stream = add_stream (pdb, NULL, NULL);
191 if (!stream)
192 return false;
193
194 bfd_putl32 (0, buf);
195
196 return bfd_bwrite (buf, sizeof (uint32_t), stream) == sizeof (uint32_t);
197 }
198
199 /* Calculate the hash of a given string. */
200 static uint32_t
201 calc_hash (const char *data, size_t len)
202 {
203 uint32_t hash = 0;
204
205 while (len >= 4)
206 {
207 hash ^= data[0];
208 hash ^= data[1] << 8;
209 hash ^= data[2] << 16;
210 hash ^= data[3] << 24;
211
212 data += 4;
213 len -= 4;
214 }
215
216 if (len >= 2)
217 {
218 hash ^= data[0];
219 hash ^= data[1] << 8;
220
221 data += 2;
222 len -= 2;
223 }
224
225 if (len != 0)
226 hash ^= *data;
227
228 hash |= 0x20202020;
229 hash ^= (hash >> 11);
230
231 return hash ^ (hash >> 16);
232 }
233
234 /* Stream 1 is the PDB info stream - see
235 https://llvm.org/docs/PDB/PdbStream.html. */
236 static bool
237 populate_info_stream (bfd *pdb, bfd *info_stream, const unsigned char *guid)
238 {
239 bool ret = false;
240 struct pdb_stream_70 h;
241 uint32_t num_entries, num_buckets;
242 uint32_t names_length, stream_num;
243 char int_buf[sizeof (uint32_t)];
244
245 struct hash_entry
246 {
247 uint32_t offset;
248 uint32_t value;
249 };
250
251 struct hash_entry **buckets = NULL;
252
253 /* Write header. */
254
255 bfd_putl32 (PDB_STREAM_VERSION_VC70, &h.version);
256 bfd_putl32 (time (NULL), &h.signature);
257 bfd_putl32 (1, &h.age);
258
259 bfd_putl32 (bfd_getb32 (guid), h.guid);
260 bfd_putl16 (bfd_getb16 (&guid[4]), &h.guid[4]);
261 bfd_putl16 (bfd_getb16 (&guid[6]), &h.guid[6]);
262 memcpy (&h.guid[8], &guid[8], 8);
263
264 if (bfd_bwrite (&h, sizeof (h), info_stream) != sizeof (h))
265 return false;
266
267 /* Write hash list of named streams. This is a "rollover" hash, i.e.
268 if a bucket is filled an entry gets placed in the next free
269 slot. */
270
271 num_entries = 0;
272 for (bfd *b = pdb->archive_head; b; b = b->archive_next)
273 {
274 if (strcmp (b->filename, ""))
275 num_entries++;
276 }
277
278 num_buckets = num_entries * 2;
279
280 names_length = 0;
281 stream_num = 0;
282
283 if (num_buckets > 0)
284 {
285 buckets = xmalloc (sizeof (struct hash_entry *) * num_buckets);
286 memset (buckets, 0, sizeof (struct hash_entry *) * num_buckets);
287
288 for (bfd *b = pdb->archive_head; b; b = b->archive_next)
289 {
290 if (strcmp (b->filename, ""))
291 {
292 size_t len = strlen (b->filename);
293 uint32_t hash = (uint16_t) calc_hash (b->filename, len);
294 uint32_t bucket_num = hash % num_buckets;
295
296 while (buckets[bucket_num])
297 {
298 bucket_num++;
299
300 if (bucket_num == num_buckets)
301 bucket_num = 0;
302 }
303
304 buckets[bucket_num] = xmalloc (sizeof (struct hash_entry));
305
306 buckets[bucket_num]->offset = names_length;
307 buckets[bucket_num]->value = stream_num;
308
309 names_length += len + 1;
310 }
311
312 stream_num++;
313 }
314 }
315
316 /* Write the strings list - the hash keys are indexes into this. */
317
318 bfd_putl32 (names_length, int_buf);
319
320 if (bfd_bwrite (int_buf, sizeof (uint32_t), info_stream) !=
321 sizeof (uint32_t))
322 goto end;
323
324 for (bfd *b = pdb->archive_head; b; b = b->archive_next)
325 {
326 if (!strcmp (b->filename, ""))
327 continue;
328
329 size_t len = strlen (b->filename) + 1;
330
331 if (bfd_bwrite (b->filename, len, info_stream) != len)
332 goto end;
333 }
334
335 /* Write the number of entries and buckets. */
336
337 bfd_putl32 (num_entries, int_buf);
338
339 if (bfd_bwrite (int_buf, sizeof (uint32_t), info_stream) !=
340 sizeof (uint32_t))
341 goto end;
342
343 bfd_putl32 (num_buckets, int_buf);
344
345 if (bfd_bwrite (int_buf, sizeof (uint32_t), info_stream) !=
346 sizeof (uint32_t))
347 goto end;
348
349 /* Write the present bitmap. */
350
351 bfd_putl32 ((num_buckets + 31) / 32, int_buf);
352
353 if (bfd_bwrite (int_buf, sizeof (uint32_t), info_stream) !=
354 sizeof (uint32_t))
355 goto end;
356
357 for (unsigned int i = 0; i < num_buckets; i += 32)
358 {
359 uint32_t v = 0;
360
361 for (unsigned int j = 0; j < 32; j++)
362 {
363 if (i + j >= num_buckets)
364 break;
365
366 if (buckets[i + j])
367 v |= 1 << j;
368 }
369
370 bfd_putl32 (v, int_buf);
371
372 if (bfd_bwrite (int_buf, sizeof (uint32_t), info_stream) !=
373 sizeof (uint32_t))
374 goto end;
375 }
376
377 /* Write the (empty) deleted bitmap. */
378
379 bfd_putl32 (0, int_buf);
380
381 if (bfd_bwrite (int_buf, sizeof (uint32_t), info_stream) !=
382 sizeof (uint32_t))
383 goto end;
384
385 /* Write the buckets. */
386
387 for (unsigned int i = 0; i < num_buckets; i++)
388 {
389 if (buckets[i])
390 {
391 bfd_putl32 (buckets[i]->offset, int_buf);
392
393 if (bfd_bwrite (int_buf, sizeof (uint32_t), info_stream) !=
394 sizeof (uint32_t))
395 goto end;
396
397 bfd_putl32 (buckets[i]->value, int_buf);
398
399 if (bfd_bwrite (int_buf, sizeof (uint32_t), info_stream) !=
400 sizeof (uint32_t))
401 goto end;
402 }
403 }
404
405 bfd_putl32 (0, int_buf);
406
407 if (bfd_bwrite (int_buf, sizeof (uint32_t), info_stream) !=
408 sizeof (uint32_t))
409 goto end;
410
411 bfd_putl32 (PDB_STREAM_VERSION_VC140, int_buf);
412
413 if (bfd_bwrite (int_buf, sizeof (uint32_t), info_stream) !=
414 sizeof (uint32_t))
415 goto end;
416
417 ret = true;
418
419 end:
420 for (unsigned int i = 0; i < num_buckets; i++)
421 {
422 if (buckets[i])
423 free (buckets[i]);
424 }
425
426 free (buckets);
427
428 return ret;
429 }
430
431 /* Calculate the CRC32 used for type hashes. */
432 static uint32_t
433 crc32 (const uint8_t *data, size_t len)
434 {
435 uint32_t crc = 0;
436
437 while (len > 0)
438 {
439 crc = (crc >> 8) ^ crc_table[(crc & 0xff) ^ *data];
440
441 data++;
442 len--;
443 }
444
445 return crc;
446 }
447
448 /* Stream 2 is the type information (TPI) stream, and stream 4 is
449 the ID information (IPI) stream. They differ only in which records
450 go in which stream. */
451 static bool
452 populate_type_stream (bfd *pdb, bfd *stream, struct types *types)
453 {
454 struct pdb_tpi_stream_header h;
455 struct type_entry *e;
456 uint32_t len = 0, index_offset_len, off;
457 struct bfd *hash_stream = NULL;
458 uint16_t hash_stream_index;
459
460 static const uint32_t index_skip = 0x2000;
461
462 e = types->first;
463
464 index_offset_len = 0;
465
466 while (e)
467 {
468 uint32_t old_len = len;
469
470 len += sizeof (uint16_t) + bfd_getl16 (e->data);
471
472 if (old_len == 0 || old_len / index_skip != len / index_skip)
473 index_offset_len += sizeof (uint32_t) * 2;
474
475 e = e->next;
476 }
477
478 /* Each type stream also has a stream which holds the hash value for each
479 type, along with a skip list to speed up searching. */
480
481 hash_stream = add_stream (pdb, "", &hash_stream_index);
482
483 if (!hash_stream)
484 return false;
485
486 bfd_putl32 (TPI_STREAM_VERSION_80, &h.version);
487 bfd_putl32 (sizeof (h), &h.header_size);
488 bfd_putl32 (TPI_FIRST_INDEX, &h.type_index_begin);
489 bfd_putl32 (TPI_FIRST_INDEX + types->num_types, &h.type_index_end);
490 bfd_putl32 (len, &h.type_record_bytes);
491 bfd_putl16 (hash_stream_index, &h.hash_stream_index);
492 bfd_putl16 (0xffff, &h.hash_aux_stream_index);
493 bfd_putl32 (sizeof (uint32_t), &h.hash_key_size);
494 bfd_putl32 (NUM_TPI_HASH_BUCKETS, &h.num_hash_buckets);
495 bfd_putl32 (0, &h.hash_value_buffer_offset);
496 bfd_putl32 (types->num_types * sizeof (uint32_t),
497 &h.hash_value_buffer_length);
498 bfd_putl32 (types->num_types * sizeof (uint32_t),
499 &h.index_offset_buffer_offset);
500 bfd_putl32 (index_offset_len, &h.index_offset_buffer_length);
501 bfd_putl32 ((types->num_types * sizeof (uint32_t)) + index_offset_len,
502 &h.hash_adj_buffer_offset);
503 bfd_putl32 (0, &h.hash_adj_buffer_length);
504
505 if (bfd_bwrite (&h, sizeof (h), stream) != sizeof (h))
506 return false;
507
508 /* Write the type definitions into the main stream, and the hashes
509 into the hash stream. The hashes have already been calculated
510 in handle_type. */
511
512 e = types->first;
513
514 while (e)
515 {
516 uint8_t buf[sizeof (uint32_t)];
517 uint16_t size;
518
519 size = bfd_getl16 (e->data);
520
521 if (bfd_bwrite (e->data, size + sizeof (uint16_t), stream)
522 != size + sizeof (uint16_t))
523 return false;
524
525 bfd_putl32 (e->cv_hash % NUM_TPI_HASH_BUCKETS, buf);
526
527 if (bfd_bwrite (buf, sizeof (uint32_t), hash_stream)
528 != sizeof (uint32_t))
529 return false;
530
531 e = e->next;
532 }
533
534 /* Write the index offsets, i.e. the skip list, into the hash stream. We
535 copy MSVC here by writing a new entry for every 8192 bytes. */
536
537 e = types->first;
538 off = 0;
539
540 while (e)
541 {
542 uint32_t old_off = off;
543 uint16_t size = bfd_getl16 (e->data);
544
545 off += size + sizeof (uint16_t);
546
547 if (old_off == 0 || old_off / index_skip != len / index_skip)
548 {
549 uint8_t buf[sizeof (uint32_t)];
550
551 bfd_putl32 (TPI_FIRST_INDEX + e->index, buf);
552
553 if (bfd_bwrite (buf, sizeof (uint32_t), hash_stream)
554 != sizeof (uint32_t))
555 return false;
556
557 bfd_putl32 (old_off, buf);
558
559 if (bfd_bwrite (buf, sizeof (uint32_t), hash_stream)
560 != sizeof (uint32_t))
561 return false;
562 }
563
564 e = e->next;
565 }
566
567 return true;
568 }
569
570 /* Return the PE architecture number for the image. */
571 static uint16_t
572 get_arch_number (bfd *abfd)
573 {
574 if (abfd->arch_info->arch != bfd_arch_i386)
575 return 0;
576
577 if (abfd->arch_info->mach & bfd_mach_x86_64)
578 return IMAGE_FILE_MACHINE_AMD64;
579
580 return IMAGE_FILE_MACHINE_I386;
581 }
582
583 /* Validate the DEBUG_S_FILECHKSMS entry within a module's .debug$S
584 section, and copy it to the module's symbol stream. */
585 static bool
586 copy_filechksms (uint8_t *data, uint32_t size, char *string_table,
587 struct string_table *strings, uint8_t *out,
588 struct mod_source_files *mod_source)
589 {
590 uint8_t *orig_data = data;
591 uint32_t orig_size = size;
592 uint16_t num_files = 0;
593 struct string **strptr;
594
595 bfd_putl32 (DEBUG_S_FILECHKSMS, out);
596 out += sizeof (uint32_t);
597
598 bfd_putl32 (size, out);
599 out += sizeof (uint32_t);
600
601 /* Calculate the number of files, and check for any overflows. */
602
603 while (size > 0)
604 {
605 struct file_checksum *fc = (struct file_checksum *) data;
606 uint8_t padding;
607 size_t len;
608
609 if (size < sizeof (struct file_checksum))
610 {
611 bfd_set_error (bfd_error_bad_value);
612 return false;
613 }
614
615 len = sizeof (struct file_checksum) + fc->checksum_length;
616
617 if (size < len)
618 {
619 bfd_set_error (bfd_error_bad_value);
620 return false;
621 }
622
623 data += len;
624 size -= len;
625
626 if (len % sizeof (uint32_t))
627 padding = sizeof (uint32_t) - (len % sizeof (uint32_t));
628 else
629 padding = 0;
630
631 if (size < padding)
632 {
633 bfd_set_error (bfd_error_bad_value);
634 return false;
635 }
636
637 num_files++;
638
639 data += padding;
640 size -= padding;
641 }
642
643 /* Add the files to mod_source, so that they'll appear in the source
644 info substream. */
645
646 strptr = NULL;
647 if (num_files > 0)
648 {
649 uint16_t new_count = num_files + mod_source->files_count;
650
651 mod_source->files = xrealloc (mod_source->files,
652 sizeof (struct string *) * new_count);
653
654 strptr = mod_source->files + mod_source->files_count;
655
656 mod_source->files_count += num_files;
657 }
658
659 /* Actually copy the data. */
660
661 data = orig_data;
662 size = orig_size;
663
664 while (size > 0)
665 {
666 struct file_checksum *fc = (struct file_checksum *) data;
667 uint32_t string_off;
668 uint8_t padding;
669 size_t len;
670 struct string *str = NULL;
671
672 string_off = bfd_getl32 (&fc->file_id);
673 len = sizeof (struct file_checksum) + fc->checksum_length;
674
675 if (len % sizeof (uint32_t))
676 padding = sizeof (uint32_t) - (len % sizeof (uint32_t));
677 else
678 padding = 0;
679
680 /* Remap the "file ID", i.e. the offset in the module's string table,
681 so it points to the right place in the main string table. */
682
683 if (string_table)
684 {
685 char *fn = string_table + string_off;
686 size_t fn_len = strlen (fn);
687 uint32_t hash = calc_hash (fn, fn_len);
688 void **slot;
689
690 slot = htab_find_slot_with_hash (strings->hashmap, fn, hash,
691 NO_INSERT);
692
693 if (slot)
694 str = (struct string *) *slot;
695 }
696
697 *strptr = str;
698 strptr++;
699
700 bfd_putl32 (str ? str->offset : 0, &fc->file_id);
701
702 memcpy (out, data, len + padding);
703
704 data += len + padding;
705 size -= len + padding;
706 out += len + padding;
707 }
708
709 return true;
710 }
711
712 /* Add a string to the strings table, if it's not already there. */
713 static void
714 add_string (char *str, size_t len, struct string_table *strings)
715 {
716 uint32_t hash = calc_hash (str, len);
717 void **slot;
718
719 slot = htab_find_slot_with_hash (strings->hashmap, str, hash, INSERT);
720
721 if (!*slot)
722 {
723 struct string *s;
724
725 *slot = xmalloc (offsetof (struct string, s) + len);
726
727 s = (struct string *) *slot;
728
729 s->next = NULL;
730 s->hash = hash;
731 s->offset = strings->strings_len;
732 s->source_file_offset = 0xffffffff;
733 s->len = len;
734 memcpy (s->s, str, len);
735
736 if (strings->strings_tail)
737 strings->strings_tail->next = s;
738 else
739 strings->strings_head = s;
740
741 strings->strings_tail = s;
742
743 strings->strings_len += len + 1;
744 }
745 }
746
747 /* Return the hash of an entry in the string table. */
748 static hashval_t
749 hash_string_table_entry (const void *p)
750 {
751 const struct string *s = (const struct string *) p;
752
753 return s->hash;
754 }
755
756 /* Compare an entry in the string table with a string. */
757 static int
758 eq_string_table_entry (const void *a, const void *b)
759 {
760 const struct string *s1 = (const struct string *) a;
761 const char *s2 = (const char *) b;
762 size_t s2_len = strlen (s2);
763
764 if (s2_len != s1->len)
765 return 0;
766
767 return memcmp (s1->s, s2, s2_len) == 0;
768 }
769
770 /* Parse the string table within the .debug$S section. */
771 static void
772 parse_string_table (bfd_byte *data, size_t size,
773 struct string_table *strings)
774 {
775 while (true)
776 {
777 size_t len = strnlen ((char *) data, size);
778
779 add_string ((char *) data, len, strings);
780
781 data += len + 1;
782
783 if (size <= len + 1)
784 break;
785
786 size -= len + 1;
787 }
788 }
789
790 /* Return the size of an extended value parameter, as used in
791 LF_ENUMERATE etc. */
792 static unsigned int
793 extended_value_len (uint16_t type)
794 {
795 switch (type)
796 {
797 case LF_CHAR:
798 return 1;
799
800 case LF_SHORT:
801 case LF_USHORT:
802 return 2;
803
804 case LF_LONG:
805 case LF_ULONG:
806 return 4;
807
808 case LF_QUADWORD:
809 case LF_UQUADWORD:
810 return 8;
811 }
812
813 return 0;
814 }
815
816 /* Parse the .debug$S section within an object file. */
817 static bool
818 handle_debugs_section (asection *s, bfd *mod, struct string_table *strings,
819 uint8_t **dataptr, uint32_t *sizeptr,
820 struct mod_source_files *mod_source,
821 bfd *abfd)
822 {
823 bfd_byte *data = NULL;
824 size_t off;
825 uint32_t c13_size = 0;
826 char *string_table = NULL;
827 uint8_t *buf, *bufptr;
828
829 if (!bfd_get_full_section_contents (mod, s, &data))
830 return false;
831
832 if (!data)
833 return false;
834
835 /* Resolve relocations. Addresses are stored within the .debug$S section as
836 a .secidx, .secrel32 pair. */
837
838 if (s->flags & SEC_RELOC)
839 {
840 struct internal_reloc *relocs;
841 struct internal_syment *symbols;
842 asection **sectlist;
843 unsigned int syment_count;
844 int sect_num;
845 struct external_syment *ext;
846
847 syment_count = obj_raw_syment_count (mod);
848
849 relocs =
850 _bfd_coff_read_internal_relocs (mod, s, false, NULL, true, NULL);
851
852 symbols = xmalloc (sizeof (struct internal_syment) * syment_count);
853 sectlist = xmalloc (sizeof (asection *) * syment_count);
854
855 ext = (struct external_syment *) (coff_data (mod)->external_syms);
856
857 for (unsigned int i = 0; i < syment_count; i++)
858 {
859 bfd_coff_swap_sym_in (mod, &ext[i], &symbols[i]);
860 }
861
862 sect_num = 1;
863
864 for (asection *sect = mod->sections; sect; sect = sect->next)
865 {
866 for (unsigned int i = 0; i < syment_count; i++)
867 {
868 if (symbols[i].n_scnum == sect_num)
869 sectlist[i] = sect;
870 }
871
872 sect_num++;
873 }
874
875 if (!bfd_coff_relocate_section (abfd, coff_data (abfd)->link_info, mod,
876 s, data, relocs, symbols, sectlist))
877 {
878 free (sectlist);
879 free (symbols);
880 free (data);
881 return false;
882 }
883
884 free (sectlist);
885 free (symbols);
886 }
887
888 if (bfd_getl32 (data) != CV_SIGNATURE_C13)
889 {
890 free (data);
891 return true;
892 }
893
894 off = sizeof (uint32_t);
895
896 /* calculate size */
897
898 while (off + sizeof (uint32_t) <= s->size)
899 {
900 uint32_t type, size;
901
902 type = bfd_getl32 (data + off);
903
904 off += sizeof (uint32_t);
905
906 if (off + sizeof (uint32_t) > s->size)
907 {
908 free (data);
909 bfd_set_error (bfd_error_bad_value);
910 return false;
911 }
912
913 size = bfd_getl32 (data + off);
914
915 off += sizeof (uint32_t);
916
917 if (off + size > s->size)
918 {
919 free (data);
920 bfd_set_error (bfd_error_bad_value);
921 return false;
922 }
923
924 switch (type)
925 {
926 case DEBUG_S_FILECHKSMS:
927 c13_size += sizeof (uint32_t) + sizeof (uint32_t) + size;
928
929 if (c13_size % sizeof (uint32_t))
930 c13_size += sizeof (uint32_t) - (c13_size % sizeof (uint32_t));
931
932 break;
933
934 case DEBUG_S_STRINGTABLE:
935 parse_string_table (data + off, size, strings);
936
937 string_table = (char *) data + off;
938
939 break;
940
941 case DEBUG_S_LINES:
942 {
943 uint16_t sect;
944
945 if (size < sizeof (uint32_t) + sizeof (uint16_t))
946 {
947 free (data);
948 bfd_set_error (bfd_error_bad_value);
949 return false;
950 }
951
952 sect = bfd_getl16 (data + off + sizeof (uint32_t));
953
954 /* Skip GC'd symbols. */
955 if (sect != 0)
956 {
957 c13_size += sizeof (uint32_t) + sizeof (uint32_t) + size;
958
959 if (c13_size % sizeof (uint32_t))
960 c13_size +=
961 sizeof (uint32_t) - (c13_size % sizeof (uint32_t));
962 }
963
964 break;
965 }
966 }
967
968 off += size;
969
970 if (off % sizeof (uint32_t))
971 off += sizeof (uint32_t) - (off % sizeof (uint32_t));
972 }
973
974 if (c13_size == 0)
975 {
976 free (data);
977 return true;
978 }
979
980 /* copy data */
981
982 buf = xmalloc (c13_size);
983 bufptr = buf;
984
985 off = sizeof (uint32_t);
986
987 while (off + sizeof (uint32_t) <= s->size)
988 {
989 uint32_t type, size;
990
991 type = bfd_getl32 (data + off);
992 off += sizeof (uint32_t);
993
994 size = bfd_getl32 (data + off);
995 off += sizeof (uint32_t);
996
997 switch (type)
998 {
999 case DEBUG_S_FILECHKSMS:
1000 if (!copy_filechksms (data + off, size, string_table,
1001 strings, bufptr, mod_source))
1002 {
1003 free (data);
1004 return false;
1005 }
1006
1007 bufptr += sizeof (uint32_t) + sizeof (uint32_t) + size;
1008
1009 break;
1010
1011 case DEBUG_S_LINES:
1012 {
1013 uint16_t sect;
1014
1015 sect = bfd_getl16 (data + off + sizeof (uint32_t));
1016
1017 /* Skip if GC'd. */
1018 if (sect != 0)
1019 {
1020 bfd_putl32 (type, bufptr);
1021 bufptr += sizeof (uint32_t);
1022
1023 bfd_putl32 (size, bufptr);
1024 bufptr += sizeof (uint32_t);
1025
1026 memcpy (bufptr, data + off, size);
1027 bufptr += size;
1028 }
1029
1030 break;
1031 }
1032 }
1033
1034 off += size;
1035
1036 if (off % sizeof (uint32_t))
1037 off += sizeof (uint32_t) - (off % sizeof (uint32_t));
1038 }
1039
1040 free (data);
1041
1042 if (*dataptr)
1043 {
1044 /* Append the C13 info to what's already there, if the module has
1045 multiple .debug$S sections. */
1046
1047 *dataptr = xrealloc (*dataptr, *sizeptr + c13_size);
1048 memcpy (*dataptr + *sizeptr, buf, c13_size);
1049
1050 free (buf);
1051 }
1052 else
1053 {
1054 *dataptr = buf;
1055 }
1056
1057 *sizeptr += c13_size;
1058
1059 return true;
1060 }
1061
1062 /* Remap the type number stored in data from the per-module numbering to
1063 that of the deduplicated output list. */
1064 static bool
1065 remap_type (void *data, struct type_entry **map,
1066 uint32_t type_num, uint32_t num_types)
1067 {
1068 uint32_t type = bfd_getl32 (data);
1069
1070 /* Ignore builtin types (those with IDs below 0x1000). */
1071 if (type < TPI_FIRST_INDEX)
1072 return true;
1073
1074 if (type >= TPI_FIRST_INDEX + type_num)
1075 {
1076 einfo (_("%P: CodeView type %v references other type %v not yet "
1077 "declared\n"), TPI_FIRST_INDEX + type_num, type);
1078 return false;
1079 }
1080
1081 if (type >= TPI_FIRST_INDEX + num_types)
1082 {
1083 einfo (_("%P: CodeView type %v references out of range type %v\n"),
1084 TPI_FIRST_INDEX + type_num, type);
1085 return false;
1086 }
1087
1088 type = TPI_FIRST_INDEX + map[type - TPI_FIRST_INDEX]->index;
1089 bfd_putl32 (type, data);
1090
1091 return true;
1092 }
1093
1094 /* Determines whether the name of a struct, class, or union counts as
1095 "anonymous". Non-anonymous types have a hash based on just the name,
1096 rather than the whole structure. */
1097 static bool
1098 is_name_anonymous (char *name, size_t len)
1099 {
1100 static const char tag1[] = "<unnamed-tag>";
1101 static const char tag2[] = "__unnamed";
1102 static const char tag3[] = "::<unnamed-tag>";
1103 static const char tag4[] = "::__unnamed";
1104
1105 if (len == sizeof (tag1) - 1 && !memcmp (name, tag1, sizeof (tag1) - 1))
1106 return true;
1107
1108 if (len == sizeof (tag2) - 1 && !memcmp (name, tag2, sizeof (tag2) - 1))
1109 return true;
1110
1111 if (len >= sizeof (tag3) - 1
1112 && !memcmp (name + len - sizeof (tag3) + 1, tag3, sizeof (tag3) - 1))
1113 return true;
1114
1115 if (len >= sizeof (tag4) - 1
1116 && !memcmp (name + len - sizeof (tag4) + 1, tag4, sizeof (tag4) - 1))
1117 return true;
1118
1119 return false;
1120 }
1121
1122 /* Parse a type definition in the .debug$T section. We remap the numbers
1123 of any referenced types, and if the type is not a duplicate of one
1124 already seen add it to types (for TPI types) or ids (for IPI types). */
1125 static bool
1126 handle_type (uint8_t *data, struct type_entry **map, uint32_t type_num,
1127 uint32_t num_types, struct types *types,
1128 struct types *ids)
1129 {
1130 uint16_t size, type;
1131 void **slot;
1132 hashval_t hash;
1133 bool other_hash = false;
1134 uint32_t cv_hash;
1135 struct types *t;
1136 bool ipi = false;
1137
1138 size = bfd_getl16 (data) + sizeof (uint16_t);
1139 type = bfd_getl16 (data + sizeof (uint16_t));
1140
1141 switch (type)
1142 {
1143 case LF_MODIFIER:
1144 {
1145 struct lf_modifier *mod = (struct lf_modifier *) data;
1146
1147 if (size < offsetof (struct lf_modifier, modifier))
1148 {
1149 einfo (_("%P: warning: truncated CodeView type record "
1150 "LF_MODIFIER\n"));
1151 return false;
1152 }
1153
1154 if (!remap_type (&mod->base_type, map, type_num, num_types))
1155 return false;
1156
1157 break;
1158 }
1159
1160 case LF_POINTER:
1161 {
1162 struct lf_pointer *ptr = (struct lf_pointer *) data;
1163
1164 if (size < offsetof (struct lf_pointer, attributes))
1165 {
1166 einfo (_("%P: warning: truncated CodeView type record"
1167 " LF_POINTER\n"));
1168 return false;
1169 }
1170
1171 if (!remap_type (&ptr->base_type, map, type_num, num_types))
1172 return false;
1173
1174 break;
1175 }
1176
1177 case LF_PROCEDURE:
1178 {
1179 struct lf_procedure *proc = (struct lf_procedure *) data;
1180
1181 if (size < sizeof (struct lf_procedure))
1182 {
1183 einfo (_("%P: warning: truncated CodeView type record"
1184 " LF_PROCEDURE\n"));
1185 return false;
1186 }
1187
1188 if (!remap_type (&proc->return_type, map, type_num, num_types))
1189 return false;
1190
1191 if (!remap_type (&proc->arglist, map, type_num, num_types))
1192 return false;
1193
1194 break;
1195 }
1196
1197 case LF_MFUNCTION:
1198 {
1199 struct lf_mfunction *func = (struct lf_mfunction *) data;
1200
1201 if (size < sizeof (struct lf_procedure))
1202 {
1203 einfo (_("%P: warning: truncated CodeView type record"
1204 " LF_MFUNCTION\n"));
1205 return false;
1206 }
1207
1208 if (!remap_type (&func->return_type, map, type_num, num_types))
1209 return false;
1210
1211 if (!remap_type (&func->containing_class_type, map, type_num,
1212 num_types))
1213 return false;
1214
1215 if (!remap_type (&func->this_type, map, type_num, num_types))
1216 return false;
1217
1218 if (!remap_type (&func->arglist, map, type_num, num_types))
1219 return false;
1220
1221 break;
1222 }
1223
1224 case LF_ARGLIST:
1225 {
1226 uint32_t num_entries;
1227 struct lf_arglist *al = (struct lf_arglist *) data;
1228
1229 if (size < offsetof (struct lf_arglist, args))
1230 {
1231 einfo (_("%P: warning: truncated CodeView type record"
1232 " LF_ARGLIST\n"));
1233 return false;
1234 }
1235
1236 num_entries = bfd_getl32 (&al->num_entries);
1237
1238 if (size < offsetof (struct lf_arglist, args)
1239 + (num_entries * sizeof (uint32_t)))
1240 {
1241 einfo (_("%P: warning: truncated CodeView type record"
1242 " LF_ARGLIST\n"));
1243 return false;
1244 }
1245
1246 for (uint32_t i = 0; i < num_entries; i++)
1247 {
1248 if (!remap_type (&al->args[i], map, type_num, num_types))
1249 return false;
1250 }
1251
1252 break;
1253 }
1254
1255 case LF_FIELDLIST:
1256 {
1257 uint16_t left = size - sizeof (uint16_t) - sizeof (uint16_t);
1258 uint8_t *ptr = data + sizeof (uint16_t) + sizeof (uint16_t);
1259
1260 while (left > 0)
1261 {
1262 uint16_t subtype;
1263
1264 if (left < sizeof (uint16_t))
1265 {
1266 einfo (_("%P: warning: truncated CodeView type record"
1267 " LF_FIELDLIST\n"));
1268 return false;
1269 }
1270
1271 subtype = bfd_getl16 (ptr);
1272
1273 switch (subtype)
1274 {
1275 case LF_MEMBER:
1276 {
1277 struct lf_member *mem = (struct lf_member *) ptr;
1278 size_t name_len, subtype_len;
1279
1280 if (left < offsetof (struct lf_member, name))
1281 {
1282 einfo (_("%P: warning: truncated CodeView type record"
1283 " LF_MEMBER\n"));
1284 return false;
1285 }
1286
1287 if (!remap_type (&mem->type, map, type_num, num_types))
1288 return false;
1289
1290 name_len =
1291 strnlen (mem->name,
1292 left - offsetof (struct lf_member, name));
1293
1294 if (name_len == left - offsetof (struct lf_member, name))
1295 {
1296 einfo (_("%P: warning: name for LF_MEMBER has no"
1297 " terminating zero\n"));
1298 return false;
1299 }
1300
1301 name_len++;
1302
1303 subtype_len = offsetof (struct lf_member, name) + name_len;
1304
1305 if (subtype_len % 4 != 0)
1306 subtype_len += 4 - (subtype_len % 4);
1307
1308 if (left < subtype_len)
1309 {
1310 einfo (_("%P: warning: truncated CodeView type record"
1311 " LF_FIELDLIST\n"));
1312 return false;
1313 }
1314
1315 ptr += subtype_len;
1316 left -= subtype_len;
1317
1318 break;
1319 }
1320
1321 case LF_ENUMERATE:
1322 {
1323 struct lf_enumerate *en = (struct lf_enumerate *) ptr;
1324 size_t name_len, subtype_len;
1325 uint16_t val;
1326
1327 if (left < offsetof (struct lf_enumerate, name))
1328 {
1329 einfo (_("%P: warning: truncated CodeView type record"
1330 " LF_ENUMERATE\n"));
1331 return false;
1332 }
1333
1334 subtype_len = offsetof (struct lf_enumerate, name);
1335
1336 val = bfd_getl16 (&en->value);
1337
1338 /* If val >= 0x8000, the actual value immediately follows. */
1339 if (val >= 0x8000)
1340 {
1341 unsigned int param_len = extended_value_len (val);
1342
1343 if (param_len == 0)
1344 {
1345 einfo (_("%P: warning: unhandled type %v within"
1346 " LF_ENUMERATE\n"), val);
1347 return false;
1348 }
1349
1350 if (left < subtype_len + param_len)
1351 {
1352 einfo (_("%P: warning: truncated CodeView type"
1353 " record LF_ENUMERATE\n"));
1354 return false;
1355 }
1356
1357 subtype_len += param_len;
1358 }
1359
1360 name_len = strnlen ((char *) ptr + subtype_len,
1361 left - subtype_len);
1362
1363 if (name_len == left - offsetof (struct lf_enumerate, name))
1364 {
1365 einfo (_("%P: warning: name for LF_ENUMERATE has no"
1366 " terminating zero\n"));
1367 return false;
1368 }
1369
1370 name_len++;
1371
1372 subtype_len += name_len;
1373
1374 if (subtype_len % 4 != 0)
1375 subtype_len += 4 - (subtype_len % 4);
1376
1377 if (left < subtype_len)
1378 {
1379 einfo (_("%P: warning: truncated CodeView type record"
1380 " LF_ENUMERATE\n"));
1381 return false;
1382 }
1383
1384 ptr += subtype_len;
1385 left -= subtype_len;
1386
1387 break;
1388 }
1389
1390 case LF_INDEX:
1391 {
1392 struct lf_index *ind = (struct lf_index *) ptr;
1393
1394 if (left < sizeof (struct lf_index))
1395 {
1396 einfo (_("%P: warning: truncated CodeView type record"
1397 " LF_INDEX\n"));
1398 return false;
1399 }
1400
1401 if (!remap_type (&ind->index, map, type_num, num_types))
1402 return false;
1403
1404 ptr += sizeof (struct lf_index);
1405 left -= sizeof (struct lf_index);
1406
1407 break;
1408 }
1409
1410 case LF_ONEMETHOD:
1411 {
1412 struct lf_onemethod *meth = (struct lf_onemethod *) ptr;
1413 size_t name_len, subtype_len;
1414
1415 if (left < offsetof (struct lf_onemethod, name))
1416 {
1417 einfo (_("%P: warning: truncated CodeView type record"
1418 " LF_ONEMETHOD\n"));
1419 return false;
1420 }
1421
1422 if (!remap_type (&meth->method_type, map, type_num,
1423 num_types))
1424 return false;
1425
1426 name_len =
1427 strnlen (meth->name,
1428 left - offsetof (struct lf_onemethod, name));
1429
1430 if (name_len == left - offsetof (struct lf_onemethod, name))
1431 {
1432 einfo (_("%P: warning: name for LF_ONEMETHOD has no"
1433 " terminating zero\n"));
1434 return false;
1435 }
1436
1437 name_len++;
1438
1439 subtype_len = offsetof (struct lf_onemethod, name)
1440 + name_len;
1441
1442 if (subtype_len % 4 != 0)
1443 subtype_len += 4 - (subtype_len % 4);
1444
1445 if (left < subtype_len)
1446 {
1447 einfo (_("%P: warning: truncated CodeView type record"
1448 " LF_FIELDLIST\n"));
1449 return false;
1450 }
1451
1452 ptr += subtype_len;
1453 left -= subtype_len;
1454
1455 break;
1456 }
1457
1458 case LF_METHOD:
1459 {
1460 struct lf_method *meth = (struct lf_method *) ptr;
1461 size_t name_len, subtype_len;
1462
1463 if (left < offsetof (struct lf_method, name))
1464 {
1465 einfo (_("%P: warning: truncated CodeView type record"
1466 " LF_METHOD\n"));
1467 return false;
1468 }
1469
1470 if (!remap_type (&meth->method_list, map, type_num,
1471 num_types))
1472 return false;
1473
1474 name_len =
1475 strnlen (meth->name,
1476 left - offsetof (struct lf_method, name));
1477
1478 if (name_len == left - offsetof (struct lf_method, name))
1479 {
1480 einfo (_("%P: warning: name for LF_METHOD has no"
1481 " terminating zero\n"));
1482 return false;
1483 }
1484
1485 name_len++;
1486
1487 subtype_len = offsetof (struct lf_method, name) + name_len;
1488
1489 if (subtype_len % 4 != 0)
1490 subtype_len += 4 - (subtype_len % 4);
1491
1492 if (left < subtype_len)
1493 {
1494 einfo (_("%P: warning: truncated CodeView type record"
1495 " LF_FIELDLIST\n"));
1496 return false;
1497 }
1498
1499 ptr += subtype_len;
1500 left -= subtype_len;
1501
1502 break;
1503 }
1504
1505 case LF_BCLASS:
1506 {
1507 struct lf_bclass *bc = (struct lf_bclass *) ptr;
1508
1509 if (left < sizeof (struct lf_bclass))
1510 {
1511 einfo (_("%P: warning: truncated CodeView type record"
1512 " LF_BCLASS\n"));
1513 return false;
1514 }
1515
1516 if (!remap_type (&bc->base_class_type, map, type_num,
1517 num_types))
1518 return false;
1519
1520 ptr += sizeof (struct lf_bclass);
1521 left -= sizeof (struct lf_bclass);
1522
1523 break;
1524 }
1525
1526 case LF_VFUNCTAB:
1527 {
1528 struct lf_vfunctab *vft = (struct lf_vfunctab *) ptr;
1529
1530 if (left < sizeof (struct lf_vfunctab))
1531 {
1532 einfo (_("%P: warning: truncated CodeView type record"
1533 " LF_VFUNCTAB\n"));
1534 return false;
1535 }
1536
1537 if (!remap_type (&vft->type, map, type_num, num_types))
1538 return false;
1539
1540 ptr += sizeof (struct lf_vfunctab);
1541 left -= sizeof (struct lf_vfunctab);
1542
1543 break;
1544 }
1545
1546 case LF_VBCLASS:
1547 case LF_IVBCLASS:
1548 {
1549 struct lf_vbclass *vbc = (struct lf_vbclass *) ptr;
1550
1551 if (left < sizeof (struct lf_vbclass))
1552 {
1553 einfo (_("%P: warning: truncated CodeView type record"
1554 " LF_VBCLASS/LF_IVBCLASS\n"));
1555 return false;
1556 }
1557
1558 if (!remap_type (&vbc->base_class_type, map, type_num,
1559 num_types))
1560 return false;
1561
1562 if (!remap_type (&vbc->virtual_base_pointer_type, map,
1563 type_num, num_types))
1564 return false;
1565
1566 ptr += sizeof (struct lf_vbclass);
1567 left -= sizeof (struct lf_vbclass);
1568
1569 break;
1570 }
1571
1572 case LF_STMEMBER:
1573 {
1574 struct lf_static_member *st =
1575 (struct lf_static_member *) ptr;
1576 size_t name_len, subtype_len;
1577
1578 if (left < offsetof (struct lf_static_member, name))
1579 {
1580 einfo (_("%P: warning: truncated CodeView type record"
1581 " LF_STMEMBER\n"));
1582 return false;
1583 }
1584
1585 if (!remap_type (&st->type, map, type_num, num_types))
1586 return false;
1587
1588 name_len =
1589 strnlen (st->name,
1590 left - offsetof (struct lf_static_member, name));
1591
1592 if (name_len == left
1593 - offsetof (struct lf_static_member, name))
1594 {
1595 einfo (_("%P: warning: name for LF_STMEMBER has no"
1596 " terminating zero\n"));
1597 return false;
1598 }
1599
1600 name_len++;
1601
1602 subtype_len = offsetof (struct lf_static_member, name)
1603 + name_len;
1604
1605 if (subtype_len % 4 != 0)
1606 subtype_len += 4 - (subtype_len % 4);
1607
1608 if (left < subtype_len)
1609 {
1610 einfo (_("%P: warning: truncated CodeView type record"
1611 " LF_FIELDLIST\n"));
1612 return false;
1613 }
1614
1615 ptr += subtype_len;
1616 left -= subtype_len;
1617
1618 break;
1619 }
1620
1621 case LF_NESTTYPE:
1622 {
1623 struct lf_nest_type *nest = (struct lf_nest_type *) ptr;
1624 size_t name_len, subtype_len;
1625
1626 if (left < offsetof (struct lf_nest_type, name))
1627 {
1628 einfo (_("%P: warning: truncated CodeView type record"
1629 " LF_NESTTYPE\n"));
1630 return false;
1631 }
1632
1633 if (!remap_type (&nest->type, map, type_num, num_types))
1634 return false;
1635
1636 name_len =
1637 strnlen (nest->name,
1638 left - offsetof (struct lf_nest_type, name));
1639
1640 if (name_len == left - offsetof (struct lf_nest_type, name))
1641 {
1642 einfo (_("%P: warning: name for LF_NESTTYPE has no"
1643 " terminating zero\n"));
1644 return false;
1645 }
1646
1647 name_len++;
1648
1649 subtype_len = offsetof (struct lf_nest_type, name)
1650 + name_len;
1651
1652 if (subtype_len % 4 != 0)
1653 subtype_len += 4 - (subtype_len % 4);
1654
1655 if (left < subtype_len)
1656 {
1657 einfo (_("%P: warning: truncated CodeView type record"
1658 " LF_FIELDLIST\n"));
1659 return false;
1660 }
1661
1662 ptr += subtype_len;
1663 left -= subtype_len;
1664
1665 break;
1666 }
1667
1668 default:
1669 einfo (_("%P: warning: unrecognized CodeView subtype %v\n"),
1670 subtype);
1671 return false;
1672 }
1673 }
1674
1675 break;
1676 }
1677
1678 case LF_BITFIELD:
1679 {
1680 struct lf_bitfield *bf = (struct lf_bitfield *) data;
1681
1682 if (size < offsetof (struct lf_bitfield, length))
1683 {
1684 einfo (_("%P: warning: truncated CodeView type record"
1685 " LF_BITFIELD\n"));
1686 return false;
1687 }
1688
1689 if (!remap_type (&bf->base_type, map, type_num, num_types))
1690 return false;
1691
1692 break;
1693 }
1694
1695 case LF_METHODLIST:
1696 {
1697 struct lf_methodlist *ml = (struct lf_methodlist *) data;
1698 unsigned int num_entries;
1699
1700 if (size < offsetof (struct lf_methodlist, entries))
1701 {
1702 einfo (_("%P: warning: truncated CodeView type record"
1703 " LF_METHODLIST\n"));
1704 return false;
1705 }
1706
1707 if ((size - offsetof (struct lf_methodlist, entries))
1708 % sizeof (struct lf_methodlist_entry))
1709 {
1710 einfo (_("%P: warning: malformed CodeView type record"
1711 " LF_METHODLIST\n"));
1712 return false;
1713 }
1714
1715 num_entries = (size - offsetof (struct lf_methodlist, entries))
1716 / sizeof (struct lf_methodlist_entry);
1717
1718 for (unsigned int i = 0; i < num_entries; i++)
1719 {
1720 if (!remap_type (&ml->entries[i].method_type, map,
1721 type_num, num_types))
1722 return false;
1723 }
1724
1725 break;
1726 }
1727
1728 case LF_ARRAY:
1729 {
1730 struct lf_array *arr = (struct lf_array *) data;
1731
1732 if (size < offsetof (struct lf_array, length_in_bytes))
1733 {
1734 einfo (_("%P: warning: truncated CodeView type record"
1735 " LF_ARRAY\n"));
1736 return false;
1737 }
1738
1739 if (!remap_type (&arr->element_type, map, type_num, num_types))
1740 return false;
1741
1742 if (!remap_type (&arr->index_type, map, type_num, num_types))
1743 return false;
1744
1745 break;
1746 }
1747
1748 case LF_CLASS:
1749 case LF_STRUCTURE:
1750 {
1751 struct lf_class *cl = (struct lf_class *) data;
1752 uint16_t prop;
1753 size_t name_len;
1754
1755 if (size < offsetof (struct lf_class, name))
1756 {
1757 einfo (_("%P: warning: truncated CodeView type record"
1758 " LF_CLASS/LF_STRUCTURE\n"));
1759 return false;
1760 }
1761
1762 if (!remap_type (&cl->field_list, map, type_num, num_types))
1763 return false;
1764
1765 if (!remap_type (&cl->derived_from, map, type_num, num_types))
1766 return false;
1767
1768 if (!remap_type (&cl->vshape, map, type_num, num_types))
1769 return false;
1770
1771 name_len = strnlen (cl->name, size - offsetof (struct lf_class, name));
1772
1773 if (name_len == size - offsetof (struct lf_class, name))
1774 {
1775 einfo (_("%P: warning: name for LF_CLASS/LF_STRUCTURE has no"
1776 " terminating zero\n"));
1777 return false;
1778 }
1779
1780 prop = bfd_getl16 (&cl->properties);
1781
1782 if (prop & CV_PROP_HAS_UNIQUE_NAME)
1783 {
1784 /* Structure has another name following first one. */
1785
1786 size_t len = offsetof (struct lf_class, name) + name_len + 1;
1787 size_t unique_name_len;
1788
1789 unique_name_len = strnlen (cl->name + name_len + 1, size - len);
1790
1791 if (unique_name_len == size - len)
1792 {
1793 einfo (_("%P: warning: unique name for LF_CLASS/LF_STRUCTURE"
1794 " has no terminating zero\n"));
1795 return false;
1796 }
1797 }
1798
1799 if (!(prop & (CV_PROP_FORWARD_REF | CV_PROP_SCOPED))
1800 && !is_name_anonymous (cl->name, name_len))
1801 {
1802 other_hash = true;
1803 cv_hash = crc32 ((uint8_t *) cl->name, name_len);
1804 }
1805
1806 break;
1807 }
1808
1809 case LF_UNION:
1810 {
1811 struct lf_union *un = (struct lf_union *) data;
1812 uint16_t prop;
1813 size_t name_len;
1814
1815 if (size < offsetof (struct lf_union, name))
1816 {
1817 einfo (_("%P: warning: truncated CodeView type record"
1818 " LF_UNION\n"));
1819 return false;
1820 }
1821
1822 if (!remap_type (&un->field_list, map, type_num, num_types))
1823 return false;
1824
1825 name_len = strnlen (un->name, size - offsetof (struct lf_union, name));
1826
1827 if (name_len == size - offsetof (struct lf_union, name))
1828 {
1829 einfo (_("%P: warning: name for LF_UNION has no"
1830 " terminating zero\n"));
1831 return false;
1832 }
1833
1834 prop = bfd_getl16 (&un->properties);
1835
1836 if (prop & CV_PROP_HAS_UNIQUE_NAME)
1837 {
1838 /* Structure has another name following first one. */
1839
1840 size_t len = offsetof (struct lf_union, name) + name_len + 1;
1841 size_t unique_name_len;
1842
1843 unique_name_len = strnlen (un->name + name_len + 1, size - len);
1844
1845 if (unique_name_len == size - len)
1846 {
1847 einfo (_("%P: warning: unique name for LF_UNION has"
1848 " no terminating zero\n"));
1849 return false;
1850 }
1851 }
1852
1853 if (!(prop & (CV_PROP_FORWARD_REF | CV_PROP_SCOPED))
1854 && !is_name_anonymous (un->name, name_len))
1855 {
1856 other_hash = true;
1857 cv_hash = crc32 ((uint8_t *) un->name, name_len);
1858 }
1859
1860 break;
1861 }
1862
1863 case LF_ENUM:
1864 {
1865 struct lf_enum *en = (struct lf_enum *) data;
1866 uint16_t prop;
1867 size_t name_len;
1868
1869 if (size < offsetof (struct lf_enum, name))
1870 {
1871 einfo (_("%P: warning: truncated CodeView type record"
1872 " LF_ENUM\n"));
1873 return false;
1874 }
1875
1876 if (!remap_type (&en->underlying_type, map, type_num, num_types))
1877 return false;
1878
1879 if (!remap_type (&en->field_list, map, type_num, num_types))
1880 return false;
1881
1882 name_len = strnlen (en->name, size - offsetof (struct lf_enum, name));
1883
1884 if (name_len == size - offsetof (struct lf_enum, name))
1885 {
1886 einfo (_("%P: warning: name for LF_ENUM has no"
1887 " terminating zero\n"));
1888 return false;
1889 }
1890
1891 prop = bfd_getl16 (&en->properties);
1892
1893 if (prop & CV_PROP_HAS_UNIQUE_NAME)
1894 {
1895 /* Structure has another name following first one. */
1896
1897 size_t len = offsetof (struct lf_enum, name) + name_len + 1;
1898 size_t unique_name_len;
1899
1900 unique_name_len = strnlen (en->name + name_len + 1, size - len);
1901
1902 if (unique_name_len == size - len)
1903 {
1904 einfo (_("%P: warning: unique name for LF_ENUM has"
1905 " no terminating zero\n"));
1906 return false;
1907 }
1908 }
1909
1910 break;
1911 }
1912
1913 case LF_VTSHAPE:
1914 /* Does not reference any types, nothing to be done. */
1915 break;
1916
1917 case LF_STRING_ID:
1918 {
1919 struct lf_string_id *str = (struct lf_string_id *) data;
1920 size_t string_len;
1921
1922 if (size < offsetof (struct lf_string_id, string))
1923 {
1924 einfo (_("%P: warning: truncated CodeView type record"
1925 " LF_STRING_ID\n"));
1926 return false;
1927 }
1928
1929 if (!remap_type (&str->substring, map, type_num, num_types))
1930 return false;
1931
1932 string_len = strnlen (str->string,
1933 size - offsetof (struct lf_string_id, string));
1934
1935 if (string_len == size - offsetof (struct lf_string_id, string))
1936 {
1937 einfo (_("%P: warning: string for LF_STRING_ID has no"
1938 " terminating zero\n"));
1939 return false;
1940 }
1941
1942 ipi = true;
1943
1944 break;
1945 }
1946
1947 case LF_SUBSTR_LIST:
1948 {
1949 uint32_t num_entries;
1950 struct lf_arglist *ssl = (struct lf_arglist *) data;
1951
1952 if (size < offsetof (struct lf_arglist, args))
1953 {
1954 einfo (_("%P: warning: truncated CodeView type record"
1955 " LF_SUBSTR_LIST\n"));
1956 return false;
1957 }
1958
1959 num_entries = bfd_getl32 (&ssl->num_entries);
1960
1961 if (size < offsetof (struct lf_arglist, args)
1962 + (num_entries * sizeof (uint32_t)))
1963 {
1964 einfo (_("%P: warning: truncated CodeView type record"
1965 " LF_SUBSTR_LIST\n"));
1966 return false;
1967 }
1968
1969 for (uint32_t i = 0; i < num_entries; i++)
1970 {
1971 if (!remap_type (&ssl->args[i], map, type_num, num_types))
1972 return false;
1973 }
1974
1975 ipi = true;
1976
1977 break;
1978 }
1979
1980 case LF_BUILDINFO:
1981 {
1982 uint16_t num_entries;
1983 struct lf_build_info *bi = (struct lf_build_info *) data;
1984
1985 if (size < offsetof (struct lf_build_info, strings))
1986 {
1987 einfo (_("%P: warning: truncated CodeView type record"
1988 " LF_BUILDINFO\n"));
1989 return false;
1990 }
1991
1992 num_entries = bfd_getl16 (&bi->count);
1993
1994 if (size < offsetof (struct lf_build_info, strings)
1995 + (num_entries * sizeof (uint32_t)))
1996 {
1997 einfo (_("%P: warning: truncated CodeView type record"
1998 " LF_BUILDINFO\n"));
1999 return false;
2000 }
2001
2002 for (uint32_t i = 0; i < num_entries; i++)
2003 {
2004 if (!remap_type (&bi->strings[i], map, type_num, num_types))
2005 return false;
2006 }
2007
2008 ipi = true;
2009
2010 break;
2011 }
2012
2013 case LF_FUNC_ID:
2014 {
2015 struct lf_func_id *func = (struct lf_func_id *) data;
2016 size_t name_len;
2017
2018 if (size < offsetof (struct lf_func_id, name))
2019 {
2020 einfo (_("%P: warning: truncated CodeView type record"
2021 " LF_FUNC_ID\n"));
2022 return false;
2023 }
2024
2025 if (!remap_type (&func->parent_scope, map, type_num, num_types))
2026 return false;
2027
2028 if (!remap_type (&func->function_type, map, type_num, num_types))
2029 return false;
2030
2031 name_len = strnlen (func->name,
2032 size - offsetof (struct lf_func_id, name));
2033
2034 if (name_len == size - offsetof (struct lf_func_id, name))
2035 {
2036 einfo (_("%P: warning: string for LF_FUNC_ID has no"
2037 " terminating zero\n"));
2038 return false;
2039 }
2040
2041 ipi = true;
2042
2043 break;
2044 }
2045
2046 case LF_MFUNC_ID:
2047 {
2048 struct lf_mfunc_id *mfunc = (struct lf_mfunc_id *) data;
2049 size_t name_len;
2050
2051 if (size < offsetof (struct lf_mfunc_id, name))
2052 {
2053 einfo (_("%P: warning: truncated CodeView type record"
2054 " LF_MFUNC_ID\n"));
2055 return false;
2056 }
2057
2058 if (!remap_type (&mfunc->parent_type, map, type_num, num_types))
2059 return false;
2060
2061 if (!remap_type (&mfunc->function_type, map, type_num, num_types))
2062 return false;
2063
2064 name_len = strnlen (mfunc->name,
2065 size - offsetof (struct lf_mfunc_id, name));
2066
2067 if (name_len == size - offsetof (struct lf_mfunc_id, name))
2068 {
2069 einfo (_("%P: warning: string for LF_MFUNC_ID has no"
2070 " terminating zero\n"));
2071 return false;
2072 }
2073
2074 ipi = true;
2075
2076 break;
2077 }
2078
2079 default:
2080 einfo (_("%P: warning: unrecognized CodeView type %v\n"), type);
2081 return false;
2082 }
2083
2084 hash = iterative_hash (data, size, 0);
2085
2086 t = ipi ? ids : types;
2087
2088 slot = htab_find_slot_with_hash (t->hashmap, data, hash, INSERT);
2089 if (!slot)
2090 return false;
2091
2092 if (!*slot) /* new entry */
2093 {
2094 struct type_entry *e;
2095
2096 *slot = xmalloc (offsetof (struct type_entry, data) + size);
2097
2098 e = (struct type_entry *) *slot;
2099
2100 e->next = NULL;
2101 e->index = t->num_types;
2102
2103 if (other_hash)
2104 e->cv_hash = cv_hash;
2105 else
2106 e->cv_hash = crc32 (data, size);
2107
2108 memcpy (e->data, data, size);
2109
2110 if (t->last)
2111 t->last->next = e;
2112 else
2113 t->first = e;
2114
2115 t->last = e;
2116
2117 map[type_num] = e;
2118
2119 t->num_types++;
2120 }
2121 else /* duplicate */
2122 {
2123 map[type_num] = (struct type_entry *) *slot;
2124 }
2125
2126 return true;
2127 }
2128
2129 /* Parse the .debug$T section of a module, and pass any type definitions
2130 found to handle_type. */
2131 static bool
2132 handle_debugt_section (asection *s, bfd *mod, struct types *types,
2133 struct types *ids)
2134 {
2135 bfd_byte *data = NULL;
2136 size_t off;
2137 unsigned int num_types = 0;
2138 struct type_entry **map;
2139 uint32_t type_num;
2140
2141 if (!bfd_get_full_section_contents (mod, s, &data))
2142 return false;
2143
2144 if (!data)
2145 return false;
2146
2147 if (bfd_getl32 (data) != CV_SIGNATURE_C13)
2148 {
2149 free (data);
2150 return true;
2151 }
2152
2153 off = sizeof (uint32_t);
2154
2155 while (off + sizeof (uint16_t) <= s->size)
2156 {
2157 uint16_t size;
2158
2159 size = bfd_getl16 (data + off);
2160 off += sizeof (uint16_t);
2161
2162 if (size + off > s->size || size <= sizeof (uint16_t))
2163 {
2164 free (data);
2165 bfd_set_error (bfd_error_bad_value);
2166 return false;
2167 }
2168
2169 num_types++;
2170 off += size;
2171 }
2172
2173 if (num_types == 0)
2174 {
2175 free (data);
2176 return true;
2177 }
2178
2179 map = xcalloc (num_types, sizeof (struct type_entry *));
2180
2181 off = sizeof (uint32_t);
2182 type_num = 0;
2183
2184 while (off + sizeof (uint16_t) <= s->size)
2185 {
2186 uint16_t size;
2187
2188 size = bfd_getl16 (data + off);
2189
2190 if (!handle_type (data + off, map, type_num, num_types, types, ids))
2191 {
2192 free (data);
2193 free (map);
2194 bfd_set_error (bfd_error_bad_value);
2195 return false;
2196 }
2197
2198 off += sizeof (uint16_t) + size;
2199 type_num++;
2200 }
2201
2202 free (data);
2203 free (map);
2204
2205 return true;
2206 }
2207
2208 /* Populate the module stream, which consists of the transformed .debug$S
2209 data for each object file. */
2210 static bool
2211 populate_module_stream (bfd *stream, bfd *mod, uint32_t *sym_byte_size,
2212 struct string_table *strings,
2213 uint32_t *c13_info_size,
2214 struct mod_source_files *mod_source,
2215 bfd *abfd, struct types *types,
2216 struct types *ids)
2217 {
2218 uint8_t int_buf[sizeof (uint32_t)];
2219 uint8_t *c13_info = NULL;
2220
2221 *sym_byte_size = sizeof (uint32_t);
2222 *c13_info_size = 0;
2223
2224 /* Process .debug$S section(s). */
2225
2226 for (asection *s = mod->sections; s; s = s->next)
2227 {
2228 if (!strcmp (s->name, ".debug$S") && s->size >= sizeof (uint32_t))
2229 {
2230 if (!handle_debugs_section (s, mod, strings, &c13_info,
2231 c13_info_size, mod_source, abfd))
2232 {
2233 free (c13_info);
2234 free (mod_source->files);
2235 return false;
2236 }
2237 }
2238 else if (!strcmp (s->name, ".debug$T") && s->size >= sizeof (uint32_t))
2239 {
2240 if (!handle_debugt_section (s, mod, types, ids))
2241 {
2242 free (c13_info);
2243 free (mod_source->files);
2244 return false;
2245 }
2246 }
2247 }
2248
2249 /* Write the signature. */
2250
2251 bfd_putl32 (CV_SIGNATURE_C13, int_buf);
2252
2253 if (bfd_bwrite (int_buf, sizeof (uint32_t), stream) != sizeof (uint32_t))
2254 {
2255 free (c13_info);
2256 return false;
2257 }
2258
2259 if (c13_info)
2260 {
2261 if (bfd_bwrite (c13_info, *c13_info_size, stream) != *c13_info_size)
2262 {
2263 free (c13_info);
2264 return false;
2265 }
2266
2267 free (c13_info);
2268 }
2269
2270 /* Write the global refs size. */
2271
2272 bfd_putl32 (0, int_buf);
2273
2274 if (bfd_bwrite (int_buf, sizeof (uint32_t), stream) != sizeof (uint32_t))
2275 return false;
2276
2277 return true;
2278 }
2279
2280 /* Create the module info substream within the DBI. */
2281 static bool
2282 create_module_info_substream (bfd *abfd, bfd *pdb, void **data,
2283 uint32_t *size, struct string_table *strings,
2284 struct source_files_info *source,
2285 struct types *types, struct types *ids)
2286 {
2287 uint8_t *ptr;
2288 unsigned int mod_num;
2289
2290 static const char linker_fn[] = "* Linker *";
2291
2292 *size = 0;
2293
2294 for (bfd *in = coff_data (abfd)->link_info->input_bfds; in;
2295 in = in->link.next)
2296 {
2297 size_t len = sizeof (struct module_info);
2298
2299 if (!strcmp (bfd_get_filename (in), "dll stuff"))
2300 {
2301 len += sizeof (linker_fn); /* Object name. */
2302 len++; /* Empty module name. */
2303 }
2304 else if (in->my_archive)
2305 {
2306 char *name = lrealpath (bfd_get_filename (in));
2307
2308 len += strlen (name) + 1; /* Object name. */
2309
2310 free (name);
2311
2312 name = lrealpath (bfd_get_filename (in->my_archive));
2313
2314 len += strlen (name) + 1; /* Archive name. */
2315
2316 free (name);
2317 }
2318 else
2319 {
2320 char *name = lrealpath (bfd_get_filename (in));
2321 size_t name_len = strlen (name) + 1;
2322
2323 len += name_len; /* Object name. */
2324 len += name_len; /* And again as the archive name. */
2325
2326 free (name);
2327 }
2328
2329 if (len % 4)
2330 len += 4 - (len % 4);
2331
2332 *size += len;
2333
2334 source->mod_count++;
2335 }
2336
2337 *data = xmalloc (*size);
2338
2339 ptr = *data;
2340
2341 source->mods = xmalloc (source->mod_count
2342 * sizeof (struct mod_source_files));
2343 memset (source->mods, 0,
2344 source->mod_count * sizeof (struct mod_source_files));
2345
2346 mod_num = 0;
2347
2348 for (bfd *in = coff_data (abfd)->link_info->input_bfds; in;
2349 in = in->link.next)
2350 {
2351 struct module_info *mod = (struct module_info *) ptr;
2352 uint16_t stream_num;
2353 bfd *stream;
2354 uint32_t sym_byte_size, c13_info_size;
2355 uint8_t *start = ptr;
2356
2357 stream = add_stream (pdb, NULL, &stream_num);
2358
2359 if (!stream)
2360 {
2361 for (unsigned int i = 0; i < source->mod_count; i++)
2362 {
2363 free (source->mods[i].files);
2364 }
2365
2366 free (source->mods);
2367 free (*data);
2368 return false;
2369 }
2370
2371 if (!populate_module_stream (stream, in, &sym_byte_size,
2372 strings, &c13_info_size,
2373 &source->mods[mod_num], abfd,
2374 types, ids))
2375 {
2376 for (unsigned int i = 0; i < source->mod_count; i++)
2377 {
2378 free (source->mods[i].files);
2379 }
2380
2381 free (source->mods);
2382 free (*data);
2383 return false;
2384 }
2385
2386 bfd_putl32 (0, &mod->unused1);
2387
2388 /* These are dummy values - MSVC copies the first section contribution
2389 entry here, but doesn't seem to use it for anything. */
2390 bfd_putl16 (0xffff, &mod->sc.section);
2391 bfd_putl16 (0, &mod->sc.padding1);
2392 bfd_putl32 (0, &mod->sc.offset);
2393 bfd_putl32 (0xffffffff, &mod->sc.size);
2394 bfd_putl32 (0, &mod->sc.characteristics);
2395 bfd_putl16 (0xffff, &mod->sc.module_index);
2396 bfd_putl16 (0, &mod->sc.padding2);
2397 bfd_putl32 (0, &mod->sc.data_crc);
2398 bfd_putl32 (0, &mod->sc.reloc_crc);
2399
2400 bfd_putl16 (0, &mod->flags);
2401 bfd_putl16 (stream_num, &mod->module_sym_stream);
2402 bfd_putl32 (sym_byte_size, &mod->sym_byte_size);
2403 bfd_putl32 (0, &mod->c11_byte_size);
2404 bfd_putl32 (c13_info_size, &mod->c13_byte_size);
2405 bfd_putl16 (0, &mod->source_file_count);
2406 bfd_putl16 (0, &mod->padding);
2407 bfd_putl32 (0, &mod->unused2);
2408 bfd_putl32 (0, &mod->source_file_name_index);
2409 bfd_putl32 (0, &mod->pdb_file_path_name_index);
2410
2411 ptr += sizeof (struct module_info);
2412
2413 if (!strcmp (bfd_get_filename (in), "dll stuff"))
2414 {
2415 /* Object name. */
2416 memcpy (ptr, linker_fn, sizeof (linker_fn));
2417 ptr += sizeof (linker_fn);
2418
2419 /* Empty module name. */
2420 *ptr = 0;
2421 ptr++;
2422 }
2423 else if (in->my_archive)
2424 {
2425 char *name = lrealpath (bfd_get_filename (in));
2426 size_t name_len = strlen (name) + 1;
2427
2428 /* Object name. */
2429 memcpy (ptr, name, name_len);
2430 ptr += name_len;
2431
2432 free (name);
2433
2434 name = lrealpath (bfd_get_filename (in->my_archive));
2435 name_len = strlen (name) + 1;
2436
2437 /* Archive name. */
2438 memcpy (ptr, name, name_len);
2439 ptr += name_len;
2440
2441 free (name);
2442 }
2443 else
2444 {
2445 char *name = lrealpath (bfd_get_filename (in));
2446 size_t name_len = strlen (name) + 1;
2447
2448 /* Object name. */
2449 memcpy (ptr, name, name_len);
2450 ptr += name_len;
2451
2452 /* Object name again as archive name. */
2453 memcpy (ptr, name, name_len);
2454 ptr += name_len;
2455
2456 free (name);
2457 }
2458
2459 /* Pad to next four-byte boundary. */
2460
2461 if ((ptr - start) % 4)
2462 {
2463 memset (ptr, 0, 4 - ((ptr - start) % 4));
2464 ptr += 4 - ((ptr - start) % 4);
2465 }
2466
2467 mod_num++;
2468 }
2469
2470 return true;
2471 }
2472
2473 /* Return the index of a given output section. */
2474 static uint16_t
2475 find_section_number (bfd *abfd, asection *sect)
2476 {
2477 uint16_t i = 1;
2478
2479 for (asection *s = abfd->sections; s; s = s->next)
2480 {
2481 if (s == sect)
2482 return i;
2483
2484 /* Empty sections aren't output. */
2485 if (s->size != 0)
2486 i++;
2487 }
2488
2489 return 0;
2490 }
2491
2492 /* Create the substream which maps addresses in the image file to locations
2493 in the original object files. */
2494 static bool
2495 create_section_contrib_substream (bfd *abfd, void **data, uint32_t *size)
2496 {
2497 unsigned int num_sc = 0;
2498 struct section_contribution *sc;
2499 uint16_t mod_index;
2500 char *sect_flags;
2501 file_ptr offset;
2502
2503 for (bfd *in = coff_data (abfd)->link_info->input_bfds; in;
2504 in = in->link.next)
2505 {
2506 for (asection *s = in->sections; s; s = s->next)
2507 {
2508 if (s->size == 0 || discarded_section (s))
2509 continue;
2510
2511 num_sc++;
2512 }
2513 }
2514
2515 *size = sizeof (uint32_t) + (num_sc * sizeof (struct section_contribution));
2516 *data = xmalloc (*size);
2517
2518 bfd_putl32 (SECTION_CONTRIB_VERSION_60, *data);
2519
2520 /* Read characteristics of outputted sections. */
2521
2522 sect_flags = xmalloc (sizeof (uint32_t) * abfd->section_count);
2523
2524 offset = bfd_coff_filhsz (abfd) + bfd_coff_aoutsz (abfd);
2525 offset += offsetof (struct external_scnhdr, s_flags);
2526
2527 for (unsigned int i = 0; i < abfd->section_count; i++)
2528 {
2529 bfd_seek (abfd, offset, SEEK_SET);
2530
2531 if (bfd_bread (sect_flags + (i * sizeof (uint32_t)), sizeof (uint32_t),
2532 abfd) != sizeof (uint32_t))
2533 {
2534 free (*data);
2535 free (sect_flags);
2536 return false;
2537 }
2538
2539 offset += sizeof (struct external_scnhdr);
2540 }
2541
2542 sc =
2543 (struct section_contribution *) ((uint8_t *) *data + sizeof (uint32_t));
2544
2545 mod_index = 0;
2546 for (bfd *in = coff_data (abfd)->link_info->input_bfds; in;
2547 in = in->link.next)
2548 {
2549 for (asection *s = in->sections; s; s = s->next)
2550 {
2551 uint16_t sect_num;
2552
2553 if (s->size == 0 || discarded_section (s))
2554 continue;
2555
2556 sect_num = find_section_number (abfd, s->output_section);
2557
2558 memcpy (&sc->characteristics,
2559 sect_flags + ((sect_num - 1) * sizeof (uint32_t)),
2560 sizeof (uint32_t));
2561
2562 bfd_putl16 (sect_num, &sc->section);
2563 bfd_putl16 (0, &sc->padding1);
2564 bfd_putl32 (s->output_offset, &sc->offset);
2565 bfd_putl32 (s->size, &sc->size);
2566 bfd_putl16 (mod_index, &sc->module_index);
2567 bfd_putl16 (0, &sc->padding2);
2568 bfd_putl32 (0, &sc->data_crc);
2569 bfd_putl32 (0, &sc->reloc_crc);
2570
2571 sc++;
2572 }
2573
2574 mod_index++;
2575 }
2576
2577 free (sect_flags);
2578
2579 return true;
2580 }
2581
2582 /* The source info substream lives within the DBI stream, and lists the
2583 source files for each object file (i.e. it's derived from the
2584 DEBUG_S_FILECHKSMS parts of the .debug$S sections). This is a bit
2585 superfluous, as the filenames are also available in the C13 parts of
2586 the module streams, but MSVC relies on it to work properly. */
2587 static void
2588 create_source_info_substream (void **data, uint32_t *size,
2589 struct source_files_info *source)
2590 {
2591 uint16_t dedupe_source_files_count = 0;
2592 uint16_t source_files_count = 0;
2593 uint32_t strings_len = 0;
2594 uint8_t *ptr;
2595
2596 /* Loop through the source files, marking unique filenames. The pointers
2597 here are for entries in the main string table, and so have already
2598 been deduplicated. */
2599
2600 for (uint16_t i = 0; i < source->mod_count; i++)
2601 {
2602 for (uint16_t j = 0; j < source->mods[i].files_count; j++)
2603 {
2604 if (source->mods[i].files[j])
2605 {
2606 if (source->mods[i].files[j]->source_file_offset == 0xffffffff)
2607 {
2608 source->mods[i].files[j]->source_file_offset = strings_len;
2609 strings_len += source->mods[i].files[j]->len + 1;
2610 dedupe_source_files_count++;
2611 }
2612
2613 source_files_count++;
2614 }
2615 }
2616 }
2617
2618 *size = sizeof (uint16_t) + sizeof (uint16_t);
2619 *size += (sizeof (uint16_t) + sizeof (uint16_t)) * source->mod_count;
2620 *size += sizeof (uint32_t) * source_files_count;
2621 *size += strings_len;
2622
2623 *data = xmalloc (*size);
2624
2625 ptr = (uint8_t *) *data;
2626
2627 /* Write header (module count and source file count). */
2628
2629 bfd_putl16 (source->mod_count, ptr);
2630 ptr += sizeof (uint16_t);
2631
2632 bfd_putl16 (dedupe_source_files_count, ptr);
2633 ptr += sizeof (uint16_t);
2634
2635 /* Write "ModIndices". As the LLVM documentation puts it, "this array is
2636 present, but does not appear to be useful". */
2637
2638 for (uint16_t i = 0; i < source->mod_count; i++)
2639 {
2640 bfd_putl16 (i, ptr);
2641 ptr += sizeof (uint16_t);
2642 }
2643
2644 /* Write source file count for each module. */
2645
2646 for (uint16_t i = 0; i < source->mod_count; i++)
2647 {
2648 bfd_putl16 (source->mods[i].files_count, ptr);
2649 ptr += sizeof (uint16_t);
2650 }
2651
2652 /* For each module, write the offsets within the string table
2653 for each source file. */
2654
2655 for (uint16_t i = 0; i < source->mod_count; i++)
2656 {
2657 for (uint16_t j = 0; j < source->mods[i].files_count; j++)
2658 {
2659 if (source->mods[i].files[j])
2660 {
2661 bfd_putl32 (source->mods[i].files[j]->source_file_offset, ptr);
2662 ptr += sizeof (uint32_t);
2663 }
2664 }
2665 }
2666
2667 /* Write the string table. We set source_file_offset to a dummy value for
2668 each entry we write, so we don't write duplicate filenames. */
2669
2670 for (uint16_t i = 0; i < source->mod_count; i++)
2671 {
2672 for (uint16_t j = 0; j < source->mods[i].files_count; j++)
2673 {
2674 if (source->mods[i].files[j]
2675 && source->mods[i].files[j]->source_file_offset != 0xffffffff)
2676 {
2677 memcpy (ptr, source->mods[i].files[j]->s,
2678 source->mods[i].files[j]->len);
2679 ptr += source->mods[i].files[j]->len;
2680
2681 *ptr = 0;
2682 ptr++;
2683
2684 source->mods[i].files[j]->source_file_offset = 0xffffffff;
2685 }
2686 }
2687 }
2688 }
2689
2690 /* Stream 4 is the debug information (DBI) stream. */
2691 static bool
2692 populate_dbi_stream (bfd *stream, bfd *abfd, bfd *pdb,
2693 uint16_t section_header_stream_num,
2694 uint16_t sym_rec_stream_num,
2695 uint16_t publics_stream_num,
2696 struct string_table *strings,
2697 struct types *types,
2698 struct types *ids)
2699 {
2700 struct pdb_dbi_stream_header h;
2701 struct optional_dbg_header opt;
2702 void *mod_info, *sc, *source_info;
2703 uint32_t mod_info_size, sc_size, source_info_size;
2704 struct source_files_info source;
2705
2706 source.mod_count = 0;
2707 source.mods = NULL;
2708
2709 if (!create_module_info_substream (abfd, pdb, &mod_info, &mod_info_size,
2710 strings, &source, types, ids))
2711 return false;
2712
2713 if (!create_section_contrib_substream (abfd, &sc, &sc_size))
2714 {
2715 for (unsigned int i = 0; i < source.mod_count; i++)
2716 {
2717 free (source.mods[i].files);
2718 }
2719 free (source.mods);
2720
2721 free (mod_info);
2722 return false;
2723 }
2724
2725 create_source_info_substream (&source_info, &source_info_size, &source);
2726
2727 for (unsigned int i = 0; i < source.mod_count; i++)
2728 {
2729 free (source.mods[i].files);
2730 }
2731 free (source.mods);
2732
2733 bfd_putl32 (0xffffffff, &h.version_signature);
2734 bfd_putl32 (DBI_STREAM_VERSION_70, &h.version_header);
2735 bfd_putl32 (1, &h.age);
2736 bfd_putl16 (0xffff, &h.global_stream_index);
2737 bfd_putl16 (0x8e1d, &h.build_number); // MSVC 14.29
2738 bfd_putl16 (publics_stream_num, &h.public_stream_index);
2739 bfd_putl16 (0, &h.pdb_dll_version);
2740 bfd_putl16 (sym_rec_stream_num, &h.sym_record_stream);
2741 bfd_putl16 (0, &h.pdb_dll_rbld);
2742 bfd_putl32 (mod_info_size, &h.mod_info_size);
2743 bfd_putl32 (sc_size, &h.section_contribution_size);
2744 bfd_putl32 (0, &h.section_map_size);
2745 bfd_putl32 (source_info_size, &h.source_info_size);
2746 bfd_putl32 (0, &h.type_server_map_size);
2747 bfd_putl32 (0, &h.mfc_type_server_index);
2748 bfd_putl32 (sizeof (opt), &h.optional_dbg_header_size);
2749 bfd_putl32 (0, &h.ec_substream_size);
2750 bfd_putl16 (0, &h.flags);
2751 bfd_putl16 (get_arch_number (abfd), &h.machine);
2752 bfd_putl32 (0, &h.padding);
2753
2754 if (bfd_bwrite (&h, sizeof (h), stream) != sizeof (h))
2755 {
2756 free (source_info);
2757 free (sc);
2758 free (mod_info);
2759 return false;
2760 }
2761
2762 if (bfd_bwrite (mod_info, mod_info_size, stream) != mod_info_size)
2763 {
2764 free (source_info);
2765 free (sc);
2766 free (mod_info);
2767 return false;
2768 }
2769
2770 free (mod_info);
2771
2772 if (bfd_bwrite (sc, sc_size, stream) != sc_size)
2773 {
2774 free (source_info);
2775 free (sc);
2776 return false;
2777 }
2778
2779 free (sc);
2780
2781 if (bfd_bwrite (source_info, source_info_size, stream) != source_info_size)
2782 {
2783 free (source_info);
2784 return false;
2785 }
2786
2787 free (source_info);
2788
2789 bfd_putl16 (0xffff, &opt.fpo_stream);
2790 bfd_putl16 (0xffff, &opt.exception_stream);
2791 bfd_putl16 (0xffff, &opt.fixup_stream);
2792 bfd_putl16 (0xffff, &opt.omap_to_src_stream);
2793 bfd_putl16 (0xffff, &opt.omap_from_src_stream);
2794 bfd_putl16 (section_header_stream_num, &opt.section_header_stream);
2795 bfd_putl16 (0xffff, &opt.token_map_stream);
2796 bfd_putl16 (0xffff, &opt.xdata_stream);
2797 bfd_putl16 (0xffff, &opt.pdata_stream);
2798 bfd_putl16 (0xffff, &opt.new_fpo_stream);
2799 bfd_putl16 (0xffff, &opt.orig_section_header_stream);
2800
2801 if (bfd_bwrite (&opt, sizeof (opt), stream) != sizeof (opt))
2802 return false;
2803
2804 return true;
2805 }
2806
2807 /* Used as parameter to qsort, to sort publics by hash. */
2808 static int
2809 public_compare_hash (const void *s1, const void *s2)
2810 {
2811 const struct public *p1 = *(const struct public **) s1;
2812 const struct public *p2 = *(const struct public **) s2;
2813
2814 if (p1->hash < p2->hash)
2815 return -1;
2816 if (p1->hash > p2->hash)
2817 return 1;
2818
2819 return 0;
2820 }
2821
2822 /* Used as parameter to qsort, to sort publics by address. */
2823 static int
2824 public_compare_addr (const void *s1, const void *s2)
2825 {
2826 const struct public *p1 = *(const struct public **) s1;
2827 const struct public *p2 = *(const struct public **) s2;
2828
2829 if (p1->section < p2->section)
2830 return -1;
2831 if (p1->section > p2->section)
2832 return 1;
2833
2834 if (p1->address < p2->address)
2835 return -1;
2836 if (p1->address > p2->address)
2837 return 1;
2838
2839 return 0;
2840 }
2841
2842 /* The publics stream is a hash map of S_PUB32 records, which are stored
2843 in the symbol record stream. Each S_PUB32 entry represents a symbol
2844 from the point of view of the linker: a section index, an offset within
2845 the section, and a mangled name. Compare with S_GDATA32 and S_GPROC32,
2846 which are the same thing but generated by the compiler. */
2847 static bool
2848 populate_publics_stream (bfd *stream, bfd *abfd, bfd *sym_rec_stream)
2849 {
2850 struct publics_header header;
2851 struct globals_hash_header hash_header;
2852 const unsigned int num_buckets = 4096;
2853 unsigned int num_entries = 0, filled_buckets = 0;
2854 unsigned int buckets_size, sym_hash_size;
2855 char int_buf[sizeof (uint32_t)];
2856 struct public *publics_head = NULL, *publics_tail = NULL;
2857 struct public **buckets;
2858 struct public **sorted = NULL;
2859 bool ret = false;
2860
2861 buckets = xmalloc (sizeof (struct public *) * num_buckets);
2862 memset (buckets, 0, sizeof (struct public *) * num_buckets);
2863
2864 /* Loop through the global symbols in our input files, and write S_PUB32
2865 records in the symbol record stream for those that make it into the
2866 final image. */
2867 for (bfd *in = coff_data (abfd)->link_info->input_bfds; in;
2868 in = in->link.next)
2869 {
2870 if (!in->outsymbols)
2871 continue;
2872
2873 for (unsigned int i = 0; i < in->symcount; i++)
2874 {
2875 struct bfd_symbol *sym = in->outsymbols[i];
2876
2877 if (sym->flags & BSF_GLOBAL)
2878 {
2879 struct pubsym ps;
2880 uint16_t record_length;
2881 const char *name = sym->name;
2882 size_t name_len = strlen (name);
2883 struct public *p = xmalloc (sizeof (struct public));
2884 unsigned int padding = 0;
2885 uint16_t section;
2886 uint32_t flags = 0;
2887
2888 section =
2889 find_section_number (abfd, sym->section->output_section);
2890
2891 if (section == 0)
2892 continue;
2893
2894 p->next = NULL;
2895 p->offset = bfd_tell (sym_rec_stream);
2896 p->hash = calc_hash (name, name_len) % num_buckets;
2897 p->section = section;
2898 p->address = sym->section->output_offset + sym->value;
2899
2900 record_length = sizeof (struct pubsym) + name_len + 1;
2901
2902 if (record_length % 4)
2903 padding = 4 - (record_length % 4);
2904
2905 /* Assume that all global symbols in executable sections
2906 are functions. */
2907 if (sym->section->flags & SEC_CODE)
2908 flags = PUBSYM_FUNCTION;
2909
2910 bfd_putl16 (record_length + padding - sizeof (uint16_t),
2911 &ps.record_length);
2912 bfd_putl16 (S_PUB32, &ps.record_type);
2913 bfd_putl32 (flags, &ps.flags);
2914 bfd_putl32 (p->address, &ps.offset);
2915 bfd_putl16 (p->section, &ps.section);
2916
2917 if (bfd_bwrite (&ps, sizeof (struct pubsym), sym_rec_stream) !=
2918 sizeof (struct pubsym))
2919 goto end;
2920
2921 if (bfd_bwrite (name, name_len + 1, sym_rec_stream) !=
2922 name_len + 1)
2923 goto end;
2924
2925 for (unsigned int j = 0; j < padding; j++)
2926 {
2927 uint8_t b = 0;
2928
2929 if (bfd_bwrite (&b, sizeof (uint8_t), sym_rec_stream) !=
2930 sizeof (uint8_t))
2931 goto end;
2932 }
2933
2934 if (!publics_head)
2935 publics_head = p;
2936 else
2937 publics_tail->next = p;
2938
2939 publics_tail = p;
2940 num_entries++;
2941 }
2942 }
2943 }
2944
2945
2946 if (num_entries > 0)
2947 {
2948 /* Create an array of pointers, sorted by hash value. */
2949
2950 sorted = xmalloc (sizeof (struct public *) * num_entries);
2951
2952 struct public *p = publics_head;
2953 for (unsigned int i = 0; i < num_entries; i++)
2954 {
2955 sorted[i] = p;
2956 p = p->next;
2957 }
2958
2959 qsort (sorted, num_entries, sizeof (struct public *),
2960 public_compare_hash);
2961
2962 /* Populate the buckets. */
2963
2964 for (unsigned int i = 0; i < num_entries; i++)
2965 {
2966 if (!buckets[sorted[i]->hash])
2967 {
2968 buckets[sorted[i]->hash] = sorted[i];
2969 filled_buckets++;
2970 }
2971
2972 sorted[i]->index = i;
2973 }
2974 }
2975
2976 buckets_size = num_buckets / 8;
2977 buckets_size += sizeof (uint32_t);
2978 buckets_size += filled_buckets * sizeof (uint32_t);
2979
2980 sym_hash_size = sizeof (hash_header);
2981 sym_hash_size += num_entries * sizeof (struct hash_record);
2982 sym_hash_size += buckets_size;
2983
2984 /* Output the publics header. */
2985
2986 bfd_putl32 (sym_hash_size, &header.sym_hash_size);
2987 bfd_putl32 (num_entries * sizeof (uint32_t), &header.addr_map_size);
2988 bfd_putl32 (0, &header.num_thunks);
2989 bfd_putl32 (0, &header.thunks_size);
2990 bfd_putl32 (0, &header.thunk_table);
2991 bfd_putl32 (0, &header.thunk_table_offset);
2992 bfd_putl32 (0, &header.num_sects);
2993
2994 if (bfd_bwrite (&header, sizeof (header), stream) != sizeof (header))
2995 goto end;
2996
2997 /* Output the global hash header. */
2998
2999 bfd_putl32 (GLOBALS_HASH_SIGNATURE, &hash_header.signature);
3000 bfd_putl32 (GLOBALS_HASH_VERSION_70, &hash_header.version);
3001 bfd_putl32 (num_entries * sizeof (struct hash_record),
3002 &hash_header.entries_size);
3003 bfd_putl32 (buckets_size, &hash_header.buckets_size);
3004
3005 if (bfd_bwrite (&hash_header, sizeof (hash_header), stream) !=
3006 sizeof (hash_header))
3007 goto end;
3008
3009 /* Write the entries in hash order. */
3010
3011 for (unsigned int i = 0; i < num_entries; i++)
3012 {
3013 struct hash_record hr;
3014
3015 bfd_putl32 (sorted[i]->offset + 1, &hr.offset);
3016 bfd_putl32 (1, &hr.reference);
3017
3018 if (bfd_bwrite (&hr, sizeof (hr), stream) != sizeof (hr))
3019 goto end;
3020 }
3021
3022 /* Write the bitmap for filled and unfilled buckets. */
3023
3024 for (unsigned int i = 0; i < num_buckets; i += 8)
3025 {
3026 uint8_t v = 0;
3027
3028 for (unsigned int j = 0; j < 8; j++)
3029 {
3030 if (buckets[i + j])
3031 v |= 1 << j;
3032 }
3033
3034 if (bfd_bwrite (&v, sizeof (v), stream) != sizeof (v))
3035 goto end;
3036 }
3037
3038 /* Add a 4-byte gap. */
3039
3040 bfd_putl32 (0, int_buf);
3041
3042 if (bfd_bwrite (int_buf, sizeof (uint32_t), stream) != sizeof (uint32_t))
3043 goto end;
3044
3045 /* Write the bucket offsets. */
3046
3047 for (unsigned int i = 0; i < num_buckets; i++)
3048 {
3049 if (buckets[i])
3050 {
3051 /* 0xc is size of internal hash_record structure in
3052 Microsoft's parser. */
3053 bfd_putl32 (buckets[i]->index * 0xc, int_buf);
3054
3055 if (bfd_bwrite (int_buf, sizeof (uint32_t), stream) !=
3056 sizeof (uint32_t))
3057 goto end;
3058 }
3059 }
3060
3061 /* Write the address map: offsets into the symbol record stream of
3062 S_PUB32 records, ordered by address. */
3063
3064 if (num_entries > 0)
3065 {
3066 qsort (sorted, num_entries, sizeof (struct public *),
3067 public_compare_addr);
3068
3069 for (unsigned int i = 0; i < num_entries; i++)
3070 {
3071 bfd_putl32 (sorted[i]->offset, int_buf);
3072
3073 if (bfd_bwrite (int_buf, sizeof (uint32_t), stream) !=
3074 sizeof (uint32_t))
3075 goto end;
3076 }
3077 }
3078
3079 ret = true;
3080
3081 end:
3082 free (buckets);
3083
3084 while (publics_head)
3085 {
3086 struct public *p = publics_head->next;
3087
3088 free (publics_head);
3089 publics_head = p;
3090 }
3091
3092 free (sorted);
3093
3094 return ret;
3095 }
3096
3097 /* The section header stream contains a copy of the section headers
3098 from the PE file, in the same format. */
3099 static bool
3100 create_section_header_stream (bfd *pdb, bfd *abfd, uint16_t *num)
3101 {
3102 bfd *stream;
3103 unsigned int section_count;
3104 file_ptr scn_base;
3105 size_t len;
3106 char *buf;
3107
3108 stream = add_stream (pdb, NULL, num);
3109 if (!stream)
3110 return false;
3111
3112 section_count = abfd->section_count;
3113
3114 /* Empty sections aren't output. */
3115 for (asection *sect = abfd->sections; sect; sect = sect->next)
3116 {
3117 if (sect->size == 0)
3118 section_count--;
3119 }
3120
3121 if (section_count == 0)
3122 return true;
3123
3124 /* Copy section table from output - it's already been written at this
3125 point. */
3126
3127 scn_base = bfd_coff_filhsz (abfd) + bfd_coff_aoutsz (abfd);
3128
3129 bfd_seek (abfd, scn_base, SEEK_SET);
3130
3131 len = section_count * sizeof (struct external_scnhdr);
3132 buf = xmalloc (len);
3133
3134 if (bfd_bread (buf, len, abfd) != len)
3135 {
3136 free (buf);
3137 return false;
3138 }
3139
3140 if (bfd_bwrite (buf, len, stream) != len)
3141 {
3142 free (buf);
3143 return false;
3144 }
3145
3146 free (buf);
3147
3148 return true;
3149 }
3150
3151 /* Populate the "/names" named stream, which contains the string table. */
3152 static bool
3153 populate_names_stream (bfd *stream, struct string_table *strings)
3154 {
3155 char int_buf[sizeof (uint32_t)];
3156 struct string_table_header h;
3157 uint32_t num_strings = 0, num_buckets;
3158 struct string **buckets;
3159
3160 bfd_putl32 (STRING_TABLE_SIGNATURE, &h.signature);
3161 bfd_putl32 (STRING_TABLE_VERSION, &h.version);
3162
3163 if (bfd_bwrite (&h, sizeof (h), stream) != sizeof (h))
3164 return false;
3165
3166 bfd_putl32 (strings->strings_len, int_buf);
3167
3168 if (bfd_bwrite (int_buf, sizeof (uint32_t), stream) != sizeof (uint32_t))
3169 return false;
3170
3171 int_buf[0] = 0;
3172
3173 if (bfd_bwrite (int_buf, 1, stream) != 1)
3174 return false;
3175
3176 for (struct string *s = strings->strings_head; s; s = s->next)
3177 {
3178 if (bfd_bwrite (s->s, s->len, stream) != s->len)
3179 return false;
3180
3181 if (bfd_bwrite (int_buf, 1, stream) != 1)
3182 return false;
3183
3184 num_strings++;
3185 }
3186
3187 num_buckets = num_strings * 2;
3188
3189 buckets = xmalloc (sizeof (struct string *) * num_buckets);
3190 memset (buckets, 0, sizeof (struct string *) * num_buckets);
3191
3192 for (struct string *s = strings->strings_head; s; s = s->next)
3193 {
3194 uint32_t bucket_num = s->hash % num_buckets;
3195
3196 while (buckets[bucket_num])
3197 {
3198 bucket_num++;
3199
3200 if (bucket_num == num_buckets)
3201 bucket_num = 0;
3202 }
3203
3204 buckets[bucket_num] = s;
3205 }
3206
3207 bfd_putl32 (num_buckets, int_buf);
3208
3209 if (bfd_bwrite (int_buf, sizeof (uint32_t), stream) != sizeof (uint32_t))
3210 {
3211 free (buckets);
3212 return false;
3213 }
3214
3215 for (unsigned int i = 0; i < num_buckets; i++)
3216 {
3217 if (buckets[i])
3218 bfd_putl32 (buckets[i]->offset, int_buf);
3219 else
3220 bfd_putl32 (0, int_buf);
3221
3222 if (bfd_bwrite (int_buf, sizeof (uint32_t), stream) !=
3223 sizeof (uint32_t))
3224 {
3225 free (buckets);
3226 return false;
3227 }
3228 }
3229
3230 free (buckets);
3231
3232 bfd_putl32 (num_strings, int_buf);
3233
3234 if (bfd_bwrite (int_buf, sizeof (uint32_t), stream) != sizeof (uint32_t))
3235 return false;
3236
3237 return true;
3238 }
3239
3240 /* Calculate the hash of a type_entry. */
3241 static hashval_t
3242 hash_type_entry (const void *p)
3243 {
3244 const struct type_entry *e = (const struct type_entry *) p;
3245 uint16_t size = bfd_getl16 (e->data) + sizeof (uint16_t);
3246
3247 return iterative_hash (e->data, size, 0);
3248 }
3249
3250 /* Compare a type_entry with a type. */
3251 static int
3252 eq_type_entry (const void *a, const void *b)
3253 {
3254 const struct type_entry *e = (const struct type_entry *) a;
3255 uint16_t size_a = bfd_getl16 (e->data);
3256 uint16_t size_b = bfd_getl16 (b);
3257
3258 if (size_a != size_b)
3259 return 0;
3260
3261 return memcmp (e->data + sizeof (uint16_t),
3262 (const uint8_t *) b + sizeof (uint16_t), size_a) == 0;
3263 }
3264
3265 /* Create a PDB debugging file for the PE image file abfd with the build ID
3266 guid, stored at pdb_name. */
3267 bool
3268 create_pdb_file (bfd *abfd, const char *pdb_name, const unsigned char *guid)
3269 {
3270 bfd *pdb;
3271 bool ret = false;
3272 bfd *info_stream, *dbi_stream, *names_stream, *sym_rec_stream,
3273 *publics_stream, *tpi_stream, *ipi_stream;
3274 uint16_t section_header_stream_num, sym_rec_stream_num, publics_stream_num;
3275 struct string_table strings;
3276 struct types types, ids;
3277
3278 pdb = bfd_openw (pdb_name, "pdb");
3279 if (!pdb)
3280 {
3281 einfo (_("%P: warning: cannot create PDB file: %E\n"));
3282 return false;
3283 }
3284
3285 strings.strings_head = NULL;
3286 strings.strings_tail = NULL;
3287 strings.strings_len = 1;
3288 strings.hashmap = htab_create_alloc (0, hash_string_table_entry,
3289 eq_string_table_entry, free,
3290 xcalloc, free);
3291
3292 bfd_set_format (pdb, bfd_archive);
3293
3294 if (!create_old_directory_stream (pdb))
3295 {
3296 einfo (_("%P: warning: cannot create old directory stream "
3297 "in PDB file: %E\n"));
3298 goto end;
3299 }
3300
3301 info_stream = add_stream (pdb, NULL, NULL);
3302
3303 if (!info_stream)
3304 {
3305 einfo (_("%P: warning: cannot create info stream "
3306 "in PDB file: %E\n"));
3307 goto end;
3308 }
3309
3310 tpi_stream = add_stream (pdb, NULL, NULL);
3311
3312 if (!tpi_stream)
3313 {
3314 einfo (_("%P: warning: cannot create TPI stream "
3315 "in PDB file: %E\n"));
3316 goto end;
3317 }
3318
3319 dbi_stream = add_stream (pdb, NULL, NULL);
3320
3321 if (!dbi_stream)
3322 {
3323 einfo (_("%P: warning: cannot create DBI stream "
3324 "in PDB file: %E\n"));
3325 goto end;
3326 }
3327
3328 ipi_stream = add_stream (pdb, NULL, NULL);
3329
3330 if (!ipi_stream)
3331 {
3332 einfo (_("%P: warning: cannot create IPI stream "
3333 "in PDB file: %E\n"));
3334 goto end;
3335 }
3336
3337 names_stream = add_stream (pdb, "/names", NULL);
3338
3339 if (!names_stream)
3340 {
3341 einfo (_("%P: warning: cannot create /names stream "
3342 "in PDB file: %E\n"));
3343 goto end;
3344 }
3345
3346 sym_rec_stream = add_stream (pdb, NULL, &sym_rec_stream_num);
3347
3348 if (!sym_rec_stream)
3349 {
3350 einfo (_("%P: warning: cannot create symbol record stream "
3351 "in PDB file: %E\n"));
3352 goto end;
3353 }
3354
3355 publics_stream = add_stream (pdb, NULL, &publics_stream_num);
3356
3357 if (!publics_stream)
3358 {
3359 einfo (_("%P: warning: cannot create publics stream "
3360 "in PDB file: %E\n"));
3361 goto end;
3362 }
3363
3364 if (!create_section_header_stream (pdb, abfd, &section_header_stream_num))
3365 {
3366 einfo (_("%P: warning: cannot create section header stream "
3367 "in PDB file: %E\n"));
3368 goto end;
3369 }
3370
3371 types.num_types = 0;
3372 types.hashmap = htab_create_alloc (0, hash_type_entry, eq_type_entry,
3373 free, xcalloc, free);
3374 types.first = types.last = NULL;
3375
3376 ids.num_types = 0;
3377 ids.hashmap = htab_create_alloc (0, hash_type_entry, eq_type_entry,
3378 free, xcalloc, free);
3379 ids.first = ids.last = NULL;
3380
3381 if (!populate_dbi_stream (dbi_stream, abfd, pdb, section_header_stream_num,
3382 sym_rec_stream_num, publics_stream_num,
3383 &strings, &types, &ids))
3384 {
3385 einfo (_("%P: warning: cannot populate DBI stream "
3386 "in PDB file: %E\n"));
3387 htab_delete (types.hashmap);
3388 htab_delete (ids.hashmap);
3389 goto end;
3390 }
3391
3392 if (!populate_type_stream (pdb, tpi_stream, &types))
3393 {
3394 einfo (_("%P: warning: cannot populate TPI stream "
3395 "in PDB file: %E\n"));
3396 htab_delete (types.hashmap);
3397 htab_delete (ids.hashmap);
3398 goto end;
3399 }
3400
3401 htab_delete (types.hashmap);
3402
3403 if (!populate_type_stream (pdb, ipi_stream, &ids))
3404 {
3405 einfo (_("%P: warning: cannot populate IPI stream "
3406 "in PDB file: %E\n"));
3407 htab_delete (ids.hashmap);
3408 goto end;
3409 }
3410
3411 htab_delete (ids.hashmap);
3412
3413 add_string ("", 0, &strings);
3414
3415 if (!populate_names_stream (names_stream, &strings))
3416 {
3417 einfo (_("%P: warning: cannot populate names stream "
3418 "in PDB file: %E\n"));
3419 goto end;
3420 }
3421
3422 if (!populate_publics_stream (publics_stream, abfd, sym_rec_stream))
3423 {
3424 einfo (_("%P: warning: cannot populate publics stream "
3425 "in PDB file: %E\n"));
3426 goto end;
3427 }
3428
3429 if (!populate_info_stream (pdb, info_stream, guid))
3430 {
3431 einfo (_("%P: warning: cannot populate info stream "
3432 "in PDB file: %E\n"));
3433 goto end;
3434 }
3435
3436 ret = true;
3437
3438 end:
3439 bfd_close (pdb);
3440
3441 htab_delete (strings.hashmap);
3442
3443 return ret;
3444 }