2.41 Release sources
[binutils-gdb.git] / ld / pdb.c
blob267da50c4a7641f4e435150b84fe5bf3c60e1f3e
1 /* Support for generating PDB CodeView debugging files.
2 Copyright (C) 2022-2023 Free Software Foundation, Inc.
4 This file is part of the GNU Binutils.
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.
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.
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. */
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>
35 struct public
37 struct public *next;
38 uint32_t offset;
39 uint32_t hash;
40 unsigned int index;
41 uint16_t section;
42 uint32_t address;
45 struct string
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[];
55 struct string_table
57 struct string *strings_head;
58 struct string *strings_tail;
59 uint32_t strings_len;
60 htab_t hashmap;
63 struct mod_source_files
65 uint16_t files_count;
66 struct string **files;
69 struct source_files_info
71 uint16_t mod_count;
72 struct mod_source_files *mods;
75 struct type_entry
77 struct type_entry *next;
78 uint32_t index;
79 uint32_t cv_hash;
80 bool has_udt_src_line;
81 uint8_t data[];
84 struct types
86 htab_t hashmap;
87 uint32_t num_types;
88 struct type_entry *first;
89 struct type_entry *last;
92 struct global
94 struct global *next;
95 uint32_t offset;
96 uint32_t hash;
97 uint32_t refcount;
98 unsigned int index;
99 uint8_t data[];
102 struct globals
104 uint32_t num_entries;
105 struct global *first;
106 struct global *last;
107 htab_t hashmap;
110 struct in_sc
112 asection *s;
113 uint16_t sect_num;
114 uint16_t mod_index;
117 static const uint32_t crc_table[] =
119 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f,
120 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988,
121 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2,
122 0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7,
123 0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9,
124 0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172,
125 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, 0x42b2986c,
126 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59,
127 0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423,
128 0xcfba9599, 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924,
129 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, 0x01db7106,
130 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433,
131 0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d,
132 0x91646c97, 0xe6635c01, 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e,
133 0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950,
134 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65,
135 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7,
136 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0,
137 0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa,
138 0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
139 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81,
140 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a,
141 0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683, 0xe3630b12, 0x94643b84,
142 0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1,
143 0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb,
144 0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc,
145 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 0xd6d6a3e8, 0xa1d1937e,
146 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b,
147 0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55,
148 0x316e8eef, 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236,
149 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, 0xb2bd0b28,
150 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d,
151 0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f,
152 0x72076785, 0x05005713, 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38,
153 0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242,
154 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777,
155 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69,
156 0x616bffd3, 0x166ccf45, 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2,
157 0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc,
158 0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
159 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693,
160 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94,
161 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d
164 /* Add a new stream to the PDB archive, and return its BFD. */
165 static bfd *
166 add_stream (bfd *pdb, const char *name, uint16_t *stream_num)
168 bfd *stream;
169 uint16_t num;
171 stream = bfd_create (name ? name : "", pdb);
172 if (!stream)
173 return NULL;
175 if (!bfd_make_writable (stream))
177 bfd_close (stream);
178 return false;
181 if (!pdb->archive_head)
183 bfd_set_archive_head (pdb, stream);
184 num = 0;
186 else
188 bfd *b = pdb->archive_head;
190 num = 1;
192 while (b->archive_next)
194 num++;
195 b = b->archive_next;
198 b->archive_next = stream;
201 if (stream_num)
202 *stream_num = num;
204 return stream;
207 /* Stream 0 ought to be a copy of the MSF directory from the last
208 time the PDB file was written. Because we don't do incremental
209 writes this isn't applicable to us, but we fill it with a dummy
210 value so as not to confuse radare. */
211 static bool
212 create_old_directory_stream (bfd *pdb)
214 bfd *stream;
215 char buf[sizeof (uint32_t)];
217 stream = add_stream (pdb, NULL, NULL);
218 if (!stream)
219 return false;
221 bfd_putl32 (0, buf);
223 return bfd_bwrite (buf, sizeof (uint32_t), stream) == sizeof (uint32_t);
226 /* Calculate the hash of a given string. */
227 static uint32_t
228 calc_hash (const char *data, size_t len)
230 uint32_t hash = 0;
232 while (len >= 4)
234 hash ^= data[0];
235 hash ^= data[1] << 8;
236 hash ^= data[2] << 16;
237 hash ^= data[3] << 24;
239 data += 4;
240 len -= 4;
243 if (len >= 2)
245 hash ^= data[0];
246 hash ^= data[1] << 8;
248 data += 2;
249 len -= 2;
252 if (len != 0)
253 hash ^= *data;
255 hash |= 0x20202020;
256 hash ^= (hash >> 11);
258 return hash ^ (hash >> 16);
261 /* Stream 1 is the PDB info stream - see
262 https://llvm.org/docs/PDB/PdbStream.html. */
263 static bool
264 populate_info_stream (bfd *pdb, bfd *info_stream, const unsigned char *guid)
266 bool ret = false;
267 struct pdb_stream_70 h;
268 uint32_t num_entries, num_buckets;
269 uint32_t names_length, stream_num;
270 char int_buf[sizeof (uint32_t)];
272 struct hash_entry
274 uint32_t offset;
275 uint32_t value;
278 struct hash_entry **buckets = NULL;
280 /* Write header. */
282 bfd_putl32 (PDB_STREAM_VERSION_VC70, &h.version);
283 bfd_putl32 (time (NULL), &h.signature);
284 bfd_putl32 (1, &h.age);
286 bfd_putl32 (bfd_getb32 (guid), h.guid);
287 bfd_putl16 (bfd_getb16 (&guid[4]), &h.guid[4]);
288 bfd_putl16 (bfd_getb16 (&guid[6]), &h.guid[6]);
289 memcpy (&h.guid[8], &guid[8], 8);
291 if (bfd_bwrite (&h, sizeof (h), info_stream) != sizeof (h))
292 return false;
294 /* Write hash list of named streams. This is a "rollover" hash, i.e.
295 if a bucket is filled an entry gets placed in the next free
296 slot. */
298 num_entries = 0;
299 for (bfd *b = pdb->archive_head; b; b = b->archive_next)
301 if (strcmp (b->filename, ""))
302 num_entries++;
305 num_buckets = num_entries * 2;
307 names_length = 0;
308 stream_num = 0;
310 if (num_buckets > 0)
312 buckets = xmalloc (sizeof (struct hash_entry *) * num_buckets);
313 memset (buckets, 0, sizeof (struct hash_entry *) * num_buckets);
315 for (bfd *b = pdb->archive_head; b; b = b->archive_next)
317 if (strcmp (b->filename, ""))
319 size_t len = strlen (b->filename);
320 uint32_t hash = (uint16_t) calc_hash (b->filename, len);
321 uint32_t bucket_num = hash % num_buckets;
323 while (buckets[bucket_num])
325 bucket_num++;
327 if (bucket_num == num_buckets)
328 bucket_num = 0;
331 buckets[bucket_num] = xmalloc (sizeof (struct hash_entry));
333 buckets[bucket_num]->offset = names_length;
334 buckets[bucket_num]->value = stream_num;
336 names_length += len + 1;
339 stream_num++;
343 /* Write the strings list - the hash keys are indexes into this. */
345 bfd_putl32 (names_length, int_buf);
347 if (bfd_bwrite (int_buf, sizeof (uint32_t), info_stream) !=
348 sizeof (uint32_t))
349 goto end;
351 for (bfd *b = pdb->archive_head; b; b = b->archive_next)
353 if (!strcmp (b->filename, ""))
354 continue;
356 size_t len = strlen (b->filename) + 1;
358 if (bfd_bwrite (b->filename, len, info_stream) != len)
359 goto end;
362 /* Write the number of entries and buckets. */
364 bfd_putl32 (num_entries, int_buf);
366 if (bfd_bwrite (int_buf, sizeof (uint32_t), info_stream) !=
367 sizeof (uint32_t))
368 goto end;
370 bfd_putl32 (num_buckets, int_buf);
372 if (bfd_bwrite (int_buf, sizeof (uint32_t), info_stream) !=
373 sizeof (uint32_t))
374 goto end;
376 /* Write the present bitmap. */
378 bfd_putl32 ((num_buckets + 31) / 32, int_buf);
380 if (bfd_bwrite (int_buf, sizeof (uint32_t), info_stream) !=
381 sizeof (uint32_t))
382 goto end;
384 for (unsigned int i = 0; i < num_buckets; i += 32)
386 uint32_t v = 0;
388 for (unsigned int j = 0; j < 32; j++)
390 if (i + j >= num_buckets)
391 break;
393 if (buckets[i + j])
394 v |= 1 << j;
397 bfd_putl32 (v, int_buf);
399 if (bfd_bwrite (int_buf, sizeof (uint32_t), info_stream) !=
400 sizeof (uint32_t))
401 goto end;
404 /* Write the (empty) deleted bitmap. */
406 bfd_putl32 (0, int_buf);
408 if (bfd_bwrite (int_buf, sizeof (uint32_t), info_stream) !=
409 sizeof (uint32_t))
410 goto end;
412 /* Write the buckets. */
414 for (unsigned int i = 0; i < num_buckets; i++)
416 if (buckets[i])
418 bfd_putl32 (buckets[i]->offset, int_buf);
420 if (bfd_bwrite (int_buf, sizeof (uint32_t), info_stream) !=
421 sizeof (uint32_t))
422 goto end;
424 bfd_putl32 (buckets[i]->value, int_buf);
426 if (bfd_bwrite (int_buf, sizeof (uint32_t), info_stream) !=
427 sizeof (uint32_t))
428 goto end;
432 bfd_putl32 (0, int_buf);
434 if (bfd_bwrite (int_buf, sizeof (uint32_t), info_stream) !=
435 sizeof (uint32_t))
436 goto end;
438 bfd_putl32 (PDB_STREAM_VERSION_VC140, int_buf);
440 if (bfd_bwrite (int_buf, sizeof (uint32_t), info_stream) !=
441 sizeof (uint32_t))
442 goto end;
444 ret = true;
446 end:
447 for (unsigned int i = 0; i < num_buckets; i++)
449 if (buckets[i])
450 free (buckets[i]);
453 free (buckets);
455 return ret;
458 /* Calculate the CRC32 used for type hashes. */
459 static uint32_t
460 crc32 (const uint8_t *data, size_t len)
462 uint32_t crc = 0;
464 while (len > 0)
466 crc = (crc >> 8) ^ crc_table[(crc & 0xff) ^ *data];
468 data++;
469 len--;
472 return crc;
475 /* Stream 2 is the type information (TPI) stream, and stream 4 is
476 the ID information (IPI) stream. They differ only in which records
477 go in which stream. */
478 static bool
479 populate_type_stream (bfd *pdb, bfd *stream, struct types *types)
481 struct pdb_tpi_stream_header h;
482 struct type_entry *e;
483 uint32_t len = 0, index_offset_len, off;
484 struct bfd *hash_stream = NULL;
485 uint16_t hash_stream_index;
487 static const uint32_t index_skip = 0x2000;
489 e = types->first;
491 index_offset_len = 0;
493 while (e)
495 uint32_t old_len = len;
497 len += sizeof (uint16_t) + bfd_getl16 (e->data);
499 if (old_len == 0 || old_len / index_skip != len / index_skip)
500 index_offset_len += sizeof (uint32_t) * 2;
502 e = e->next;
505 /* Each type stream also has a stream which holds the hash value for each
506 type, along with a skip list to speed up searching. */
508 hash_stream = add_stream (pdb, "", &hash_stream_index);
510 if (!hash_stream)
511 return false;
513 bfd_putl32 (TPI_STREAM_VERSION_80, &h.version);
514 bfd_putl32 (sizeof (h), &h.header_size);
515 bfd_putl32 (TPI_FIRST_INDEX, &h.type_index_begin);
516 bfd_putl32 (TPI_FIRST_INDEX + types->num_types, &h.type_index_end);
517 bfd_putl32 (len, &h.type_record_bytes);
518 bfd_putl16 (hash_stream_index, &h.hash_stream_index);
519 bfd_putl16 (0xffff, &h.hash_aux_stream_index);
520 bfd_putl32 (sizeof (uint32_t), &h.hash_key_size);
521 bfd_putl32 (NUM_TPI_HASH_BUCKETS, &h.num_hash_buckets);
522 bfd_putl32 (0, &h.hash_value_buffer_offset);
523 bfd_putl32 (types->num_types * sizeof (uint32_t),
524 &h.hash_value_buffer_length);
525 bfd_putl32 (types->num_types * sizeof (uint32_t),
526 &h.index_offset_buffer_offset);
527 bfd_putl32 (index_offset_len, &h.index_offset_buffer_length);
528 bfd_putl32 ((types->num_types * sizeof (uint32_t)) + index_offset_len,
529 &h.hash_adj_buffer_offset);
530 bfd_putl32 (0, &h.hash_adj_buffer_length);
532 if (bfd_bwrite (&h, sizeof (h), stream) != sizeof (h))
533 return false;
535 /* Write the type definitions into the main stream, and the hashes
536 into the hash stream. The hashes have already been calculated
537 in handle_type. */
539 e = types->first;
541 while (e)
543 uint8_t buf[sizeof (uint32_t)];
544 uint16_t size;
546 size = bfd_getl16 (e->data);
548 if (bfd_bwrite (e->data, size + sizeof (uint16_t), stream)
549 != size + sizeof (uint16_t))
550 return false;
552 bfd_putl32 (e->cv_hash % NUM_TPI_HASH_BUCKETS, buf);
554 if (bfd_bwrite (buf, sizeof (uint32_t), hash_stream)
555 != sizeof (uint32_t))
556 return false;
558 e = e->next;
561 /* Write the index offsets, i.e. the skip list, into the hash stream. We
562 copy MSVC here by writing a new entry for every 8192 bytes. */
564 e = types->first;
565 off = 0;
567 while (e)
569 uint32_t old_off = off;
570 uint16_t size = bfd_getl16 (e->data);
572 off += size + sizeof (uint16_t);
574 if (old_off == 0 || old_off / index_skip != len / index_skip)
576 uint8_t buf[sizeof (uint32_t)];
578 bfd_putl32 (TPI_FIRST_INDEX + e->index, buf);
580 if (bfd_bwrite (buf, sizeof (uint32_t), hash_stream)
581 != sizeof (uint32_t))
582 return false;
584 bfd_putl32 (old_off, buf);
586 if (bfd_bwrite (buf, sizeof (uint32_t), hash_stream)
587 != sizeof (uint32_t))
588 return false;
591 e = e->next;
594 return true;
597 /* Return the PE architecture number for the image. */
598 static uint16_t
599 get_arch_number (bfd *abfd)
601 switch (abfd->arch_info->arch)
603 case bfd_arch_i386:
604 if (abfd->arch_info->mach & bfd_mach_x86_64)
605 return IMAGE_FILE_MACHINE_AMD64;
606 else
607 return IMAGE_FILE_MACHINE_I386;
609 case bfd_arch_aarch64:
610 return IMAGE_FILE_MACHINE_ARM64;
612 default:
613 return 0;
617 /* Validate the DEBUG_S_FILECHKSMS entry within a module's .debug$S
618 section, and copy it to the module's symbol stream. */
619 static bool
620 copy_filechksms (uint8_t *data, uint32_t size, char *string_table,
621 struct string_table *strings, uint8_t *out,
622 struct mod_source_files *mod_source)
624 uint8_t *orig_data = data;
625 uint32_t orig_size = size;
626 uint16_t num_files = 0;
627 struct string **strptr;
629 bfd_putl32 (DEBUG_S_FILECHKSMS, out);
630 out += sizeof (uint32_t);
632 bfd_putl32 (size, out);
633 out += sizeof (uint32_t);
635 /* Calculate the number of files, and check for any overflows. */
637 while (size > 0)
639 struct file_checksum *fc = (struct file_checksum *) data;
640 uint8_t padding;
641 size_t len;
643 if (size < sizeof (struct file_checksum))
645 bfd_set_error (bfd_error_bad_value);
646 return false;
649 len = sizeof (struct file_checksum) + fc->checksum_length;
651 if (size < len)
653 bfd_set_error (bfd_error_bad_value);
654 return false;
657 data += len;
658 size -= len;
660 if (len % sizeof (uint32_t))
661 padding = sizeof (uint32_t) - (len % sizeof (uint32_t));
662 else
663 padding = 0;
665 if (size < padding)
667 bfd_set_error (bfd_error_bad_value);
668 return false;
671 num_files++;
673 data += padding;
674 size -= padding;
677 /* Add the files to mod_source, so that they'll appear in the source
678 info substream. */
680 strptr = NULL;
681 if (num_files > 0)
683 uint16_t new_count = num_files + mod_source->files_count;
685 mod_source->files = xrealloc (mod_source->files,
686 sizeof (struct string *) * new_count);
688 strptr = mod_source->files + mod_source->files_count;
690 mod_source->files_count += num_files;
693 /* Actually copy the data. */
695 data = orig_data;
696 size = orig_size;
698 while (size > 0)
700 struct file_checksum *fc = (struct file_checksum *) data;
701 uint32_t string_off;
702 uint8_t padding;
703 size_t len;
704 struct string *str = NULL;
706 string_off = bfd_getl32 (&fc->file_id);
707 len = sizeof (struct file_checksum) + fc->checksum_length;
709 if (len % sizeof (uint32_t))
710 padding = sizeof (uint32_t) - (len % sizeof (uint32_t));
711 else
712 padding = 0;
714 /* Remap the "file ID", i.e. the offset in the module's string table,
715 so it points to the right place in the main string table. */
717 if (string_table)
719 char *fn = string_table + string_off;
720 size_t fn_len = strlen (fn);
721 uint32_t hash = calc_hash (fn, fn_len);
722 void **slot;
724 slot = htab_find_slot_with_hash (strings->hashmap, fn, hash,
725 NO_INSERT);
727 if (slot)
728 str = (struct string *) *slot;
731 *strptr = str;
732 strptr++;
734 bfd_putl32 (str ? str->offset : 0, &fc->file_id);
736 memcpy (out, data, len + padding);
738 data += len + padding;
739 size -= len + padding;
740 out += len + padding;
743 return true;
746 /* Add a string to the strings table, if it's not already there. Returns its
747 offset within the string table. */
748 static uint32_t
749 add_string (char *str, size_t len, struct string_table *strings)
751 uint32_t hash = calc_hash (str, len);
752 struct string *s;
753 void **slot;
755 slot = htab_find_slot_with_hash (strings->hashmap, str, hash, INSERT);
757 if (!*slot)
759 *slot = xmalloc (offsetof (struct string, s) + len);
761 s = (struct string *) *slot;
763 s->next = NULL;
764 s->hash = hash;
765 s->offset = strings->strings_len;
766 s->source_file_offset = 0xffffffff;
767 s->len = len;
768 memcpy (s->s, str, len);
770 if (strings->strings_tail)
771 strings->strings_tail->next = s;
772 else
773 strings->strings_head = s;
775 strings->strings_tail = s;
777 strings->strings_len += len + 1;
779 else
781 s = (struct string *) *slot;
784 return s->offset;
787 /* Return the hash of an entry in the string table. */
788 static hashval_t
789 hash_string_table_entry (const void *p)
791 const struct string *s = (const struct string *) p;
793 return s->hash;
796 /* Compare an entry in the string table with a string. */
797 static int
798 eq_string_table_entry (const void *a, const void *b)
800 const struct string *s1 = (const struct string *) a;
801 const char *s2 = (const char *) b;
802 size_t s2_len = strlen (s2);
804 if (s2_len != s1->len)
805 return 0;
807 return memcmp (s1->s, s2, s2_len) == 0;
810 /* Parse the string table within the .debug$S section. */
811 static void
812 parse_string_table (bfd_byte *data, size_t size,
813 struct string_table *strings)
815 while (true)
817 size_t len = strnlen ((char *) data, size);
819 add_string ((char *) data, len, strings);
821 data += len + 1;
823 if (size <= len + 1)
824 break;
826 size -= len + 1;
830 /* Remap a type reference within a CodeView symbol. */
831 static bool
832 remap_symbol_type (void *data, struct type_entry **map, uint32_t num_types)
834 uint32_t type = bfd_getl32 (data);
836 /* Ignore builtin types (those with IDs below 0x1000). */
837 if (type < TPI_FIRST_INDEX)
838 return true;
840 if (type >= TPI_FIRST_INDEX + num_types)
842 einfo (_("%P: CodeView symbol references out of range type %v\n"),
843 type);
844 return false;
847 type = TPI_FIRST_INDEX + map[type - TPI_FIRST_INDEX]->index;
848 bfd_putl32 (type, data);
850 return true;
853 /* Add an entry into the globals stream. If it already exists, increase
854 the refcount. */
855 static bool
856 add_globals_ref (struct globals *glob, bfd *sym_rec_stream, const char *name,
857 size_t name_len, uint8_t *data, size_t len)
859 void **slot;
860 uint32_t hash;
861 struct global *g;
863 slot = htab_find_slot_with_hash (glob->hashmap, data,
864 iterative_hash (data, len, 0), INSERT);
866 if (*slot)
868 g = *slot;
869 g->refcount++;
870 return true;
873 *slot = xmalloc (offsetof (struct global, data) + len);
875 hash = crc32 ((const uint8_t *) name, name_len);
876 hash %= NUM_GLOBALS_HASH_BUCKETS;
878 g = *slot;
879 g->next = NULL;
880 g->offset = bfd_tell (sym_rec_stream);
881 g->hash = hash;
882 g->refcount = 1;
883 memcpy (g->data, data, len);
885 glob->num_entries++;
887 if (glob->last)
888 glob->last->next = g;
889 else
890 glob->first = g;
892 glob->last = g;
894 return bfd_bwrite (data, len, sym_rec_stream) == len;
897 /* Find the end of the current scope within symbols data. */
898 static uint8_t *
899 find_end_of_scope (uint8_t *data, uint32_t size)
901 unsigned int scope_level = 1;
902 uint16_t len;
904 len = bfd_getl16 (data) + sizeof (uint16_t);
906 data += len;
907 size -= len;
909 while (true)
911 uint16_t type;
913 if (size < sizeof (uint32_t))
914 return NULL;
916 len = bfd_getl16 (data) + sizeof (uint16_t);
917 type = bfd_getl16 (data + sizeof (uint16_t));
919 if (size < len)
920 return NULL;
922 switch (type)
924 case S_GPROC32:
925 case S_LPROC32:
926 case S_BLOCK32:
927 case S_INLINESITE:
928 case S_THUNK32:
929 scope_level++;
930 break;
932 case S_END:
933 case S_PROC_ID_END:
934 case S_INLINESITE_END:
935 scope_level--;
937 if (scope_level == 0)
938 return data;
940 break;
943 data += len;
944 size -= len;
948 /* Return the size of an extended value parameter, as used in
949 LF_ENUMERATE etc. */
950 static unsigned int
951 extended_value_len (uint16_t type)
953 switch (type)
955 case LF_CHAR:
956 return 1;
958 case LF_SHORT:
959 case LF_USHORT:
960 return 2;
962 case LF_LONG:
963 case LF_ULONG:
964 return 4;
966 case LF_QUADWORD:
967 case LF_UQUADWORD:
968 return 8;
971 return 0;
974 /* Parse the symbols in a .debug$S section, and copy them to the module's
975 symbol stream. */
976 static bool
977 parse_symbols (uint8_t *data, uint32_t size, uint8_t **buf,
978 struct type_entry **map, uint32_t num_types,
979 bfd *sym_rec_stream, struct globals *glob, uint16_t mod_num)
981 uint8_t *orig_buf = *buf;
982 unsigned int scope_level = 0;
983 uint8_t *scope = NULL;
985 while (size >= sizeof (uint16_t))
987 uint16_t len, type;
989 len = bfd_getl16 (data) + sizeof (uint16_t);
991 if (len > size)
993 bfd_set_error (bfd_error_bad_value);
994 return false;
997 type = bfd_getl16 (data + sizeof (uint16_t));
999 switch (type)
1001 case S_LDATA32:
1002 case S_GDATA32:
1003 case S_LTHREAD32:
1004 case S_GTHREAD32:
1006 struct datasym *d = (struct datasym *) data;
1007 size_t name_len;
1009 if (len < offsetof (struct datasym, name))
1011 einfo (_("%P: warning: truncated CodeView record"
1012 " S_LDATA32/S_GDATA32/S_LTHREAD32/S_GTHREAD32\n"));
1013 bfd_set_error (bfd_error_bad_value);
1014 return false;
1017 if (scope_level == 0)
1019 uint16_t section = bfd_getl16 (&d->section);
1021 if (section == 0) /* GC'd, ignore */
1022 break;
1025 name_len =
1026 strnlen (d->name, len - offsetof (struct datasym, name));
1028 if (name_len == len - offsetof (struct datasym, name))
1030 einfo (_("%P: warning: name for S_LDATA32/S_GDATA32/"
1031 "S_LTHREAD32/S_GTHREAD32 has no terminating"
1032 " zero\n"));
1033 bfd_set_error (bfd_error_bad_value);
1034 return false;
1037 if (!remap_symbol_type (&d->type, map, num_types))
1039 bfd_set_error (bfd_error_bad_value);
1040 return false;
1043 /* If S_LDATA32 or S_LTHREAD32, copy into module symbols. */
1045 if (type == S_LDATA32 || type == S_LTHREAD32)
1047 memcpy (*buf, d, len);
1048 *buf += len;
1051 /* S_LDATA32 and S_LTHREAD32 only go in globals if
1052 not in function scope. */
1053 if (type == S_GDATA32 || type == S_GTHREAD32 || scope_level == 0)
1055 if (!add_globals_ref (glob, sym_rec_stream, d->name,
1056 name_len, data, len))
1057 return false;
1060 break;
1063 case S_GPROC32:
1064 case S_LPROC32:
1065 case S_GPROC32_ID:
1066 case S_LPROC32_ID:
1068 struct procsym *proc = (struct procsym *) data;
1069 size_t name_len;
1070 uint16_t section;
1071 uint32_t end;
1072 uint8_t *endptr;
1073 size_t ref_size, padding;
1074 struct refsym *ref;
1076 if (len < offsetof (struct procsym, name))
1078 einfo (_("%P: warning: truncated CodeView record"
1079 " S_GPROC32/S_LPROC32\n"));
1080 bfd_set_error (bfd_error_bad_value);
1081 return false;
1084 section = bfd_getl16 (&proc->section);
1086 endptr = find_end_of_scope (data, size);
1088 if (!endptr)
1090 einfo (_("%P: warning: could not find end of"
1091 " S_GPROC32/S_LPROC32 record\n"));
1092 bfd_set_error (bfd_error_bad_value);
1093 return false;
1096 if (section == 0) /* skip if GC'd */
1098 /* Skip to after S_END. */
1100 size -= endptr - data;
1101 data = endptr;
1103 len = bfd_getl16 (data) + sizeof (uint16_t);
1105 data += len;
1106 size -= len;
1108 continue;
1111 name_len =
1112 strnlen (proc->name, len - offsetof (struct procsym, name));
1114 if (name_len == len - offsetof (struct procsym, name))
1116 einfo (_("%P: warning: name for S_GPROC32/S_LPROC32 has no"
1117 " terminating zero\n"));
1118 bfd_set_error (bfd_error_bad_value);
1119 return false;
1122 if (type == S_GPROC32_ID || type == S_LPROC32_ID)
1124 /* Transform into S_GPROC32 / S_LPROC32. */
1126 uint32_t t_idx = bfd_getl32 (&proc->type);
1127 struct type_entry *t;
1128 uint16_t t_type;
1130 if (t_idx < TPI_FIRST_INDEX
1131 || t_idx >= TPI_FIRST_INDEX + num_types)
1133 einfo (_("%P: CodeView symbol references out of range"
1134 " type %v\n"), type);
1135 bfd_set_error (bfd_error_bad_value);
1136 return false;
1139 t = map[t_idx - TPI_FIRST_INDEX];
1141 t_type = bfd_getl16 (t->data + sizeof (uint16_t));
1143 switch (t_type)
1145 case LF_FUNC_ID:
1147 struct lf_func_id *t_data =
1148 (struct lf_func_id *) t->data;
1150 /* Replace proc->type with function type. */
1152 memcpy (&proc->type, &t_data->function_type,
1153 sizeof (uint32_t));
1155 break;
1158 case LF_MFUNC_ID:
1160 struct lf_mfunc_id *t_data =
1161 (struct lf_mfunc_id *) t->data;
1163 /* Replace proc->type with function type. */
1165 memcpy (&proc->type, &t_data->function_type,
1166 sizeof (uint32_t));
1168 break;
1171 default:
1172 einfo (_("%P: CodeView S_GPROC32_ID/S_LPROC32_ID symbol"
1173 " referenced unknown type as ID\n"));
1174 bfd_set_error (bfd_error_bad_value);
1175 return false;
1178 /* Change record type. */
1180 if (type == S_GPROC32_ID)
1181 bfd_putl32 (S_GPROC32, &proc->kind);
1182 else
1183 bfd_putl32 (S_LPROC32, &proc->kind);
1185 else
1187 if (!remap_symbol_type (&proc->type, map, num_types))
1189 bfd_set_error (bfd_error_bad_value);
1190 return false;
1194 end = *buf - orig_buf + sizeof (uint32_t) + endptr - data;
1195 bfd_putl32 (end, &proc->end);
1197 /* Add S_PROCREF / S_LPROCREF to globals stream. */
1199 ref_size = offsetof (struct refsym, name) + name_len + 1;
1201 if (ref_size % sizeof (uint32_t))
1202 padding = sizeof (uint32_t) - (ref_size % sizeof (uint32_t));
1203 else
1204 padding = 0;
1206 ref = xmalloc (ref_size + padding);
1208 bfd_putl16 (ref_size + padding - sizeof (uint16_t), &ref->size);
1209 bfd_putl16 (type == S_GPROC32 || type == S_GPROC32_ID ?
1210 S_PROCREF : S_LPROCREF, &ref->kind);
1211 bfd_putl32 (0, &ref->sum_name);
1212 bfd_putl32 (*buf - orig_buf + sizeof (uint32_t),
1213 &ref->symbol_offset);
1214 bfd_putl16 (mod_num + 1, &ref->mod);
1216 memcpy (ref->name, proc->name, name_len + 1);
1218 memset (ref->name + name_len + 1, 0, padding);
1220 if (!add_globals_ref (glob, sym_rec_stream, proc->name, name_len,
1221 (uint8_t *) ref, ref_size + padding))
1223 free (ref);
1224 return false;
1227 free (ref);
1229 scope = *buf;
1231 memcpy (*buf, proc, len);
1232 *buf += len;
1234 scope_level++;
1236 break;
1239 case S_UDT:
1241 struct udtsym *udt = (struct udtsym *) data;
1242 size_t name_len;
1244 if (len < offsetof (struct udtsym, name))
1246 einfo (_("%P: warning: truncated CodeView record"
1247 " S_UDT\n"));
1248 bfd_set_error (bfd_error_bad_value);
1249 return false;
1252 name_len =
1253 strnlen (udt->name, len - offsetof (struct udtsym, name));
1255 if (name_len == len - offsetof (struct udtsym, name))
1257 einfo (_("%P: warning: name for S_UDT has no"
1258 " terminating zero\n"));
1259 bfd_set_error (bfd_error_bad_value);
1260 return false;
1263 if (!remap_symbol_type (&udt->type, map, num_types))
1265 bfd_set_error (bfd_error_bad_value);
1266 return false;
1269 /* S_UDT goes in the symbols stream if within a procedure,
1270 otherwise it goes in the globals stream. */
1271 if (scope_level == 0)
1273 if (!add_globals_ref (glob, sym_rec_stream, udt->name,
1274 name_len, data, len))
1275 return false;
1277 else
1279 memcpy (*buf, udt, len);
1280 *buf += len;
1283 break;
1286 case S_CONSTANT:
1288 struct constsym *c = (struct constsym *) data;
1289 size_t name_len, rec_size;
1290 uint16_t val;
1292 if (len < offsetof (struct constsym, name))
1294 einfo (_("%P: warning: truncated CodeView record"
1295 " S_CONSTANT\n"));
1296 bfd_set_error (bfd_error_bad_value);
1297 return false;
1300 rec_size = offsetof (struct constsym, name);
1302 val = bfd_getl16 (&c->value);
1304 /* If val >= 0x8000, actual value follows. */
1305 if (val >= 0x8000)
1307 unsigned int param_len = extended_value_len (val);
1309 if (param_len == 0)
1311 einfo (_("%P: warning: unhandled type %v within"
1312 " S_CONSTANT\n"), val);
1313 bfd_set_error (bfd_error_bad_value);
1314 return false;
1317 rec_size += param_len;
1320 name_len =
1321 strnlen ((const char *) data + rec_size, len - rec_size);
1323 if (name_len == len - rec_size)
1325 einfo (_("%P: warning: name for S_CONSTANT has no"
1326 " terminating zero\n"));
1327 bfd_set_error (bfd_error_bad_value);
1328 return false;
1331 if (!remap_symbol_type (&c->type, map, num_types))
1333 bfd_set_error (bfd_error_bad_value);
1334 return false;
1337 if (!add_globals_ref (glob, sym_rec_stream,
1338 (const char *) data + rec_size, name_len,
1339 data, len))
1340 return false;
1342 break;
1345 case S_END:
1346 case S_INLINESITE_END:
1347 case S_PROC_ID_END:
1348 memcpy (*buf, data, len);
1350 if (type == S_PROC_ID_END) /* transform to S_END */
1351 bfd_putl16 (S_END, *buf + sizeof (uint16_t));
1353 /* Reset scope variable back to the address of the previous
1354 scope start. */
1355 if (scope)
1357 uint32_t parent;
1358 uint16_t scope_start_type =
1359 bfd_getl16 (scope + sizeof (uint16_t));
1361 switch (scope_start_type)
1363 case S_GPROC32:
1364 case S_LPROC32:
1365 parent = bfd_getl32 (scope + offsetof (struct procsym,
1366 parent));
1367 break;
1369 case S_BLOCK32:
1370 parent = bfd_getl32 (scope + offsetof (struct blocksym,
1371 parent));
1372 break;
1374 case S_INLINESITE:
1375 parent = bfd_getl32 (scope + offsetof (struct inline_site,
1376 parent));
1377 break;
1379 case S_THUNK32:
1380 parent = bfd_getl32 (scope + offsetof (struct thunk,
1381 parent));
1382 break;
1384 default:
1385 einfo (_("%P: warning: unexpected CodeView scope start"
1386 " record %v\n"), scope_start_type);
1387 bfd_set_error (bfd_error_bad_value);
1388 return false;
1391 if (parent == 0)
1392 scope = NULL;
1393 else
1394 scope = orig_buf + parent - sizeof (uint32_t);
1397 *buf += len;
1398 scope_level--;
1399 break;
1401 case S_BUILDINFO:
1403 struct buildinfosym *bi = (struct buildinfosym *) data;
1405 if (len < sizeof (struct buildinfosym))
1407 einfo (_("%P: warning: truncated CodeView record"
1408 " S_BUILDINFO\n"));
1409 bfd_set_error (bfd_error_bad_value);
1410 return false;
1413 if (!remap_symbol_type (&bi->type, map, num_types))
1415 bfd_set_error (bfd_error_bad_value);
1416 return false;
1419 memcpy (*buf, data, len);
1420 *buf += len;
1422 break;
1425 case S_BLOCK32:
1427 struct blocksym *bl = (struct blocksym *) data;
1428 uint8_t *endptr;
1429 uint32_t end;
1431 if (len < offsetof (struct blocksym, name))
1433 einfo (_("%P: warning: truncated CodeView record"
1434 " S_BLOCK32\n"));
1435 bfd_set_error (bfd_error_bad_value);
1436 return false;
1439 bfd_putl32 (scope - orig_buf + sizeof (uint32_t), &bl->parent);
1441 endptr = find_end_of_scope (data, size);
1443 if (!endptr)
1445 einfo (_("%P: warning: could not find end of"
1446 " S_BLOCK32 record\n"));
1447 bfd_set_error (bfd_error_bad_value);
1448 return false;
1451 end = *buf - orig_buf + sizeof (uint32_t) + endptr - data;
1452 bfd_putl32 (end, &bl->end);
1454 scope = *buf;
1456 memcpy (*buf, data, len);
1457 *buf += len;
1459 scope_level++;
1461 break;
1464 case S_BPREL32:
1466 struct bprelsym *bp = (struct bprelsym *) data;
1468 if (len < offsetof (struct bprelsym, name))
1470 einfo (_("%P: warning: truncated CodeView record"
1471 " S_BPREL32\n"));
1472 bfd_set_error (bfd_error_bad_value);
1473 return false;
1476 if (!remap_symbol_type (&bp->type, map, num_types))
1478 bfd_set_error (bfd_error_bad_value);
1479 return false;
1482 memcpy (*buf, data, len);
1483 *buf += len;
1485 break;
1488 case S_REGISTER:
1490 struct regsym *reg = (struct regsym *) data;
1492 if (len < offsetof (struct regsym, name))
1494 einfo (_("%P: warning: truncated CodeView record"
1495 " S_REGISTER\n"));
1496 bfd_set_error (bfd_error_bad_value);
1497 return false;
1500 if (!remap_symbol_type (&reg->type, map, num_types))
1502 bfd_set_error (bfd_error_bad_value);
1503 return false;
1506 memcpy (*buf, data, len);
1507 *buf += len;
1509 break;
1512 case S_REGREL32:
1514 struct regrel *rr = (struct regrel *) data;
1516 if (len < offsetof (struct regrel, name))
1518 einfo (_("%P: warning: truncated CodeView record"
1519 " S_REGREL32\n"));
1520 bfd_set_error (bfd_error_bad_value);
1521 return false;
1524 if (!remap_symbol_type (&rr->type, map, num_types))
1526 bfd_set_error (bfd_error_bad_value);
1527 return false;
1530 memcpy (*buf, data, len);
1531 *buf += len;
1533 break;
1536 case S_LOCAL:
1538 struct localsym *l = (struct localsym *) data;
1540 if (len < offsetof (struct localsym, name))
1542 einfo (_("%P: warning: truncated CodeView record"
1543 " S_LOCAL\n"));
1544 bfd_set_error (bfd_error_bad_value);
1545 return false;
1548 if (!remap_symbol_type (&l->type, map, num_types))
1550 bfd_set_error (bfd_error_bad_value);
1551 return false;
1554 memcpy (*buf, data, len);
1555 *buf += len;
1557 break;
1560 case S_INLINESITE:
1562 struct inline_site *is = (struct inline_site *) data;
1563 uint8_t *endptr;
1564 uint32_t end;
1566 if (len < offsetof (struct inline_site, binary_annotations))
1568 einfo (_("%P: warning: truncated CodeView record"
1569 " S_INLINESITE\n"));
1570 bfd_set_error (bfd_error_bad_value);
1571 return false;
1574 bfd_putl32 (scope - orig_buf + sizeof (uint32_t), &is->parent);
1576 endptr = find_end_of_scope (data, size);
1578 if (!endptr)
1580 einfo (_("%P: warning: could not find end of"
1581 " S_INLINESITE record\n"));
1582 bfd_set_error (bfd_error_bad_value);
1583 return false;
1586 end = *buf - orig_buf + sizeof (uint32_t) + endptr - data;
1587 bfd_putl32 (end, &is->end);
1589 if (!remap_symbol_type (&is->inlinee, map, num_types))
1591 bfd_set_error (bfd_error_bad_value);
1592 return false;
1595 scope = *buf;
1597 memcpy (*buf, data, len);
1598 *buf += len;
1600 scope_level++;
1602 break;
1605 case S_THUNK32:
1607 struct thunk *th = (struct thunk *) data;
1608 uint8_t *endptr;
1609 uint32_t end;
1611 if (len < offsetof (struct thunk, name))
1613 einfo (_("%P: warning: truncated CodeView record"
1614 " S_THUNK32\n"));
1615 bfd_set_error (bfd_error_bad_value);
1616 return false;
1619 bfd_putl32 (scope - orig_buf + sizeof (uint32_t), &th->parent);
1621 endptr = find_end_of_scope (data, size);
1623 if (!endptr)
1625 einfo (_("%P: warning: could not find end of"
1626 " S_THUNK32 record\n"));
1627 bfd_set_error (bfd_error_bad_value);
1628 return false;
1631 end = *buf - orig_buf + sizeof (uint32_t) + endptr - data;
1632 bfd_putl32 (end, &th->end);
1634 scope = *buf;
1636 memcpy (*buf, data, len);
1637 *buf += len;
1639 scope_level++;
1641 break;
1644 case S_HEAPALLOCSITE:
1646 struct heap_alloc_site *has = (struct heap_alloc_site *) data;
1648 if (len < sizeof (struct heap_alloc_site))
1650 einfo (_("%P: warning: truncated CodeView record"
1651 " S_HEAPALLOCSITE\n"));
1652 bfd_set_error (bfd_error_bad_value);
1653 return false;
1656 if (!remap_symbol_type (&has->type, map, num_types))
1658 bfd_set_error (bfd_error_bad_value);
1659 return false;
1662 memcpy (*buf, data, len);
1663 *buf += len;
1665 break;
1668 case S_OBJNAME: /* just copy */
1669 case S_COMPILE3:
1670 case S_UNAMESPACE:
1671 case S_FRAMEPROC:
1672 case S_FRAMECOOKIE:
1673 case S_LABEL32:
1674 case S_DEFRANGE_REGISTER_REL:
1675 case S_DEFRANGE_FRAMEPOINTER_REL:
1676 case S_DEFRANGE_SUBFIELD_REGISTER:
1677 case S_DEFRANGE_FRAMEPOINTER_REL_FULL_SCOPE:
1678 case S_DEFRANGE_REGISTER:
1679 memcpy (*buf, data, len);
1680 *buf += len;
1681 break;
1683 default:
1684 einfo (_("%P: warning: unrecognized CodeView record %v\n"), type);
1685 bfd_set_error (bfd_error_bad_value);
1686 return false;
1689 data += len;
1690 size -= len;
1693 return true;
1696 /* For a given symbol subsection, work out how much space to allocate in the
1697 result module stream. This is different because we don't copy certain
1698 symbols, such as S_CONSTANT, and we skip over any procedures or data that
1699 have been GC'd out. */
1700 static bool
1701 calculate_symbols_size (uint8_t *data, uint32_t size, uint32_t *sym_size)
1703 unsigned int scope_level = 0;
1705 while (size >= sizeof (uint32_t))
1707 uint16_t len = bfd_getl16 (data) + sizeof (uint16_t);
1708 uint16_t type = bfd_getl16 (data + sizeof (uint16_t));
1710 switch (type)
1712 case S_LDATA32:
1713 case S_LTHREAD32:
1715 struct datasym *d = (struct datasym *) data;
1716 uint16_t section;
1718 if (len < offsetof (struct datasym, name))
1720 einfo (_("%P: warning: truncated CodeView record"
1721 " S_LDATA32/S_LTHREAD32\n"));
1722 return false;
1725 section = bfd_getl16 (&d->section);
1727 /* copy if not GC'd or within function */
1728 if (scope_level != 0 || section != 0)
1729 *sym_size += len;
1732 case S_GDATA32:
1733 case S_GTHREAD32:
1734 case S_CONSTANT:
1735 /* Not copied into symbols stream. */
1736 break;
1738 case S_GPROC32:
1739 case S_LPROC32:
1740 case S_GPROC32_ID:
1741 case S_LPROC32_ID:
1743 struct procsym *proc = (struct procsym *) data;
1744 uint16_t section;
1746 if (len < offsetof (struct procsym, name))
1748 einfo (_("%P: warning: truncated CodeView record"
1749 " S_GPROC32/S_LPROC32\n"));
1750 return false;
1753 section = bfd_getl16 (&proc->section);
1755 if (section != 0)
1757 *sym_size += len;
1759 else
1761 uint8_t *endptr = find_end_of_scope (data, size);
1763 if (!endptr)
1765 einfo (_("%P: warning: could not find end of"
1766 " S_GPROC32/S_LPROC32 record\n"));
1767 return false;
1770 /* Skip to after S_END. */
1772 size -= endptr - data;
1773 data = endptr;
1775 len = bfd_getl16 (data) + sizeof (uint16_t);
1777 data += len;
1778 size -= len;
1780 continue;
1783 scope_level++;
1785 break;
1788 case S_UDT:
1789 if (scope_level != 0) /* only goes in symbols if local */
1790 *sym_size += len;
1791 break;
1793 case S_BLOCK32: /* always copied */
1794 case S_INLINESITE:
1795 case S_THUNK32:
1796 *sym_size += len;
1797 scope_level++;
1798 break;
1800 case S_END: /* always copied */
1801 case S_PROC_ID_END:
1802 case S_INLINESITE_END:
1803 *sym_size += len;
1804 scope_level--;
1805 break;
1807 case S_OBJNAME: /* always copied */
1808 case S_COMPILE3:
1809 case S_UNAMESPACE:
1810 case S_FRAMEPROC:
1811 case S_FRAMECOOKIE:
1812 case S_LABEL32:
1813 case S_BUILDINFO:
1814 case S_BPREL32:
1815 case S_REGISTER:
1816 case S_REGREL32:
1817 case S_LOCAL:
1818 case S_DEFRANGE_REGISTER_REL:
1819 case S_DEFRANGE_FRAMEPOINTER_REL:
1820 case S_DEFRANGE_SUBFIELD_REGISTER:
1821 case S_DEFRANGE_FRAMEPOINTER_REL_FULL_SCOPE:
1822 case S_DEFRANGE_REGISTER:
1823 case S_HEAPALLOCSITE:
1824 *sym_size += len;
1825 break;
1827 default:
1828 einfo (_("%P: warning: unrecognized CodeView record %v\n"), type);
1829 return false;
1832 data += len;
1833 size -= len;
1836 return true;
1839 /* Parse the .debug$S section within an object file. */
1840 static bool
1841 handle_debugs_section (asection *s, bfd *mod, struct string_table *strings,
1842 uint8_t **dataptr, uint32_t *sizeptr,
1843 struct mod_source_files *mod_source,
1844 bfd *abfd, uint8_t **syms, uint32_t *sym_byte_size,
1845 struct type_entry **map, uint32_t num_types,
1846 bfd *sym_rec_stream, struct globals *glob,
1847 uint16_t mod_num)
1849 bfd_byte *data = NULL;
1850 size_t off;
1851 uint32_t c13_size = 0;
1852 char *string_table = NULL;
1853 uint8_t *buf, *bufptr, *symbuf, *symbufptr;
1854 uint32_t sym_size = 0;
1856 if (!bfd_get_full_section_contents (mod, s, &data))
1857 return false;
1859 if (!data)
1860 return false;
1862 /* Resolve relocations. Addresses are stored within the .debug$S section as
1863 a .secidx, .secrel32 pair. */
1865 if (s->flags & SEC_RELOC)
1867 struct internal_reloc *relocs;
1868 struct internal_syment *symbols;
1869 asection **sectlist;
1870 unsigned int syment_count;
1871 int sect_num;
1872 struct external_syment *ext;
1874 syment_count = obj_raw_syment_count (mod);
1876 relocs =
1877 _bfd_coff_read_internal_relocs (mod, s, false, NULL, true, NULL);
1879 symbols = xmalloc (sizeof (struct internal_syment) * syment_count);
1880 sectlist = xmalloc (sizeof (asection *) * syment_count);
1882 ext = (struct external_syment *) (coff_data (mod)->external_syms);
1884 for (unsigned int i = 0; i < syment_count; i++)
1886 bfd_coff_swap_sym_in (mod, &ext[i], &symbols[i]);
1889 sect_num = 1;
1891 for (asection *sect = mod->sections; sect; sect = sect->next)
1893 for (unsigned int i = 0; i < syment_count; i++)
1895 if (symbols[i].n_scnum == sect_num)
1896 sectlist[i] = sect;
1899 sect_num++;
1902 if (!bfd_coff_relocate_section (abfd, coff_data (abfd)->link_info, mod,
1903 s, data, relocs, symbols, sectlist))
1905 free (sectlist);
1906 free (symbols);
1907 free (data);
1908 return false;
1911 free (sectlist);
1912 free (symbols);
1915 if (bfd_getl32 (data) != CV_SIGNATURE_C13)
1917 free (data);
1918 return true;
1921 off = sizeof (uint32_t);
1923 /* calculate size */
1925 while (off + sizeof (uint32_t) <= s->size)
1927 uint32_t type, size;
1929 type = bfd_getl32 (data + off);
1931 off += sizeof (uint32_t);
1933 if (off + sizeof (uint32_t) > s->size)
1935 free (data);
1936 bfd_set_error (bfd_error_bad_value);
1937 return false;
1940 size = bfd_getl32 (data + off);
1942 off += sizeof (uint32_t);
1944 if (off + size > s->size)
1946 free (data);
1947 bfd_set_error (bfd_error_bad_value);
1948 return false;
1951 switch (type)
1953 case DEBUG_S_FILECHKSMS:
1954 c13_size += sizeof (uint32_t) + sizeof (uint32_t) + size;
1956 if (c13_size % sizeof (uint32_t))
1957 c13_size += sizeof (uint32_t) - (c13_size % sizeof (uint32_t));
1959 break;
1961 case DEBUG_S_STRINGTABLE:
1962 parse_string_table (data + off, size, strings);
1964 string_table = (char *) data + off;
1966 break;
1968 case DEBUG_S_LINES:
1970 uint16_t sect;
1972 if (size < sizeof (uint32_t) + sizeof (uint16_t))
1974 free (data);
1975 bfd_set_error (bfd_error_bad_value);
1976 return false;
1979 sect = bfd_getl16 (data + off + sizeof (uint32_t));
1981 /* Skip GC'd symbols. */
1982 if (sect != 0)
1984 c13_size += sizeof (uint32_t) + sizeof (uint32_t) + size;
1986 if (c13_size % sizeof (uint32_t))
1987 c13_size +=
1988 sizeof (uint32_t) - (c13_size % sizeof (uint32_t));
1991 break;
1994 case DEBUG_S_SYMBOLS:
1995 if (!calculate_symbols_size (data + off, size, &sym_size))
1997 free (data);
1998 bfd_set_error (bfd_error_bad_value);
1999 return false;
2002 break;
2005 off += size;
2007 if (off % sizeof (uint32_t))
2008 off += sizeof (uint32_t) - (off % sizeof (uint32_t));
2011 if (sym_size % sizeof (uint32_t))
2012 sym_size += sizeof (uint32_t) - (sym_size % sizeof (uint32_t));
2014 if (c13_size == 0 && sym_size == 0)
2016 free (data);
2017 return true;
2020 /* copy data */
2022 buf = NULL;
2023 if (c13_size != 0)
2024 buf = xmalloc (c13_size);
2025 bufptr = buf;
2027 symbuf = NULL;
2028 if (sym_size != 0)
2029 symbuf = xmalloc (sym_size);
2030 symbufptr = symbuf;
2032 off = sizeof (uint32_t);
2034 while (off + sizeof (uint32_t) <= s->size)
2036 uint32_t type, size;
2038 type = bfd_getl32 (data + off);
2039 off += sizeof (uint32_t);
2041 size = bfd_getl32 (data + off);
2042 off += sizeof (uint32_t);
2044 switch (type)
2046 case DEBUG_S_FILECHKSMS:
2047 if (!copy_filechksms (data + off, size, string_table,
2048 strings, bufptr, mod_source))
2050 free (data);
2051 free (symbuf);
2052 return false;
2055 bufptr += sizeof (uint32_t) + sizeof (uint32_t) + size;
2057 break;
2059 case DEBUG_S_LINES:
2061 uint16_t sect;
2063 sect = bfd_getl16 (data + off + sizeof (uint32_t));
2065 /* Skip if GC'd. */
2066 if (sect != 0)
2068 bfd_putl32 (type, bufptr);
2069 bufptr += sizeof (uint32_t);
2071 bfd_putl32 (size, bufptr);
2072 bufptr += sizeof (uint32_t);
2074 memcpy (bufptr, data + off, size);
2075 bufptr += size;
2078 break;
2081 case DEBUG_S_SYMBOLS:
2082 if (!parse_symbols (data + off, size, &symbufptr, map, num_types,
2083 sym_rec_stream, glob, mod_num))
2085 free (data);
2086 free (symbuf);
2087 return false;
2090 break;
2093 off += size;
2095 if (off % sizeof (uint32_t))
2096 off += sizeof (uint32_t) - (off % sizeof (uint32_t));
2099 free (data);
2101 if (buf)
2103 if (*dataptr)
2105 /* Append the C13 info to what's already there, if the module has
2106 multiple .debug$S sections. */
2108 *dataptr = xrealloc (*dataptr, *sizeptr + c13_size);
2109 memcpy (*dataptr + *sizeptr, buf, c13_size);
2111 free (buf);
2113 else
2115 *dataptr = buf;
2118 *sizeptr += c13_size;
2121 if (symbuf)
2123 if (*syms)
2125 *syms = xrealloc (*syms, *sym_byte_size + sym_size);
2126 memcpy (*syms + *sym_byte_size, symbuf, sym_size);
2128 free (symbuf);
2130 else
2132 *syms = symbuf;
2135 *sym_byte_size += sym_size;
2138 return true;
2141 /* Remap the type number stored in data from the per-module numbering to
2142 that of the deduplicated output list. */
2143 static bool
2144 remap_type (void *data, struct type_entry **map,
2145 uint32_t type_num, uint32_t num_types)
2147 uint32_t type = bfd_getl32 (data);
2149 /* Ignore builtin types (those with IDs below 0x1000). */
2150 if (type < TPI_FIRST_INDEX)
2151 return true;
2153 if (type >= TPI_FIRST_INDEX + type_num)
2155 einfo (_("%P: CodeView type %v references other type %v not yet "
2156 "declared\n"), TPI_FIRST_INDEX + type_num, type);
2157 return false;
2160 if (type >= TPI_FIRST_INDEX + num_types)
2162 einfo (_("%P: CodeView type %v references out of range type %v\n"),
2163 TPI_FIRST_INDEX + type_num, type);
2164 return false;
2167 type = TPI_FIRST_INDEX + map[type - TPI_FIRST_INDEX]->index;
2168 bfd_putl32 (type, data);
2170 return true;
2173 /* Determines whether the name of a struct, class, or union counts as
2174 "anonymous". Non-anonymous types have a hash based on just the name,
2175 rather than the whole structure. */
2176 static bool
2177 is_name_anonymous (char *name, size_t len)
2179 static const char tag1[] = "<unnamed-tag>";
2180 static const char tag2[] = "__unnamed";
2181 static const char tag3[] = "::<unnamed-tag>";
2182 static const char tag4[] = "::__unnamed";
2184 if (len == sizeof (tag1) - 1 && !memcmp (name, tag1, sizeof (tag1) - 1))
2185 return true;
2187 if (len == sizeof (tag2) - 1 && !memcmp (name, tag2, sizeof (tag2) - 1))
2188 return true;
2190 if (len >= sizeof (tag3) - 1
2191 && !memcmp (name + len - sizeof (tag3) + 1, tag3, sizeof (tag3) - 1))
2192 return true;
2194 if (len >= sizeof (tag4) - 1
2195 && !memcmp (name + len - sizeof (tag4) + 1, tag4, sizeof (tag4) - 1))
2196 return true;
2198 return false;
2201 /* Handle LF_UDT_SRC_LINE type entries, which are a special case. These
2202 give the source file and line number for each user-defined type that is
2203 declared. We parse these and emit instead an LF_UDT_MOD_SRC_LINE entry,
2204 which also includes the module number. */
2205 static bool
2206 handle_udt_src_line (uint8_t *data, uint16_t size, struct type_entry **map,
2207 uint32_t type_num, uint32_t num_types,
2208 struct types *ids, uint16_t mod_num,
2209 struct string_table *strings)
2211 struct lf_udt_src_line *usl = (struct lf_udt_src_line *) data;
2212 uint32_t orig_type, source_file_type;
2213 void **slot;
2214 hashval_t hash;
2215 struct type_entry *e, *type_e, *str_e;
2216 struct lf_udt_mod_src_line *umsl;
2217 struct lf_string_id *str;
2218 uint32_t source_file_offset;
2220 if (size < sizeof (struct lf_udt_src_line))
2222 einfo (_("%P: warning: truncated CodeView type record"
2223 " LF_UDT_SRC_LINE\n"));
2224 return false;
2227 /* Check if LF_UDT_MOD_SRC_LINE already present for type, and return. */
2229 orig_type = bfd_getl32 (&usl->type);
2231 if (orig_type < TPI_FIRST_INDEX ||
2232 orig_type >= TPI_FIRST_INDEX + num_types ||
2233 !map[orig_type - TPI_FIRST_INDEX])
2235 einfo (_("%P: warning: CodeView type record LF_UDT_SRC_LINE"
2236 " referred to unknown type %v\n"), orig_type);
2237 return false;
2240 type_e = map[orig_type - TPI_FIRST_INDEX];
2242 /* Skip if type already declared in other module. */
2243 if (type_e->has_udt_src_line)
2244 return true;
2246 if (!remap_type (&usl->type, map, type_num, num_types))
2247 return false;
2249 /* Extract string from source_file_type. */
2251 source_file_type = bfd_getl32 (&usl->source_file_type);
2253 if (source_file_type < TPI_FIRST_INDEX ||
2254 source_file_type >= TPI_FIRST_INDEX + num_types ||
2255 !map[source_file_type - TPI_FIRST_INDEX])
2257 einfo (_("%P: warning: CodeView type record LF_UDT_SRC_LINE"
2258 " referred to unknown string %v\n"), source_file_type);
2259 return false;
2262 str_e = map[source_file_type - TPI_FIRST_INDEX];
2264 if (bfd_getl16 (str_e->data + sizeof (uint16_t)) != LF_STRING_ID)
2266 einfo (_("%P: warning: CodeView type record LF_UDT_SRC_LINE"
2267 " pointed to unexpected record type\n"));
2268 return false;
2271 str = (struct lf_string_id *) str_e->data;
2273 /* Add string to string table. */
2275 source_file_offset = add_string (str->string, strlen (str->string),
2276 strings);
2278 /* Add LF_UDT_MOD_SRC_LINE entry. */
2280 size = sizeof (struct lf_udt_mod_src_line);
2282 e = xmalloc (offsetof (struct type_entry, data) + size);
2284 e->next = NULL;
2285 e->index = ids->num_types;
2286 e->has_udt_src_line = false;
2288 /* LF_UDT_MOD_SRC_LINE use calc_hash on the type number, rather than
2289 the crc32 used for type hashes elsewhere. */
2290 e->cv_hash = calc_hash ((char *) &usl->type, sizeof (uint32_t));
2292 type_e->has_udt_src_line = true;
2294 umsl = (struct lf_udt_mod_src_line *) e->data;
2296 bfd_putl16 (size - sizeof (uint16_t), &umsl->size);
2297 bfd_putl16 (LF_UDT_MOD_SRC_LINE, &umsl->kind);
2298 memcpy (&umsl->type, &usl->type, sizeof (uint32_t));
2299 bfd_putl32 (source_file_offset, &umsl->source_file_string);
2300 memcpy (&umsl->line_no, &usl->line_no, sizeof (uint32_t));
2301 bfd_putl16 (mod_num + 1, &umsl->module_no);
2303 hash = iterative_hash (e->data, size, 0);
2305 slot = htab_find_slot_with_hash (ids->hashmap, data, hash, INSERT);
2306 if (!slot)
2308 free (e);
2309 return false;
2312 if (*slot)
2314 free (e);
2315 einfo (_("%P: warning: duplicate CodeView type record "
2316 "LF_UDT_MOD_SRC_LINE\n"));
2317 return false;
2320 *slot = e;
2322 if (ids->last)
2323 ids->last->next = e;
2324 else
2325 ids->first = e;
2327 ids->last = e;
2329 map[type_num] = e;
2331 ids->num_types++;
2333 return true;
2336 /* Parse a type definition in the .debug$T section. We remap the numbers
2337 of any referenced types, and if the type is not a duplicate of one
2338 already seen add it to types (for TPI types) or ids (for IPI types). */
2339 static bool
2340 handle_type (uint8_t *data, struct type_entry **map, uint32_t type_num,
2341 uint32_t num_types, struct types *types,
2342 struct types *ids, uint16_t mod_num,
2343 struct string_table *strings)
2345 uint16_t size, type;
2346 void **slot;
2347 hashval_t hash;
2348 bool other_hash = false;
2349 uint32_t cv_hash;
2350 struct types *t;
2351 bool ipi = false;
2353 size = bfd_getl16 (data) + sizeof (uint16_t);
2354 type = bfd_getl16 (data + sizeof (uint16_t));
2356 switch (type)
2358 case LF_MODIFIER:
2360 struct lf_modifier *mod = (struct lf_modifier *) data;
2362 if (size < offsetof (struct lf_modifier, modifier))
2364 einfo (_("%P: warning: truncated CodeView type record "
2365 "LF_MODIFIER\n"));
2366 return false;
2369 if (!remap_type (&mod->base_type, map, type_num, num_types))
2370 return false;
2372 break;
2375 case LF_POINTER:
2377 struct lf_pointer *ptr = (struct lf_pointer *) data;
2379 if (size < offsetof (struct lf_pointer, attributes))
2381 einfo (_("%P: warning: truncated CodeView type record"
2382 " LF_POINTER\n"));
2383 return false;
2386 if (!remap_type (&ptr->base_type, map, type_num, num_types))
2387 return false;
2389 break;
2392 case LF_PROCEDURE:
2394 struct lf_procedure *proc = (struct lf_procedure *) data;
2396 if (size < sizeof (struct lf_procedure))
2398 einfo (_("%P: warning: truncated CodeView type record"
2399 " LF_PROCEDURE\n"));
2400 return false;
2403 if (!remap_type (&proc->return_type, map, type_num, num_types))
2404 return false;
2406 if (!remap_type (&proc->arglist, map, type_num, num_types))
2407 return false;
2409 break;
2412 case LF_MFUNCTION:
2414 struct lf_mfunction *func = (struct lf_mfunction *) data;
2416 if (size < sizeof (struct lf_procedure))
2418 einfo (_("%P: warning: truncated CodeView type record"
2419 " LF_MFUNCTION\n"));
2420 return false;
2423 if (!remap_type (&func->return_type, map, type_num, num_types))
2424 return false;
2426 if (!remap_type (&func->containing_class_type, map, type_num,
2427 num_types))
2428 return false;
2430 if (!remap_type (&func->this_type, map, type_num, num_types))
2431 return false;
2433 if (!remap_type (&func->arglist, map, type_num, num_types))
2434 return false;
2436 break;
2439 case LF_ARGLIST:
2441 uint32_t num_entries;
2442 struct lf_arglist *al = (struct lf_arglist *) data;
2444 if (size < offsetof (struct lf_arglist, args))
2446 einfo (_("%P: warning: truncated CodeView type record"
2447 " LF_ARGLIST\n"));
2448 return false;
2451 num_entries = bfd_getl32 (&al->num_entries);
2453 if (size < offsetof (struct lf_arglist, args)
2454 + (num_entries * sizeof (uint32_t)))
2456 einfo (_("%P: warning: truncated CodeView type record"
2457 " LF_ARGLIST\n"));
2458 return false;
2461 for (uint32_t i = 0; i < num_entries; i++)
2463 if (!remap_type (&al->args[i], map, type_num, num_types))
2464 return false;
2467 break;
2470 case LF_FIELDLIST:
2472 uint16_t left = size - sizeof (uint16_t) - sizeof (uint16_t);
2473 uint8_t *ptr = data + sizeof (uint16_t) + sizeof (uint16_t);
2475 while (left > 0)
2477 uint16_t subtype;
2479 if (left < sizeof (uint16_t))
2481 einfo (_("%P: warning: truncated CodeView type record"
2482 " LF_FIELDLIST\n"));
2483 return false;
2486 subtype = bfd_getl16 (ptr);
2488 switch (subtype)
2490 case LF_MEMBER:
2492 struct lf_member *mem = (struct lf_member *) ptr;
2493 uint16_t offset;
2494 size_t name_len, subtype_len;
2496 if (left < offsetof (struct lf_member, name))
2498 einfo (_("%P: warning: truncated CodeView type record"
2499 " LF_MEMBER\n"));
2500 return false;
2503 if (!remap_type (&mem->type, map, type_num, num_types))
2504 return false;
2506 subtype_len = offsetof (struct lf_member, name);
2508 offset = bfd_getl16 (&mem->offset);
2510 /* If offset >= 0x8000, actual value follows. */
2511 if (offset >= 0x8000)
2513 unsigned int param_len = extended_value_len (offset);
2515 if (param_len == 0)
2517 einfo (_("%P: warning: unhandled type %v within"
2518 " LF_MEMBER\n"), offset);
2519 return false;
2522 subtype_len += param_len;
2524 if (left < subtype_len)
2526 einfo (_("%P: warning: truncated CodeView type record"
2527 " LF_MEMBER\n"));
2528 return false;
2532 name_len =
2533 strnlen ((char *) mem + subtype_len, left - subtype_len);
2535 if (name_len == left - offsetof (struct lf_member, name))
2537 einfo (_("%P: warning: name for LF_MEMBER has no"
2538 " terminating zero\n"));
2539 return false;
2542 name_len++;
2544 subtype_len += name_len;
2546 if (subtype_len % 4 != 0)
2547 subtype_len += 4 - (subtype_len % 4);
2549 if (left < subtype_len)
2551 einfo (_("%P: warning: truncated CodeView type record"
2552 " LF_FIELDLIST\n"));
2553 return false;
2556 ptr += subtype_len;
2557 left -= subtype_len;
2559 break;
2562 case LF_ENUMERATE:
2564 struct lf_enumerate *en = (struct lf_enumerate *) ptr;
2565 size_t name_len, subtype_len;
2566 uint16_t val;
2568 if (left < offsetof (struct lf_enumerate, name))
2570 einfo (_("%P: warning: truncated CodeView type record"
2571 " LF_ENUMERATE\n"));
2572 return false;
2575 subtype_len = offsetof (struct lf_enumerate, name);
2577 val = bfd_getl16 (&en->value);
2579 /* If val >= 0x8000, the actual value immediately follows. */
2580 if (val >= 0x8000)
2582 unsigned int param_len = extended_value_len (val);
2584 if (param_len == 0)
2586 einfo (_("%P: warning: unhandled type %v within"
2587 " LF_ENUMERATE\n"), val);
2588 return false;
2591 if (left < subtype_len + param_len)
2593 einfo (_("%P: warning: truncated CodeView type"
2594 " record LF_ENUMERATE\n"));
2595 return false;
2598 subtype_len += param_len;
2601 name_len = strnlen ((char *) ptr + subtype_len,
2602 left - subtype_len);
2604 if (name_len == left - offsetof (struct lf_enumerate, name))
2606 einfo (_("%P: warning: name for LF_ENUMERATE has no"
2607 " terminating zero\n"));
2608 return false;
2611 name_len++;
2613 subtype_len += name_len;
2615 if (subtype_len % 4 != 0)
2616 subtype_len += 4 - (subtype_len % 4);
2618 if (left < subtype_len)
2620 einfo (_("%P: warning: truncated CodeView type record"
2621 " LF_ENUMERATE\n"));
2622 return false;
2625 ptr += subtype_len;
2626 left -= subtype_len;
2628 break;
2631 case LF_INDEX:
2633 struct lf_index *ind = (struct lf_index *) ptr;
2635 if (left < sizeof (struct lf_index))
2637 einfo (_("%P: warning: truncated CodeView type record"
2638 " LF_INDEX\n"));
2639 return false;
2642 if (!remap_type (&ind->index, map, type_num, num_types))
2643 return false;
2645 ptr += sizeof (struct lf_index);
2646 left -= sizeof (struct lf_index);
2648 break;
2651 case LF_ONEMETHOD:
2653 struct lf_onemethod *meth = (struct lf_onemethod *) ptr;
2654 size_t name_len, subtype_len;
2656 if (left < offsetof (struct lf_onemethod, name))
2658 einfo (_("%P: warning: truncated CodeView type record"
2659 " LF_ONEMETHOD\n"));
2660 return false;
2663 if (!remap_type (&meth->method_type, map, type_num,
2664 num_types))
2665 return false;
2667 name_len =
2668 strnlen (meth->name,
2669 left - offsetof (struct lf_onemethod, name));
2671 if (name_len == left - offsetof (struct lf_onemethod, name))
2673 einfo (_("%P: warning: name for LF_ONEMETHOD has no"
2674 " terminating zero\n"));
2675 return false;
2678 name_len++;
2680 subtype_len = offsetof (struct lf_onemethod, name)
2681 + name_len;
2683 if (subtype_len % 4 != 0)
2684 subtype_len += 4 - (subtype_len % 4);
2686 if (left < subtype_len)
2688 einfo (_("%P: warning: truncated CodeView type record"
2689 " LF_FIELDLIST\n"));
2690 return false;
2693 ptr += subtype_len;
2694 left -= subtype_len;
2696 break;
2699 case LF_METHOD:
2701 struct lf_method *meth = (struct lf_method *) ptr;
2702 size_t name_len, subtype_len;
2704 if (left < offsetof (struct lf_method, name))
2706 einfo (_("%P: warning: truncated CodeView type record"
2707 " LF_METHOD\n"));
2708 return false;
2711 if (!remap_type (&meth->method_list, map, type_num,
2712 num_types))
2713 return false;
2715 name_len =
2716 strnlen (meth->name,
2717 left - offsetof (struct lf_method, name));
2719 if (name_len == left - offsetof (struct lf_method, name))
2721 einfo (_("%P: warning: name for LF_METHOD has no"
2722 " terminating zero\n"));
2723 return false;
2726 name_len++;
2728 subtype_len = offsetof (struct lf_method, name) + name_len;
2730 if (subtype_len % 4 != 0)
2731 subtype_len += 4 - (subtype_len % 4);
2733 if (left < subtype_len)
2735 einfo (_("%P: warning: truncated CodeView type record"
2736 " LF_FIELDLIST\n"));
2737 return false;
2740 ptr += subtype_len;
2741 left -= subtype_len;
2743 break;
2746 case LF_BCLASS:
2748 struct lf_bclass *bc = (struct lf_bclass *) ptr;
2749 size_t subtype_len;
2750 uint16_t offset;
2752 if (left < sizeof (struct lf_bclass))
2754 einfo (_("%P: warning: truncated CodeView type record"
2755 " LF_BCLASS\n"));
2756 return false;
2759 if (!remap_type (&bc->base_class_type, map, type_num,
2760 num_types))
2761 return false;
2763 subtype_len = sizeof (struct lf_bclass);
2765 offset = bfd_getl16 (&bc->offset);
2767 /* If offset >= 0x8000, actual value follows. */
2768 if (offset >= 0x8000)
2770 unsigned int param_len = extended_value_len (offset);
2772 if (param_len == 0)
2774 einfo (_("%P: warning: unhandled type %v within"
2775 " LF_BCLASS\n"), offset);
2776 return false;
2779 subtype_len += param_len;
2781 if (left < subtype_len)
2783 einfo (_("%P: warning: truncated CodeView type record"
2784 " LF_BCLASS\n"));
2785 return false;
2789 if (subtype_len % 4 != 0)
2790 subtype_len += 4 - (subtype_len % 4);
2792 if (left < subtype_len)
2794 einfo (_("%P: warning: truncated CodeView type record"
2795 " LF_BCLASS\n"));
2796 return false;
2799 ptr += subtype_len;
2800 left -= subtype_len;
2802 break;
2805 case LF_VFUNCTAB:
2807 struct lf_vfunctab *vft = (struct lf_vfunctab *) ptr;
2809 if (left < sizeof (struct lf_vfunctab))
2811 einfo (_("%P: warning: truncated CodeView type record"
2812 " LF_VFUNCTAB\n"));
2813 return false;
2816 if (!remap_type (&vft->type, map, type_num, num_types))
2817 return false;
2819 ptr += sizeof (struct lf_vfunctab);
2820 left -= sizeof (struct lf_vfunctab);
2822 break;
2825 case LF_VBCLASS:
2826 case LF_IVBCLASS:
2828 struct lf_vbclass *vbc = (struct lf_vbclass *) ptr;
2829 size_t subtype_len;
2830 uint16_t offset;
2832 if (left < sizeof (struct lf_vbclass))
2834 einfo (_("%P: warning: truncated CodeView type record"
2835 " LF_VBCLASS/LF_IVBCLASS\n"));
2836 return false;
2839 if (!remap_type (&vbc->base_class_type, map, type_num,
2840 num_types))
2841 return false;
2843 if (!remap_type (&vbc->virtual_base_pointer_type, map,
2844 type_num, num_types))
2845 return false;
2847 subtype_len = offsetof (struct lf_vbclass,
2848 virtual_base_vbtable_offset);
2850 offset = bfd_getl16 (&vbc->virtual_base_pointer_offset);
2852 /* If offset >= 0x8000, actual value follows. */
2853 if (offset >= 0x8000)
2855 unsigned int param_len = extended_value_len (offset);
2857 if (param_len == 0)
2859 einfo (_("%P: warning: unhandled type %v within"
2860 " LF_VBCLASS/LF_IVBCLASS\n"), offset);
2861 return false;
2864 subtype_len += param_len;
2866 if (left < subtype_len)
2868 einfo (_("%P: warning: truncated CodeView type record"
2869 " LF_VBCLASS/LF_IVBCLASS\n"));
2870 return false;
2874 offset = bfd_getl16 ((char *)vbc + subtype_len);
2875 subtype_len += sizeof (uint16_t);
2877 /* If offset >= 0x8000, actual value follows. */
2878 if (offset >= 0x8000)
2880 unsigned int param_len = extended_value_len (offset);
2882 if (param_len == 0)
2884 einfo (_("%P: warning: unhandled type %v within"
2885 " LF_VBCLASS/LF_IVBCLASS\n"), offset);
2886 return false;
2889 subtype_len += param_len;
2891 if (left < subtype_len)
2893 einfo (_("%P: warning: truncated CodeView type record"
2894 " LF_VBCLASS/LF_IVBCLASS\n"));
2895 return false;
2899 if (subtype_len % 4 != 0)
2900 subtype_len += 4 - (subtype_len % 4);
2902 if (left < subtype_len)
2904 einfo (_("%P: warning: truncated CodeView type record"
2905 " LF_VBCLASS/LF_IVBCLASS\n"));
2906 return false;
2909 ptr += subtype_len;
2910 left -= subtype_len;
2912 break;
2915 case LF_STMEMBER:
2917 struct lf_static_member *st =
2918 (struct lf_static_member *) ptr;
2919 size_t name_len, subtype_len;
2921 if (left < offsetof (struct lf_static_member, name))
2923 einfo (_("%P: warning: truncated CodeView type record"
2924 " LF_STMEMBER\n"));
2925 return false;
2928 if (!remap_type (&st->type, map, type_num, num_types))
2929 return false;
2931 name_len =
2932 strnlen (st->name,
2933 left - offsetof (struct lf_static_member, name));
2935 if (name_len == left
2936 - offsetof (struct lf_static_member, name))
2938 einfo (_("%P: warning: name for LF_STMEMBER has no"
2939 " terminating zero\n"));
2940 return false;
2943 name_len++;
2945 subtype_len = offsetof (struct lf_static_member, name)
2946 + name_len;
2948 if (subtype_len % 4 != 0)
2949 subtype_len += 4 - (subtype_len % 4);
2951 if (left < subtype_len)
2953 einfo (_("%P: warning: truncated CodeView type record"
2954 " LF_FIELDLIST\n"));
2955 return false;
2958 ptr += subtype_len;
2959 left -= subtype_len;
2961 break;
2964 case LF_NESTTYPE:
2966 struct lf_nest_type *nest = (struct lf_nest_type *) ptr;
2967 size_t name_len, subtype_len;
2969 if (left < offsetof (struct lf_nest_type, name))
2971 einfo (_("%P: warning: truncated CodeView type record"
2972 " LF_NESTTYPE\n"));
2973 return false;
2976 if (!remap_type (&nest->type, map, type_num, num_types))
2977 return false;
2979 name_len =
2980 strnlen (nest->name,
2981 left - offsetof (struct lf_nest_type, name));
2983 if (name_len == left - offsetof (struct lf_nest_type, name))
2985 einfo (_("%P: warning: name for LF_NESTTYPE has no"
2986 " terminating zero\n"));
2987 return false;
2990 name_len++;
2992 subtype_len = offsetof (struct lf_nest_type, name)
2993 + name_len;
2995 if (subtype_len % 4 != 0)
2996 subtype_len += 4 - (subtype_len % 4);
2998 if (left < subtype_len)
3000 einfo (_("%P: warning: truncated CodeView type record"
3001 " LF_FIELDLIST\n"));
3002 return false;
3005 ptr += subtype_len;
3006 left -= subtype_len;
3008 break;
3011 default:
3012 einfo (_("%P: warning: unrecognized CodeView subtype %v\n"),
3013 subtype);
3014 return false;
3018 break;
3021 case LF_BITFIELD:
3023 struct lf_bitfield *bf = (struct lf_bitfield *) data;
3025 if (size < offsetof (struct lf_bitfield, length))
3027 einfo (_("%P: warning: truncated CodeView type record"
3028 " LF_BITFIELD\n"));
3029 return false;
3032 if (!remap_type (&bf->base_type, map, type_num, num_types))
3033 return false;
3035 break;
3038 case LF_METHODLIST:
3040 struct lf_methodlist *ml = (struct lf_methodlist *) data;
3041 unsigned int num_entries;
3043 if (size < offsetof (struct lf_methodlist, entries))
3045 einfo (_("%P: warning: truncated CodeView type record"
3046 " LF_METHODLIST\n"));
3047 return false;
3050 if ((size - offsetof (struct lf_methodlist, entries))
3051 % sizeof (struct lf_methodlist_entry))
3053 einfo (_("%P: warning: malformed CodeView type record"
3054 " LF_METHODLIST\n"));
3055 return false;
3058 num_entries = (size - offsetof (struct lf_methodlist, entries))
3059 / sizeof (struct lf_methodlist_entry);
3061 for (unsigned int i = 0; i < num_entries; i++)
3063 if (!remap_type (&ml->entries[i].method_type, map,
3064 type_num, num_types))
3065 return false;
3068 break;
3071 case LF_ARRAY:
3073 struct lf_array *arr = (struct lf_array *) data;
3075 if (size < offsetof (struct lf_array, length_in_bytes))
3077 einfo (_("%P: warning: truncated CodeView type record"
3078 " LF_ARRAY\n"));
3079 return false;
3082 if (!remap_type (&arr->element_type, map, type_num, num_types))
3083 return false;
3085 if (!remap_type (&arr->index_type, map, type_num, num_types))
3086 return false;
3088 break;
3091 case LF_CLASS:
3092 case LF_STRUCTURE:
3094 struct lf_class *cl = (struct lf_class *) data;
3095 uint16_t prop, num_bytes;
3096 size_t name_len, name_off;
3098 if (size < offsetof (struct lf_class, name))
3100 einfo (_("%P: warning: truncated CodeView type record"
3101 " LF_CLASS/LF_STRUCTURE\n"));
3102 return false;
3105 if (!remap_type (&cl->field_list, map, type_num, num_types))
3106 return false;
3108 if (!remap_type (&cl->derived_from, map, type_num, num_types))
3109 return false;
3111 if (!remap_type (&cl->vshape, map, type_num, num_types))
3112 return false;
3114 name_off = offsetof (struct lf_class, name);
3116 num_bytes = bfd_getl16 (&cl->length);
3118 /* If num_bytes >= 0x8000, actual value follows. */
3119 if (num_bytes >= 0x8000)
3121 unsigned int param_len = extended_value_len (num_bytes);
3123 if (param_len == 0)
3125 einfo (_("%P: warning: unhandled type %v within"
3126 " LF_CLASS/LF_STRUCTURE\n"), num_bytes);
3127 return false;
3130 name_off += param_len;
3132 if (size < name_off)
3134 einfo (_("%P: warning: truncated CodeView type record"
3135 " LF_CLASS/LF_STRUCTURE\n"));
3136 return false;
3140 name_len = strnlen ((char *) cl + name_off, size - name_off);
3142 if (name_len == size - name_off)
3144 einfo (_("%P: warning: name for LF_CLASS/LF_STRUCTURE has no"
3145 " terminating zero\n"));
3146 return false;
3149 prop = bfd_getl16 (&cl->properties);
3151 if (prop & CV_PROP_HAS_UNIQUE_NAME)
3153 /* Structure has another name following first one. */
3155 size_t len = name_off + name_len + 1;
3156 size_t unique_name_len;
3158 unique_name_len = strnlen ((char *) cl + name_off + name_len + 1,
3159 size - len);
3161 if (unique_name_len == size - len)
3163 einfo (_("%P: warning: unique name for LF_CLASS/LF_STRUCTURE"
3164 " has no terminating zero\n"));
3165 return false;
3169 if (!(prop & (CV_PROP_FORWARD_REF | CV_PROP_SCOPED))
3170 && !is_name_anonymous ((char *) cl + name_off, name_len))
3172 other_hash = true;
3173 cv_hash = crc32 ((uint8_t *) cl + name_off, name_len);
3176 break;
3179 case LF_UNION:
3181 struct lf_union *un = (struct lf_union *) data;
3182 uint16_t prop, num_bytes;
3183 size_t name_len, name_off;
3185 if (size < offsetof (struct lf_union, name))
3187 einfo (_("%P: warning: truncated CodeView type record"
3188 " LF_UNION\n"));
3189 return false;
3192 if (!remap_type (&un->field_list, map, type_num, num_types))
3193 return false;
3195 name_off = offsetof (struct lf_union, name);
3197 num_bytes = bfd_getl16 (&un->length);
3199 /* If num_bytes >= 0x8000, actual value follows. */
3200 if (num_bytes >= 0x8000)
3202 unsigned int param_len = extended_value_len (num_bytes);
3204 if (param_len == 0)
3206 einfo (_("%P: warning: unhandled type %v within"
3207 " LF_UNION\n"), num_bytes);
3208 return false;
3211 name_off += param_len;
3213 if (size < name_off)
3215 einfo (_("%P: warning: truncated CodeView type record"
3216 " LF_UNION\n"));
3217 return false;
3221 name_len = strnlen ((char *) un + name_off, size - name_off);
3223 if (name_len == size - name_off)
3225 einfo (_("%P: warning: name for LF_UNION has no"
3226 " terminating zero\n"));
3227 return false;
3230 prop = bfd_getl16 (&un->properties);
3232 if (prop & CV_PROP_HAS_UNIQUE_NAME)
3234 /* Structure has another name following first one. */
3236 size_t len = name_off + name_len + 1;
3237 size_t unique_name_len;
3239 unique_name_len = strnlen ((char *) un + name_off + name_len + 1,
3240 size - len);
3242 if (unique_name_len == size - len)
3244 einfo (_("%P: warning: unique name for LF_UNION has"
3245 " no terminating zero\n"));
3246 return false;
3250 if (!(prop & (CV_PROP_FORWARD_REF | CV_PROP_SCOPED))
3251 && !is_name_anonymous ((char *) un + name_off, name_len))
3253 other_hash = true;
3254 cv_hash = crc32 ((uint8_t *) un + name_off, name_len);
3257 break;
3260 case LF_ENUM:
3262 struct lf_enum *en = (struct lf_enum *) data;
3263 uint16_t prop;
3264 size_t name_len;
3266 if (size < offsetof (struct lf_enum, name))
3268 einfo (_("%P: warning: truncated CodeView type record"
3269 " LF_ENUM\n"));
3270 return false;
3273 if (!remap_type (&en->underlying_type, map, type_num, num_types))
3274 return false;
3276 if (!remap_type (&en->field_list, map, type_num, num_types))
3277 return false;
3279 name_len = strnlen (en->name, size - offsetof (struct lf_enum, name));
3281 if (name_len == size - offsetof (struct lf_enum, name))
3283 einfo (_("%P: warning: name for LF_ENUM has no"
3284 " terminating zero\n"));
3285 return false;
3288 prop = bfd_getl16 (&en->properties);
3290 if (prop & CV_PROP_HAS_UNIQUE_NAME)
3292 /* Structure has another name following first one. */
3294 size_t len = offsetof (struct lf_enum, name) + name_len + 1;
3295 size_t unique_name_len;
3297 unique_name_len = strnlen (en->name + name_len + 1, size - len);
3299 if (unique_name_len == size - len)
3301 einfo (_("%P: warning: unique name for LF_ENUM has"
3302 " no terminating zero\n"));
3303 return false;
3307 break;
3310 case LF_VTSHAPE:
3311 /* Does not reference any types, nothing to be done. */
3312 break;
3314 case LF_VFTABLE:
3316 struct lf_vftable *vft = (struct lf_vftable *) data;
3318 if (size < offsetof (struct lf_vftable, names))
3320 einfo (_("%P: warning: truncated CodeView type record"
3321 " LF_VFTABLE\n"));
3322 return false;
3325 if (!remap_type (&vft->type, map, type_num, num_types))
3326 return false;
3328 if (!remap_type (&vft->base_vftable, map, type_num, num_types))
3329 return false;
3331 break;
3334 case LF_STRING_ID:
3336 struct lf_string_id *str = (struct lf_string_id *) data;
3337 size_t string_len;
3339 if (size < offsetof (struct lf_string_id, string))
3341 einfo (_("%P: warning: truncated CodeView type record"
3342 " LF_STRING_ID\n"));
3343 return false;
3346 if (!remap_type (&str->substring, map, type_num, num_types))
3347 return false;
3349 string_len = strnlen (str->string,
3350 size - offsetof (struct lf_string_id, string));
3352 if (string_len == size - offsetof (struct lf_string_id, string))
3354 einfo (_("%P: warning: string for LF_STRING_ID has no"
3355 " terminating zero\n"));
3356 return false;
3359 ipi = true;
3361 break;
3364 case LF_SUBSTR_LIST:
3366 uint32_t num_entries;
3367 struct lf_arglist *ssl = (struct lf_arglist *) data;
3369 if (size < offsetof (struct lf_arglist, args))
3371 einfo (_("%P: warning: truncated CodeView type record"
3372 " LF_SUBSTR_LIST\n"));
3373 return false;
3376 num_entries = bfd_getl32 (&ssl->num_entries);
3378 if (size < offsetof (struct lf_arglist, args)
3379 + (num_entries * sizeof (uint32_t)))
3381 einfo (_("%P: warning: truncated CodeView type record"
3382 " LF_SUBSTR_LIST\n"));
3383 return false;
3386 for (uint32_t i = 0; i < num_entries; i++)
3388 if (!remap_type (&ssl->args[i], map, type_num, num_types))
3389 return false;
3392 ipi = true;
3394 break;
3397 case LF_BUILDINFO:
3399 uint16_t num_entries;
3400 struct lf_build_info *bi = (struct lf_build_info *) data;
3402 if (size < offsetof (struct lf_build_info, strings))
3404 einfo (_("%P: warning: truncated CodeView type record"
3405 " LF_BUILDINFO\n"));
3406 return false;
3409 num_entries = bfd_getl16 (&bi->count);
3411 if (size < offsetof (struct lf_build_info, strings)
3412 + (num_entries * sizeof (uint32_t)))
3414 einfo (_("%P: warning: truncated CodeView type record"
3415 " LF_BUILDINFO\n"));
3416 return false;
3419 for (uint32_t i = 0; i < num_entries; i++)
3421 if (!remap_type (&bi->strings[i], map, type_num, num_types))
3422 return false;
3425 ipi = true;
3427 break;
3430 case LF_FUNC_ID:
3432 struct lf_func_id *func = (struct lf_func_id *) data;
3433 size_t name_len;
3435 if (size < offsetof (struct lf_func_id, name))
3437 einfo (_("%P: warning: truncated CodeView type record"
3438 " LF_FUNC_ID\n"));
3439 return false;
3442 if (!remap_type (&func->parent_scope, map, type_num, num_types))
3443 return false;
3445 if (!remap_type (&func->function_type, map, type_num, num_types))
3446 return false;
3448 name_len = strnlen (func->name,
3449 size - offsetof (struct lf_func_id, name));
3451 if (name_len == size - offsetof (struct lf_func_id, name))
3453 einfo (_("%P: warning: string for LF_FUNC_ID has no"
3454 " terminating zero\n"));
3455 return false;
3458 ipi = true;
3460 break;
3463 case LF_MFUNC_ID:
3465 struct lf_mfunc_id *mfunc = (struct lf_mfunc_id *) data;
3466 size_t name_len;
3468 if (size < offsetof (struct lf_mfunc_id, name))
3470 einfo (_("%P: warning: truncated CodeView type record"
3471 " LF_MFUNC_ID\n"));
3472 return false;
3475 if (!remap_type (&mfunc->parent_type, map, type_num, num_types))
3476 return false;
3478 if (!remap_type (&mfunc->function_type, map, type_num, num_types))
3479 return false;
3481 name_len = strnlen (mfunc->name,
3482 size - offsetof (struct lf_mfunc_id, name));
3484 if (name_len == size - offsetof (struct lf_mfunc_id, name))
3486 einfo (_("%P: warning: string for LF_MFUNC_ID has no"
3487 " terminating zero\n"));
3488 return false;
3491 ipi = true;
3493 break;
3496 case LF_UDT_SRC_LINE:
3497 return handle_udt_src_line (data, size, map, type_num, num_types,
3498 ids, mod_num, strings);
3500 default:
3501 einfo (_("%P: warning: unrecognized CodeView type %v\n"), type);
3502 return false;
3505 hash = iterative_hash (data, size, 0);
3507 t = ipi ? ids : types;
3509 slot = htab_find_slot_with_hash (t->hashmap, data, hash, INSERT);
3510 if (!slot)
3511 return false;
3513 if (!*slot) /* new entry */
3515 struct type_entry *e;
3517 *slot = xmalloc (offsetof (struct type_entry, data) + size);
3519 e = (struct type_entry *) *slot;
3521 e->next = NULL;
3522 e->index = t->num_types;
3524 if (other_hash)
3525 e->cv_hash = cv_hash;
3526 else
3527 e->cv_hash = crc32 (data, size);
3529 e->has_udt_src_line = false;
3531 memcpy (e->data, data, size);
3533 if (t->last)
3534 t->last->next = e;
3535 else
3536 t->first = e;
3538 t->last = e;
3540 map[type_num] = e;
3542 t->num_types++;
3544 else /* duplicate */
3546 map[type_num] = (struct type_entry *) *slot;
3549 return true;
3552 /* Parse the .debug$T section of a module, and pass any type definitions
3553 found to handle_type. */
3554 static bool
3555 handle_debugt_section (asection *s, bfd *mod, struct types *types,
3556 struct types *ids, uint16_t mod_num,
3557 struct string_table *strings,
3558 struct type_entry ***map, uint32_t *num_types)
3560 bfd_byte *data = NULL;
3561 size_t off;
3562 uint32_t type_num;
3564 if (!bfd_get_full_section_contents (mod, s, &data))
3565 return false;
3567 if (!data)
3568 return false;
3570 if (bfd_getl32 (data) != CV_SIGNATURE_C13)
3572 free (data);
3573 return true;
3576 off = sizeof (uint32_t);
3578 while (off + sizeof (uint16_t) <= s->size)
3580 uint16_t size;
3582 size = bfd_getl16 (data + off);
3583 off += sizeof (uint16_t);
3585 if (size + off > s->size || size <= sizeof (uint16_t))
3587 free (data);
3588 bfd_set_error (bfd_error_bad_value);
3589 return false;
3592 (*num_types)++;
3593 off += size;
3596 if (*num_types == 0)
3598 free (data);
3599 return true;
3602 *map = xcalloc (*num_types, sizeof (struct type_entry *));
3604 off = sizeof (uint32_t);
3605 type_num = 0;
3607 while (off + sizeof (uint16_t) <= s->size)
3609 uint16_t size;
3611 size = bfd_getl16 (data + off);
3613 if (!handle_type (data + off, *map, type_num, *num_types, types, ids,
3614 mod_num, strings))
3616 free (data);
3617 free (*map);
3618 bfd_set_error (bfd_error_bad_value);
3619 return false;
3622 off += sizeof (uint16_t) + size;
3623 type_num++;
3626 free (data);
3628 return true;
3631 /* Return the CodeView constant for the selected architecture. */
3632 static uint16_t
3633 target_processor (bfd *abfd)
3635 switch (abfd->arch_info->arch)
3637 case bfd_arch_i386:
3638 if (abfd->arch_info->mach & bfd_mach_x86_64)
3639 return CV_CFL_X64;
3640 else
3641 return CV_CFL_80386;
3643 case bfd_arch_aarch64:
3644 return CV_CFL_ARM64;
3646 default:
3647 return 0;
3651 /* Create the symbols that go in "* Linker *", the dummy module created
3652 for the linker itself. */
3653 static bool
3654 create_linker_symbols (bfd *abfd, uint8_t **syms, uint32_t *sym_byte_size,
3655 const char *pdb_name)
3657 uint8_t *ptr;
3658 struct objname *name;
3659 struct compile3 *comp;
3660 struct envblock *env;
3661 size_t padding1, padding2, env_size;
3662 char *cwdval, *exeval, *pdbval;
3664 /* extra NUL for padding */
3665 static const char linker_fn[] = "* Linker *\0";
3666 static const char linker_name[] = "GNU LD " VERSION;
3668 static const char cwd[] = "cwd";
3669 static const char exe[] = "exe";
3670 static const char pdb[] = "pdb";
3672 cwdval = getcwd (NULL, 0);
3673 if (!cwdval)
3675 einfo (_("%P: warning: unable to get working directory\n"));
3676 return false;
3679 exeval = lrealpath (program_name);
3681 if (!exeval)
3683 einfo (_("%P: warning: unable to get program name\n"));
3684 free (cwdval);
3685 return false;
3688 pdbval = lrealpath (pdb_name);
3690 if (!pdbval)
3692 einfo (_("%P: warning: unable to get full path to PDB\n"));
3693 free (exeval);
3694 free (cwdval);
3695 return false;
3698 *sym_byte_size += offsetof (struct objname, name) + sizeof (linker_fn);
3699 *sym_byte_size += offsetof (struct compile3, compiler) + sizeof (linker_name);
3701 if (*sym_byte_size % 4)
3702 padding1 = 4 - (*sym_byte_size % 4);
3703 else
3704 padding1 = 0;
3706 *sym_byte_size += padding1;
3708 env_size = offsetof (struct envblock, strings);
3709 env_size += sizeof (cwd);
3710 env_size += strlen (cwdval) + 1;
3711 env_size += sizeof (exe);
3712 env_size += strlen (exeval) + 1;
3713 env_size += sizeof (pdb);
3714 env_size += strlen (pdbval) + 1;
3716 if (env_size % 4)
3717 padding2 = 4 - (env_size % 4);
3718 else
3719 padding2 = 0;
3721 env_size += padding2;
3723 *sym_byte_size += env_size;
3725 *syms = xmalloc (*sym_byte_size);
3726 ptr = *syms;
3728 /* Write S_OBJNAME */
3730 name = (struct objname *) ptr;
3731 bfd_putl16 (offsetof (struct objname, name)
3732 + sizeof (linker_fn) - sizeof (uint16_t), &name->size);
3733 bfd_putl16 (S_OBJNAME, &name->kind);
3734 bfd_putl32 (0, &name->signature);
3735 memcpy (name->name, linker_fn, sizeof (linker_fn));
3737 ptr += offsetof (struct objname, name) + sizeof (linker_fn);
3739 /* Write S_COMPILE3 */
3741 comp = (struct compile3 *) ptr;
3743 bfd_putl16 (offsetof (struct compile3, compiler) + sizeof (linker_name)
3744 + padding1 - sizeof (uint16_t), &comp->size);
3745 bfd_putl16 (S_COMPILE3, &comp->kind);
3746 bfd_putl32 (CV_CFL_LINK, &comp->flags);
3747 bfd_putl16 (target_processor (abfd), &comp->machine);
3748 bfd_putl16 (0, &comp->frontend_major);
3749 bfd_putl16 (0, &comp->frontend_minor);
3750 bfd_putl16 (0, &comp->frontend_build);
3751 bfd_putl16 (0, &comp->frontend_qfe);
3752 bfd_putl16 (0, &comp->backend_major);
3753 bfd_putl16 (0, &comp->backend_minor);
3754 bfd_putl16 (0, &comp->backend_build);
3755 bfd_putl16 (0, &comp->backend_qfe);
3756 memcpy (comp->compiler, linker_name, sizeof (linker_name));
3758 memset (comp->compiler + sizeof (linker_name), 0, padding1);
3760 ptr += offsetof (struct compile3, compiler) + sizeof (linker_name) + padding1;
3762 /* Write S_ENVBLOCK */
3764 env = (struct envblock *) ptr;
3766 bfd_putl16 (env_size - sizeof (uint16_t), &env->size);
3767 bfd_putl16 (S_ENVBLOCK, &env->kind);
3768 env->flags = 0;
3770 ptr += offsetof (struct envblock, strings);
3772 memcpy (ptr, cwd, sizeof (cwd));
3773 ptr += sizeof (cwd);
3774 memcpy (ptr, cwdval, strlen (cwdval) + 1);
3775 ptr += strlen (cwdval) + 1;
3777 memcpy (ptr, exe, sizeof (exe));
3778 ptr += sizeof (exe);
3779 memcpy (ptr, exeval, strlen (exeval) + 1);
3780 ptr += strlen (exeval) + 1;
3782 memcpy (ptr, pdb, sizeof (pdb));
3783 ptr += sizeof (pdb);
3784 memcpy (ptr, pdbval, strlen (pdbval) + 1);
3785 ptr += strlen (pdbval) + 1;
3787 /* Microsoft's LINK also includes "cmd", the command-line options passed
3788 to the linker, but unfortunately we don't have access to argc and argv
3789 at this stage. */
3791 memset (ptr, 0, padding2);
3793 free (pdbval);
3794 free (exeval);
3795 free (cwdval);
3797 return true;
3800 /* Populate the module stream, which consists of the transformed .debug$S
3801 data for each object file. */
3802 static bool
3803 populate_module_stream (bfd *stream, bfd *mod, uint32_t *sym_byte_size,
3804 struct string_table *strings,
3805 uint32_t *c13_info_size,
3806 struct mod_source_files *mod_source,
3807 bfd *abfd, struct types *types,
3808 struct types *ids, uint16_t mod_num,
3809 bfd *sym_rec_stream, struct globals *glob,
3810 const char *pdb_name)
3812 uint8_t int_buf[sizeof (uint32_t)];
3813 uint8_t *c13_info = NULL;
3814 uint8_t *syms = NULL;
3816 *sym_byte_size = 0;
3817 *c13_info_size = 0;
3819 if (!strcmp (bfd_get_filename (mod), "dll stuff"))
3821 if (!create_linker_symbols (mod, &syms, sym_byte_size, pdb_name))
3822 return false;
3824 else
3826 struct type_entry **map = NULL;
3827 uint32_t num_types = 0;
3829 /* Process .debug$T section. */
3831 for (asection *s = mod->sections; s; s = s->next)
3833 if (!strcmp (s->name, ".debug$T") && s->size >= sizeof (uint32_t))
3835 if (!handle_debugt_section (s, mod, types, ids, mod_num, strings,
3836 &map, &num_types))
3838 free (mod_source->files);
3839 return false;
3842 break;
3846 /* Process .debug$S section(s). */
3848 for (asection *s = mod->sections; s; s = s->next)
3850 if (!strcmp (s->name, ".debug$S") && s->size >= sizeof (uint32_t))
3852 if (!handle_debugs_section (s, mod, strings, &c13_info,
3853 c13_info_size, mod_source, abfd,
3854 &syms, sym_byte_size, map, num_types,
3855 sym_rec_stream, glob, mod_num))
3857 free (c13_info);
3858 free (syms);
3859 free (mod_source->files);
3860 free (map);
3861 return false;
3866 free (map);
3869 /* Write the signature. */
3871 bfd_putl32 (CV_SIGNATURE_C13, int_buf);
3873 if (bfd_bwrite (int_buf, sizeof (uint32_t), stream) != sizeof (uint32_t))
3875 free (c13_info);
3876 free (syms);
3877 return false;
3880 if (syms)
3882 if (bfd_bwrite (syms, *sym_byte_size, stream) != *sym_byte_size)
3884 free (c13_info);
3885 free (syms);
3886 return false;
3889 free (syms);
3892 if (c13_info)
3894 if (bfd_bwrite (c13_info, *c13_info_size, stream) != *c13_info_size)
3896 free (c13_info);
3897 return false;
3900 free (c13_info);
3903 /* Write the global refs size. */
3905 bfd_putl32 (0, int_buf);
3907 if (bfd_bwrite (int_buf, sizeof (uint32_t), stream) != sizeof (uint32_t))
3908 return false;
3910 return true;
3913 /* Create the module info substream within the DBI. */
3914 static bool
3915 create_module_info_substream (bfd *abfd, bfd *pdb, void **data,
3916 uint32_t *size, struct string_table *strings,
3917 struct source_files_info *source,
3918 struct types *types, struct types *ids,
3919 bfd *sym_rec_stream, struct globals *glob,
3920 const char *pdb_name)
3922 uint8_t *ptr;
3923 unsigned int mod_num;
3925 static const char linker_fn[] = "* Linker *";
3927 *size = 0;
3929 for (bfd *in = coff_data (abfd)->link_info->input_bfds; in;
3930 in = in->link.next)
3932 size_t len = sizeof (struct module_info);
3934 if (!strcmp (bfd_get_filename (in), "dll stuff"))
3936 len += sizeof (linker_fn); /* Object name. */
3937 len++; /* Empty module name. */
3939 else if (in->my_archive)
3941 char *name = lrealpath (bfd_get_filename (in));
3943 len += strlen (name) + 1; /* Object name. */
3945 free (name);
3947 name = lrealpath (bfd_get_filename (in->my_archive));
3949 len += strlen (name) + 1; /* Archive name. */
3951 free (name);
3953 else
3955 char *name = lrealpath (bfd_get_filename (in));
3956 size_t name_len = strlen (name) + 1;
3958 len += name_len; /* Object name. */
3959 len += name_len; /* And again as the archive name. */
3961 free (name);
3964 if (len % 4)
3965 len += 4 - (len % 4);
3967 *size += len;
3969 source->mod_count++;
3972 *data = xmalloc (*size);
3974 ptr = *data;
3976 source->mods = xmalloc (source->mod_count
3977 * sizeof (struct mod_source_files));
3978 memset (source->mods, 0,
3979 source->mod_count * sizeof (struct mod_source_files));
3981 mod_num = 0;
3983 for (bfd *in = coff_data (abfd)->link_info->input_bfds; in;
3984 in = in->link.next)
3986 struct module_info *mod = (struct module_info *) ptr;
3987 uint16_t stream_num;
3988 bfd *stream;
3989 uint32_t sym_byte_size, c13_info_size;
3990 uint8_t *start = ptr;
3992 stream = add_stream (pdb, NULL, &stream_num);
3994 if (!stream)
3996 for (unsigned int i = 0; i < source->mod_count; i++)
3998 free (source->mods[i].files);
4001 free (source->mods);
4002 free (*data);
4003 return false;
4006 if (!populate_module_stream (stream, in, &sym_byte_size,
4007 strings, &c13_info_size,
4008 &source->mods[mod_num], abfd,
4009 types, ids, mod_num,
4010 sym_rec_stream, glob, pdb_name))
4012 for (unsigned int i = 0; i < source->mod_count; i++)
4014 free (source->mods[i].files);
4017 free (source->mods);
4018 free (*data);
4019 return false;
4022 bfd_putl32 (0, &mod->unused1);
4024 /* These are dummy values - MSVC copies the first section contribution
4025 entry here, but doesn't seem to use it for anything. */
4026 bfd_putl16 (0xffff, &mod->sc.section);
4027 bfd_putl16 (0, &mod->sc.padding1);
4028 bfd_putl32 (0, &mod->sc.offset);
4029 bfd_putl32 (0xffffffff, &mod->sc.size);
4030 bfd_putl32 (0, &mod->sc.characteristics);
4031 bfd_putl16 (0xffff, &mod->sc.module_index);
4032 bfd_putl16 (0, &mod->sc.padding2);
4033 bfd_putl32 (0, &mod->sc.data_crc);
4034 bfd_putl32 (0, &mod->sc.reloc_crc);
4036 bfd_putl16 (0, &mod->flags);
4037 bfd_putl16 (stream_num, &mod->module_sym_stream);
4038 bfd_putl32 (sizeof (uint32_t) + sym_byte_size, &mod->sym_byte_size);
4039 bfd_putl32 (0, &mod->c11_byte_size);
4040 bfd_putl32 (c13_info_size, &mod->c13_byte_size);
4041 bfd_putl16 (0, &mod->source_file_count);
4042 bfd_putl16 (0, &mod->padding);
4043 bfd_putl32 (0, &mod->unused2);
4044 bfd_putl32 (0, &mod->source_file_name_index);
4045 bfd_putl32 (0, &mod->pdb_file_path_name_index);
4047 ptr += sizeof (struct module_info);
4049 if (!strcmp (bfd_get_filename (in), "dll stuff"))
4051 /* Object name. */
4052 memcpy (ptr, linker_fn, sizeof (linker_fn));
4053 ptr += sizeof (linker_fn);
4055 /* Empty module name. */
4056 *ptr = 0;
4057 ptr++;
4059 else if (in->my_archive)
4061 char *name = lrealpath (bfd_get_filename (in));
4062 size_t name_len = strlen (name) + 1;
4064 /* Object name. */
4065 memcpy (ptr, name, name_len);
4066 ptr += name_len;
4068 free (name);
4070 name = lrealpath (bfd_get_filename (in->my_archive));
4071 name_len = strlen (name) + 1;
4073 /* Archive name. */
4074 memcpy (ptr, name, name_len);
4075 ptr += name_len;
4077 free (name);
4079 else
4081 char *name = lrealpath (bfd_get_filename (in));
4082 size_t name_len = strlen (name) + 1;
4084 /* Object name. */
4085 memcpy (ptr, name, name_len);
4086 ptr += name_len;
4088 /* Object name again as archive name. */
4089 memcpy (ptr, name, name_len);
4090 ptr += name_len;
4092 free (name);
4095 /* Pad to next four-byte boundary. */
4097 if ((ptr - start) % 4)
4099 memset (ptr, 0, 4 - ((ptr - start) % 4));
4100 ptr += 4 - ((ptr - start) % 4);
4103 mod_num++;
4106 return true;
4109 /* Return the index of a given output section. */
4110 static uint16_t
4111 find_section_number (bfd *abfd, asection *sect)
4113 uint16_t i = 1;
4115 for (asection *s = abfd->sections; s; s = s->next)
4117 if (s == sect)
4118 return i;
4120 /* Empty sections aren't output. */
4121 if (s->size != 0)
4122 i++;
4125 return 0;
4128 /* Used as parameter to qsort, to sort section contributions by section and
4129 offset. */
4130 static int
4131 section_contribs_compare (const void *p1, const void *p2)
4133 const struct in_sc *sc1 = p1;
4134 const struct in_sc *sc2 = p2;
4136 if (sc1->sect_num < sc2->sect_num)
4137 return -1;
4138 if (sc1->sect_num > sc2->sect_num)
4139 return 1;
4141 if (sc1->s->output_offset < sc2->s->output_offset)
4142 return -1;
4143 if (sc1->s->output_offset > sc2->s->output_offset)
4144 return 1;
4146 return 0;
4149 /* Create the substream which maps addresses in the image file to locations
4150 in the original object files. */
4151 static bool
4152 create_section_contrib_substream (bfd *abfd, void **data, uint32_t *size)
4154 unsigned int num_sc = 0;
4155 struct section_contribution *sc;
4156 uint16_t mod_index;
4157 char *sect_flags;
4158 file_ptr offset;
4159 struct in_sc *sc_in, *sc2;
4160 uint32_t *ptr;
4162 for (bfd *in = coff_data (abfd)->link_info->input_bfds; in;
4163 in = in->link.next)
4165 for (asection *s = in->sections; s; s = s->next)
4167 if (s->size == 0 || discarded_section (s))
4168 continue;
4170 num_sc++;
4174 *size = sizeof (uint32_t) + (num_sc * sizeof (struct section_contribution));
4175 *data = xmalloc (*size);
4177 bfd_putl32 (SECTION_CONTRIB_VERSION_60, *data);
4179 /* Read characteristics of outputted sections. */
4181 sect_flags = xmalloc (sizeof (uint32_t) * abfd->section_count);
4183 offset = bfd_coff_filhsz (abfd) + bfd_coff_aoutsz (abfd);
4184 offset += offsetof (struct external_scnhdr, s_flags);
4186 for (unsigned int i = 0; i < abfd->section_count; i++)
4188 bfd_seek (abfd, offset, SEEK_SET);
4190 if (bfd_bread (sect_flags + (i * sizeof (uint32_t)), sizeof (uint32_t),
4191 abfd) != sizeof (uint32_t))
4193 free (*data);
4194 free (sect_flags);
4195 return false;
4198 offset += sizeof (struct external_scnhdr);
4201 /* Microsoft's DIA expects section contributions to be sorted by section
4202 number and offset, otherwise it will be unable to resolve line numbers. */
4204 sc_in = xmalloc (num_sc * sizeof (* sc_in));
4205 sc2 = sc_in;
4207 mod_index = 0;
4208 for (bfd *in = coff_data (abfd)->link_info->input_bfds; in;
4209 in = in->link.next)
4211 for (asection *s = in->sections; s; s = s->next)
4213 if (s->size == 0 || discarded_section (s))
4214 continue;
4216 sc2->s = s;
4217 sc2->sect_num = find_section_number (abfd, s->output_section);
4218 sc2->mod_index = mod_index;
4220 sc2++;
4223 mod_index++;
4226 qsort (sc_in, num_sc, sizeof (* sc_in), section_contribs_compare);
4228 ptr = *data;
4229 sc = (struct section_contribution *) (ptr + 1); /* Skip the version word. */
4231 for (unsigned int i = 0; i < num_sc; i++)
4233 memcpy (&sc->characteristics,
4234 sect_flags + ((sc_in[i].sect_num - 1) * sizeof (uint32_t)),
4235 sizeof (uint32_t));
4237 bfd_putl16 (sc_in[i].sect_num, &sc->section);
4238 bfd_putl16 (0, &sc->padding1);
4239 bfd_putl32 (sc_in[i].s->output_offset, &sc->offset);
4240 bfd_putl32 (sc_in[i].s->size, &sc->size);
4241 bfd_putl16 (sc_in[i].mod_index, &sc->module_index);
4242 bfd_putl16 (0, &sc->padding2);
4243 bfd_putl32 (0, &sc->data_crc);
4244 bfd_putl32 (0, &sc->reloc_crc);
4246 sc++;
4249 free (sc_in);
4250 free (sect_flags);
4252 return true;
4255 /* The source info substream lives within the DBI stream, and lists the
4256 source files for each object file (i.e. it's derived from the
4257 DEBUG_S_FILECHKSMS parts of the .debug$S sections). This is a bit
4258 superfluous, as the filenames are also available in the C13 parts of
4259 the module streams, but MSVC relies on it to work properly. */
4260 static void
4261 create_source_info_substream (void **data, uint32_t *size,
4262 struct source_files_info *source)
4264 uint16_t dedupe_source_files_count = 0;
4265 uint16_t source_files_count = 0;
4266 uint32_t strings_len = 0;
4267 uint8_t *ptr;
4269 /* Loop through the source files, marking unique filenames. The pointers
4270 here are for entries in the main string table, and so have already
4271 been deduplicated. */
4273 for (uint16_t i = 0; i < source->mod_count; i++)
4275 for (uint16_t j = 0; j < source->mods[i].files_count; j++)
4277 if (source->mods[i].files[j])
4279 if (source->mods[i].files[j]->source_file_offset == 0xffffffff)
4281 source->mods[i].files[j]->source_file_offset = strings_len;
4282 strings_len += source->mods[i].files[j]->len + 1;
4283 dedupe_source_files_count++;
4286 source_files_count++;
4291 *size = sizeof (uint16_t) + sizeof (uint16_t);
4292 *size += (sizeof (uint16_t) + sizeof (uint16_t)) * source->mod_count;
4293 *size += sizeof (uint32_t) * source_files_count;
4294 *size += strings_len;
4296 *data = xmalloc (*size);
4298 ptr = (uint8_t *) *data;
4300 /* Write header (module count and source file count). */
4302 bfd_putl16 (source->mod_count, ptr);
4303 ptr += sizeof (uint16_t);
4305 bfd_putl16 (dedupe_source_files_count, ptr);
4306 ptr += sizeof (uint16_t);
4308 /* Write "ModIndices". As the LLVM documentation puts it, "this array is
4309 present, but does not appear to be useful". */
4311 for (uint16_t i = 0; i < source->mod_count; i++)
4313 bfd_putl16 (i, ptr);
4314 ptr += sizeof (uint16_t);
4317 /* Write source file count for each module. */
4319 for (uint16_t i = 0; i < source->mod_count; i++)
4321 bfd_putl16 (source->mods[i].files_count, ptr);
4322 ptr += sizeof (uint16_t);
4325 /* For each module, write the offsets within the string table
4326 for each source file. */
4328 for (uint16_t i = 0; i < source->mod_count; i++)
4330 for (uint16_t j = 0; j < source->mods[i].files_count; j++)
4332 if (source->mods[i].files[j])
4334 bfd_putl32 (source->mods[i].files[j]->source_file_offset, ptr);
4335 ptr += sizeof (uint32_t);
4340 /* Write the string table. We set source_file_offset to a dummy value for
4341 each entry we write, so we don't write duplicate filenames. */
4343 for (uint16_t i = 0; i < source->mod_count; i++)
4345 for (uint16_t j = 0; j < source->mods[i].files_count; j++)
4347 if (source->mods[i].files[j]
4348 && source->mods[i].files[j]->source_file_offset != 0xffffffff)
4350 memcpy (ptr, source->mods[i].files[j]->s,
4351 source->mods[i].files[j]->len);
4352 ptr += source->mods[i].files[j]->len;
4354 *ptr = 0;
4355 ptr++;
4357 source->mods[i].files[j]->source_file_offset = 0xffffffff;
4363 /* Used as parameter to qsort, to sort globals by hash. */
4364 static int
4365 global_compare_hash (const void *s1, const void *s2)
4367 const struct global *g1 = *(const struct global **) s1;
4368 const struct global *g2 = *(const struct global **) s2;
4370 if (g1->hash < g2->hash)
4371 return -1;
4372 if (g1->hash > g2->hash)
4373 return 1;
4375 return 0;
4378 /* Create the globals stream, which contains the unmangled symbol names. */
4379 static bool
4380 create_globals_stream (bfd *pdb, struct globals *glob, uint16_t *stream_num)
4382 bfd *stream;
4383 struct globals_hash_header h;
4384 uint32_t buckets_size, filled_buckets = 0;
4385 struct global **sorted = NULL;
4386 bool ret = false;
4387 struct global *buckets[NUM_GLOBALS_HASH_BUCKETS];
4388 char int_buf[sizeof (uint32_t)];
4390 stream = add_stream (pdb, NULL, stream_num);
4391 if (!stream)
4392 return false;
4394 memset (buckets, 0, sizeof (buckets));
4396 if (glob->num_entries > 0)
4398 struct global *g;
4400 /* Create an array of pointers, sorted by hash value. */
4402 sorted = xmalloc (sizeof (struct global *) * glob->num_entries);
4404 g = glob->first;
4405 for (unsigned int i = 0; i < glob->num_entries; i++)
4407 sorted[i] = g;
4408 g = g->next;
4411 qsort (sorted, glob->num_entries, sizeof (struct global *),
4412 global_compare_hash);
4414 /* Populate the buckets. */
4416 for (unsigned int i = 0; i < glob->num_entries; i++)
4418 if (!buckets[sorted[i]->hash])
4420 buckets[sorted[i]->hash] = sorted[i];
4421 filled_buckets++;
4424 sorted[i]->index = i;
4428 buckets_size = NUM_GLOBALS_HASH_BUCKETS / 8;
4429 buckets_size += sizeof (uint32_t);
4430 buckets_size += filled_buckets * sizeof (uint32_t);
4432 bfd_putl32 (GLOBALS_HASH_SIGNATURE, &h.signature);
4433 bfd_putl32 (GLOBALS_HASH_VERSION_70, &h.version);
4434 bfd_putl32 (glob->num_entries * sizeof (struct hash_record),
4435 &h.entries_size);
4436 bfd_putl32 (buckets_size, &h.buckets_size);
4438 if (bfd_bwrite (&h, sizeof (h), stream) != sizeof (h))
4439 return false;
4441 /* Write hash entries, sorted by hash. */
4443 for (unsigned int i = 0; i < glob->num_entries; i++)
4445 struct hash_record hr;
4447 bfd_putl32 (sorted[i]->offset + 1, &hr.offset);
4448 bfd_putl32 (sorted[i]->refcount, &hr.reference);
4450 if (bfd_bwrite (&hr, sizeof (hr), stream) != sizeof (hr))
4451 goto end;
4454 /* Write the bitmap for filled and unfilled buckets. */
4456 for (unsigned int i = 0; i < NUM_GLOBALS_HASH_BUCKETS; i += 8)
4458 uint8_t v = 0;
4460 for (unsigned int j = 0; j < 8; j++)
4462 if (buckets[i + j])
4463 v |= 1 << j;
4466 if (bfd_bwrite (&v, sizeof (v), stream) != sizeof (v))
4467 goto end;
4470 /* Add a 4-byte gap. */
4472 bfd_putl32 (0, int_buf);
4474 if (bfd_bwrite (int_buf, sizeof (uint32_t), stream) != sizeof (uint32_t))
4475 goto end;
4477 /* Write the bucket offsets. */
4479 for (unsigned int i = 0; i < NUM_GLOBALS_HASH_BUCKETS; i++)
4481 if (buckets[i])
4483 /* 0xc is size of internal hash_record structure in
4484 Microsoft's parser. */
4485 bfd_putl32 (buckets[i]->index * 0xc, int_buf);
4487 if (bfd_bwrite (int_buf, sizeof (uint32_t), stream) !=
4488 sizeof (uint32_t))
4489 goto end;
4493 ret = true;
4495 end:
4496 free (sorted);
4498 return ret;
4501 /* Hash an entry in the globals list. */
4502 static hashval_t
4503 hash_global_entry (const void *p)
4505 const struct global *g = (const struct global *) p;
4506 uint16_t len = bfd_getl16 (g->data);
4508 return iterative_hash (g->data, len, 0);
4511 /* Compare an entry in the globals list with a symbol. */
4512 static int
4513 eq_global_entry (const void *a, const void *b)
4515 const struct global *g = (const struct global *) a;
4516 uint16_t len1, len2;
4518 len1 = bfd_getl16 (g->data) + sizeof (uint16_t);
4519 len2 = bfd_getl16 (b) + sizeof (uint16_t);
4521 if (len1 != len2)
4522 return 0;
4524 return !memcmp (g->data, b, len1);
4527 /* Stream 4 is the debug information (DBI) stream. */
4528 static bool
4529 populate_dbi_stream (bfd *stream, bfd *abfd, bfd *pdb,
4530 uint16_t section_header_stream_num,
4531 uint16_t sym_rec_stream_num,
4532 uint16_t publics_stream_num,
4533 struct string_table *strings,
4534 struct types *types,
4535 struct types *ids,
4536 bfd *sym_rec_stream, const char *pdb_name)
4538 struct pdb_dbi_stream_header h;
4539 struct optional_dbg_header opt;
4540 void *mod_info, *sc, *source_info;
4541 uint32_t mod_info_size, sc_size, source_info_size;
4542 struct source_files_info source;
4543 struct globals glob;
4544 uint16_t globals_stream_num;
4546 source.mod_count = 0;
4547 source.mods = NULL;
4549 glob.num_entries = 0;
4550 glob.first = NULL;
4551 glob.last = NULL;
4553 glob.hashmap = htab_create_alloc (0, hash_global_entry,
4554 eq_global_entry, free, xcalloc, free);
4556 if (!create_module_info_substream (abfd, pdb, &mod_info, &mod_info_size,
4557 strings, &source, types, ids,
4558 sym_rec_stream, &glob, pdb_name))
4560 htab_delete (glob.hashmap);
4561 return false;
4564 if (!create_globals_stream (pdb, &glob, &globals_stream_num))
4566 htab_delete (glob.hashmap);
4568 for (unsigned int i = 0; i < source.mod_count; i++)
4570 free (source.mods[i].files);
4572 free (source.mods);
4574 free (mod_info);
4575 return false;
4578 htab_delete (glob.hashmap);
4580 if (!create_section_contrib_substream (abfd, &sc, &sc_size))
4582 for (unsigned int i = 0; i < source.mod_count; i++)
4584 free (source.mods[i].files);
4586 free (source.mods);
4588 free (mod_info);
4589 return false;
4592 create_source_info_substream (&source_info, &source_info_size, &source);
4594 for (unsigned int i = 0; i < source.mod_count; i++)
4596 free (source.mods[i].files);
4598 free (source.mods);
4600 bfd_putl32 (0xffffffff, &h.version_signature);
4601 bfd_putl32 (DBI_STREAM_VERSION_70, &h.version_header);
4602 bfd_putl32 (1, &h.age);
4603 bfd_putl16 (globals_stream_num, &h.global_stream_index);
4604 bfd_putl16 (0x8e1d, &h.build_number); // MSVC 14.29
4605 bfd_putl16 (publics_stream_num, &h.public_stream_index);
4606 bfd_putl16 (0, &h.pdb_dll_version);
4607 bfd_putl16 (sym_rec_stream_num, &h.sym_record_stream);
4608 bfd_putl16 (0, &h.pdb_dll_rbld);
4609 bfd_putl32 (mod_info_size, &h.mod_info_size);
4610 bfd_putl32 (sc_size, &h.section_contribution_size);
4611 bfd_putl32 (0, &h.section_map_size);
4612 bfd_putl32 (source_info_size, &h.source_info_size);
4613 bfd_putl32 (0, &h.type_server_map_size);
4614 bfd_putl32 (0, &h.mfc_type_server_index);
4615 bfd_putl32 (sizeof (opt), &h.optional_dbg_header_size);
4616 bfd_putl32 (0, &h.ec_substream_size);
4617 bfd_putl16 (0, &h.flags);
4618 bfd_putl16 (get_arch_number (abfd), &h.machine);
4619 bfd_putl32 (0, &h.padding);
4621 if (bfd_bwrite (&h, sizeof (h), stream) != sizeof (h))
4623 free (source_info);
4624 free (sc);
4625 free (mod_info);
4626 return false;
4629 if (bfd_bwrite (mod_info, mod_info_size, stream) != mod_info_size)
4631 free (source_info);
4632 free (sc);
4633 free (mod_info);
4634 return false;
4637 free (mod_info);
4639 if (bfd_bwrite (sc, sc_size, stream) != sc_size)
4641 free (source_info);
4642 free (sc);
4643 return false;
4646 free (sc);
4648 if (bfd_bwrite (source_info, source_info_size, stream) != source_info_size)
4650 free (source_info);
4651 return false;
4654 free (source_info);
4656 bfd_putl16 (0xffff, &opt.fpo_stream);
4657 bfd_putl16 (0xffff, &opt.exception_stream);
4658 bfd_putl16 (0xffff, &opt.fixup_stream);
4659 bfd_putl16 (0xffff, &opt.omap_to_src_stream);
4660 bfd_putl16 (0xffff, &opt.omap_from_src_stream);
4661 bfd_putl16 (section_header_stream_num, &opt.section_header_stream);
4662 bfd_putl16 (0xffff, &opt.token_map_stream);
4663 bfd_putl16 (0xffff, &opt.xdata_stream);
4664 bfd_putl16 (0xffff, &opt.pdata_stream);
4665 bfd_putl16 (0xffff, &opt.new_fpo_stream);
4666 bfd_putl16 (0xffff, &opt.orig_section_header_stream);
4668 if (bfd_bwrite (&opt, sizeof (opt), stream) != sizeof (opt))
4669 return false;
4671 return true;
4674 /* Used as parameter to qsort, to sort publics by hash. */
4675 static int
4676 public_compare_hash (const void *s1, const void *s2)
4678 const struct public *p1 = *(const struct public **) s1;
4679 const struct public *p2 = *(const struct public **) s2;
4681 if (p1->hash < p2->hash)
4682 return -1;
4683 if (p1->hash > p2->hash)
4684 return 1;
4686 return 0;
4689 /* Used as parameter to qsort, to sort publics by address. */
4690 static int
4691 public_compare_addr (const void *s1, const void *s2)
4693 const struct public *p1 = *(const struct public **) s1;
4694 const struct public *p2 = *(const struct public **) s2;
4696 if (p1->section < p2->section)
4697 return -1;
4698 if (p1->section > p2->section)
4699 return 1;
4701 if (p1->address < p2->address)
4702 return -1;
4703 if (p1->address > p2->address)
4704 return 1;
4706 return 0;
4709 /* The publics stream is a hash map of S_PUB32 records, which are stored
4710 in the symbol record stream. Each S_PUB32 entry represents a symbol
4711 from the point of view of the linker: a section index, an offset within
4712 the section, and a mangled name. Compare with S_GDATA32 and S_GPROC32,
4713 which are the same thing but generated by the compiler. */
4714 static bool
4715 populate_publics_stream (bfd *stream, bfd *abfd, bfd *sym_rec_stream)
4717 struct publics_header header;
4718 struct globals_hash_header hash_header;
4719 const unsigned int num_buckets = 4096;
4720 unsigned int num_entries = 0, filled_buckets = 0;
4721 unsigned int buckets_size, sym_hash_size;
4722 char int_buf[sizeof (uint32_t)];
4723 struct public *publics_head = NULL, *publics_tail = NULL;
4724 struct public **buckets;
4725 struct public **sorted = NULL;
4726 bool ret = false;
4728 buckets = xmalloc (sizeof (struct public *) * num_buckets);
4729 memset (buckets, 0, sizeof (struct public *) * num_buckets);
4731 /* Loop through the global symbols in our input files, and write S_PUB32
4732 records in the symbol record stream for those that make it into the
4733 final image. */
4734 for (bfd *in = coff_data (abfd)->link_info->input_bfds; in;
4735 in = in->link.next)
4737 if (!in->outsymbols)
4738 continue;
4740 for (unsigned int i = 0; i < in->symcount; i++)
4742 struct bfd_symbol *sym = in->outsymbols[i];
4744 if (sym->flags & BSF_GLOBAL)
4746 struct pubsym ps;
4747 uint16_t record_length;
4748 const char *name = sym->name;
4749 size_t name_len = strlen (name);
4750 struct public *p = xmalloc (sizeof (struct public));
4751 unsigned int padding = 0;
4752 uint16_t section;
4753 uint32_t flags = 0;
4755 section =
4756 find_section_number (abfd, sym->section->output_section);
4758 if (section == 0)
4759 continue;
4761 p->next = NULL;
4762 p->offset = bfd_tell (sym_rec_stream);
4763 p->hash = calc_hash (name, name_len) % num_buckets;
4764 p->section = section;
4765 p->address = sym->section->output_offset + sym->value;
4767 record_length = sizeof (struct pubsym) + name_len + 1;
4769 if (record_length % 4)
4770 padding = 4 - (record_length % 4);
4772 /* Assume that all global symbols in executable sections
4773 are functions. */
4774 if (sym->section->flags & SEC_CODE)
4775 flags = PUBSYM_FUNCTION;
4777 bfd_putl16 (record_length + padding - sizeof (uint16_t),
4778 &ps.record_length);
4779 bfd_putl16 (S_PUB32, &ps.record_type);
4780 bfd_putl32 (flags, &ps.flags);
4781 bfd_putl32 (p->address, &ps.offset);
4782 bfd_putl16 (p->section, &ps.section);
4784 if (bfd_bwrite (&ps, sizeof (struct pubsym), sym_rec_stream) !=
4785 sizeof (struct pubsym))
4786 goto end;
4788 if (bfd_bwrite (name, name_len + 1, sym_rec_stream) !=
4789 name_len + 1)
4790 goto end;
4792 for (unsigned int j = 0; j < padding; j++)
4794 uint8_t b = 0;
4796 if (bfd_bwrite (&b, sizeof (uint8_t), sym_rec_stream) !=
4797 sizeof (uint8_t))
4798 goto end;
4801 if (!publics_head)
4802 publics_head = p;
4803 else
4804 publics_tail->next = p;
4806 publics_tail = p;
4807 num_entries++;
4813 if (num_entries > 0)
4815 /* Create an array of pointers, sorted by hash value. */
4817 sorted = xmalloc (sizeof (struct public *) * num_entries);
4819 struct public *p = publics_head;
4820 for (unsigned int i = 0; i < num_entries; i++)
4822 sorted[i] = p;
4823 p = p->next;
4826 qsort (sorted, num_entries, sizeof (struct public *),
4827 public_compare_hash);
4829 /* Populate the buckets. */
4831 for (unsigned int i = 0; i < num_entries; i++)
4833 if (!buckets[sorted[i]->hash])
4835 buckets[sorted[i]->hash] = sorted[i];
4836 filled_buckets++;
4839 sorted[i]->index = i;
4843 buckets_size = num_buckets / 8;
4844 buckets_size += sizeof (uint32_t);
4845 buckets_size += filled_buckets * sizeof (uint32_t);
4847 sym_hash_size = sizeof (hash_header);
4848 sym_hash_size += num_entries * sizeof (struct hash_record);
4849 sym_hash_size += buckets_size;
4851 /* Output the publics header. */
4853 bfd_putl32 (sym_hash_size, &header.sym_hash_size);
4854 bfd_putl32 (num_entries * sizeof (uint32_t), &header.addr_map_size);
4855 bfd_putl32 (0, &header.num_thunks);
4856 bfd_putl32 (0, &header.thunks_size);
4857 bfd_putl32 (0, &header.thunk_table);
4858 bfd_putl32 (0, &header.thunk_table_offset);
4859 bfd_putl32 (0, &header.num_sects);
4861 if (bfd_bwrite (&header, sizeof (header), stream) != sizeof (header))
4862 goto end;
4864 /* Output the global hash header. */
4866 bfd_putl32 (GLOBALS_HASH_SIGNATURE, &hash_header.signature);
4867 bfd_putl32 (GLOBALS_HASH_VERSION_70, &hash_header.version);
4868 bfd_putl32 (num_entries * sizeof (struct hash_record),
4869 &hash_header.entries_size);
4870 bfd_putl32 (buckets_size, &hash_header.buckets_size);
4872 if (bfd_bwrite (&hash_header, sizeof (hash_header), stream) !=
4873 sizeof (hash_header))
4874 goto end;
4876 /* Write the entries in hash order. */
4878 for (unsigned int i = 0; i < num_entries; i++)
4880 struct hash_record hr;
4882 bfd_putl32 (sorted[i]->offset + 1, &hr.offset);
4883 bfd_putl32 (1, &hr.reference);
4885 if (bfd_bwrite (&hr, sizeof (hr), stream) != sizeof (hr))
4886 goto end;
4889 /* Write the bitmap for filled and unfilled buckets. */
4891 for (unsigned int i = 0; i < num_buckets; i += 8)
4893 uint8_t v = 0;
4895 for (unsigned int j = 0; j < 8; j++)
4897 if (buckets[i + j])
4898 v |= 1 << j;
4901 if (bfd_bwrite (&v, sizeof (v), stream) != sizeof (v))
4902 goto end;
4905 /* Add a 4-byte gap. */
4907 bfd_putl32 (0, int_buf);
4909 if (bfd_bwrite (int_buf, sizeof (uint32_t), stream) != sizeof (uint32_t))
4910 goto end;
4912 /* Write the bucket offsets. */
4914 for (unsigned int i = 0; i < num_buckets; i++)
4916 if (buckets[i])
4918 /* 0xc is size of internal hash_record structure in
4919 Microsoft's parser. */
4920 bfd_putl32 (buckets[i]->index * 0xc, int_buf);
4922 if (bfd_bwrite (int_buf, sizeof (uint32_t), stream) !=
4923 sizeof (uint32_t))
4924 goto end;
4928 /* Write the address map: offsets into the symbol record stream of
4929 S_PUB32 records, ordered by address. */
4931 if (num_entries > 0)
4933 qsort (sorted, num_entries, sizeof (struct public *),
4934 public_compare_addr);
4936 for (unsigned int i = 0; i < num_entries; i++)
4938 bfd_putl32 (sorted[i]->offset, int_buf);
4940 if (bfd_bwrite (int_buf, sizeof (uint32_t), stream) !=
4941 sizeof (uint32_t))
4942 goto end;
4946 ret = true;
4948 end:
4949 free (buckets);
4951 while (publics_head)
4953 struct public *p = publics_head->next;
4955 free (publics_head);
4956 publics_head = p;
4959 free (sorted);
4961 return ret;
4964 /* The section header stream contains a copy of the section headers
4965 from the PE file, in the same format. */
4966 static bool
4967 create_section_header_stream (bfd *pdb, bfd *abfd, uint16_t *num)
4969 bfd *stream;
4970 unsigned int section_count;
4971 file_ptr scn_base;
4972 size_t len;
4973 char *buf;
4975 stream = add_stream (pdb, NULL, num);
4976 if (!stream)
4977 return false;
4979 section_count = abfd->section_count;
4981 /* Empty sections aren't output. */
4982 for (asection *sect = abfd->sections; sect; sect = sect->next)
4984 if (sect->size == 0)
4985 section_count--;
4988 if (section_count == 0)
4989 return true;
4991 /* Copy section table from output - it's already been written at this
4992 point. */
4994 scn_base = bfd_coff_filhsz (abfd) + bfd_coff_aoutsz (abfd);
4996 bfd_seek (abfd, scn_base, SEEK_SET);
4998 len = section_count * sizeof (struct external_scnhdr);
4999 buf = xmalloc (len);
5001 if (bfd_bread (buf, len, abfd) != len)
5003 free (buf);
5004 return false;
5007 if (bfd_bwrite (buf, len, stream) != len)
5009 free (buf);
5010 return false;
5013 free (buf);
5015 return true;
5018 /* Populate the "/names" named stream, which contains the string table. */
5019 static bool
5020 populate_names_stream (bfd *stream, struct string_table *strings)
5022 char int_buf[sizeof (uint32_t)];
5023 struct string_table_header h;
5024 uint32_t num_strings = 0, num_buckets;
5025 struct string **buckets;
5027 bfd_putl32 (STRING_TABLE_SIGNATURE, &h.signature);
5028 bfd_putl32 (STRING_TABLE_VERSION, &h.version);
5030 if (bfd_bwrite (&h, sizeof (h), stream) != sizeof (h))
5031 return false;
5033 bfd_putl32 (strings->strings_len, int_buf);
5035 if (bfd_bwrite (int_buf, sizeof (uint32_t), stream) != sizeof (uint32_t))
5036 return false;
5038 int_buf[0] = 0;
5040 if (bfd_bwrite (int_buf, 1, stream) != 1)
5041 return false;
5043 for (struct string *s = strings->strings_head; s; s = s->next)
5045 if (bfd_bwrite (s->s, s->len, stream) != s->len)
5046 return false;
5048 if (bfd_bwrite (int_buf, 1, stream) != 1)
5049 return false;
5051 num_strings++;
5054 num_buckets = num_strings * 2;
5056 buckets = xmalloc (sizeof (struct string *) * num_buckets);
5057 memset (buckets, 0, sizeof (struct string *) * num_buckets);
5059 for (struct string *s = strings->strings_head; s; s = s->next)
5061 uint32_t bucket_num = s->hash % num_buckets;
5063 while (buckets[bucket_num])
5065 bucket_num++;
5067 if (bucket_num == num_buckets)
5068 bucket_num = 0;
5071 buckets[bucket_num] = s;
5074 bfd_putl32 (num_buckets, int_buf);
5076 if (bfd_bwrite (int_buf, sizeof (uint32_t), stream) != sizeof (uint32_t))
5078 free (buckets);
5079 return false;
5082 for (unsigned int i = 0; i < num_buckets; i++)
5084 if (buckets[i])
5085 bfd_putl32 (buckets[i]->offset, int_buf);
5086 else
5087 bfd_putl32 (0, int_buf);
5089 if (bfd_bwrite (int_buf, sizeof (uint32_t), stream) !=
5090 sizeof (uint32_t))
5092 free (buckets);
5093 return false;
5097 free (buckets);
5099 bfd_putl32 (num_strings, int_buf);
5101 if (bfd_bwrite (int_buf, sizeof (uint32_t), stream) != sizeof (uint32_t))
5102 return false;
5104 return true;
5107 /* Calculate the hash of a type_entry. */
5108 static hashval_t
5109 hash_type_entry (const void *p)
5111 const struct type_entry *e = (const struct type_entry *) p;
5112 uint16_t size = bfd_getl16 (e->data) + sizeof (uint16_t);
5114 return iterative_hash (e->data, size, 0);
5117 /* Compare a type_entry with a type. */
5118 static int
5119 eq_type_entry (const void *a, const void *b)
5121 const struct type_entry *e = (const struct type_entry *) a;
5122 uint16_t size_a = bfd_getl16 (e->data);
5123 uint16_t size_b = bfd_getl16 (b);
5125 if (size_a != size_b)
5126 return 0;
5128 return memcmp (e->data + sizeof (uint16_t),
5129 (const uint8_t *) b + sizeof (uint16_t), size_a) == 0;
5132 /* Create a PDB debugging file for the PE image file abfd with the build ID
5133 guid, stored at pdb_name. */
5134 bool
5135 create_pdb_file (bfd *abfd, const char *pdb_name, const unsigned char *guid)
5137 bfd *pdb;
5138 bool ret = false;
5139 bfd *info_stream, *dbi_stream, *names_stream, *sym_rec_stream,
5140 *publics_stream, *tpi_stream, *ipi_stream;
5141 uint16_t section_header_stream_num, sym_rec_stream_num, publics_stream_num;
5142 struct string_table strings;
5143 struct types types, ids;
5145 pdb = bfd_openw (pdb_name, "pdb");
5146 if (!pdb)
5148 einfo (_("%P: warning: cannot create PDB file: %E\n"));
5149 return false;
5152 strings.strings_head = NULL;
5153 strings.strings_tail = NULL;
5154 strings.strings_len = 1;
5155 strings.hashmap = htab_create_alloc (0, hash_string_table_entry,
5156 eq_string_table_entry, free,
5157 xcalloc, free);
5159 bfd_set_format (pdb, bfd_archive);
5161 if (!create_old_directory_stream (pdb))
5163 einfo (_("%P: warning: cannot create old directory stream "
5164 "in PDB file: %E\n"));
5165 goto end;
5168 info_stream = add_stream (pdb, NULL, NULL);
5170 if (!info_stream)
5172 einfo (_("%P: warning: cannot create info stream "
5173 "in PDB file: %E\n"));
5174 goto end;
5177 tpi_stream = add_stream (pdb, NULL, NULL);
5179 if (!tpi_stream)
5181 einfo (_("%P: warning: cannot create TPI stream "
5182 "in PDB file: %E\n"));
5183 goto end;
5186 dbi_stream = add_stream (pdb, NULL, NULL);
5188 if (!dbi_stream)
5190 einfo (_("%P: warning: cannot create DBI stream "
5191 "in PDB file: %E\n"));
5192 goto end;
5195 ipi_stream = add_stream (pdb, NULL, NULL);
5197 if (!ipi_stream)
5199 einfo (_("%P: warning: cannot create IPI stream "
5200 "in PDB file: %E\n"));
5201 goto end;
5204 names_stream = add_stream (pdb, "/names", NULL);
5206 if (!names_stream)
5208 einfo (_("%P: warning: cannot create /names stream "
5209 "in PDB file: %E\n"));
5210 goto end;
5213 sym_rec_stream = add_stream (pdb, NULL, &sym_rec_stream_num);
5215 if (!sym_rec_stream)
5217 einfo (_("%P: warning: cannot create symbol record stream "
5218 "in PDB file: %E\n"));
5219 goto end;
5222 publics_stream = add_stream (pdb, NULL, &publics_stream_num);
5224 if (!publics_stream)
5226 einfo (_("%P: warning: cannot create publics stream "
5227 "in PDB file: %E\n"));
5228 goto end;
5231 if (!create_section_header_stream (pdb, abfd, &section_header_stream_num))
5233 einfo (_("%P: warning: cannot create section header stream "
5234 "in PDB file: %E\n"));
5235 goto end;
5238 types.num_types = 0;
5239 types.hashmap = htab_create_alloc (0, hash_type_entry, eq_type_entry,
5240 free, xcalloc, free);
5241 types.first = types.last = NULL;
5243 ids.num_types = 0;
5244 ids.hashmap = htab_create_alloc (0, hash_type_entry, eq_type_entry,
5245 free, xcalloc, free);
5246 ids.first = ids.last = NULL;
5248 if (!populate_dbi_stream (dbi_stream, abfd, pdb, section_header_stream_num,
5249 sym_rec_stream_num, publics_stream_num,
5250 &strings, &types, &ids, sym_rec_stream, pdb_name))
5252 einfo (_("%P: warning: cannot populate DBI stream "
5253 "in PDB file: %E\n"));
5254 htab_delete (types.hashmap);
5255 htab_delete (ids.hashmap);
5256 goto end;
5259 if (!populate_type_stream (pdb, tpi_stream, &types))
5261 einfo (_("%P: warning: cannot populate TPI stream "
5262 "in PDB file: %E\n"));
5263 htab_delete (types.hashmap);
5264 htab_delete (ids.hashmap);
5265 goto end;
5268 htab_delete (types.hashmap);
5270 if (!populate_type_stream (pdb, ipi_stream, &ids))
5272 einfo (_("%P: warning: cannot populate IPI stream "
5273 "in PDB file: %E\n"));
5274 htab_delete (ids.hashmap);
5275 goto end;
5278 htab_delete (ids.hashmap);
5280 add_string ("", 0, &strings);
5282 if (!populate_names_stream (names_stream, &strings))
5284 einfo (_("%P: warning: cannot populate names stream "
5285 "in PDB file: %E\n"));
5286 goto end;
5289 if (!populate_publics_stream (publics_stream, abfd, sym_rec_stream))
5291 einfo (_("%P: warning: cannot populate publics stream "
5292 "in PDB file: %E\n"));
5293 goto end;
5296 if (!populate_info_stream (pdb, info_stream, guid))
5298 einfo (_("%P: warning: cannot populate info stream "
5299 "in PDB file: %E\n"));
5300 goto end;
5303 ret = true;
5305 end:
5306 bfd_close (pdb);
5308 htab_delete (strings.hashmap);
5310 return ret;