ld: Add pdb support to aarch64-w64-mingw32
[binutils-gdb.git] / ld / pdb.c
1 /* Support for generating PDB CodeView debugging files.
2 Copyright (C) 2022-2023 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 "ldmain.h"
25 #include "ldmisc.h"
26 #include "libbfd.h"
27 #include "libiberty.h"
28 #include "coff/i386.h"
29 #include "coff/external.h"
30 #include "coff/internal.h"
31 #include "coff/pe.h"
32 #include "libcoff.h"
33 #include <time.h>
34
35 struct public
36 {
37 struct public *next;
38 uint32_t offset;
39 uint32_t hash;
40 unsigned int index;
41 uint16_t section;
42 uint32_t address;
43 };
44
45 struct string
46 {
47 struct string *next;
48 uint32_t hash;
49 uint32_t offset;
50 uint32_t source_file_offset;
51 size_t len;
52 char s[];
53 };
54
55 struct string_table
56 {
57 struct string *strings_head;
58 struct string *strings_tail;
59 uint32_t strings_len;
60 htab_t hashmap;
61 };
62
63 struct mod_source_files
64 {
65 uint16_t files_count;
66 struct string **files;
67 };
68
69 struct source_files_info
70 {
71 uint16_t mod_count;
72 struct mod_source_files *mods;
73 };
74
75 struct type_entry
76 {
77 struct type_entry *next;
78 uint32_t index;
79 uint32_t cv_hash;
80 bool has_udt_src_line;
81 uint8_t data[];
82 };
83
84 struct types
85 {
86 htab_t hashmap;
87 uint32_t num_types;
88 struct type_entry *first;
89 struct type_entry *last;
90 };
91
92 struct global
93 {
94 struct global *next;
95 uint32_t offset;
96 uint32_t hash;
97 uint32_t refcount;
98 unsigned int index;
99 uint8_t data[];
100 };
101
102 struct globals
103 {
104 uint32_t num_entries;
105 struct global *first;
106 struct global *last;
107 htab_t hashmap;
108 };
109
110 static const uint32_t crc_table[] =
111 {
112 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f,
113 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988,
114 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2,
115 0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7,
116 0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9,
117 0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172,
118 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, 0x42b2986c,
119 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59,
120 0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423,
121 0xcfba9599, 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924,
122 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, 0x01db7106,
123 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433,
124 0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d,
125 0x91646c97, 0xe6635c01, 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e,
126 0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950,
127 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65,
128 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7,
129 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0,
130 0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa,
131 0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
132 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81,
133 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a,
134 0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683, 0xe3630b12, 0x94643b84,
135 0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1,
136 0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb,
137 0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc,
138 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 0xd6d6a3e8, 0xa1d1937e,
139 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b,
140 0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55,
141 0x316e8eef, 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236,
142 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, 0xb2bd0b28,
143 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d,
144 0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f,
145 0x72076785, 0x05005713, 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38,
146 0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242,
147 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777,
148 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69,
149 0x616bffd3, 0x166ccf45, 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2,
150 0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc,
151 0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
152 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693,
153 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94,
154 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d
155 };
156
157 /* Add a new stream to the PDB archive, and return its BFD. */
158 static bfd *
159 add_stream (bfd *pdb, const char *name, uint16_t *stream_num)
160 {
161 bfd *stream;
162 uint16_t num;
163
164 stream = bfd_create (name ? name : "", pdb);
165 if (!stream)
166 return NULL;
167
168 if (!bfd_make_writable (stream))
169 {
170 bfd_close (stream);
171 return false;
172 }
173
174 if (!pdb->archive_head)
175 {
176 bfd_set_archive_head (pdb, stream);
177 num = 0;
178 }
179 else
180 {
181 bfd *b = pdb->archive_head;
182
183 num = 1;
184
185 while (b->archive_next)
186 {
187 num++;
188 b = b->archive_next;
189 }
190
191 b->archive_next = stream;
192 }
193
194 if (stream_num)
195 *stream_num = num;
196
197 return stream;
198 }
199
200 /* Stream 0 ought to be a copy of the MSF directory from the last
201 time the PDB file was written. Because we don't do incremental
202 writes this isn't applicable to us, but we fill it with a dummy
203 value so as not to confuse radare. */
204 static bool
205 create_old_directory_stream (bfd *pdb)
206 {
207 bfd *stream;
208 char buf[sizeof (uint32_t)];
209
210 stream = add_stream (pdb, NULL, NULL);
211 if (!stream)
212 return false;
213
214 bfd_putl32 (0, buf);
215
216 return bfd_bwrite (buf, sizeof (uint32_t), stream) == sizeof (uint32_t);
217 }
218
219 /* Calculate the hash of a given string. */
220 static uint32_t
221 calc_hash (const char *data, size_t len)
222 {
223 uint32_t hash = 0;
224
225 while (len >= 4)
226 {
227 hash ^= data[0];
228 hash ^= data[1] << 8;
229 hash ^= data[2] << 16;
230 hash ^= data[3] << 24;
231
232 data += 4;
233 len -= 4;
234 }
235
236 if (len >= 2)
237 {
238 hash ^= data[0];
239 hash ^= data[1] << 8;
240
241 data += 2;
242 len -= 2;
243 }
244
245 if (len != 0)
246 hash ^= *data;
247
248 hash |= 0x20202020;
249 hash ^= (hash >> 11);
250
251 return hash ^ (hash >> 16);
252 }
253
254 /* Stream 1 is the PDB info stream - see
255 https://llvm.org/docs/PDB/PdbStream.html. */
256 static bool
257 populate_info_stream (bfd *pdb, bfd *info_stream, const unsigned char *guid)
258 {
259 bool ret = false;
260 struct pdb_stream_70 h;
261 uint32_t num_entries, num_buckets;
262 uint32_t names_length, stream_num;
263 char int_buf[sizeof (uint32_t)];
264
265 struct hash_entry
266 {
267 uint32_t offset;
268 uint32_t value;
269 };
270
271 struct hash_entry **buckets = NULL;
272
273 /* Write header. */
274
275 bfd_putl32 (PDB_STREAM_VERSION_VC70, &h.version);
276 bfd_putl32 (time (NULL), &h.signature);
277 bfd_putl32 (1, &h.age);
278
279 bfd_putl32 (bfd_getb32 (guid), h.guid);
280 bfd_putl16 (bfd_getb16 (&guid[4]), &h.guid[4]);
281 bfd_putl16 (bfd_getb16 (&guid[6]), &h.guid[6]);
282 memcpy (&h.guid[8], &guid[8], 8);
283
284 if (bfd_bwrite (&h, sizeof (h), info_stream) != sizeof (h))
285 return false;
286
287 /* Write hash list of named streams. This is a "rollover" hash, i.e.
288 if a bucket is filled an entry gets placed in the next free
289 slot. */
290
291 num_entries = 0;
292 for (bfd *b = pdb->archive_head; b; b = b->archive_next)
293 {
294 if (strcmp (b->filename, ""))
295 num_entries++;
296 }
297
298 num_buckets = num_entries * 2;
299
300 names_length = 0;
301 stream_num = 0;
302
303 if (num_buckets > 0)
304 {
305 buckets = xmalloc (sizeof (struct hash_entry *) * num_buckets);
306 memset (buckets, 0, sizeof (struct hash_entry *) * num_buckets);
307
308 for (bfd *b = pdb->archive_head; b; b = b->archive_next)
309 {
310 if (strcmp (b->filename, ""))
311 {
312 size_t len = strlen (b->filename);
313 uint32_t hash = (uint16_t) calc_hash (b->filename, len);
314 uint32_t bucket_num = hash % num_buckets;
315
316 while (buckets[bucket_num])
317 {
318 bucket_num++;
319
320 if (bucket_num == num_buckets)
321 bucket_num = 0;
322 }
323
324 buckets[bucket_num] = xmalloc (sizeof (struct hash_entry));
325
326 buckets[bucket_num]->offset = names_length;
327 buckets[bucket_num]->value = stream_num;
328
329 names_length += len + 1;
330 }
331
332 stream_num++;
333 }
334 }
335
336 /* Write the strings list - the hash keys are indexes into this. */
337
338 bfd_putl32 (names_length, int_buf);
339
340 if (bfd_bwrite (int_buf, sizeof (uint32_t), info_stream) !=
341 sizeof (uint32_t))
342 goto end;
343
344 for (bfd *b = pdb->archive_head; b; b = b->archive_next)
345 {
346 if (!strcmp (b->filename, ""))
347 continue;
348
349 size_t len = strlen (b->filename) + 1;
350
351 if (bfd_bwrite (b->filename, len, info_stream) != len)
352 goto end;
353 }
354
355 /* Write the number of entries and buckets. */
356
357 bfd_putl32 (num_entries, int_buf);
358
359 if (bfd_bwrite (int_buf, sizeof (uint32_t), info_stream) !=
360 sizeof (uint32_t))
361 goto end;
362
363 bfd_putl32 (num_buckets, int_buf);
364
365 if (bfd_bwrite (int_buf, sizeof (uint32_t), info_stream) !=
366 sizeof (uint32_t))
367 goto end;
368
369 /* Write the present bitmap. */
370
371 bfd_putl32 ((num_buckets + 31) / 32, int_buf);
372
373 if (bfd_bwrite (int_buf, sizeof (uint32_t), info_stream) !=
374 sizeof (uint32_t))
375 goto end;
376
377 for (unsigned int i = 0; i < num_buckets; i += 32)
378 {
379 uint32_t v = 0;
380
381 for (unsigned int j = 0; j < 32; j++)
382 {
383 if (i + j >= num_buckets)
384 break;
385
386 if (buckets[i + j])
387 v |= 1 << j;
388 }
389
390 bfd_putl32 (v, int_buf);
391
392 if (bfd_bwrite (int_buf, sizeof (uint32_t), info_stream) !=
393 sizeof (uint32_t))
394 goto end;
395 }
396
397 /* Write the (empty) deleted bitmap. */
398
399 bfd_putl32 (0, int_buf);
400
401 if (bfd_bwrite (int_buf, sizeof (uint32_t), info_stream) !=
402 sizeof (uint32_t))
403 goto end;
404
405 /* Write the buckets. */
406
407 for (unsigned int i = 0; i < num_buckets; i++)
408 {
409 if (buckets[i])
410 {
411 bfd_putl32 (buckets[i]->offset, int_buf);
412
413 if (bfd_bwrite (int_buf, sizeof (uint32_t), info_stream) !=
414 sizeof (uint32_t))
415 goto end;
416
417 bfd_putl32 (buckets[i]->value, int_buf);
418
419 if (bfd_bwrite (int_buf, sizeof (uint32_t), info_stream) !=
420 sizeof (uint32_t))
421 goto end;
422 }
423 }
424
425 bfd_putl32 (0, int_buf);
426
427 if (bfd_bwrite (int_buf, sizeof (uint32_t), info_stream) !=
428 sizeof (uint32_t))
429 goto end;
430
431 bfd_putl32 (PDB_STREAM_VERSION_VC140, int_buf);
432
433 if (bfd_bwrite (int_buf, sizeof (uint32_t), info_stream) !=
434 sizeof (uint32_t))
435 goto end;
436
437 ret = true;
438
439 end:
440 for (unsigned int i = 0; i < num_buckets; i++)
441 {
442 if (buckets[i])
443 free (buckets[i]);
444 }
445
446 free (buckets);
447
448 return ret;
449 }
450
451 /* Calculate the CRC32 used for type hashes. */
452 static uint32_t
453 crc32 (const uint8_t *data, size_t len)
454 {
455 uint32_t crc = 0;
456
457 while (len > 0)
458 {
459 crc = (crc >> 8) ^ crc_table[(crc & 0xff) ^ *data];
460
461 data++;
462 len--;
463 }
464
465 return crc;
466 }
467
468 /* Stream 2 is the type information (TPI) stream, and stream 4 is
469 the ID information (IPI) stream. They differ only in which records
470 go in which stream. */
471 static bool
472 populate_type_stream (bfd *pdb, bfd *stream, struct types *types)
473 {
474 struct pdb_tpi_stream_header h;
475 struct type_entry *e;
476 uint32_t len = 0, index_offset_len, off;
477 struct bfd *hash_stream = NULL;
478 uint16_t hash_stream_index;
479
480 static const uint32_t index_skip = 0x2000;
481
482 e = types->first;
483
484 index_offset_len = 0;
485
486 while (e)
487 {
488 uint32_t old_len = len;
489
490 len += sizeof (uint16_t) + bfd_getl16 (e->data);
491
492 if (old_len == 0 || old_len / index_skip != len / index_skip)
493 index_offset_len += sizeof (uint32_t) * 2;
494
495 e = e->next;
496 }
497
498 /* Each type stream also has a stream which holds the hash value for each
499 type, along with a skip list to speed up searching. */
500
501 hash_stream = add_stream (pdb, "", &hash_stream_index);
502
503 if (!hash_stream)
504 return false;
505
506 bfd_putl32 (TPI_STREAM_VERSION_80, &h.version);
507 bfd_putl32 (sizeof (h), &h.header_size);
508 bfd_putl32 (TPI_FIRST_INDEX, &h.type_index_begin);
509 bfd_putl32 (TPI_FIRST_INDEX + types->num_types, &h.type_index_end);
510 bfd_putl32 (len, &h.type_record_bytes);
511 bfd_putl16 (hash_stream_index, &h.hash_stream_index);
512 bfd_putl16 (0xffff, &h.hash_aux_stream_index);
513 bfd_putl32 (sizeof (uint32_t), &h.hash_key_size);
514 bfd_putl32 (NUM_TPI_HASH_BUCKETS, &h.num_hash_buckets);
515 bfd_putl32 (0, &h.hash_value_buffer_offset);
516 bfd_putl32 (types->num_types * sizeof (uint32_t),
517 &h.hash_value_buffer_length);
518 bfd_putl32 (types->num_types * sizeof (uint32_t),
519 &h.index_offset_buffer_offset);
520 bfd_putl32 (index_offset_len, &h.index_offset_buffer_length);
521 bfd_putl32 ((types->num_types * sizeof (uint32_t)) + index_offset_len,
522 &h.hash_adj_buffer_offset);
523 bfd_putl32 (0, &h.hash_adj_buffer_length);
524
525 if (bfd_bwrite (&h, sizeof (h), stream) != sizeof (h))
526 return false;
527
528 /* Write the type definitions into the main stream, and the hashes
529 into the hash stream. The hashes have already been calculated
530 in handle_type. */
531
532 e = types->first;
533
534 while (e)
535 {
536 uint8_t buf[sizeof (uint32_t)];
537 uint16_t size;
538
539 size = bfd_getl16 (e->data);
540
541 if (bfd_bwrite (e->data, size + sizeof (uint16_t), stream)
542 != size + sizeof (uint16_t))
543 return false;
544
545 bfd_putl32 (e->cv_hash % NUM_TPI_HASH_BUCKETS, buf);
546
547 if (bfd_bwrite (buf, sizeof (uint32_t), hash_stream)
548 != sizeof (uint32_t))
549 return false;
550
551 e = e->next;
552 }
553
554 /* Write the index offsets, i.e. the skip list, into the hash stream. We
555 copy MSVC here by writing a new entry for every 8192 bytes. */
556
557 e = types->first;
558 off = 0;
559
560 while (e)
561 {
562 uint32_t old_off = off;
563 uint16_t size = bfd_getl16 (e->data);
564
565 off += size + sizeof (uint16_t);
566
567 if (old_off == 0 || old_off / index_skip != len / index_skip)
568 {
569 uint8_t buf[sizeof (uint32_t)];
570
571 bfd_putl32 (TPI_FIRST_INDEX + e->index, buf);
572
573 if (bfd_bwrite (buf, sizeof (uint32_t), hash_stream)
574 != sizeof (uint32_t))
575 return false;
576
577 bfd_putl32 (old_off, buf);
578
579 if (bfd_bwrite (buf, sizeof (uint32_t), hash_stream)
580 != sizeof (uint32_t))
581 return false;
582 }
583
584 e = e->next;
585 }
586
587 return true;
588 }
589
590 /* Return the PE architecture number for the image. */
591 static uint16_t
592 get_arch_number (bfd *abfd)
593 {
594 switch (abfd->arch_info->arch)
595 {
596 case bfd_arch_i386:
597 if (abfd->arch_info->mach & bfd_mach_x86_64)
598 return IMAGE_FILE_MACHINE_AMD64;
599 else
600 return IMAGE_FILE_MACHINE_I386;
601
602 case bfd_arch_aarch64:
603 return IMAGE_FILE_MACHINE_ARM64;
604
605 default:
606 return 0;
607 }
608 }
609
610 /* Validate the DEBUG_S_FILECHKSMS entry within a module's .debug$S
611 section, and copy it to the module's symbol stream. */
612 static bool
613 copy_filechksms (uint8_t *data, uint32_t size, char *string_table,
614 struct string_table *strings, uint8_t *out,
615 struct mod_source_files *mod_source)
616 {
617 uint8_t *orig_data = data;
618 uint32_t orig_size = size;
619 uint16_t num_files = 0;
620 struct string **strptr;
621
622 bfd_putl32 (DEBUG_S_FILECHKSMS, out);
623 out += sizeof (uint32_t);
624
625 bfd_putl32 (size, out);
626 out += sizeof (uint32_t);
627
628 /* Calculate the number of files, and check for any overflows. */
629
630 while (size > 0)
631 {
632 struct file_checksum *fc = (struct file_checksum *) data;
633 uint8_t padding;
634 size_t len;
635
636 if (size < sizeof (struct file_checksum))
637 {
638 bfd_set_error (bfd_error_bad_value);
639 return false;
640 }
641
642 len = sizeof (struct file_checksum) + fc->checksum_length;
643
644 if (size < len)
645 {
646 bfd_set_error (bfd_error_bad_value);
647 return false;
648 }
649
650 data += len;
651 size -= len;
652
653 if (len % sizeof (uint32_t))
654 padding = sizeof (uint32_t) - (len % sizeof (uint32_t));
655 else
656 padding = 0;
657
658 if (size < padding)
659 {
660 bfd_set_error (bfd_error_bad_value);
661 return false;
662 }
663
664 num_files++;
665
666 data += padding;
667 size -= padding;
668 }
669
670 /* Add the files to mod_source, so that they'll appear in the source
671 info substream. */
672
673 strptr = NULL;
674 if (num_files > 0)
675 {
676 uint16_t new_count = num_files + mod_source->files_count;
677
678 mod_source->files = xrealloc (mod_source->files,
679 sizeof (struct string *) * new_count);
680
681 strptr = mod_source->files + mod_source->files_count;
682
683 mod_source->files_count += num_files;
684 }
685
686 /* Actually copy the data. */
687
688 data = orig_data;
689 size = orig_size;
690
691 while (size > 0)
692 {
693 struct file_checksum *fc = (struct file_checksum *) data;
694 uint32_t string_off;
695 uint8_t padding;
696 size_t len;
697 struct string *str = NULL;
698
699 string_off = bfd_getl32 (&fc->file_id);
700 len = sizeof (struct file_checksum) + fc->checksum_length;
701
702 if (len % sizeof (uint32_t))
703 padding = sizeof (uint32_t) - (len % sizeof (uint32_t));
704 else
705 padding = 0;
706
707 /* Remap the "file ID", i.e. the offset in the module's string table,
708 so it points to the right place in the main string table. */
709
710 if (string_table)
711 {
712 char *fn = string_table + string_off;
713 size_t fn_len = strlen (fn);
714 uint32_t hash = calc_hash (fn, fn_len);
715 void **slot;
716
717 slot = htab_find_slot_with_hash (strings->hashmap, fn, hash,
718 NO_INSERT);
719
720 if (slot)
721 str = (struct string *) *slot;
722 }
723
724 *strptr = str;
725 strptr++;
726
727 bfd_putl32 (str ? str->offset : 0, &fc->file_id);
728
729 memcpy (out, data, len + padding);
730
731 data += len + padding;
732 size -= len + padding;
733 out += len + padding;
734 }
735
736 return true;
737 }
738
739 /* Add a string to the strings table, if it's not already there. Returns its
740 offset within the string table. */
741 static uint32_t
742 add_string (char *str, size_t len, struct string_table *strings)
743 {
744 uint32_t hash = calc_hash (str, len);
745 struct string *s;
746 void **slot;
747
748 slot = htab_find_slot_with_hash (strings->hashmap, str, hash, INSERT);
749
750 if (!*slot)
751 {
752 *slot = xmalloc (offsetof (struct string, s) + len);
753
754 s = (struct string *) *slot;
755
756 s->next = NULL;
757 s->hash = hash;
758 s->offset = strings->strings_len;
759 s->source_file_offset = 0xffffffff;
760 s->len = len;
761 memcpy (s->s, str, len);
762
763 if (strings->strings_tail)
764 strings->strings_tail->next = s;
765 else
766 strings->strings_head = s;
767
768 strings->strings_tail = s;
769
770 strings->strings_len += len + 1;
771 }
772 else
773 {
774 s = (struct string *) *slot;
775 }
776
777 return s->offset;
778 }
779
780 /* Return the hash of an entry in the string table. */
781 static hashval_t
782 hash_string_table_entry (const void *p)
783 {
784 const struct string *s = (const struct string *) p;
785
786 return s->hash;
787 }
788
789 /* Compare an entry in the string table with a string. */
790 static int
791 eq_string_table_entry (const void *a, const void *b)
792 {
793 const struct string *s1 = (const struct string *) a;
794 const char *s2 = (const char *) b;
795 size_t s2_len = strlen (s2);
796
797 if (s2_len != s1->len)
798 return 0;
799
800 return memcmp (s1->s, s2, s2_len) == 0;
801 }
802
803 /* Parse the string table within the .debug$S section. */
804 static void
805 parse_string_table (bfd_byte *data, size_t size,
806 struct string_table *strings)
807 {
808 while (true)
809 {
810 size_t len = strnlen ((char *) data, size);
811
812 add_string ((char *) data, len, strings);
813
814 data += len + 1;
815
816 if (size <= len + 1)
817 break;
818
819 size -= len + 1;
820 }
821 }
822
823 /* Remap a type reference within a CodeView symbol. */
824 static bool
825 remap_symbol_type (void *data, struct type_entry **map, uint32_t num_types)
826 {
827 uint32_t type = bfd_getl32 (data);
828
829 /* Ignore builtin types (those with IDs below 0x1000). */
830 if (type < TPI_FIRST_INDEX)
831 return true;
832
833 if (type >= TPI_FIRST_INDEX + num_types)
834 {
835 einfo (_("%P: CodeView symbol references out of range type %v\n"),
836 type);
837 return false;
838 }
839
840 type = TPI_FIRST_INDEX + map[type - TPI_FIRST_INDEX]->index;
841 bfd_putl32 (type, data);
842
843 return true;
844 }
845
846 /* Add an entry into the globals stream. If it already exists, increase
847 the refcount. */
848 static bool
849 add_globals_ref (struct globals *glob, bfd *sym_rec_stream, const char *name,
850 size_t name_len, uint8_t *data, size_t len)
851 {
852 void **slot;
853 uint32_t hash;
854 struct global *g;
855
856 slot = htab_find_slot_with_hash (glob->hashmap, data,
857 iterative_hash (data, len, 0), INSERT);
858
859 if (*slot)
860 {
861 g = *slot;
862 g->refcount++;
863 return true;
864 }
865
866 *slot = xmalloc (offsetof (struct global, data) + len);
867
868 hash = crc32 ((const uint8_t *) name, name_len);
869 hash %= NUM_GLOBALS_HASH_BUCKETS;
870
871 g = *slot;
872 g->next = NULL;
873 g->offset = bfd_tell (sym_rec_stream);
874 g->hash = hash;
875 g->refcount = 1;
876 memcpy (g->data, data, len + 1);
877
878 glob->num_entries++;
879
880 if (glob->last)
881 glob->last->next = g;
882 else
883 glob->first = g;
884
885 glob->last = g;
886
887 return bfd_bwrite (data, len, sym_rec_stream) == len;
888 }
889
890 /* Find the end of the current scope within symbols data. */
891 static uint8_t *
892 find_end_of_scope (uint8_t *data, uint32_t size)
893 {
894 unsigned int scope_level = 1;
895 uint16_t len;
896
897 len = bfd_getl16 (data) + sizeof (uint16_t);
898
899 data += len;
900 size -= len;
901
902 while (true)
903 {
904 uint16_t type;
905
906 if (size < sizeof (uint32_t))
907 return NULL;
908
909 len = bfd_getl16 (data) + sizeof (uint16_t);
910 type = bfd_getl16 (data + sizeof (uint16_t));
911
912 if (size < len)
913 return NULL;
914
915 switch (type)
916 {
917 case S_GPROC32:
918 case S_LPROC32:
919 case S_BLOCK32:
920 case S_INLINESITE:
921 case S_THUNK32:
922 scope_level++;
923 break;
924
925 case S_END:
926 case S_PROC_ID_END:
927 case S_INLINESITE_END:
928 scope_level--;
929
930 if (scope_level == 0)
931 return data;
932
933 break;
934 }
935
936 data += len;
937 size -= len;
938 }
939 }
940
941 /* Return the size of an extended value parameter, as used in
942 LF_ENUMERATE etc. */
943 static unsigned int
944 extended_value_len (uint16_t type)
945 {
946 switch (type)
947 {
948 case LF_CHAR:
949 return 1;
950
951 case LF_SHORT:
952 case LF_USHORT:
953 return 2;
954
955 case LF_LONG:
956 case LF_ULONG:
957 return 4;
958
959 case LF_QUADWORD:
960 case LF_UQUADWORD:
961 return 8;
962 }
963
964 return 0;
965 }
966
967 /* Parse the symbols in a .debug$S section, and copy them to the module's
968 symbol stream. */
969 static bool
970 parse_symbols (uint8_t *data, uint32_t size, uint8_t **buf,
971 struct type_entry **map, uint32_t num_types,
972 bfd *sym_rec_stream, struct globals *glob, uint16_t mod_num)
973 {
974 uint8_t *orig_buf = *buf;
975 unsigned int scope_level = 0;
976 uint8_t *scope = NULL;
977
978 while (size >= sizeof (uint16_t))
979 {
980 uint16_t len, type;
981
982 len = bfd_getl16 (data) + sizeof (uint16_t);
983
984 if (len > size)
985 {
986 bfd_set_error (bfd_error_bad_value);
987 return false;
988 }
989
990 type = bfd_getl16 (data + sizeof (uint16_t));
991
992 switch (type)
993 {
994 case S_LDATA32:
995 case S_GDATA32:
996 case S_LTHREAD32:
997 case S_GTHREAD32:
998 {
999 struct datasym *d = (struct datasym *) data;
1000 size_t name_len;
1001
1002 if (len < offsetof (struct datasym, name))
1003 {
1004 einfo (_("%P: warning: truncated CodeView record"
1005 " S_LDATA32/S_GDATA32/S_LTHREAD32/S_GTHREAD32\n"));
1006 bfd_set_error (bfd_error_bad_value);
1007 return false;
1008 }
1009
1010 if (scope_level == 0)
1011 {
1012 uint16_t section = bfd_getl16 (&d->section);
1013
1014 if (section == 0) /* GC'd, ignore */
1015 break;
1016 }
1017
1018 name_len =
1019 strnlen (d->name, len - offsetof (struct datasym, name));
1020
1021 if (name_len == len - offsetof (struct datasym, name))
1022 {
1023 einfo (_("%P: warning: name for S_LDATA32/S_GDATA32/"
1024 "S_LTHREAD32/S_GTHREAD32 has no terminating"
1025 " zero\n"));
1026 bfd_set_error (bfd_error_bad_value);
1027 return false;
1028 }
1029
1030 if (!remap_symbol_type (&d->type, map, num_types))
1031 {
1032 bfd_set_error (bfd_error_bad_value);
1033 return false;
1034 }
1035
1036 /* If S_LDATA32 or S_LTHREAD32, copy into module symbols. */
1037
1038 if (type == S_LDATA32 || type == S_LTHREAD32)
1039 {
1040 memcpy (*buf, d, len);
1041 *buf += len;
1042 }
1043
1044 /* S_LDATA32 and S_LTHREAD32 only go in globals if
1045 not in function scope. */
1046 if (type == S_GDATA32 || type == S_GTHREAD32 || scope_level == 0)
1047 {
1048 if (!add_globals_ref (glob, sym_rec_stream, d->name,
1049 name_len, data, len))
1050 return false;
1051 }
1052
1053 break;
1054 }
1055
1056 case S_GPROC32:
1057 case S_LPROC32:
1058 case S_GPROC32_ID:
1059 case S_LPROC32_ID:
1060 {
1061 struct procsym *proc = (struct procsym *) data;
1062 size_t name_len;
1063 uint16_t section;
1064 uint32_t end;
1065 uint8_t *endptr;
1066 size_t ref_size, padding;
1067 struct refsym *ref;
1068
1069 if (len < offsetof (struct procsym, name))
1070 {
1071 einfo (_("%P: warning: truncated CodeView record"
1072 " S_GPROC32/S_LPROC32\n"));
1073 bfd_set_error (bfd_error_bad_value);
1074 return false;
1075 }
1076
1077 section = bfd_getl16 (&proc->section);
1078
1079 endptr = find_end_of_scope (data, size);
1080
1081 if (!endptr)
1082 {
1083 einfo (_("%P: warning: could not find end of"
1084 " S_GPROC32/S_LPROC32 record\n"));
1085 bfd_set_error (bfd_error_bad_value);
1086 return false;
1087 }
1088
1089 if (section == 0) /* skip if GC'd */
1090 {
1091 /* Skip to after S_END. */
1092
1093 size -= endptr - data;
1094 data = endptr;
1095
1096 len = bfd_getl16 (data) + sizeof (uint16_t);
1097
1098 data += len;
1099 size -= len;
1100
1101 continue;
1102 }
1103
1104 name_len =
1105 strnlen (proc->name, len - offsetof (struct procsym, name));
1106
1107 if (name_len == len - offsetof (struct procsym, name))
1108 {
1109 einfo (_("%P: warning: name for S_GPROC32/S_LPROC32 has no"
1110 " terminating zero\n"));
1111 bfd_set_error (bfd_error_bad_value);
1112 return false;
1113 }
1114
1115 if (type == S_GPROC32_ID || type == S_LPROC32_ID)
1116 {
1117 /* Transform into S_GPROC32 / S_LPROC32. */
1118
1119 uint32_t t_idx = bfd_getl32 (&proc->type);
1120 struct type_entry *t;
1121 uint16_t t_type;
1122
1123 if (t_idx < TPI_FIRST_INDEX
1124 || t_idx >= TPI_FIRST_INDEX + num_types)
1125 {
1126 einfo (_("%P: CodeView symbol references out of range"
1127 " type %v\n"), type);
1128 bfd_set_error (bfd_error_bad_value);
1129 return false;
1130 }
1131
1132 t = map[t_idx - TPI_FIRST_INDEX];
1133
1134 t_type = bfd_getl16 (t->data + sizeof (uint16_t));
1135
1136 switch (t_type)
1137 {
1138 case LF_FUNC_ID:
1139 {
1140 struct lf_func_id *t_data =
1141 (struct lf_func_id *) t->data;
1142
1143 /* Replace proc->type with function type. */
1144
1145 memcpy (&proc->type, &t_data->function_type,
1146 sizeof (uint32_t));
1147
1148 break;
1149 }
1150
1151 case LF_MFUNC_ID:
1152 {
1153 struct lf_mfunc_id *t_data =
1154 (struct lf_mfunc_id *) t->data;
1155
1156 /* Replace proc->type with function type. */
1157
1158 memcpy (&proc->type, &t_data->function_type,
1159 sizeof (uint32_t));
1160
1161 break;
1162 }
1163
1164 default:
1165 einfo (_("%P: CodeView S_GPROC32_ID/S_LPROC32_ID symbol"
1166 " referenced unknown type as ID\n"));
1167 bfd_set_error (bfd_error_bad_value);
1168 return false;
1169 }
1170
1171 /* Change record type. */
1172
1173 if (type == S_GPROC32_ID)
1174 bfd_putl32 (S_GPROC32, &proc->kind);
1175 else
1176 bfd_putl32 (S_LPROC32, &proc->kind);
1177 }
1178 else
1179 {
1180 if (!remap_symbol_type (&proc->type, map, num_types))
1181 {
1182 bfd_set_error (bfd_error_bad_value);
1183 return false;
1184 }
1185 }
1186
1187 end = *buf - orig_buf + sizeof (uint32_t) + endptr - data;
1188 bfd_putl32 (end, &proc->end);
1189
1190 /* Add S_PROCREF / S_LPROCREF to globals stream. */
1191
1192 ref_size = offsetof (struct refsym, name) + name_len + 1;
1193
1194 if (ref_size % sizeof (uint32_t))
1195 padding = sizeof (uint32_t) - (ref_size % sizeof (uint32_t));
1196 else
1197 padding = 0;
1198
1199 ref = xmalloc (ref_size + padding);
1200
1201 bfd_putl16 (ref_size + padding - sizeof (uint16_t), &ref->size);
1202 bfd_putl16 (type == S_GPROC32 || type == S_GPROC32_ID ?
1203 S_PROCREF : S_LPROCREF, &ref->kind);
1204 bfd_putl32 (0, &ref->sum_name);
1205 bfd_putl32 (*buf - orig_buf + sizeof (uint32_t),
1206 &ref->symbol_offset);
1207 bfd_putl16 (mod_num + 1, &ref->mod);
1208
1209 memcpy (ref->name, proc->name, name_len + 1);
1210
1211 memset (ref->name + name_len + 1, 0, padding);
1212
1213 if (!add_globals_ref (glob, sym_rec_stream, proc->name, name_len,
1214 (uint8_t *) ref, ref_size + padding))
1215 {
1216 free (ref);
1217 return false;
1218 }
1219
1220 free (ref);
1221
1222 scope = *buf;
1223
1224 memcpy (*buf, proc, len);
1225 *buf += len;
1226
1227 scope_level++;
1228
1229 break;
1230 }
1231
1232 case S_UDT:
1233 {
1234 struct udtsym *udt = (struct udtsym *) data;
1235 size_t name_len;
1236
1237 if (len < offsetof (struct udtsym, name))
1238 {
1239 einfo (_("%P: warning: truncated CodeView record"
1240 " S_UDT\n"));
1241 bfd_set_error (bfd_error_bad_value);
1242 return false;
1243 }
1244
1245 name_len =
1246 strnlen (udt->name, len - offsetof (struct udtsym, name));
1247
1248 if (name_len == len - offsetof (struct udtsym, name))
1249 {
1250 einfo (_("%P: warning: name for S_UDT has no"
1251 " terminating zero\n"));
1252 bfd_set_error (bfd_error_bad_value);
1253 return false;
1254 }
1255
1256 if (!remap_symbol_type (&udt->type, map, num_types))
1257 {
1258 bfd_set_error (bfd_error_bad_value);
1259 return false;
1260 }
1261
1262 /* S_UDT goes in the symbols stream if within a procedure,
1263 otherwise it goes in the globals stream. */
1264 if (scope_level == 0)
1265 {
1266 if (!add_globals_ref (glob, sym_rec_stream, udt->name,
1267 name_len, data, len))
1268 return false;
1269 }
1270 else
1271 {
1272 memcpy (*buf, udt, len);
1273 *buf += len;
1274 }
1275
1276 break;
1277 }
1278
1279 case S_CONSTANT:
1280 {
1281 struct constsym *c = (struct constsym *) data;
1282 size_t name_len, rec_size;
1283 uint16_t val;
1284
1285 if (len < offsetof (struct constsym, name))
1286 {
1287 einfo (_("%P: warning: truncated CodeView record"
1288 " S_CONSTANT\n"));
1289 bfd_set_error (bfd_error_bad_value);
1290 return false;
1291 }
1292
1293 rec_size = offsetof (struct constsym, name);
1294
1295 val = bfd_getl16 (&c->value);
1296
1297 /* If val >= 0x8000, actual value follows. */
1298 if (val >= 0x8000)
1299 {
1300 unsigned int param_len = extended_value_len (val);
1301
1302 if (param_len == 0)
1303 {
1304 einfo (_("%P: warning: unhandled type %v within"
1305 " S_CONSTANT\n"), val);
1306 bfd_set_error (bfd_error_bad_value);
1307 return false;
1308 }
1309
1310 rec_size += param_len;
1311 }
1312
1313 name_len =
1314 strnlen ((const char *) data + rec_size, len - rec_size);
1315
1316 if (name_len == len - rec_size)
1317 {
1318 einfo (_("%P: warning: name for S_CONSTANT has no"
1319 " terminating zero\n"));
1320 bfd_set_error (bfd_error_bad_value);
1321 return false;
1322 }
1323
1324 if (!remap_symbol_type (&c->type, map, num_types))
1325 {
1326 bfd_set_error (bfd_error_bad_value);
1327 return false;
1328 }
1329
1330 if (!add_globals_ref (glob, sym_rec_stream,
1331 (const char *) data + rec_size, name_len,
1332 data, len))
1333 return false;
1334
1335 break;
1336 }
1337
1338 case S_END:
1339 case S_INLINESITE_END:
1340 case S_PROC_ID_END:
1341 memcpy (*buf, data, len);
1342
1343 if (type == S_PROC_ID_END) /* transform to S_END */
1344 bfd_putl16 (S_END, *buf + sizeof (uint16_t));
1345
1346 /* Reset scope variable back to the address of the previous
1347 scope start. */
1348 if (scope)
1349 {
1350 uint32_t parent;
1351 uint16_t scope_start_type =
1352 bfd_getl16 (scope + sizeof (uint16_t));
1353
1354 switch (scope_start_type)
1355 {
1356 case S_GPROC32:
1357 case S_LPROC32:
1358 parent = bfd_getl32 (scope + offsetof (struct procsym,
1359 parent));
1360 break;
1361
1362 case S_BLOCK32:
1363 parent = bfd_getl32 (scope + offsetof (struct blocksym,
1364 parent));
1365 break;
1366
1367 case S_INLINESITE:
1368 parent = bfd_getl32 (scope + offsetof (struct inline_site,
1369 parent));
1370 break;
1371
1372 case S_THUNK32:
1373 parent = bfd_getl32 (scope + offsetof (struct thunk,
1374 parent));
1375 break;
1376
1377 default:
1378 einfo (_("%P: warning: unexpected CodeView scope start"
1379 " record %v\n"), scope_start_type);
1380 bfd_set_error (bfd_error_bad_value);
1381 return false;
1382 }
1383
1384 if (parent == 0)
1385 scope = NULL;
1386 else
1387 scope = orig_buf + parent - sizeof (uint32_t);
1388 }
1389
1390 *buf += len;
1391 scope_level--;
1392 break;
1393
1394 case S_BUILDINFO:
1395 {
1396 struct buildinfosym *bi = (struct buildinfosym *) data;
1397
1398 if (len < sizeof (struct buildinfosym))
1399 {
1400 einfo (_("%P: warning: truncated CodeView record"
1401 " S_BUILDINFO\n"));
1402 bfd_set_error (bfd_error_bad_value);
1403 return false;
1404 }
1405
1406 if (!remap_symbol_type (&bi->type, map, num_types))
1407 {
1408 bfd_set_error (bfd_error_bad_value);
1409 return false;
1410 }
1411
1412 memcpy (*buf, data, len);
1413 *buf += len;
1414
1415 break;
1416 }
1417
1418 case S_BLOCK32:
1419 {
1420 struct blocksym *bl = (struct blocksym *) data;
1421 uint8_t *endptr;
1422 uint32_t end;
1423
1424 if (len < offsetof (struct blocksym, name))
1425 {
1426 einfo (_("%P: warning: truncated CodeView record"
1427 " S_BLOCK32\n"));
1428 bfd_set_error (bfd_error_bad_value);
1429 return false;
1430 }
1431
1432 bfd_putl32 (scope - orig_buf + sizeof (uint32_t), &bl->parent);
1433
1434 endptr = find_end_of_scope (data, size);
1435
1436 if (!endptr)
1437 {
1438 einfo (_("%P: warning: could not find end of"
1439 " S_BLOCK32 record\n"));
1440 bfd_set_error (bfd_error_bad_value);
1441 return false;
1442 }
1443
1444 end = *buf - orig_buf + sizeof (uint32_t) + endptr - data;
1445 bfd_putl32 (end, &bl->end);
1446
1447 scope = *buf;
1448
1449 memcpy (*buf, data, len);
1450 *buf += len;
1451
1452 scope_level++;
1453
1454 break;
1455 }
1456
1457 case S_BPREL32:
1458 {
1459 struct bprelsym *bp = (struct bprelsym *) data;
1460
1461 if (len < offsetof (struct bprelsym, name))
1462 {
1463 einfo (_("%P: warning: truncated CodeView record"
1464 " S_BPREL32\n"));
1465 bfd_set_error (bfd_error_bad_value);
1466 return false;
1467 }
1468
1469 if (!remap_symbol_type (&bp->type, map, num_types))
1470 {
1471 bfd_set_error (bfd_error_bad_value);
1472 return false;
1473 }
1474
1475 memcpy (*buf, data, len);
1476 *buf += len;
1477
1478 break;
1479 }
1480
1481 case S_REGISTER:
1482 {
1483 struct regsym *reg = (struct regsym *) data;
1484
1485 if (len < offsetof (struct regsym, name))
1486 {
1487 einfo (_("%P: warning: truncated CodeView record"
1488 " S_REGISTER\n"));
1489 bfd_set_error (bfd_error_bad_value);
1490 return false;
1491 }
1492
1493 if (!remap_symbol_type (&reg->type, map, num_types))
1494 {
1495 bfd_set_error (bfd_error_bad_value);
1496 return false;
1497 }
1498
1499 memcpy (*buf, data, len);
1500 *buf += len;
1501
1502 break;
1503 }
1504
1505 case S_REGREL32:
1506 {
1507 struct regrel *rr = (struct regrel *) data;
1508
1509 if (len < offsetof (struct regrel, name))
1510 {
1511 einfo (_("%P: warning: truncated CodeView record"
1512 " S_REGREL32\n"));
1513 bfd_set_error (bfd_error_bad_value);
1514 return false;
1515 }
1516
1517 if (!remap_symbol_type (&rr->type, map, num_types))
1518 {
1519 bfd_set_error (bfd_error_bad_value);
1520 return false;
1521 }
1522
1523 memcpy (*buf, data, len);
1524 *buf += len;
1525
1526 break;
1527 }
1528
1529 case S_LOCAL:
1530 {
1531 struct localsym *l = (struct localsym *) data;
1532
1533 if (len < offsetof (struct localsym, name))
1534 {
1535 einfo (_("%P: warning: truncated CodeView record"
1536 " S_LOCAL\n"));
1537 bfd_set_error (bfd_error_bad_value);
1538 return false;
1539 }
1540
1541 if (!remap_symbol_type (&l->type, map, num_types))
1542 {
1543 bfd_set_error (bfd_error_bad_value);
1544 return false;
1545 }
1546
1547 memcpy (*buf, data, len);
1548 *buf += len;
1549
1550 break;
1551 }
1552
1553 case S_INLINESITE:
1554 {
1555 struct inline_site *is = (struct inline_site *) data;
1556 uint8_t *endptr;
1557 uint32_t end;
1558
1559 if (len < offsetof (struct inline_site, binary_annotations))
1560 {
1561 einfo (_("%P: warning: truncated CodeView record"
1562 " S_INLINESITE\n"));
1563 bfd_set_error (bfd_error_bad_value);
1564 return false;
1565 }
1566
1567 bfd_putl32 (scope - orig_buf + sizeof (uint32_t), &is->parent);
1568
1569 endptr = find_end_of_scope (data, size);
1570
1571 if (!endptr)
1572 {
1573 einfo (_("%P: warning: could not find end of"
1574 " S_INLINESITE record\n"));
1575 bfd_set_error (bfd_error_bad_value);
1576 return false;
1577 }
1578
1579 end = *buf - orig_buf + sizeof (uint32_t) + endptr - data;
1580 bfd_putl32 (end, &is->end);
1581
1582 if (!remap_symbol_type (&is->inlinee, map, num_types))
1583 {
1584 bfd_set_error (bfd_error_bad_value);
1585 return false;
1586 }
1587
1588 scope = *buf;
1589
1590 memcpy (*buf, data, len);
1591 *buf += len;
1592
1593 scope_level++;
1594
1595 break;
1596 }
1597
1598 case S_THUNK32:
1599 {
1600 struct thunk *th = (struct thunk *) data;
1601 uint8_t *endptr;
1602 uint32_t end;
1603
1604 if (len < offsetof (struct thunk, name))
1605 {
1606 einfo (_("%P: warning: truncated CodeView record"
1607 " S_THUNK32\n"));
1608 bfd_set_error (bfd_error_bad_value);
1609 return false;
1610 }
1611
1612 bfd_putl32 (scope - orig_buf + sizeof (uint32_t), &th->parent);
1613
1614 endptr = find_end_of_scope (data, size);
1615
1616 if (!endptr)
1617 {
1618 einfo (_("%P: warning: could not find end of"
1619 " S_THUNK32 record\n"));
1620 bfd_set_error (bfd_error_bad_value);
1621 return false;
1622 }
1623
1624 end = *buf - orig_buf + sizeof (uint32_t) + endptr - data;
1625 bfd_putl32 (end, &th->end);
1626
1627 scope = *buf;
1628
1629 memcpy (*buf, data, len);
1630 *buf += len;
1631
1632 scope_level++;
1633
1634 break;
1635 }
1636
1637 case S_HEAPALLOCSITE:
1638 {
1639 struct heap_alloc_site *has = (struct heap_alloc_site *) data;
1640
1641 if (len < sizeof (struct heap_alloc_site))
1642 {
1643 einfo (_("%P: warning: truncated CodeView record"
1644 " S_HEAPALLOCSITE\n"));
1645 bfd_set_error (bfd_error_bad_value);
1646 return false;
1647 }
1648
1649 if (!remap_symbol_type (&has->type, map, num_types))
1650 {
1651 bfd_set_error (bfd_error_bad_value);
1652 return false;
1653 }
1654
1655 memcpy (*buf, data, len);
1656 *buf += len;
1657
1658 break;
1659 }
1660
1661 case S_OBJNAME: /* just copy */
1662 case S_COMPILE3:
1663 case S_UNAMESPACE:
1664 case S_FRAMEPROC:
1665 case S_FRAMECOOKIE:
1666 case S_LABEL32:
1667 case S_DEFRANGE_REGISTER_REL:
1668 case S_DEFRANGE_FRAMEPOINTER_REL:
1669 case S_DEFRANGE_SUBFIELD_REGISTER:
1670 case S_DEFRANGE_FRAMEPOINTER_REL_FULL_SCOPE:
1671 case S_DEFRANGE_REGISTER:
1672 memcpy (*buf, data, len);
1673 *buf += len;
1674 break;
1675
1676 default:
1677 einfo (_("%P: warning: unrecognized CodeView record %v\n"), type);
1678 bfd_set_error (bfd_error_bad_value);
1679 return false;
1680 }
1681
1682 data += len;
1683 size -= len;
1684 }
1685
1686 return true;
1687 }
1688
1689 /* For a given symbol subsection, work out how much space to allocate in the
1690 result module stream. This is different because we don't copy certain
1691 symbols, such as S_CONSTANT, and we skip over any procedures or data that
1692 have been GC'd out. */
1693 static bool
1694 calculate_symbols_size (uint8_t *data, uint32_t size, uint32_t *sym_size)
1695 {
1696 unsigned int scope_level = 0;
1697
1698 while (size >= sizeof (uint32_t))
1699 {
1700 uint16_t len = bfd_getl16 (data) + sizeof (uint16_t);
1701 uint16_t type = bfd_getl16 (data + sizeof (uint16_t));
1702
1703 switch (type)
1704 {
1705 case S_LDATA32:
1706 case S_LTHREAD32:
1707 {
1708 struct datasym *d = (struct datasym *) data;
1709 uint16_t section;
1710
1711 if (len < offsetof (struct datasym, name))
1712 {
1713 einfo (_("%P: warning: truncated CodeView record"
1714 " S_LDATA32/S_LTHREAD32\n"));
1715 return false;
1716 }
1717
1718 section = bfd_getl16 (&d->section);
1719
1720 /* copy if not GC'd or within function */
1721 if (scope_level != 0 || section != 0)
1722 *sym_size += len;
1723 }
1724
1725 case S_GDATA32:
1726 case S_GTHREAD32:
1727 case S_CONSTANT:
1728 /* Not copied into symbols stream. */
1729 break;
1730
1731 case S_GPROC32:
1732 case S_LPROC32:
1733 case S_GPROC32_ID:
1734 case S_LPROC32_ID:
1735 {
1736 struct procsym *proc = (struct procsym *) data;
1737 uint16_t section;
1738
1739 if (len < offsetof (struct procsym, name))
1740 {
1741 einfo (_("%P: warning: truncated CodeView record"
1742 " S_GPROC32/S_LPROC32\n"));
1743 return false;
1744 }
1745
1746 section = bfd_getl16 (&proc->section);
1747
1748 if (section != 0)
1749 {
1750 *sym_size += len;
1751 }
1752 else
1753 {
1754 uint8_t *endptr = find_end_of_scope (data, size);
1755
1756 if (!endptr)
1757 {
1758 einfo (_("%P: warning: could not find end of"
1759 " S_GPROC32/S_LPROC32 record\n"));
1760 return false;
1761 }
1762
1763 /* Skip to after S_END. */
1764
1765 size -= endptr - data;
1766 data = endptr;
1767
1768 len = bfd_getl16 (data) + sizeof (uint16_t);
1769
1770 data += len;
1771 size -= len;
1772
1773 continue;
1774 }
1775
1776 scope_level++;
1777
1778 break;
1779 }
1780
1781 case S_UDT:
1782 if (scope_level != 0) /* only goes in symbols if local */
1783 *sym_size += len;
1784 break;
1785
1786 case S_BLOCK32: /* always copied */
1787 case S_INLINESITE:
1788 case S_THUNK32:
1789 *sym_size += len;
1790 scope_level++;
1791 break;
1792
1793 case S_END: /* always copied */
1794 case S_PROC_ID_END:
1795 case S_INLINESITE_END:
1796 *sym_size += len;
1797 scope_level--;
1798 break;
1799
1800 case S_OBJNAME: /* always copied */
1801 case S_COMPILE3:
1802 case S_UNAMESPACE:
1803 case S_FRAMEPROC:
1804 case S_FRAMECOOKIE:
1805 case S_LABEL32:
1806 case S_BUILDINFO:
1807 case S_BPREL32:
1808 case S_REGISTER:
1809 case S_REGREL32:
1810 case S_LOCAL:
1811 case S_DEFRANGE_REGISTER_REL:
1812 case S_DEFRANGE_FRAMEPOINTER_REL:
1813 case S_DEFRANGE_SUBFIELD_REGISTER:
1814 case S_DEFRANGE_FRAMEPOINTER_REL_FULL_SCOPE:
1815 case S_DEFRANGE_REGISTER:
1816 case S_HEAPALLOCSITE:
1817 *sym_size += len;
1818 break;
1819
1820 default:
1821 einfo (_("%P: warning: unrecognized CodeView record %v\n"), type);
1822 return false;
1823 }
1824
1825 data += len;
1826 size -= len;
1827 }
1828
1829 return true;
1830 }
1831
1832 /* Parse the .debug$S section within an object file. */
1833 static bool
1834 handle_debugs_section (asection *s, bfd *mod, struct string_table *strings,
1835 uint8_t **dataptr, uint32_t *sizeptr,
1836 struct mod_source_files *mod_source,
1837 bfd *abfd, uint8_t **syms, uint32_t *sym_byte_size,
1838 struct type_entry **map, uint32_t num_types,
1839 bfd *sym_rec_stream, struct globals *glob,
1840 uint16_t mod_num)
1841 {
1842 bfd_byte *data = NULL;
1843 size_t off;
1844 uint32_t c13_size = 0;
1845 char *string_table = NULL;
1846 uint8_t *buf, *bufptr, *symbuf, *symbufptr;
1847 uint32_t sym_size = 0;
1848
1849 if (!bfd_get_full_section_contents (mod, s, &data))
1850 return false;
1851
1852 if (!data)
1853 return false;
1854
1855 /* Resolve relocations. Addresses are stored within the .debug$S section as
1856 a .secidx, .secrel32 pair. */
1857
1858 if (s->flags & SEC_RELOC)
1859 {
1860 struct internal_reloc *relocs;
1861 struct internal_syment *symbols;
1862 asection **sectlist;
1863 unsigned int syment_count;
1864 int sect_num;
1865 struct external_syment *ext;
1866
1867 syment_count = obj_raw_syment_count (mod);
1868
1869 relocs =
1870 _bfd_coff_read_internal_relocs (mod, s, false, NULL, true, NULL);
1871
1872 symbols = xmalloc (sizeof (struct internal_syment) * syment_count);
1873 sectlist = xmalloc (sizeof (asection *) * syment_count);
1874
1875 ext = (struct external_syment *) (coff_data (mod)->external_syms);
1876
1877 for (unsigned int i = 0; i < syment_count; i++)
1878 {
1879 bfd_coff_swap_sym_in (mod, &ext[i], &symbols[i]);
1880 }
1881
1882 sect_num = 1;
1883
1884 for (asection *sect = mod->sections; sect; sect = sect->next)
1885 {
1886 for (unsigned int i = 0; i < syment_count; i++)
1887 {
1888 if (symbols[i].n_scnum == sect_num)
1889 sectlist[i] = sect;
1890 }
1891
1892 sect_num++;
1893 }
1894
1895 if (!bfd_coff_relocate_section (abfd, coff_data (abfd)->link_info, mod,
1896 s, data, relocs, symbols, sectlist))
1897 {
1898 free (sectlist);
1899 free (symbols);
1900 free (data);
1901 return false;
1902 }
1903
1904 free (sectlist);
1905 free (symbols);
1906 }
1907
1908 if (bfd_getl32 (data) != CV_SIGNATURE_C13)
1909 {
1910 free (data);
1911 return true;
1912 }
1913
1914 off = sizeof (uint32_t);
1915
1916 /* calculate size */
1917
1918 while (off + sizeof (uint32_t) <= s->size)
1919 {
1920 uint32_t type, size;
1921
1922 type = bfd_getl32 (data + off);
1923
1924 off += sizeof (uint32_t);
1925
1926 if (off + sizeof (uint32_t) > s->size)
1927 {
1928 free (data);
1929 bfd_set_error (bfd_error_bad_value);
1930 return false;
1931 }
1932
1933 size = bfd_getl32 (data + off);
1934
1935 off += sizeof (uint32_t);
1936
1937 if (off + size > s->size)
1938 {
1939 free (data);
1940 bfd_set_error (bfd_error_bad_value);
1941 return false;
1942 }
1943
1944 switch (type)
1945 {
1946 case DEBUG_S_FILECHKSMS:
1947 c13_size += sizeof (uint32_t) + sizeof (uint32_t) + size;
1948
1949 if (c13_size % sizeof (uint32_t))
1950 c13_size += sizeof (uint32_t) - (c13_size % sizeof (uint32_t));
1951
1952 break;
1953
1954 case DEBUG_S_STRINGTABLE:
1955 parse_string_table (data + off, size, strings);
1956
1957 string_table = (char *) data + off;
1958
1959 break;
1960
1961 case DEBUG_S_LINES:
1962 {
1963 uint16_t sect;
1964
1965 if (size < sizeof (uint32_t) + sizeof (uint16_t))
1966 {
1967 free (data);
1968 bfd_set_error (bfd_error_bad_value);
1969 return false;
1970 }
1971
1972 sect = bfd_getl16 (data + off + sizeof (uint32_t));
1973
1974 /* Skip GC'd symbols. */
1975 if (sect != 0)
1976 {
1977 c13_size += sizeof (uint32_t) + sizeof (uint32_t) + size;
1978
1979 if (c13_size % sizeof (uint32_t))
1980 c13_size +=
1981 sizeof (uint32_t) - (c13_size % sizeof (uint32_t));
1982 }
1983
1984 break;
1985 }
1986
1987 case DEBUG_S_SYMBOLS:
1988 if (!calculate_symbols_size (data + off, size, &sym_size))
1989 {
1990 free (data);
1991 bfd_set_error (bfd_error_bad_value);
1992 return false;
1993 }
1994
1995 break;
1996 }
1997
1998 off += size;
1999
2000 if (off % sizeof (uint32_t))
2001 off += sizeof (uint32_t) - (off % sizeof (uint32_t));
2002 }
2003
2004 if (sym_size % sizeof (uint32_t))
2005 sym_size += sizeof (uint32_t) - (sym_size % sizeof (uint32_t));
2006
2007 if (c13_size == 0 && sym_size == 0)
2008 {
2009 free (data);
2010 return true;
2011 }
2012
2013 /* copy data */
2014
2015 buf = NULL;
2016 if (c13_size != 0)
2017 buf = xmalloc (c13_size);
2018 bufptr = buf;
2019
2020 symbuf = NULL;
2021 if (sym_size != 0)
2022 symbuf = xmalloc (sym_size);
2023 symbufptr = symbuf;
2024
2025 off = sizeof (uint32_t);
2026
2027 while (off + sizeof (uint32_t) <= s->size)
2028 {
2029 uint32_t type, size;
2030
2031 type = bfd_getl32 (data + off);
2032 off += sizeof (uint32_t);
2033
2034 size = bfd_getl32 (data + off);
2035 off += sizeof (uint32_t);
2036
2037 switch (type)
2038 {
2039 case DEBUG_S_FILECHKSMS:
2040 if (!copy_filechksms (data + off, size, string_table,
2041 strings, bufptr, mod_source))
2042 {
2043 free (data);
2044 free (symbuf);
2045 return false;
2046 }
2047
2048 bufptr += sizeof (uint32_t) + sizeof (uint32_t) + size;
2049
2050 break;
2051
2052 case DEBUG_S_LINES:
2053 {
2054 uint16_t sect;
2055
2056 sect = bfd_getl16 (data + off + sizeof (uint32_t));
2057
2058 /* Skip if GC'd. */
2059 if (sect != 0)
2060 {
2061 bfd_putl32 (type, bufptr);
2062 bufptr += sizeof (uint32_t);
2063
2064 bfd_putl32 (size, bufptr);
2065 bufptr += sizeof (uint32_t);
2066
2067 memcpy (bufptr, data + off, size);
2068 bufptr += size;
2069 }
2070
2071 break;
2072 }
2073
2074 case DEBUG_S_SYMBOLS:
2075 if (!parse_symbols (data + off, size, &symbufptr, map, num_types,
2076 sym_rec_stream, glob, mod_num))
2077 {
2078 free (data);
2079 free (symbuf);
2080 return false;
2081 }
2082
2083 break;
2084 }
2085
2086 off += size;
2087
2088 if (off % sizeof (uint32_t))
2089 off += sizeof (uint32_t) - (off % sizeof (uint32_t));
2090 }
2091
2092 free (data);
2093
2094 if (buf)
2095 {
2096 if (*dataptr)
2097 {
2098 /* Append the C13 info to what's already there, if the module has
2099 multiple .debug$S sections. */
2100
2101 *dataptr = xrealloc (*dataptr, *sizeptr + c13_size);
2102 memcpy (*dataptr + *sizeptr, buf, c13_size);
2103
2104 free (buf);
2105 }
2106 else
2107 {
2108 *dataptr = buf;
2109 }
2110
2111 *sizeptr += c13_size;
2112 }
2113
2114 if (symbuf)
2115 {
2116 if (*syms)
2117 {
2118 *syms = xrealloc (*syms, *sym_byte_size + sym_size);
2119 memcpy (*syms + *sym_byte_size, symbuf, sym_size);
2120
2121 free (symbuf);
2122 }
2123 else
2124 {
2125 *syms = symbuf;
2126 }
2127
2128 *sym_byte_size += sym_size;
2129 }
2130
2131 return true;
2132 }
2133
2134 /* Remap the type number stored in data from the per-module numbering to
2135 that of the deduplicated output list. */
2136 static bool
2137 remap_type (void *data, struct type_entry **map,
2138 uint32_t type_num, uint32_t num_types)
2139 {
2140 uint32_t type = bfd_getl32 (data);
2141
2142 /* Ignore builtin types (those with IDs below 0x1000). */
2143 if (type < TPI_FIRST_INDEX)
2144 return true;
2145
2146 if (type >= TPI_FIRST_INDEX + type_num)
2147 {
2148 einfo (_("%P: CodeView type %v references other type %v not yet "
2149 "declared\n"), TPI_FIRST_INDEX + type_num, type);
2150 return false;
2151 }
2152
2153 if (type >= TPI_FIRST_INDEX + num_types)
2154 {
2155 einfo (_("%P: CodeView type %v references out of range type %v\n"),
2156 TPI_FIRST_INDEX + type_num, type);
2157 return false;
2158 }
2159
2160 type = TPI_FIRST_INDEX + map[type - TPI_FIRST_INDEX]->index;
2161 bfd_putl32 (type, data);
2162
2163 return true;
2164 }
2165
2166 /* Determines whether the name of a struct, class, or union counts as
2167 "anonymous". Non-anonymous types have a hash based on just the name,
2168 rather than the whole structure. */
2169 static bool
2170 is_name_anonymous (char *name, size_t len)
2171 {
2172 static const char tag1[] = "<unnamed-tag>";
2173 static const char tag2[] = "__unnamed";
2174 static const char tag3[] = "::<unnamed-tag>";
2175 static const char tag4[] = "::__unnamed";
2176
2177 if (len == sizeof (tag1) - 1 && !memcmp (name, tag1, sizeof (tag1) - 1))
2178 return true;
2179
2180 if (len == sizeof (tag2) - 1 && !memcmp (name, tag2, sizeof (tag2) - 1))
2181 return true;
2182
2183 if (len >= sizeof (tag3) - 1
2184 && !memcmp (name + len - sizeof (tag3) + 1, tag3, sizeof (tag3) - 1))
2185 return true;
2186
2187 if (len >= sizeof (tag4) - 1
2188 && !memcmp (name + len - sizeof (tag4) + 1, tag4, sizeof (tag4) - 1))
2189 return true;
2190
2191 return false;
2192 }
2193
2194 /* Handle LF_UDT_SRC_LINE type entries, which are a special case. These
2195 give the source file and line number for each user-defined type that is
2196 declared. We parse these and emit instead an LF_UDT_MOD_SRC_LINE entry,
2197 which also includes the module number. */
2198 static bool
2199 handle_udt_src_line (uint8_t *data, uint16_t size, struct type_entry **map,
2200 uint32_t type_num, uint32_t num_types,
2201 struct types *ids, uint16_t mod_num,
2202 struct string_table *strings)
2203 {
2204 struct lf_udt_src_line *usl = (struct lf_udt_src_line *) data;
2205 uint32_t orig_type, source_file_type;
2206 void **slot;
2207 hashval_t hash;
2208 struct type_entry *e, *type_e, *str_e;
2209 struct lf_udt_mod_src_line *umsl;
2210 struct lf_string_id *str;
2211 uint32_t source_file_offset;
2212
2213 if (size < sizeof (struct lf_udt_src_line))
2214 {
2215 einfo (_("%P: warning: truncated CodeView type record"
2216 " LF_UDT_SRC_LINE\n"));
2217 return false;
2218 }
2219
2220 /* Check if LF_UDT_MOD_SRC_LINE already present for type, and return. */
2221
2222 orig_type = bfd_getl32 (&usl->type);
2223
2224 if (orig_type < TPI_FIRST_INDEX ||
2225 orig_type >= TPI_FIRST_INDEX + num_types ||
2226 !map[orig_type - TPI_FIRST_INDEX])
2227 {
2228 einfo (_("%P: warning: CodeView type record LF_UDT_SRC_LINE"
2229 " referred to unknown type %v\n"), orig_type);
2230 return false;
2231 }
2232
2233 type_e = map[orig_type - TPI_FIRST_INDEX];
2234
2235 /* Skip if type already declared in other module. */
2236 if (type_e->has_udt_src_line)
2237 return true;
2238
2239 if (!remap_type (&usl->type, map, type_num, num_types))
2240 return false;
2241
2242 /* Extract string from source_file_type. */
2243
2244 source_file_type = bfd_getl32 (&usl->source_file_type);
2245
2246 if (source_file_type < TPI_FIRST_INDEX ||
2247 source_file_type >= TPI_FIRST_INDEX + num_types ||
2248 !map[source_file_type - TPI_FIRST_INDEX])
2249 {
2250 einfo (_("%P: warning: CodeView type record LF_UDT_SRC_LINE"
2251 " referred to unknown string %v\n"), source_file_type);
2252 return false;
2253 }
2254
2255 str_e = map[source_file_type - TPI_FIRST_INDEX];
2256
2257 if (bfd_getl16 (str_e->data + sizeof (uint16_t)) != LF_STRING_ID)
2258 {
2259 einfo (_("%P: warning: CodeView type record LF_UDT_SRC_LINE"
2260 " pointed to unexpected record type\n"));
2261 return false;
2262 }
2263
2264 str = (struct lf_string_id *) str_e->data;
2265
2266 /* Add string to string table. */
2267
2268 source_file_offset = add_string (str->string, strlen (str->string),
2269 strings);
2270
2271 /* Add LF_UDT_MOD_SRC_LINE entry. */
2272
2273 size = sizeof (struct lf_udt_mod_src_line);
2274
2275 e = xmalloc (offsetof (struct type_entry, data) + size);
2276
2277 e->next = NULL;
2278 e->index = ids->num_types;
2279 e->has_udt_src_line = false;
2280
2281 /* LF_UDT_MOD_SRC_LINE use calc_hash on the type number, rather than
2282 the crc32 used for type hashes elsewhere. */
2283 e->cv_hash = calc_hash ((char *) &usl->type, sizeof (uint32_t));
2284
2285 type_e->has_udt_src_line = true;
2286
2287 umsl = (struct lf_udt_mod_src_line *) e->data;
2288
2289 bfd_putl16 (size - sizeof (uint16_t), &umsl->size);
2290 bfd_putl16 (LF_UDT_MOD_SRC_LINE, &umsl->kind);
2291 memcpy (&umsl->type, &usl->type, sizeof (uint32_t));
2292 bfd_putl32 (source_file_offset, &umsl->source_file_string);
2293 memcpy (&umsl->line_no, &usl->line_no, sizeof (uint32_t));
2294 bfd_putl16 (mod_num + 1, &umsl->module_no);
2295
2296 hash = iterative_hash (e->data, size, 0);
2297
2298 slot = htab_find_slot_with_hash (ids->hashmap, data, hash, INSERT);
2299 if (!slot)
2300 {
2301 free (e);
2302 return false;
2303 }
2304
2305 if (*slot)
2306 {
2307 free (e);
2308 einfo (_("%P: warning: duplicate CodeView type record "
2309 "LF_UDT_MOD_SRC_LINE\n"));
2310 return false;
2311 }
2312
2313 *slot = e;
2314
2315 if (ids->last)
2316 ids->last->next = e;
2317 else
2318 ids->first = e;
2319
2320 ids->last = e;
2321
2322 map[type_num] = e;
2323
2324 ids->num_types++;
2325
2326 return true;
2327 }
2328
2329 /* Parse a type definition in the .debug$T section. We remap the numbers
2330 of any referenced types, and if the type is not a duplicate of one
2331 already seen add it to types (for TPI types) or ids (for IPI types). */
2332 static bool
2333 handle_type (uint8_t *data, struct type_entry **map, uint32_t type_num,
2334 uint32_t num_types, struct types *types,
2335 struct types *ids, uint16_t mod_num,
2336 struct string_table *strings)
2337 {
2338 uint16_t size, type;
2339 void **slot;
2340 hashval_t hash;
2341 bool other_hash = false;
2342 uint32_t cv_hash;
2343 struct types *t;
2344 bool ipi = false;
2345
2346 size = bfd_getl16 (data) + sizeof (uint16_t);
2347 type = bfd_getl16 (data + sizeof (uint16_t));
2348
2349 switch (type)
2350 {
2351 case LF_MODIFIER:
2352 {
2353 struct lf_modifier *mod = (struct lf_modifier *) data;
2354
2355 if (size < offsetof (struct lf_modifier, modifier))
2356 {
2357 einfo (_("%P: warning: truncated CodeView type record "
2358 "LF_MODIFIER\n"));
2359 return false;
2360 }
2361
2362 if (!remap_type (&mod->base_type, map, type_num, num_types))
2363 return false;
2364
2365 break;
2366 }
2367
2368 case LF_POINTER:
2369 {
2370 struct lf_pointer *ptr = (struct lf_pointer *) data;
2371
2372 if (size < offsetof (struct lf_pointer, attributes))
2373 {
2374 einfo (_("%P: warning: truncated CodeView type record"
2375 " LF_POINTER\n"));
2376 return false;
2377 }
2378
2379 if (!remap_type (&ptr->base_type, map, type_num, num_types))
2380 return false;
2381
2382 break;
2383 }
2384
2385 case LF_PROCEDURE:
2386 {
2387 struct lf_procedure *proc = (struct lf_procedure *) data;
2388
2389 if (size < sizeof (struct lf_procedure))
2390 {
2391 einfo (_("%P: warning: truncated CodeView type record"
2392 " LF_PROCEDURE\n"));
2393 return false;
2394 }
2395
2396 if (!remap_type (&proc->return_type, map, type_num, num_types))
2397 return false;
2398
2399 if (!remap_type (&proc->arglist, map, type_num, num_types))
2400 return false;
2401
2402 break;
2403 }
2404
2405 case LF_MFUNCTION:
2406 {
2407 struct lf_mfunction *func = (struct lf_mfunction *) data;
2408
2409 if (size < sizeof (struct lf_procedure))
2410 {
2411 einfo (_("%P: warning: truncated CodeView type record"
2412 " LF_MFUNCTION\n"));
2413 return false;
2414 }
2415
2416 if (!remap_type (&func->return_type, map, type_num, num_types))
2417 return false;
2418
2419 if (!remap_type (&func->containing_class_type, map, type_num,
2420 num_types))
2421 return false;
2422
2423 if (!remap_type (&func->this_type, map, type_num, num_types))
2424 return false;
2425
2426 if (!remap_type (&func->arglist, map, type_num, num_types))
2427 return false;
2428
2429 break;
2430 }
2431
2432 case LF_ARGLIST:
2433 {
2434 uint32_t num_entries;
2435 struct lf_arglist *al = (struct lf_arglist *) data;
2436
2437 if (size < offsetof (struct lf_arglist, args))
2438 {
2439 einfo (_("%P: warning: truncated CodeView type record"
2440 " LF_ARGLIST\n"));
2441 return false;
2442 }
2443
2444 num_entries = bfd_getl32 (&al->num_entries);
2445
2446 if (size < offsetof (struct lf_arglist, args)
2447 + (num_entries * sizeof (uint32_t)))
2448 {
2449 einfo (_("%P: warning: truncated CodeView type record"
2450 " LF_ARGLIST\n"));
2451 return false;
2452 }
2453
2454 for (uint32_t i = 0; i < num_entries; i++)
2455 {
2456 if (!remap_type (&al->args[i], map, type_num, num_types))
2457 return false;
2458 }
2459
2460 break;
2461 }
2462
2463 case LF_FIELDLIST:
2464 {
2465 uint16_t left = size - sizeof (uint16_t) - sizeof (uint16_t);
2466 uint8_t *ptr = data + sizeof (uint16_t) + sizeof (uint16_t);
2467
2468 while (left > 0)
2469 {
2470 uint16_t subtype;
2471
2472 if (left < sizeof (uint16_t))
2473 {
2474 einfo (_("%P: warning: truncated CodeView type record"
2475 " LF_FIELDLIST\n"));
2476 return false;
2477 }
2478
2479 subtype = bfd_getl16 (ptr);
2480
2481 switch (subtype)
2482 {
2483 case LF_MEMBER:
2484 {
2485 struct lf_member *mem = (struct lf_member *) ptr;
2486 uint16_t offset;
2487 size_t name_len, subtype_len;
2488
2489 if (left < offsetof (struct lf_member, name))
2490 {
2491 einfo (_("%P: warning: truncated CodeView type record"
2492 " LF_MEMBER\n"));
2493 return false;
2494 }
2495
2496 if (!remap_type (&mem->type, map, type_num, num_types))
2497 return false;
2498
2499 subtype_len = offsetof (struct lf_member, name);
2500
2501 offset = bfd_getl16 (&mem->offset);
2502
2503 /* If offset >= 0x8000, actual value follows. */
2504 if (offset >= 0x8000)
2505 {
2506 unsigned int param_len = extended_value_len (offset);
2507
2508 if (param_len == 0)
2509 {
2510 einfo (_("%P: warning: unhandled type %v within"
2511 " LF_MEMBER\n"), offset);
2512 return false;
2513 }
2514
2515 subtype_len += param_len;
2516
2517 if (left < subtype_len)
2518 {
2519 einfo (_("%P: warning: truncated CodeView type record"
2520 " LF_MEMBER\n"));
2521 return false;
2522 }
2523 }
2524
2525 name_len =
2526 strnlen ((char *) mem + subtype_len, left - subtype_len);
2527
2528 if (name_len == left - offsetof (struct lf_member, name))
2529 {
2530 einfo (_("%P: warning: name for LF_MEMBER has no"
2531 " terminating zero\n"));
2532 return false;
2533 }
2534
2535 name_len++;
2536
2537 subtype_len += name_len;
2538
2539 if (subtype_len % 4 != 0)
2540 subtype_len += 4 - (subtype_len % 4);
2541
2542 if (left < subtype_len)
2543 {
2544 einfo (_("%P: warning: truncated CodeView type record"
2545 " LF_FIELDLIST\n"));
2546 return false;
2547 }
2548
2549 ptr += subtype_len;
2550 left -= subtype_len;
2551
2552 break;
2553 }
2554
2555 case LF_ENUMERATE:
2556 {
2557 struct lf_enumerate *en = (struct lf_enumerate *) ptr;
2558 size_t name_len, subtype_len;
2559 uint16_t val;
2560
2561 if (left < offsetof (struct lf_enumerate, name))
2562 {
2563 einfo (_("%P: warning: truncated CodeView type record"
2564 " LF_ENUMERATE\n"));
2565 return false;
2566 }
2567
2568 subtype_len = offsetof (struct lf_enumerate, name);
2569
2570 val = bfd_getl16 (&en->value);
2571
2572 /* If val >= 0x8000, the actual value immediately follows. */
2573 if (val >= 0x8000)
2574 {
2575 unsigned int param_len = extended_value_len (val);
2576
2577 if (param_len == 0)
2578 {
2579 einfo (_("%P: warning: unhandled type %v within"
2580 " LF_ENUMERATE\n"), val);
2581 return false;
2582 }
2583
2584 if (left < subtype_len + param_len)
2585 {
2586 einfo (_("%P: warning: truncated CodeView type"
2587 " record LF_ENUMERATE\n"));
2588 return false;
2589 }
2590
2591 subtype_len += param_len;
2592 }
2593
2594 name_len = strnlen ((char *) ptr + subtype_len,
2595 left - subtype_len);
2596
2597 if (name_len == left - offsetof (struct lf_enumerate, name))
2598 {
2599 einfo (_("%P: warning: name for LF_ENUMERATE has no"
2600 " terminating zero\n"));
2601 return false;
2602 }
2603
2604 name_len++;
2605
2606 subtype_len += name_len;
2607
2608 if (subtype_len % 4 != 0)
2609 subtype_len += 4 - (subtype_len % 4);
2610
2611 if (left < subtype_len)
2612 {
2613 einfo (_("%P: warning: truncated CodeView type record"
2614 " LF_ENUMERATE\n"));
2615 return false;
2616 }
2617
2618 ptr += subtype_len;
2619 left -= subtype_len;
2620
2621 break;
2622 }
2623
2624 case LF_INDEX:
2625 {
2626 struct lf_index *ind = (struct lf_index *) ptr;
2627
2628 if (left < sizeof (struct lf_index))
2629 {
2630 einfo (_("%P: warning: truncated CodeView type record"
2631 " LF_INDEX\n"));
2632 return false;
2633 }
2634
2635 if (!remap_type (&ind->index, map, type_num, num_types))
2636 return false;
2637
2638 ptr += sizeof (struct lf_index);
2639 left -= sizeof (struct lf_index);
2640
2641 break;
2642 }
2643
2644 case LF_ONEMETHOD:
2645 {
2646 struct lf_onemethod *meth = (struct lf_onemethod *) ptr;
2647 size_t name_len, subtype_len;
2648
2649 if (left < offsetof (struct lf_onemethod, name))
2650 {
2651 einfo (_("%P: warning: truncated CodeView type record"
2652 " LF_ONEMETHOD\n"));
2653 return false;
2654 }
2655
2656 if (!remap_type (&meth->method_type, map, type_num,
2657 num_types))
2658 return false;
2659
2660 name_len =
2661 strnlen (meth->name,
2662 left - offsetof (struct lf_onemethod, name));
2663
2664 if (name_len == left - offsetof (struct lf_onemethod, name))
2665 {
2666 einfo (_("%P: warning: name for LF_ONEMETHOD has no"
2667 " terminating zero\n"));
2668 return false;
2669 }
2670
2671 name_len++;
2672
2673 subtype_len = offsetof (struct lf_onemethod, name)
2674 + name_len;
2675
2676 if (subtype_len % 4 != 0)
2677 subtype_len += 4 - (subtype_len % 4);
2678
2679 if (left < subtype_len)
2680 {
2681 einfo (_("%P: warning: truncated CodeView type record"
2682 " LF_FIELDLIST\n"));
2683 return false;
2684 }
2685
2686 ptr += subtype_len;
2687 left -= subtype_len;
2688
2689 break;
2690 }
2691
2692 case LF_METHOD:
2693 {
2694 struct lf_method *meth = (struct lf_method *) ptr;
2695 size_t name_len, subtype_len;
2696
2697 if (left < offsetof (struct lf_method, name))
2698 {
2699 einfo (_("%P: warning: truncated CodeView type record"
2700 " LF_METHOD\n"));
2701 return false;
2702 }
2703
2704 if (!remap_type (&meth->method_list, map, type_num,
2705 num_types))
2706 return false;
2707
2708 name_len =
2709 strnlen (meth->name,
2710 left - offsetof (struct lf_method, name));
2711
2712 if (name_len == left - offsetof (struct lf_method, name))
2713 {
2714 einfo (_("%P: warning: name for LF_METHOD has no"
2715 " terminating zero\n"));
2716 return false;
2717 }
2718
2719 name_len++;
2720
2721 subtype_len = offsetof (struct lf_method, name) + name_len;
2722
2723 if (subtype_len % 4 != 0)
2724 subtype_len += 4 - (subtype_len % 4);
2725
2726 if (left < subtype_len)
2727 {
2728 einfo (_("%P: warning: truncated CodeView type record"
2729 " LF_FIELDLIST\n"));
2730 return false;
2731 }
2732
2733 ptr += subtype_len;
2734 left -= subtype_len;
2735
2736 break;
2737 }
2738
2739 case LF_BCLASS:
2740 {
2741 struct lf_bclass *bc = (struct lf_bclass *) ptr;
2742 size_t subtype_len;
2743 uint16_t offset;
2744
2745 if (left < sizeof (struct lf_bclass))
2746 {
2747 einfo (_("%P: warning: truncated CodeView type record"
2748 " LF_BCLASS\n"));
2749 return false;
2750 }
2751
2752 if (!remap_type (&bc->base_class_type, map, type_num,
2753 num_types))
2754 return false;
2755
2756 subtype_len = sizeof (struct lf_bclass);
2757
2758 offset = bfd_getl16 (&bc->offset);
2759
2760 /* If offset >= 0x8000, actual value follows. */
2761 if (offset >= 0x8000)
2762 {
2763 unsigned int param_len = extended_value_len (offset);
2764
2765 if (param_len == 0)
2766 {
2767 einfo (_("%P: warning: unhandled type %v within"
2768 " LF_BCLASS\n"), offset);
2769 return false;
2770 }
2771
2772 subtype_len += param_len;
2773
2774 if (left < subtype_len)
2775 {
2776 einfo (_("%P: warning: truncated CodeView type record"
2777 " LF_BCLASS\n"));
2778 return false;
2779 }
2780 }
2781
2782 if (subtype_len % 4 != 0)
2783 subtype_len += 4 - (subtype_len % 4);
2784
2785 if (left < subtype_len)
2786 {
2787 einfo (_("%P: warning: truncated CodeView type record"
2788 " LF_BCLASS\n"));
2789 return false;
2790 }
2791
2792 ptr += subtype_len;
2793 left -= subtype_len;
2794
2795 break;
2796 }
2797
2798 case LF_VFUNCTAB:
2799 {
2800 struct lf_vfunctab *vft = (struct lf_vfunctab *) ptr;
2801
2802 if (left < sizeof (struct lf_vfunctab))
2803 {
2804 einfo (_("%P: warning: truncated CodeView type record"
2805 " LF_VFUNCTAB\n"));
2806 return false;
2807 }
2808
2809 if (!remap_type (&vft->type, map, type_num, num_types))
2810 return false;
2811
2812 ptr += sizeof (struct lf_vfunctab);
2813 left -= sizeof (struct lf_vfunctab);
2814
2815 break;
2816 }
2817
2818 case LF_VBCLASS:
2819 case LF_IVBCLASS:
2820 {
2821 struct lf_vbclass *vbc = (struct lf_vbclass *) ptr;
2822 size_t subtype_len;
2823 uint16_t offset;
2824
2825 if (left < sizeof (struct lf_vbclass))
2826 {
2827 einfo (_("%P: warning: truncated CodeView type record"
2828 " LF_VBCLASS/LF_IVBCLASS\n"));
2829 return false;
2830 }
2831
2832 if (!remap_type (&vbc->base_class_type, map, type_num,
2833 num_types))
2834 return false;
2835
2836 if (!remap_type (&vbc->virtual_base_pointer_type, map,
2837 type_num, num_types))
2838 return false;
2839
2840 subtype_len = offsetof (struct lf_vbclass,
2841 virtual_base_vbtable_offset);
2842
2843 offset = bfd_getl16 (&vbc->virtual_base_pointer_offset);
2844
2845 /* If offset >= 0x8000, actual value follows. */
2846 if (offset >= 0x8000)
2847 {
2848 unsigned int param_len = extended_value_len (offset);
2849
2850 if (param_len == 0)
2851 {
2852 einfo (_("%P: warning: unhandled type %v within"
2853 " LF_VBCLASS/LF_IVBCLASS\n"), offset);
2854 return false;
2855 }
2856
2857 subtype_len += param_len;
2858
2859 if (left < subtype_len)
2860 {
2861 einfo (_("%P: warning: truncated CodeView type record"
2862 " LF_VBCLASS/LF_IVBCLASS\n"));
2863 return false;
2864 }
2865 }
2866
2867 offset = bfd_getl16 ((char *)vbc + subtype_len);
2868 subtype_len += sizeof (uint16_t);
2869
2870 /* If offset >= 0x8000, actual value follows. */
2871 if (offset >= 0x8000)
2872 {
2873 unsigned int param_len = extended_value_len (offset);
2874
2875 if (param_len == 0)
2876 {
2877 einfo (_("%P: warning: unhandled type %v within"
2878 " LF_VBCLASS/LF_IVBCLASS\n"), offset);
2879 return false;
2880 }
2881
2882 subtype_len += param_len;
2883
2884 if (left < subtype_len)
2885 {
2886 einfo (_("%P: warning: truncated CodeView type record"
2887 " LF_VBCLASS/LF_IVBCLASS\n"));
2888 return false;
2889 }
2890 }
2891
2892 if (subtype_len % 4 != 0)
2893 subtype_len += 4 - (subtype_len % 4);
2894
2895 if (left < subtype_len)
2896 {
2897 einfo (_("%P: warning: truncated CodeView type record"
2898 " LF_VBCLASS/LF_IVBCLASS\n"));
2899 return false;
2900 }
2901
2902 ptr += subtype_len;
2903 left -= subtype_len;
2904
2905 break;
2906 }
2907
2908 case LF_STMEMBER:
2909 {
2910 struct lf_static_member *st =
2911 (struct lf_static_member *) ptr;
2912 size_t name_len, subtype_len;
2913
2914 if (left < offsetof (struct lf_static_member, name))
2915 {
2916 einfo (_("%P: warning: truncated CodeView type record"
2917 " LF_STMEMBER\n"));
2918 return false;
2919 }
2920
2921 if (!remap_type (&st->type, map, type_num, num_types))
2922 return false;
2923
2924 name_len =
2925 strnlen (st->name,
2926 left - offsetof (struct lf_static_member, name));
2927
2928 if (name_len == left
2929 - offsetof (struct lf_static_member, name))
2930 {
2931 einfo (_("%P: warning: name for LF_STMEMBER has no"
2932 " terminating zero\n"));
2933 return false;
2934 }
2935
2936 name_len++;
2937
2938 subtype_len = offsetof (struct lf_static_member, name)
2939 + name_len;
2940
2941 if (subtype_len % 4 != 0)
2942 subtype_len += 4 - (subtype_len % 4);
2943
2944 if (left < subtype_len)
2945 {
2946 einfo (_("%P: warning: truncated CodeView type record"
2947 " LF_FIELDLIST\n"));
2948 return false;
2949 }
2950
2951 ptr += subtype_len;
2952 left -= subtype_len;
2953
2954 break;
2955 }
2956
2957 case LF_NESTTYPE:
2958 {
2959 struct lf_nest_type *nest = (struct lf_nest_type *) ptr;
2960 size_t name_len, subtype_len;
2961
2962 if (left < offsetof (struct lf_nest_type, name))
2963 {
2964 einfo (_("%P: warning: truncated CodeView type record"
2965 " LF_NESTTYPE\n"));
2966 return false;
2967 }
2968
2969 if (!remap_type (&nest->type, map, type_num, num_types))
2970 return false;
2971
2972 name_len =
2973 strnlen (nest->name,
2974 left - offsetof (struct lf_nest_type, name));
2975
2976 if (name_len == left - offsetof (struct lf_nest_type, name))
2977 {
2978 einfo (_("%P: warning: name for LF_NESTTYPE has no"
2979 " terminating zero\n"));
2980 return false;
2981 }
2982
2983 name_len++;
2984
2985 subtype_len = offsetof (struct lf_nest_type, name)
2986 + name_len;
2987
2988 if (subtype_len % 4 != 0)
2989 subtype_len += 4 - (subtype_len % 4);
2990
2991 if (left < subtype_len)
2992 {
2993 einfo (_("%P: warning: truncated CodeView type record"
2994 " LF_FIELDLIST\n"));
2995 return false;
2996 }
2997
2998 ptr += subtype_len;
2999 left -= subtype_len;
3000
3001 break;
3002 }
3003
3004 default:
3005 einfo (_("%P: warning: unrecognized CodeView subtype %v\n"),
3006 subtype);
3007 return false;
3008 }
3009 }
3010
3011 break;
3012 }
3013
3014 case LF_BITFIELD:
3015 {
3016 struct lf_bitfield *bf = (struct lf_bitfield *) data;
3017
3018 if (size < offsetof (struct lf_bitfield, length))
3019 {
3020 einfo (_("%P: warning: truncated CodeView type record"
3021 " LF_BITFIELD\n"));
3022 return false;
3023 }
3024
3025 if (!remap_type (&bf->base_type, map, type_num, num_types))
3026 return false;
3027
3028 break;
3029 }
3030
3031 case LF_METHODLIST:
3032 {
3033 struct lf_methodlist *ml = (struct lf_methodlist *) data;
3034 unsigned int num_entries;
3035
3036 if (size < offsetof (struct lf_methodlist, entries))
3037 {
3038 einfo (_("%P: warning: truncated CodeView type record"
3039 " LF_METHODLIST\n"));
3040 return false;
3041 }
3042
3043 if ((size - offsetof (struct lf_methodlist, entries))
3044 % sizeof (struct lf_methodlist_entry))
3045 {
3046 einfo (_("%P: warning: malformed CodeView type record"
3047 " LF_METHODLIST\n"));
3048 return false;
3049 }
3050
3051 num_entries = (size - offsetof (struct lf_methodlist, entries))
3052 / sizeof (struct lf_methodlist_entry);
3053
3054 for (unsigned int i = 0; i < num_entries; i++)
3055 {
3056 if (!remap_type (&ml->entries[i].method_type, map,
3057 type_num, num_types))
3058 return false;
3059 }
3060
3061 break;
3062 }
3063
3064 case LF_ARRAY:
3065 {
3066 struct lf_array *arr = (struct lf_array *) data;
3067
3068 if (size < offsetof (struct lf_array, length_in_bytes))
3069 {
3070 einfo (_("%P: warning: truncated CodeView type record"
3071 " LF_ARRAY\n"));
3072 return false;
3073 }
3074
3075 if (!remap_type (&arr->element_type, map, type_num, num_types))
3076 return false;
3077
3078 if (!remap_type (&arr->index_type, map, type_num, num_types))
3079 return false;
3080
3081 break;
3082 }
3083
3084 case LF_CLASS:
3085 case LF_STRUCTURE:
3086 {
3087 struct lf_class *cl = (struct lf_class *) data;
3088 uint16_t prop, num_bytes;
3089 size_t name_len, name_off;
3090
3091 if (size < offsetof (struct lf_class, name))
3092 {
3093 einfo (_("%P: warning: truncated CodeView type record"
3094 " LF_CLASS/LF_STRUCTURE\n"));
3095 return false;
3096 }
3097
3098 if (!remap_type (&cl->field_list, map, type_num, num_types))
3099 return false;
3100
3101 if (!remap_type (&cl->derived_from, map, type_num, num_types))
3102 return false;
3103
3104 if (!remap_type (&cl->vshape, map, type_num, num_types))
3105 return false;
3106
3107 name_off = offsetof (struct lf_class, name);
3108
3109 num_bytes = bfd_getl16 (&cl->length);
3110
3111 /* If num_bytes >= 0x8000, actual value follows. */
3112 if (num_bytes >= 0x8000)
3113 {
3114 unsigned int param_len = extended_value_len (num_bytes);
3115
3116 if (param_len == 0)
3117 {
3118 einfo (_("%P: warning: unhandled type %v within"
3119 " LF_CLASS/LF_STRUCTURE\n"), num_bytes);
3120 return false;
3121 }
3122
3123 name_off += param_len;
3124
3125 if (size < name_off)
3126 {
3127 einfo (_("%P: warning: truncated CodeView type record"
3128 " LF_CLASS/LF_STRUCTURE\n"));
3129 return false;
3130 }
3131 }
3132
3133 name_len = strnlen ((char *) cl + name_off, size - name_off);
3134
3135 if (name_len == size - name_off)
3136 {
3137 einfo (_("%P: warning: name for LF_CLASS/LF_STRUCTURE has no"
3138 " terminating zero\n"));
3139 return false;
3140 }
3141
3142 prop = bfd_getl16 (&cl->properties);
3143
3144 if (prop & CV_PROP_HAS_UNIQUE_NAME)
3145 {
3146 /* Structure has another name following first one. */
3147
3148 size_t len = name_off + name_len + 1;
3149 size_t unique_name_len;
3150
3151 unique_name_len = strnlen ((char *) cl + name_off + name_len + 1,
3152 size - len);
3153
3154 if (unique_name_len == size - len)
3155 {
3156 einfo (_("%P: warning: unique name for LF_CLASS/LF_STRUCTURE"
3157 " has no terminating zero\n"));
3158 return false;
3159 }
3160 }
3161
3162 if (!(prop & (CV_PROP_FORWARD_REF | CV_PROP_SCOPED))
3163 && !is_name_anonymous ((char *) cl + name_off, name_len))
3164 {
3165 other_hash = true;
3166 cv_hash = crc32 ((uint8_t *) cl + name_off, name_len);
3167 }
3168
3169 break;
3170 }
3171
3172 case LF_UNION:
3173 {
3174 struct lf_union *un = (struct lf_union *) data;
3175 uint16_t prop, num_bytes;
3176 size_t name_len, name_off;
3177
3178 if (size < offsetof (struct lf_union, name))
3179 {
3180 einfo (_("%P: warning: truncated CodeView type record"
3181 " LF_UNION\n"));
3182 return false;
3183 }
3184
3185 if (!remap_type (&un->field_list, map, type_num, num_types))
3186 return false;
3187
3188 name_off = offsetof (struct lf_union, name);
3189
3190 num_bytes = bfd_getl16 (&un->length);
3191
3192 /* If num_bytes >= 0x8000, actual value follows. */
3193 if (num_bytes >= 0x8000)
3194 {
3195 unsigned int param_len = extended_value_len (num_bytes);
3196
3197 if (param_len == 0)
3198 {
3199 einfo (_("%P: warning: unhandled type %v within"
3200 " LF_UNION\n"), num_bytes);
3201 return false;
3202 }
3203
3204 name_off += param_len;
3205
3206 if (size < name_off)
3207 {
3208 einfo (_("%P: warning: truncated CodeView type record"
3209 " LF_UNION\n"));
3210 return false;
3211 }
3212 }
3213
3214 name_len = strnlen ((char *) un + name_off, size - name_off);
3215
3216 if (name_len == size - name_off)
3217 {
3218 einfo (_("%P: warning: name for LF_UNION has no"
3219 " terminating zero\n"));
3220 return false;
3221 }
3222
3223 prop = bfd_getl16 (&un->properties);
3224
3225 if (prop & CV_PROP_HAS_UNIQUE_NAME)
3226 {
3227 /* Structure has another name following first one. */
3228
3229 size_t len = name_off + name_len + 1;
3230 size_t unique_name_len;
3231
3232 unique_name_len = strnlen ((char *) un + name_off + name_len + 1,
3233 size - len);
3234
3235 if (unique_name_len == size - len)
3236 {
3237 einfo (_("%P: warning: unique name for LF_UNION has"
3238 " no terminating zero\n"));
3239 return false;
3240 }
3241 }
3242
3243 if (!(prop & (CV_PROP_FORWARD_REF | CV_PROP_SCOPED))
3244 && !is_name_anonymous ((char *) un + name_off, name_len))
3245 {
3246 other_hash = true;
3247 cv_hash = crc32 ((uint8_t *) un + name_off, name_len);
3248 }
3249
3250 break;
3251 }
3252
3253 case LF_ENUM:
3254 {
3255 struct lf_enum *en = (struct lf_enum *) data;
3256 uint16_t prop;
3257 size_t name_len;
3258
3259 if (size < offsetof (struct lf_enum, name))
3260 {
3261 einfo (_("%P: warning: truncated CodeView type record"
3262 " LF_ENUM\n"));
3263 return false;
3264 }
3265
3266 if (!remap_type (&en->underlying_type, map, type_num, num_types))
3267 return false;
3268
3269 if (!remap_type (&en->field_list, map, type_num, num_types))
3270 return false;
3271
3272 name_len = strnlen (en->name, size - offsetof (struct lf_enum, name));
3273
3274 if (name_len == size - offsetof (struct lf_enum, name))
3275 {
3276 einfo (_("%P: warning: name for LF_ENUM has no"
3277 " terminating zero\n"));
3278 return false;
3279 }
3280
3281 prop = bfd_getl16 (&en->properties);
3282
3283 if (prop & CV_PROP_HAS_UNIQUE_NAME)
3284 {
3285 /* Structure has another name following first one. */
3286
3287 size_t len = offsetof (struct lf_enum, name) + name_len + 1;
3288 size_t unique_name_len;
3289
3290 unique_name_len = strnlen (en->name + name_len + 1, size - len);
3291
3292 if (unique_name_len == size - len)
3293 {
3294 einfo (_("%P: warning: unique name for LF_ENUM has"
3295 " no terminating zero\n"));
3296 return false;
3297 }
3298 }
3299
3300 break;
3301 }
3302
3303 case LF_VTSHAPE:
3304 /* Does not reference any types, nothing to be done. */
3305 break;
3306
3307 case LF_VFTABLE:
3308 {
3309 struct lf_vftable *vft = (struct lf_vftable *) data;
3310
3311 if (size < offsetof (struct lf_vftable, names))
3312 {
3313 einfo (_("%P: warning: truncated CodeView type record"
3314 " LF_VFTABLE\n"));
3315 return false;
3316 }
3317
3318 if (!remap_type (&vft->type, map, type_num, num_types))
3319 return false;
3320
3321 if (!remap_type (&vft->base_vftable, map, type_num, num_types))
3322 return false;
3323
3324 break;
3325 }
3326
3327 case LF_STRING_ID:
3328 {
3329 struct lf_string_id *str = (struct lf_string_id *) data;
3330 size_t string_len;
3331
3332 if (size < offsetof (struct lf_string_id, string))
3333 {
3334 einfo (_("%P: warning: truncated CodeView type record"
3335 " LF_STRING_ID\n"));
3336 return false;
3337 }
3338
3339 if (!remap_type (&str->substring, map, type_num, num_types))
3340 return false;
3341
3342 string_len = strnlen (str->string,
3343 size - offsetof (struct lf_string_id, string));
3344
3345 if (string_len == size - offsetof (struct lf_string_id, string))
3346 {
3347 einfo (_("%P: warning: string for LF_STRING_ID has no"
3348 " terminating zero\n"));
3349 return false;
3350 }
3351
3352 ipi = true;
3353
3354 break;
3355 }
3356
3357 case LF_SUBSTR_LIST:
3358 {
3359 uint32_t num_entries;
3360 struct lf_arglist *ssl = (struct lf_arglist *) data;
3361
3362 if (size < offsetof (struct lf_arglist, args))
3363 {
3364 einfo (_("%P: warning: truncated CodeView type record"
3365 " LF_SUBSTR_LIST\n"));
3366 return false;
3367 }
3368
3369 num_entries = bfd_getl32 (&ssl->num_entries);
3370
3371 if (size < offsetof (struct lf_arglist, args)
3372 + (num_entries * sizeof (uint32_t)))
3373 {
3374 einfo (_("%P: warning: truncated CodeView type record"
3375 " LF_SUBSTR_LIST\n"));
3376 return false;
3377 }
3378
3379 for (uint32_t i = 0; i < num_entries; i++)
3380 {
3381 if (!remap_type (&ssl->args[i], map, type_num, num_types))
3382 return false;
3383 }
3384
3385 ipi = true;
3386
3387 break;
3388 }
3389
3390 case LF_BUILDINFO:
3391 {
3392 uint16_t num_entries;
3393 struct lf_build_info *bi = (struct lf_build_info *) data;
3394
3395 if (size < offsetof (struct lf_build_info, strings))
3396 {
3397 einfo (_("%P: warning: truncated CodeView type record"
3398 " LF_BUILDINFO\n"));
3399 return false;
3400 }
3401
3402 num_entries = bfd_getl16 (&bi->count);
3403
3404 if (size < offsetof (struct lf_build_info, strings)
3405 + (num_entries * sizeof (uint32_t)))
3406 {
3407 einfo (_("%P: warning: truncated CodeView type record"
3408 " LF_BUILDINFO\n"));
3409 return false;
3410 }
3411
3412 for (uint32_t i = 0; i < num_entries; i++)
3413 {
3414 if (!remap_type (&bi->strings[i], map, type_num, num_types))
3415 return false;
3416 }
3417
3418 ipi = true;
3419
3420 break;
3421 }
3422
3423 case LF_FUNC_ID:
3424 {
3425 struct lf_func_id *func = (struct lf_func_id *) data;
3426 size_t name_len;
3427
3428 if (size < offsetof (struct lf_func_id, name))
3429 {
3430 einfo (_("%P: warning: truncated CodeView type record"
3431 " LF_FUNC_ID\n"));
3432 return false;
3433 }
3434
3435 if (!remap_type (&func->parent_scope, map, type_num, num_types))
3436 return false;
3437
3438 if (!remap_type (&func->function_type, map, type_num, num_types))
3439 return false;
3440
3441 name_len = strnlen (func->name,
3442 size - offsetof (struct lf_func_id, name));
3443
3444 if (name_len == size - offsetof (struct lf_func_id, name))
3445 {
3446 einfo (_("%P: warning: string for LF_FUNC_ID has no"
3447 " terminating zero\n"));
3448 return false;
3449 }
3450
3451 ipi = true;
3452
3453 break;
3454 }
3455
3456 case LF_MFUNC_ID:
3457 {
3458 struct lf_mfunc_id *mfunc = (struct lf_mfunc_id *) data;
3459 size_t name_len;
3460
3461 if (size < offsetof (struct lf_mfunc_id, name))
3462 {
3463 einfo (_("%P: warning: truncated CodeView type record"
3464 " LF_MFUNC_ID\n"));
3465 return false;
3466 }
3467
3468 if (!remap_type (&mfunc->parent_type, map, type_num, num_types))
3469 return false;
3470
3471 if (!remap_type (&mfunc->function_type, map, type_num, num_types))
3472 return false;
3473
3474 name_len = strnlen (mfunc->name,
3475 size - offsetof (struct lf_mfunc_id, name));
3476
3477 if (name_len == size - offsetof (struct lf_mfunc_id, name))
3478 {
3479 einfo (_("%P: warning: string for LF_MFUNC_ID has no"
3480 " terminating zero\n"));
3481 return false;
3482 }
3483
3484 ipi = true;
3485
3486 break;
3487 }
3488
3489 case LF_UDT_SRC_LINE:
3490 return handle_udt_src_line (data, size, map, type_num, num_types,
3491 ids, mod_num, strings);
3492
3493 default:
3494 einfo (_("%P: warning: unrecognized CodeView type %v\n"), type);
3495 return false;
3496 }
3497
3498 hash = iterative_hash (data, size, 0);
3499
3500 t = ipi ? ids : types;
3501
3502 slot = htab_find_slot_with_hash (t->hashmap, data, hash, INSERT);
3503 if (!slot)
3504 return false;
3505
3506 if (!*slot) /* new entry */
3507 {
3508 struct type_entry *e;
3509
3510 *slot = xmalloc (offsetof (struct type_entry, data) + size);
3511
3512 e = (struct type_entry *) *slot;
3513
3514 e->next = NULL;
3515 e->index = t->num_types;
3516
3517 if (other_hash)
3518 e->cv_hash = cv_hash;
3519 else
3520 e->cv_hash = crc32 (data, size);
3521
3522 e->has_udt_src_line = false;
3523
3524 memcpy (e->data, data, size);
3525
3526 if (t->last)
3527 t->last->next = e;
3528 else
3529 t->first = e;
3530
3531 t->last = e;
3532
3533 map[type_num] = e;
3534
3535 t->num_types++;
3536 }
3537 else /* duplicate */
3538 {
3539 map[type_num] = (struct type_entry *) *slot;
3540 }
3541
3542 return true;
3543 }
3544
3545 /* Parse the .debug$T section of a module, and pass any type definitions
3546 found to handle_type. */
3547 static bool
3548 handle_debugt_section (asection *s, bfd *mod, struct types *types,
3549 struct types *ids, uint16_t mod_num,
3550 struct string_table *strings,
3551 struct type_entry ***map, uint32_t *num_types)
3552 {
3553 bfd_byte *data = NULL;
3554 size_t off;
3555 uint32_t type_num;
3556
3557 if (!bfd_get_full_section_contents (mod, s, &data))
3558 return false;
3559
3560 if (!data)
3561 return false;
3562
3563 if (bfd_getl32 (data) != CV_SIGNATURE_C13)
3564 {
3565 free (data);
3566 return true;
3567 }
3568
3569 off = sizeof (uint32_t);
3570
3571 while (off + sizeof (uint16_t) <= s->size)
3572 {
3573 uint16_t size;
3574
3575 size = bfd_getl16 (data + off);
3576 off += sizeof (uint16_t);
3577
3578 if (size + off > s->size || size <= sizeof (uint16_t))
3579 {
3580 free (data);
3581 bfd_set_error (bfd_error_bad_value);
3582 return false;
3583 }
3584
3585 (*num_types)++;
3586 off += size;
3587 }
3588
3589 if (*num_types == 0)
3590 {
3591 free (data);
3592 return true;
3593 }
3594
3595 *map = xcalloc (*num_types, sizeof (struct type_entry *));
3596
3597 off = sizeof (uint32_t);
3598 type_num = 0;
3599
3600 while (off + sizeof (uint16_t) <= s->size)
3601 {
3602 uint16_t size;
3603
3604 size = bfd_getl16 (data + off);
3605
3606 if (!handle_type (data + off, *map, type_num, *num_types, types, ids,
3607 mod_num, strings))
3608 {
3609 free (data);
3610 free (*map);
3611 bfd_set_error (bfd_error_bad_value);
3612 return false;
3613 }
3614
3615 off += sizeof (uint16_t) + size;
3616 type_num++;
3617 }
3618
3619 free (data);
3620
3621 return true;
3622 }
3623
3624 /* Return the CodeView constant for the selected architecture. */
3625 static uint16_t
3626 target_processor (bfd *abfd)
3627 {
3628 switch (abfd->arch_info->arch)
3629 {
3630 case bfd_arch_i386:
3631 if (abfd->arch_info->mach & bfd_mach_x86_64)
3632 return CV_CFL_X64;
3633 else
3634 return CV_CFL_80386;
3635
3636 case bfd_arch_aarch64:
3637 return CV_CFL_ARM64;
3638
3639 default:
3640 return 0;
3641 }
3642 }
3643
3644 /* Create the symbols that go in "* Linker *", the dummy module created
3645 for the linker itself. */
3646 static bool
3647 create_linker_symbols (bfd *abfd, uint8_t **syms, uint32_t *sym_byte_size,
3648 const char *pdb_name)
3649 {
3650 uint8_t *ptr;
3651 struct objname *name;
3652 struct compile3 *comp;
3653 struct envblock *env;
3654 size_t padding1, padding2, env_size;
3655 char *cwdval, *exeval, *pdbval;
3656
3657 /* extra NUL for padding */
3658 static const char linker_fn[] = "* Linker *\0";
3659 static const char linker_name[] = "GNU LD " VERSION;
3660
3661 static const char cwd[] = "cwd";
3662 static const char exe[] = "exe";
3663 static const char pdb[] = "pdb";
3664
3665 cwdval = getcwd (NULL, 0);
3666 if (!cwdval)
3667 {
3668 einfo (_("%P: warning: unable to get working directory\n"));
3669 return false;
3670 }
3671
3672 exeval = lrealpath (program_name);
3673
3674 if (!exeval)
3675 {
3676 einfo (_("%P: warning: unable to get program name\n"));
3677 free (cwdval);
3678 return false;
3679 }
3680
3681 pdbval = lrealpath (pdb_name);
3682
3683 if (!pdbval)
3684 {
3685 einfo (_("%P: warning: unable to get full path to PDB\n"));
3686 free (exeval);
3687 free (cwdval);
3688 return false;
3689 }
3690
3691 *sym_byte_size += offsetof (struct objname, name) + sizeof (linker_fn);
3692 *sym_byte_size += offsetof (struct compile3, compiler) + sizeof (linker_name);
3693
3694 if (*sym_byte_size % 4)
3695 padding1 = 4 - (*sym_byte_size % 4);
3696 else
3697 padding1 = 0;
3698
3699 *sym_byte_size += padding1;
3700
3701 env_size = offsetof (struct envblock, strings);
3702 env_size += sizeof (cwd);
3703 env_size += strlen (cwdval) + 1;
3704 env_size += sizeof (exe);
3705 env_size += strlen (exeval) + 1;
3706 env_size += sizeof (pdb);
3707 env_size += strlen (pdbval) + 1;
3708
3709 if (env_size % 4)
3710 padding2 = 4 - (env_size % 4);
3711 else
3712 padding2 = 0;
3713
3714 env_size += padding2;
3715
3716 *sym_byte_size += env_size;
3717
3718 *syms = xmalloc (*sym_byte_size);
3719 ptr = *syms;
3720
3721 /* Write S_OBJNAME */
3722
3723 name = (struct objname *) ptr;
3724 bfd_putl16 (offsetof (struct objname, name)
3725 + sizeof (linker_fn) - sizeof (uint16_t), &name->size);
3726 bfd_putl16 (S_OBJNAME, &name->kind);
3727 bfd_putl32 (0, &name->signature);
3728 memcpy (name->name, linker_fn, sizeof (linker_fn));
3729
3730 ptr += offsetof (struct objname, name) + sizeof (linker_fn);
3731
3732 /* Write S_COMPILE3 */
3733
3734 comp = (struct compile3 *) ptr;
3735
3736 bfd_putl16 (offsetof (struct compile3, compiler) + sizeof (linker_name)
3737 + padding1 - sizeof (uint16_t), &comp->size);
3738 bfd_putl16 (S_COMPILE3, &comp->kind);
3739 bfd_putl32 (CV_CFL_LINK, &comp->flags);
3740 bfd_putl16 (target_processor (abfd), &comp->machine);
3741 bfd_putl16 (0, &comp->frontend_major);
3742 bfd_putl16 (0, &comp->frontend_minor);
3743 bfd_putl16 (0, &comp->frontend_build);
3744 bfd_putl16 (0, &comp->frontend_qfe);
3745 bfd_putl16 (0, &comp->backend_major);
3746 bfd_putl16 (0, &comp->backend_minor);
3747 bfd_putl16 (0, &comp->backend_build);
3748 bfd_putl16 (0, &comp->backend_qfe);
3749 memcpy (comp->compiler, linker_name, sizeof (linker_name));
3750
3751 memset (comp->compiler + sizeof (linker_name), 0, padding1);
3752
3753 ptr += offsetof (struct compile3, compiler) + sizeof (linker_name) + padding1;
3754
3755 /* Write S_ENVBLOCK */
3756
3757 env = (struct envblock *) ptr;
3758
3759 bfd_putl16 (env_size - sizeof (uint16_t), &env->size);
3760 bfd_putl16 (S_ENVBLOCK, &env->kind);
3761 env->flags = 0;
3762
3763 ptr += offsetof (struct envblock, strings);
3764
3765 memcpy (ptr, cwd, sizeof (cwd));
3766 ptr += sizeof (cwd);
3767 memcpy (ptr, cwdval, strlen (cwdval) + 1);
3768 ptr += strlen (cwdval) + 1;
3769
3770 memcpy (ptr, exe, sizeof (exe));
3771 ptr += sizeof (exe);
3772 memcpy (ptr, exeval, strlen (exeval) + 1);
3773 ptr += strlen (exeval) + 1;
3774
3775 memcpy (ptr, pdb, sizeof (pdb));
3776 ptr += sizeof (pdb);
3777 memcpy (ptr, pdbval, strlen (pdbval) + 1);
3778 ptr += strlen (pdbval) + 1;
3779
3780 /* Microsoft's LINK also includes "cmd", the command-line options passed
3781 to the linker, but unfortunately we don't have access to argc and argv
3782 at this stage. */
3783
3784 memset (ptr, 0, padding2);
3785
3786 free (pdbval);
3787 free (exeval);
3788 free (cwdval);
3789
3790 return true;
3791 }
3792
3793 /* Populate the module stream, which consists of the transformed .debug$S
3794 data for each object file. */
3795 static bool
3796 populate_module_stream (bfd *stream, bfd *mod, uint32_t *sym_byte_size,
3797 struct string_table *strings,
3798 uint32_t *c13_info_size,
3799 struct mod_source_files *mod_source,
3800 bfd *abfd, struct types *types,
3801 struct types *ids, uint16_t mod_num,
3802 bfd *sym_rec_stream, struct globals *glob,
3803 const char *pdb_name)
3804 {
3805 uint8_t int_buf[sizeof (uint32_t)];
3806 uint8_t *c13_info = NULL;
3807 uint8_t *syms = NULL;
3808
3809 *sym_byte_size = 0;
3810 *c13_info_size = 0;
3811
3812 if (!strcmp (bfd_get_filename (mod), "dll stuff"))
3813 {
3814 if (!create_linker_symbols (mod, &syms, sym_byte_size, pdb_name))
3815 return false;
3816 }
3817 else
3818 {
3819 struct type_entry **map = NULL;
3820 uint32_t num_types = 0;
3821
3822 /* Process .debug$T section. */
3823
3824 for (asection *s = mod->sections; s; s = s->next)
3825 {
3826 if (!strcmp (s->name, ".debug$T") && s->size >= sizeof (uint32_t))
3827 {
3828 if (!handle_debugt_section (s, mod, types, ids, mod_num, strings,
3829 &map, &num_types))
3830 {
3831 free (mod_source->files);
3832 return false;
3833 }
3834
3835 break;
3836 }
3837 }
3838
3839 /* Process .debug$S section(s). */
3840
3841 for (asection *s = mod->sections; s; s = s->next)
3842 {
3843 if (!strcmp (s->name, ".debug$S") && s->size >= sizeof (uint32_t))
3844 {
3845 if (!handle_debugs_section (s, mod, strings, &c13_info,
3846 c13_info_size, mod_source, abfd,
3847 &syms, sym_byte_size, map, num_types,
3848 sym_rec_stream, glob, mod_num))
3849 {
3850 free (c13_info);
3851 free (syms);
3852 free (mod_source->files);
3853 free (map);
3854 return false;
3855 }
3856 }
3857 }
3858
3859 free (map);
3860 }
3861
3862 /* Write the signature. */
3863
3864 bfd_putl32 (CV_SIGNATURE_C13, int_buf);
3865
3866 if (bfd_bwrite (int_buf, sizeof (uint32_t), stream) != sizeof (uint32_t))
3867 {
3868 free (c13_info);
3869 free (syms);
3870 return false;
3871 }
3872
3873 if (syms)
3874 {
3875 if (bfd_bwrite (syms, *sym_byte_size, stream) != *sym_byte_size)
3876 {
3877 free (c13_info);
3878 free (syms);
3879 return false;
3880 }
3881
3882 free (syms);
3883 }
3884
3885 if (c13_info)
3886 {
3887 if (bfd_bwrite (c13_info, *c13_info_size, stream) != *c13_info_size)
3888 {
3889 free (c13_info);
3890 return false;
3891 }
3892
3893 free (c13_info);
3894 }
3895
3896 /* Write the global refs size. */
3897
3898 bfd_putl32 (0, int_buf);
3899
3900 if (bfd_bwrite (int_buf, sizeof (uint32_t), stream) != sizeof (uint32_t))
3901 return false;
3902
3903 return true;
3904 }
3905
3906 /* Create the module info substream within the DBI. */
3907 static bool
3908 create_module_info_substream (bfd *abfd, bfd *pdb, void **data,
3909 uint32_t *size, struct string_table *strings,
3910 struct source_files_info *source,
3911 struct types *types, struct types *ids,
3912 bfd *sym_rec_stream, struct globals *glob,
3913 const char *pdb_name)
3914 {
3915 uint8_t *ptr;
3916 unsigned int mod_num;
3917
3918 static const char linker_fn[] = "* Linker *";
3919
3920 *size = 0;
3921
3922 for (bfd *in = coff_data (abfd)->link_info->input_bfds; in;
3923 in = in->link.next)
3924 {
3925 size_t len = sizeof (struct module_info);
3926
3927 if (!strcmp (bfd_get_filename (in), "dll stuff"))
3928 {
3929 len += sizeof (linker_fn); /* Object name. */
3930 len++; /* Empty module name. */
3931 }
3932 else if (in->my_archive)
3933 {
3934 char *name = lrealpath (bfd_get_filename (in));
3935
3936 len += strlen (name) + 1; /* Object name. */
3937
3938 free (name);
3939
3940 name = lrealpath (bfd_get_filename (in->my_archive));
3941
3942 len += strlen (name) + 1; /* Archive name. */
3943
3944 free (name);
3945 }
3946 else
3947 {
3948 char *name = lrealpath (bfd_get_filename (in));
3949 size_t name_len = strlen (name) + 1;
3950
3951 len += name_len; /* Object name. */
3952 len += name_len; /* And again as the archive name. */
3953
3954 free (name);
3955 }
3956
3957 if (len % 4)
3958 len += 4 - (len % 4);
3959
3960 *size += len;
3961
3962 source->mod_count++;
3963 }
3964
3965 *data = xmalloc (*size);
3966
3967 ptr = *data;
3968
3969 source->mods = xmalloc (source->mod_count
3970 * sizeof (struct mod_source_files));
3971 memset (source->mods, 0,
3972 source->mod_count * sizeof (struct mod_source_files));
3973
3974 mod_num = 0;
3975
3976 for (bfd *in = coff_data (abfd)->link_info->input_bfds; in;
3977 in = in->link.next)
3978 {
3979 struct module_info *mod = (struct module_info *) ptr;
3980 uint16_t stream_num;
3981 bfd *stream;
3982 uint32_t sym_byte_size, c13_info_size;
3983 uint8_t *start = ptr;
3984
3985 stream = add_stream (pdb, NULL, &stream_num);
3986
3987 if (!stream)
3988 {
3989 for (unsigned int i = 0; i < source->mod_count; i++)
3990 {
3991 free (source->mods[i].files);
3992 }
3993
3994 free (source->mods);
3995 free (*data);
3996 return false;
3997 }
3998
3999 if (!populate_module_stream (stream, in, &sym_byte_size,
4000 strings, &c13_info_size,
4001 &source->mods[mod_num], abfd,
4002 types, ids, mod_num,
4003 sym_rec_stream, glob, pdb_name))
4004 {
4005 for (unsigned int i = 0; i < source->mod_count; i++)
4006 {
4007 free (source->mods[i].files);
4008 }
4009
4010 free (source->mods);
4011 free (*data);
4012 return false;
4013 }
4014
4015 bfd_putl32 (0, &mod->unused1);
4016
4017 /* These are dummy values - MSVC copies the first section contribution
4018 entry here, but doesn't seem to use it for anything. */
4019 bfd_putl16 (0xffff, &mod->sc.section);
4020 bfd_putl16 (0, &mod->sc.padding1);
4021 bfd_putl32 (0, &mod->sc.offset);
4022 bfd_putl32 (0xffffffff, &mod->sc.size);
4023 bfd_putl32 (0, &mod->sc.characteristics);
4024 bfd_putl16 (0xffff, &mod->sc.module_index);
4025 bfd_putl16 (0, &mod->sc.padding2);
4026 bfd_putl32 (0, &mod->sc.data_crc);
4027 bfd_putl32 (0, &mod->sc.reloc_crc);
4028
4029 bfd_putl16 (0, &mod->flags);
4030 bfd_putl16 (stream_num, &mod->module_sym_stream);
4031 bfd_putl32 (sizeof (uint32_t) + sym_byte_size, &mod->sym_byte_size);
4032 bfd_putl32 (0, &mod->c11_byte_size);
4033 bfd_putl32 (c13_info_size, &mod->c13_byte_size);
4034 bfd_putl16 (0, &mod->source_file_count);
4035 bfd_putl16 (0, &mod->padding);
4036 bfd_putl32 (0, &mod->unused2);
4037 bfd_putl32 (0, &mod->source_file_name_index);
4038 bfd_putl32 (0, &mod->pdb_file_path_name_index);
4039
4040 ptr += sizeof (struct module_info);
4041
4042 if (!strcmp (bfd_get_filename (in), "dll stuff"))
4043 {
4044 /* Object name. */
4045 memcpy (ptr, linker_fn, sizeof (linker_fn));
4046 ptr += sizeof (linker_fn);
4047
4048 /* Empty module name. */
4049 *ptr = 0;
4050 ptr++;
4051 }
4052 else if (in->my_archive)
4053 {
4054 char *name = lrealpath (bfd_get_filename (in));
4055 size_t name_len = strlen (name) + 1;
4056
4057 /* Object name. */
4058 memcpy (ptr, name, name_len);
4059 ptr += name_len;
4060
4061 free (name);
4062
4063 name = lrealpath (bfd_get_filename (in->my_archive));
4064 name_len = strlen (name) + 1;
4065
4066 /* Archive name. */
4067 memcpy (ptr, name, name_len);
4068 ptr += name_len;
4069
4070 free (name);
4071 }
4072 else
4073 {
4074 char *name = lrealpath (bfd_get_filename (in));
4075 size_t name_len = strlen (name) + 1;
4076
4077 /* Object name. */
4078 memcpy (ptr, name, name_len);
4079 ptr += name_len;
4080
4081 /* Object name again as archive name. */
4082 memcpy (ptr, name, name_len);
4083 ptr += name_len;
4084
4085 free (name);
4086 }
4087
4088 /* Pad to next four-byte boundary. */
4089
4090 if ((ptr - start) % 4)
4091 {
4092 memset (ptr, 0, 4 - ((ptr - start) % 4));
4093 ptr += 4 - ((ptr - start) % 4);
4094 }
4095
4096 mod_num++;
4097 }
4098
4099 return true;
4100 }
4101
4102 /* Return the index of a given output section. */
4103 static uint16_t
4104 find_section_number (bfd *abfd, asection *sect)
4105 {
4106 uint16_t i = 1;
4107
4108 for (asection *s = abfd->sections; s; s = s->next)
4109 {
4110 if (s == sect)
4111 return i;
4112
4113 /* Empty sections aren't output. */
4114 if (s->size != 0)
4115 i++;
4116 }
4117
4118 return 0;
4119 }
4120
4121 /* Create the substream which maps addresses in the image file to locations
4122 in the original object files. */
4123 static bool
4124 create_section_contrib_substream (bfd *abfd, void **data, uint32_t *size)
4125 {
4126 unsigned int num_sc = 0;
4127 struct section_contribution *sc;
4128 uint16_t mod_index;
4129 char *sect_flags;
4130 file_ptr offset;
4131
4132 for (bfd *in = coff_data (abfd)->link_info->input_bfds; in;
4133 in = in->link.next)
4134 {
4135 for (asection *s = in->sections; s; s = s->next)
4136 {
4137 if (s->size == 0 || discarded_section (s))
4138 continue;
4139
4140 num_sc++;
4141 }
4142 }
4143
4144 *size = sizeof (uint32_t) + (num_sc * sizeof (struct section_contribution));
4145 *data = xmalloc (*size);
4146
4147 bfd_putl32 (SECTION_CONTRIB_VERSION_60, *data);
4148
4149 /* Read characteristics of outputted sections. */
4150
4151 sect_flags = xmalloc (sizeof (uint32_t) * abfd->section_count);
4152
4153 offset = bfd_coff_filhsz (abfd) + bfd_coff_aoutsz (abfd);
4154 offset += offsetof (struct external_scnhdr, s_flags);
4155
4156 for (unsigned int i = 0; i < abfd->section_count; i++)
4157 {
4158 bfd_seek (abfd, offset, SEEK_SET);
4159
4160 if (bfd_bread (sect_flags + (i * sizeof (uint32_t)), sizeof (uint32_t),
4161 abfd) != sizeof (uint32_t))
4162 {
4163 free (*data);
4164 free (sect_flags);
4165 return false;
4166 }
4167
4168 offset += sizeof (struct external_scnhdr);
4169 }
4170
4171 sc =
4172 (struct section_contribution *) ((uint8_t *) *data + sizeof (uint32_t));
4173
4174 mod_index = 0;
4175 for (bfd *in = coff_data (abfd)->link_info->input_bfds; in;
4176 in = in->link.next)
4177 {
4178 for (asection *s = in->sections; s; s = s->next)
4179 {
4180 uint16_t sect_num;
4181
4182 if (s->size == 0 || discarded_section (s))
4183 continue;
4184
4185 sect_num = find_section_number (abfd, s->output_section);
4186
4187 memcpy (&sc->characteristics,
4188 sect_flags + ((sect_num - 1) * sizeof (uint32_t)),
4189 sizeof (uint32_t));
4190
4191 bfd_putl16 (sect_num, &sc->section);
4192 bfd_putl16 (0, &sc->padding1);
4193 bfd_putl32 (s->output_offset, &sc->offset);
4194 bfd_putl32 (s->size, &sc->size);
4195 bfd_putl16 (mod_index, &sc->module_index);
4196 bfd_putl16 (0, &sc->padding2);
4197 bfd_putl32 (0, &sc->data_crc);
4198 bfd_putl32 (0, &sc->reloc_crc);
4199
4200 sc++;
4201 }
4202
4203 mod_index++;
4204 }
4205
4206 free (sect_flags);
4207
4208 return true;
4209 }
4210
4211 /* The source info substream lives within the DBI stream, and lists the
4212 source files for each object file (i.e. it's derived from the
4213 DEBUG_S_FILECHKSMS parts of the .debug$S sections). This is a bit
4214 superfluous, as the filenames are also available in the C13 parts of
4215 the module streams, but MSVC relies on it to work properly. */
4216 static void
4217 create_source_info_substream (void **data, uint32_t *size,
4218 struct source_files_info *source)
4219 {
4220 uint16_t dedupe_source_files_count = 0;
4221 uint16_t source_files_count = 0;
4222 uint32_t strings_len = 0;
4223 uint8_t *ptr;
4224
4225 /* Loop through the source files, marking unique filenames. The pointers
4226 here are for entries in the main string table, and so have already
4227 been deduplicated. */
4228
4229 for (uint16_t i = 0; i < source->mod_count; i++)
4230 {
4231 for (uint16_t j = 0; j < source->mods[i].files_count; j++)
4232 {
4233 if (source->mods[i].files[j])
4234 {
4235 if (source->mods[i].files[j]->source_file_offset == 0xffffffff)
4236 {
4237 source->mods[i].files[j]->source_file_offset = strings_len;
4238 strings_len += source->mods[i].files[j]->len + 1;
4239 dedupe_source_files_count++;
4240 }
4241
4242 source_files_count++;
4243 }
4244 }
4245 }
4246
4247 *size = sizeof (uint16_t) + sizeof (uint16_t);
4248 *size += (sizeof (uint16_t) + sizeof (uint16_t)) * source->mod_count;
4249 *size += sizeof (uint32_t) * source_files_count;
4250 *size += strings_len;
4251
4252 *data = xmalloc (*size);
4253
4254 ptr = (uint8_t *) *data;
4255
4256 /* Write header (module count and source file count). */
4257
4258 bfd_putl16 (source->mod_count, ptr);
4259 ptr += sizeof (uint16_t);
4260
4261 bfd_putl16 (dedupe_source_files_count, ptr);
4262 ptr += sizeof (uint16_t);
4263
4264 /* Write "ModIndices". As the LLVM documentation puts it, "this array is
4265 present, but does not appear to be useful". */
4266
4267 for (uint16_t i = 0; i < source->mod_count; i++)
4268 {
4269 bfd_putl16 (i, ptr);
4270 ptr += sizeof (uint16_t);
4271 }
4272
4273 /* Write source file count for each module. */
4274
4275 for (uint16_t i = 0; i < source->mod_count; i++)
4276 {
4277 bfd_putl16 (source->mods[i].files_count, ptr);
4278 ptr += sizeof (uint16_t);
4279 }
4280
4281 /* For each module, write the offsets within the string table
4282 for each source file. */
4283
4284 for (uint16_t i = 0; i < source->mod_count; i++)
4285 {
4286 for (uint16_t j = 0; j < source->mods[i].files_count; j++)
4287 {
4288 if (source->mods[i].files[j])
4289 {
4290 bfd_putl32 (source->mods[i].files[j]->source_file_offset, ptr);
4291 ptr += sizeof (uint32_t);
4292 }
4293 }
4294 }
4295
4296 /* Write the string table. We set source_file_offset to a dummy value for
4297 each entry we write, so we don't write duplicate filenames. */
4298
4299 for (uint16_t i = 0; i < source->mod_count; i++)
4300 {
4301 for (uint16_t j = 0; j < source->mods[i].files_count; j++)
4302 {
4303 if (source->mods[i].files[j]
4304 && source->mods[i].files[j]->source_file_offset != 0xffffffff)
4305 {
4306 memcpy (ptr, source->mods[i].files[j]->s,
4307 source->mods[i].files[j]->len);
4308 ptr += source->mods[i].files[j]->len;
4309
4310 *ptr = 0;
4311 ptr++;
4312
4313 source->mods[i].files[j]->source_file_offset = 0xffffffff;
4314 }
4315 }
4316 }
4317 }
4318
4319 /* Used as parameter to qsort, to sort globals by hash. */
4320 static int
4321 global_compare_hash (const void *s1, const void *s2)
4322 {
4323 const struct global *g1 = *(const struct global **) s1;
4324 const struct global *g2 = *(const struct global **) s2;
4325
4326 if (g1->hash < g2->hash)
4327 return -1;
4328 if (g1->hash > g2->hash)
4329 return 1;
4330
4331 return 0;
4332 }
4333
4334 /* Create the globals stream, which contains the unmangled symbol names. */
4335 static bool
4336 create_globals_stream (bfd *pdb, struct globals *glob, uint16_t *stream_num)
4337 {
4338 bfd *stream;
4339 struct globals_hash_header h;
4340 uint32_t buckets_size, filled_buckets = 0;
4341 struct global **sorted = NULL;
4342 bool ret = false;
4343 struct global *buckets[NUM_GLOBALS_HASH_BUCKETS];
4344 char int_buf[sizeof (uint32_t)];
4345
4346 stream = add_stream (pdb, NULL, stream_num);
4347 if (!stream)
4348 return false;
4349
4350 memset (buckets, 0, sizeof (buckets));
4351
4352 if (glob->num_entries > 0)
4353 {
4354 struct global *g;
4355
4356 /* Create an array of pointers, sorted by hash value. */
4357
4358 sorted = xmalloc (sizeof (struct global *) * glob->num_entries);
4359
4360 g = glob->first;
4361 for (unsigned int i = 0; i < glob->num_entries; i++)
4362 {
4363 sorted[i] = g;
4364 g = g->next;
4365 }
4366
4367 qsort (sorted, glob->num_entries, sizeof (struct global *),
4368 global_compare_hash);
4369
4370 /* Populate the buckets. */
4371
4372 for (unsigned int i = 0; i < glob->num_entries; i++)
4373 {
4374 if (!buckets[sorted[i]->hash])
4375 {
4376 buckets[sorted[i]->hash] = sorted[i];
4377 filled_buckets++;
4378 }
4379
4380 sorted[i]->index = i;
4381 }
4382 }
4383
4384 buckets_size = NUM_GLOBALS_HASH_BUCKETS / 8;
4385 buckets_size += sizeof (uint32_t);
4386 buckets_size += filled_buckets * sizeof (uint32_t);
4387
4388 bfd_putl32 (GLOBALS_HASH_SIGNATURE, &h.signature);
4389 bfd_putl32 (GLOBALS_HASH_VERSION_70, &h.version);
4390 bfd_putl32 (glob->num_entries * sizeof (struct hash_record),
4391 &h.entries_size);
4392 bfd_putl32 (buckets_size, &h.buckets_size);
4393
4394 if (bfd_bwrite (&h, sizeof (h), stream) != sizeof (h))
4395 return false;
4396
4397 /* Write hash entries, sorted by hash. */
4398
4399 for (unsigned int i = 0; i < glob->num_entries; i++)
4400 {
4401 struct hash_record hr;
4402
4403 bfd_putl32 (sorted[i]->offset + 1, &hr.offset);
4404 bfd_putl32 (sorted[i]->refcount, &hr.reference);
4405
4406 if (bfd_bwrite (&hr, sizeof (hr), stream) != sizeof (hr))
4407 goto end;
4408 }
4409
4410 /* Write the bitmap for filled and unfilled buckets. */
4411
4412 for (unsigned int i = 0; i < NUM_GLOBALS_HASH_BUCKETS; i += 8)
4413 {
4414 uint8_t v = 0;
4415
4416 for (unsigned int j = 0; j < 8; j++)
4417 {
4418 if (buckets[i + j])
4419 v |= 1 << j;
4420 }
4421
4422 if (bfd_bwrite (&v, sizeof (v), stream) != sizeof (v))
4423 goto end;
4424 }
4425
4426 /* Add a 4-byte gap. */
4427
4428 bfd_putl32 (0, int_buf);
4429
4430 if (bfd_bwrite (int_buf, sizeof (uint32_t), stream) != sizeof (uint32_t))
4431 goto end;
4432
4433 /* Write the bucket offsets. */
4434
4435 for (unsigned int i = 0; i < NUM_GLOBALS_HASH_BUCKETS; i++)
4436 {
4437 if (buckets[i])
4438 {
4439 /* 0xc is size of internal hash_record structure in
4440 Microsoft's parser. */
4441 bfd_putl32 (buckets[i]->index * 0xc, int_buf);
4442
4443 if (bfd_bwrite (int_buf, sizeof (uint32_t), stream) !=
4444 sizeof (uint32_t))
4445 goto end;
4446 }
4447 }
4448
4449 ret = true;
4450
4451 end:
4452 free (sorted);
4453
4454 return ret;
4455 }
4456
4457 /* Hash an entry in the globals list. */
4458 static hashval_t
4459 hash_global_entry (const void *p)
4460 {
4461 const struct global *g = (const struct global *) p;
4462 uint16_t len = bfd_getl16 (g->data);
4463
4464 return iterative_hash (g->data, len, 0);
4465 }
4466
4467 /* Compare an entry in the globals list with a symbol. */
4468 static int
4469 eq_global_entry (const void *a, const void *b)
4470 {
4471 const struct global *g = (const struct global *) a;
4472 uint16_t len1, len2;
4473
4474 len1 = bfd_getl16 (g->data) + sizeof (uint16_t);
4475 len2 = bfd_getl16 (b) + sizeof (uint16_t);
4476
4477 if (len1 != len2)
4478 return 0;
4479
4480 return !memcmp (g->data, b, len1);
4481 }
4482
4483 /* Stream 4 is the debug information (DBI) stream. */
4484 static bool
4485 populate_dbi_stream (bfd *stream, bfd *abfd, bfd *pdb,
4486 uint16_t section_header_stream_num,
4487 uint16_t sym_rec_stream_num,
4488 uint16_t publics_stream_num,
4489 struct string_table *strings,
4490 struct types *types,
4491 struct types *ids,
4492 bfd *sym_rec_stream, const char *pdb_name)
4493 {
4494 struct pdb_dbi_stream_header h;
4495 struct optional_dbg_header opt;
4496 void *mod_info, *sc, *source_info;
4497 uint32_t mod_info_size, sc_size, source_info_size;
4498 struct source_files_info source;
4499 struct globals glob;
4500 uint16_t globals_stream_num;
4501
4502 source.mod_count = 0;
4503 source.mods = NULL;
4504
4505 glob.num_entries = 0;
4506 glob.first = NULL;
4507 glob.last = NULL;
4508
4509 glob.hashmap = htab_create_alloc (0, hash_global_entry,
4510 eq_global_entry, free, xcalloc, free);
4511
4512 if (!create_module_info_substream (abfd, pdb, &mod_info, &mod_info_size,
4513 strings, &source, types, ids,
4514 sym_rec_stream, &glob, pdb_name))
4515 {
4516 htab_delete (glob.hashmap);
4517 return false;
4518 }
4519
4520 if (!create_globals_stream (pdb, &glob, &globals_stream_num))
4521 {
4522 htab_delete (glob.hashmap);
4523
4524 for (unsigned int i = 0; i < source.mod_count; i++)
4525 {
4526 free (source.mods[i].files);
4527 }
4528 free (source.mods);
4529
4530 free (mod_info);
4531 return false;
4532 }
4533
4534 htab_delete (glob.hashmap);
4535
4536 if (!create_section_contrib_substream (abfd, &sc, &sc_size))
4537 {
4538 for (unsigned int i = 0; i < source.mod_count; i++)
4539 {
4540 free (source.mods[i].files);
4541 }
4542 free (source.mods);
4543
4544 free (mod_info);
4545 return false;
4546 }
4547
4548 create_source_info_substream (&source_info, &source_info_size, &source);
4549
4550 for (unsigned int i = 0; i < source.mod_count; i++)
4551 {
4552 free (source.mods[i].files);
4553 }
4554 free (source.mods);
4555
4556 bfd_putl32 (0xffffffff, &h.version_signature);
4557 bfd_putl32 (DBI_STREAM_VERSION_70, &h.version_header);
4558 bfd_putl32 (1, &h.age);
4559 bfd_putl16 (globals_stream_num, &h.global_stream_index);
4560 bfd_putl16 (0x8e1d, &h.build_number); // MSVC 14.29
4561 bfd_putl16 (publics_stream_num, &h.public_stream_index);
4562 bfd_putl16 (0, &h.pdb_dll_version);
4563 bfd_putl16 (sym_rec_stream_num, &h.sym_record_stream);
4564 bfd_putl16 (0, &h.pdb_dll_rbld);
4565 bfd_putl32 (mod_info_size, &h.mod_info_size);
4566 bfd_putl32 (sc_size, &h.section_contribution_size);
4567 bfd_putl32 (0, &h.section_map_size);
4568 bfd_putl32 (source_info_size, &h.source_info_size);
4569 bfd_putl32 (0, &h.type_server_map_size);
4570 bfd_putl32 (0, &h.mfc_type_server_index);
4571 bfd_putl32 (sizeof (opt), &h.optional_dbg_header_size);
4572 bfd_putl32 (0, &h.ec_substream_size);
4573 bfd_putl16 (0, &h.flags);
4574 bfd_putl16 (get_arch_number (abfd), &h.machine);
4575 bfd_putl32 (0, &h.padding);
4576
4577 if (bfd_bwrite (&h, sizeof (h), stream) != sizeof (h))
4578 {
4579 free (source_info);
4580 free (sc);
4581 free (mod_info);
4582 return false;
4583 }
4584
4585 if (bfd_bwrite (mod_info, mod_info_size, stream) != mod_info_size)
4586 {
4587 free (source_info);
4588 free (sc);
4589 free (mod_info);
4590 return false;
4591 }
4592
4593 free (mod_info);
4594
4595 if (bfd_bwrite (sc, sc_size, stream) != sc_size)
4596 {
4597 free (source_info);
4598 free (sc);
4599 return false;
4600 }
4601
4602 free (sc);
4603
4604 if (bfd_bwrite (source_info, source_info_size, stream) != source_info_size)
4605 {
4606 free (source_info);
4607 return false;
4608 }
4609
4610 free (source_info);
4611
4612 bfd_putl16 (0xffff, &opt.fpo_stream);
4613 bfd_putl16 (0xffff, &opt.exception_stream);
4614 bfd_putl16 (0xffff, &opt.fixup_stream);
4615 bfd_putl16 (0xffff, &opt.omap_to_src_stream);
4616 bfd_putl16 (0xffff, &opt.omap_from_src_stream);
4617 bfd_putl16 (section_header_stream_num, &opt.section_header_stream);
4618 bfd_putl16 (0xffff, &opt.token_map_stream);
4619 bfd_putl16 (0xffff, &opt.xdata_stream);
4620 bfd_putl16 (0xffff, &opt.pdata_stream);
4621 bfd_putl16 (0xffff, &opt.new_fpo_stream);
4622 bfd_putl16 (0xffff, &opt.orig_section_header_stream);
4623
4624 if (bfd_bwrite (&opt, sizeof (opt), stream) != sizeof (opt))
4625 return false;
4626
4627 return true;
4628 }
4629
4630 /* Used as parameter to qsort, to sort publics by hash. */
4631 static int
4632 public_compare_hash (const void *s1, const void *s2)
4633 {
4634 const struct public *p1 = *(const struct public **) s1;
4635 const struct public *p2 = *(const struct public **) s2;
4636
4637 if (p1->hash < p2->hash)
4638 return -1;
4639 if (p1->hash > p2->hash)
4640 return 1;
4641
4642 return 0;
4643 }
4644
4645 /* Used as parameter to qsort, to sort publics by address. */
4646 static int
4647 public_compare_addr (const void *s1, const void *s2)
4648 {
4649 const struct public *p1 = *(const struct public **) s1;
4650 const struct public *p2 = *(const struct public **) s2;
4651
4652 if (p1->section < p2->section)
4653 return -1;
4654 if (p1->section > p2->section)
4655 return 1;
4656
4657 if (p1->address < p2->address)
4658 return -1;
4659 if (p1->address > p2->address)
4660 return 1;
4661
4662 return 0;
4663 }
4664
4665 /* The publics stream is a hash map of S_PUB32 records, which are stored
4666 in the symbol record stream. Each S_PUB32 entry represents a symbol
4667 from the point of view of the linker: a section index, an offset within
4668 the section, and a mangled name. Compare with S_GDATA32 and S_GPROC32,
4669 which are the same thing but generated by the compiler. */
4670 static bool
4671 populate_publics_stream (bfd *stream, bfd *abfd, bfd *sym_rec_stream)
4672 {
4673 struct publics_header header;
4674 struct globals_hash_header hash_header;
4675 const unsigned int num_buckets = 4096;
4676 unsigned int num_entries = 0, filled_buckets = 0;
4677 unsigned int buckets_size, sym_hash_size;
4678 char int_buf[sizeof (uint32_t)];
4679 struct public *publics_head = NULL, *publics_tail = NULL;
4680 struct public **buckets;
4681 struct public **sorted = NULL;
4682 bool ret = false;
4683
4684 buckets = xmalloc (sizeof (struct public *) * num_buckets);
4685 memset (buckets, 0, sizeof (struct public *) * num_buckets);
4686
4687 /* Loop through the global symbols in our input files, and write S_PUB32
4688 records in the symbol record stream for those that make it into the
4689 final image. */
4690 for (bfd *in = coff_data (abfd)->link_info->input_bfds; in;
4691 in = in->link.next)
4692 {
4693 if (!in->outsymbols)
4694 continue;
4695
4696 for (unsigned int i = 0; i < in->symcount; i++)
4697 {
4698 struct bfd_symbol *sym = in->outsymbols[i];
4699
4700 if (sym->flags & BSF_GLOBAL)
4701 {
4702 struct pubsym ps;
4703 uint16_t record_length;
4704 const char *name = sym->name;
4705 size_t name_len = strlen (name);
4706 struct public *p = xmalloc (sizeof (struct public));
4707 unsigned int padding = 0;
4708 uint16_t section;
4709 uint32_t flags = 0;
4710
4711 section =
4712 find_section_number (abfd, sym->section->output_section);
4713
4714 if (section == 0)
4715 continue;
4716
4717 p->next = NULL;
4718 p->offset = bfd_tell (sym_rec_stream);
4719 p->hash = calc_hash (name, name_len) % num_buckets;
4720 p->section = section;
4721 p->address = sym->section->output_offset + sym->value;
4722
4723 record_length = sizeof (struct pubsym) + name_len + 1;
4724
4725 if (record_length % 4)
4726 padding = 4 - (record_length % 4);
4727
4728 /* Assume that all global symbols in executable sections
4729 are functions. */
4730 if (sym->section->flags & SEC_CODE)
4731 flags = PUBSYM_FUNCTION;
4732
4733 bfd_putl16 (record_length + padding - sizeof (uint16_t),
4734 &ps.record_length);
4735 bfd_putl16 (S_PUB32, &ps.record_type);
4736 bfd_putl32 (flags, &ps.flags);
4737 bfd_putl32 (p->address, &ps.offset);
4738 bfd_putl16 (p->section, &ps.section);
4739
4740 if (bfd_bwrite (&ps, sizeof (struct pubsym), sym_rec_stream) !=
4741 sizeof (struct pubsym))
4742 goto end;
4743
4744 if (bfd_bwrite (name, name_len + 1, sym_rec_stream) !=
4745 name_len + 1)
4746 goto end;
4747
4748 for (unsigned int j = 0; j < padding; j++)
4749 {
4750 uint8_t b = 0;
4751
4752 if (bfd_bwrite (&b, sizeof (uint8_t), sym_rec_stream) !=
4753 sizeof (uint8_t))
4754 goto end;
4755 }
4756
4757 if (!publics_head)
4758 publics_head = p;
4759 else
4760 publics_tail->next = p;
4761
4762 publics_tail = p;
4763 num_entries++;
4764 }
4765 }
4766 }
4767
4768
4769 if (num_entries > 0)
4770 {
4771 /* Create an array of pointers, sorted by hash value. */
4772
4773 sorted = xmalloc (sizeof (struct public *) * num_entries);
4774
4775 struct public *p = publics_head;
4776 for (unsigned int i = 0; i < num_entries; i++)
4777 {
4778 sorted[i] = p;
4779 p = p->next;
4780 }
4781
4782 qsort (sorted, num_entries, sizeof (struct public *),
4783 public_compare_hash);
4784
4785 /* Populate the buckets. */
4786
4787 for (unsigned int i = 0; i < num_entries; i++)
4788 {
4789 if (!buckets[sorted[i]->hash])
4790 {
4791 buckets[sorted[i]->hash] = sorted[i];
4792 filled_buckets++;
4793 }
4794
4795 sorted[i]->index = i;
4796 }
4797 }
4798
4799 buckets_size = num_buckets / 8;
4800 buckets_size += sizeof (uint32_t);
4801 buckets_size += filled_buckets * sizeof (uint32_t);
4802
4803 sym_hash_size = sizeof (hash_header);
4804 sym_hash_size += num_entries * sizeof (struct hash_record);
4805 sym_hash_size += buckets_size;
4806
4807 /* Output the publics header. */
4808
4809 bfd_putl32 (sym_hash_size, &header.sym_hash_size);
4810 bfd_putl32 (num_entries * sizeof (uint32_t), &header.addr_map_size);
4811 bfd_putl32 (0, &header.num_thunks);
4812 bfd_putl32 (0, &header.thunks_size);
4813 bfd_putl32 (0, &header.thunk_table);
4814 bfd_putl32 (0, &header.thunk_table_offset);
4815 bfd_putl32 (0, &header.num_sects);
4816
4817 if (bfd_bwrite (&header, sizeof (header), stream) != sizeof (header))
4818 goto end;
4819
4820 /* Output the global hash header. */
4821
4822 bfd_putl32 (GLOBALS_HASH_SIGNATURE, &hash_header.signature);
4823 bfd_putl32 (GLOBALS_HASH_VERSION_70, &hash_header.version);
4824 bfd_putl32 (num_entries * sizeof (struct hash_record),
4825 &hash_header.entries_size);
4826 bfd_putl32 (buckets_size, &hash_header.buckets_size);
4827
4828 if (bfd_bwrite (&hash_header, sizeof (hash_header), stream) !=
4829 sizeof (hash_header))
4830 goto end;
4831
4832 /* Write the entries in hash order. */
4833
4834 for (unsigned int i = 0; i < num_entries; i++)
4835 {
4836 struct hash_record hr;
4837
4838 bfd_putl32 (sorted[i]->offset + 1, &hr.offset);
4839 bfd_putl32 (1, &hr.reference);
4840
4841 if (bfd_bwrite (&hr, sizeof (hr), stream) != sizeof (hr))
4842 goto end;
4843 }
4844
4845 /* Write the bitmap for filled and unfilled buckets. */
4846
4847 for (unsigned int i = 0; i < num_buckets; i += 8)
4848 {
4849 uint8_t v = 0;
4850
4851 for (unsigned int j = 0; j < 8; j++)
4852 {
4853 if (buckets[i + j])
4854 v |= 1 << j;
4855 }
4856
4857 if (bfd_bwrite (&v, sizeof (v), stream) != sizeof (v))
4858 goto end;
4859 }
4860
4861 /* Add a 4-byte gap. */
4862
4863 bfd_putl32 (0, int_buf);
4864
4865 if (bfd_bwrite (int_buf, sizeof (uint32_t), stream) != sizeof (uint32_t))
4866 goto end;
4867
4868 /* Write the bucket offsets. */
4869
4870 for (unsigned int i = 0; i < num_buckets; i++)
4871 {
4872 if (buckets[i])
4873 {
4874 /* 0xc is size of internal hash_record structure in
4875 Microsoft's parser. */
4876 bfd_putl32 (buckets[i]->index * 0xc, int_buf);
4877
4878 if (bfd_bwrite (int_buf, sizeof (uint32_t), stream) !=
4879 sizeof (uint32_t))
4880 goto end;
4881 }
4882 }
4883
4884 /* Write the address map: offsets into the symbol record stream of
4885 S_PUB32 records, ordered by address. */
4886
4887 if (num_entries > 0)
4888 {
4889 qsort (sorted, num_entries, sizeof (struct public *),
4890 public_compare_addr);
4891
4892 for (unsigned int i = 0; i < num_entries; i++)
4893 {
4894 bfd_putl32 (sorted[i]->offset, int_buf);
4895
4896 if (bfd_bwrite (int_buf, sizeof (uint32_t), stream) !=
4897 sizeof (uint32_t))
4898 goto end;
4899 }
4900 }
4901
4902 ret = true;
4903
4904 end:
4905 free (buckets);
4906
4907 while (publics_head)
4908 {
4909 struct public *p = publics_head->next;
4910
4911 free (publics_head);
4912 publics_head = p;
4913 }
4914
4915 free (sorted);
4916
4917 return ret;
4918 }
4919
4920 /* The section header stream contains a copy of the section headers
4921 from the PE file, in the same format. */
4922 static bool
4923 create_section_header_stream (bfd *pdb, bfd *abfd, uint16_t *num)
4924 {
4925 bfd *stream;
4926 unsigned int section_count;
4927 file_ptr scn_base;
4928 size_t len;
4929 char *buf;
4930
4931 stream = add_stream (pdb, NULL, num);
4932 if (!stream)
4933 return false;
4934
4935 section_count = abfd->section_count;
4936
4937 /* Empty sections aren't output. */
4938 for (asection *sect = abfd->sections; sect; sect = sect->next)
4939 {
4940 if (sect->size == 0)
4941 section_count--;
4942 }
4943
4944 if (section_count == 0)
4945 return true;
4946
4947 /* Copy section table from output - it's already been written at this
4948 point. */
4949
4950 scn_base = bfd_coff_filhsz (abfd) + bfd_coff_aoutsz (abfd);
4951
4952 bfd_seek (abfd, scn_base, SEEK_SET);
4953
4954 len = section_count * sizeof (struct external_scnhdr);
4955 buf = xmalloc (len);
4956
4957 if (bfd_bread (buf, len, abfd) != len)
4958 {
4959 free (buf);
4960 return false;
4961 }
4962
4963 if (bfd_bwrite (buf, len, stream) != len)
4964 {
4965 free (buf);
4966 return false;
4967 }
4968
4969 free (buf);
4970
4971 return true;
4972 }
4973
4974 /* Populate the "/names" named stream, which contains the string table. */
4975 static bool
4976 populate_names_stream (bfd *stream, struct string_table *strings)
4977 {
4978 char int_buf[sizeof (uint32_t)];
4979 struct string_table_header h;
4980 uint32_t num_strings = 0, num_buckets;
4981 struct string **buckets;
4982
4983 bfd_putl32 (STRING_TABLE_SIGNATURE, &h.signature);
4984 bfd_putl32 (STRING_TABLE_VERSION, &h.version);
4985
4986 if (bfd_bwrite (&h, sizeof (h), stream) != sizeof (h))
4987 return false;
4988
4989 bfd_putl32 (strings->strings_len, int_buf);
4990
4991 if (bfd_bwrite (int_buf, sizeof (uint32_t), stream) != sizeof (uint32_t))
4992 return false;
4993
4994 int_buf[0] = 0;
4995
4996 if (bfd_bwrite (int_buf, 1, stream) != 1)
4997 return false;
4998
4999 for (struct string *s = strings->strings_head; s; s = s->next)
5000 {
5001 if (bfd_bwrite (s->s, s->len, stream) != s->len)
5002 return false;
5003
5004 if (bfd_bwrite (int_buf, 1, stream) != 1)
5005 return false;
5006
5007 num_strings++;
5008 }
5009
5010 num_buckets = num_strings * 2;
5011
5012 buckets = xmalloc (sizeof (struct string *) * num_buckets);
5013 memset (buckets, 0, sizeof (struct string *) * num_buckets);
5014
5015 for (struct string *s = strings->strings_head; s; s = s->next)
5016 {
5017 uint32_t bucket_num = s->hash % num_buckets;
5018
5019 while (buckets[bucket_num])
5020 {
5021 bucket_num++;
5022
5023 if (bucket_num == num_buckets)
5024 bucket_num = 0;
5025 }
5026
5027 buckets[bucket_num] = s;
5028 }
5029
5030 bfd_putl32 (num_buckets, int_buf);
5031
5032 if (bfd_bwrite (int_buf, sizeof (uint32_t), stream) != sizeof (uint32_t))
5033 {
5034 free (buckets);
5035 return false;
5036 }
5037
5038 for (unsigned int i = 0; i < num_buckets; i++)
5039 {
5040 if (buckets[i])
5041 bfd_putl32 (buckets[i]->offset, int_buf);
5042 else
5043 bfd_putl32 (0, int_buf);
5044
5045 if (bfd_bwrite (int_buf, sizeof (uint32_t), stream) !=
5046 sizeof (uint32_t))
5047 {
5048 free (buckets);
5049 return false;
5050 }
5051 }
5052
5053 free (buckets);
5054
5055 bfd_putl32 (num_strings, int_buf);
5056
5057 if (bfd_bwrite (int_buf, sizeof (uint32_t), stream) != sizeof (uint32_t))
5058 return false;
5059
5060 return true;
5061 }
5062
5063 /* Calculate the hash of a type_entry. */
5064 static hashval_t
5065 hash_type_entry (const void *p)
5066 {
5067 const struct type_entry *e = (const struct type_entry *) p;
5068 uint16_t size = bfd_getl16 (e->data) + sizeof (uint16_t);
5069
5070 return iterative_hash (e->data, size, 0);
5071 }
5072
5073 /* Compare a type_entry with a type. */
5074 static int
5075 eq_type_entry (const void *a, const void *b)
5076 {
5077 const struct type_entry *e = (const struct type_entry *) a;
5078 uint16_t size_a = bfd_getl16 (e->data);
5079 uint16_t size_b = bfd_getl16 (b);
5080
5081 if (size_a != size_b)
5082 return 0;
5083
5084 return memcmp (e->data + sizeof (uint16_t),
5085 (const uint8_t *) b + sizeof (uint16_t), size_a) == 0;
5086 }
5087
5088 /* Create a PDB debugging file for the PE image file abfd with the build ID
5089 guid, stored at pdb_name. */
5090 bool
5091 create_pdb_file (bfd *abfd, const char *pdb_name, const unsigned char *guid)
5092 {
5093 bfd *pdb;
5094 bool ret = false;
5095 bfd *info_stream, *dbi_stream, *names_stream, *sym_rec_stream,
5096 *publics_stream, *tpi_stream, *ipi_stream;
5097 uint16_t section_header_stream_num, sym_rec_stream_num, publics_stream_num;
5098 struct string_table strings;
5099 struct types types, ids;
5100
5101 pdb = bfd_openw (pdb_name, "pdb");
5102 if (!pdb)
5103 {
5104 einfo (_("%P: warning: cannot create PDB file: %E\n"));
5105 return false;
5106 }
5107
5108 strings.strings_head = NULL;
5109 strings.strings_tail = NULL;
5110 strings.strings_len = 1;
5111 strings.hashmap = htab_create_alloc (0, hash_string_table_entry,
5112 eq_string_table_entry, free,
5113 xcalloc, free);
5114
5115 bfd_set_format (pdb, bfd_archive);
5116
5117 if (!create_old_directory_stream (pdb))
5118 {
5119 einfo (_("%P: warning: cannot create old directory stream "
5120 "in PDB file: %E\n"));
5121 goto end;
5122 }
5123
5124 info_stream = add_stream (pdb, NULL, NULL);
5125
5126 if (!info_stream)
5127 {
5128 einfo (_("%P: warning: cannot create info stream "
5129 "in PDB file: %E\n"));
5130 goto end;
5131 }
5132
5133 tpi_stream = add_stream (pdb, NULL, NULL);
5134
5135 if (!tpi_stream)
5136 {
5137 einfo (_("%P: warning: cannot create TPI stream "
5138 "in PDB file: %E\n"));
5139 goto end;
5140 }
5141
5142 dbi_stream = add_stream (pdb, NULL, NULL);
5143
5144 if (!dbi_stream)
5145 {
5146 einfo (_("%P: warning: cannot create DBI stream "
5147 "in PDB file: %E\n"));
5148 goto end;
5149 }
5150
5151 ipi_stream = add_stream (pdb, NULL, NULL);
5152
5153 if (!ipi_stream)
5154 {
5155 einfo (_("%P: warning: cannot create IPI stream "
5156 "in PDB file: %E\n"));
5157 goto end;
5158 }
5159
5160 names_stream = add_stream (pdb, "/names", NULL);
5161
5162 if (!names_stream)
5163 {
5164 einfo (_("%P: warning: cannot create /names stream "
5165 "in PDB file: %E\n"));
5166 goto end;
5167 }
5168
5169 sym_rec_stream = add_stream (pdb, NULL, &sym_rec_stream_num);
5170
5171 if (!sym_rec_stream)
5172 {
5173 einfo (_("%P: warning: cannot create symbol record stream "
5174 "in PDB file: %E\n"));
5175 goto end;
5176 }
5177
5178 publics_stream = add_stream (pdb, NULL, &publics_stream_num);
5179
5180 if (!publics_stream)
5181 {
5182 einfo (_("%P: warning: cannot create publics stream "
5183 "in PDB file: %E\n"));
5184 goto end;
5185 }
5186
5187 if (!create_section_header_stream (pdb, abfd, &section_header_stream_num))
5188 {
5189 einfo (_("%P: warning: cannot create section header stream "
5190 "in PDB file: %E\n"));
5191 goto end;
5192 }
5193
5194 types.num_types = 0;
5195 types.hashmap = htab_create_alloc (0, hash_type_entry, eq_type_entry,
5196 free, xcalloc, free);
5197 types.first = types.last = NULL;
5198
5199 ids.num_types = 0;
5200 ids.hashmap = htab_create_alloc (0, hash_type_entry, eq_type_entry,
5201 free, xcalloc, free);
5202 ids.first = ids.last = NULL;
5203
5204 if (!populate_dbi_stream (dbi_stream, abfd, pdb, section_header_stream_num,
5205 sym_rec_stream_num, publics_stream_num,
5206 &strings, &types, &ids, sym_rec_stream, pdb_name))
5207 {
5208 einfo (_("%P: warning: cannot populate DBI stream "
5209 "in PDB file: %E\n"));
5210 htab_delete (types.hashmap);
5211 htab_delete (ids.hashmap);
5212 goto end;
5213 }
5214
5215 if (!populate_type_stream (pdb, tpi_stream, &types))
5216 {
5217 einfo (_("%P: warning: cannot populate TPI stream "
5218 "in PDB file: %E\n"));
5219 htab_delete (types.hashmap);
5220 htab_delete (ids.hashmap);
5221 goto end;
5222 }
5223
5224 htab_delete (types.hashmap);
5225
5226 if (!populate_type_stream (pdb, ipi_stream, &ids))
5227 {
5228 einfo (_("%P: warning: cannot populate IPI stream "
5229 "in PDB file: %E\n"));
5230 htab_delete (ids.hashmap);
5231 goto end;
5232 }
5233
5234 htab_delete (ids.hashmap);
5235
5236 add_string ("", 0, &strings);
5237
5238 if (!populate_names_stream (names_stream, &strings))
5239 {
5240 einfo (_("%P: warning: cannot populate names stream "
5241 "in PDB file: %E\n"));
5242 goto end;
5243 }
5244
5245 if (!populate_publics_stream (publics_stream, abfd, sym_rec_stream))
5246 {
5247 einfo (_("%P: warning: cannot populate publics stream "
5248 "in PDB file: %E\n"));
5249 goto end;
5250 }
5251
5252 if (!populate_info_stream (pdb, info_stream, guid))
5253 {
5254 einfo (_("%P: warning: cannot populate info stream "
5255 "in PDB file: %E\n"));
5256 goto end;
5257 }
5258
5259 ret = true;
5260
5261 end:
5262 bfd_close (pdb);
5263
5264 htab_delete (strings.hashmap);
5265
5266 return ret;
5267 }