1 /* Support for generating PDB CodeView debugging files.
2 Copyright (C) 2022 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. */
27 #include "libiberty.h"
28 #include "coff/i386.h"
29 #include "coff/external.h"
30 #include "coff/internal.h"
50 uint32_t source_file_offset;
57 struct string *strings_head;
58 struct string *strings_tail;
63 struct mod_source_files
66 struct string **files;
69 struct source_files_info
72 struct mod_source_files *mods;
77 struct type_entry *next;
80 bool has_udt_src_line;
88 struct type_entry *first;
89 struct type_entry *last;
104 uint32_t num_entries;
105 struct global *first;
110 static const uint32_t crc_table[] =
112 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f,
113 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988,
114 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2,
115 0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7,
116 0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9,
117 0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172,
118 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, 0x42b2986c,
119 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59,
120 0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423,
121 0xcfba9599, 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924,
122 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, 0x01db7106,
123 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433,
124 0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d,
125 0x91646c97, 0xe6635c01, 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e,
126 0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950,
127 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65,
128 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7,
129 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0,
130 0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa,
131 0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
132 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81,
133 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a,
134 0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683, 0xe3630b12, 0x94643b84,
135 0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1,
136 0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb,
137 0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc,
138 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 0xd6d6a3e8, 0xa1d1937e,
139 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b,
140 0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55,
141 0x316e8eef, 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236,
142 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, 0xb2bd0b28,
143 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d,
144 0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f,
145 0x72076785, 0x05005713, 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38,
146 0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242,
147 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777,
148 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69,
149 0x616bffd3, 0x166ccf45, 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2,
150 0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc,
151 0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
152 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693,
153 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94,
154 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d
157 /* Add a new stream to the PDB archive, and return its BFD. */
159 add_stream (bfd *pdb, const char *name, uint16_t *stream_num)
164 stream = bfd_create (name ? name : "", pdb);
168 if (!bfd_make_writable (stream))
174 if (!pdb->archive_head)
176 bfd_set_archive_head (pdb, stream);
181 bfd *b = pdb->archive_head;
185 while (b->archive_next)
191 b->archive_next = stream;
200 /* Stream 0 ought to be a copy of the MSF directory from the last
201 time the PDB file was written. Because we don't do incremental
202 writes this isn't applicable to us, but we fill it with a dummy
203 value so as not to confuse radare. */
205 create_old_directory_stream (bfd *pdb)
208 char buf[sizeof (uint32_t)];
210 stream = add_stream (pdb, NULL, NULL);
216 return bfd_bwrite (buf, sizeof (uint32_t), stream) == sizeof (uint32_t);
219 /* Calculate the hash of a given string. */
221 calc_hash (const char *data, size_t len)
228 hash ^= data[1] << 8;
229 hash ^= data[2] << 16;
230 hash ^= data[3] << 24;
239 hash ^= data[1] << 8;
249 hash ^= (hash >> 11);
251 return hash ^ (hash >> 16);
254 /* Stream 1 is the PDB info stream - see
255 https://llvm.org/docs/PDB/PdbStream.html. */
257 populate_info_stream (bfd *pdb, bfd *info_stream, const unsigned char *guid)
260 struct pdb_stream_70 h;
261 uint32_t num_entries, num_buckets;
262 uint32_t names_length, stream_num;
263 char int_buf[sizeof (uint32_t)];
271 struct hash_entry **buckets = NULL;
275 bfd_putl32 (PDB_STREAM_VERSION_VC70, &h.version);
276 bfd_putl32 (time (NULL), &h.signature);
277 bfd_putl32 (1, &h.age);
279 bfd_putl32 (bfd_getb32 (guid), h.guid);
280 bfd_putl16 (bfd_getb16 (&guid[4]), &h.guid[4]);
281 bfd_putl16 (bfd_getb16 (&guid[6]), &h.guid[6]);
282 memcpy (&h.guid[8], &guid[8], 8);
284 if (bfd_bwrite (&h, sizeof (h), info_stream) != sizeof (h))
287 /* Write hash list of named streams. This is a "rollover" hash, i.e.
288 if a bucket is filled an entry gets placed in the next free
292 for (bfd *b = pdb->archive_head; b; b = b->archive_next)
294 if (strcmp (b->filename, ""))
298 num_buckets = num_entries * 2;
305 buckets = xmalloc (sizeof (struct hash_entry *) * num_buckets);
306 memset (buckets, 0, sizeof (struct hash_entry *) * num_buckets);
308 for (bfd *b = pdb->archive_head; b; b = b->archive_next)
310 if (strcmp (b->filename, ""))
312 size_t len = strlen (b->filename);
313 uint32_t hash = (uint16_t) calc_hash (b->filename, len);
314 uint32_t bucket_num = hash % num_buckets;
316 while (buckets[bucket_num])
320 if (bucket_num == num_buckets)
324 buckets[bucket_num] = xmalloc (sizeof (struct hash_entry));
326 buckets[bucket_num]->offset = names_length;
327 buckets[bucket_num]->value = stream_num;
329 names_length += len + 1;
336 /* Write the strings list - the hash keys are indexes into this. */
338 bfd_putl32 (names_length, int_buf);
340 if (bfd_bwrite (int_buf, sizeof (uint32_t), info_stream) !=
344 for (bfd *b = pdb->archive_head; b; b = b->archive_next)
346 if (!strcmp (b->filename, ""))
349 size_t len = strlen (b->filename) + 1;
351 if (bfd_bwrite (b->filename, len, info_stream) != len)
355 /* Write the number of entries and buckets. */
357 bfd_putl32 (num_entries, int_buf);
359 if (bfd_bwrite (int_buf, sizeof (uint32_t), info_stream) !=
363 bfd_putl32 (num_buckets, int_buf);
365 if (bfd_bwrite (int_buf, sizeof (uint32_t), info_stream) !=
369 /* Write the present bitmap. */
371 bfd_putl32 ((num_buckets + 31) / 32, int_buf);
373 if (bfd_bwrite (int_buf, sizeof (uint32_t), info_stream) !=
377 for (unsigned int i = 0; i < num_buckets; i += 32)
381 for (unsigned int j = 0; j < 32; j++)
383 if (i + j >= num_buckets)
390 bfd_putl32 (v, int_buf);
392 if (bfd_bwrite (int_buf, sizeof (uint32_t), info_stream) !=
397 /* Write the (empty) deleted bitmap. */
399 bfd_putl32 (0, int_buf);
401 if (bfd_bwrite (int_buf, sizeof (uint32_t), info_stream) !=
405 /* Write the buckets. */
407 for (unsigned int i = 0; i < num_buckets; i++)
411 bfd_putl32 (buckets[i]->offset, int_buf);
413 if (bfd_bwrite (int_buf, sizeof (uint32_t), info_stream) !=
417 bfd_putl32 (buckets[i]->value, int_buf);
419 if (bfd_bwrite (int_buf, sizeof (uint32_t), info_stream) !=
425 bfd_putl32 (0, int_buf);
427 if (bfd_bwrite (int_buf, sizeof (uint32_t), info_stream) !=
431 bfd_putl32 (PDB_STREAM_VERSION_VC140, int_buf);
433 if (bfd_bwrite (int_buf, sizeof (uint32_t), info_stream) !=
440 for (unsigned int i = 0; i < num_buckets; i++)
451 /* Calculate the CRC32 used for type hashes. */
453 crc32 (const uint8_t *data, size_t len)
459 crc = (crc >> 8) ^ crc_table[(crc & 0xff) ^ *data];
468 /* Stream 2 is the type information (TPI) stream, and stream 4 is
469 the ID information (IPI) stream. They differ only in which records
470 go in which stream. */
472 populate_type_stream (bfd *pdb, bfd *stream, struct types *types)
474 struct pdb_tpi_stream_header h;
475 struct type_entry *e;
476 uint32_t len = 0, index_offset_len, off;
477 struct bfd *hash_stream = NULL;
478 uint16_t hash_stream_index;
480 static const uint32_t index_skip = 0x2000;
484 index_offset_len = 0;
488 uint32_t old_len = len;
490 len += sizeof (uint16_t) + bfd_getl16 (e->data);
492 if (old_len == 0 || old_len / index_skip != len / index_skip)
493 index_offset_len += sizeof (uint32_t) * 2;
498 /* Each type stream also has a stream which holds the hash value for each
499 type, along with a skip list to speed up searching. */
501 hash_stream = add_stream (pdb, "", &hash_stream_index);
506 bfd_putl32 (TPI_STREAM_VERSION_80, &h.version);
507 bfd_putl32 (sizeof (h), &h.header_size);
508 bfd_putl32 (TPI_FIRST_INDEX, &h.type_index_begin);
509 bfd_putl32 (TPI_FIRST_INDEX + types->num_types, &h.type_index_end);
510 bfd_putl32 (len, &h.type_record_bytes);
511 bfd_putl16 (hash_stream_index, &h.hash_stream_index);
512 bfd_putl16 (0xffff, &h.hash_aux_stream_index);
513 bfd_putl32 (sizeof (uint32_t), &h.hash_key_size);
514 bfd_putl32 (NUM_TPI_HASH_BUCKETS, &h.num_hash_buckets);
515 bfd_putl32 (0, &h.hash_value_buffer_offset);
516 bfd_putl32 (types->num_types * sizeof (uint32_t),
517 &h.hash_value_buffer_length);
518 bfd_putl32 (types->num_types * sizeof (uint32_t),
519 &h.index_offset_buffer_offset);
520 bfd_putl32 (index_offset_len, &h.index_offset_buffer_length);
521 bfd_putl32 ((types->num_types * sizeof (uint32_t)) + index_offset_len,
522 &h.hash_adj_buffer_offset);
523 bfd_putl32 (0, &h.hash_adj_buffer_length);
525 if (bfd_bwrite (&h, sizeof (h), stream) != sizeof (h))
528 /* Write the type definitions into the main stream, and the hashes
529 into the hash stream. The hashes have already been calculated
536 uint8_t buf[sizeof (uint32_t)];
539 size = bfd_getl16 (e->data);
541 if (bfd_bwrite (e->data, size + sizeof (uint16_t), stream)
542 != size + sizeof (uint16_t))
545 bfd_putl32 (e->cv_hash % NUM_TPI_HASH_BUCKETS, buf);
547 if (bfd_bwrite (buf, sizeof (uint32_t), hash_stream)
548 != sizeof (uint32_t))
554 /* Write the index offsets, i.e. the skip list, into the hash stream. We
555 copy MSVC here by writing a new entry for every 8192 bytes. */
562 uint32_t old_off = off;
563 uint16_t size = bfd_getl16 (e->data);
565 off += size + sizeof (uint16_t);
567 if (old_off == 0 || old_off / index_skip != len / index_skip)
569 uint8_t buf[sizeof (uint32_t)];
571 bfd_putl32 (TPI_FIRST_INDEX + e->index, buf);
573 if (bfd_bwrite (buf, sizeof (uint32_t), hash_stream)
574 != sizeof (uint32_t))
577 bfd_putl32 (old_off, buf);
579 if (bfd_bwrite (buf, sizeof (uint32_t), hash_stream)
580 != sizeof (uint32_t))
590 /* Return the PE architecture number for the image. */
592 get_arch_number (bfd *abfd)
594 if (abfd->arch_info->arch != bfd_arch_i386)
597 if (abfd->arch_info->mach & bfd_mach_x86_64)
598 return IMAGE_FILE_MACHINE_AMD64;
600 return IMAGE_FILE_MACHINE_I386;
603 /* Validate the DEBUG_S_FILECHKSMS entry within a module's .debug$S
604 section, and copy it to the module's symbol stream. */
606 copy_filechksms (uint8_t *data, uint32_t size, char *string_table,
607 struct string_table *strings, uint8_t *out,
608 struct mod_source_files *mod_source)
610 uint8_t *orig_data = data;
611 uint32_t orig_size = size;
612 uint16_t num_files = 0;
613 struct string **strptr;
615 bfd_putl32 (DEBUG_S_FILECHKSMS, out);
616 out += sizeof (uint32_t);
618 bfd_putl32 (size, out);
619 out += sizeof (uint32_t);
621 /* Calculate the number of files, and check for any overflows. */
625 struct file_checksum *fc = (struct file_checksum *) data;
629 if (size < sizeof (struct file_checksum))
631 bfd_set_error (bfd_error_bad_value);
635 len = sizeof (struct file_checksum) + fc->checksum_length;
639 bfd_set_error (bfd_error_bad_value);
646 if (len % sizeof (uint32_t))
647 padding = sizeof (uint32_t) - (len % sizeof (uint32_t));
653 bfd_set_error (bfd_error_bad_value);
663 /* Add the files to mod_source, so that they'll appear in the source
669 uint16_t new_count = num_files + mod_source->files_count;
671 mod_source->files = xrealloc (mod_source->files,
672 sizeof (struct string *) * new_count);
674 strptr = mod_source->files + mod_source->files_count;
676 mod_source->files_count += num_files;
679 /* Actually copy the data. */
686 struct file_checksum *fc = (struct file_checksum *) data;
690 struct string *str = NULL;
692 string_off = bfd_getl32 (&fc->file_id);
693 len = sizeof (struct file_checksum) + fc->checksum_length;
695 if (len % sizeof (uint32_t))
696 padding = sizeof (uint32_t) - (len % sizeof (uint32_t));
700 /* Remap the "file ID", i.e. the offset in the module's string table,
701 so it points to the right place in the main string table. */
705 char *fn = string_table + string_off;
706 size_t fn_len = strlen (fn);
707 uint32_t hash = calc_hash (fn, fn_len);
710 slot = htab_find_slot_with_hash (strings->hashmap, fn, hash,
714 str = (struct string *) *slot;
720 bfd_putl32 (str ? str->offset : 0, &fc->file_id);
722 memcpy (out, data, len + padding);
724 data += len + padding;
725 size -= len + padding;
726 out += len + padding;
732 /* Add a string to the strings table, if it's not already there. Returns its
733 offset within the string table. */
735 add_string (char *str, size_t len, struct string_table *strings)
737 uint32_t hash = calc_hash (str, len);
741 slot = htab_find_slot_with_hash (strings->hashmap, str, hash, INSERT);
745 *slot = xmalloc (offsetof (struct string, s) + len);
747 s = (struct string *) *slot;
751 s->offset = strings->strings_len;
752 s->source_file_offset = 0xffffffff;
754 memcpy (s->s, str, len);
756 if (strings->strings_tail)
757 strings->strings_tail->next = s;
759 strings->strings_head = s;
761 strings->strings_tail = s;
763 strings->strings_len += len + 1;
767 s = (struct string *) *slot;
773 /* Return the hash of an entry in the string table. */
775 hash_string_table_entry (const void *p)
777 const struct string *s = (const struct string *) p;
782 /* Compare an entry in the string table with a string. */
784 eq_string_table_entry (const void *a, const void *b)
786 const struct string *s1 = (const struct string *) a;
787 const char *s2 = (const char *) b;
788 size_t s2_len = strlen (s2);
790 if (s2_len != s1->len)
793 return memcmp (s1->s, s2, s2_len) == 0;
796 /* Parse the string table within the .debug$S section. */
798 parse_string_table (bfd_byte *data, size_t size,
799 struct string_table *strings)
803 size_t len = strnlen ((char *) data, size);
805 add_string ((char *) data, len, strings);
816 /* Remap a type reference within a CodeView symbol. */
818 remap_symbol_type (void *data, struct type_entry **map, uint32_t num_types)
820 uint32_t type = bfd_getl32 (data);
822 /* Ignore builtin types (those with IDs below 0x1000). */
823 if (type < TPI_FIRST_INDEX)
826 if (type >= TPI_FIRST_INDEX + num_types)
828 einfo (_("%P: CodeView symbol references out of range type %v\n"),
833 type = TPI_FIRST_INDEX + map[type - TPI_FIRST_INDEX]->index;
834 bfd_putl32 (type, data);
839 /* Add an entry into the globals stream. If it already exists, increase
842 add_globals_ref (struct globals *glob, bfd *sym_rec_stream, const char *name,
843 size_t name_len, uint8_t *data, size_t len)
849 slot = htab_find_slot_with_hash (glob->hashmap, data,
850 iterative_hash (data, len, 0), INSERT);
859 *slot = xmalloc (offsetof (struct global, data) + len);
861 hash = crc32 ((const uint8_t *) name, name_len);
862 hash %= NUM_GLOBALS_HASH_BUCKETS;
866 g->offset = bfd_tell (sym_rec_stream);
869 memcpy (g->data, data, len + 1);
874 glob->last->next = g;
880 return bfd_bwrite (data, len, sym_rec_stream) == len;
883 /* Find the end of the current scope within symbols data. */
885 find_end_of_scope (uint8_t *data, uint32_t size)
887 unsigned int scope_level = 1;
890 len = bfd_getl16 (data) + sizeof (uint16_t);
899 if (size < sizeof (uint32_t))
902 len = bfd_getl16 (data) + sizeof (uint16_t);
903 type = bfd_getl16 (data + sizeof (uint16_t));
920 case S_INLINESITE_END:
923 if (scope_level == 0)
934 /* Return the size of an extended value parameter, as used in
937 extended_value_len (uint16_t type)
960 /* Parse the symbols in a .debug$S section, and copy them to the module's
963 parse_symbols (uint8_t *data, uint32_t size, uint8_t **buf,
964 struct type_entry **map, uint32_t num_types,
965 bfd *sym_rec_stream, struct globals *glob, uint16_t mod_num)
967 uint8_t *orig_buf = *buf;
968 unsigned int scope_level = 0;
969 uint8_t *scope = NULL;
971 while (size >= sizeof (uint16_t))
975 len = bfd_getl16 (data) + sizeof (uint16_t);
979 bfd_set_error (bfd_error_bad_value);
983 type = bfd_getl16 (data + sizeof (uint16_t));
992 struct datasym *d = (struct datasym *) data;
995 if (len < offsetof (struct datasym, name))
997 einfo (_("%P: warning: truncated CodeView record"
998 " S_LDATA32/S_GDATA32/S_LTHREAD32/S_GTHREAD32\n"));
999 bfd_set_error (bfd_error_bad_value);
1003 if (scope_level == 0)
1005 uint16_t section = bfd_getl16 (&d->section);
1007 if (section == 0) /* GC'd, ignore */
1012 strnlen (d->name, len - offsetof (struct datasym, name));
1014 if (name_len == len - offsetof (struct datasym, name))
1016 einfo (_("%P: warning: name for S_LDATA32/S_GDATA32/"
1017 "S_LTHREAD32/S_GTHREAD32 has no terminating"
1019 bfd_set_error (bfd_error_bad_value);
1023 if (!remap_symbol_type (&d->type, map, num_types))
1025 bfd_set_error (bfd_error_bad_value);
1029 /* If S_LDATA32 or S_LTHREAD32, copy into module symbols. */
1031 if (type == S_LDATA32 || type == S_LTHREAD32)
1033 memcpy (*buf, d, len);
1037 /* S_LDATA32 and S_LTHREAD32 only go in globals if
1038 not in function scope. */
1039 if (type == S_GDATA32 || type == S_GTHREAD32 || scope_level == 0)
1041 if (!add_globals_ref (glob, sym_rec_stream, d->name,
1042 name_len, data, len))
1054 struct procsym *proc = (struct procsym *) data;
1059 size_t ref_size, padding;
1062 if (len < offsetof (struct procsym, name))
1064 einfo (_("%P: warning: truncated CodeView record"
1065 " S_GPROC32/S_LPROC32\n"));
1066 bfd_set_error (bfd_error_bad_value);
1070 section = bfd_getl16 (&proc->section);
1072 endptr = find_end_of_scope (data, size);
1076 einfo (_("%P: warning: could not find end of"
1077 " S_GPROC32/S_LPROC32 record\n"));
1078 bfd_set_error (bfd_error_bad_value);
1082 if (section == 0) /* skip if GC'd */
1084 /* Skip to after S_END. */
1086 size -= endptr - data;
1089 len = bfd_getl16 (data) + sizeof (uint16_t);
1098 strnlen (proc->name, len - offsetof (struct procsym, name));
1100 if (name_len == len - offsetof (struct procsym, name))
1102 einfo (_("%P: warning: name for S_GPROC32/S_LPROC32 has no"
1103 " terminating zero\n"));
1104 bfd_set_error (bfd_error_bad_value);
1108 if (type == S_GPROC32_ID || type == S_LPROC32_ID)
1110 /* Transform into S_GPROC32 / S_LPROC32. */
1112 uint32_t t_idx = bfd_getl32 (&proc->type);
1113 struct type_entry *t;
1116 if (t_idx < TPI_FIRST_INDEX
1117 || t_idx >= TPI_FIRST_INDEX + num_types)
1119 einfo (_("%P: CodeView symbol references out of range"
1120 " type %v\n"), type);
1121 bfd_set_error (bfd_error_bad_value);
1125 t = map[t_idx - TPI_FIRST_INDEX];
1127 t_type = bfd_getl16 (t->data + sizeof (uint16_t));
1133 struct lf_func_id *t_data =
1134 (struct lf_func_id *) t->data;
1136 /* Replace proc->type with function type. */
1138 memcpy (&proc->type, &t_data->function_type,
1146 struct lf_mfunc_id *t_data =
1147 (struct lf_mfunc_id *) t->data;
1149 /* Replace proc->type with function type. */
1151 memcpy (&proc->type, &t_data->function_type,
1158 einfo (_("%P: CodeView S_GPROC32_ID/S_LPROC32_ID symbol"
1159 " referenced unknown type as ID\n"));
1160 bfd_set_error (bfd_error_bad_value);
1164 /* Change record type. */
1166 if (type == S_GPROC32_ID)
1167 bfd_putl32 (S_GPROC32, &proc->kind);
1169 bfd_putl32 (S_LPROC32, &proc->kind);
1173 if (!remap_symbol_type (&proc->type, map, num_types))
1175 bfd_set_error (bfd_error_bad_value);
1180 end = *buf - orig_buf + sizeof (uint32_t) + endptr - data;
1181 bfd_putl32 (end, &proc->end);
1183 /* Add S_PROCREF / S_LPROCREF to globals stream. */
1185 ref_size = offsetof (struct refsym, name) + name_len + 1;
1187 if (ref_size % sizeof (uint32_t))
1188 padding = sizeof (uint32_t) - (ref_size % sizeof (uint32_t));
1192 ref = xmalloc (ref_size + padding);
1194 bfd_putl16 (ref_size + padding - sizeof (uint16_t), &ref->size);
1195 bfd_putl16 (type == S_GPROC32 || type == S_GPROC32_ID ?
1196 S_PROCREF : S_LPROCREF, &ref->kind);
1197 bfd_putl32 (0, &ref->sum_name);
1198 bfd_putl32 (*buf - orig_buf + sizeof (uint32_t),
1199 &ref->symbol_offset);
1200 bfd_putl16 (mod_num + 1, &ref->mod);
1202 memcpy (ref->name, proc->name, name_len + 1);
1204 memset (ref->name + name_len + 1, 0, padding);
1206 if (!add_globals_ref (glob, sym_rec_stream, proc->name, name_len,
1207 (uint8_t *) ref, ref_size + padding))
1217 memcpy (*buf, proc, len);
1227 struct udtsym *udt = (struct udtsym *) data;
1230 if (len < offsetof (struct udtsym, name))
1232 einfo (_("%P: warning: truncated CodeView record"
1234 bfd_set_error (bfd_error_bad_value);
1239 strnlen (udt->name, len - offsetof (struct udtsym, name));
1241 if (name_len == len - offsetof (struct udtsym, name))
1243 einfo (_("%P: warning: name for S_UDT has no"
1244 " terminating zero\n"));
1245 bfd_set_error (bfd_error_bad_value);
1249 if (!remap_symbol_type (&udt->type, map, num_types))
1251 bfd_set_error (bfd_error_bad_value);
1255 /* S_UDT goes in the symbols stream if within a procedure,
1256 otherwise it goes in the globals stream. */
1257 if (scope_level == 0)
1259 if (!add_globals_ref (glob, sym_rec_stream, udt->name,
1260 name_len, data, len))
1265 memcpy (*buf, udt, len);
1274 struct constsym *c = (struct constsym *) data;
1275 size_t name_len, rec_size;
1278 if (len < offsetof (struct constsym, name))
1280 einfo (_("%P: warning: truncated CodeView record"
1282 bfd_set_error (bfd_error_bad_value);
1286 rec_size = offsetof (struct constsym, name);
1288 val = bfd_getl16 (&c->value);
1290 /* If val >= 0x8000, actual value follows. */
1293 unsigned int param_len = extended_value_len (val);
1297 einfo (_("%P: warning: unhandled type %v within"
1298 " S_CONSTANT\n"), val);
1299 bfd_set_error (bfd_error_bad_value);
1303 rec_size += param_len;
1307 strnlen ((const char *) data + rec_size, len - rec_size);
1309 if (name_len == len - rec_size)
1311 einfo (_("%P: warning: name for S_CONSTANT has no"
1312 " terminating zero\n"));
1313 bfd_set_error (bfd_error_bad_value);
1317 if (!remap_symbol_type (&c->type, map, num_types))
1319 bfd_set_error (bfd_error_bad_value);
1323 if (!add_globals_ref (glob, sym_rec_stream,
1324 (const char *) data + rec_size, name_len,
1332 case S_INLINESITE_END:
1334 memcpy (*buf, data, len);
1336 if (type == S_PROC_ID_END) /* transform to S_END */
1337 bfd_putl16 (S_END, *buf + sizeof (uint16_t));
1339 /* Reset scope variable back to the address of the previous
1344 uint16_t scope_start_type =
1345 bfd_getl16 (scope + sizeof (uint16_t));
1347 switch (scope_start_type)
1351 parent = bfd_getl32 (scope + offsetof (struct procsym,
1356 parent = bfd_getl32 (scope + offsetof (struct blocksym,
1361 parent = bfd_getl32 (scope + offsetof (struct inline_site,
1366 parent = bfd_getl32 (scope + offsetof (struct thunk,
1371 einfo (_("%P: warning: unexpected CodeView scope start"
1372 " record %v\n"), scope_start_type);
1373 bfd_set_error (bfd_error_bad_value);
1380 scope = orig_buf + parent - sizeof (uint32_t);
1389 struct buildinfosym *bi = (struct buildinfosym *) data;
1391 if (len < sizeof (struct buildinfosym))
1393 einfo (_("%P: warning: truncated CodeView record"
1395 bfd_set_error (bfd_error_bad_value);
1399 if (!remap_symbol_type (&bi->type, map, num_types))
1401 bfd_set_error (bfd_error_bad_value);
1405 memcpy (*buf, data, len);
1413 struct blocksym *bl = (struct blocksym *) data;
1417 if (len < offsetof (struct blocksym, name))
1419 einfo (_("%P: warning: truncated CodeView record"
1421 bfd_set_error (bfd_error_bad_value);
1425 bfd_putl32 (scope - orig_buf + sizeof (uint32_t), &bl->parent);
1427 endptr = find_end_of_scope (data, size);
1431 einfo (_("%P: warning: could not find end of"
1432 " S_BLOCK32 record\n"));
1433 bfd_set_error (bfd_error_bad_value);
1437 end = *buf - orig_buf + sizeof (uint32_t) + endptr - data;
1438 bfd_putl32 (end, &bl->end);
1442 memcpy (*buf, data, len);
1452 struct bprelsym *bp = (struct bprelsym *) data;
1454 if (len < offsetof (struct bprelsym, name))
1456 einfo (_("%P: warning: truncated CodeView record"
1458 bfd_set_error (bfd_error_bad_value);
1462 if (!remap_symbol_type (&bp->type, map, num_types))
1464 bfd_set_error (bfd_error_bad_value);
1468 memcpy (*buf, data, len);
1476 struct regsym *reg = (struct regsym *) data;
1478 if (len < offsetof (struct regsym, name))
1480 einfo (_("%P: warning: truncated CodeView record"
1482 bfd_set_error (bfd_error_bad_value);
1486 if (!remap_symbol_type (®->type, map, num_types))
1488 bfd_set_error (bfd_error_bad_value);
1492 memcpy (*buf, data, len);
1500 struct regrel *rr = (struct regrel *) data;
1502 if (len < offsetof (struct regrel, name))
1504 einfo (_("%P: warning: truncated CodeView record"
1506 bfd_set_error (bfd_error_bad_value);
1510 if (!remap_symbol_type (&rr->type, map, num_types))
1512 bfd_set_error (bfd_error_bad_value);
1516 memcpy (*buf, data, len);
1524 struct localsym *l = (struct localsym *) data;
1526 if (len < offsetof (struct localsym, name))
1528 einfo (_("%P: warning: truncated CodeView record"
1530 bfd_set_error (bfd_error_bad_value);
1534 if (!remap_symbol_type (&l->type, map, num_types))
1536 bfd_set_error (bfd_error_bad_value);
1540 memcpy (*buf, data, len);
1548 struct inline_site *is = (struct inline_site *) data;
1552 if (len < offsetof (struct inline_site, binary_annotations))
1554 einfo (_("%P: warning: truncated CodeView record"
1555 " S_INLINESITE\n"));
1556 bfd_set_error (bfd_error_bad_value);
1560 bfd_putl32 (scope - orig_buf + sizeof (uint32_t), &is->parent);
1562 endptr = find_end_of_scope (data, size);
1566 einfo (_("%P: warning: could not find end of"
1567 " S_INLINESITE record\n"));
1568 bfd_set_error (bfd_error_bad_value);
1572 end = *buf - orig_buf + sizeof (uint32_t) + endptr - data;
1573 bfd_putl32 (end, &is->end);
1575 if (!remap_symbol_type (&is->inlinee, map, num_types))
1577 bfd_set_error (bfd_error_bad_value);
1583 memcpy (*buf, data, len);
1593 struct thunk *th = (struct thunk *) data;
1597 if (len < offsetof (struct thunk, name))
1599 einfo (_("%P: warning: truncated CodeView record"
1601 bfd_set_error (bfd_error_bad_value);
1605 bfd_putl32 (scope - orig_buf + sizeof (uint32_t), &th->parent);
1607 endptr = find_end_of_scope (data, size);
1611 einfo (_("%P: warning: could not find end of"
1612 " S_THUNK32 record\n"));
1613 bfd_set_error (bfd_error_bad_value);
1617 end = *buf - orig_buf + sizeof (uint32_t) + endptr - data;
1618 bfd_putl32 (end, &th->end);
1622 memcpy (*buf, data, len);
1630 case S_HEAPALLOCSITE:
1632 struct heap_alloc_site *has = (struct heap_alloc_site *) data;
1634 if (len < sizeof (struct heap_alloc_site))
1636 einfo (_("%P: warning: truncated CodeView record"
1637 " S_HEAPALLOCSITE\n"));
1638 bfd_set_error (bfd_error_bad_value);
1642 if (!remap_symbol_type (&has->type, map, num_types))
1644 bfd_set_error (bfd_error_bad_value);
1648 memcpy (*buf, data, len);
1654 case S_OBJNAME: /* just copy */
1660 case S_DEFRANGE_REGISTER_REL:
1661 case S_DEFRANGE_FRAMEPOINTER_REL:
1662 case S_DEFRANGE_SUBFIELD_REGISTER:
1663 case S_DEFRANGE_FRAMEPOINTER_REL_FULL_SCOPE:
1664 case S_DEFRANGE_REGISTER:
1665 memcpy (*buf, data, len);
1670 einfo (_("%P: warning: unrecognized CodeView record %v\n"), type);
1671 bfd_set_error (bfd_error_bad_value);
1682 /* For a given symbol subsection, work out how much space to allocate in the
1683 result module stream. This is different because we don't copy certain
1684 symbols, such as S_CONSTANT, and we skip over any procedures or data that
1685 have been GC'd out. */
1687 calculate_symbols_size (uint8_t *data, uint32_t size, uint32_t *sym_size)
1689 unsigned int scope_level = 0;
1691 while (size >= sizeof (uint32_t))
1693 uint16_t len = bfd_getl16 (data) + sizeof (uint16_t);
1694 uint16_t type = bfd_getl16 (data + sizeof (uint16_t));
1701 struct datasym *d = (struct datasym *) data;
1704 if (len < offsetof (struct datasym, name))
1706 einfo (_("%P: warning: truncated CodeView record"
1707 " S_LDATA32/S_LTHREAD32\n"));
1711 section = bfd_getl16 (&d->section);
1713 /* copy if not GC'd or within function */
1714 if (scope_level != 0 || section != 0)
1721 /* Not copied into symbols stream. */
1729 struct procsym *proc = (struct procsym *) data;
1732 if (len < offsetof (struct procsym, name))
1734 einfo (_("%P: warning: truncated CodeView record"
1735 " S_GPROC32/S_LPROC32\n"));
1739 section = bfd_getl16 (&proc->section);
1747 uint8_t *endptr = find_end_of_scope (data, size);
1751 einfo (_("%P: warning: could not find end of"
1752 " S_GPROC32/S_LPROC32 record\n"));
1756 /* Skip to after S_END. */
1758 size -= endptr - data;
1761 len = bfd_getl16 (data) + sizeof (uint16_t);
1775 if (scope_level != 0) /* only goes in symbols if local */
1779 case S_BLOCK32: /* always copied */
1786 case S_END: /* always copied */
1788 case S_INLINESITE_END:
1793 case S_OBJNAME: /* always copied */
1804 case S_DEFRANGE_REGISTER_REL:
1805 case S_DEFRANGE_FRAMEPOINTER_REL:
1806 case S_DEFRANGE_SUBFIELD_REGISTER:
1807 case S_DEFRANGE_FRAMEPOINTER_REL_FULL_SCOPE:
1808 case S_DEFRANGE_REGISTER:
1809 case S_HEAPALLOCSITE:
1814 einfo (_("%P: warning: unrecognized CodeView record %v\n"), type);
1825 /* Parse the .debug$S section within an object file. */
1827 handle_debugs_section (asection *s, bfd *mod, struct string_table *strings,
1828 uint8_t **dataptr, uint32_t *sizeptr,
1829 struct mod_source_files *mod_source,
1830 bfd *abfd, uint8_t **syms, uint32_t *sym_byte_size,
1831 struct type_entry **map, uint32_t num_types,
1832 bfd *sym_rec_stream, struct globals *glob,
1835 bfd_byte *data = NULL;
1837 uint32_t c13_size = 0;
1838 char *string_table = NULL;
1839 uint8_t *buf, *bufptr, *symbuf, *symbufptr;
1840 uint32_t sym_size = 0;
1842 if (!bfd_get_full_section_contents (mod, s, &data))
1848 /* Resolve relocations. Addresses are stored within the .debug$S section as
1849 a .secidx, .secrel32 pair. */
1851 if (s->flags & SEC_RELOC)
1853 struct internal_reloc *relocs;
1854 struct internal_syment *symbols;
1855 asection **sectlist;
1856 unsigned int syment_count;
1858 struct external_syment *ext;
1860 syment_count = obj_raw_syment_count (mod);
1863 _bfd_coff_read_internal_relocs (mod, s, false, NULL, true, NULL);
1865 symbols = xmalloc (sizeof (struct internal_syment) * syment_count);
1866 sectlist = xmalloc (sizeof (asection *) * syment_count);
1868 ext = (struct external_syment *) (coff_data (mod)->external_syms);
1870 for (unsigned int i = 0; i < syment_count; i++)
1872 bfd_coff_swap_sym_in (mod, &ext[i], &symbols[i]);
1877 for (asection *sect = mod->sections; sect; sect = sect->next)
1879 for (unsigned int i = 0; i < syment_count; i++)
1881 if (symbols[i].n_scnum == sect_num)
1888 if (!bfd_coff_relocate_section (abfd, coff_data (abfd)->link_info, mod,
1889 s, data, relocs, symbols, sectlist))
1901 if (bfd_getl32 (data) != CV_SIGNATURE_C13)
1907 off = sizeof (uint32_t);
1909 /* calculate size */
1911 while (off + sizeof (uint32_t) <= s->size)
1913 uint32_t type, size;
1915 type = bfd_getl32 (data + off);
1917 off += sizeof (uint32_t);
1919 if (off + sizeof (uint32_t) > s->size)
1922 bfd_set_error (bfd_error_bad_value);
1926 size = bfd_getl32 (data + off);
1928 off += sizeof (uint32_t);
1930 if (off + size > s->size)
1933 bfd_set_error (bfd_error_bad_value);
1939 case DEBUG_S_FILECHKSMS:
1940 c13_size += sizeof (uint32_t) + sizeof (uint32_t) + size;
1942 if (c13_size % sizeof (uint32_t))
1943 c13_size += sizeof (uint32_t) - (c13_size % sizeof (uint32_t));
1947 case DEBUG_S_STRINGTABLE:
1948 parse_string_table (data + off, size, strings);
1950 string_table = (char *) data + off;
1958 if (size < sizeof (uint32_t) + sizeof (uint16_t))
1961 bfd_set_error (bfd_error_bad_value);
1965 sect = bfd_getl16 (data + off + sizeof (uint32_t));
1967 /* Skip GC'd symbols. */
1970 c13_size += sizeof (uint32_t) + sizeof (uint32_t) + size;
1972 if (c13_size % sizeof (uint32_t))
1974 sizeof (uint32_t) - (c13_size % sizeof (uint32_t));
1980 case DEBUG_S_SYMBOLS:
1981 if (!calculate_symbols_size (data + off, size, &sym_size))
1984 bfd_set_error (bfd_error_bad_value);
1993 if (off % sizeof (uint32_t))
1994 off += sizeof (uint32_t) - (off % sizeof (uint32_t));
1997 if (sym_size % sizeof (uint32_t))
1998 sym_size += sizeof (uint32_t) - (sym_size % sizeof (uint32_t));
2000 if (c13_size == 0 && sym_size == 0)
2010 buf = xmalloc (c13_size);
2015 symbuf = xmalloc (sym_size);
2018 off = sizeof (uint32_t);
2020 while (off + sizeof (uint32_t) <= s->size)
2022 uint32_t type, size;
2024 type = bfd_getl32 (data + off);
2025 off += sizeof (uint32_t);
2027 size = bfd_getl32 (data + off);
2028 off += sizeof (uint32_t);
2032 case DEBUG_S_FILECHKSMS:
2033 if (!copy_filechksms (data + off, size, string_table,
2034 strings, bufptr, mod_source))
2041 bufptr += sizeof (uint32_t) + sizeof (uint32_t) + size;
2049 sect = bfd_getl16 (data + off + sizeof (uint32_t));
2054 bfd_putl32 (type, bufptr);
2055 bufptr += sizeof (uint32_t);
2057 bfd_putl32 (size, bufptr);
2058 bufptr += sizeof (uint32_t);
2060 memcpy (bufptr, data + off, size);
2067 case DEBUG_S_SYMBOLS:
2068 if (!parse_symbols (data + off, size, &symbufptr, map, num_types,
2069 sym_rec_stream, glob, mod_num))
2081 if (off % sizeof (uint32_t))
2082 off += sizeof (uint32_t) - (off % sizeof (uint32_t));
2091 /* Append the C13 info to what's already there, if the module has
2092 multiple .debug$S sections. */
2094 *dataptr = xrealloc (*dataptr, *sizeptr + c13_size);
2095 memcpy (*dataptr + *sizeptr, buf, c13_size);
2104 *sizeptr += c13_size;
2111 *syms = xrealloc (*syms, *sym_byte_size + sym_size);
2112 memcpy (*syms + *sym_byte_size, symbuf, sym_size);
2121 *sym_byte_size += sym_size;
2127 /* Remap the type number stored in data from the per-module numbering to
2128 that of the deduplicated output list. */
2130 remap_type (void *data, struct type_entry **map,
2131 uint32_t type_num, uint32_t num_types)
2133 uint32_t type = bfd_getl32 (data);
2135 /* Ignore builtin types (those with IDs below 0x1000). */
2136 if (type < TPI_FIRST_INDEX)
2139 if (type >= TPI_FIRST_INDEX + type_num)
2141 einfo (_("%P: CodeView type %v references other type %v not yet "
2142 "declared\n"), TPI_FIRST_INDEX + type_num, type);
2146 if (type >= TPI_FIRST_INDEX + num_types)
2148 einfo (_("%P: CodeView type %v references out of range type %v\n"),
2149 TPI_FIRST_INDEX + type_num, type);
2153 type = TPI_FIRST_INDEX + map[type - TPI_FIRST_INDEX]->index;
2154 bfd_putl32 (type, data);
2159 /* Determines whether the name of a struct, class, or union counts as
2160 "anonymous". Non-anonymous types have a hash based on just the name,
2161 rather than the whole structure. */
2163 is_name_anonymous (char *name, size_t len)
2165 static const char tag1[] = "<unnamed-tag>";
2166 static const char tag2[] = "__unnamed";
2167 static const char tag3[] = "::<unnamed-tag>";
2168 static const char tag4[] = "::__unnamed";
2170 if (len == sizeof (tag1) - 1 && !memcmp (name, tag1, sizeof (tag1) - 1))
2173 if (len == sizeof (tag2) - 1 && !memcmp (name, tag2, sizeof (tag2) - 1))
2176 if (len >= sizeof (tag3) - 1
2177 && !memcmp (name + len - sizeof (tag3) + 1, tag3, sizeof (tag3) - 1))
2180 if (len >= sizeof (tag4) - 1
2181 && !memcmp (name + len - sizeof (tag4) + 1, tag4, sizeof (tag4) - 1))
2187 /* Handle LF_UDT_SRC_LINE type entries, which are a special case. These
2188 give the source file and line number for each user-defined type that is
2189 declared. We parse these and emit instead an LF_UDT_MOD_SRC_LINE entry,
2190 which also includes the module number. */
2192 handle_udt_src_line (uint8_t *data, uint16_t size, struct type_entry **map,
2193 uint32_t type_num, uint32_t num_types,
2194 struct types *ids, uint16_t mod_num,
2195 struct string_table *strings)
2197 struct lf_udt_src_line *usl = (struct lf_udt_src_line *) data;
2198 uint32_t orig_type, source_file_type;
2201 struct type_entry *e, *type_e, *str_e;
2202 struct lf_udt_mod_src_line *umsl;
2203 struct lf_string_id *str;
2204 uint32_t source_file_offset;
2206 if (size < sizeof (struct lf_udt_src_line))
2208 einfo (_("%P: warning: truncated CodeView type record"
2209 " LF_UDT_SRC_LINE\n"));
2213 /* Check if LF_UDT_MOD_SRC_LINE already present for type, and return. */
2215 orig_type = bfd_getl32 (&usl->type);
2217 if (orig_type < TPI_FIRST_INDEX ||
2218 orig_type >= TPI_FIRST_INDEX + num_types ||
2219 !map[orig_type - TPI_FIRST_INDEX])
2221 einfo (_("%P: warning: CodeView type record LF_UDT_SRC_LINE"
2222 " referred to unknown type %v\n"), orig_type);
2226 type_e = map[orig_type - TPI_FIRST_INDEX];
2228 /* Skip if type already declared in other module. */
2229 if (type_e->has_udt_src_line)
2232 if (!remap_type (&usl->type, map, type_num, num_types))
2235 /* Extract string from source_file_type. */
2237 source_file_type = bfd_getl32 (&usl->source_file_type);
2239 if (source_file_type < TPI_FIRST_INDEX ||
2240 source_file_type >= TPI_FIRST_INDEX + num_types ||
2241 !map[source_file_type - TPI_FIRST_INDEX])
2243 einfo (_("%P: warning: CodeView type record LF_UDT_SRC_LINE"
2244 " referred to unknown string %v\n"), source_file_type);
2248 str_e = map[source_file_type - TPI_FIRST_INDEX];
2250 if (bfd_getl16 (str_e->data + sizeof (uint16_t)) != LF_STRING_ID)
2252 einfo (_("%P: warning: CodeView type record LF_UDT_SRC_LINE"
2253 " pointed to unexpected record type\n"));
2257 str = (struct lf_string_id *) str_e->data;
2259 /* Add string to string table. */
2261 source_file_offset = add_string (str->string, strlen (str->string),
2264 /* Add LF_UDT_MOD_SRC_LINE entry. */
2266 size = sizeof (struct lf_udt_mod_src_line);
2268 e = xmalloc (offsetof (struct type_entry, data) + size);
2271 e->index = ids->num_types;
2272 e->has_udt_src_line = false;
2274 /* LF_UDT_MOD_SRC_LINE use calc_hash on the type number, rather than
2275 the crc32 used for type hashes elsewhere. */
2276 e->cv_hash = calc_hash ((char *) &usl->type, sizeof (uint32_t));
2278 type_e->has_udt_src_line = true;
2280 umsl = (struct lf_udt_mod_src_line *) e->data;
2282 bfd_putl16 (size - sizeof (uint16_t), &umsl->size);
2283 bfd_putl16 (LF_UDT_MOD_SRC_LINE, &umsl->kind);
2284 memcpy (&umsl->type, &usl->type, sizeof (uint32_t));
2285 bfd_putl32 (source_file_offset, &umsl->source_file_string);
2286 memcpy (&umsl->line_no, &usl->line_no, sizeof (uint32_t));
2287 bfd_putl16 (mod_num + 1, &umsl->module_no);
2289 hash = iterative_hash (e->data, size, 0);
2291 slot = htab_find_slot_with_hash (ids->hashmap, data, hash, INSERT);
2301 einfo (_("%P: warning: duplicate CodeView type record "
2302 "LF_UDT_MOD_SRC_LINE\n"));
2309 ids->last->next = e;
2322 /* Parse a type definition in the .debug$T section. We remap the numbers
2323 of any referenced types, and if the type is not a duplicate of one
2324 already seen add it to types (for TPI types) or ids (for IPI types). */
2326 handle_type (uint8_t *data, struct type_entry **map, uint32_t type_num,
2327 uint32_t num_types, struct types *types,
2328 struct types *ids, uint16_t mod_num,
2329 struct string_table *strings)
2331 uint16_t size, type;
2334 bool other_hash = false;
2339 size = bfd_getl16 (data) + sizeof (uint16_t);
2340 type = bfd_getl16 (data + sizeof (uint16_t));
2346 struct lf_modifier *mod = (struct lf_modifier *) data;
2348 if (size < offsetof (struct lf_modifier, modifier))
2350 einfo (_("%P: warning: truncated CodeView type record "
2355 if (!remap_type (&mod->base_type, map, type_num, num_types))
2363 struct lf_pointer *ptr = (struct lf_pointer *) data;
2365 if (size < offsetof (struct lf_pointer, attributes))
2367 einfo (_("%P: warning: truncated CodeView type record"
2372 if (!remap_type (&ptr->base_type, map, type_num, num_types))
2380 struct lf_procedure *proc = (struct lf_procedure *) data;
2382 if (size < sizeof (struct lf_procedure))
2384 einfo (_("%P: warning: truncated CodeView type record"
2385 " LF_PROCEDURE\n"));
2389 if (!remap_type (&proc->return_type, map, type_num, num_types))
2392 if (!remap_type (&proc->arglist, map, type_num, num_types))
2400 struct lf_mfunction *func = (struct lf_mfunction *) data;
2402 if (size < sizeof (struct lf_procedure))
2404 einfo (_("%P: warning: truncated CodeView type record"
2405 " LF_MFUNCTION\n"));
2409 if (!remap_type (&func->return_type, map, type_num, num_types))
2412 if (!remap_type (&func->containing_class_type, map, type_num,
2416 if (!remap_type (&func->this_type, map, type_num, num_types))
2419 if (!remap_type (&func->arglist, map, type_num, num_types))
2427 uint32_t num_entries;
2428 struct lf_arglist *al = (struct lf_arglist *) data;
2430 if (size < offsetof (struct lf_arglist, args))
2432 einfo (_("%P: warning: truncated CodeView type record"
2437 num_entries = bfd_getl32 (&al->num_entries);
2439 if (size < offsetof (struct lf_arglist, args)
2440 + (num_entries * sizeof (uint32_t)))
2442 einfo (_("%P: warning: truncated CodeView type record"
2447 for (uint32_t i = 0; i < num_entries; i++)
2449 if (!remap_type (&al->args[i], map, type_num, num_types))
2458 uint16_t left = size - sizeof (uint16_t) - sizeof (uint16_t);
2459 uint8_t *ptr = data + sizeof (uint16_t) + sizeof (uint16_t);
2465 if (left < sizeof (uint16_t))
2467 einfo (_("%P: warning: truncated CodeView type record"
2468 " LF_FIELDLIST\n"));
2472 subtype = bfd_getl16 (ptr);
2478 struct lf_member *mem = (struct lf_member *) ptr;
2480 size_t name_len, subtype_len;
2482 if (left < offsetof (struct lf_member, name))
2484 einfo (_("%P: warning: truncated CodeView type record"
2489 if (!remap_type (&mem->type, map, type_num, num_types))
2492 subtype_len = offsetof (struct lf_member, name);
2494 offset = bfd_getl16 (&mem->offset);
2496 /* If offset >= 0x8000, actual value follows. */
2497 if (offset >= 0x8000)
2499 unsigned int param_len = extended_value_len (offset);
2503 einfo (_("%P: warning: unhandled type %v within"
2504 " LF_MEMBER\n"), offset);
2508 subtype_len += param_len;
2510 if (left < subtype_len)
2512 einfo (_("%P: warning: truncated CodeView type record"
2519 strnlen ((char *) mem + subtype_len, left - subtype_len);
2521 if (name_len == left - offsetof (struct lf_member, name))
2523 einfo (_("%P: warning: name for LF_MEMBER has no"
2524 " terminating zero\n"));
2530 subtype_len += name_len;
2532 if (subtype_len % 4 != 0)
2533 subtype_len += 4 - (subtype_len % 4);
2535 if (left < subtype_len)
2537 einfo (_("%P: warning: truncated CodeView type record"
2538 " LF_FIELDLIST\n"));
2543 left -= subtype_len;
2550 struct lf_enumerate *en = (struct lf_enumerate *) ptr;
2551 size_t name_len, subtype_len;
2554 if (left < offsetof (struct lf_enumerate, name))
2556 einfo (_("%P: warning: truncated CodeView type record"
2557 " LF_ENUMERATE\n"));
2561 subtype_len = offsetof (struct lf_enumerate, name);
2563 val = bfd_getl16 (&en->value);
2565 /* If val >= 0x8000, the actual value immediately follows. */
2568 unsigned int param_len = extended_value_len (val);
2572 einfo (_("%P: warning: unhandled type %v within"
2573 " LF_ENUMERATE\n"), val);
2577 if (left < subtype_len + param_len)
2579 einfo (_("%P: warning: truncated CodeView type"
2580 " record LF_ENUMERATE\n"));
2584 subtype_len += param_len;
2587 name_len = strnlen ((char *) ptr + subtype_len,
2588 left - subtype_len);
2590 if (name_len == left - offsetof (struct lf_enumerate, name))
2592 einfo (_("%P: warning: name for LF_ENUMERATE has no"
2593 " terminating zero\n"));
2599 subtype_len += name_len;
2601 if (subtype_len % 4 != 0)
2602 subtype_len += 4 - (subtype_len % 4);
2604 if (left < subtype_len)
2606 einfo (_("%P: warning: truncated CodeView type record"
2607 " LF_ENUMERATE\n"));
2612 left -= subtype_len;
2619 struct lf_index *ind = (struct lf_index *) ptr;
2621 if (left < sizeof (struct lf_index))
2623 einfo (_("%P: warning: truncated CodeView type record"
2628 if (!remap_type (&ind->index, map, type_num, num_types))
2631 ptr += sizeof (struct lf_index);
2632 left -= sizeof (struct lf_index);
2639 struct lf_onemethod *meth = (struct lf_onemethod *) ptr;
2640 size_t name_len, subtype_len;
2642 if (left < offsetof (struct lf_onemethod, name))
2644 einfo (_("%P: warning: truncated CodeView type record"
2645 " LF_ONEMETHOD\n"));
2649 if (!remap_type (&meth->method_type, map, type_num,
2654 strnlen (meth->name,
2655 left - offsetof (struct lf_onemethod, name));
2657 if (name_len == left - offsetof (struct lf_onemethod, name))
2659 einfo (_("%P: warning: name for LF_ONEMETHOD has no"
2660 " terminating zero\n"));
2666 subtype_len = offsetof (struct lf_onemethod, name)
2669 if (subtype_len % 4 != 0)
2670 subtype_len += 4 - (subtype_len % 4);
2672 if (left < subtype_len)
2674 einfo (_("%P: warning: truncated CodeView type record"
2675 " LF_FIELDLIST\n"));
2680 left -= subtype_len;
2687 struct lf_method *meth = (struct lf_method *) ptr;
2688 size_t name_len, subtype_len;
2690 if (left < offsetof (struct lf_method, name))
2692 einfo (_("%P: warning: truncated CodeView type record"
2697 if (!remap_type (&meth->method_list, map, type_num,
2702 strnlen (meth->name,
2703 left - offsetof (struct lf_method, name));
2705 if (name_len == left - offsetof (struct lf_method, name))
2707 einfo (_("%P: warning: name for LF_METHOD has no"
2708 " terminating zero\n"));
2714 subtype_len = offsetof (struct lf_method, name) + name_len;
2716 if (subtype_len % 4 != 0)
2717 subtype_len += 4 - (subtype_len % 4);
2719 if (left < subtype_len)
2721 einfo (_("%P: warning: truncated CodeView type record"
2722 " LF_FIELDLIST\n"));
2727 left -= subtype_len;
2734 struct lf_bclass *bc = (struct lf_bclass *) ptr;
2738 if (left < sizeof (struct lf_bclass))
2740 einfo (_("%P: warning: truncated CodeView type record"
2745 if (!remap_type (&bc->base_class_type, map, type_num,
2749 subtype_len = sizeof (struct lf_bclass);
2751 offset = bfd_getl16 (&bc->offset);
2753 /* If offset >= 0x8000, actual value follows. */
2754 if (offset >= 0x8000)
2756 unsigned int param_len = extended_value_len (offset);
2760 einfo (_("%P: warning: unhandled type %v within"
2761 " LF_BCLASS\n"), offset);
2765 subtype_len += param_len;
2767 if (left < subtype_len)
2769 einfo (_("%P: warning: truncated CodeView type record"
2775 if (subtype_len % 4 != 0)
2776 subtype_len += 4 - (subtype_len % 4);
2778 if (left < subtype_len)
2780 einfo (_("%P: warning: truncated CodeView type record"
2786 left -= subtype_len;
2793 struct lf_vfunctab *vft = (struct lf_vfunctab *) ptr;
2795 if (left < sizeof (struct lf_vfunctab))
2797 einfo (_("%P: warning: truncated CodeView type record"
2802 if (!remap_type (&vft->type, map, type_num, num_types))
2805 ptr += sizeof (struct lf_vfunctab);
2806 left -= sizeof (struct lf_vfunctab);
2814 struct lf_vbclass *vbc = (struct lf_vbclass *) ptr;
2818 if (left < sizeof (struct lf_vbclass))
2820 einfo (_("%P: warning: truncated CodeView type record"
2821 " LF_VBCLASS/LF_IVBCLASS\n"));
2825 if (!remap_type (&vbc->base_class_type, map, type_num,
2829 if (!remap_type (&vbc->virtual_base_pointer_type, map,
2830 type_num, num_types))
2833 subtype_len = offsetof (struct lf_vbclass,
2834 virtual_base_vbtable_offset);
2836 offset = bfd_getl16 (&vbc->virtual_base_pointer_offset);
2838 /* If offset >= 0x8000, actual value follows. */
2839 if (offset >= 0x8000)
2841 unsigned int param_len = extended_value_len (offset);
2845 einfo (_("%P: warning: unhandled type %v within"
2846 " LF_VBCLASS/LF_IVBCLASS\n"), offset);
2850 subtype_len += param_len;
2852 if (left < subtype_len)
2854 einfo (_("%P: warning: truncated CodeView type record"
2855 " LF_VBCLASS/LF_IVBCLASS\n"));
2860 offset = bfd_getl16 ((char *)vbc + subtype_len);
2861 subtype_len += sizeof (uint16_t);
2863 /* If offset >= 0x8000, actual value follows. */
2864 if (offset >= 0x8000)
2866 unsigned int param_len = extended_value_len (offset);
2870 einfo (_("%P: warning: unhandled type %v within"
2871 " LF_VBCLASS/LF_IVBCLASS\n"), offset);
2875 subtype_len += param_len;
2877 if (left < subtype_len)
2879 einfo (_("%P: warning: truncated CodeView type record"
2880 " LF_VBCLASS/LF_IVBCLASS\n"));
2885 if (subtype_len % 4 != 0)
2886 subtype_len += 4 - (subtype_len % 4);
2888 if (left < subtype_len)
2890 einfo (_("%P: warning: truncated CodeView type record"
2891 " LF_VBCLASS/LF_IVBCLASS\n"));
2896 left -= subtype_len;
2903 struct lf_static_member *st =
2904 (struct lf_static_member *) ptr;
2905 size_t name_len, subtype_len;
2907 if (left < offsetof (struct lf_static_member, name))
2909 einfo (_("%P: warning: truncated CodeView type record"
2914 if (!remap_type (&st->type, map, type_num, num_types))
2919 left - offsetof (struct lf_static_member, name));
2921 if (name_len == left
2922 - offsetof (struct lf_static_member, name))
2924 einfo (_("%P: warning: name for LF_STMEMBER has no"
2925 " terminating zero\n"));
2931 subtype_len = offsetof (struct lf_static_member, name)
2934 if (subtype_len % 4 != 0)
2935 subtype_len += 4 - (subtype_len % 4);
2937 if (left < subtype_len)
2939 einfo (_("%P: warning: truncated CodeView type record"
2940 " LF_FIELDLIST\n"));
2945 left -= subtype_len;
2952 struct lf_nest_type *nest = (struct lf_nest_type *) ptr;
2953 size_t name_len, subtype_len;
2955 if (left < offsetof (struct lf_nest_type, name))
2957 einfo (_("%P: warning: truncated CodeView type record"
2962 if (!remap_type (&nest->type, map, type_num, num_types))
2966 strnlen (nest->name,
2967 left - offsetof (struct lf_nest_type, name));
2969 if (name_len == left - offsetof (struct lf_nest_type, name))
2971 einfo (_("%P: warning: name for LF_NESTTYPE has no"
2972 " terminating zero\n"));
2978 subtype_len = offsetof (struct lf_nest_type, name)
2981 if (subtype_len % 4 != 0)
2982 subtype_len += 4 - (subtype_len % 4);
2984 if (left < subtype_len)
2986 einfo (_("%P: warning: truncated CodeView type record"
2987 " LF_FIELDLIST\n"));
2992 left -= subtype_len;
2998 einfo (_("%P: warning: unrecognized CodeView subtype %v\n"),
3009 struct lf_bitfield *bf = (struct lf_bitfield *) data;
3011 if (size < offsetof (struct lf_bitfield, length))
3013 einfo (_("%P: warning: truncated CodeView type record"
3018 if (!remap_type (&bf->base_type, map, type_num, num_types))
3026 struct lf_methodlist *ml = (struct lf_methodlist *) data;
3027 unsigned int num_entries;
3029 if (size < offsetof (struct lf_methodlist, entries))
3031 einfo (_("%P: warning: truncated CodeView type record"
3032 " LF_METHODLIST\n"));
3036 if ((size - offsetof (struct lf_methodlist, entries))
3037 % sizeof (struct lf_methodlist_entry))
3039 einfo (_("%P: warning: malformed CodeView type record"
3040 " LF_METHODLIST\n"));
3044 num_entries = (size - offsetof (struct lf_methodlist, entries))
3045 / sizeof (struct lf_methodlist_entry);
3047 for (unsigned int i = 0; i < num_entries; i++)
3049 if (!remap_type (&ml->entries[i].method_type, map,
3050 type_num, num_types))
3059 struct lf_array *arr = (struct lf_array *) data;
3061 if (size < offsetof (struct lf_array, length_in_bytes))
3063 einfo (_("%P: warning: truncated CodeView type record"
3068 if (!remap_type (&arr->element_type, map, type_num, num_types))
3071 if (!remap_type (&arr->index_type, map, type_num, num_types))
3080 struct lf_class *cl = (struct lf_class *) data;
3081 uint16_t prop, num_bytes;
3082 size_t name_len, name_off;
3084 if (size < offsetof (struct lf_class, name))
3086 einfo (_("%P: warning: truncated CodeView type record"
3087 " LF_CLASS/LF_STRUCTURE\n"));
3091 if (!remap_type (&cl->field_list, map, type_num, num_types))
3094 if (!remap_type (&cl->derived_from, map, type_num, num_types))
3097 if (!remap_type (&cl->vshape, map, type_num, num_types))
3100 name_off = offsetof (struct lf_class, name);
3102 num_bytes = bfd_getl16 (&cl->length);
3104 /* If num_bytes >= 0x8000, actual value follows. */
3105 if (num_bytes >= 0x8000)
3107 unsigned int param_len = extended_value_len (num_bytes);
3111 einfo (_("%P: warning: unhandled type %v within"
3112 " LF_CLASS/LF_STRUCTURE\n"), num_bytes);
3116 name_off += param_len;
3118 if (size < name_off)
3120 einfo (_("%P: warning: truncated CodeView type record"
3121 " LF_CLASS/LF_STRUCTURE\n"));
3126 name_len = strnlen ((char *) cl + name_off, size - name_off);
3128 if (name_len == size - name_off)
3130 einfo (_("%P: warning: name for LF_CLASS/LF_STRUCTURE has no"
3131 " terminating zero\n"));
3135 prop = bfd_getl16 (&cl->properties);
3137 if (prop & CV_PROP_HAS_UNIQUE_NAME)
3139 /* Structure has another name following first one. */
3141 size_t len = name_off + name_len + 1;
3142 size_t unique_name_len;
3144 unique_name_len = strnlen ((char *) cl + name_off + name_len + 1,
3147 if (unique_name_len == size - len)
3149 einfo (_("%P: warning: unique name for LF_CLASS/LF_STRUCTURE"
3150 " has no terminating zero\n"));
3155 if (!(prop & (CV_PROP_FORWARD_REF | CV_PROP_SCOPED))
3156 && !is_name_anonymous ((char *) cl + name_off, name_len))
3159 cv_hash = crc32 ((uint8_t *) cl + name_off, name_len);
3167 struct lf_union *un = (struct lf_union *) data;
3168 uint16_t prop, num_bytes;
3169 size_t name_len, name_off;
3171 if (size < offsetof (struct lf_union, name))
3173 einfo (_("%P: warning: truncated CodeView type record"
3178 if (!remap_type (&un->field_list, map, type_num, num_types))
3181 name_off = offsetof (struct lf_union, name);
3183 num_bytes = bfd_getl16 (&un->length);
3185 /* If num_bytes >= 0x8000, actual value follows. */
3186 if (num_bytes >= 0x8000)
3188 unsigned int param_len = extended_value_len (num_bytes);
3192 einfo (_("%P: warning: unhandled type %v within"
3193 " LF_UNION\n"), num_bytes);
3197 name_off += param_len;
3199 if (size < name_off)
3201 einfo (_("%P: warning: truncated CodeView type record"
3207 name_len = strnlen ((char *) un + name_off, size - name_off);
3209 if (name_len == size - name_off)
3211 einfo (_("%P: warning: name for LF_UNION has no"
3212 " terminating zero\n"));
3216 prop = bfd_getl16 (&un->properties);
3218 if (prop & CV_PROP_HAS_UNIQUE_NAME)
3220 /* Structure has another name following first one. */
3222 size_t len = name_off + name_len + 1;
3223 size_t unique_name_len;
3225 unique_name_len = strnlen ((char *) un + name_off + name_len + 1,
3228 if (unique_name_len == size - len)
3230 einfo (_("%P: warning: unique name for LF_UNION has"
3231 " no terminating zero\n"));
3236 if (!(prop & (CV_PROP_FORWARD_REF | CV_PROP_SCOPED))
3237 && !is_name_anonymous ((char *) un + name_off, name_len))
3240 cv_hash = crc32 ((uint8_t *) un + name_off, name_len);
3248 struct lf_enum *en = (struct lf_enum *) data;
3252 if (size < offsetof (struct lf_enum, name))
3254 einfo (_("%P: warning: truncated CodeView type record"
3259 if (!remap_type (&en->underlying_type, map, type_num, num_types))
3262 if (!remap_type (&en->field_list, map, type_num, num_types))
3265 name_len = strnlen (en->name, size - offsetof (struct lf_enum, name));
3267 if (name_len == size - offsetof (struct lf_enum, name))
3269 einfo (_("%P: warning: name for LF_ENUM has no"
3270 " terminating zero\n"));
3274 prop = bfd_getl16 (&en->properties);
3276 if (prop & CV_PROP_HAS_UNIQUE_NAME)
3278 /* Structure has another name following first one. */
3280 size_t len = offsetof (struct lf_enum, name) + name_len + 1;
3281 size_t unique_name_len;
3283 unique_name_len = strnlen (en->name + name_len + 1, size - len);
3285 if (unique_name_len == size - len)
3287 einfo (_("%P: warning: unique name for LF_ENUM has"
3288 " no terminating zero\n"));
3297 /* Does not reference any types, nothing to be done. */
3302 struct lf_string_id *str = (struct lf_string_id *) data;
3305 if (size < offsetof (struct lf_string_id, string))
3307 einfo (_("%P: warning: truncated CodeView type record"
3308 " LF_STRING_ID\n"));
3312 if (!remap_type (&str->substring, map, type_num, num_types))
3315 string_len = strnlen (str->string,
3316 size - offsetof (struct lf_string_id, string));
3318 if (string_len == size - offsetof (struct lf_string_id, string))
3320 einfo (_("%P: warning: string for LF_STRING_ID has no"
3321 " terminating zero\n"));
3330 case LF_SUBSTR_LIST:
3332 uint32_t num_entries;
3333 struct lf_arglist *ssl = (struct lf_arglist *) data;
3335 if (size < offsetof (struct lf_arglist, args))
3337 einfo (_("%P: warning: truncated CodeView type record"
3338 " LF_SUBSTR_LIST\n"));
3342 num_entries = bfd_getl32 (&ssl->num_entries);
3344 if (size < offsetof (struct lf_arglist, args)
3345 + (num_entries * sizeof (uint32_t)))
3347 einfo (_("%P: warning: truncated CodeView type record"
3348 " LF_SUBSTR_LIST\n"));
3352 for (uint32_t i = 0; i < num_entries; i++)
3354 if (!remap_type (&ssl->args[i], map, type_num, num_types))
3365 uint16_t num_entries;
3366 struct lf_build_info *bi = (struct lf_build_info *) data;
3368 if (size < offsetof (struct lf_build_info, strings))
3370 einfo (_("%P: warning: truncated CodeView type record"
3371 " LF_BUILDINFO\n"));
3375 num_entries = bfd_getl16 (&bi->count);
3377 if (size < offsetof (struct lf_build_info, strings)
3378 + (num_entries * sizeof (uint32_t)))
3380 einfo (_("%P: warning: truncated CodeView type record"
3381 " LF_BUILDINFO\n"));
3385 for (uint32_t i = 0; i < num_entries; i++)
3387 if (!remap_type (&bi->strings[i], map, type_num, num_types))
3398 struct lf_func_id *func = (struct lf_func_id *) data;
3401 if (size < offsetof (struct lf_func_id, name))
3403 einfo (_("%P: warning: truncated CodeView type record"
3408 if (!remap_type (&func->parent_scope, map, type_num, num_types))
3411 if (!remap_type (&func->function_type, map, type_num, num_types))
3414 name_len = strnlen (func->name,
3415 size - offsetof (struct lf_func_id, name));
3417 if (name_len == size - offsetof (struct lf_func_id, name))
3419 einfo (_("%P: warning: string for LF_FUNC_ID has no"
3420 " terminating zero\n"));
3431 struct lf_mfunc_id *mfunc = (struct lf_mfunc_id *) data;
3434 if (size < offsetof (struct lf_mfunc_id, name))
3436 einfo (_("%P: warning: truncated CodeView type record"
3441 if (!remap_type (&mfunc->parent_type, map, type_num, num_types))
3444 if (!remap_type (&mfunc->function_type, map, type_num, num_types))
3447 name_len = strnlen (mfunc->name,
3448 size - offsetof (struct lf_mfunc_id, name));
3450 if (name_len == size - offsetof (struct lf_mfunc_id, name))
3452 einfo (_("%P: warning: string for LF_MFUNC_ID has no"
3453 " terminating zero\n"));
3462 case LF_UDT_SRC_LINE:
3463 return handle_udt_src_line (data, size, map, type_num, num_types,
3464 ids, mod_num, strings);
3467 einfo (_("%P: warning: unrecognized CodeView type %v\n"), type);
3471 hash = iterative_hash (data, size, 0);
3473 t = ipi ? ids : types;
3475 slot = htab_find_slot_with_hash (t->hashmap, data, hash, INSERT);
3479 if (!*slot) /* new entry */
3481 struct type_entry *e;
3483 *slot = xmalloc (offsetof (struct type_entry, data) + size);
3485 e = (struct type_entry *) *slot;
3488 e->index = t->num_types;
3491 e->cv_hash = cv_hash;
3493 e->cv_hash = crc32 (data, size);
3495 e->has_udt_src_line = false;
3497 memcpy (e->data, data, size);
3510 else /* duplicate */
3512 map[type_num] = (struct type_entry *) *slot;
3518 /* Parse the .debug$T section of a module, and pass any type definitions
3519 found to handle_type. */
3521 handle_debugt_section (asection *s, bfd *mod, struct types *types,
3522 struct types *ids, uint16_t mod_num,
3523 struct string_table *strings,
3524 struct type_entry ***map, uint32_t *num_types)
3526 bfd_byte *data = NULL;
3530 if (!bfd_get_full_section_contents (mod, s, &data))
3536 if (bfd_getl32 (data) != CV_SIGNATURE_C13)
3542 off = sizeof (uint32_t);
3544 while (off + sizeof (uint16_t) <= s->size)
3548 size = bfd_getl16 (data + off);
3549 off += sizeof (uint16_t);
3551 if (size + off > s->size || size <= sizeof (uint16_t))
3554 bfd_set_error (bfd_error_bad_value);
3562 if (*num_types == 0)
3568 *map = xcalloc (*num_types, sizeof (struct type_entry *));
3570 off = sizeof (uint32_t);
3573 while (off + sizeof (uint16_t) <= s->size)
3577 size = bfd_getl16 (data + off);
3579 if (!handle_type (data + off, *map, type_num, *num_types, types, ids,
3584 bfd_set_error (bfd_error_bad_value);
3588 off += sizeof (uint16_t) + size;
3597 /* Return the CodeView constant for the selected architecture. */
3599 target_processor (bfd *abfd)
3601 if (abfd->arch_info->arch != bfd_arch_i386)
3604 if (abfd->arch_info->mach & bfd_mach_x86_64)
3607 return CV_CFL_80386;
3610 /* Create the symbols that go in "* Linker *", the dummy module created
3611 for the linker itself. */
3613 create_linker_symbols (bfd *abfd, uint8_t **syms, uint32_t *sym_byte_size,
3614 const char *pdb_name)
3617 struct objname *name;
3618 struct compile3 *comp;
3619 struct envblock *env;
3620 size_t padding1, padding2, env_size;
3621 char *cwdval, *exeval, *pdbval;
3623 /* extra NUL for padding */
3624 static const char linker_fn[] = "* Linker *\0";
3625 static const char linker_name[] = "GNU LD " VERSION;
3627 static const char cwd[] = "cwd";
3628 static const char exe[] = "exe";
3629 static const char pdb[] = "pdb";
3631 cwdval = getcwd (NULL, 0);
3634 einfo (_("%P: warning: unable to get working directory\n"));
3638 exeval = lrealpath (program_name);
3642 einfo (_("%P: warning: unable to get program name\n"));
3647 pdbval = lrealpath (pdb_name);
3651 einfo (_("%P: warning: unable to get full path to PDB\n"));
3657 *sym_byte_size += offsetof (struct objname, name) + sizeof (linker_fn);
3658 *sym_byte_size += offsetof (struct compile3, compiler) + sizeof (linker_name);
3660 if (*sym_byte_size % 4)
3661 padding1 = 4 - (*sym_byte_size % 4);
3665 *sym_byte_size += padding1;
3667 env_size = offsetof (struct envblock, strings);
3668 env_size += sizeof (cwd);
3669 env_size += strlen (cwdval) + 1;
3670 env_size += sizeof (exe);
3671 env_size += strlen (exeval) + 1;
3672 env_size += sizeof (pdb);
3673 env_size += strlen (pdbval) + 1;
3676 padding2 = 4 - (env_size % 4);
3680 env_size += padding2;
3682 *sym_byte_size += env_size;
3684 *syms = xmalloc (*sym_byte_size);
3687 /* Write S_OBJNAME */
3689 name = (struct objname *) ptr;
3690 bfd_putl16 (offsetof (struct objname, name)
3691 + sizeof (linker_fn) - sizeof (uint16_t), &name->size);
3692 bfd_putl16 (S_OBJNAME, &name->kind);
3693 bfd_putl32 (0, &name->signature);
3694 memcpy (name->name, linker_fn, sizeof (linker_fn));
3696 ptr += offsetof (struct objname, name) + sizeof (linker_fn);
3698 /* Write S_COMPILE3 */
3700 comp = (struct compile3 *) ptr;
3702 bfd_putl16 (offsetof (struct compile3, compiler) + sizeof (linker_name)
3703 + padding1 - sizeof (uint16_t), &comp->size);
3704 bfd_putl16 (S_COMPILE3, &comp->kind);
3705 bfd_putl32 (CV_CFL_LINK, &comp->flags);
3706 bfd_putl16 (target_processor (abfd), &comp->machine);
3707 bfd_putl16 (0, &comp->frontend_major);
3708 bfd_putl16 (0, &comp->frontend_minor);
3709 bfd_putl16 (0, &comp->frontend_build);
3710 bfd_putl16 (0, &comp->frontend_qfe);
3711 bfd_putl16 (0, &comp->backend_major);
3712 bfd_putl16 (0, &comp->backend_minor);
3713 bfd_putl16 (0, &comp->backend_build);
3714 bfd_putl16 (0, &comp->backend_qfe);
3715 memcpy (comp->compiler, linker_name, sizeof (linker_name));
3717 memset (comp->compiler + sizeof (linker_name), 0, padding1);
3719 ptr += offsetof (struct compile3, compiler) + sizeof (linker_name) + padding1;
3721 /* Write S_ENVBLOCK */
3723 env = (struct envblock *) ptr;
3725 bfd_putl16 (env_size - sizeof (uint16_t), &env->size);
3726 bfd_putl16 (S_ENVBLOCK, &env->kind);
3729 ptr += offsetof (struct envblock, strings);
3731 memcpy (ptr, cwd, sizeof (cwd));
3732 ptr += sizeof (cwd);
3733 memcpy (ptr, cwdval, strlen (cwdval) + 1);
3734 ptr += strlen (cwdval) + 1;
3736 memcpy (ptr, exe, sizeof (exe));
3737 ptr += sizeof (exe);
3738 memcpy (ptr, exeval, strlen (exeval) + 1);
3739 ptr += strlen (exeval) + 1;
3741 memcpy (ptr, pdb, sizeof (pdb));
3742 ptr += sizeof (pdb);
3743 memcpy (ptr, pdbval, strlen (pdbval) + 1);
3744 ptr += strlen (pdbval) + 1;
3746 /* Microsoft's LINK also includes "cmd", the command-line options passed
3747 to the linker, but unfortunately we don't have access to argc and argv
3750 memset (ptr, 0, padding2);
3759 /* Populate the module stream, which consists of the transformed .debug$S
3760 data for each object file. */
3762 populate_module_stream (bfd *stream, bfd *mod, uint32_t *sym_byte_size,
3763 struct string_table *strings,
3764 uint32_t *c13_info_size,
3765 struct mod_source_files *mod_source,
3766 bfd *abfd, struct types *types,
3767 struct types *ids, uint16_t mod_num,
3768 bfd *sym_rec_stream, struct globals *glob,
3769 const char *pdb_name)
3771 uint8_t int_buf[sizeof (uint32_t)];
3772 uint8_t *c13_info = NULL;
3773 uint8_t *syms = NULL;
3778 if (!strcmp (bfd_get_filename (mod), "dll stuff"))
3780 if (!create_linker_symbols (mod, &syms, sym_byte_size, pdb_name))
3785 struct type_entry **map = NULL;
3786 uint32_t num_types = 0;
3788 /* Process .debug$T section. */
3790 for (asection *s = mod->sections; s; s = s->next)
3792 if (!strcmp (s->name, ".debug$T") && s->size >= sizeof (uint32_t))
3794 if (!handle_debugt_section (s, mod, types, ids, mod_num, strings,
3797 free (mod_source->files);
3805 /* Process .debug$S section(s). */
3807 for (asection *s = mod->sections; s; s = s->next)
3809 if (!strcmp (s->name, ".debug$S") && s->size >= sizeof (uint32_t))
3811 if (!handle_debugs_section (s, mod, strings, &c13_info,
3812 c13_info_size, mod_source, abfd,
3813 &syms, sym_byte_size, map, num_types,
3814 sym_rec_stream, glob, mod_num))
3818 free (mod_source->files);
3828 /* Write the signature. */
3830 bfd_putl32 (CV_SIGNATURE_C13, int_buf);
3832 if (bfd_bwrite (int_buf, sizeof (uint32_t), stream) != sizeof (uint32_t))
3841 if (bfd_bwrite (syms, *sym_byte_size, stream) != *sym_byte_size)
3853 if (bfd_bwrite (c13_info, *c13_info_size, stream) != *c13_info_size)
3862 /* Write the global refs size. */
3864 bfd_putl32 (0, int_buf);
3866 if (bfd_bwrite (int_buf, sizeof (uint32_t), stream) != sizeof (uint32_t))
3872 /* Create the module info substream within the DBI. */
3874 create_module_info_substream (bfd *abfd, bfd *pdb, void **data,
3875 uint32_t *size, struct string_table *strings,
3876 struct source_files_info *source,
3877 struct types *types, struct types *ids,
3878 bfd *sym_rec_stream, struct globals *glob,
3879 const char *pdb_name)
3882 unsigned int mod_num;
3884 static const char linker_fn[] = "* Linker *";
3888 for (bfd *in = coff_data (abfd)->link_info->input_bfds; in;
3891 size_t len = sizeof (struct module_info);
3893 if (!strcmp (bfd_get_filename (in), "dll stuff"))
3895 len += sizeof (linker_fn); /* Object name. */
3896 len++; /* Empty module name. */
3898 else if (in->my_archive)
3900 char *name = lrealpath (bfd_get_filename (in));
3902 len += strlen (name) + 1; /* Object name. */
3906 name = lrealpath (bfd_get_filename (in->my_archive));
3908 len += strlen (name) + 1; /* Archive name. */
3914 char *name = lrealpath (bfd_get_filename (in));
3915 size_t name_len = strlen (name) + 1;
3917 len += name_len; /* Object name. */
3918 len += name_len; /* And again as the archive name. */
3924 len += 4 - (len % 4);
3928 source->mod_count++;
3931 *data = xmalloc (*size);
3935 source->mods = xmalloc (source->mod_count
3936 * sizeof (struct mod_source_files));
3937 memset (source->mods, 0,
3938 source->mod_count * sizeof (struct mod_source_files));
3942 for (bfd *in = coff_data (abfd)->link_info->input_bfds; in;
3945 struct module_info *mod = (struct module_info *) ptr;
3946 uint16_t stream_num;
3948 uint32_t sym_byte_size, c13_info_size;
3949 uint8_t *start = ptr;
3951 stream = add_stream (pdb, NULL, &stream_num);
3955 for (unsigned int i = 0; i < source->mod_count; i++)
3957 free (source->mods[i].files);
3960 free (source->mods);
3965 if (!populate_module_stream (stream, in, &sym_byte_size,
3966 strings, &c13_info_size,
3967 &source->mods[mod_num], abfd,
3968 types, ids, mod_num,
3969 sym_rec_stream, glob, pdb_name))
3971 for (unsigned int i = 0; i < source->mod_count; i++)
3973 free (source->mods[i].files);
3976 free (source->mods);
3981 bfd_putl32 (0, &mod->unused1);
3983 /* These are dummy values - MSVC copies the first section contribution
3984 entry here, but doesn't seem to use it for anything. */
3985 bfd_putl16 (0xffff, &mod->sc.section);
3986 bfd_putl16 (0, &mod->sc.padding1);
3987 bfd_putl32 (0, &mod->sc.offset);
3988 bfd_putl32 (0xffffffff, &mod->sc.size);
3989 bfd_putl32 (0, &mod->sc.characteristics);
3990 bfd_putl16 (0xffff, &mod->sc.module_index);
3991 bfd_putl16 (0, &mod->sc.padding2);
3992 bfd_putl32 (0, &mod->sc.data_crc);
3993 bfd_putl32 (0, &mod->sc.reloc_crc);
3995 bfd_putl16 (0, &mod->flags);
3996 bfd_putl16 (stream_num, &mod->module_sym_stream);
3997 bfd_putl32 (sizeof (uint32_t) + sym_byte_size, &mod->sym_byte_size);
3998 bfd_putl32 (0, &mod->c11_byte_size);
3999 bfd_putl32 (c13_info_size, &mod->c13_byte_size);
4000 bfd_putl16 (0, &mod->source_file_count);
4001 bfd_putl16 (0, &mod->padding);
4002 bfd_putl32 (0, &mod->unused2);
4003 bfd_putl32 (0, &mod->source_file_name_index);
4004 bfd_putl32 (0, &mod->pdb_file_path_name_index);
4006 ptr += sizeof (struct module_info);
4008 if (!strcmp (bfd_get_filename (in), "dll stuff"))
4011 memcpy (ptr, linker_fn, sizeof (linker_fn));
4012 ptr += sizeof (linker_fn);
4014 /* Empty module name. */
4018 else if (in->my_archive)
4020 char *name = lrealpath (bfd_get_filename (in));
4021 size_t name_len = strlen (name) + 1;
4024 memcpy (ptr, name, name_len);
4029 name = lrealpath (bfd_get_filename (in->my_archive));
4030 name_len = strlen (name) + 1;
4033 memcpy (ptr, name, name_len);
4040 char *name = lrealpath (bfd_get_filename (in));
4041 size_t name_len = strlen (name) + 1;
4044 memcpy (ptr, name, name_len);
4047 /* Object name again as archive name. */
4048 memcpy (ptr, name, name_len);
4054 /* Pad to next four-byte boundary. */
4056 if ((ptr - start) % 4)
4058 memset (ptr, 0, 4 - ((ptr - start) % 4));
4059 ptr += 4 - ((ptr - start) % 4);
4068 /* Return the index of a given output section. */
4070 find_section_number (bfd *abfd, asection *sect)
4074 for (asection *s = abfd->sections; s; s = s->next)
4079 /* Empty sections aren't output. */
4087 /* Create the substream which maps addresses in the image file to locations
4088 in the original object files. */
4090 create_section_contrib_substream (bfd *abfd, void **data, uint32_t *size)
4092 unsigned int num_sc = 0;
4093 struct section_contribution *sc;
4098 for (bfd *in = coff_data (abfd)->link_info->input_bfds; in;
4101 for (asection *s = in->sections; s; s = s->next)
4103 if (s->size == 0 || discarded_section (s))
4110 *size = sizeof (uint32_t) + (num_sc * sizeof (struct section_contribution));
4111 *data = xmalloc (*size);
4113 bfd_putl32 (SECTION_CONTRIB_VERSION_60, *data);
4115 /* Read characteristics of outputted sections. */
4117 sect_flags = xmalloc (sizeof (uint32_t) * abfd->section_count);
4119 offset = bfd_coff_filhsz (abfd) + bfd_coff_aoutsz (abfd);
4120 offset += offsetof (struct external_scnhdr, s_flags);
4122 for (unsigned int i = 0; i < abfd->section_count; i++)
4124 bfd_seek (abfd, offset, SEEK_SET);
4126 if (bfd_bread (sect_flags + (i * sizeof (uint32_t)), sizeof (uint32_t),
4127 abfd) != sizeof (uint32_t))
4134 offset += sizeof (struct external_scnhdr);
4138 (struct section_contribution *) ((uint8_t *) *data + sizeof (uint32_t));
4141 for (bfd *in = coff_data (abfd)->link_info->input_bfds; in;
4144 for (asection *s = in->sections; s; s = s->next)
4148 if (s->size == 0 || discarded_section (s))
4151 sect_num = find_section_number (abfd, s->output_section);
4153 memcpy (&sc->characteristics,
4154 sect_flags + ((sect_num - 1) * sizeof (uint32_t)),
4157 bfd_putl16 (sect_num, &sc->section);
4158 bfd_putl16 (0, &sc->padding1);
4159 bfd_putl32 (s->output_offset, &sc->offset);
4160 bfd_putl32 (s->size, &sc->size);
4161 bfd_putl16 (mod_index, &sc->module_index);
4162 bfd_putl16 (0, &sc->padding2);
4163 bfd_putl32 (0, &sc->data_crc);
4164 bfd_putl32 (0, &sc->reloc_crc);
4177 /* The source info substream lives within the DBI stream, and lists the
4178 source files for each object file (i.e. it's derived from the
4179 DEBUG_S_FILECHKSMS parts of the .debug$S sections). This is a bit
4180 superfluous, as the filenames are also available in the C13 parts of
4181 the module streams, but MSVC relies on it to work properly. */
4183 create_source_info_substream (void **data, uint32_t *size,
4184 struct source_files_info *source)
4186 uint16_t dedupe_source_files_count = 0;
4187 uint16_t source_files_count = 0;
4188 uint32_t strings_len = 0;
4191 /* Loop through the source files, marking unique filenames. The pointers
4192 here are for entries in the main string table, and so have already
4193 been deduplicated. */
4195 for (uint16_t i = 0; i < source->mod_count; i++)
4197 for (uint16_t j = 0; j < source->mods[i].files_count; j++)
4199 if (source->mods[i].files[j])
4201 if (source->mods[i].files[j]->source_file_offset == 0xffffffff)
4203 source->mods[i].files[j]->source_file_offset = strings_len;
4204 strings_len += source->mods[i].files[j]->len + 1;
4205 dedupe_source_files_count++;
4208 source_files_count++;
4213 *size = sizeof (uint16_t) + sizeof (uint16_t);
4214 *size += (sizeof (uint16_t) + sizeof (uint16_t)) * source->mod_count;
4215 *size += sizeof (uint32_t) * source_files_count;
4216 *size += strings_len;
4218 *data = xmalloc (*size);
4220 ptr = (uint8_t *) *data;
4222 /* Write header (module count and source file count). */
4224 bfd_putl16 (source->mod_count, ptr);
4225 ptr += sizeof (uint16_t);
4227 bfd_putl16 (dedupe_source_files_count, ptr);
4228 ptr += sizeof (uint16_t);
4230 /* Write "ModIndices". As the LLVM documentation puts it, "this array is
4231 present, but does not appear to be useful". */
4233 for (uint16_t i = 0; i < source->mod_count; i++)
4235 bfd_putl16 (i, ptr);
4236 ptr += sizeof (uint16_t);
4239 /* Write source file count for each module. */
4241 for (uint16_t i = 0; i < source->mod_count; i++)
4243 bfd_putl16 (source->mods[i].files_count, ptr);
4244 ptr += sizeof (uint16_t);
4247 /* For each module, write the offsets within the string table
4248 for each source file. */
4250 for (uint16_t i = 0; i < source->mod_count; i++)
4252 for (uint16_t j = 0; j < source->mods[i].files_count; j++)
4254 if (source->mods[i].files[j])
4256 bfd_putl32 (source->mods[i].files[j]->source_file_offset, ptr);
4257 ptr += sizeof (uint32_t);
4262 /* Write the string table. We set source_file_offset to a dummy value for
4263 each entry we write, so we don't write duplicate filenames. */
4265 for (uint16_t i = 0; i < source->mod_count; i++)
4267 for (uint16_t j = 0; j < source->mods[i].files_count; j++)
4269 if (source->mods[i].files[j]
4270 && source->mods[i].files[j]->source_file_offset != 0xffffffff)
4272 memcpy (ptr, source->mods[i].files[j]->s,
4273 source->mods[i].files[j]->len);
4274 ptr += source->mods[i].files[j]->len;
4279 source->mods[i].files[j]->source_file_offset = 0xffffffff;
4285 /* Used as parameter to qsort, to sort globals by hash. */
4287 global_compare_hash (const void *s1, const void *s2)
4289 const struct global *g1 = *(const struct global **) s1;
4290 const struct global *g2 = *(const struct global **) s2;
4292 if (g1->hash < g2->hash)
4294 if (g1->hash > g2->hash)
4300 /* Create the globals stream, which contains the unmangled symbol names. */
4302 create_globals_stream (bfd *pdb, struct globals *glob, uint16_t *stream_num)
4305 struct globals_hash_header h;
4306 uint32_t buckets_size, filled_buckets = 0;
4307 struct global **sorted = NULL;
4309 struct global *buckets[NUM_GLOBALS_HASH_BUCKETS];
4310 char int_buf[sizeof (uint32_t)];
4312 stream = add_stream (pdb, NULL, stream_num);
4316 memset (buckets, 0, sizeof (buckets));
4318 if (glob->num_entries > 0)
4322 /* Create an array of pointers, sorted by hash value. */
4324 sorted = xmalloc (sizeof (struct global *) * glob->num_entries);
4327 for (unsigned int i = 0; i < glob->num_entries; i++)
4333 qsort (sorted, glob->num_entries, sizeof (struct global *),
4334 global_compare_hash);
4336 /* Populate the buckets. */
4338 for (unsigned int i = 0; i < glob->num_entries; i++)
4340 if (!buckets[sorted[i]->hash])
4342 buckets[sorted[i]->hash] = sorted[i];
4346 sorted[i]->index = i;
4350 buckets_size = NUM_GLOBALS_HASH_BUCKETS / 8;
4351 buckets_size += sizeof (uint32_t);
4352 buckets_size += filled_buckets * sizeof (uint32_t);
4354 bfd_putl32 (GLOBALS_HASH_SIGNATURE, &h.signature);
4355 bfd_putl32 (GLOBALS_HASH_VERSION_70, &h.version);
4356 bfd_putl32 (glob->num_entries * sizeof (struct hash_record),
4358 bfd_putl32 (buckets_size, &h.buckets_size);
4360 if (bfd_bwrite (&h, sizeof (h), stream) != sizeof (h))
4363 /* Write hash entries, sorted by hash. */
4365 for (unsigned int i = 0; i < glob->num_entries; i++)
4367 struct hash_record hr;
4369 bfd_putl32 (sorted[i]->offset + 1, &hr.offset);
4370 bfd_putl32 (sorted[i]->refcount, &hr.reference);
4372 if (bfd_bwrite (&hr, sizeof (hr), stream) != sizeof (hr))
4376 /* Write the bitmap for filled and unfilled buckets. */
4378 for (unsigned int i = 0; i < NUM_GLOBALS_HASH_BUCKETS; i += 8)
4382 for (unsigned int j = 0; j < 8; j++)
4388 if (bfd_bwrite (&v, sizeof (v), stream) != sizeof (v))
4392 /* Add a 4-byte gap. */
4394 bfd_putl32 (0, int_buf);
4396 if (bfd_bwrite (int_buf, sizeof (uint32_t), stream) != sizeof (uint32_t))
4399 /* Write the bucket offsets. */
4401 for (unsigned int i = 0; i < NUM_GLOBALS_HASH_BUCKETS; i++)
4405 /* 0xc is size of internal hash_record structure in
4406 Microsoft's parser. */
4407 bfd_putl32 (buckets[i]->index * 0xc, int_buf);
4409 if (bfd_bwrite (int_buf, sizeof (uint32_t), stream) !=
4423 /* Hash an entry in the globals list. */
4425 hash_global_entry (const void *p)
4427 const struct global *g = (const struct global *) p;
4428 uint16_t len = bfd_getl16 (g->data);
4430 return iterative_hash (g->data, len, 0);
4433 /* Compare an entry in the globals list with a symbol. */
4435 eq_global_entry (const void *a, const void *b)
4437 const struct global *g = (const struct global *) a;
4438 uint16_t len1, len2;
4440 len1 = bfd_getl16 (g->data) + sizeof (uint16_t);
4441 len2 = bfd_getl16 (b) + sizeof (uint16_t);
4446 return !memcmp (g->data, b, len1);
4449 /* Stream 4 is the debug information (DBI) stream. */
4451 populate_dbi_stream (bfd *stream, bfd *abfd, bfd *pdb,
4452 uint16_t section_header_stream_num,
4453 uint16_t sym_rec_stream_num,
4454 uint16_t publics_stream_num,
4455 struct string_table *strings,
4456 struct types *types,
4458 bfd *sym_rec_stream, const char *pdb_name)
4460 struct pdb_dbi_stream_header h;
4461 struct optional_dbg_header opt;
4462 void *mod_info, *sc, *source_info;
4463 uint32_t mod_info_size, sc_size, source_info_size;
4464 struct source_files_info source;
4465 struct globals glob;
4466 uint16_t globals_stream_num;
4468 source.mod_count = 0;
4471 glob.num_entries = 0;
4475 glob.hashmap = htab_create_alloc (0, hash_global_entry,
4476 eq_global_entry, free, xcalloc, free);
4478 if (!create_module_info_substream (abfd, pdb, &mod_info, &mod_info_size,
4479 strings, &source, types, ids,
4480 sym_rec_stream, &glob, pdb_name))
4482 htab_delete (glob.hashmap);
4486 if (!create_globals_stream (pdb, &glob, &globals_stream_num))
4488 htab_delete (glob.hashmap);
4490 for (unsigned int i = 0; i < source.mod_count; i++)
4492 free (source.mods[i].files);
4500 htab_delete (glob.hashmap);
4502 if (!create_section_contrib_substream (abfd, &sc, &sc_size))
4504 for (unsigned int i = 0; i < source.mod_count; i++)
4506 free (source.mods[i].files);
4514 create_source_info_substream (&source_info, &source_info_size, &source);
4516 for (unsigned int i = 0; i < source.mod_count; i++)
4518 free (source.mods[i].files);
4522 bfd_putl32 (0xffffffff, &h.version_signature);
4523 bfd_putl32 (DBI_STREAM_VERSION_70, &h.version_header);
4524 bfd_putl32 (1, &h.age);
4525 bfd_putl16 (globals_stream_num, &h.global_stream_index);
4526 bfd_putl16 (0x8e1d, &h.build_number); // MSVC 14.29
4527 bfd_putl16 (publics_stream_num, &h.public_stream_index);
4528 bfd_putl16 (0, &h.pdb_dll_version);
4529 bfd_putl16 (sym_rec_stream_num, &h.sym_record_stream);
4530 bfd_putl16 (0, &h.pdb_dll_rbld);
4531 bfd_putl32 (mod_info_size, &h.mod_info_size);
4532 bfd_putl32 (sc_size, &h.section_contribution_size);
4533 bfd_putl32 (0, &h.section_map_size);
4534 bfd_putl32 (source_info_size, &h.source_info_size);
4535 bfd_putl32 (0, &h.type_server_map_size);
4536 bfd_putl32 (0, &h.mfc_type_server_index);
4537 bfd_putl32 (sizeof (opt), &h.optional_dbg_header_size);
4538 bfd_putl32 (0, &h.ec_substream_size);
4539 bfd_putl16 (0, &h.flags);
4540 bfd_putl16 (get_arch_number (abfd), &h.machine);
4541 bfd_putl32 (0, &h.padding);
4543 if (bfd_bwrite (&h, sizeof (h), stream) != sizeof (h))
4551 if (bfd_bwrite (mod_info, mod_info_size, stream) != mod_info_size)
4561 if (bfd_bwrite (sc, sc_size, stream) != sc_size)
4570 if (bfd_bwrite (source_info, source_info_size, stream) != source_info_size)
4578 bfd_putl16 (0xffff, &opt.fpo_stream);
4579 bfd_putl16 (0xffff, &opt.exception_stream);
4580 bfd_putl16 (0xffff, &opt.fixup_stream);
4581 bfd_putl16 (0xffff, &opt.omap_to_src_stream);
4582 bfd_putl16 (0xffff, &opt.omap_from_src_stream);
4583 bfd_putl16 (section_header_stream_num, &opt.section_header_stream);
4584 bfd_putl16 (0xffff, &opt.token_map_stream);
4585 bfd_putl16 (0xffff, &opt.xdata_stream);
4586 bfd_putl16 (0xffff, &opt.pdata_stream);
4587 bfd_putl16 (0xffff, &opt.new_fpo_stream);
4588 bfd_putl16 (0xffff, &opt.orig_section_header_stream);
4590 if (bfd_bwrite (&opt, sizeof (opt), stream) != sizeof (opt))
4596 /* Used as parameter to qsort, to sort publics by hash. */
4598 public_compare_hash (const void *s1, const void *s2)
4600 const struct public *p1 = *(const struct public **) s1;
4601 const struct public *p2 = *(const struct public **) s2;
4603 if (p1->hash < p2->hash)
4605 if (p1->hash > p2->hash)
4611 /* Used as parameter to qsort, to sort publics by address. */
4613 public_compare_addr (const void *s1, const void *s2)
4615 const struct public *p1 = *(const struct public **) s1;
4616 const struct public *p2 = *(const struct public **) s2;
4618 if (p1->section < p2->section)
4620 if (p1->section > p2->section)
4623 if (p1->address < p2->address)
4625 if (p1->address > p2->address)
4631 /* The publics stream is a hash map of S_PUB32 records, which are stored
4632 in the symbol record stream. Each S_PUB32 entry represents a symbol
4633 from the point of view of the linker: a section index, an offset within
4634 the section, and a mangled name. Compare with S_GDATA32 and S_GPROC32,
4635 which are the same thing but generated by the compiler. */
4637 populate_publics_stream (bfd *stream, bfd *abfd, bfd *sym_rec_stream)
4639 struct publics_header header;
4640 struct globals_hash_header hash_header;
4641 const unsigned int num_buckets = 4096;
4642 unsigned int num_entries = 0, filled_buckets = 0;
4643 unsigned int buckets_size, sym_hash_size;
4644 char int_buf[sizeof (uint32_t)];
4645 struct public *publics_head = NULL, *publics_tail = NULL;
4646 struct public **buckets;
4647 struct public **sorted = NULL;
4650 buckets = xmalloc (sizeof (struct public *) * num_buckets);
4651 memset (buckets, 0, sizeof (struct public *) * num_buckets);
4653 /* Loop through the global symbols in our input files, and write S_PUB32
4654 records in the symbol record stream for those that make it into the
4656 for (bfd *in = coff_data (abfd)->link_info->input_bfds; in;
4659 if (!in->outsymbols)
4662 for (unsigned int i = 0; i < in->symcount; i++)
4664 struct bfd_symbol *sym = in->outsymbols[i];
4666 if (sym->flags & BSF_GLOBAL)
4669 uint16_t record_length;
4670 const char *name = sym->name;
4671 size_t name_len = strlen (name);
4672 struct public *p = xmalloc (sizeof (struct public));
4673 unsigned int padding = 0;
4678 find_section_number (abfd, sym->section->output_section);
4684 p->offset = bfd_tell (sym_rec_stream);
4685 p->hash = calc_hash (name, name_len) % num_buckets;
4686 p->section = section;
4687 p->address = sym->section->output_offset + sym->value;
4689 record_length = sizeof (struct pubsym) + name_len + 1;
4691 if (record_length % 4)
4692 padding = 4 - (record_length % 4);
4694 /* Assume that all global symbols in executable sections
4696 if (sym->section->flags & SEC_CODE)
4697 flags = PUBSYM_FUNCTION;
4699 bfd_putl16 (record_length + padding - sizeof (uint16_t),
4701 bfd_putl16 (S_PUB32, &ps.record_type);
4702 bfd_putl32 (flags, &ps.flags);
4703 bfd_putl32 (p->address, &ps.offset);
4704 bfd_putl16 (p->section, &ps.section);
4706 if (bfd_bwrite (&ps, sizeof (struct pubsym), sym_rec_stream) !=
4707 sizeof (struct pubsym))
4710 if (bfd_bwrite (name, name_len + 1, sym_rec_stream) !=
4714 for (unsigned int j = 0; j < padding; j++)
4718 if (bfd_bwrite (&b, sizeof (uint8_t), sym_rec_stream) !=
4726 publics_tail->next = p;
4735 if (num_entries > 0)
4737 /* Create an array of pointers, sorted by hash value. */
4739 sorted = xmalloc (sizeof (struct public *) * num_entries);
4741 struct public *p = publics_head;
4742 for (unsigned int i = 0; i < num_entries; i++)
4748 qsort (sorted, num_entries, sizeof (struct public *),
4749 public_compare_hash);
4751 /* Populate the buckets. */
4753 for (unsigned int i = 0; i < num_entries; i++)
4755 if (!buckets[sorted[i]->hash])
4757 buckets[sorted[i]->hash] = sorted[i];
4761 sorted[i]->index = i;
4765 buckets_size = num_buckets / 8;
4766 buckets_size += sizeof (uint32_t);
4767 buckets_size += filled_buckets * sizeof (uint32_t);
4769 sym_hash_size = sizeof (hash_header);
4770 sym_hash_size += num_entries * sizeof (struct hash_record);
4771 sym_hash_size += buckets_size;
4773 /* Output the publics header. */
4775 bfd_putl32 (sym_hash_size, &header.sym_hash_size);
4776 bfd_putl32 (num_entries * sizeof (uint32_t), &header.addr_map_size);
4777 bfd_putl32 (0, &header.num_thunks);
4778 bfd_putl32 (0, &header.thunks_size);
4779 bfd_putl32 (0, &header.thunk_table);
4780 bfd_putl32 (0, &header.thunk_table_offset);
4781 bfd_putl32 (0, &header.num_sects);
4783 if (bfd_bwrite (&header, sizeof (header), stream) != sizeof (header))
4786 /* Output the global hash header. */
4788 bfd_putl32 (GLOBALS_HASH_SIGNATURE, &hash_header.signature);
4789 bfd_putl32 (GLOBALS_HASH_VERSION_70, &hash_header.version);
4790 bfd_putl32 (num_entries * sizeof (struct hash_record),
4791 &hash_header.entries_size);
4792 bfd_putl32 (buckets_size, &hash_header.buckets_size);
4794 if (bfd_bwrite (&hash_header, sizeof (hash_header), stream) !=
4795 sizeof (hash_header))
4798 /* Write the entries in hash order. */
4800 for (unsigned int i = 0; i < num_entries; i++)
4802 struct hash_record hr;
4804 bfd_putl32 (sorted[i]->offset + 1, &hr.offset);
4805 bfd_putl32 (1, &hr.reference);
4807 if (bfd_bwrite (&hr, sizeof (hr), stream) != sizeof (hr))
4811 /* Write the bitmap for filled and unfilled buckets. */
4813 for (unsigned int i = 0; i < num_buckets; i += 8)
4817 for (unsigned int j = 0; j < 8; j++)
4823 if (bfd_bwrite (&v, sizeof (v), stream) != sizeof (v))
4827 /* Add a 4-byte gap. */
4829 bfd_putl32 (0, int_buf);
4831 if (bfd_bwrite (int_buf, sizeof (uint32_t), stream) != sizeof (uint32_t))
4834 /* Write the bucket offsets. */
4836 for (unsigned int i = 0; i < num_buckets; i++)
4840 /* 0xc is size of internal hash_record structure in
4841 Microsoft's parser. */
4842 bfd_putl32 (buckets[i]->index * 0xc, int_buf);
4844 if (bfd_bwrite (int_buf, sizeof (uint32_t), stream) !=
4850 /* Write the address map: offsets into the symbol record stream of
4851 S_PUB32 records, ordered by address. */
4853 if (num_entries > 0)
4855 qsort (sorted, num_entries, sizeof (struct public *),
4856 public_compare_addr);
4858 for (unsigned int i = 0; i < num_entries; i++)
4860 bfd_putl32 (sorted[i]->offset, int_buf);
4862 if (bfd_bwrite (int_buf, sizeof (uint32_t), stream) !=
4873 while (publics_head)
4875 struct public *p = publics_head->next;
4877 free (publics_head);
4886 /* The section header stream contains a copy of the section headers
4887 from the PE file, in the same format. */
4889 create_section_header_stream (bfd *pdb, bfd *abfd, uint16_t *num)
4892 unsigned int section_count;
4897 stream = add_stream (pdb, NULL, num);
4901 section_count = abfd->section_count;
4903 /* Empty sections aren't output. */
4904 for (asection *sect = abfd->sections; sect; sect = sect->next)
4906 if (sect->size == 0)
4910 if (section_count == 0)
4913 /* Copy section table from output - it's already been written at this
4916 scn_base = bfd_coff_filhsz (abfd) + bfd_coff_aoutsz (abfd);
4918 bfd_seek (abfd, scn_base, SEEK_SET);
4920 len = section_count * sizeof (struct external_scnhdr);
4921 buf = xmalloc (len);
4923 if (bfd_bread (buf, len, abfd) != len)
4929 if (bfd_bwrite (buf, len, stream) != len)
4940 /* Populate the "/names" named stream, which contains the string table. */
4942 populate_names_stream (bfd *stream, struct string_table *strings)
4944 char int_buf[sizeof (uint32_t)];
4945 struct string_table_header h;
4946 uint32_t num_strings = 0, num_buckets;
4947 struct string **buckets;
4949 bfd_putl32 (STRING_TABLE_SIGNATURE, &h.signature);
4950 bfd_putl32 (STRING_TABLE_VERSION, &h.version);
4952 if (bfd_bwrite (&h, sizeof (h), stream) != sizeof (h))
4955 bfd_putl32 (strings->strings_len, int_buf);
4957 if (bfd_bwrite (int_buf, sizeof (uint32_t), stream) != sizeof (uint32_t))
4962 if (bfd_bwrite (int_buf, 1, stream) != 1)
4965 for (struct string *s = strings->strings_head; s; s = s->next)
4967 if (bfd_bwrite (s->s, s->len, stream) != s->len)
4970 if (bfd_bwrite (int_buf, 1, stream) != 1)
4976 num_buckets = num_strings * 2;
4978 buckets = xmalloc (sizeof (struct string *) * num_buckets);
4979 memset (buckets, 0, sizeof (struct string *) * num_buckets);
4981 for (struct string *s = strings->strings_head; s; s = s->next)
4983 uint32_t bucket_num = s->hash % num_buckets;
4985 while (buckets[bucket_num])
4989 if (bucket_num == num_buckets)
4993 buckets[bucket_num] = s;
4996 bfd_putl32 (num_buckets, int_buf);
4998 if (bfd_bwrite (int_buf, sizeof (uint32_t), stream) != sizeof (uint32_t))
5004 for (unsigned int i = 0; i < num_buckets; i++)
5007 bfd_putl32 (buckets[i]->offset, int_buf);
5009 bfd_putl32 (0, int_buf);
5011 if (bfd_bwrite (int_buf, sizeof (uint32_t), stream) !=
5021 bfd_putl32 (num_strings, int_buf);
5023 if (bfd_bwrite (int_buf, sizeof (uint32_t), stream) != sizeof (uint32_t))
5029 /* Calculate the hash of a type_entry. */
5031 hash_type_entry (const void *p)
5033 const struct type_entry *e = (const struct type_entry *) p;
5034 uint16_t size = bfd_getl16 (e->data) + sizeof (uint16_t);
5036 return iterative_hash (e->data, size, 0);
5039 /* Compare a type_entry with a type. */
5041 eq_type_entry (const void *a, const void *b)
5043 const struct type_entry *e = (const struct type_entry *) a;
5044 uint16_t size_a = bfd_getl16 (e->data);
5045 uint16_t size_b = bfd_getl16 (b);
5047 if (size_a != size_b)
5050 return memcmp (e->data + sizeof (uint16_t),
5051 (const uint8_t *) b + sizeof (uint16_t), size_a) == 0;
5054 /* Create a PDB debugging file for the PE image file abfd with the build ID
5055 guid, stored at pdb_name. */
5057 create_pdb_file (bfd *abfd, const char *pdb_name, const unsigned char *guid)
5061 bfd *info_stream, *dbi_stream, *names_stream, *sym_rec_stream,
5062 *publics_stream, *tpi_stream, *ipi_stream;
5063 uint16_t section_header_stream_num, sym_rec_stream_num, publics_stream_num;
5064 struct string_table strings;
5065 struct types types, ids;
5067 pdb = bfd_openw (pdb_name, "pdb");
5070 einfo (_("%P: warning: cannot create PDB file: %E\n"));
5074 strings.strings_head = NULL;
5075 strings.strings_tail = NULL;
5076 strings.strings_len = 1;
5077 strings.hashmap = htab_create_alloc (0, hash_string_table_entry,
5078 eq_string_table_entry, free,
5081 bfd_set_format (pdb, bfd_archive);
5083 if (!create_old_directory_stream (pdb))
5085 einfo (_("%P: warning: cannot create old directory stream "
5086 "in PDB file: %E\n"));
5090 info_stream = add_stream (pdb, NULL, NULL);
5094 einfo (_("%P: warning: cannot create info stream "
5095 "in PDB file: %E\n"));
5099 tpi_stream = add_stream (pdb, NULL, NULL);
5103 einfo (_("%P: warning: cannot create TPI stream "
5104 "in PDB file: %E\n"));
5108 dbi_stream = add_stream (pdb, NULL, NULL);
5112 einfo (_("%P: warning: cannot create DBI stream "
5113 "in PDB file: %E\n"));
5117 ipi_stream = add_stream (pdb, NULL, NULL);
5121 einfo (_("%P: warning: cannot create IPI stream "
5122 "in PDB file: %E\n"));
5126 names_stream = add_stream (pdb, "/names", NULL);
5130 einfo (_("%P: warning: cannot create /names stream "
5131 "in PDB file: %E\n"));
5135 sym_rec_stream = add_stream (pdb, NULL, &sym_rec_stream_num);
5137 if (!sym_rec_stream)
5139 einfo (_("%P: warning: cannot create symbol record stream "
5140 "in PDB file: %E\n"));
5144 publics_stream = add_stream (pdb, NULL, &publics_stream_num);
5146 if (!publics_stream)
5148 einfo (_("%P: warning: cannot create publics stream "
5149 "in PDB file: %E\n"));
5153 if (!create_section_header_stream (pdb, abfd, §ion_header_stream_num))
5155 einfo (_("%P: warning: cannot create section header stream "
5156 "in PDB file: %E\n"));
5160 types.num_types = 0;
5161 types.hashmap = htab_create_alloc (0, hash_type_entry, eq_type_entry,
5162 free, xcalloc, free);
5163 types.first = types.last = NULL;
5166 ids.hashmap = htab_create_alloc (0, hash_type_entry, eq_type_entry,
5167 free, xcalloc, free);
5168 ids.first = ids.last = NULL;
5170 if (!populate_dbi_stream (dbi_stream, abfd, pdb, section_header_stream_num,
5171 sym_rec_stream_num, publics_stream_num,
5172 &strings, &types, &ids, sym_rec_stream, pdb_name))
5174 einfo (_("%P: warning: cannot populate DBI stream "
5175 "in PDB file: %E\n"));
5176 htab_delete (types.hashmap);
5177 htab_delete (ids.hashmap);
5181 if (!populate_type_stream (pdb, tpi_stream, &types))
5183 einfo (_("%P: warning: cannot populate TPI stream "
5184 "in PDB file: %E\n"));
5185 htab_delete (types.hashmap);
5186 htab_delete (ids.hashmap);
5190 htab_delete (types.hashmap);
5192 if (!populate_type_stream (pdb, ipi_stream, &ids))
5194 einfo (_("%P: warning: cannot populate IPI stream "
5195 "in PDB file: %E\n"));
5196 htab_delete (ids.hashmap);
5200 htab_delete (ids.hashmap);
5202 add_string ("", 0, &strings);
5204 if (!populate_names_stream (names_stream, &strings))
5206 einfo (_("%P: warning: cannot populate names stream "
5207 "in PDB file: %E\n"));
5211 if (!populate_publics_stream (publics_stream, abfd, sym_rec_stream))
5213 einfo (_("%P: warning: cannot populate publics stream "
5214 "in PDB file: %E\n"));
5218 if (!populate_info_stream (pdb, info_stream, guid))
5220 einfo (_("%P: warning: cannot populate info stream "
5221 "in PDB file: %E\n"));
5230 htab_delete (strings.hashmap);