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