ld: Write types into TPI 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 {
1129 uint16_t size, type;
1130 void **slot;
1131 hashval_t hash;
1132 bool other_hash = false;
1133 uint32_t cv_hash;
1134
1135 size = bfd_getl16 (data) + sizeof (uint16_t);
1136 type = bfd_getl16 (data + sizeof (uint16_t));
1137
1138 switch (type)
1139 {
1140 case LF_MODIFIER:
1141 {
1142 struct lf_modifier *mod = (struct lf_modifier *) data;
1143
1144 if (size < offsetof (struct lf_modifier, modifier))
1145 {
1146 einfo (_("%P: warning: truncated CodeView type record "
1147 "LF_MODIFIER\n"));
1148 return false;
1149 }
1150
1151 if (!remap_type (&mod->base_type, map, type_num, num_types))
1152 return false;
1153
1154 break;
1155 }
1156
1157 case LF_POINTER:
1158 {
1159 struct lf_pointer *ptr = (struct lf_pointer *) data;
1160
1161 if (size < offsetof (struct lf_pointer, attributes))
1162 {
1163 einfo (_("%P: warning: truncated CodeView type record"
1164 " LF_POINTER\n"));
1165 return false;
1166 }
1167
1168 if (!remap_type (&ptr->base_type, map, type_num, num_types))
1169 return false;
1170
1171 break;
1172 }
1173
1174 case LF_PROCEDURE:
1175 {
1176 struct lf_procedure *proc = (struct lf_procedure *) data;
1177
1178 if (size < sizeof (struct lf_procedure))
1179 {
1180 einfo (_("%P: warning: truncated CodeView type record"
1181 " LF_PROCEDURE\n"));
1182 return false;
1183 }
1184
1185 if (!remap_type (&proc->return_type, map, type_num, num_types))
1186 return false;
1187
1188 if (!remap_type (&proc->arglist, map, type_num, num_types))
1189 return false;
1190
1191 break;
1192 }
1193
1194 case LF_MFUNCTION:
1195 {
1196 struct lf_mfunction *func = (struct lf_mfunction *) data;
1197
1198 if (size < sizeof (struct lf_procedure))
1199 {
1200 einfo (_("%P: warning: truncated CodeView type record"
1201 " LF_MFUNCTION\n"));
1202 return false;
1203 }
1204
1205 if (!remap_type (&func->return_type, map, type_num, num_types))
1206 return false;
1207
1208 if (!remap_type (&func->containing_class_type, map, type_num,
1209 num_types))
1210 return false;
1211
1212 if (!remap_type (&func->this_type, map, type_num, num_types))
1213 return false;
1214
1215 if (!remap_type (&func->arglist, map, type_num, num_types))
1216 return false;
1217
1218 break;
1219 }
1220
1221 case LF_ARGLIST:
1222 {
1223 uint32_t num_entries;
1224 struct lf_arglist *al = (struct lf_arglist *) data;
1225
1226 if (size < offsetof (struct lf_arglist, args))
1227 {
1228 einfo (_("%P: warning: truncated CodeView type record"
1229 " LF_ARGLIST\n"));
1230 return false;
1231 }
1232
1233 num_entries = bfd_getl32 (&al->num_entries);
1234
1235 if (size < offsetof (struct lf_arglist, args)
1236 + (num_entries * sizeof (uint32_t)))
1237 {
1238 einfo (_("%P: warning: truncated CodeView type record"
1239 " LF_ARGLIST\n"));
1240 return false;
1241 }
1242
1243 for (uint32_t i = 0; i < num_entries; i++)
1244 {
1245 if (!remap_type (&al->args[i], map, type_num, num_types))
1246 return false;
1247 }
1248
1249 break;
1250 }
1251
1252 case LF_FIELDLIST:
1253 {
1254 uint16_t left = size - sizeof (uint16_t) - sizeof (uint16_t);
1255 uint8_t *ptr = data + sizeof (uint16_t) + sizeof (uint16_t);
1256
1257 while (left > 0)
1258 {
1259 uint16_t subtype;
1260
1261 if (left < sizeof (uint16_t))
1262 {
1263 einfo (_("%P: warning: truncated CodeView type record"
1264 " LF_FIELDLIST\n"));
1265 return false;
1266 }
1267
1268 subtype = bfd_getl16 (ptr);
1269
1270 switch (subtype)
1271 {
1272 case LF_MEMBER:
1273 {
1274 struct lf_member *mem = (struct lf_member *) ptr;
1275 size_t name_len, subtype_len;
1276
1277 if (left < offsetof (struct lf_member, name))
1278 {
1279 einfo (_("%P: warning: truncated CodeView type record"
1280 " LF_MEMBER\n"));
1281 return false;
1282 }
1283
1284 if (!remap_type (&mem->type, map, type_num, num_types))
1285 return false;
1286
1287 name_len =
1288 strnlen (mem->name,
1289 left - offsetof (struct lf_member, name));
1290
1291 if (name_len == left - offsetof (struct lf_member, name))
1292 {
1293 einfo (_("%P: warning: name for LF_MEMBER has no"
1294 " terminating zero\n"));
1295 return false;
1296 }
1297
1298 name_len++;
1299
1300 subtype_len = offsetof (struct lf_member, name) + name_len;
1301
1302 if (subtype_len % 4 != 0)
1303 subtype_len += 4 - (subtype_len % 4);
1304
1305 if (left < subtype_len)
1306 {
1307 einfo (_("%P: warning: truncated CodeView type record"
1308 " LF_FIELDLIST\n"));
1309 return false;
1310 }
1311
1312 ptr += subtype_len;
1313 left -= subtype_len;
1314
1315 break;
1316 }
1317
1318 case LF_ENUMERATE:
1319 {
1320 struct lf_enumerate *en = (struct lf_enumerate *) ptr;
1321 size_t name_len, subtype_len;
1322 uint16_t val;
1323
1324 if (left < offsetof (struct lf_enumerate, name))
1325 {
1326 einfo (_("%P: warning: truncated CodeView type record"
1327 " LF_ENUMERATE\n"));
1328 return false;
1329 }
1330
1331 subtype_len = offsetof (struct lf_enumerate, name);
1332
1333 val = bfd_getl16 (&en->value);
1334
1335 /* If val >= 0x8000, the actual value immediately follows. */
1336 if (val >= 0x8000)
1337 {
1338 unsigned int param_len = extended_value_len (val);
1339
1340 if (param_len == 0)
1341 {
1342 einfo (_("%P: warning: unhandled type %v within"
1343 " LF_ENUMERATE\n"), val);
1344 return false;
1345 }
1346
1347 if (left < subtype_len + param_len)
1348 {
1349 einfo (_("%P: warning: truncated CodeView type"
1350 " record LF_ENUMERATE\n"));
1351 return false;
1352 }
1353
1354 subtype_len += param_len;
1355 }
1356
1357 name_len = strnlen ((char *) ptr + subtype_len,
1358 left - subtype_len);
1359
1360 if (name_len == left - offsetof (struct lf_enumerate, name))
1361 {
1362 einfo (_("%P: warning: name for LF_ENUMERATE has no"
1363 " terminating zero\n"));
1364 return false;
1365 }
1366
1367 name_len++;
1368
1369 subtype_len += name_len;
1370
1371 if (subtype_len % 4 != 0)
1372 subtype_len += 4 - (subtype_len % 4);
1373
1374 if (left < subtype_len)
1375 {
1376 einfo (_("%P: warning: truncated CodeView type record"
1377 " LF_ENUMERATE\n"));
1378 return false;
1379 }
1380
1381 ptr += subtype_len;
1382 left -= subtype_len;
1383
1384 break;
1385 }
1386
1387 case LF_INDEX:
1388 {
1389 struct lf_index *ind = (struct lf_index *) ptr;
1390
1391 if (left < sizeof (struct lf_index))
1392 {
1393 einfo (_("%P: warning: truncated CodeView type record"
1394 " LF_INDEX\n"));
1395 return false;
1396 }
1397
1398 if (!remap_type (&ind->index, map, type_num, num_types))
1399 return false;
1400
1401 ptr += sizeof (struct lf_index);
1402 left -= sizeof (struct lf_index);
1403
1404 break;
1405 }
1406
1407 case LF_ONEMETHOD:
1408 {
1409 struct lf_onemethod *meth = (struct lf_onemethod *) ptr;
1410 size_t name_len, subtype_len;
1411
1412 if (left < offsetof (struct lf_onemethod, name))
1413 {
1414 einfo (_("%P: warning: truncated CodeView type record"
1415 " LF_ONEMETHOD\n"));
1416 return false;
1417 }
1418
1419 if (!remap_type (&meth->method_type, map, type_num,
1420 num_types))
1421 return false;
1422
1423 name_len =
1424 strnlen (meth->name,
1425 left - offsetof (struct lf_onemethod, name));
1426
1427 if (name_len == left - offsetof (struct lf_onemethod, name))
1428 {
1429 einfo (_("%P: warning: name for LF_ONEMETHOD has no"
1430 " terminating zero\n"));
1431 return false;
1432 }
1433
1434 name_len++;
1435
1436 subtype_len = offsetof (struct lf_onemethod, name)
1437 + name_len;
1438
1439 if (subtype_len % 4 != 0)
1440 subtype_len += 4 - (subtype_len % 4);
1441
1442 if (left < subtype_len)
1443 {
1444 einfo (_("%P: warning: truncated CodeView type record"
1445 " LF_FIELDLIST\n"));
1446 return false;
1447 }
1448
1449 ptr += subtype_len;
1450 left -= subtype_len;
1451
1452 break;
1453 }
1454
1455 case LF_METHOD:
1456 {
1457 struct lf_method *meth = (struct lf_method *) ptr;
1458 size_t name_len, subtype_len;
1459
1460 if (left < offsetof (struct lf_method, name))
1461 {
1462 einfo (_("%P: warning: truncated CodeView type record"
1463 " LF_METHOD\n"));
1464 return false;
1465 }
1466
1467 if (!remap_type (&meth->method_list, map, type_num,
1468 num_types))
1469 return false;
1470
1471 name_len =
1472 strnlen (meth->name,
1473 left - offsetof (struct lf_method, name));
1474
1475 if (name_len == left - offsetof (struct lf_method, name))
1476 {
1477 einfo (_("%P: warning: name for LF_METHOD has no"
1478 " terminating zero\n"));
1479 return false;
1480 }
1481
1482 name_len++;
1483
1484 subtype_len = offsetof (struct lf_method, name) + name_len;
1485
1486 if (subtype_len % 4 != 0)
1487 subtype_len += 4 - (subtype_len % 4);
1488
1489 if (left < subtype_len)
1490 {
1491 einfo (_("%P: warning: truncated CodeView type record"
1492 " LF_FIELDLIST\n"));
1493 return false;
1494 }
1495
1496 ptr += subtype_len;
1497 left -= subtype_len;
1498
1499 break;
1500 }
1501
1502 case LF_BCLASS:
1503 {
1504 struct lf_bclass *bc = (struct lf_bclass *) ptr;
1505
1506 if (left < sizeof (struct lf_bclass))
1507 {
1508 einfo (_("%P: warning: truncated CodeView type record"
1509 " LF_BCLASS\n"));
1510 return false;
1511 }
1512
1513 if (!remap_type (&bc->base_class_type, map, type_num,
1514 num_types))
1515 return false;
1516
1517 ptr += sizeof (struct lf_bclass);
1518 left -= sizeof (struct lf_bclass);
1519
1520 break;
1521 }
1522
1523 case LF_VFUNCTAB:
1524 {
1525 struct lf_vfunctab *vft = (struct lf_vfunctab *) ptr;
1526
1527 if (left < sizeof (struct lf_vfunctab))
1528 {
1529 einfo (_("%P: warning: truncated CodeView type record"
1530 " LF_VFUNCTAB\n"));
1531 return false;
1532 }
1533
1534 if (!remap_type (&vft->type, map, type_num, num_types))
1535 return false;
1536
1537 ptr += sizeof (struct lf_vfunctab);
1538 left -= sizeof (struct lf_vfunctab);
1539
1540 break;
1541 }
1542
1543 case LF_VBCLASS:
1544 case LF_IVBCLASS:
1545 {
1546 struct lf_vbclass *vbc = (struct lf_vbclass *) ptr;
1547
1548 if (left < sizeof (struct lf_vbclass))
1549 {
1550 einfo (_("%P: warning: truncated CodeView type record"
1551 " LF_VBCLASS/LF_IVBCLASS\n"));
1552 return false;
1553 }
1554
1555 if (!remap_type (&vbc->base_class_type, map, type_num,
1556 num_types))
1557 return false;
1558
1559 if (!remap_type (&vbc->virtual_base_pointer_type, map,
1560 type_num, num_types))
1561 return false;
1562
1563 ptr += sizeof (struct lf_vbclass);
1564 left -= sizeof (struct lf_vbclass);
1565
1566 break;
1567 }
1568
1569 case LF_STMEMBER:
1570 {
1571 struct lf_static_member *st =
1572 (struct lf_static_member *) ptr;
1573 size_t name_len, subtype_len;
1574
1575 if (left < offsetof (struct lf_static_member, name))
1576 {
1577 einfo (_("%P: warning: truncated CodeView type record"
1578 " LF_STMEMBER\n"));
1579 return false;
1580 }
1581
1582 if (!remap_type (&st->type, map, type_num, num_types))
1583 return false;
1584
1585 name_len =
1586 strnlen (st->name,
1587 left - offsetof (struct lf_static_member, name));
1588
1589 if (name_len == left
1590 - offsetof (struct lf_static_member, name))
1591 {
1592 einfo (_("%P: warning: name for LF_STMEMBER has no"
1593 " terminating zero\n"));
1594 return false;
1595 }
1596
1597 name_len++;
1598
1599 subtype_len = offsetof (struct lf_static_member, name)
1600 + name_len;
1601
1602 if (subtype_len % 4 != 0)
1603 subtype_len += 4 - (subtype_len % 4);
1604
1605 if (left < subtype_len)
1606 {
1607 einfo (_("%P: warning: truncated CodeView type record"
1608 " LF_FIELDLIST\n"));
1609 return false;
1610 }
1611
1612 ptr += subtype_len;
1613 left -= subtype_len;
1614
1615 break;
1616 }
1617
1618 case LF_NESTTYPE:
1619 {
1620 struct lf_nest_type *nest = (struct lf_nest_type *) ptr;
1621 size_t name_len, subtype_len;
1622
1623 if (left < offsetof (struct lf_nest_type, name))
1624 {
1625 einfo (_("%P: warning: truncated CodeView type record"
1626 " LF_NESTTYPE\n"));
1627 return false;
1628 }
1629
1630 if (!remap_type (&nest->type, map, type_num, num_types))
1631 return false;
1632
1633 name_len =
1634 strnlen (nest->name,
1635 left - offsetof (struct lf_nest_type, name));
1636
1637 if (name_len == left - offsetof (struct lf_nest_type, name))
1638 {
1639 einfo (_("%P: warning: name for LF_NESTTYPE has no"
1640 " terminating zero\n"));
1641 return false;
1642 }
1643
1644 name_len++;
1645
1646 subtype_len = offsetof (struct lf_nest_type, name)
1647 + name_len;
1648
1649 if (subtype_len % 4 != 0)
1650 subtype_len += 4 - (subtype_len % 4);
1651
1652 if (left < subtype_len)
1653 {
1654 einfo (_("%P: warning: truncated CodeView type record"
1655 " LF_FIELDLIST\n"));
1656 return false;
1657 }
1658
1659 ptr += subtype_len;
1660 left -= subtype_len;
1661
1662 break;
1663 }
1664
1665 default:
1666 einfo (_("%P: warning: unrecognized CodeView subtype %v\n"),
1667 subtype);
1668 return false;
1669 }
1670 }
1671
1672 break;
1673 }
1674
1675 case LF_BITFIELD:
1676 {
1677 struct lf_bitfield *bf = (struct lf_bitfield *) data;
1678
1679 if (size < offsetof (struct lf_bitfield, length))
1680 {
1681 einfo (_("%P: warning: truncated CodeView type record"
1682 " LF_BITFIELD\n"));
1683 return false;
1684 }
1685
1686 if (!remap_type (&bf->base_type, map, type_num, num_types))
1687 return false;
1688
1689 break;
1690 }
1691
1692 case LF_METHODLIST:
1693 {
1694 struct lf_methodlist *ml = (struct lf_methodlist *) data;
1695 unsigned int num_entries;
1696
1697 if (size < offsetof (struct lf_methodlist, entries))
1698 {
1699 einfo (_("%P: warning: truncated CodeView type record"
1700 " LF_METHODLIST\n"));
1701 return false;
1702 }
1703
1704 if ((size - offsetof (struct lf_methodlist, entries))
1705 % sizeof (struct lf_methodlist_entry))
1706 {
1707 einfo (_("%P: warning: malformed CodeView type record"
1708 " LF_METHODLIST\n"));
1709 return false;
1710 }
1711
1712 num_entries = (size - offsetof (struct lf_methodlist, entries))
1713 / sizeof (struct lf_methodlist_entry);
1714
1715 for (unsigned int i = 0; i < num_entries; i++)
1716 {
1717 if (!remap_type (&ml->entries[i].method_type, map,
1718 type_num, num_types))
1719 return false;
1720 }
1721
1722 break;
1723 }
1724
1725 case LF_ARRAY:
1726 {
1727 struct lf_array *arr = (struct lf_array *) data;
1728
1729 if (size < offsetof (struct lf_array, length_in_bytes))
1730 {
1731 einfo (_("%P: warning: truncated CodeView type record"
1732 " LF_ARRAY\n"));
1733 return false;
1734 }
1735
1736 if (!remap_type (&arr->element_type, map, type_num, num_types))
1737 return false;
1738
1739 if (!remap_type (&arr->index_type, map, type_num, num_types))
1740 return false;
1741
1742 break;
1743 }
1744
1745 case LF_CLASS:
1746 case LF_STRUCTURE:
1747 {
1748 struct lf_class *cl = (struct lf_class *) data;
1749 uint16_t prop;
1750 size_t name_len;
1751
1752 if (size < offsetof (struct lf_class, name))
1753 {
1754 einfo (_("%P: warning: truncated CodeView type record"
1755 " LF_CLASS/LF_STRUCTURE\n"));
1756 return false;
1757 }
1758
1759 if (!remap_type (&cl->field_list, map, type_num, num_types))
1760 return false;
1761
1762 if (!remap_type (&cl->derived_from, map, type_num, num_types))
1763 return false;
1764
1765 if (!remap_type (&cl->vshape, map, type_num, num_types))
1766 return false;
1767
1768 name_len = strnlen (cl->name, size - offsetof (struct lf_class, name));
1769
1770 if (name_len == size - offsetof (struct lf_class, name))
1771 {
1772 einfo (_("%P: warning: name for LF_CLASS/LF_STRUCTURE has no"
1773 " terminating zero\n"));
1774 return false;
1775 }
1776
1777 prop = bfd_getl16 (&cl->properties);
1778
1779 if (prop & CV_PROP_HAS_UNIQUE_NAME)
1780 {
1781 /* Structure has another name following first one. */
1782
1783 size_t len = offsetof (struct lf_class, name) + name_len + 1;
1784 size_t unique_name_len;
1785
1786 unique_name_len = strnlen (cl->name + name_len + 1, size - len);
1787
1788 if (unique_name_len == size - len)
1789 {
1790 einfo (_("%P: warning: unique name for LF_CLASS/LF_STRUCTURE"
1791 " has no terminating zero\n"));
1792 return false;
1793 }
1794 }
1795
1796 if (!(prop & (CV_PROP_FORWARD_REF | CV_PROP_SCOPED))
1797 && !is_name_anonymous (cl->name, name_len))
1798 {
1799 other_hash = true;
1800 cv_hash = crc32 ((uint8_t *) cl->name, name_len);
1801 }
1802
1803 break;
1804 }
1805
1806 case LF_UNION:
1807 {
1808 struct lf_union *un = (struct lf_union *) data;
1809 uint16_t prop;
1810 size_t name_len;
1811
1812 if (size < offsetof (struct lf_union, name))
1813 {
1814 einfo (_("%P: warning: truncated CodeView type record"
1815 " LF_UNION\n"));
1816 return false;
1817 }
1818
1819 if (!remap_type (&un->field_list, map, type_num, num_types))
1820 return false;
1821
1822 name_len = strnlen (un->name, size - offsetof (struct lf_union, name));
1823
1824 if (name_len == size - offsetof (struct lf_union, name))
1825 {
1826 einfo (_("%P: warning: name for LF_UNION has no"
1827 " terminating zero\n"));
1828 return false;
1829 }
1830
1831 prop = bfd_getl16 (&un->properties);
1832
1833 if (prop & CV_PROP_HAS_UNIQUE_NAME)
1834 {
1835 /* Structure has another name following first one. */
1836
1837 size_t len = offsetof (struct lf_union, name) + name_len + 1;
1838 size_t unique_name_len;
1839
1840 unique_name_len = strnlen (un->name + name_len + 1, size - len);
1841
1842 if (unique_name_len == size - len)
1843 {
1844 einfo (_("%P: warning: unique name for LF_UNION has"
1845 " no terminating zero\n"));
1846 return false;
1847 }
1848 }
1849
1850 if (!(prop & (CV_PROP_FORWARD_REF | CV_PROP_SCOPED))
1851 && !is_name_anonymous (un->name, name_len))
1852 {
1853 other_hash = true;
1854 cv_hash = crc32 ((uint8_t *) un->name, name_len);
1855 }
1856
1857 break;
1858 }
1859
1860 case LF_ENUM:
1861 {
1862 struct lf_enum *en = (struct lf_enum *) data;
1863 uint16_t prop;
1864 size_t name_len;
1865
1866 if (size < offsetof (struct lf_enum, name))
1867 {
1868 einfo (_("%P: warning: truncated CodeView type record"
1869 " LF_ENUM\n"));
1870 return false;
1871 }
1872
1873 if (!remap_type (&en->underlying_type, map, type_num, num_types))
1874 return false;
1875
1876 if (!remap_type (&en->field_list, map, type_num, num_types))
1877 return false;
1878
1879 name_len = strnlen (en->name, size - offsetof (struct lf_enum, name));
1880
1881 if (name_len == size - offsetof (struct lf_enum, name))
1882 {
1883 einfo (_("%P: warning: name for LF_ENUM has no"
1884 " terminating zero\n"));
1885 return false;
1886 }
1887
1888 prop = bfd_getl16 (&en->properties);
1889
1890 if (prop & CV_PROP_HAS_UNIQUE_NAME)
1891 {
1892 /* Structure has another name following first one. */
1893
1894 size_t len = offsetof (struct lf_enum, name) + name_len + 1;
1895 size_t unique_name_len;
1896
1897 unique_name_len = strnlen (en->name + name_len + 1, size - len);
1898
1899 if (unique_name_len == size - len)
1900 {
1901 einfo (_("%P: warning: unique name for LF_ENUM has"
1902 " no terminating zero\n"));
1903 return false;
1904 }
1905 }
1906
1907 break;
1908 }
1909
1910 case LF_VTSHAPE:
1911 /* Does not reference any types, nothing to be done. */
1912 break;
1913
1914 default:
1915 einfo (_("%P: warning: unrecognized CodeView type %v\n"), type);
1916 return false;
1917 }
1918
1919 hash = iterative_hash (data, size, 0);
1920
1921 slot = htab_find_slot_with_hash (types->hashmap, data, hash, INSERT);
1922 if (!slot)
1923 return false;
1924
1925 if (!*slot) /* new entry */
1926 {
1927 struct type_entry *e;
1928
1929 *slot = xmalloc (offsetof (struct type_entry, data) + size);
1930
1931 e = (struct type_entry *) *slot;
1932
1933 e->next = NULL;
1934 e->index = types->num_types;
1935
1936 if (other_hash)
1937 e->cv_hash = cv_hash;
1938 else
1939 e->cv_hash = crc32 (data, size);
1940
1941 memcpy (e->data, data, size);
1942
1943 if (types->last)
1944 types->last->next = e;
1945 else
1946 types->first = e;
1947
1948 types->last = e;
1949
1950 map[type_num] = e;
1951
1952 types->num_types++;
1953 }
1954 else /* duplicate */
1955 {
1956 map[type_num] = (struct type_entry *) *slot;
1957 }
1958
1959 return true;
1960 }
1961
1962 /* Parse the .debug$T section of a module, and pass any type definitions
1963 found to handle_type. */
1964 static bool
1965 handle_debugt_section (asection *s, bfd *mod, struct types *types)
1966 {
1967 bfd_byte *data = NULL;
1968 size_t off;
1969 unsigned int num_types = 0;
1970 struct type_entry **map;
1971 uint32_t type_num;
1972
1973 if (!bfd_get_full_section_contents (mod, s, &data))
1974 return false;
1975
1976 if (!data)
1977 return false;
1978
1979 if (bfd_getl32 (data) != CV_SIGNATURE_C13)
1980 {
1981 free (data);
1982 return true;
1983 }
1984
1985 off = sizeof (uint32_t);
1986
1987 while (off + sizeof (uint16_t) <= s->size)
1988 {
1989 uint16_t size;
1990
1991 size = bfd_getl16 (data + off);
1992 off += sizeof (uint16_t);
1993
1994 if (size + off > s->size || size <= sizeof (uint16_t))
1995 {
1996 free (data);
1997 bfd_set_error (bfd_error_bad_value);
1998 return false;
1999 }
2000
2001 num_types++;
2002 off += size;
2003 }
2004
2005 if (num_types == 0)
2006 {
2007 free (data);
2008 return true;
2009 }
2010
2011 map = xcalloc (num_types, sizeof (struct type_entry *));
2012
2013 off = sizeof (uint32_t);
2014 type_num = 0;
2015
2016 while (off + sizeof (uint16_t) <= s->size)
2017 {
2018 uint16_t size;
2019
2020 size = bfd_getl16 (data + off);
2021
2022 if (!handle_type (data + off, map, type_num, num_types, types))
2023 {
2024 free (data);
2025 free (map);
2026 bfd_set_error (bfd_error_bad_value);
2027 return false;
2028 }
2029
2030 off += sizeof (uint16_t) + size;
2031 type_num++;
2032 }
2033
2034 free (data);
2035 free (map);
2036
2037 return true;
2038 }
2039
2040 /* Populate the module stream, which consists of the transformed .debug$S
2041 data for each object file. */
2042 static bool
2043 populate_module_stream (bfd *stream, bfd *mod, uint32_t *sym_byte_size,
2044 struct string_table *strings,
2045 uint32_t *c13_info_size,
2046 struct mod_source_files *mod_source,
2047 bfd *abfd, struct types *types)
2048 {
2049 uint8_t int_buf[sizeof (uint32_t)];
2050 uint8_t *c13_info = NULL;
2051
2052 *sym_byte_size = sizeof (uint32_t);
2053 *c13_info_size = 0;
2054
2055 /* Process .debug$S section(s). */
2056
2057 for (asection *s = mod->sections; s; s = s->next)
2058 {
2059 if (!strcmp (s->name, ".debug$S") && s->size >= sizeof (uint32_t))
2060 {
2061 if (!handle_debugs_section (s, mod, strings, &c13_info,
2062 c13_info_size, mod_source, abfd))
2063 {
2064 free (c13_info);
2065 free (mod_source->files);
2066 return false;
2067 }
2068 }
2069 else if (!strcmp (s->name, ".debug$T") && s->size >= sizeof (uint32_t))
2070 {
2071 if (!handle_debugt_section (s, mod, types))
2072 {
2073 free (c13_info);
2074 free (mod_source->files);
2075 return false;
2076 }
2077 }
2078 }
2079
2080 /* Write the signature. */
2081
2082 bfd_putl32 (CV_SIGNATURE_C13, int_buf);
2083
2084 if (bfd_bwrite (int_buf, sizeof (uint32_t), stream) != sizeof (uint32_t))
2085 {
2086 free (c13_info);
2087 return false;
2088 }
2089
2090 if (c13_info)
2091 {
2092 if (bfd_bwrite (c13_info, *c13_info_size, stream) != *c13_info_size)
2093 {
2094 free (c13_info);
2095 return false;
2096 }
2097
2098 free (c13_info);
2099 }
2100
2101 /* Write the global refs size. */
2102
2103 bfd_putl32 (0, int_buf);
2104
2105 if (bfd_bwrite (int_buf, sizeof (uint32_t), stream) != sizeof (uint32_t))
2106 return false;
2107
2108 return true;
2109 }
2110
2111 /* Create the module info substream within the DBI. */
2112 static bool
2113 create_module_info_substream (bfd *abfd, bfd *pdb, void **data,
2114 uint32_t *size, struct string_table *strings,
2115 struct source_files_info *source,
2116 struct types *types)
2117 {
2118 uint8_t *ptr;
2119 unsigned int mod_num;
2120
2121 static const char linker_fn[] = "* Linker *";
2122
2123 *size = 0;
2124
2125 for (bfd *in = coff_data (abfd)->link_info->input_bfds; in;
2126 in = in->link.next)
2127 {
2128 size_t len = sizeof (struct module_info);
2129
2130 if (!strcmp (bfd_get_filename (in), "dll stuff"))
2131 {
2132 len += sizeof (linker_fn); /* Object name. */
2133 len++; /* Empty module name. */
2134 }
2135 else if (in->my_archive)
2136 {
2137 char *name = lrealpath (bfd_get_filename (in));
2138
2139 len += strlen (name) + 1; /* Object name. */
2140
2141 free (name);
2142
2143 name = lrealpath (bfd_get_filename (in->my_archive));
2144
2145 len += strlen (name) + 1; /* Archive name. */
2146
2147 free (name);
2148 }
2149 else
2150 {
2151 char *name = lrealpath (bfd_get_filename (in));
2152 size_t name_len = strlen (name) + 1;
2153
2154 len += name_len; /* Object name. */
2155 len += name_len; /* And again as the archive name. */
2156
2157 free (name);
2158 }
2159
2160 if (len % 4)
2161 len += 4 - (len % 4);
2162
2163 *size += len;
2164
2165 source->mod_count++;
2166 }
2167
2168 *data = xmalloc (*size);
2169
2170 ptr = *data;
2171
2172 source->mods = xmalloc (source->mod_count
2173 * sizeof (struct mod_source_files));
2174 memset (source->mods, 0,
2175 source->mod_count * sizeof (struct mod_source_files));
2176
2177 mod_num = 0;
2178
2179 for (bfd *in = coff_data (abfd)->link_info->input_bfds; in;
2180 in = in->link.next)
2181 {
2182 struct module_info *mod = (struct module_info *) ptr;
2183 uint16_t stream_num;
2184 bfd *stream;
2185 uint32_t sym_byte_size, c13_info_size;
2186 uint8_t *start = ptr;
2187
2188 stream = add_stream (pdb, NULL, &stream_num);
2189
2190 if (!stream)
2191 {
2192 for (unsigned int i = 0; i < source->mod_count; i++)
2193 {
2194 free (source->mods[i].files);
2195 }
2196
2197 free (source->mods);
2198 free (*data);
2199 return false;
2200 }
2201
2202 if (!populate_module_stream (stream, in, &sym_byte_size,
2203 strings, &c13_info_size,
2204 &source->mods[mod_num], abfd,
2205 types))
2206 {
2207 for (unsigned int i = 0; i < source->mod_count; i++)
2208 {
2209 free (source->mods[i].files);
2210 }
2211
2212 free (source->mods);
2213 free (*data);
2214 return false;
2215 }
2216
2217 bfd_putl32 (0, &mod->unused1);
2218
2219 /* These are dummy values - MSVC copies the first section contribution
2220 entry here, but doesn't seem to use it for anything. */
2221 bfd_putl16 (0xffff, &mod->sc.section);
2222 bfd_putl16 (0, &mod->sc.padding1);
2223 bfd_putl32 (0, &mod->sc.offset);
2224 bfd_putl32 (0xffffffff, &mod->sc.size);
2225 bfd_putl32 (0, &mod->sc.characteristics);
2226 bfd_putl16 (0xffff, &mod->sc.module_index);
2227 bfd_putl16 (0, &mod->sc.padding2);
2228 bfd_putl32 (0, &mod->sc.data_crc);
2229 bfd_putl32 (0, &mod->sc.reloc_crc);
2230
2231 bfd_putl16 (0, &mod->flags);
2232 bfd_putl16 (stream_num, &mod->module_sym_stream);
2233 bfd_putl32 (sym_byte_size, &mod->sym_byte_size);
2234 bfd_putl32 (0, &mod->c11_byte_size);
2235 bfd_putl32 (c13_info_size, &mod->c13_byte_size);
2236 bfd_putl16 (0, &mod->source_file_count);
2237 bfd_putl16 (0, &mod->padding);
2238 bfd_putl32 (0, &mod->unused2);
2239 bfd_putl32 (0, &mod->source_file_name_index);
2240 bfd_putl32 (0, &mod->pdb_file_path_name_index);
2241
2242 ptr += sizeof (struct module_info);
2243
2244 if (!strcmp (bfd_get_filename (in), "dll stuff"))
2245 {
2246 /* Object name. */
2247 memcpy (ptr, linker_fn, sizeof (linker_fn));
2248 ptr += sizeof (linker_fn);
2249
2250 /* Empty module name. */
2251 *ptr = 0;
2252 ptr++;
2253 }
2254 else if (in->my_archive)
2255 {
2256 char *name = lrealpath (bfd_get_filename (in));
2257 size_t name_len = strlen (name) + 1;
2258
2259 /* Object name. */
2260 memcpy (ptr, name, name_len);
2261 ptr += name_len;
2262
2263 free (name);
2264
2265 name = lrealpath (bfd_get_filename (in->my_archive));
2266 name_len = strlen (name) + 1;
2267
2268 /* Archive name. */
2269 memcpy (ptr, name, name_len);
2270 ptr += name_len;
2271
2272 free (name);
2273 }
2274 else
2275 {
2276 char *name = lrealpath (bfd_get_filename (in));
2277 size_t name_len = strlen (name) + 1;
2278
2279 /* Object name. */
2280 memcpy (ptr, name, name_len);
2281 ptr += name_len;
2282
2283 /* Object name again as archive name. */
2284 memcpy (ptr, name, name_len);
2285 ptr += name_len;
2286
2287 free (name);
2288 }
2289
2290 /* Pad to next four-byte boundary. */
2291
2292 if ((ptr - start) % 4)
2293 {
2294 memset (ptr, 0, 4 - ((ptr - start) % 4));
2295 ptr += 4 - ((ptr - start) % 4);
2296 }
2297
2298 mod_num++;
2299 }
2300
2301 return true;
2302 }
2303
2304 /* Return the index of a given output section. */
2305 static uint16_t
2306 find_section_number (bfd *abfd, asection *sect)
2307 {
2308 uint16_t i = 1;
2309
2310 for (asection *s = abfd->sections; s; s = s->next)
2311 {
2312 if (s == sect)
2313 return i;
2314
2315 /* Empty sections aren't output. */
2316 if (s->size != 0)
2317 i++;
2318 }
2319
2320 return 0;
2321 }
2322
2323 /* Create the substream which maps addresses in the image file to locations
2324 in the original object files. */
2325 static bool
2326 create_section_contrib_substream (bfd *abfd, void **data, uint32_t *size)
2327 {
2328 unsigned int num_sc = 0;
2329 struct section_contribution *sc;
2330 uint16_t mod_index;
2331 char *sect_flags;
2332 file_ptr offset;
2333
2334 for (bfd *in = coff_data (abfd)->link_info->input_bfds; in;
2335 in = in->link.next)
2336 {
2337 for (asection *s = in->sections; s; s = s->next)
2338 {
2339 if (s->size == 0 || discarded_section (s))
2340 continue;
2341
2342 num_sc++;
2343 }
2344 }
2345
2346 *size = sizeof (uint32_t) + (num_sc * sizeof (struct section_contribution));
2347 *data = xmalloc (*size);
2348
2349 bfd_putl32 (SECTION_CONTRIB_VERSION_60, *data);
2350
2351 /* Read characteristics of outputted sections. */
2352
2353 sect_flags = xmalloc (sizeof (uint32_t) * abfd->section_count);
2354
2355 offset = bfd_coff_filhsz (abfd) + bfd_coff_aoutsz (abfd);
2356 offset += offsetof (struct external_scnhdr, s_flags);
2357
2358 for (unsigned int i = 0; i < abfd->section_count; i++)
2359 {
2360 bfd_seek (abfd, offset, SEEK_SET);
2361
2362 if (bfd_bread (sect_flags + (i * sizeof (uint32_t)), sizeof (uint32_t),
2363 abfd) != sizeof (uint32_t))
2364 {
2365 free (*data);
2366 free (sect_flags);
2367 return false;
2368 }
2369
2370 offset += sizeof (struct external_scnhdr);
2371 }
2372
2373 sc =
2374 (struct section_contribution *) ((uint8_t *) *data + sizeof (uint32_t));
2375
2376 mod_index = 0;
2377 for (bfd *in = coff_data (abfd)->link_info->input_bfds; in;
2378 in = in->link.next)
2379 {
2380 for (asection *s = in->sections; s; s = s->next)
2381 {
2382 uint16_t sect_num;
2383
2384 if (s->size == 0 || discarded_section (s))
2385 continue;
2386
2387 sect_num = find_section_number (abfd, s->output_section);
2388
2389 memcpy (&sc->characteristics,
2390 sect_flags + ((sect_num - 1) * sizeof (uint32_t)),
2391 sizeof (uint32_t));
2392
2393 bfd_putl16 (sect_num, &sc->section);
2394 bfd_putl16 (0, &sc->padding1);
2395 bfd_putl32 (s->output_offset, &sc->offset);
2396 bfd_putl32 (s->size, &sc->size);
2397 bfd_putl16 (mod_index, &sc->module_index);
2398 bfd_putl16 (0, &sc->padding2);
2399 bfd_putl32 (0, &sc->data_crc);
2400 bfd_putl32 (0, &sc->reloc_crc);
2401
2402 sc++;
2403 }
2404
2405 mod_index++;
2406 }
2407
2408 free (sect_flags);
2409
2410 return true;
2411 }
2412
2413 /* The source info substream lives within the DBI stream, and lists the
2414 source files for each object file (i.e. it's derived from the
2415 DEBUG_S_FILECHKSMS parts of the .debug$S sections). This is a bit
2416 superfluous, as the filenames are also available in the C13 parts of
2417 the module streams, but MSVC relies on it to work properly. */
2418 static void
2419 create_source_info_substream (void **data, uint32_t *size,
2420 struct source_files_info *source)
2421 {
2422 uint16_t dedupe_source_files_count = 0;
2423 uint16_t source_files_count = 0;
2424 uint32_t strings_len = 0;
2425 uint8_t *ptr;
2426
2427 /* Loop through the source files, marking unique filenames. The pointers
2428 here are for entries in the main string table, and so have already
2429 been deduplicated. */
2430
2431 for (uint16_t i = 0; i < source->mod_count; i++)
2432 {
2433 for (uint16_t j = 0; j < source->mods[i].files_count; j++)
2434 {
2435 if (source->mods[i].files[j])
2436 {
2437 if (source->mods[i].files[j]->source_file_offset == 0xffffffff)
2438 {
2439 source->mods[i].files[j]->source_file_offset = strings_len;
2440 strings_len += source->mods[i].files[j]->len + 1;
2441 dedupe_source_files_count++;
2442 }
2443
2444 source_files_count++;
2445 }
2446 }
2447 }
2448
2449 *size = sizeof (uint16_t) + sizeof (uint16_t);
2450 *size += (sizeof (uint16_t) + sizeof (uint16_t)) * source->mod_count;
2451 *size += sizeof (uint32_t) * source_files_count;
2452 *size += strings_len;
2453
2454 *data = xmalloc (*size);
2455
2456 ptr = (uint8_t *) *data;
2457
2458 /* Write header (module count and source file count). */
2459
2460 bfd_putl16 (source->mod_count, ptr);
2461 ptr += sizeof (uint16_t);
2462
2463 bfd_putl16 (dedupe_source_files_count, ptr);
2464 ptr += sizeof (uint16_t);
2465
2466 /* Write "ModIndices". As the LLVM documentation puts it, "this array is
2467 present, but does not appear to be useful". */
2468
2469 for (uint16_t i = 0; i < source->mod_count; i++)
2470 {
2471 bfd_putl16 (i, ptr);
2472 ptr += sizeof (uint16_t);
2473 }
2474
2475 /* Write source file count for each module. */
2476
2477 for (uint16_t i = 0; i < source->mod_count; i++)
2478 {
2479 bfd_putl16 (source->mods[i].files_count, ptr);
2480 ptr += sizeof (uint16_t);
2481 }
2482
2483 /* For each module, write the offsets within the string table
2484 for each source file. */
2485
2486 for (uint16_t i = 0; i < source->mod_count; i++)
2487 {
2488 for (uint16_t j = 0; j < source->mods[i].files_count; j++)
2489 {
2490 if (source->mods[i].files[j])
2491 {
2492 bfd_putl32 (source->mods[i].files[j]->source_file_offset, ptr);
2493 ptr += sizeof (uint32_t);
2494 }
2495 }
2496 }
2497
2498 /* Write the string table. We set source_file_offset to a dummy value for
2499 each entry we write, so we don't write duplicate filenames. */
2500
2501 for (uint16_t i = 0; i < source->mod_count; i++)
2502 {
2503 for (uint16_t j = 0; j < source->mods[i].files_count; j++)
2504 {
2505 if (source->mods[i].files[j]
2506 && source->mods[i].files[j]->source_file_offset != 0xffffffff)
2507 {
2508 memcpy (ptr, source->mods[i].files[j]->s,
2509 source->mods[i].files[j]->len);
2510 ptr += source->mods[i].files[j]->len;
2511
2512 *ptr = 0;
2513 ptr++;
2514
2515 source->mods[i].files[j]->source_file_offset = 0xffffffff;
2516 }
2517 }
2518 }
2519 }
2520
2521 /* Stream 4 is the debug information (DBI) stream. */
2522 static bool
2523 populate_dbi_stream (bfd *stream, bfd *abfd, bfd *pdb,
2524 uint16_t section_header_stream_num,
2525 uint16_t sym_rec_stream_num,
2526 uint16_t publics_stream_num,
2527 struct string_table *strings,
2528 struct types *types)
2529 {
2530 struct pdb_dbi_stream_header h;
2531 struct optional_dbg_header opt;
2532 void *mod_info, *sc, *source_info;
2533 uint32_t mod_info_size, sc_size, source_info_size;
2534 struct source_files_info source;
2535
2536 source.mod_count = 0;
2537 source.mods = NULL;
2538
2539 if (!create_module_info_substream (abfd, pdb, &mod_info, &mod_info_size,
2540 strings, &source, types))
2541 return false;
2542
2543 if (!create_section_contrib_substream (abfd, &sc, &sc_size))
2544 {
2545 for (unsigned int i = 0; i < source.mod_count; i++)
2546 {
2547 free (source.mods[i].files);
2548 }
2549 free (source.mods);
2550
2551 free (mod_info);
2552 return false;
2553 }
2554
2555 create_source_info_substream (&source_info, &source_info_size, &source);
2556
2557 for (unsigned int i = 0; i < source.mod_count; i++)
2558 {
2559 free (source.mods[i].files);
2560 }
2561 free (source.mods);
2562
2563 bfd_putl32 (0xffffffff, &h.version_signature);
2564 bfd_putl32 (DBI_STREAM_VERSION_70, &h.version_header);
2565 bfd_putl32 (1, &h.age);
2566 bfd_putl16 (0xffff, &h.global_stream_index);
2567 bfd_putl16 (0x8e1d, &h.build_number); // MSVC 14.29
2568 bfd_putl16 (publics_stream_num, &h.public_stream_index);
2569 bfd_putl16 (0, &h.pdb_dll_version);
2570 bfd_putl16 (sym_rec_stream_num, &h.sym_record_stream);
2571 bfd_putl16 (0, &h.pdb_dll_rbld);
2572 bfd_putl32 (mod_info_size, &h.mod_info_size);
2573 bfd_putl32 (sc_size, &h.section_contribution_size);
2574 bfd_putl32 (0, &h.section_map_size);
2575 bfd_putl32 (source_info_size, &h.source_info_size);
2576 bfd_putl32 (0, &h.type_server_map_size);
2577 bfd_putl32 (0, &h.mfc_type_server_index);
2578 bfd_putl32 (sizeof (opt), &h.optional_dbg_header_size);
2579 bfd_putl32 (0, &h.ec_substream_size);
2580 bfd_putl16 (0, &h.flags);
2581 bfd_putl16 (get_arch_number (abfd), &h.machine);
2582 bfd_putl32 (0, &h.padding);
2583
2584 if (bfd_bwrite (&h, sizeof (h), stream) != sizeof (h))
2585 {
2586 free (source_info);
2587 free (sc);
2588 free (mod_info);
2589 return false;
2590 }
2591
2592 if (bfd_bwrite (mod_info, mod_info_size, stream) != mod_info_size)
2593 {
2594 free (source_info);
2595 free (sc);
2596 free (mod_info);
2597 return false;
2598 }
2599
2600 free (mod_info);
2601
2602 if (bfd_bwrite (sc, sc_size, stream) != sc_size)
2603 {
2604 free (source_info);
2605 free (sc);
2606 return false;
2607 }
2608
2609 free (sc);
2610
2611 if (bfd_bwrite (source_info, source_info_size, stream) != source_info_size)
2612 {
2613 free (source_info);
2614 return false;
2615 }
2616
2617 free (source_info);
2618
2619 bfd_putl16 (0xffff, &opt.fpo_stream);
2620 bfd_putl16 (0xffff, &opt.exception_stream);
2621 bfd_putl16 (0xffff, &opt.fixup_stream);
2622 bfd_putl16 (0xffff, &opt.omap_to_src_stream);
2623 bfd_putl16 (0xffff, &opt.omap_from_src_stream);
2624 bfd_putl16 (section_header_stream_num, &opt.section_header_stream);
2625 bfd_putl16 (0xffff, &opt.token_map_stream);
2626 bfd_putl16 (0xffff, &opt.xdata_stream);
2627 bfd_putl16 (0xffff, &opt.pdata_stream);
2628 bfd_putl16 (0xffff, &opt.new_fpo_stream);
2629 bfd_putl16 (0xffff, &opt.orig_section_header_stream);
2630
2631 if (bfd_bwrite (&opt, sizeof (opt), stream) != sizeof (opt))
2632 return false;
2633
2634 return true;
2635 }
2636
2637 /* Used as parameter to qsort, to sort publics by hash. */
2638 static int
2639 public_compare_hash (const void *s1, const void *s2)
2640 {
2641 const struct public *p1 = *(const struct public **) s1;
2642 const struct public *p2 = *(const struct public **) s2;
2643
2644 if (p1->hash < p2->hash)
2645 return -1;
2646 if (p1->hash > p2->hash)
2647 return 1;
2648
2649 return 0;
2650 }
2651
2652 /* Used as parameter to qsort, to sort publics by address. */
2653 static int
2654 public_compare_addr (const void *s1, const void *s2)
2655 {
2656 const struct public *p1 = *(const struct public **) s1;
2657 const struct public *p2 = *(const struct public **) s2;
2658
2659 if (p1->section < p2->section)
2660 return -1;
2661 if (p1->section > p2->section)
2662 return 1;
2663
2664 if (p1->address < p2->address)
2665 return -1;
2666 if (p1->address > p2->address)
2667 return 1;
2668
2669 return 0;
2670 }
2671
2672 /* The publics stream is a hash map of S_PUB32 records, which are stored
2673 in the symbol record stream. Each S_PUB32 entry represents a symbol
2674 from the point of view of the linker: a section index, an offset within
2675 the section, and a mangled name. Compare with S_GDATA32 and S_GPROC32,
2676 which are the same thing but generated by the compiler. */
2677 static bool
2678 populate_publics_stream (bfd *stream, bfd *abfd, bfd *sym_rec_stream)
2679 {
2680 struct publics_header header;
2681 struct globals_hash_header hash_header;
2682 const unsigned int num_buckets = 4096;
2683 unsigned int num_entries = 0, filled_buckets = 0;
2684 unsigned int buckets_size, sym_hash_size;
2685 char int_buf[sizeof (uint32_t)];
2686 struct public *publics_head = NULL, *publics_tail = NULL;
2687 struct public **buckets;
2688 struct public **sorted = NULL;
2689 bool ret = false;
2690
2691 buckets = xmalloc (sizeof (struct public *) * num_buckets);
2692 memset (buckets, 0, sizeof (struct public *) * num_buckets);
2693
2694 /* Loop through the global symbols in our input files, and write S_PUB32
2695 records in the symbol record stream for those that make it into the
2696 final image. */
2697 for (bfd *in = coff_data (abfd)->link_info->input_bfds; in;
2698 in = in->link.next)
2699 {
2700 if (!in->outsymbols)
2701 continue;
2702
2703 for (unsigned int i = 0; i < in->symcount; i++)
2704 {
2705 struct bfd_symbol *sym = in->outsymbols[i];
2706
2707 if (sym->flags & BSF_GLOBAL)
2708 {
2709 struct pubsym ps;
2710 uint16_t record_length;
2711 const char *name = sym->name;
2712 size_t name_len = strlen (name);
2713 struct public *p = xmalloc (sizeof (struct public));
2714 unsigned int padding = 0;
2715 uint16_t section;
2716 uint32_t flags = 0;
2717
2718 section =
2719 find_section_number (abfd, sym->section->output_section);
2720
2721 if (section == 0)
2722 continue;
2723
2724 p->next = NULL;
2725 p->offset = bfd_tell (sym_rec_stream);
2726 p->hash = calc_hash (name, name_len) % num_buckets;
2727 p->section = section;
2728 p->address = sym->section->output_offset + sym->value;
2729
2730 record_length = sizeof (struct pubsym) + name_len + 1;
2731
2732 if (record_length % 4)
2733 padding = 4 - (record_length % 4);
2734
2735 /* Assume that all global symbols in executable sections
2736 are functions. */
2737 if (sym->section->flags & SEC_CODE)
2738 flags = PUBSYM_FUNCTION;
2739
2740 bfd_putl16 (record_length + padding - sizeof (uint16_t),
2741 &ps.record_length);
2742 bfd_putl16 (S_PUB32, &ps.record_type);
2743 bfd_putl32 (flags, &ps.flags);
2744 bfd_putl32 (p->address, &ps.offset);
2745 bfd_putl16 (p->section, &ps.section);
2746
2747 if (bfd_bwrite (&ps, sizeof (struct pubsym), sym_rec_stream) !=
2748 sizeof (struct pubsym))
2749 goto end;
2750
2751 if (bfd_bwrite (name, name_len + 1, sym_rec_stream) !=
2752 name_len + 1)
2753 goto end;
2754
2755 for (unsigned int j = 0; j < padding; j++)
2756 {
2757 uint8_t b = 0;
2758
2759 if (bfd_bwrite (&b, sizeof (uint8_t), sym_rec_stream) !=
2760 sizeof (uint8_t))
2761 goto end;
2762 }
2763
2764 if (!publics_head)
2765 publics_head = p;
2766 else
2767 publics_tail->next = p;
2768
2769 publics_tail = p;
2770 num_entries++;
2771 }
2772 }
2773 }
2774
2775
2776 if (num_entries > 0)
2777 {
2778 /* Create an array of pointers, sorted by hash value. */
2779
2780 sorted = xmalloc (sizeof (struct public *) * num_entries);
2781
2782 struct public *p = publics_head;
2783 for (unsigned int i = 0; i < num_entries; i++)
2784 {
2785 sorted[i] = p;
2786 p = p->next;
2787 }
2788
2789 qsort (sorted, num_entries, sizeof (struct public *),
2790 public_compare_hash);
2791
2792 /* Populate the buckets. */
2793
2794 for (unsigned int i = 0; i < num_entries; i++)
2795 {
2796 if (!buckets[sorted[i]->hash])
2797 {
2798 buckets[sorted[i]->hash] = sorted[i];
2799 filled_buckets++;
2800 }
2801
2802 sorted[i]->index = i;
2803 }
2804 }
2805
2806 buckets_size = num_buckets / 8;
2807 buckets_size += sizeof (uint32_t);
2808 buckets_size += filled_buckets * sizeof (uint32_t);
2809
2810 sym_hash_size = sizeof (hash_header);
2811 sym_hash_size += num_entries * sizeof (struct hash_record);
2812 sym_hash_size += buckets_size;
2813
2814 /* Output the publics header. */
2815
2816 bfd_putl32 (sym_hash_size, &header.sym_hash_size);
2817 bfd_putl32 (num_entries * sizeof (uint32_t), &header.addr_map_size);
2818 bfd_putl32 (0, &header.num_thunks);
2819 bfd_putl32 (0, &header.thunks_size);
2820 bfd_putl32 (0, &header.thunk_table);
2821 bfd_putl32 (0, &header.thunk_table_offset);
2822 bfd_putl32 (0, &header.num_sects);
2823
2824 if (bfd_bwrite (&header, sizeof (header), stream) != sizeof (header))
2825 goto end;
2826
2827 /* Output the global hash header. */
2828
2829 bfd_putl32 (GLOBALS_HASH_SIGNATURE, &hash_header.signature);
2830 bfd_putl32 (GLOBALS_HASH_VERSION_70, &hash_header.version);
2831 bfd_putl32 (num_entries * sizeof (struct hash_record),
2832 &hash_header.entries_size);
2833 bfd_putl32 (buckets_size, &hash_header.buckets_size);
2834
2835 if (bfd_bwrite (&hash_header, sizeof (hash_header), stream) !=
2836 sizeof (hash_header))
2837 goto end;
2838
2839 /* Write the entries in hash order. */
2840
2841 for (unsigned int i = 0; i < num_entries; i++)
2842 {
2843 struct hash_record hr;
2844
2845 bfd_putl32 (sorted[i]->offset + 1, &hr.offset);
2846 bfd_putl32 (1, &hr.reference);
2847
2848 if (bfd_bwrite (&hr, sizeof (hr), stream) != sizeof (hr))
2849 goto end;
2850 }
2851
2852 /* Write the bitmap for filled and unfilled buckets. */
2853
2854 for (unsigned int i = 0; i < num_buckets; i += 8)
2855 {
2856 uint8_t v = 0;
2857
2858 for (unsigned int j = 0; j < 8; j++)
2859 {
2860 if (buckets[i + j])
2861 v |= 1 << j;
2862 }
2863
2864 if (bfd_bwrite (&v, sizeof (v), stream) != sizeof (v))
2865 goto end;
2866 }
2867
2868 /* Add a 4-byte gap. */
2869
2870 bfd_putl32 (0, int_buf);
2871
2872 if (bfd_bwrite (int_buf, sizeof (uint32_t), stream) != sizeof (uint32_t))
2873 goto end;
2874
2875 /* Write the bucket offsets. */
2876
2877 for (unsigned int i = 0; i < num_buckets; i++)
2878 {
2879 if (buckets[i])
2880 {
2881 /* 0xc is size of internal hash_record structure in
2882 Microsoft's parser. */
2883 bfd_putl32 (buckets[i]->index * 0xc, int_buf);
2884
2885 if (bfd_bwrite (int_buf, sizeof (uint32_t), stream) !=
2886 sizeof (uint32_t))
2887 goto end;
2888 }
2889 }
2890
2891 /* Write the address map: offsets into the symbol record stream of
2892 S_PUB32 records, ordered by address. */
2893
2894 if (num_entries > 0)
2895 {
2896 qsort (sorted, num_entries, sizeof (struct public *),
2897 public_compare_addr);
2898
2899 for (unsigned int i = 0; i < num_entries; i++)
2900 {
2901 bfd_putl32 (sorted[i]->offset, int_buf);
2902
2903 if (bfd_bwrite (int_buf, sizeof (uint32_t), stream) !=
2904 sizeof (uint32_t))
2905 goto end;
2906 }
2907 }
2908
2909 ret = true;
2910
2911 end:
2912 free (buckets);
2913
2914 while (publics_head)
2915 {
2916 struct public *p = publics_head->next;
2917
2918 free (publics_head);
2919 publics_head = p;
2920 }
2921
2922 free (sorted);
2923
2924 return ret;
2925 }
2926
2927 /* The section header stream contains a copy of the section headers
2928 from the PE file, in the same format. */
2929 static bool
2930 create_section_header_stream (bfd *pdb, bfd *abfd, uint16_t *num)
2931 {
2932 bfd *stream;
2933 unsigned int section_count;
2934 file_ptr scn_base;
2935 size_t len;
2936 char *buf;
2937
2938 stream = add_stream (pdb, NULL, num);
2939 if (!stream)
2940 return false;
2941
2942 section_count = abfd->section_count;
2943
2944 /* Empty sections aren't output. */
2945 for (asection *sect = abfd->sections; sect; sect = sect->next)
2946 {
2947 if (sect->size == 0)
2948 section_count--;
2949 }
2950
2951 if (section_count == 0)
2952 return true;
2953
2954 /* Copy section table from output - it's already been written at this
2955 point. */
2956
2957 scn_base = bfd_coff_filhsz (abfd) + bfd_coff_aoutsz (abfd);
2958
2959 bfd_seek (abfd, scn_base, SEEK_SET);
2960
2961 len = section_count * sizeof (struct external_scnhdr);
2962 buf = xmalloc (len);
2963
2964 if (bfd_bread (buf, len, abfd) != len)
2965 {
2966 free (buf);
2967 return false;
2968 }
2969
2970 if (bfd_bwrite (buf, len, stream) != len)
2971 {
2972 free (buf);
2973 return false;
2974 }
2975
2976 free (buf);
2977
2978 return true;
2979 }
2980
2981 /* Populate the "/names" named stream, which contains the string table. */
2982 static bool
2983 populate_names_stream (bfd *stream, struct string_table *strings)
2984 {
2985 char int_buf[sizeof (uint32_t)];
2986 struct string_table_header h;
2987 uint32_t num_strings = 0, num_buckets;
2988 struct string **buckets;
2989
2990 bfd_putl32 (STRING_TABLE_SIGNATURE, &h.signature);
2991 bfd_putl32 (STRING_TABLE_VERSION, &h.version);
2992
2993 if (bfd_bwrite (&h, sizeof (h), stream) != sizeof (h))
2994 return false;
2995
2996 bfd_putl32 (strings->strings_len, int_buf);
2997
2998 if (bfd_bwrite (int_buf, sizeof (uint32_t), stream) != sizeof (uint32_t))
2999 return false;
3000
3001 int_buf[0] = 0;
3002
3003 if (bfd_bwrite (int_buf, 1, stream) != 1)
3004 return false;
3005
3006 for (struct string *s = strings->strings_head; s; s = s->next)
3007 {
3008 if (bfd_bwrite (s->s, s->len, stream) != s->len)
3009 return false;
3010
3011 if (bfd_bwrite (int_buf, 1, stream) != 1)
3012 return false;
3013
3014 num_strings++;
3015 }
3016
3017 num_buckets = num_strings * 2;
3018
3019 buckets = xmalloc (sizeof (struct string *) * num_buckets);
3020 memset (buckets, 0, sizeof (struct string *) * num_buckets);
3021
3022 for (struct string *s = strings->strings_head; s; s = s->next)
3023 {
3024 uint32_t bucket_num = s->hash % num_buckets;
3025
3026 while (buckets[bucket_num])
3027 {
3028 bucket_num++;
3029
3030 if (bucket_num == num_buckets)
3031 bucket_num = 0;
3032 }
3033
3034 buckets[bucket_num] = s;
3035 }
3036
3037 bfd_putl32 (num_buckets, int_buf);
3038
3039 if (bfd_bwrite (int_buf, sizeof (uint32_t), stream) != sizeof (uint32_t))
3040 {
3041 free (buckets);
3042 return false;
3043 }
3044
3045 for (unsigned int i = 0; i < num_buckets; i++)
3046 {
3047 if (buckets[i])
3048 bfd_putl32 (buckets[i]->offset, int_buf);
3049 else
3050 bfd_putl32 (0, int_buf);
3051
3052 if (bfd_bwrite (int_buf, sizeof (uint32_t), stream) !=
3053 sizeof (uint32_t))
3054 {
3055 free (buckets);
3056 return false;
3057 }
3058 }
3059
3060 free (buckets);
3061
3062 bfd_putl32 (num_strings, int_buf);
3063
3064 if (bfd_bwrite (int_buf, sizeof (uint32_t), stream) != sizeof (uint32_t))
3065 return false;
3066
3067 return true;
3068 }
3069
3070 /* Calculate the hash of a type_entry. */
3071 static hashval_t
3072 hash_type_entry (const void *p)
3073 {
3074 const struct type_entry *e = (const struct type_entry *) p;
3075 uint16_t size = bfd_getl16 (e->data) + sizeof (uint16_t);
3076
3077 return iterative_hash (e->data, size, 0);
3078 }
3079
3080 /* Compare a type_entry with a type. */
3081 static int
3082 eq_type_entry (const void *a, const void *b)
3083 {
3084 const struct type_entry *e = (const struct type_entry *) a;
3085 uint16_t size_a = bfd_getl16 (e->data);
3086 uint16_t size_b = bfd_getl16 (b);
3087
3088 if (size_a != size_b)
3089 return 0;
3090
3091 return memcmp (e->data + sizeof (uint16_t),
3092 (const uint8_t *) b + sizeof (uint16_t), size_a) == 0;
3093 }
3094
3095 /* Create a PDB debugging file for the PE image file abfd with the build ID
3096 guid, stored at pdb_name. */
3097 bool
3098 create_pdb_file (bfd *abfd, const char *pdb_name, const unsigned char *guid)
3099 {
3100 bfd *pdb;
3101 bool ret = false;
3102 bfd *info_stream, *dbi_stream, *names_stream, *sym_rec_stream,
3103 *publics_stream, *tpi_stream, *ipi_stream;
3104 uint16_t section_header_stream_num, sym_rec_stream_num, publics_stream_num;
3105 struct string_table strings;
3106 struct types types, ids;
3107
3108 pdb = bfd_openw (pdb_name, "pdb");
3109 if (!pdb)
3110 {
3111 einfo (_("%P: warning: cannot create PDB file: %E\n"));
3112 return false;
3113 }
3114
3115 strings.strings_head = NULL;
3116 strings.strings_tail = NULL;
3117 strings.strings_len = 1;
3118 strings.hashmap = htab_create_alloc (0, hash_string_table_entry,
3119 eq_string_table_entry, free,
3120 xcalloc, free);
3121
3122 bfd_set_format (pdb, bfd_archive);
3123
3124 if (!create_old_directory_stream (pdb))
3125 {
3126 einfo (_("%P: warning: cannot create old directory stream "
3127 "in PDB file: %E\n"));
3128 goto end;
3129 }
3130
3131 info_stream = add_stream (pdb, NULL, NULL);
3132
3133 if (!info_stream)
3134 {
3135 einfo (_("%P: warning: cannot create info stream "
3136 "in PDB file: %E\n"));
3137 goto end;
3138 }
3139
3140 tpi_stream = add_stream (pdb, NULL, NULL);
3141
3142 if (!tpi_stream)
3143 {
3144 einfo (_("%P: warning: cannot create TPI stream "
3145 "in PDB file: %E\n"));
3146 goto end;
3147 }
3148
3149 dbi_stream = add_stream (pdb, NULL, NULL);
3150
3151 if (!dbi_stream)
3152 {
3153 einfo (_("%P: warning: cannot create DBI stream "
3154 "in PDB file: %E\n"));
3155 goto end;
3156 }
3157
3158 ipi_stream = add_stream (pdb, NULL, NULL);
3159
3160 if (!ipi_stream)
3161 {
3162 einfo (_("%P: warning: cannot create IPI stream "
3163 "in PDB file: %E\n"));
3164 goto end;
3165 }
3166
3167 names_stream = add_stream (pdb, "/names", NULL);
3168
3169 if (!names_stream)
3170 {
3171 einfo (_("%P: warning: cannot create /names stream "
3172 "in PDB file: %E\n"));
3173 goto end;
3174 }
3175
3176 sym_rec_stream = add_stream (pdb, NULL, &sym_rec_stream_num);
3177
3178 if (!sym_rec_stream)
3179 {
3180 einfo (_("%P: warning: cannot create symbol record stream "
3181 "in PDB file: %E\n"));
3182 goto end;
3183 }
3184
3185 publics_stream = add_stream (pdb, NULL, &publics_stream_num);
3186
3187 if (!publics_stream)
3188 {
3189 einfo (_("%P: warning: cannot create publics stream "
3190 "in PDB file: %E\n"));
3191 goto end;
3192 }
3193
3194 if (!create_section_header_stream (pdb, abfd, &section_header_stream_num))
3195 {
3196 einfo (_("%P: warning: cannot create section header stream "
3197 "in PDB file: %E\n"));
3198 goto end;
3199 }
3200
3201 types.num_types = 0;
3202 types.hashmap = htab_create_alloc (0, hash_type_entry, eq_type_entry,
3203 free, xcalloc, free);
3204 types.first = types.last = NULL;
3205
3206 ids.num_types = 0;
3207 ids.hashmap = htab_create_alloc (0, hash_type_entry, eq_type_entry,
3208 free, xcalloc, free);
3209 ids.first = ids.last = NULL;
3210
3211 if (!populate_dbi_stream (dbi_stream, abfd, pdb, section_header_stream_num,
3212 sym_rec_stream_num, publics_stream_num,
3213 &strings, &types))
3214 {
3215 einfo (_("%P: warning: cannot populate DBI stream "
3216 "in PDB file: %E\n"));
3217 htab_delete (types.hashmap);
3218 htab_delete (ids.hashmap);
3219 goto end;
3220 }
3221
3222 if (!populate_type_stream (pdb, tpi_stream, &types))
3223 {
3224 einfo (_("%P: warning: cannot populate TPI stream "
3225 "in PDB file: %E\n"));
3226 htab_delete (types.hashmap);
3227 htab_delete (ids.hashmap);
3228 goto end;
3229 }
3230
3231 htab_delete (types.hashmap);
3232
3233 if (!populate_type_stream (pdb, ipi_stream, &ids))
3234 {
3235 einfo (_("%P: warning: cannot populate IPI stream "
3236 "in PDB file: %E\n"));
3237 htab_delete (ids.hashmap);
3238 goto end;
3239 }
3240
3241 htab_delete (ids.hashmap);
3242
3243 add_string ("", 0, &strings);
3244
3245 if (!populate_names_stream (names_stream, &strings))
3246 {
3247 einfo (_("%P: warning: cannot populate names stream "
3248 "in PDB file: %E\n"));
3249 goto end;
3250 }
3251
3252 if (!populate_publics_stream (publics_stream, abfd, sym_rec_stream))
3253 {
3254 einfo (_("%P: warning: cannot populate publics stream "
3255 "in PDB file: %E\n"));
3256 goto end;
3257 }
3258
3259 if (!populate_info_stream (pdb, info_stream, guid))
3260 {
3261 einfo (_("%P: warning: cannot populate info stream "
3262 "in PDB file: %E\n"));
3263 goto end;
3264 }
3265
3266 ret = true;
3267
3268 end:
3269 bfd_close (pdb);
3270
3271 htab_delete (strings.hashmap);
3272
3273 return ret;
3274 }