Add translations for various sub-directories
[binutils-gdb.git] / ld / pdb.c
blobd0d51c7a79ada908cc3c4b91d183d93c05fff3bb
1 /* Support for generating PDB CodeView debugging files.
2 Copyright (C) 2022-2025 Free Software Foundation, Inc.
4 This file is part of the GNU Binutils.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
21 #include "pdb.h"
22 #include "bfdlink.h"
23 #include "ld.h"
24 #include "ldmain.h"
25 #include "ldmisc.h"
26 #include "libbfd.h"
27 #include "libiberty.h"
28 #include "coff/i386.h"
29 #include "coff/external.h"
30 #include "coff/internal.h"
31 #include "coff/pe.h"
32 #include "libcoff.h"
33 #include <time.h>
35 struct public
37 struct public *next;
38 uint32_t offset;
39 uint32_t hash;
40 unsigned int index;
41 uint16_t section;
42 uint32_t address;
45 struct string
47 struct string *next;
48 uint32_t hash;
49 uint32_t offset;
50 uint32_t source_file_offset;
51 size_t len;
52 char s[];
55 struct string_table
57 struct string *strings_head;
58 struct string *strings_tail;
59 uint32_t strings_len;
60 htab_t hashmap;
63 struct mod_source_files
65 uint16_t files_count;
66 struct string **files;
69 struct source_files_info
71 uint16_t mod_count;
72 struct mod_source_files *mods;
75 struct type_entry
77 struct type_entry *next;
78 uint32_t index;
79 uint32_t cv_hash;
80 bool has_udt_src_line;
81 uint8_t data[];
84 struct types
86 htab_t hashmap;
87 uint32_t num_types;
88 struct type_entry *first;
89 struct type_entry *last;
92 struct global
94 struct global *next;
95 uint32_t offset;
96 uint32_t hash;
97 uint32_t refcount;
98 unsigned int index;
99 uint8_t data[];
102 struct globals
104 uint32_t num_entries;
105 struct global *first;
106 struct global *last;
107 htab_t hashmap;
110 struct in_sc
112 asection *s;
113 uint16_t sect_num;
114 uint16_t mod_index;
117 static const uint32_t crc_table[] =
119 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f,
120 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988,
121 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2,
122 0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7,
123 0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9,
124 0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172,
125 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, 0x42b2986c,
126 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59,
127 0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423,
128 0xcfba9599, 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924,
129 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, 0x01db7106,
130 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433,
131 0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d,
132 0x91646c97, 0xe6635c01, 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e,
133 0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950,
134 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65,
135 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7,
136 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0,
137 0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa,
138 0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
139 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81,
140 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a,
141 0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683, 0xe3630b12, 0x94643b84,
142 0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1,
143 0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb,
144 0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc,
145 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 0xd6d6a3e8, 0xa1d1937e,
146 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b,
147 0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55,
148 0x316e8eef, 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236,
149 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, 0xb2bd0b28,
150 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d,
151 0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f,
152 0x72076785, 0x05005713, 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38,
153 0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242,
154 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777,
155 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69,
156 0x616bffd3, 0x166ccf45, 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2,
157 0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc,
158 0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
159 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693,
160 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94,
161 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d
164 static bool remap_type (void *data, struct type_entry **map,
165 uint32_t type_num, uint32_t num_types);
167 /* Add a new stream to the PDB archive, and return its BFD. */
168 static bfd *
169 add_stream (bfd *pdb, const char *name, uint16_t *stream_num)
171 bfd *stream;
172 uint16_t num;
174 stream = bfd_create (name ? name : "", pdb);
175 if (!stream)
176 return NULL;
178 if (!bfd_make_writable (stream))
180 bfd_close (stream);
181 return NULL;
184 if (!pdb->archive_head)
186 bfd_set_archive_head (pdb, stream);
187 num = 0;
189 else
191 bfd *b = pdb->archive_head;
193 num = 1;
195 while (b->archive_next)
197 num++;
198 b = b->archive_next;
201 b->archive_next = stream;
204 if (stream_num)
205 *stream_num = num;
207 return stream;
210 /* Stream 0 ought to be a copy of the MSF directory from the last
211 time the PDB file was written. Because we don't do incremental
212 writes this isn't applicable to us, but we fill it with a dummy
213 value so as not to confuse radare. */
214 static bool
215 create_old_directory_stream (bfd *pdb)
217 bfd *stream;
218 char buf[sizeof (uint32_t)];
220 stream = add_stream (pdb, NULL, NULL);
221 if (!stream)
222 return false;
224 bfd_putl32 (0, buf);
226 return bfd_write (buf, sizeof (uint32_t), stream) == sizeof (uint32_t);
229 /* Calculate the hash of a given string. */
230 static uint32_t
231 calc_hash (const char *data, size_t len)
233 uint32_t hash = 0;
235 while (len >= 4)
237 hash ^= data[0];
238 hash ^= data[1] << 8;
239 hash ^= data[2] << 16;
240 hash ^= data[3] << 24;
242 data += 4;
243 len -= 4;
246 if (len >= 2)
248 hash ^= data[0];
249 hash ^= data[1] << 8;
251 data += 2;
252 len -= 2;
255 if (len != 0)
256 hash ^= *data;
258 hash |= 0x20202020;
259 hash ^= (hash >> 11);
261 return hash ^ (hash >> 16);
264 /* Stream 1 is the PDB info stream - see
265 https://llvm.org/docs/PDB/PdbStream.html. */
266 static bool
267 populate_info_stream (bfd *pdb, bfd *info_stream, const unsigned char *guid)
269 bool ret = false;
270 struct pdb_stream_70 h;
271 uint32_t num_entries, num_buckets;
272 uint32_t names_length, stream_num;
273 char int_buf[sizeof (uint32_t)];
275 struct hash_entry
277 uint32_t offset;
278 uint32_t value;
281 struct hash_entry **buckets = NULL;
283 /* Write header. */
285 bfd_putl32 (PDB_STREAM_VERSION_VC70, &h.version);
286 bfd_putl32 (time (NULL), &h.signature);
287 bfd_putl32 (1, &h.age);
289 bfd_putl32 (bfd_getb32 (guid), h.guid);
290 bfd_putl16 (bfd_getb16 (&guid[4]), &h.guid[4]);
291 bfd_putl16 (bfd_getb16 (&guid[6]), &h.guid[6]);
292 memcpy (&h.guid[8], &guid[8], 8);
294 if (bfd_write (&h, sizeof (h), info_stream) != sizeof (h))
295 return false;
297 /* Write hash list of named streams. This is a "rollover" hash, i.e.
298 if a bucket is filled an entry gets placed in the next free
299 slot. */
301 num_entries = 0;
302 for (bfd *b = pdb->archive_head; b; b = b->archive_next)
304 if (strcmp (b->filename, ""))
305 num_entries++;
308 num_buckets = num_entries * 2;
310 names_length = 0;
311 stream_num = 0;
313 if (num_buckets > 0)
315 buckets = xmalloc (sizeof (struct hash_entry *) * num_buckets);
316 memset (buckets, 0, sizeof (struct hash_entry *) * num_buckets);
318 for (bfd *b = pdb->archive_head; b; b = b->archive_next)
320 if (strcmp (b->filename, ""))
322 size_t len = strlen (b->filename);
323 uint32_t hash = (uint16_t) calc_hash (b->filename, len);
324 uint32_t bucket_num = hash % num_buckets;
326 while (buckets[bucket_num])
328 bucket_num++;
330 if (bucket_num == num_buckets)
331 bucket_num = 0;
334 buckets[bucket_num] = xmalloc (sizeof (struct hash_entry));
336 buckets[bucket_num]->offset = names_length;
337 buckets[bucket_num]->value = stream_num;
339 names_length += len + 1;
342 stream_num++;
346 /* Write the strings list - the hash keys are indexes into this. */
348 bfd_putl32 (names_length, int_buf);
350 if (bfd_write (int_buf, sizeof (uint32_t), info_stream) !=
351 sizeof (uint32_t))
352 goto end;
354 for (bfd *b = pdb->archive_head; b; b = b->archive_next)
356 if (!strcmp (b->filename, ""))
357 continue;
359 size_t len = strlen (b->filename) + 1;
361 if (bfd_write (b->filename, len, info_stream) != len)
362 goto end;
365 /* Write the number of entries and buckets. */
367 bfd_putl32 (num_entries, int_buf);
369 if (bfd_write (int_buf, sizeof (uint32_t), info_stream) !=
370 sizeof (uint32_t))
371 goto end;
373 bfd_putl32 (num_buckets, int_buf);
375 if (bfd_write (int_buf, sizeof (uint32_t), info_stream) !=
376 sizeof (uint32_t))
377 goto end;
379 /* Write the present bitmap. */
381 bfd_putl32 ((num_buckets + 31) / 32, int_buf);
383 if (bfd_write (int_buf, sizeof (uint32_t), info_stream) !=
384 sizeof (uint32_t))
385 goto end;
387 for (unsigned int i = 0; i < num_buckets; i += 32)
389 uint32_t v = 0;
391 for (unsigned int j = 0; j < 32; j++)
393 if (i + j >= num_buckets)
394 break;
396 if (buckets[i + j])
397 v |= 1 << j;
400 bfd_putl32 (v, int_buf);
402 if (bfd_write (int_buf, sizeof (uint32_t), info_stream) !=
403 sizeof (uint32_t))
404 goto end;
407 /* Write the (empty) deleted bitmap. */
409 bfd_putl32 (0, int_buf);
411 if (bfd_write (int_buf, sizeof (uint32_t), info_stream) !=
412 sizeof (uint32_t))
413 goto end;
415 /* Write the buckets. */
417 for (unsigned int i = 0; i < num_buckets; i++)
419 if (buckets[i])
421 bfd_putl32 (buckets[i]->offset, int_buf);
423 if (bfd_write (int_buf, sizeof (uint32_t), info_stream) !=
424 sizeof (uint32_t))
425 goto end;
427 bfd_putl32 (buckets[i]->value, int_buf);
429 if (bfd_write (int_buf, sizeof (uint32_t), info_stream) !=
430 sizeof (uint32_t))
431 goto end;
435 bfd_putl32 (0, int_buf);
437 if (bfd_write (int_buf, sizeof (uint32_t), info_stream) !=
438 sizeof (uint32_t))
439 goto end;
441 bfd_putl32 (PDB_STREAM_VERSION_VC140, int_buf);
443 if (bfd_write (int_buf, sizeof (uint32_t), info_stream) !=
444 sizeof (uint32_t))
445 goto end;
447 ret = true;
449 end:
450 for (unsigned int i = 0; i < num_buckets; i++)
452 if (buckets[i])
453 free (buckets[i]);
456 free (buckets);
458 return ret;
461 /* Calculate the CRC32 used for type hashes. */
462 static uint32_t
463 crc32 (const uint8_t *data, size_t len)
465 uint32_t crc = 0;
467 while (len > 0)
469 crc = (crc >> 8) ^ crc_table[(crc & 0xff) ^ *data];
471 data++;
472 len--;
475 return crc;
478 /* Stream 2 is the type information (TPI) stream, and stream 4 is
479 the ID information (IPI) stream. They differ only in which records
480 go in which stream. */
481 static bool
482 populate_type_stream (bfd *pdb, bfd *stream, struct types *types)
484 struct pdb_tpi_stream_header h;
485 struct type_entry *e;
486 uint32_t len = 0, index_offset_len, off;
487 struct bfd *hash_stream = NULL;
488 uint16_t hash_stream_index;
490 static const uint32_t index_skip = 0x2000;
492 e = types->first;
494 index_offset_len = 0;
496 while (e)
498 uint32_t old_len = len;
500 len += sizeof (uint16_t) + bfd_getl16 (e->data);
502 if (old_len == 0 || old_len / index_skip != len / index_skip)
503 index_offset_len += sizeof (uint32_t) * 2;
505 e = e->next;
508 /* Each type stream also has a stream which holds the hash value for each
509 type, along with a skip list to speed up searching. */
511 hash_stream = add_stream (pdb, "", &hash_stream_index);
513 if (!hash_stream)
514 return false;
516 bfd_putl32 (TPI_STREAM_VERSION_80, &h.version);
517 bfd_putl32 (sizeof (h), &h.header_size);
518 bfd_putl32 (TPI_FIRST_INDEX, &h.type_index_begin);
519 bfd_putl32 (TPI_FIRST_INDEX + types->num_types, &h.type_index_end);
520 bfd_putl32 (len, &h.type_record_bytes);
521 bfd_putl16 (hash_stream_index, &h.hash_stream_index);
522 bfd_putl16 (0xffff, &h.hash_aux_stream_index);
523 bfd_putl32 (sizeof (uint32_t), &h.hash_key_size);
524 bfd_putl32 (NUM_TPI_HASH_BUCKETS, &h.num_hash_buckets);
525 bfd_putl32 (0, &h.hash_value_buffer_offset);
526 bfd_putl32 (types->num_types * sizeof (uint32_t),
527 &h.hash_value_buffer_length);
528 bfd_putl32 (types->num_types * sizeof (uint32_t),
529 &h.index_offset_buffer_offset);
530 bfd_putl32 (index_offset_len, &h.index_offset_buffer_length);
531 bfd_putl32 ((types->num_types * sizeof (uint32_t)) + index_offset_len,
532 &h.hash_adj_buffer_offset);
533 bfd_putl32 (0, &h.hash_adj_buffer_length);
535 if (bfd_write (&h, sizeof (h), stream) != sizeof (h))
536 return false;
538 /* Write the type definitions into the main stream, and the hashes
539 into the hash stream. The hashes have already been calculated
540 in handle_type. */
542 e = types->first;
544 while (e)
546 uint8_t buf[sizeof (uint32_t)];
547 uint16_t size;
549 size = bfd_getl16 (e->data);
551 if (bfd_write (e->data, size + sizeof (uint16_t), stream)
552 != size + sizeof (uint16_t))
553 return false;
555 bfd_putl32 (e->cv_hash % NUM_TPI_HASH_BUCKETS, buf);
557 if (bfd_write (buf, sizeof (uint32_t), hash_stream)
558 != sizeof (uint32_t))
559 return false;
561 e = e->next;
564 /* Write the index offsets, i.e. the skip list, into the hash stream. We
565 copy MSVC here by writing a new entry for every 8192 bytes. */
567 e = types->first;
568 off = 0;
570 while (e)
572 uint32_t old_off = off;
573 uint16_t size = bfd_getl16 (e->data);
575 off += size + sizeof (uint16_t);
577 if (old_off == 0 || old_off / index_skip != len / index_skip)
579 uint8_t buf[sizeof (uint32_t)];
581 bfd_putl32 (TPI_FIRST_INDEX + e->index, buf);
583 if (bfd_write (buf, sizeof (uint32_t), hash_stream)
584 != sizeof (uint32_t))
585 return false;
587 bfd_putl32 (old_off, buf);
589 if (bfd_write (buf, sizeof (uint32_t), hash_stream)
590 != sizeof (uint32_t))
591 return false;
594 e = e->next;
597 return true;
600 /* Return the PE architecture number for the image. */
601 static uint16_t
602 get_arch_number (bfd *abfd)
604 switch (abfd->arch_info->arch)
606 case bfd_arch_i386:
607 if (abfd->arch_info->mach & bfd_mach_x86_64)
608 return IMAGE_FILE_MACHINE_AMD64;
609 else
610 return IMAGE_FILE_MACHINE_I386;
612 case bfd_arch_aarch64:
613 return IMAGE_FILE_MACHINE_ARM64;
615 default:
616 return 0;
620 /* Validate the DEBUG_S_FILECHKSMS entry within a module's .debug$S
621 section, and copy it to the module's symbol stream. */
622 static bool
623 copy_filechksms (uint8_t *data, uint32_t size, char *string_table,
624 struct string_table *strings, uint8_t *out,
625 struct mod_source_files *mod_source)
627 uint8_t *orig_data = data;
628 uint32_t orig_size = size;
629 uint16_t num_files = 0;
630 struct string **strptr;
632 bfd_putl32 (DEBUG_S_FILECHKSMS, out);
633 out += sizeof (uint32_t);
635 bfd_putl32 (size, out);
636 out += sizeof (uint32_t);
638 /* Calculate the number of files, and check for any overflows. */
640 while (size > 0)
642 struct file_checksum *fc = (struct file_checksum *) data;
643 uint8_t padding;
644 size_t len;
646 if (size < sizeof (struct file_checksum))
648 bfd_set_error (bfd_error_bad_value);
649 return false;
652 len = sizeof (struct file_checksum) + fc->checksum_length;
654 if (size < len)
656 bfd_set_error (bfd_error_bad_value);
657 return false;
660 data += len;
661 size -= len;
663 if (len % sizeof (uint32_t))
664 padding = sizeof (uint32_t) - (len % sizeof (uint32_t));
665 else
666 padding = 0;
668 if (size < padding)
670 bfd_set_error (bfd_error_bad_value);
671 return false;
674 num_files++;
676 data += padding;
677 size -= padding;
680 /* Add the files to mod_source, so that they'll appear in the source
681 info substream. */
683 strptr = NULL;
684 if (num_files > 0)
686 uint16_t new_count = num_files + mod_source->files_count;
688 mod_source->files = xrealloc (mod_source->files,
689 sizeof (struct string *) * new_count);
691 strptr = mod_source->files + mod_source->files_count;
693 mod_source->files_count += num_files;
696 /* Actually copy the data. */
698 data = orig_data;
699 size = orig_size;
701 while (size > 0)
703 struct file_checksum *fc = (struct file_checksum *) data;
704 uint32_t string_off;
705 uint8_t padding;
706 size_t len;
707 struct string *str = NULL;
709 string_off = bfd_getl32 (&fc->file_id);
710 len = sizeof (struct file_checksum) + fc->checksum_length;
712 if (len % sizeof (uint32_t))
713 padding = sizeof (uint32_t) - (len % sizeof (uint32_t));
714 else
715 padding = 0;
717 /* Remap the "file ID", i.e. the offset in the module's string table,
718 so it points to the right place in the main string table. */
720 if (string_table)
722 char *fn = string_table + string_off;
723 size_t fn_len = strlen (fn);
724 uint32_t hash = calc_hash (fn, fn_len);
725 void **slot;
727 slot = htab_find_slot_with_hash (strings->hashmap, fn, hash,
728 NO_INSERT);
730 if (slot)
731 str = (struct string *) *slot;
734 *strptr = str;
735 strptr++;
737 bfd_putl32 (str ? str->offset : 0, &fc->file_id);
739 memcpy (out, data, len + padding);
741 data += len + padding;
742 size -= len + padding;
743 out += len + padding;
746 return true;
749 /* Add a string to the strings table, if it's not already there. Returns its
750 offset within the string table. */
751 static uint32_t
752 add_string (char *str, size_t len, struct string_table *strings)
754 uint32_t hash = calc_hash (str, len);
755 struct string *s;
756 void **slot;
758 slot = htab_find_slot_with_hash (strings->hashmap, str, hash, INSERT);
760 if (!*slot)
762 *slot = xmalloc (offsetof (struct string, s) + len);
764 s = (struct string *) *slot;
766 s->next = NULL;
767 s->hash = hash;
768 s->offset = strings->strings_len;
769 s->source_file_offset = 0xffffffff;
770 s->len = len;
771 memcpy (s->s, str, len);
773 if (strings->strings_tail)
774 strings->strings_tail->next = s;
775 else
776 strings->strings_head = s;
778 strings->strings_tail = s;
780 strings->strings_len += len + 1;
782 else
784 s = (struct string *) *slot;
787 return s->offset;
790 /* Return the hash of an entry in the string table. */
791 static hashval_t
792 hash_string_table_entry (const void *p)
794 const struct string *s = (const struct string *) p;
796 return s->hash;
799 /* Compare an entry in the string table with a string. */
800 static int
801 eq_string_table_entry (const void *a, const void *b)
803 const struct string *s1 = (const struct string *) a;
804 const char *s2 = (const char *) b;
805 size_t s2_len = strlen (s2);
807 if (s2_len != s1->len)
808 return 0;
810 return memcmp (s1->s, s2, s2_len) == 0;
813 /* Parse the string table within the .debug$S section. */
814 static void
815 parse_string_table (bfd_byte *data, size_t size,
816 struct string_table *strings)
818 while (true)
820 size_t len = strnlen ((char *) data, size);
822 add_string ((char *) data, len, strings);
824 data += len + 1;
826 if (size <= len + 1)
827 break;
829 size -= len + 1;
833 /* Remap a type reference within a CodeView symbol. */
834 static bool
835 remap_symbol_type (void *data, struct type_entry **map, uint32_t num_types)
837 uint32_t type = bfd_getl32 (data);
839 /* Ignore builtin types (those with IDs below 0x1000). */
840 if (type < TPI_FIRST_INDEX)
841 return true;
843 if (type >= TPI_FIRST_INDEX + num_types)
845 einfo (_("%P: CodeView symbol references out of range type %v\n"),
846 type);
847 return false;
850 type = TPI_FIRST_INDEX + map[type - TPI_FIRST_INDEX]->index;
851 bfd_putl32 (type, data);
853 return true;
856 /* Add an entry into the globals stream. If it already exists, increase
857 the refcount. */
858 static bool
859 add_globals_ref (struct globals *glob, bfd *sym_rec_stream, const char *name,
860 size_t name_len, uint8_t *data, size_t len)
862 void **slot;
863 uint32_t hash;
864 struct global *g;
866 slot = htab_find_slot_with_hash (glob->hashmap, data,
867 iterative_hash (data, len, 0), INSERT);
869 if (*slot)
871 g = *slot;
872 g->refcount++;
873 return true;
876 *slot = xmalloc (offsetof (struct global, data) + len);
878 hash = calc_hash (name, name_len);
879 hash %= NUM_GLOBALS_HASH_BUCKETS;
881 g = *slot;
882 g->next = NULL;
883 g->offset = bfd_tell (sym_rec_stream);
884 g->hash = hash;
885 g->refcount = 1;
886 memcpy (g->data, data, len);
888 glob->num_entries++;
890 if (glob->last)
891 glob->last->next = g;
892 else
893 glob->first = g;
895 glob->last = g;
897 return bfd_write (data, len, sym_rec_stream) == len;
900 /* Find the end of the current scope within symbols data. */
901 static uint8_t *
902 find_end_of_scope (uint8_t *data, uint32_t size)
904 unsigned int scope_level = 1;
905 uint16_t len;
907 len = bfd_getl16 (data) + sizeof (uint16_t);
909 data += len;
910 size -= len;
912 while (true)
914 uint16_t type;
916 if (size < sizeof (uint32_t))
917 return NULL;
919 len = bfd_getl16 (data) + sizeof (uint16_t);
920 type = bfd_getl16 (data + sizeof (uint16_t));
922 if (size < len)
923 return NULL;
925 switch (type)
927 case S_GPROC32:
928 case S_LPROC32:
929 case S_BLOCK32:
930 case S_INLINESITE:
931 case S_THUNK32:
932 scope_level++;
933 break;
935 case S_END:
936 case S_PROC_ID_END:
937 case S_INLINESITE_END:
938 scope_level--;
940 if (scope_level == 0)
941 return data;
943 break;
946 data += len;
947 size -= len;
951 /* Return the size of an extended value parameter, as used in
952 LF_ENUMERATE etc. */
953 static unsigned int
954 extended_value_len (uint16_t type)
956 switch (type)
958 case LF_CHAR:
959 return 1;
961 case LF_SHORT:
962 case LF_USHORT:
963 return 2;
965 case LF_LONG:
966 case LF_ULONG:
967 return 4;
969 case LF_QUADWORD:
970 case LF_UQUADWORD:
971 return 8;
974 return 0;
977 /* Parse the symbols in a .debug$S section, and copy them to the module's
978 symbol stream. */
979 static bool
980 parse_symbols (uint8_t *data, uint32_t size, uint8_t **buf,
981 struct type_entry **map, uint32_t num_types,
982 bfd *sym_rec_stream, struct globals *glob, uint16_t mod_num)
984 uint8_t *orig_buf = *buf;
985 unsigned int scope_level = 0;
986 uint8_t *scope = NULL;
988 while (size >= sizeof (uint16_t))
990 uint16_t len, type;
992 len = bfd_getl16 (data) + sizeof (uint16_t);
994 if (len > size)
996 bfd_set_error (bfd_error_bad_value);
997 return false;
1000 type = bfd_getl16 (data + sizeof (uint16_t));
1002 switch (type)
1004 case S_LDATA32:
1005 case S_GDATA32:
1006 case S_LTHREAD32:
1007 case S_GTHREAD32:
1009 struct datasym *d = (struct datasym *) data;
1010 size_t name_len;
1012 if (len < offsetof (struct datasym, name))
1014 einfo (_("%P: warning: truncated CodeView record"
1015 " S_LDATA32/S_GDATA32/S_LTHREAD32/S_GTHREAD32\n"));
1016 bfd_set_error (bfd_error_bad_value);
1017 return false;
1020 if (scope_level == 0)
1022 uint16_t section = bfd_getl16 (&d->section);
1024 if (section == 0) /* GC'd, ignore */
1025 break;
1028 name_len =
1029 strnlen (d->name, len - offsetof (struct datasym, name));
1031 if (name_len == len - offsetof (struct datasym, name))
1033 einfo (_("%P: warning: name for S_LDATA32/S_GDATA32/"
1034 "S_LTHREAD32/S_GTHREAD32 has no terminating"
1035 " zero\n"));
1036 bfd_set_error (bfd_error_bad_value);
1037 return false;
1040 if (!remap_symbol_type (&d->type, map, num_types))
1042 bfd_set_error (bfd_error_bad_value);
1043 return false;
1046 /* If S_LDATA32 or S_LTHREAD32, copy into module symbols. */
1048 if (type == S_LDATA32 || type == S_LTHREAD32)
1050 memcpy (*buf, d, len);
1051 *buf += len;
1054 /* S_LDATA32 and S_LTHREAD32 only go in globals if
1055 not in function scope. */
1056 if (type == S_GDATA32 || type == S_GTHREAD32 || scope_level == 0)
1058 if (!add_globals_ref (glob, sym_rec_stream, d->name,
1059 name_len, data, len))
1060 return false;
1063 break;
1066 case S_GPROC32:
1067 case S_LPROC32:
1068 case S_GPROC32_ID:
1069 case S_LPROC32_ID:
1071 struct procsym *proc = (struct procsym *) data;
1072 size_t name_len;
1073 uint16_t section;
1074 uint32_t end;
1075 uint8_t *endptr;
1076 size_t ref_size, padding;
1077 struct refsym *ref;
1079 if (len < offsetof (struct procsym, name))
1081 einfo (_("%P: warning: truncated CodeView record"
1082 " S_GPROC32/S_LPROC32\n"));
1083 bfd_set_error (bfd_error_bad_value);
1084 return false;
1087 section = bfd_getl16 (&proc->section);
1089 endptr = find_end_of_scope (data, size);
1091 if (!endptr)
1093 einfo (_("%P: warning: could not find end of"
1094 " S_GPROC32/S_LPROC32 record\n"));
1095 bfd_set_error (bfd_error_bad_value);
1096 return false;
1099 if (section == 0) /* skip if GC'd */
1101 /* Skip to after S_END. */
1103 size -= endptr - data;
1104 data = endptr;
1106 len = bfd_getl16 (data) + sizeof (uint16_t);
1108 data += len;
1109 size -= len;
1111 continue;
1114 name_len =
1115 strnlen (proc->name, len - offsetof (struct procsym, name));
1117 if (name_len == len - offsetof (struct procsym, name))
1119 einfo (_("%P: warning: name for S_GPROC32/S_LPROC32 has no"
1120 " terminating zero\n"));
1121 bfd_set_error (bfd_error_bad_value);
1122 return false;
1125 if (type == S_GPROC32_ID || type == S_LPROC32_ID)
1127 /* Transform into S_GPROC32 / S_LPROC32. */
1129 uint32_t t_idx = bfd_getl32 (&proc->type);
1130 struct type_entry *t;
1131 uint16_t t_type;
1133 if (t_idx < TPI_FIRST_INDEX
1134 || t_idx >= TPI_FIRST_INDEX + num_types)
1136 einfo (_("%P: CodeView symbol references out of range"
1137 " type %v\n"), type);
1138 bfd_set_error (bfd_error_bad_value);
1139 return false;
1142 t = map[t_idx - TPI_FIRST_INDEX];
1144 t_type = bfd_getl16 (t->data + sizeof (uint16_t));
1146 switch (t_type)
1148 case LF_FUNC_ID:
1150 struct lf_func_id *t_data =
1151 (struct lf_func_id *) t->data;
1153 /* Replace proc->type with function type. */
1155 memcpy (&proc->type, &t_data->function_type,
1156 sizeof (uint32_t));
1158 break;
1161 case LF_MFUNC_ID:
1163 struct lf_mfunc_id *t_data =
1164 (struct lf_mfunc_id *) t->data;
1166 /* Replace proc->type with function type. */
1168 memcpy (&proc->type, &t_data->function_type,
1169 sizeof (uint32_t));
1171 break;
1174 default:
1175 einfo (_("%P: CodeView S_GPROC32_ID/S_LPROC32_ID symbol"
1176 " referenced unknown type as ID\n"));
1177 bfd_set_error (bfd_error_bad_value);
1178 return false;
1181 /* Change record type. */
1183 if (type == S_GPROC32_ID)
1184 bfd_putl32 (S_GPROC32, &proc->kind);
1185 else
1186 bfd_putl32 (S_LPROC32, &proc->kind);
1188 else
1190 if (!remap_symbol_type (&proc->type, map, num_types))
1192 bfd_set_error (bfd_error_bad_value);
1193 return false;
1197 end = *buf - orig_buf + sizeof (uint32_t) + endptr - data;
1198 bfd_putl32 (end, &proc->end);
1200 /* Add S_PROCREF / S_LPROCREF to globals stream. */
1202 ref_size = offsetof (struct refsym, name) + name_len + 1;
1204 if (ref_size % sizeof (uint32_t))
1205 padding = sizeof (uint32_t) - (ref_size % sizeof (uint32_t));
1206 else
1207 padding = 0;
1209 ref = xmalloc (ref_size + padding);
1211 bfd_putl16 (ref_size + padding - sizeof (uint16_t), &ref->size);
1212 bfd_putl16 (type == S_GPROC32 || type == S_GPROC32_ID ?
1213 S_PROCREF : S_LPROCREF, &ref->kind);
1214 bfd_putl32 (0, &ref->sum_name);
1215 bfd_putl32 (*buf - orig_buf + sizeof (uint32_t),
1216 &ref->symbol_offset);
1217 bfd_putl16 (mod_num + 1, &ref->mod);
1219 memcpy (ref->name, proc->name, name_len + 1);
1221 memset (ref->name + name_len + 1, 0, padding);
1223 if (!add_globals_ref (glob, sym_rec_stream, proc->name, name_len,
1224 (uint8_t *) ref, ref_size + padding))
1226 free (ref);
1227 return false;
1230 free (ref);
1232 scope = *buf;
1234 memcpy (*buf, proc, len);
1235 *buf += len;
1237 scope_level++;
1239 break;
1242 case S_UDT:
1244 struct udtsym *udt = (struct udtsym *) data;
1245 size_t name_len;
1247 if (len < offsetof (struct udtsym, name))
1249 einfo (_("%P: warning: truncated CodeView record"
1250 " S_UDT\n"));
1251 bfd_set_error (bfd_error_bad_value);
1252 return false;
1255 name_len =
1256 strnlen (udt->name, len - offsetof (struct udtsym, name));
1258 if (name_len == len - offsetof (struct udtsym, name))
1260 einfo (_("%P: warning: name for S_UDT has no"
1261 " terminating zero\n"));
1262 bfd_set_error (bfd_error_bad_value);
1263 return false;
1266 if (!remap_symbol_type (&udt->type, map, num_types))
1268 bfd_set_error (bfd_error_bad_value);
1269 return false;
1272 /* S_UDT goes in the symbols stream if within a procedure,
1273 otherwise it goes in the globals stream. */
1274 if (scope_level == 0)
1276 if (!add_globals_ref (glob, sym_rec_stream, udt->name,
1277 name_len, data, len))
1278 return false;
1280 else
1282 memcpy (*buf, udt, len);
1283 *buf += len;
1286 break;
1289 case S_CONSTANT:
1291 struct constsym *c = (struct constsym *) data;
1292 size_t name_len, rec_size;
1293 uint16_t val;
1295 if (len < offsetof (struct constsym, name))
1297 einfo (_("%P: warning: truncated CodeView record"
1298 " S_CONSTANT\n"));
1299 bfd_set_error (bfd_error_bad_value);
1300 return false;
1303 rec_size = offsetof (struct constsym, name);
1305 val = bfd_getl16 (&c->value);
1307 /* If val >= 0x8000, actual value follows. */
1308 if (val >= 0x8000)
1310 unsigned int param_len = extended_value_len (val);
1312 if (param_len == 0)
1314 einfo (_("%P: warning: unhandled type %v within"
1315 " S_CONSTANT\n"), val);
1316 bfd_set_error (bfd_error_bad_value);
1317 return false;
1320 rec_size += param_len;
1323 name_len =
1324 strnlen ((const char *) data + rec_size, len - rec_size);
1326 if (name_len == len - rec_size)
1328 einfo (_("%P: warning: name for S_CONSTANT has no"
1329 " terminating zero\n"));
1330 bfd_set_error (bfd_error_bad_value);
1331 return false;
1334 if (!remap_symbol_type (&c->type, map, num_types))
1336 bfd_set_error (bfd_error_bad_value);
1337 return false;
1340 if (!add_globals_ref (glob, sym_rec_stream,
1341 (const char *) data + rec_size, name_len,
1342 data, len))
1343 return false;
1345 break;
1348 case S_END:
1349 case S_INLINESITE_END:
1350 case S_PROC_ID_END:
1351 memcpy (*buf, data, len);
1353 if (type == S_PROC_ID_END) /* transform to S_END */
1354 bfd_putl16 (S_END, *buf + sizeof (uint16_t));
1356 /* Reset scope variable back to the address of the previous
1357 scope start. */
1358 if (scope)
1360 uint32_t parent;
1361 uint16_t scope_start_type =
1362 bfd_getl16 (scope + sizeof (uint16_t));
1364 switch (scope_start_type)
1366 case S_GPROC32:
1367 case S_LPROC32:
1368 parent = bfd_getl32 (scope + offsetof (struct procsym,
1369 parent));
1370 break;
1372 case S_BLOCK32:
1373 parent = bfd_getl32 (scope + offsetof (struct blocksym,
1374 parent));
1375 break;
1377 case S_INLINESITE:
1378 parent = bfd_getl32 (scope + offsetof (struct inline_site,
1379 parent));
1380 break;
1382 case S_THUNK32:
1383 parent = bfd_getl32 (scope + offsetof (struct thunk,
1384 parent));
1385 break;
1387 default:
1388 einfo (_("%P: warning: unexpected CodeView scope start"
1389 " record %v\n"), scope_start_type);
1390 bfd_set_error (bfd_error_bad_value);
1391 return false;
1394 if (parent == 0)
1395 scope = NULL;
1396 else
1397 scope = orig_buf + parent - sizeof (uint32_t);
1400 *buf += len;
1401 scope_level--;
1402 break;
1404 case S_BUILDINFO:
1406 struct buildinfosym *bi = (struct buildinfosym *) data;
1408 if (len < sizeof (struct buildinfosym))
1410 einfo (_("%P: warning: truncated CodeView record"
1411 " S_BUILDINFO\n"));
1412 bfd_set_error (bfd_error_bad_value);
1413 return false;
1416 if (!remap_symbol_type (&bi->type, map, num_types))
1418 bfd_set_error (bfd_error_bad_value);
1419 return false;
1422 memcpy (*buf, data, len);
1423 *buf += len;
1425 break;
1428 case S_BLOCK32:
1430 struct blocksym *bl = (struct blocksym *) data;
1431 uint8_t *endptr;
1432 uint32_t end;
1434 if (len < offsetof (struct blocksym, name))
1436 einfo (_("%P: warning: truncated CodeView record"
1437 " S_BLOCK32\n"));
1438 bfd_set_error (bfd_error_bad_value);
1439 return false;
1442 bfd_putl32 (scope - orig_buf + sizeof (uint32_t), &bl->parent);
1444 endptr = find_end_of_scope (data, size);
1446 if (!endptr)
1448 einfo (_("%P: warning: could not find end of"
1449 " S_BLOCK32 record\n"));
1450 bfd_set_error (bfd_error_bad_value);
1451 return false;
1454 end = *buf - orig_buf + sizeof (uint32_t) + endptr - data;
1455 bfd_putl32 (end, &bl->end);
1457 scope = *buf;
1459 memcpy (*buf, data, len);
1460 *buf += len;
1462 scope_level++;
1464 break;
1467 case S_BPREL32:
1469 struct bprelsym *bp = (struct bprelsym *) data;
1471 if (len < offsetof (struct bprelsym, name))
1473 einfo (_("%P: warning: truncated CodeView record"
1474 " S_BPREL32\n"));
1475 bfd_set_error (bfd_error_bad_value);
1476 return false;
1479 if (!remap_symbol_type (&bp->type, map, num_types))
1481 bfd_set_error (bfd_error_bad_value);
1482 return false;
1485 memcpy (*buf, data, len);
1486 *buf += len;
1488 break;
1491 case S_REGISTER:
1493 struct regsym *reg = (struct regsym *) data;
1495 if (len < offsetof (struct regsym, name))
1497 einfo (_("%P: warning: truncated CodeView record"
1498 " S_REGISTER\n"));
1499 bfd_set_error (bfd_error_bad_value);
1500 return false;
1503 if (!remap_symbol_type (&reg->type, map, num_types))
1505 bfd_set_error (bfd_error_bad_value);
1506 return false;
1509 memcpy (*buf, data, len);
1510 *buf += len;
1512 break;
1515 case S_REGREL32:
1517 struct regrel *rr = (struct regrel *) data;
1519 if (len < offsetof (struct regrel, name))
1521 einfo (_("%P: warning: truncated CodeView record"
1522 " S_REGREL32\n"));
1523 bfd_set_error (bfd_error_bad_value);
1524 return false;
1527 if (!remap_symbol_type (&rr->type, map, num_types))
1529 bfd_set_error (bfd_error_bad_value);
1530 return false;
1533 memcpy (*buf, data, len);
1534 *buf += len;
1536 break;
1539 case S_LOCAL:
1541 struct localsym *l = (struct localsym *) data;
1543 if (len < offsetof (struct localsym, name))
1545 einfo (_("%P: warning: truncated CodeView record"
1546 " S_LOCAL\n"));
1547 bfd_set_error (bfd_error_bad_value);
1548 return false;
1551 if (!remap_symbol_type (&l->type, map, num_types))
1553 bfd_set_error (bfd_error_bad_value);
1554 return false;
1557 memcpy (*buf, data, len);
1558 *buf += len;
1560 break;
1563 case S_INLINESITE:
1565 struct inline_site *is = (struct inline_site *) data;
1566 uint8_t *endptr;
1567 uint32_t end;
1569 if (len < offsetof (struct inline_site, binary_annotations))
1571 einfo (_("%P: warning: truncated CodeView record"
1572 " S_INLINESITE\n"));
1573 bfd_set_error (bfd_error_bad_value);
1574 return false;
1577 bfd_putl32 (scope - orig_buf + sizeof (uint32_t), &is->parent);
1579 endptr = find_end_of_scope (data, size);
1581 if (!endptr)
1583 einfo (_("%P: warning: could not find end of"
1584 " S_INLINESITE record\n"));
1585 bfd_set_error (bfd_error_bad_value);
1586 return false;
1589 end = *buf - orig_buf + sizeof (uint32_t) + endptr - data;
1590 bfd_putl32 (end, &is->end);
1592 if (!remap_symbol_type (&is->inlinee, map, num_types))
1594 bfd_set_error (bfd_error_bad_value);
1595 return false;
1598 scope = *buf;
1600 memcpy (*buf, data, len);
1601 *buf += len;
1603 scope_level++;
1605 break;
1608 case S_THUNK32:
1610 struct thunk *th = (struct thunk *) data;
1611 uint8_t *endptr;
1612 uint32_t end;
1614 if (len < offsetof (struct thunk, name))
1616 einfo (_("%P: warning: truncated CodeView record"
1617 " S_THUNK32\n"));
1618 bfd_set_error (bfd_error_bad_value);
1619 return false;
1622 bfd_putl32 (scope - orig_buf + sizeof (uint32_t), &th->parent);
1624 endptr = find_end_of_scope (data, size);
1626 if (!endptr)
1628 einfo (_("%P: warning: could not find end of"
1629 " S_THUNK32 record\n"));
1630 bfd_set_error (bfd_error_bad_value);
1631 return false;
1634 end = *buf - orig_buf + sizeof (uint32_t) + endptr - data;
1635 bfd_putl32 (end, &th->end);
1637 scope = *buf;
1639 memcpy (*buf, data, len);
1640 *buf += len;
1642 scope_level++;
1644 break;
1647 case S_HEAPALLOCSITE:
1649 struct heap_alloc_site *has = (struct heap_alloc_site *) data;
1651 if (len < sizeof (struct heap_alloc_site))
1653 einfo (_("%P: warning: truncated CodeView record"
1654 " S_HEAPALLOCSITE\n"));
1655 bfd_set_error (bfd_error_bad_value);
1656 return false;
1659 if (!remap_symbol_type (&has->type, map, num_types))
1661 bfd_set_error (bfd_error_bad_value);
1662 return false;
1665 memcpy (*buf, data, len);
1666 *buf += len;
1668 break;
1671 case S_OBJNAME: /* just copy */
1672 case S_COMPILE3:
1673 case S_UNAMESPACE:
1674 case S_FRAMEPROC:
1675 case S_FRAMECOOKIE:
1676 case S_LABEL32:
1677 case S_DEFRANGE_REGISTER_REL:
1678 case S_DEFRANGE_FRAMEPOINTER_REL:
1679 case S_DEFRANGE_SUBFIELD_REGISTER:
1680 case S_DEFRANGE_FRAMEPOINTER_REL_FULL_SCOPE:
1681 case S_DEFRANGE_REGISTER:
1682 memcpy (*buf, data, len);
1683 *buf += len;
1684 break;
1686 default:
1687 einfo (_("%P: warning: unrecognized CodeView record %v\n"), type);
1688 bfd_set_error (bfd_error_bad_value);
1689 return false;
1692 data += len;
1693 size -= len;
1696 return true;
1699 /* For a given symbol subsection, work out how much space to allocate in the
1700 result module stream. This is different because we don't copy certain
1701 symbols, such as S_CONSTANT, and we skip over any procedures or data that
1702 have been GC'd out. */
1703 static bool
1704 calculate_symbols_size (uint8_t *data, uint32_t size, uint32_t *sym_size)
1706 unsigned int scope_level = 0;
1708 while (size >= sizeof (uint32_t))
1710 uint16_t len = bfd_getl16 (data) + sizeof (uint16_t);
1711 uint16_t type = bfd_getl16 (data + sizeof (uint16_t));
1713 switch (type)
1715 case S_LDATA32:
1716 case S_LTHREAD32:
1718 struct datasym *d = (struct datasym *) data;
1719 uint16_t section;
1721 if (len < offsetof (struct datasym, name))
1723 einfo (_("%P: warning: truncated CodeView record"
1724 " S_LDATA32/S_LTHREAD32\n"));
1725 return false;
1728 section = bfd_getl16 (&d->section);
1730 /* copy if not GC'd or within function */
1731 if (scope_level != 0 || section != 0)
1732 *sym_size += len;
1735 case S_GDATA32:
1736 case S_GTHREAD32:
1737 case S_CONSTANT:
1738 /* Not copied into symbols stream. */
1739 break;
1741 case S_GPROC32:
1742 case S_LPROC32:
1743 case S_GPROC32_ID:
1744 case S_LPROC32_ID:
1746 struct procsym *proc = (struct procsym *) data;
1747 uint16_t section;
1749 if (len < offsetof (struct procsym, name))
1751 einfo (_("%P: warning: truncated CodeView record"
1752 " S_GPROC32/S_LPROC32\n"));
1753 return false;
1756 section = bfd_getl16 (&proc->section);
1758 if (section != 0)
1760 *sym_size += len;
1762 else
1764 uint8_t *endptr = find_end_of_scope (data, size);
1766 if (!endptr)
1768 einfo (_("%P: warning: could not find end of"
1769 " S_GPROC32/S_LPROC32 record\n"));
1770 return false;
1773 /* Skip to after S_END. */
1775 size -= endptr - data;
1776 data = endptr;
1778 len = bfd_getl16 (data) + sizeof (uint16_t);
1780 data += len;
1781 size -= len;
1783 continue;
1786 scope_level++;
1788 break;
1791 case S_UDT:
1792 if (scope_level != 0) /* only goes in symbols if local */
1793 *sym_size += len;
1794 break;
1796 case S_BLOCK32: /* always copied */
1797 case S_INLINESITE:
1798 case S_THUNK32:
1799 *sym_size += len;
1800 scope_level++;
1801 break;
1803 case S_END: /* always copied */
1804 case S_PROC_ID_END:
1805 case S_INLINESITE_END:
1806 *sym_size += len;
1807 scope_level--;
1808 break;
1810 case S_OBJNAME: /* always copied */
1811 case S_COMPILE3:
1812 case S_UNAMESPACE:
1813 case S_FRAMEPROC:
1814 case S_FRAMECOOKIE:
1815 case S_LABEL32:
1816 case S_BUILDINFO:
1817 case S_BPREL32:
1818 case S_REGISTER:
1819 case S_REGREL32:
1820 case S_LOCAL:
1821 case S_DEFRANGE_REGISTER_REL:
1822 case S_DEFRANGE_FRAMEPOINTER_REL:
1823 case S_DEFRANGE_SUBFIELD_REGISTER:
1824 case S_DEFRANGE_FRAMEPOINTER_REL_FULL_SCOPE:
1825 case S_DEFRANGE_REGISTER:
1826 case S_HEAPALLOCSITE:
1827 *sym_size += len;
1828 break;
1830 default:
1831 einfo (_("%P: warning: unrecognized CodeView record %v\n"), type);
1832 return false;
1835 data += len;
1836 size -= len;
1839 return true;
1842 /* Parse the DEBUG_S_INLINEELINES data, which records the line numbers that
1843 correspond to inlined functions. This is similar to DEBUG_S_LINES (see
1844 handle_debugs_section), but rather than just copying we also need to remap
1845 the numbers of the referenced LF_FUNC_ID types. */
1847 static bool
1848 parse_inlinee_lines (uint8_t *data, uint32_t size, uint8_t **bufptr,
1849 struct type_entry **map, uint32_t num_types)
1851 uint32_t version;
1852 uint8_t *ptr;
1853 unsigned int num_entries;
1855 bfd_putl32 (DEBUG_S_INLINEELINES, *bufptr);
1856 *bufptr += sizeof (uint32_t);
1858 bfd_putl32 (size, *bufptr);
1859 *bufptr += sizeof (uint32_t);
1861 /* The inlinee lines data consists of a version uint32_t (0), followed by an
1862 array of struct inlinee_source_line:
1864 struct inlinee_source_line
1866 uint32_t function_id;
1867 uint32_t file_id;
1868 uint32_t line_no;
1871 (see InlineeSourceLine in cvinfo.h)
1873 We're only interested here in the function_id, as we need to remap its
1874 type number.
1877 if (size < sizeof (uint32_t))
1879 einfo (_("%P: warning: truncated DEBUG_S_INLINEELINES data\n"));
1880 return false;
1883 version = bfd_getl32 (data + sizeof (uint32_t) + sizeof (uint32_t));
1884 if (version != CV_INLINEE_SOURCE_LINE_SIGNATURE)
1886 einfo (_("%P: warning: unexpected DEBUG_S_INLINEELINES version %u\n"),
1887 version);
1888 return false;
1891 memcpy (*bufptr, data, size);
1892 ptr = *bufptr + sizeof (uint32_t);
1893 *bufptr += size;
1895 num_entries = (size - sizeof (uint32_t)) / (3 * sizeof (uint32_t));
1897 for (unsigned int i = 0; i < num_entries; i++)
1899 uint32_t func_id;
1901 func_id = bfd_getl32 (ptr);
1903 if (!remap_type (ptr, map, func_id, num_types))
1904 return false;
1906 ptr += 3 * sizeof (uint32_t);
1909 return true;
1912 /* Parse the .debug$S section within an object file. */
1913 static bool
1914 handle_debugs_section (asection *s, bfd *mod, struct string_table *strings,
1915 uint8_t **dataptr, uint32_t *sizeptr,
1916 struct mod_source_files *mod_source,
1917 bfd *abfd, uint8_t **syms, uint32_t *sym_byte_size,
1918 struct type_entry **map, uint32_t num_types,
1919 bfd *sym_rec_stream, struct globals *glob,
1920 uint16_t mod_num)
1922 bfd_byte *data = NULL;
1923 size_t off;
1924 uint32_t c13_size = 0;
1925 char *string_table = NULL;
1926 uint8_t *buf, *bufptr, *symbuf, *symbufptr;
1927 uint32_t sym_size = 0;
1929 if (!bfd_get_full_section_contents (mod, s, &data))
1930 return false;
1932 if (!data)
1933 return false;
1935 /* Resolve relocations. Addresses are stored within the .debug$S section as
1936 a .secidx, .secrel32 pair. */
1938 if (s->flags & SEC_RELOC)
1940 struct internal_reloc *relocs;
1941 struct internal_syment *symbols;
1942 asection **sectlist;
1943 unsigned int syment_count;
1944 int sect_num;
1945 struct external_syment *ext;
1947 syment_count = obj_raw_syment_count (mod);
1949 relocs =
1950 _bfd_coff_read_internal_relocs (mod, s, false, NULL, true, NULL);
1952 symbols = xmalloc (sizeof (struct internal_syment) * syment_count);
1953 sectlist = xmalloc (sizeof (asection *) * syment_count);
1955 ext = (struct external_syment *) (coff_data (mod)->external_syms);
1957 for (unsigned int i = 0; i < syment_count; i++)
1959 bfd_coff_swap_sym_in (mod, &ext[i], &symbols[i]);
1962 sect_num = 1;
1964 for (asection *sect = mod->sections; sect; sect = sect->next)
1966 for (unsigned int i = 0; i < syment_count; i++)
1968 if (symbols[i].n_scnum == sect_num)
1969 sectlist[i] = sect;
1972 sect_num++;
1975 if (!bfd_coff_relocate_section (abfd, coff_data (abfd)->link_info, mod,
1976 s, data, relocs, symbols, sectlist))
1978 free (sectlist);
1979 free (symbols);
1980 free (data);
1981 return false;
1984 free (sectlist);
1985 free (symbols);
1988 if (bfd_getl32 (data) != CV_SIGNATURE_C13)
1990 free (data);
1991 return true;
1994 off = sizeof (uint32_t);
1996 /* calculate size */
1998 while (off + sizeof (uint32_t) <= s->size)
2000 uint32_t type, size;
2002 type = bfd_getl32 (data + off);
2004 off += sizeof (uint32_t);
2006 if (off + sizeof (uint32_t) > s->size)
2008 free (data);
2009 bfd_set_error (bfd_error_bad_value);
2010 return false;
2013 size = bfd_getl32 (data + off);
2015 off += sizeof (uint32_t);
2017 if (off + size > s->size)
2019 free (data);
2020 bfd_set_error (bfd_error_bad_value);
2021 return false;
2024 switch (type)
2026 case DEBUG_S_FILECHKSMS:
2027 case DEBUG_S_INLINEELINES:
2028 c13_size += sizeof (uint32_t) + sizeof (uint32_t) + size;
2030 if (c13_size % sizeof (uint32_t))
2031 c13_size += sizeof (uint32_t) - (c13_size % sizeof (uint32_t));
2033 break;
2035 case DEBUG_S_STRINGTABLE:
2036 parse_string_table (data + off, size, strings);
2038 string_table = (char *) data + off;
2040 break;
2042 case DEBUG_S_LINES:
2044 uint16_t sect;
2046 if (size < sizeof (uint32_t) + sizeof (uint16_t))
2048 free (data);
2049 bfd_set_error (bfd_error_bad_value);
2050 return false;
2053 sect = bfd_getl16 (data + off + sizeof (uint32_t));
2055 /* Skip GC'd symbols. */
2056 if (sect != 0)
2058 c13_size += sizeof (uint32_t) + sizeof (uint32_t) + size;
2060 if (c13_size % sizeof (uint32_t))
2061 c13_size +=
2062 sizeof (uint32_t) - (c13_size % sizeof (uint32_t));
2065 break;
2068 case DEBUG_S_SYMBOLS:
2069 if (!calculate_symbols_size (data + off, size, &sym_size))
2071 free (data);
2072 bfd_set_error (bfd_error_bad_value);
2073 return false;
2076 break;
2079 off += size;
2081 if (off % sizeof (uint32_t))
2082 off += sizeof (uint32_t) - (off % sizeof (uint32_t));
2085 if (sym_size % sizeof (uint32_t))
2086 sym_size += sizeof (uint32_t) - (sym_size % sizeof (uint32_t));
2088 if (c13_size == 0 && sym_size == 0)
2090 free (data);
2091 return true;
2094 /* copy data */
2096 buf = NULL;
2097 if (c13_size != 0)
2098 buf = xmalloc (c13_size);
2099 bufptr = buf;
2101 symbuf = NULL;
2102 if (sym_size != 0)
2103 symbuf = xmalloc (sym_size);
2104 symbufptr = symbuf;
2106 off = sizeof (uint32_t);
2108 while (off + sizeof (uint32_t) <= s->size)
2110 uint32_t type, size;
2112 type = bfd_getl32 (data + off);
2113 off += sizeof (uint32_t);
2115 size = bfd_getl32 (data + off);
2116 off += sizeof (uint32_t);
2118 switch (type)
2120 case DEBUG_S_FILECHKSMS:
2121 if (!copy_filechksms (data + off, size, string_table,
2122 strings, bufptr, mod_source))
2124 free (data);
2125 free (symbuf);
2126 return false;
2129 bufptr += sizeof (uint32_t) + sizeof (uint32_t) + size;
2131 break;
2133 case DEBUG_S_LINES:
2135 uint16_t sect;
2137 sect = bfd_getl16 (data + off + sizeof (uint32_t));
2139 /* Skip if GC'd. */
2140 if (sect != 0)
2142 bfd_putl32 (type, bufptr);
2143 bufptr += sizeof (uint32_t);
2145 bfd_putl32 (size, bufptr);
2146 bufptr += sizeof (uint32_t);
2148 memcpy (bufptr, data + off, size);
2149 bufptr += size;
2152 break;
2155 case DEBUG_S_SYMBOLS:
2156 if (!parse_symbols (data + off, size, &symbufptr, map, num_types,
2157 sym_rec_stream, glob, mod_num))
2159 free (data);
2160 free (symbuf);
2161 return false;
2164 break;
2166 case DEBUG_S_INLINEELINES:
2167 if (!parse_inlinee_lines (data + off, size, &bufptr, map, num_types))
2169 free (data);
2170 free (symbuf);
2171 return false;
2174 break;
2177 off += size;
2179 if (off % sizeof (uint32_t))
2180 off += sizeof (uint32_t) - (off % sizeof (uint32_t));
2183 free (data);
2185 if (buf)
2187 if (*dataptr)
2189 /* Append the C13 info to what's already there, if the module has
2190 multiple .debug$S sections. */
2192 *dataptr = xrealloc (*dataptr, *sizeptr + c13_size);
2193 memcpy (*dataptr + *sizeptr, buf, c13_size);
2195 free (buf);
2197 else
2199 *dataptr = buf;
2202 *sizeptr += c13_size;
2205 if (symbuf)
2207 if (*syms)
2209 *syms = xrealloc (*syms, *sym_byte_size + sym_size);
2210 memcpy (*syms + *sym_byte_size, symbuf, sym_size);
2212 free (symbuf);
2214 else
2216 *syms = symbuf;
2219 *sym_byte_size += sym_size;
2222 return true;
2225 /* Remap the type number stored in data from the per-module numbering to
2226 that of the deduplicated output list. */
2227 static bool
2228 remap_type (void *data, struct type_entry **map,
2229 uint32_t type_num, uint32_t num_types)
2231 uint32_t type = bfd_getl32 (data);
2233 /* Ignore builtin types (those with IDs below 0x1000). */
2234 if (type < TPI_FIRST_INDEX)
2235 return true;
2237 if (type >= TPI_FIRST_INDEX + type_num)
2239 einfo (_("%P: CodeView type %v references other type %v not yet "
2240 "declared\n"), TPI_FIRST_INDEX + type_num, type);
2241 return false;
2244 if (type >= TPI_FIRST_INDEX + num_types)
2246 einfo (_("%P: CodeView type %v references out of range type %v\n"),
2247 TPI_FIRST_INDEX + type_num, type);
2248 return false;
2251 type = TPI_FIRST_INDEX + map[type - TPI_FIRST_INDEX]->index;
2252 bfd_putl32 (type, data);
2254 return true;
2257 /* Determines whether the name of a struct, class, or union counts as
2258 "anonymous". Non-anonymous types have a hash based on just the name,
2259 rather than the whole structure. */
2260 static bool
2261 is_name_anonymous (char *name, size_t len)
2263 static const char tag1[] = "<unnamed-tag>";
2264 static const char tag2[] = "__unnamed";
2265 static const char tag3[] = "::<unnamed-tag>";
2266 static const char tag4[] = "::__unnamed";
2268 if (len == sizeof (tag1) - 1 && !memcmp (name, tag1, sizeof (tag1) - 1))
2269 return true;
2271 if (len == sizeof (tag2) - 1 && !memcmp (name, tag2, sizeof (tag2) - 1))
2272 return true;
2274 if (len >= sizeof (tag3) - 1
2275 && !memcmp (name + len - sizeof (tag3) + 1, tag3, sizeof (tag3) - 1))
2276 return true;
2278 if (len >= sizeof (tag4) - 1
2279 && !memcmp (name + len - sizeof (tag4) + 1, tag4, sizeof (tag4) - 1))
2280 return true;
2282 return false;
2285 /* Handle LF_UDT_SRC_LINE type entries, which are a special case. These
2286 give the source file and line number for each user-defined type that is
2287 declared. We parse these and emit instead an LF_UDT_MOD_SRC_LINE entry,
2288 which also includes the module number. */
2289 static bool
2290 handle_udt_src_line (uint8_t *data, uint16_t size, struct type_entry **map,
2291 uint32_t type_num, uint32_t num_types,
2292 struct types *ids, uint16_t mod_num,
2293 struct string_table *strings)
2295 struct lf_udt_src_line *usl = (struct lf_udt_src_line *) data;
2296 uint32_t orig_type, source_file_type;
2297 void **slot;
2298 hashval_t hash;
2299 struct type_entry *e, *type_e, *str_e;
2300 struct lf_udt_mod_src_line *umsl;
2301 struct lf_string_id *str;
2302 uint32_t source_file_offset;
2304 if (size < sizeof (struct lf_udt_src_line))
2306 einfo (_("%P: warning: truncated CodeView type record"
2307 " LF_UDT_SRC_LINE\n"));
2308 return false;
2311 /* Check if LF_UDT_MOD_SRC_LINE already present for type, and return. */
2313 orig_type = bfd_getl32 (&usl->type);
2315 if (orig_type < TPI_FIRST_INDEX ||
2316 orig_type >= TPI_FIRST_INDEX + num_types ||
2317 !map[orig_type - TPI_FIRST_INDEX])
2319 einfo (_("%P: warning: CodeView type record LF_UDT_SRC_LINE"
2320 " referred to unknown type %v\n"), orig_type);
2321 return false;
2324 type_e = map[orig_type - TPI_FIRST_INDEX];
2326 /* Skip if type already declared in other module. */
2327 if (type_e->has_udt_src_line)
2328 return true;
2330 if (!remap_type (&usl->type, map, type_num, num_types))
2331 return false;
2333 /* Extract string from source_file_type. */
2335 source_file_type = bfd_getl32 (&usl->source_file_type);
2337 if (source_file_type < TPI_FIRST_INDEX ||
2338 source_file_type >= TPI_FIRST_INDEX + num_types ||
2339 !map[source_file_type - TPI_FIRST_INDEX])
2341 einfo (_("%P: warning: CodeView type record LF_UDT_SRC_LINE"
2342 " referred to unknown string %v\n"), source_file_type);
2343 return false;
2346 str_e = map[source_file_type - TPI_FIRST_INDEX];
2348 if (bfd_getl16 (str_e->data + sizeof (uint16_t)) != LF_STRING_ID)
2350 einfo (_("%P: warning: CodeView type record LF_UDT_SRC_LINE"
2351 " pointed to unexpected record type\n"));
2352 return false;
2355 str = (struct lf_string_id *) str_e->data;
2357 /* Add string to string table. */
2359 source_file_offset = add_string (str->string, strlen (str->string),
2360 strings);
2362 /* Add LF_UDT_MOD_SRC_LINE entry. */
2364 size = sizeof (struct lf_udt_mod_src_line);
2366 e = xmalloc (offsetof (struct type_entry, data) + size);
2368 e->next = NULL;
2369 e->index = ids->num_types;
2370 e->has_udt_src_line = false;
2372 /* LF_UDT_MOD_SRC_LINE use calc_hash on the type number, rather than
2373 the crc32 used for type hashes elsewhere. */
2374 e->cv_hash = calc_hash ((char *) &usl->type, sizeof (uint32_t));
2376 type_e->has_udt_src_line = true;
2378 umsl = (struct lf_udt_mod_src_line *) e->data;
2380 bfd_putl16 (size - sizeof (uint16_t), &umsl->size);
2381 bfd_putl16 (LF_UDT_MOD_SRC_LINE, &umsl->kind);
2382 memcpy (&umsl->type, &usl->type, sizeof (uint32_t));
2383 bfd_putl32 (source_file_offset, &umsl->source_file_string);
2384 memcpy (&umsl->line_no, &usl->line_no, sizeof (uint32_t));
2385 bfd_putl16 (mod_num + 1, &umsl->module_no);
2387 hash = iterative_hash (e->data, size, 0);
2389 slot = htab_find_slot_with_hash (ids->hashmap, data, hash, INSERT);
2390 if (!slot)
2392 free (e);
2393 return false;
2396 if (*slot)
2398 free (e);
2399 einfo (_("%P: warning: duplicate CodeView type record "
2400 "LF_UDT_MOD_SRC_LINE\n"));
2401 return false;
2404 *slot = e;
2406 if (ids->last)
2407 ids->last->next = e;
2408 else
2409 ids->first = e;
2411 ids->last = e;
2413 map[type_num] = e;
2415 ids->num_types++;
2417 return true;
2420 /* Parse a type definition in the .debug$T section. We remap the numbers
2421 of any referenced types, and if the type is not a duplicate of one
2422 already seen add it to types (for TPI types) or ids (for IPI types). */
2423 static bool
2424 handle_type (uint8_t *data, struct type_entry **map, uint32_t type_num,
2425 uint32_t num_types, struct types *types,
2426 struct types *ids, uint16_t mod_num,
2427 struct string_table *strings)
2429 uint16_t size, type;
2430 void **slot;
2431 hashval_t hash;
2432 bool other_hash = false;
2433 uint32_t cv_hash;
2434 struct types *t;
2435 bool ipi = false;
2437 size = bfd_getl16 (data) + sizeof (uint16_t);
2438 type = bfd_getl16 (data + sizeof (uint16_t));
2440 switch (type)
2442 case LF_MODIFIER:
2444 struct lf_modifier *mod = (struct lf_modifier *) data;
2446 if (size < offsetof (struct lf_modifier, modifier))
2448 einfo (_("%P: warning: truncated CodeView type record "
2449 "LF_MODIFIER\n"));
2450 return false;
2453 if (!remap_type (&mod->base_type, map, type_num, num_types))
2454 return false;
2456 break;
2459 case LF_POINTER:
2461 struct lf_pointer *ptr = (struct lf_pointer *) data;
2462 uint32_t attributes;
2464 if (size < offsetof (struct lf_pointer, attributes))
2466 einfo (_("%P: warning: truncated CodeView type record"
2467 " LF_POINTER\n"));
2468 return false;
2471 if (!remap_type (&ptr->base_type, map, type_num, num_types))
2472 return false;
2474 attributes = bfd_getl32 (&ptr->attributes);
2476 if ((attributes & CV_PTR_MODE_MASK) == CV_PTR_MODE_PMEM
2477 || (attributes & CV_PTR_MODE_MASK) == CV_PTR_MODE_PMFUNC)
2479 if (size < offsetof (struct lf_pointer, ptr_to_mem_type))
2481 einfo (_("%P: warning: truncated CodeView type record"
2482 " LF_POINTER\n"));
2483 return false;
2486 if (!remap_type (&ptr->containing_class, map, type_num, num_types))
2487 return false;
2490 break;
2493 case LF_PROCEDURE:
2495 struct lf_procedure *proc = (struct lf_procedure *) data;
2497 if (size < sizeof (struct lf_procedure))
2499 einfo (_("%P: warning: truncated CodeView type record"
2500 " LF_PROCEDURE\n"));
2501 return false;
2504 if (!remap_type (&proc->return_type, map, type_num, num_types))
2505 return false;
2507 if (!remap_type (&proc->arglist, map, type_num, num_types))
2508 return false;
2510 break;
2513 case LF_MFUNCTION:
2515 struct lf_mfunction *func = (struct lf_mfunction *) data;
2517 if (size < sizeof (struct lf_procedure))
2519 einfo (_("%P: warning: truncated CodeView type record"
2520 " LF_MFUNCTION\n"));
2521 return false;
2524 if (!remap_type (&func->return_type, map, type_num, num_types))
2525 return false;
2527 if (!remap_type (&func->containing_class_type, map, type_num,
2528 num_types))
2529 return false;
2531 if (!remap_type (&func->this_type, map, type_num, num_types))
2532 return false;
2534 if (!remap_type (&func->arglist, map, type_num, num_types))
2535 return false;
2537 break;
2540 case LF_ARGLIST:
2542 uint32_t num_entries;
2543 struct lf_arglist *al = (struct lf_arglist *) data;
2545 if (size < offsetof (struct lf_arglist, args))
2547 einfo (_("%P: warning: truncated CodeView type record"
2548 " LF_ARGLIST\n"));
2549 return false;
2552 num_entries = bfd_getl32 (&al->num_entries);
2554 if (size < offsetof (struct lf_arglist, args)
2555 + (num_entries * sizeof (uint32_t)))
2557 einfo (_("%P: warning: truncated CodeView type record"
2558 " LF_ARGLIST\n"));
2559 return false;
2562 for (uint32_t i = 0; i < num_entries; i++)
2564 if (!remap_type (&al->args[i], map, type_num, num_types))
2565 return false;
2568 break;
2571 case LF_FIELDLIST:
2573 uint16_t left = size - sizeof (uint16_t) - sizeof (uint16_t);
2574 uint8_t *ptr = data + sizeof (uint16_t) + sizeof (uint16_t);
2576 while (left > 0)
2578 uint16_t subtype;
2580 if (left < sizeof (uint16_t))
2582 einfo (_("%P: warning: truncated CodeView type record"
2583 " LF_FIELDLIST\n"));
2584 return false;
2587 subtype = bfd_getl16 (ptr);
2589 switch (subtype)
2591 case LF_MEMBER:
2593 struct lf_member *mem = (struct lf_member *) ptr;
2594 uint16_t offset;
2595 size_t name_len, subtype_len;
2597 if (left < offsetof (struct lf_member, name))
2599 einfo (_("%P: warning: truncated CodeView type record"
2600 " LF_MEMBER\n"));
2601 return false;
2604 if (!remap_type (&mem->type, map, type_num, num_types))
2605 return false;
2607 subtype_len = offsetof (struct lf_member, name);
2609 offset = bfd_getl16 (&mem->offset);
2611 /* If offset >= 0x8000, actual value follows. */
2612 if (offset >= 0x8000)
2614 unsigned int param_len = extended_value_len (offset);
2616 if (param_len == 0)
2618 einfo (_("%P: warning: unhandled type %v within"
2619 " LF_MEMBER\n"), offset);
2620 return false;
2623 subtype_len += param_len;
2625 if (left < subtype_len)
2627 einfo (_("%P: warning: truncated CodeView type record"
2628 " LF_MEMBER\n"));
2629 return false;
2633 name_len =
2634 strnlen ((char *) mem + subtype_len, left - subtype_len);
2636 if (name_len == left - offsetof (struct lf_member, name))
2638 einfo (_("%P: warning: name for LF_MEMBER has no"
2639 " terminating zero\n"));
2640 return false;
2643 name_len++;
2645 subtype_len += name_len;
2647 if (subtype_len % 4 != 0)
2648 subtype_len += 4 - (subtype_len % 4);
2650 if (left < subtype_len)
2652 einfo (_("%P: warning: truncated CodeView type record"
2653 " LF_FIELDLIST\n"));
2654 return false;
2657 ptr += subtype_len;
2658 left -= subtype_len;
2660 break;
2663 case LF_ENUMERATE:
2665 struct lf_enumerate *en = (struct lf_enumerate *) ptr;
2666 size_t name_len, subtype_len;
2667 uint16_t val;
2669 if (left < offsetof (struct lf_enumerate, name))
2671 einfo (_("%P: warning: truncated CodeView type record"
2672 " LF_ENUMERATE\n"));
2673 return false;
2676 subtype_len = offsetof (struct lf_enumerate, name);
2678 val = bfd_getl16 (&en->value);
2680 /* If val >= 0x8000, the actual value immediately follows. */
2681 if (val >= 0x8000)
2683 unsigned int param_len = extended_value_len (val);
2685 if (param_len == 0)
2687 einfo (_("%P: warning: unhandled type %v within"
2688 " LF_ENUMERATE\n"), val);
2689 return false;
2692 if (left < subtype_len + param_len)
2694 einfo (_("%P: warning: truncated CodeView type"
2695 " record LF_ENUMERATE\n"));
2696 return false;
2699 subtype_len += param_len;
2702 name_len = strnlen ((char *) ptr + subtype_len,
2703 left - subtype_len);
2705 if (name_len == left - offsetof (struct lf_enumerate, name))
2707 einfo (_("%P: warning: name for LF_ENUMERATE has no"
2708 " terminating zero\n"));
2709 return false;
2712 name_len++;
2714 subtype_len += 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_ENUMERATE\n"));
2723 return false;
2726 ptr += subtype_len;
2727 left -= subtype_len;
2729 break;
2732 case LF_INDEX:
2734 struct lf_index *ind = (struct lf_index *) ptr;
2736 if (left < sizeof (struct lf_index))
2738 einfo (_("%P: warning: truncated CodeView type record"
2739 " LF_INDEX\n"));
2740 return false;
2743 if (!remap_type (&ind->index, map, type_num, num_types))
2744 return false;
2746 ptr += sizeof (struct lf_index);
2747 left -= sizeof (struct lf_index);
2749 break;
2752 case LF_ONEMETHOD:
2754 struct lf_onemethod *meth = (struct lf_onemethod *) ptr;
2755 size_t name_len, subtype_len;
2757 if (left < offsetof (struct lf_onemethod, name))
2759 einfo (_("%P: warning: truncated CodeView type record"
2760 " LF_ONEMETHOD\n"));
2761 return false;
2764 if (!remap_type (&meth->method_type, map, type_num,
2765 num_types))
2766 return false;
2768 name_len =
2769 strnlen (meth->name,
2770 left - offsetof (struct lf_onemethod, name));
2772 if (name_len == left - offsetof (struct lf_onemethod, name))
2774 einfo (_("%P: warning: name for LF_ONEMETHOD has no"
2775 " terminating zero\n"));
2776 return false;
2779 name_len++;
2781 subtype_len = offsetof (struct lf_onemethod, name)
2782 + name_len;
2784 if (subtype_len % 4 != 0)
2785 subtype_len += 4 - (subtype_len % 4);
2787 if (left < subtype_len)
2789 einfo (_("%P: warning: truncated CodeView type record"
2790 " LF_FIELDLIST\n"));
2791 return false;
2794 ptr += subtype_len;
2795 left -= subtype_len;
2797 break;
2800 case LF_METHOD:
2802 struct lf_method *meth = (struct lf_method *) ptr;
2803 size_t name_len, subtype_len;
2805 if (left < offsetof (struct lf_method, name))
2807 einfo (_("%P: warning: truncated CodeView type record"
2808 " LF_METHOD\n"));
2809 return false;
2812 if (!remap_type (&meth->method_list, map, type_num,
2813 num_types))
2814 return false;
2816 name_len =
2817 strnlen (meth->name,
2818 left - offsetof (struct lf_method, name));
2820 if (name_len == left - offsetof (struct lf_method, name))
2822 einfo (_("%P: warning: name for LF_METHOD has no"
2823 " terminating zero\n"));
2824 return false;
2827 name_len++;
2829 subtype_len = offsetof (struct lf_method, name) + name_len;
2831 if (subtype_len % 4 != 0)
2832 subtype_len += 4 - (subtype_len % 4);
2834 if (left < subtype_len)
2836 einfo (_("%P: warning: truncated CodeView type record"
2837 " LF_FIELDLIST\n"));
2838 return false;
2841 ptr += subtype_len;
2842 left -= subtype_len;
2844 break;
2847 case LF_BCLASS:
2849 struct lf_bclass *bc = (struct lf_bclass *) ptr;
2850 size_t subtype_len;
2851 uint16_t offset;
2853 if (left < sizeof (struct lf_bclass))
2855 einfo (_("%P: warning: truncated CodeView type record"
2856 " LF_BCLASS\n"));
2857 return false;
2860 if (!remap_type (&bc->base_class_type, map, type_num,
2861 num_types))
2862 return false;
2864 subtype_len = sizeof (struct lf_bclass);
2866 offset = bfd_getl16 (&bc->offset);
2868 /* If offset >= 0x8000, actual value follows. */
2869 if (offset >= 0x8000)
2871 unsigned int param_len = extended_value_len (offset);
2873 if (param_len == 0)
2875 einfo (_("%P: warning: unhandled type %v within"
2876 " LF_BCLASS\n"), offset);
2877 return false;
2880 subtype_len += param_len;
2882 if (left < subtype_len)
2884 einfo (_("%P: warning: truncated CodeView type record"
2885 " LF_BCLASS\n"));
2886 return false;
2890 if (subtype_len % 4 != 0)
2891 subtype_len += 4 - (subtype_len % 4);
2893 if (left < subtype_len)
2895 einfo (_("%P: warning: truncated CodeView type record"
2896 " LF_BCLASS\n"));
2897 return false;
2900 ptr += subtype_len;
2901 left -= subtype_len;
2903 break;
2906 case LF_VFUNCTAB:
2908 struct lf_vfunctab *vft = (struct lf_vfunctab *) ptr;
2910 if (left < sizeof (struct lf_vfunctab))
2912 einfo (_("%P: warning: truncated CodeView type record"
2913 " LF_VFUNCTAB\n"));
2914 return false;
2917 if (!remap_type (&vft->type, map, type_num, num_types))
2918 return false;
2920 ptr += sizeof (struct lf_vfunctab);
2921 left -= sizeof (struct lf_vfunctab);
2923 break;
2926 case LF_VBCLASS:
2927 case LF_IVBCLASS:
2929 struct lf_vbclass *vbc = (struct lf_vbclass *) ptr;
2930 size_t subtype_len;
2931 uint16_t offset;
2933 if (left < sizeof (struct lf_vbclass))
2935 einfo (_("%P: warning: truncated CodeView type record"
2936 " LF_VBCLASS/LF_IVBCLASS\n"));
2937 return false;
2940 if (!remap_type (&vbc->base_class_type, map, type_num,
2941 num_types))
2942 return false;
2944 if (!remap_type (&vbc->virtual_base_pointer_type, map,
2945 type_num, num_types))
2946 return false;
2948 subtype_len = offsetof (struct lf_vbclass,
2949 virtual_base_vbtable_offset);
2951 offset = bfd_getl16 (&vbc->virtual_base_pointer_offset);
2953 /* If offset >= 0x8000, actual value follows. */
2954 if (offset >= 0x8000)
2956 unsigned int param_len = extended_value_len (offset);
2958 if (param_len == 0)
2960 einfo (_("%P: warning: unhandled type %v within"
2961 " LF_VBCLASS/LF_IVBCLASS\n"), offset);
2962 return false;
2965 subtype_len += param_len;
2967 if (left < subtype_len)
2969 einfo (_("%P: warning: truncated CodeView type record"
2970 " LF_VBCLASS/LF_IVBCLASS\n"));
2971 return false;
2975 offset = bfd_getl16 ((char *)vbc + subtype_len);
2976 subtype_len += sizeof (uint16_t);
2978 /* If offset >= 0x8000, actual value follows. */
2979 if (offset >= 0x8000)
2981 unsigned int param_len = extended_value_len (offset);
2983 if (param_len == 0)
2985 einfo (_("%P: warning: unhandled type %v within"
2986 " LF_VBCLASS/LF_IVBCLASS\n"), offset);
2987 return false;
2990 subtype_len += param_len;
2992 if (left < subtype_len)
2994 einfo (_("%P: warning: truncated CodeView type record"
2995 " LF_VBCLASS/LF_IVBCLASS\n"));
2996 return false;
3000 if (subtype_len % 4 != 0)
3001 subtype_len += 4 - (subtype_len % 4);
3003 if (left < subtype_len)
3005 einfo (_("%P: warning: truncated CodeView type record"
3006 " LF_VBCLASS/LF_IVBCLASS\n"));
3007 return false;
3010 ptr += subtype_len;
3011 left -= subtype_len;
3013 break;
3016 case LF_STMEMBER:
3018 struct lf_static_member *st =
3019 (struct lf_static_member *) ptr;
3020 size_t name_len, subtype_len;
3022 if (left < offsetof (struct lf_static_member, name))
3024 einfo (_("%P: warning: truncated CodeView type record"
3025 " LF_STMEMBER\n"));
3026 return false;
3029 if (!remap_type (&st->type, map, type_num, num_types))
3030 return false;
3032 name_len =
3033 strnlen (st->name,
3034 left - offsetof (struct lf_static_member, name));
3036 if (name_len == left
3037 - offsetof (struct lf_static_member, name))
3039 einfo (_("%P: warning: name for LF_STMEMBER has no"
3040 " terminating zero\n"));
3041 return false;
3044 name_len++;
3046 subtype_len = offsetof (struct lf_static_member, name)
3047 + name_len;
3049 if (subtype_len % 4 != 0)
3050 subtype_len += 4 - (subtype_len % 4);
3052 if (left < subtype_len)
3054 einfo (_("%P: warning: truncated CodeView type record"
3055 " LF_FIELDLIST\n"));
3056 return false;
3059 ptr += subtype_len;
3060 left -= subtype_len;
3062 break;
3065 case LF_NESTTYPE:
3067 struct lf_nest_type *nest = (struct lf_nest_type *) ptr;
3068 size_t name_len, subtype_len;
3070 if (left < offsetof (struct lf_nest_type, name))
3072 einfo (_("%P: warning: truncated CodeView type record"
3073 " LF_NESTTYPE\n"));
3074 return false;
3077 if (!remap_type (&nest->type, map, type_num, num_types))
3078 return false;
3080 name_len =
3081 strnlen (nest->name,
3082 left - offsetof (struct lf_nest_type, name));
3084 if (name_len == left - offsetof (struct lf_nest_type, name))
3086 einfo (_("%P: warning: name for LF_NESTTYPE has no"
3087 " terminating zero\n"));
3088 return false;
3091 name_len++;
3093 subtype_len = offsetof (struct lf_nest_type, name)
3094 + name_len;
3096 if (subtype_len % 4 != 0)
3097 subtype_len += 4 - (subtype_len % 4);
3099 if (left < subtype_len)
3101 einfo (_("%P: warning: truncated CodeView type record"
3102 " LF_FIELDLIST\n"));
3103 return false;
3106 ptr += subtype_len;
3107 left -= subtype_len;
3109 break;
3112 default:
3113 einfo (_("%P: warning: unrecognized CodeView subtype %v\n"),
3114 subtype);
3115 return false;
3119 break;
3122 case LF_BITFIELD:
3124 struct lf_bitfield *bf = (struct lf_bitfield *) data;
3126 if (size < offsetof (struct lf_bitfield, length))
3128 einfo (_("%P: warning: truncated CodeView type record"
3129 " LF_BITFIELD\n"));
3130 return false;
3133 if (!remap_type (&bf->base_type, map, type_num, num_types))
3134 return false;
3136 break;
3139 case LF_METHODLIST:
3141 struct lf_methodlist *ml = (struct lf_methodlist *) data;
3142 unsigned int num_entries;
3144 if (size < offsetof (struct lf_methodlist, entries))
3146 einfo (_("%P: warning: truncated CodeView type record"
3147 " LF_METHODLIST\n"));
3148 return false;
3151 if ((size - offsetof (struct lf_methodlist, entries))
3152 % sizeof (struct lf_methodlist_entry))
3154 einfo (_("%P: warning: malformed CodeView type record"
3155 " LF_METHODLIST\n"));
3156 return false;
3159 num_entries = (size - offsetof (struct lf_methodlist, entries))
3160 / sizeof (struct lf_methodlist_entry);
3162 for (unsigned int i = 0; i < num_entries; i++)
3164 if (!remap_type (&ml->entries[i].method_type, map,
3165 type_num, num_types))
3166 return false;
3169 break;
3172 case LF_ARRAY:
3174 struct lf_array *arr = (struct lf_array *) data;
3176 if (size < offsetof (struct lf_array, length_in_bytes))
3178 einfo (_("%P: warning: truncated CodeView type record"
3179 " LF_ARRAY\n"));
3180 return false;
3183 if (!remap_type (&arr->element_type, map, type_num, num_types))
3184 return false;
3186 if (!remap_type (&arr->index_type, map, type_num, num_types))
3187 return false;
3189 break;
3192 case LF_CLASS:
3193 case LF_STRUCTURE:
3195 struct lf_class *cl = (struct lf_class *) data;
3196 uint16_t prop, num_bytes;
3197 size_t name_len, name_off;
3199 if (size < offsetof (struct lf_class, name))
3201 einfo (_("%P: warning: truncated CodeView type record"
3202 " LF_CLASS/LF_STRUCTURE\n"));
3203 return false;
3206 if (!remap_type (&cl->field_list, map, type_num, num_types))
3207 return false;
3209 if (!remap_type (&cl->derived_from, map, type_num, num_types))
3210 return false;
3212 if (!remap_type (&cl->vshape, map, type_num, num_types))
3213 return false;
3215 name_off = offsetof (struct lf_class, name);
3217 num_bytes = bfd_getl16 (&cl->length);
3219 /* If num_bytes >= 0x8000, actual value follows. */
3220 if (num_bytes >= 0x8000)
3222 unsigned int param_len = extended_value_len (num_bytes);
3224 if (param_len == 0)
3226 einfo (_("%P: warning: unhandled type %v within"
3227 " LF_CLASS/LF_STRUCTURE\n"), num_bytes);
3228 return false;
3231 name_off += param_len;
3233 if (size < name_off)
3235 einfo (_("%P: warning: truncated CodeView type record"
3236 " LF_CLASS/LF_STRUCTURE\n"));
3237 return false;
3241 name_len = strnlen ((char *) cl + name_off, size - name_off);
3243 if (name_len == size - name_off)
3245 einfo (_("%P: warning: name for LF_CLASS/LF_STRUCTURE has no"
3246 " terminating zero\n"));
3247 return false;
3250 prop = bfd_getl16 (&cl->properties);
3252 if (prop & CV_PROP_HAS_UNIQUE_NAME)
3254 /* Structure has another name following first one. */
3256 size_t len = name_off + name_len + 1;
3257 size_t unique_name_len;
3259 unique_name_len = strnlen ((char *) cl + name_off + name_len + 1,
3260 size - len);
3262 if (unique_name_len == size - len)
3264 einfo (_("%P: warning: unique name for LF_CLASS/LF_STRUCTURE"
3265 " has no terminating zero\n"));
3266 return false;
3270 if (!(prop & (CV_PROP_FORWARD_REF | CV_PROP_SCOPED))
3271 && !is_name_anonymous ((char *) cl + name_off, name_len))
3273 other_hash = true;
3274 cv_hash = crc32 ((uint8_t *) cl + name_off, name_len);
3277 break;
3280 case LF_UNION:
3282 struct lf_union *un = (struct lf_union *) data;
3283 uint16_t prop, num_bytes;
3284 size_t name_len, name_off;
3286 if (size < offsetof (struct lf_union, name))
3288 einfo (_("%P: warning: truncated CodeView type record"
3289 " LF_UNION\n"));
3290 return false;
3293 if (!remap_type (&un->field_list, map, type_num, num_types))
3294 return false;
3296 name_off = offsetof (struct lf_union, name);
3298 num_bytes = bfd_getl16 (&un->length);
3300 /* If num_bytes >= 0x8000, actual value follows. */
3301 if (num_bytes >= 0x8000)
3303 unsigned int param_len = extended_value_len (num_bytes);
3305 if (param_len == 0)
3307 einfo (_("%P: warning: unhandled type %v within"
3308 " LF_UNION\n"), num_bytes);
3309 return false;
3312 name_off += param_len;
3314 if (size < name_off)
3316 einfo (_("%P: warning: truncated CodeView type record"
3317 " LF_UNION\n"));
3318 return false;
3322 name_len = strnlen ((char *) un + name_off, size - name_off);
3324 if (name_len == size - name_off)
3326 einfo (_("%P: warning: name for LF_UNION has no"
3327 " terminating zero\n"));
3328 return false;
3331 prop = bfd_getl16 (&un->properties);
3333 if (prop & CV_PROP_HAS_UNIQUE_NAME)
3335 /* Structure has another name following first one. */
3337 size_t len = name_off + name_len + 1;
3338 size_t unique_name_len;
3340 unique_name_len = strnlen ((char *) un + name_off + name_len + 1,
3341 size - len);
3343 if (unique_name_len == size - len)
3345 einfo (_("%P: warning: unique name for LF_UNION has"
3346 " no terminating zero\n"));
3347 return false;
3351 if (!(prop & (CV_PROP_FORWARD_REF | CV_PROP_SCOPED))
3352 && !is_name_anonymous ((char *) un + name_off, name_len))
3354 other_hash = true;
3355 cv_hash = crc32 ((uint8_t *) un + name_off, name_len);
3358 break;
3361 case LF_ENUM:
3363 struct lf_enum *en = (struct lf_enum *) data;
3364 uint16_t prop;
3365 size_t name_len;
3367 if (size < offsetof (struct lf_enum, name))
3369 einfo (_("%P: warning: truncated CodeView type record"
3370 " LF_ENUM\n"));
3371 return false;
3374 if (!remap_type (&en->underlying_type, map, type_num, num_types))
3375 return false;
3377 if (!remap_type (&en->field_list, map, type_num, num_types))
3378 return false;
3380 name_len = strnlen (en->name, size - offsetof (struct lf_enum, name));
3382 if (name_len == size - offsetof (struct lf_enum, name))
3384 einfo (_("%P: warning: name for LF_ENUM has no"
3385 " terminating zero\n"));
3386 return false;
3389 prop = bfd_getl16 (&en->properties);
3391 if (prop & CV_PROP_HAS_UNIQUE_NAME)
3393 /* Structure has another name following first one. */
3395 size_t len = offsetof (struct lf_enum, name) + name_len + 1;
3396 size_t unique_name_len;
3398 unique_name_len = strnlen (en->name + name_len + 1, size - len);
3400 if (unique_name_len == size - len)
3402 einfo (_("%P: warning: unique name for LF_ENUM has"
3403 " no terminating zero\n"));
3404 return false;
3408 break;
3411 case LF_VTSHAPE:
3412 /* Does not reference any types, nothing to be done. */
3413 break;
3415 case LF_VFTABLE:
3417 struct lf_vftable *vft = (struct lf_vftable *) data;
3419 if (size < offsetof (struct lf_vftable, names))
3421 einfo (_("%P: warning: truncated CodeView type record"
3422 " LF_VFTABLE\n"));
3423 return false;
3426 if (!remap_type (&vft->type, map, type_num, num_types))
3427 return false;
3429 if (!remap_type (&vft->base_vftable, map, type_num, num_types))
3430 return false;
3432 break;
3435 case LF_STRING_ID:
3437 struct lf_string_id *str = (struct lf_string_id *) data;
3438 size_t string_len;
3440 if (size < offsetof (struct lf_string_id, string))
3442 einfo (_("%P: warning: truncated CodeView type record"
3443 " LF_STRING_ID\n"));
3444 return false;
3447 if (!remap_type (&str->substring, map, type_num, num_types))
3448 return false;
3450 string_len = strnlen (str->string,
3451 size - offsetof (struct lf_string_id, string));
3453 if (string_len == size - offsetof (struct lf_string_id, string))
3455 einfo (_("%P: warning: string for LF_STRING_ID has no"
3456 " terminating zero\n"));
3457 return false;
3460 ipi = true;
3462 break;
3465 case LF_SUBSTR_LIST:
3467 uint32_t num_entries;
3468 struct lf_arglist *ssl = (struct lf_arglist *) data;
3470 if (size < offsetof (struct lf_arglist, args))
3472 einfo (_("%P: warning: truncated CodeView type record"
3473 " LF_SUBSTR_LIST\n"));
3474 return false;
3477 num_entries = bfd_getl32 (&ssl->num_entries);
3479 if (size < offsetof (struct lf_arglist, args)
3480 + (num_entries * sizeof (uint32_t)))
3482 einfo (_("%P: warning: truncated CodeView type record"
3483 " LF_SUBSTR_LIST\n"));
3484 return false;
3487 for (uint32_t i = 0; i < num_entries; i++)
3489 if (!remap_type (&ssl->args[i], map, type_num, num_types))
3490 return false;
3493 ipi = true;
3495 break;
3498 case LF_BUILDINFO:
3500 uint16_t num_entries;
3501 struct lf_build_info *bi = (struct lf_build_info *) data;
3503 if (size < offsetof (struct lf_build_info, strings))
3505 einfo (_("%P: warning: truncated CodeView type record"
3506 " LF_BUILDINFO\n"));
3507 return false;
3510 num_entries = bfd_getl16 (&bi->count);
3512 if (size < offsetof (struct lf_build_info, strings)
3513 + (num_entries * sizeof (uint32_t)))
3515 einfo (_("%P: warning: truncated CodeView type record"
3516 " LF_BUILDINFO\n"));
3517 return false;
3520 for (uint32_t i = 0; i < num_entries; i++)
3522 if (!remap_type (&bi->strings[i], map, type_num, num_types))
3523 return false;
3526 ipi = true;
3528 break;
3531 case LF_FUNC_ID:
3533 struct lf_func_id *func = (struct lf_func_id *) data;
3534 size_t name_len;
3536 if (size < offsetof (struct lf_func_id, name))
3538 einfo (_("%P: warning: truncated CodeView type record"
3539 " LF_FUNC_ID\n"));
3540 return false;
3543 if (!remap_type (&func->parent_scope, map, type_num, num_types))
3544 return false;
3546 if (!remap_type (&func->function_type, map, type_num, num_types))
3547 return false;
3549 name_len = strnlen (func->name,
3550 size - offsetof (struct lf_func_id, name));
3552 if (name_len == size - offsetof (struct lf_func_id, name))
3554 einfo (_("%P: warning: string for LF_FUNC_ID has no"
3555 " terminating zero\n"));
3556 return false;
3559 ipi = true;
3561 break;
3564 case LF_MFUNC_ID:
3566 struct lf_mfunc_id *mfunc = (struct lf_mfunc_id *) data;
3567 size_t name_len;
3569 if (size < offsetof (struct lf_mfunc_id, name))
3571 einfo (_("%P: warning: truncated CodeView type record"
3572 " LF_MFUNC_ID\n"));
3573 return false;
3576 if (!remap_type (&mfunc->parent_type, map, type_num, num_types))
3577 return false;
3579 if (!remap_type (&mfunc->function_type, map, type_num, num_types))
3580 return false;
3582 name_len = strnlen (mfunc->name,
3583 size - offsetof (struct lf_mfunc_id, name));
3585 if (name_len == size - offsetof (struct lf_mfunc_id, name))
3587 einfo (_("%P: warning: string for LF_MFUNC_ID has no"
3588 " terminating zero\n"));
3589 return false;
3592 ipi = true;
3594 break;
3597 case LF_UDT_SRC_LINE:
3598 return handle_udt_src_line (data, size, map, type_num, num_types,
3599 ids, mod_num, strings);
3601 default:
3602 einfo (_("%P: warning: unrecognized CodeView type %v\n"), type);
3603 return false;
3606 hash = iterative_hash (data, size, 0);
3608 t = ipi ? ids : types;
3610 slot = htab_find_slot_with_hash (t->hashmap, data, hash, INSERT);
3611 if (!slot)
3612 return false;
3614 if (!*slot) /* new entry */
3616 struct type_entry *e;
3618 *slot = xmalloc (offsetof (struct type_entry, data) + size);
3620 e = (struct type_entry *) *slot;
3622 e->next = NULL;
3623 e->index = t->num_types;
3625 if (other_hash)
3626 e->cv_hash = cv_hash;
3627 else
3628 e->cv_hash = crc32 (data, size);
3630 e->has_udt_src_line = false;
3632 memcpy (e->data, data, size);
3634 if (t->last)
3635 t->last->next = e;
3636 else
3637 t->first = e;
3639 t->last = e;
3641 map[type_num] = e;
3643 t->num_types++;
3645 else /* duplicate */
3647 map[type_num] = (struct type_entry *) *slot;
3650 return true;
3653 /* Parse the .debug$T section of a module, and pass any type definitions
3654 found to handle_type. */
3655 static bool
3656 handle_debugt_section (asection *s, bfd *mod, struct types *types,
3657 struct types *ids, uint16_t mod_num,
3658 struct string_table *strings,
3659 struct type_entry ***map, uint32_t *num_types)
3661 bfd_byte *data = NULL;
3662 size_t off;
3663 uint32_t type_num;
3665 if (!bfd_get_full_section_contents (mod, s, &data))
3666 return false;
3668 if (!data)
3669 return false;
3671 if (bfd_getl32 (data) != CV_SIGNATURE_C13)
3673 free (data);
3674 return true;
3677 off = sizeof (uint32_t);
3679 while (off + sizeof (uint16_t) <= s->size)
3681 uint16_t size;
3683 size = bfd_getl16 (data + off);
3684 off += sizeof (uint16_t);
3686 if (size + off > s->size || size < sizeof (uint16_t))
3688 free (data);
3689 bfd_set_error (bfd_error_bad_value);
3690 return false;
3693 (*num_types)++;
3694 off += size;
3697 if (*num_types == 0)
3699 free (data);
3700 return true;
3703 *map = xcalloc (*num_types, sizeof (struct type_entry *));
3705 off = sizeof (uint32_t);
3706 type_num = 0;
3708 while (off + sizeof (uint16_t) <= s->size)
3710 uint16_t size;
3712 size = bfd_getl16 (data + off);
3714 if (!handle_type (data + off, *map, type_num, *num_types, types, ids,
3715 mod_num, strings))
3717 free (data);
3718 free (*map);
3719 bfd_set_error (bfd_error_bad_value);
3720 return false;
3723 off += sizeof (uint16_t) + size;
3724 type_num++;
3727 free (data);
3729 return true;
3732 /* Return the CodeView constant for the selected architecture. */
3733 static uint16_t
3734 target_processor (bfd *abfd)
3736 switch (abfd->arch_info->arch)
3738 case bfd_arch_i386:
3739 if (abfd->arch_info->mach & bfd_mach_x86_64)
3740 return CV_CFL_X64;
3741 else
3742 return CV_CFL_80386;
3744 case bfd_arch_aarch64:
3745 return CV_CFL_ARM64;
3747 default:
3748 return 0;
3752 /* Create the symbols that go in "* Linker *", the dummy module created
3753 for the linker itself. */
3754 static bool
3755 create_linker_symbols (bfd *abfd, uint8_t **syms, uint32_t *sym_byte_size,
3756 const char *pdb_name)
3758 uint8_t *ptr;
3759 struct objname *name;
3760 struct compile3 *comp;
3761 struct envblock *env;
3762 size_t padding1, padding2, env_size;
3763 char *cwdval, *exeval, *pdbval;
3765 /* extra NUL for padding */
3766 static const char linker_fn[] = "* Linker *\0";
3767 static const char linker_name[] = "GNU LD " VERSION;
3769 static const char cwd[] = "cwd";
3770 static const char exe[] = "exe";
3771 static const char pdb[] = "pdb";
3773 cwdval = getcwd (NULL, 0);
3774 if (!cwdval)
3776 einfo (_("%P: warning: unable to get working directory\n"));
3777 return false;
3780 exeval = lrealpath (program_name);
3782 if (!exeval)
3784 einfo (_("%P: warning: unable to get program name\n"));
3785 free (cwdval);
3786 return false;
3789 pdbval = lrealpath (pdb_name);
3791 if (!pdbval)
3793 einfo (_("%P: warning: unable to get full path to PDB\n"));
3794 free (exeval);
3795 free (cwdval);
3796 return false;
3799 *sym_byte_size += offsetof (struct objname, name) + sizeof (linker_fn);
3800 *sym_byte_size += offsetof (struct compile3, compiler) + sizeof (linker_name);
3802 if (*sym_byte_size % 4)
3803 padding1 = 4 - (*sym_byte_size % 4);
3804 else
3805 padding1 = 0;
3807 *sym_byte_size += padding1;
3809 env_size = offsetof (struct envblock, strings);
3810 env_size += sizeof (cwd);
3811 env_size += strlen (cwdval) + 1;
3812 env_size += sizeof (exe);
3813 env_size += strlen (exeval) + 1;
3814 env_size += sizeof (pdb);
3815 env_size += strlen (pdbval) + 1;
3817 if (env_size % 4)
3818 padding2 = 4 - (env_size % 4);
3819 else
3820 padding2 = 0;
3822 env_size += padding2;
3824 *sym_byte_size += env_size;
3826 *syms = xmalloc (*sym_byte_size);
3827 ptr = *syms;
3829 /* Write S_OBJNAME */
3831 name = (struct objname *) ptr;
3832 bfd_putl16 (offsetof (struct objname, name)
3833 + sizeof (linker_fn) - sizeof (uint16_t), &name->size);
3834 bfd_putl16 (S_OBJNAME, &name->kind);
3835 bfd_putl32 (0, &name->signature);
3836 memcpy (name->name, linker_fn, sizeof (linker_fn));
3838 ptr += offsetof (struct objname, name) + sizeof (linker_fn);
3840 /* Write S_COMPILE3 */
3842 comp = (struct compile3 *) ptr;
3844 bfd_putl16 (offsetof (struct compile3, compiler) + sizeof (linker_name)
3845 + padding1 - sizeof (uint16_t), &comp->size);
3846 bfd_putl16 (S_COMPILE3, &comp->kind);
3847 bfd_putl32 (CV_CFL_LINK, &comp->flags);
3848 bfd_putl16 (target_processor (abfd), &comp->machine);
3849 bfd_putl16 (0, &comp->frontend_major);
3850 bfd_putl16 (0, &comp->frontend_minor);
3851 bfd_putl16 (0, &comp->frontend_build);
3852 bfd_putl16 (0, &comp->frontend_qfe);
3853 bfd_putl16 (0, &comp->backend_major);
3854 bfd_putl16 (0, &comp->backend_minor);
3855 bfd_putl16 (0, &comp->backend_build);
3856 bfd_putl16 (0, &comp->backend_qfe);
3857 memcpy (comp->compiler, linker_name, sizeof (linker_name));
3859 memset (comp->compiler + sizeof (linker_name), 0, padding1);
3861 ptr += offsetof (struct compile3, compiler) + sizeof (linker_name) + padding1;
3863 /* Write S_ENVBLOCK */
3865 env = (struct envblock *) ptr;
3867 bfd_putl16 (env_size - sizeof (uint16_t), &env->size);
3868 bfd_putl16 (S_ENVBLOCK, &env->kind);
3869 env->flags = 0;
3871 ptr += offsetof (struct envblock, strings);
3873 memcpy (ptr, cwd, sizeof (cwd));
3874 ptr += sizeof (cwd);
3875 memcpy (ptr, cwdval, strlen (cwdval) + 1);
3876 ptr += strlen (cwdval) + 1;
3878 memcpy (ptr, exe, sizeof (exe));
3879 ptr += sizeof (exe);
3880 memcpy (ptr, exeval, strlen (exeval) + 1);
3881 ptr += strlen (exeval) + 1;
3883 memcpy (ptr, pdb, sizeof (pdb));
3884 ptr += sizeof (pdb);
3885 memcpy (ptr, pdbval, strlen (pdbval) + 1);
3886 ptr += strlen (pdbval) + 1;
3888 /* Microsoft's LINK also includes "cmd", the command-line options passed
3889 to the linker, but unfortunately we don't have access to argc and argv
3890 at this stage. */
3892 memset (ptr, 0, padding2);
3894 free (pdbval);
3895 free (exeval);
3896 free (cwdval);
3898 return true;
3901 /* Populate the module stream, which consists of the transformed .debug$S
3902 data for each object file. */
3903 static bool
3904 populate_module_stream (bfd *stream, bfd *mod, uint32_t *sym_byte_size,
3905 struct string_table *strings,
3906 uint32_t *c13_info_size,
3907 struct mod_source_files *mod_source,
3908 bfd *abfd, struct types *types,
3909 struct types *ids, uint16_t mod_num,
3910 bfd *sym_rec_stream, struct globals *glob,
3911 const char *pdb_name)
3913 uint8_t int_buf[sizeof (uint32_t)];
3914 uint8_t *c13_info = NULL;
3915 uint8_t *syms = NULL;
3917 *sym_byte_size = 0;
3918 *c13_info_size = 0;
3920 if (!strcmp (bfd_get_filename (mod), "dll stuff"))
3922 if (!create_linker_symbols (mod, &syms, sym_byte_size, pdb_name))
3923 return false;
3925 else
3927 struct type_entry **map = NULL;
3928 uint32_t num_types = 0;
3930 /* Process .debug$T section. */
3932 for (asection *s = mod->sections; s; s = s->next)
3934 if (!strcmp (s->name, ".debug$T") && s->size >= sizeof (uint32_t))
3936 if (!handle_debugt_section (s, mod, types, ids, mod_num, strings,
3937 &map, &num_types))
3939 free (mod_source->files);
3940 return false;
3943 break;
3947 /* Process .debug$S section(s). */
3949 for (asection *s = mod->sections; s; s = s->next)
3951 if (!strcmp (s->name, ".debug$S") && s->size >= sizeof (uint32_t))
3953 if (!handle_debugs_section (s, mod, strings, &c13_info,
3954 c13_info_size, mod_source, abfd,
3955 &syms, sym_byte_size, map, num_types,
3956 sym_rec_stream, glob, mod_num))
3958 free (c13_info);
3959 free (syms);
3960 free (mod_source->files);
3961 free (map);
3962 return false;
3967 free (map);
3970 /* Write the signature. */
3972 bfd_putl32 (CV_SIGNATURE_C13, int_buf);
3974 if (bfd_write (int_buf, sizeof (uint32_t), stream) != sizeof (uint32_t))
3976 free (c13_info);
3977 free (syms);
3978 return false;
3981 if (syms)
3983 if (bfd_write (syms, *sym_byte_size, stream) != *sym_byte_size)
3985 free (c13_info);
3986 free (syms);
3987 return false;
3990 free (syms);
3993 if (c13_info)
3995 if (bfd_write (c13_info, *c13_info_size, stream) != *c13_info_size)
3997 free (c13_info);
3998 return false;
4001 free (c13_info);
4004 /* Write the global refs size. */
4006 bfd_putl32 (0, int_buf);
4008 if (bfd_write (int_buf, sizeof (uint32_t), stream) != sizeof (uint32_t))
4009 return false;
4011 return true;
4014 /* Create the module info substream within the DBI. */
4015 static bool
4016 create_module_info_substream (bfd *abfd, bfd *pdb, void **data,
4017 uint32_t *size, struct string_table *strings,
4018 struct source_files_info *source,
4019 struct types *types, struct types *ids,
4020 bfd *sym_rec_stream, struct globals *glob,
4021 const char *pdb_name)
4023 uint8_t *ptr;
4024 unsigned int mod_num;
4026 static const char linker_fn[] = "* Linker *";
4028 *size = 0;
4030 for (bfd *in = coff_data (abfd)->link_info->input_bfds; in;
4031 in = in->link.next)
4033 size_t len = sizeof (struct module_info);
4035 if (!strcmp (bfd_get_filename (in), "dll stuff"))
4037 len += sizeof (linker_fn); /* Object name. */
4038 len++; /* Empty module name. */
4040 else if (in->my_archive)
4042 char *name = lrealpath (bfd_get_filename (in));
4044 len += strlen (name) + 1; /* Object name. */
4046 free (name);
4048 name = lrealpath (bfd_get_filename (in->my_archive));
4050 len += strlen (name) + 1; /* Archive name. */
4052 free (name);
4054 else
4056 char *name = lrealpath (bfd_get_filename (in));
4057 size_t name_len = strlen (name) + 1;
4059 len += name_len; /* Object name. */
4060 len += name_len; /* And again as the archive name. */
4062 free (name);
4065 if (len % 4)
4066 len += 4 - (len % 4);
4068 *size += len;
4070 source->mod_count++;
4073 *data = xmalloc (*size);
4075 ptr = *data;
4077 source->mods = xmalloc (source->mod_count
4078 * sizeof (struct mod_source_files));
4079 memset (source->mods, 0,
4080 source->mod_count * sizeof (struct mod_source_files));
4082 mod_num = 0;
4084 for (bfd *in = coff_data (abfd)->link_info->input_bfds; in;
4085 in = in->link.next)
4087 struct module_info *mod = (struct module_info *) ptr;
4088 uint16_t stream_num;
4089 bfd *stream;
4090 uint32_t sym_byte_size, c13_info_size;
4091 uint8_t *start = ptr;
4093 stream = add_stream (pdb, NULL, &stream_num);
4095 if (!stream)
4097 for (unsigned int i = 0; i < source->mod_count; i++)
4099 free (source->mods[i].files);
4102 free (source->mods);
4103 free (*data);
4104 return false;
4107 if (!populate_module_stream (stream, in, &sym_byte_size,
4108 strings, &c13_info_size,
4109 &source->mods[mod_num], abfd,
4110 types, ids, mod_num,
4111 sym_rec_stream, glob, pdb_name))
4113 for (unsigned int i = 0; i < source->mod_count; i++)
4115 free (source->mods[i].files);
4118 free (source->mods);
4119 free (*data);
4120 return false;
4123 bfd_putl32 (0, &mod->unused1);
4125 /* These are dummy values - MSVC copies the first section contribution
4126 entry here, but doesn't seem to use it for anything. */
4127 bfd_putl16 (0xffff, &mod->sc.section);
4128 bfd_putl16 (0, &mod->sc.padding1);
4129 bfd_putl32 (0, &mod->sc.offset);
4130 bfd_putl32 (0xffffffff, &mod->sc.size);
4131 bfd_putl32 (0, &mod->sc.characteristics);
4132 bfd_putl16 (0xffff, &mod->sc.module_index);
4133 bfd_putl16 (0, &mod->sc.padding2);
4134 bfd_putl32 (0, &mod->sc.data_crc);
4135 bfd_putl32 (0, &mod->sc.reloc_crc);
4137 bfd_putl16 (0, &mod->flags);
4138 bfd_putl16 (stream_num, &mod->module_sym_stream);
4139 bfd_putl32 (sizeof (uint32_t) + sym_byte_size, &mod->sym_byte_size);
4140 bfd_putl32 (0, &mod->c11_byte_size);
4141 bfd_putl32 (c13_info_size, &mod->c13_byte_size);
4142 bfd_putl16 (0, &mod->source_file_count);
4143 bfd_putl16 (0, &mod->padding);
4144 bfd_putl32 (0, &mod->unused2);
4145 bfd_putl32 (0, &mod->source_file_name_index);
4146 bfd_putl32 (0, &mod->pdb_file_path_name_index);
4148 ptr += sizeof (struct module_info);
4150 if (!strcmp (bfd_get_filename (in), "dll stuff"))
4152 /* Object name. */
4153 memcpy (ptr, linker_fn, sizeof (linker_fn));
4154 ptr += sizeof (linker_fn);
4156 /* Empty module name. */
4157 *ptr = 0;
4158 ptr++;
4160 else if (in->my_archive)
4162 char *name = lrealpath (bfd_get_filename (in));
4163 size_t name_len = strlen (name) + 1;
4165 /* Object name. */
4166 memcpy (ptr, name, name_len);
4167 ptr += name_len;
4169 free (name);
4171 name = lrealpath (bfd_get_filename (in->my_archive));
4172 name_len = strlen (name) + 1;
4174 /* Archive name. */
4175 memcpy (ptr, name, name_len);
4176 ptr += name_len;
4178 free (name);
4180 else
4182 char *name = lrealpath (bfd_get_filename (in));
4183 size_t name_len = strlen (name) + 1;
4185 /* Object name. */
4186 memcpy (ptr, name, name_len);
4187 ptr += name_len;
4189 /* Object name again as archive name. */
4190 memcpy (ptr, name, name_len);
4191 ptr += name_len;
4193 free (name);
4196 /* Pad to next four-byte boundary. */
4198 if ((ptr - start) % 4)
4200 memset (ptr, 0, 4 - ((ptr - start) % 4));
4201 ptr += 4 - ((ptr - start) % 4);
4204 mod_num++;
4207 return true;
4210 /* Return the index of a given output section. */
4211 static uint16_t
4212 find_section_number (bfd *abfd, asection *sect)
4214 uint16_t i = 1;
4216 for (asection *s = abfd->sections; s; s = s->next)
4218 if (s == sect)
4219 return i;
4221 /* Empty sections aren't output. */
4222 if (s->size != 0)
4223 i++;
4226 return 0;
4229 /* Used as parameter to qsort, to sort section contributions by section and
4230 offset. */
4231 static int
4232 section_contribs_compare (const void *p1, const void *p2)
4234 const struct in_sc *sc1 = p1;
4235 const struct in_sc *sc2 = p2;
4237 if (sc1->sect_num < sc2->sect_num)
4238 return -1;
4239 if (sc1->sect_num > sc2->sect_num)
4240 return 1;
4242 if (sc1->s->output_offset < sc2->s->output_offset)
4243 return -1;
4244 if (sc1->s->output_offset > sc2->s->output_offset)
4245 return 1;
4247 return 0;
4250 /* Create the substream which maps addresses in the image file to locations
4251 in the original object files. */
4252 static bool
4253 create_section_contrib_substream (bfd *abfd, void **data, uint32_t *size)
4255 unsigned int num_sc = 0;
4256 struct section_contribution *sc;
4257 uint16_t mod_index;
4258 char *sect_flags;
4259 file_ptr offset;
4260 struct in_sc *sc_in, *sc2;
4261 uint32_t *ptr;
4263 for (bfd *in = coff_data (abfd)->link_info->input_bfds; in;
4264 in = in->link.next)
4266 for (asection *s = in->sections; s; s = s->next)
4268 if (s->size == 0 || discarded_section (s))
4269 continue;
4271 num_sc++;
4275 *size = sizeof (uint32_t) + (num_sc * sizeof (struct section_contribution));
4276 *data = xmalloc (*size);
4278 bfd_putl32 (SECTION_CONTRIB_VERSION_60, *data);
4280 /* Read characteristics of outputted sections. */
4282 sect_flags = xmalloc (sizeof (uint32_t) * abfd->section_count);
4284 offset = bfd_coff_filhsz (abfd) + bfd_coff_aoutsz (abfd);
4285 offset += offsetof (struct external_scnhdr, s_flags);
4287 for (unsigned int i = 0; i < abfd->section_count; i++)
4289 if (bfd_seek (abfd, offset, SEEK_SET) != 0
4290 || bfd_read (sect_flags + (i * sizeof (uint32_t)), sizeof (uint32_t),
4291 abfd) != sizeof (uint32_t))
4293 free (*data);
4294 free (sect_flags);
4295 return false;
4298 offset += sizeof (struct external_scnhdr);
4301 /* Microsoft's DIA expects section contributions to be sorted by section
4302 number and offset, otherwise it will be unable to resolve line numbers. */
4304 sc_in = xmalloc (num_sc * sizeof (* sc_in));
4305 sc2 = sc_in;
4307 mod_index = 0;
4308 for (bfd *in = coff_data (abfd)->link_info->input_bfds; in;
4309 in = in->link.next)
4311 for (asection *s = in->sections; s; s = s->next)
4313 if (s->size == 0 || discarded_section (s))
4314 continue;
4316 sc2->s = s;
4317 sc2->sect_num = find_section_number (abfd, s->output_section);
4318 sc2->mod_index = mod_index;
4320 sc2++;
4323 mod_index++;
4326 qsort (sc_in, num_sc, sizeof (* sc_in), section_contribs_compare);
4328 ptr = *data;
4329 sc = (struct section_contribution *) (ptr + 1); /* Skip the version word. */
4331 for (unsigned int i = 0; i < num_sc; i++)
4333 memcpy (&sc->characteristics,
4334 sect_flags + ((sc_in[i].sect_num - 1) * sizeof (uint32_t)),
4335 sizeof (uint32_t));
4337 bfd_putl16 (sc_in[i].sect_num, &sc->section);
4338 bfd_putl16 (0, &sc->padding1);
4339 bfd_putl32 (sc_in[i].s->output_offset, &sc->offset);
4340 bfd_putl32 (sc_in[i].s->size, &sc->size);
4341 bfd_putl16 (sc_in[i].mod_index, &sc->module_index);
4342 bfd_putl16 (0, &sc->padding2);
4343 bfd_putl32 (0, &sc->data_crc);
4344 bfd_putl32 (0, &sc->reloc_crc);
4346 sc++;
4349 free (sc_in);
4350 free (sect_flags);
4352 return true;
4355 /* The source info substream lives within the DBI stream, and lists the
4356 source files for each object file (i.e. it's derived from the
4357 DEBUG_S_FILECHKSMS parts of the .debug$S sections). This is a bit
4358 superfluous, as the filenames are also available in the C13 parts of
4359 the module streams, but MSVC relies on it to work properly. */
4360 static void
4361 create_source_info_substream (void **data, uint32_t *size,
4362 struct source_files_info *source)
4364 uint16_t dedupe_source_files_count = 0;
4365 uint16_t source_files_count = 0;
4366 uint32_t strings_len = 0;
4367 uint8_t *ptr;
4369 /* Loop through the source files, marking unique filenames. The pointers
4370 here are for entries in the main string table, and so have already
4371 been deduplicated. */
4373 for (uint16_t i = 0; i < source->mod_count; i++)
4375 for (uint16_t j = 0; j < source->mods[i].files_count; j++)
4377 if (source->mods[i].files[j])
4379 if (source->mods[i].files[j]->source_file_offset == 0xffffffff)
4381 source->mods[i].files[j]->source_file_offset = strings_len;
4382 strings_len += source->mods[i].files[j]->len + 1;
4383 dedupe_source_files_count++;
4386 source_files_count++;
4391 *size = sizeof (uint16_t) + sizeof (uint16_t);
4392 *size += (sizeof (uint16_t) + sizeof (uint16_t)) * source->mod_count;
4393 *size += sizeof (uint32_t) * source_files_count;
4394 *size += strings_len;
4396 *data = xmalloc (*size);
4398 ptr = (uint8_t *) *data;
4400 /* Write header (module count and source file count). */
4402 bfd_putl16 (source->mod_count, ptr);
4403 ptr += sizeof (uint16_t);
4405 bfd_putl16 (dedupe_source_files_count, ptr);
4406 ptr += sizeof (uint16_t);
4408 /* Write "ModIndices". As the LLVM documentation puts it, "this array is
4409 present, but does not appear to be useful". */
4411 for (uint16_t i = 0; i < source->mod_count; i++)
4413 bfd_putl16 (i, ptr);
4414 ptr += sizeof (uint16_t);
4417 /* Write source file count for each module. */
4419 for (uint16_t i = 0; i < source->mod_count; i++)
4421 bfd_putl16 (source->mods[i].files_count, ptr);
4422 ptr += sizeof (uint16_t);
4425 /* For each module, write the offsets within the string table
4426 for each source file. */
4428 for (uint16_t i = 0; i < source->mod_count; i++)
4430 for (uint16_t j = 0; j < source->mods[i].files_count; j++)
4432 if (source->mods[i].files[j])
4434 bfd_putl32 (source->mods[i].files[j]->source_file_offset, ptr);
4435 ptr += sizeof (uint32_t);
4440 /* Write the string table. We set source_file_offset to a dummy value for
4441 each entry we write, so we don't write duplicate filenames. */
4443 for (uint16_t i = 0; i < source->mod_count; i++)
4445 for (uint16_t j = 0; j < source->mods[i].files_count; j++)
4447 if (source->mods[i].files[j]
4448 && source->mods[i].files[j]->source_file_offset != 0xffffffff)
4450 memcpy (ptr, source->mods[i].files[j]->s,
4451 source->mods[i].files[j]->len);
4452 ptr += source->mods[i].files[j]->len;
4454 *ptr = 0;
4455 ptr++;
4457 source->mods[i].files[j]->source_file_offset = 0xffffffff;
4463 /* Used as parameter to qsort, to sort globals by hash. */
4464 static int
4465 global_compare_hash (const void *s1, const void *s2)
4467 const struct global *g1 = *(const struct global **) s1;
4468 const struct global *g2 = *(const struct global **) s2;
4470 if (g1->hash < g2->hash)
4471 return -1;
4472 if (g1->hash > g2->hash)
4473 return 1;
4475 return 0;
4478 /* Create the globals stream, which contains the unmangled symbol names. */
4479 static bool
4480 create_globals_stream (bfd *pdb, struct globals *glob, uint16_t *stream_num)
4482 bfd *stream;
4483 struct globals_hash_header h;
4484 uint32_t buckets_size, filled_buckets = 0;
4485 struct global **sorted = NULL;
4486 bool ret = false;
4487 struct global *buckets[NUM_GLOBALS_HASH_BUCKETS];
4488 char int_buf[sizeof (uint32_t)];
4490 stream = add_stream (pdb, NULL, stream_num);
4491 if (!stream)
4492 return false;
4494 memset (buckets, 0, sizeof (buckets));
4496 if (glob->num_entries > 0)
4498 struct global *g;
4500 /* Create an array of pointers, sorted by hash value. */
4502 sorted = xmalloc (sizeof (struct global *) * glob->num_entries);
4504 g = glob->first;
4505 for (unsigned int i = 0; i < glob->num_entries; i++)
4507 sorted[i] = g;
4508 g = g->next;
4511 qsort (sorted, glob->num_entries, sizeof (struct global *),
4512 global_compare_hash);
4514 /* Populate the buckets. */
4516 for (unsigned int i = 0; i < glob->num_entries; i++)
4518 if (!buckets[sorted[i]->hash])
4520 buckets[sorted[i]->hash] = sorted[i];
4521 filled_buckets++;
4524 sorted[i]->index = i;
4528 buckets_size = NUM_GLOBALS_HASH_BUCKETS / 8;
4529 buckets_size += sizeof (uint32_t);
4530 buckets_size += filled_buckets * sizeof (uint32_t);
4532 bfd_putl32 (GLOBALS_HASH_SIGNATURE, &h.signature);
4533 bfd_putl32 (GLOBALS_HASH_VERSION_70, &h.version);
4534 bfd_putl32 (glob->num_entries * sizeof (struct hash_record),
4535 &h.entries_size);
4536 bfd_putl32 (buckets_size, &h.buckets_size);
4538 if (bfd_write (&h, sizeof (h), stream) != sizeof (h))
4539 return false;
4541 /* Write hash entries, sorted by hash. */
4543 for (unsigned int i = 0; i < glob->num_entries; i++)
4545 struct hash_record hr;
4547 bfd_putl32 (sorted[i]->offset + 1, &hr.offset);
4548 bfd_putl32 (sorted[i]->refcount, &hr.reference);
4550 if (bfd_write (&hr, sizeof (hr), stream) != sizeof (hr))
4551 goto end;
4554 /* Write the bitmap for filled and unfilled buckets. */
4556 for (unsigned int i = 0; i < NUM_GLOBALS_HASH_BUCKETS; i += 8)
4558 uint8_t v = 0;
4560 for (unsigned int j = 0; j < 8; j++)
4562 if (buckets[i + j])
4563 v |= 1 << j;
4566 if (bfd_write (&v, sizeof (v), stream) != sizeof (v))
4567 goto end;
4570 /* Add a 4-byte gap. */
4572 bfd_putl32 (0, int_buf);
4574 if (bfd_write (int_buf, sizeof (uint32_t), stream) != sizeof (uint32_t))
4575 goto end;
4577 /* Write the bucket offsets. */
4579 for (unsigned int i = 0; i < NUM_GLOBALS_HASH_BUCKETS; i++)
4581 if (buckets[i])
4583 /* 0xc is size of internal hash_record structure in
4584 Microsoft's parser. */
4585 bfd_putl32 (buckets[i]->index * 0xc, int_buf);
4587 if (bfd_write (int_buf, sizeof (uint32_t), stream) !=
4588 sizeof (uint32_t))
4589 goto end;
4593 ret = true;
4595 end:
4596 free (sorted);
4598 return ret;
4601 /* Hash an entry in the globals list. */
4602 static hashval_t
4603 hash_global_entry (const void *p)
4605 const struct global *g = (const struct global *) p;
4606 uint16_t len = bfd_getl16 (g->data);
4608 return iterative_hash (g->data, len, 0);
4611 /* Compare an entry in the globals list with a symbol. */
4612 static int
4613 eq_global_entry (const void *a, const void *b)
4615 const struct global *g = (const struct global *) a;
4616 uint16_t len1, len2;
4618 len1 = bfd_getl16 (g->data) + sizeof (uint16_t);
4619 len2 = bfd_getl16 (b) + sizeof (uint16_t);
4621 if (len1 != len2)
4622 return 0;
4624 return !memcmp (g->data, b, len1);
4627 /* Stream 4 is the debug information (DBI) stream. */
4628 static bool
4629 populate_dbi_stream (bfd *stream, bfd *abfd, bfd *pdb,
4630 uint16_t section_header_stream_num,
4631 uint16_t sym_rec_stream_num,
4632 uint16_t publics_stream_num,
4633 struct string_table *strings,
4634 struct types *types,
4635 struct types *ids,
4636 bfd *sym_rec_stream, const char *pdb_name)
4638 struct pdb_dbi_stream_header h;
4639 struct optional_dbg_header opt;
4640 void *mod_info, *sc, *source_info;
4641 uint32_t mod_info_size, sc_size, source_info_size;
4642 struct source_files_info source;
4643 struct globals glob;
4644 uint16_t globals_stream_num;
4646 source.mod_count = 0;
4647 source.mods = NULL;
4649 glob.num_entries = 0;
4650 glob.first = NULL;
4651 glob.last = NULL;
4653 glob.hashmap = htab_create_alloc (0, hash_global_entry,
4654 eq_global_entry, free, xcalloc, free);
4656 if (!create_module_info_substream (abfd, pdb, &mod_info, &mod_info_size,
4657 strings, &source, types, ids,
4658 sym_rec_stream, &glob, pdb_name))
4660 htab_delete (glob.hashmap);
4661 return false;
4664 if (!create_globals_stream (pdb, &glob, &globals_stream_num))
4666 htab_delete (glob.hashmap);
4668 for (unsigned int i = 0; i < source.mod_count; i++)
4670 free (source.mods[i].files);
4672 free (source.mods);
4674 free (mod_info);
4675 return false;
4678 htab_delete (glob.hashmap);
4680 if (!create_section_contrib_substream (abfd, &sc, &sc_size))
4682 for (unsigned int i = 0; i < source.mod_count; i++)
4684 free (source.mods[i].files);
4686 free (source.mods);
4688 free (mod_info);
4689 return false;
4692 create_source_info_substream (&source_info, &source_info_size, &source);
4694 for (unsigned int i = 0; i < source.mod_count; i++)
4696 free (source.mods[i].files);
4698 free (source.mods);
4700 bfd_putl32 (0xffffffff, &h.version_signature);
4701 bfd_putl32 (DBI_STREAM_VERSION_70, &h.version_header);
4702 bfd_putl32 (1, &h.age);
4703 bfd_putl16 (globals_stream_num, &h.global_stream_index);
4704 bfd_putl16 (0x8e1d, &h.build_number); // MSVC 14.29
4705 bfd_putl16 (publics_stream_num, &h.public_stream_index);
4706 bfd_putl16 (0, &h.pdb_dll_version);
4707 bfd_putl16 (sym_rec_stream_num, &h.sym_record_stream);
4708 bfd_putl16 (0, &h.pdb_dll_rbld);
4709 bfd_putl32 (mod_info_size, &h.mod_info_size);
4710 bfd_putl32 (sc_size, &h.section_contribution_size);
4711 bfd_putl32 (0, &h.section_map_size);
4712 bfd_putl32 (source_info_size, &h.source_info_size);
4713 bfd_putl32 (0, &h.type_server_map_size);
4714 bfd_putl32 (0, &h.mfc_type_server_index);
4715 bfd_putl32 (sizeof (opt), &h.optional_dbg_header_size);
4716 bfd_putl32 (0, &h.ec_substream_size);
4717 bfd_putl16 (0, &h.flags);
4718 bfd_putl16 (get_arch_number (abfd), &h.machine);
4719 bfd_putl32 (0, &h.padding);
4721 if (bfd_write (&h, sizeof (h), stream) != sizeof (h))
4723 free (source_info);
4724 free (sc);
4725 free (mod_info);
4726 return false;
4729 if (bfd_write (mod_info, mod_info_size, stream) != mod_info_size)
4731 free (source_info);
4732 free (sc);
4733 free (mod_info);
4734 return false;
4737 free (mod_info);
4739 if (bfd_write (sc, sc_size, stream) != sc_size)
4741 free (source_info);
4742 free (sc);
4743 return false;
4746 free (sc);
4748 if (bfd_write (source_info, source_info_size, stream) != source_info_size)
4750 free (source_info);
4751 return false;
4754 free (source_info);
4756 bfd_putl16 (0xffff, &opt.fpo_stream);
4757 bfd_putl16 (0xffff, &opt.exception_stream);
4758 bfd_putl16 (0xffff, &opt.fixup_stream);
4759 bfd_putl16 (0xffff, &opt.omap_to_src_stream);
4760 bfd_putl16 (0xffff, &opt.omap_from_src_stream);
4761 bfd_putl16 (section_header_stream_num, &opt.section_header_stream);
4762 bfd_putl16 (0xffff, &opt.token_map_stream);
4763 bfd_putl16 (0xffff, &opt.xdata_stream);
4764 bfd_putl16 (0xffff, &opt.pdata_stream);
4765 bfd_putl16 (0xffff, &opt.new_fpo_stream);
4766 bfd_putl16 (0xffff, &opt.orig_section_header_stream);
4768 if (bfd_write (&opt, sizeof (opt), stream) != sizeof (opt))
4769 return false;
4771 return true;
4774 /* Used as parameter to qsort, to sort publics by hash. */
4775 static int
4776 public_compare_hash (const void *s1, const void *s2)
4778 const struct public *p1 = *(const struct public **) s1;
4779 const struct public *p2 = *(const struct public **) s2;
4781 if (p1->hash < p2->hash)
4782 return -1;
4783 if (p1->hash > p2->hash)
4784 return 1;
4786 return 0;
4789 /* Used as parameter to qsort, to sort publics by address. */
4790 static int
4791 public_compare_addr (const void *s1, const void *s2)
4793 const struct public *p1 = *(const struct public **) s1;
4794 const struct public *p2 = *(const struct public **) s2;
4796 if (p1->section < p2->section)
4797 return -1;
4798 if (p1->section > p2->section)
4799 return 1;
4801 if (p1->address < p2->address)
4802 return -1;
4803 if (p1->address > p2->address)
4804 return 1;
4806 return 0;
4809 /* The publics stream is a hash map of S_PUB32 records, which are stored
4810 in the symbol record stream. Each S_PUB32 entry represents a symbol
4811 from the point of view of the linker: a section index, an offset within
4812 the section, and a mangled name. Compare with S_GDATA32 and S_GPROC32,
4813 which are the same thing but generated by the compiler. */
4814 static bool
4815 populate_publics_stream (bfd *stream, bfd *abfd, bfd *sym_rec_stream)
4817 struct publics_header header;
4818 struct globals_hash_header hash_header;
4819 const unsigned int num_buckets = 4096;
4820 unsigned int num_entries = 0, filled_buckets = 0;
4821 unsigned int buckets_size, sym_hash_size;
4822 char int_buf[sizeof (uint32_t)];
4823 struct public *publics_head = NULL, *publics_tail = NULL;
4824 struct public **buckets;
4825 struct public **sorted = NULL;
4826 bool ret = false;
4828 buckets = xmalloc (sizeof (struct public *) * num_buckets);
4829 memset (buckets, 0, sizeof (struct public *) * num_buckets);
4831 /* Loop through the global symbols in our input files, and write S_PUB32
4832 records in the symbol record stream for those that make it into the
4833 final image. */
4834 for (bfd *in = coff_data (abfd)->link_info->input_bfds; in;
4835 in = in->link.next)
4837 if (!in->outsymbols)
4838 continue;
4840 for (unsigned int i = 0; i < in->symcount; i++)
4842 struct bfd_symbol *sym = in->outsymbols[i];
4844 if (sym->flags & BSF_GLOBAL)
4846 struct pubsym ps;
4847 uint16_t record_length;
4848 const char *name = sym->name;
4849 size_t name_len = strlen (name);
4850 struct public *p = xmalloc (sizeof (struct public));
4851 unsigned int padding = 0;
4852 uint16_t section;
4853 uint32_t flags = 0;
4855 section =
4856 find_section_number (abfd, sym->section->output_section);
4858 if (section == 0)
4859 continue;
4861 p->next = NULL;
4862 p->offset = bfd_tell (sym_rec_stream);
4863 p->hash = calc_hash (name, name_len) % num_buckets;
4864 p->section = section;
4865 p->address = sym->section->output_offset + sym->value;
4867 record_length = sizeof (struct pubsym) + name_len + 1;
4869 if (record_length % 4)
4870 padding = 4 - (record_length % 4);
4872 /* Assume that all global symbols in executable sections
4873 are functions. */
4874 if (sym->section->flags & SEC_CODE)
4875 flags = PUBSYM_FUNCTION;
4877 bfd_putl16 (record_length + padding - sizeof (uint16_t),
4878 &ps.record_length);
4879 bfd_putl16 (S_PUB32, &ps.record_type);
4880 bfd_putl32 (flags, &ps.flags);
4881 bfd_putl32 (p->address, &ps.offset);
4882 bfd_putl16 (p->section, &ps.section);
4884 if (bfd_write (&ps, sizeof (struct pubsym), sym_rec_stream) !=
4885 sizeof (struct pubsym))
4886 goto end;
4888 if (bfd_write (name, name_len + 1, sym_rec_stream) !=
4889 name_len + 1)
4890 goto end;
4892 for (unsigned int j = 0; j < padding; j++)
4894 uint8_t b = 0;
4896 if (bfd_write (&b, sizeof (uint8_t), sym_rec_stream) !=
4897 sizeof (uint8_t))
4898 goto end;
4901 if (!publics_head)
4902 publics_head = p;
4903 else
4904 publics_tail->next = p;
4906 publics_tail = p;
4907 num_entries++;
4913 if (num_entries > 0)
4915 /* Create an array of pointers, sorted by hash value. */
4917 sorted = xmalloc (sizeof (struct public *) * num_entries);
4919 struct public *p = publics_head;
4920 for (unsigned int i = 0; i < num_entries; i++)
4922 sorted[i] = p;
4923 p = p->next;
4926 qsort (sorted, num_entries, sizeof (struct public *),
4927 public_compare_hash);
4929 /* Populate the buckets. */
4931 for (unsigned int i = 0; i < num_entries; i++)
4933 if (!buckets[sorted[i]->hash])
4935 buckets[sorted[i]->hash] = sorted[i];
4936 filled_buckets++;
4939 sorted[i]->index = i;
4943 buckets_size = num_buckets / 8;
4944 buckets_size += sizeof (uint32_t);
4945 buckets_size += filled_buckets * sizeof (uint32_t);
4947 sym_hash_size = sizeof (hash_header);
4948 sym_hash_size += num_entries * sizeof (struct hash_record);
4949 sym_hash_size += buckets_size;
4951 /* Output the publics header. */
4953 bfd_putl32 (sym_hash_size, &header.sym_hash_size);
4954 bfd_putl32 (num_entries * sizeof (uint32_t), &header.addr_map_size);
4955 bfd_putl32 (0, &header.num_thunks);
4956 bfd_putl32 (0, &header.thunks_size);
4957 bfd_putl32 (0, &header.thunk_table);
4958 bfd_putl32 (0, &header.thunk_table_offset);
4959 bfd_putl32 (0, &header.num_sects);
4961 if (bfd_write (&header, sizeof (header), stream) != sizeof (header))
4962 goto end;
4964 /* Output the global hash header. */
4966 bfd_putl32 (GLOBALS_HASH_SIGNATURE, &hash_header.signature);
4967 bfd_putl32 (GLOBALS_HASH_VERSION_70, &hash_header.version);
4968 bfd_putl32 (num_entries * sizeof (struct hash_record),
4969 &hash_header.entries_size);
4970 bfd_putl32 (buckets_size, &hash_header.buckets_size);
4972 if (bfd_write (&hash_header, sizeof (hash_header), stream) !=
4973 sizeof (hash_header))
4974 goto end;
4976 /* Write the entries in hash order. */
4978 for (unsigned int i = 0; i < num_entries; i++)
4980 struct hash_record hr;
4982 bfd_putl32 (sorted[i]->offset + 1, &hr.offset);
4983 bfd_putl32 (1, &hr.reference);
4985 if (bfd_write (&hr, sizeof (hr), stream) != sizeof (hr))
4986 goto end;
4989 /* Write the bitmap for filled and unfilled buckets. */
4991 for (unsigned int i = 0; i < num_buckets; i += 8)
4993 uint8_t v = 0;
4995 for (unsigned int j = 0; j < 8; j++)
4997 if (buckets[i + j])
4998 v |= 1 << j;
5001 if (bfd_write (&v, sizeof (v), stream) != sizeof (v))
5002 goto end;
5005 /* Add a 4-byte gap. */
5007 bfd_putl32 (0, int_buf);
5009 if (bfd_write (int_buf, sizeof (uint32_t), stream) != sizeof (uint32_t))
5010 goto end;
5012 /* Write the bucket offsets. */
5014 for (unsigned int i = 0; i < num_buckets; i++)
5016 if (buckets[i])
5018 /* 0xc is size of internal hash_record structure in
5019 Microsoft's parser. */
5020 bfd_putl32 (buckets[i]->index * 0xc, int_buf);
5022 if (bfd_write (int_buf, sizeof (uint32_t), stream) !=
5023 sizeof (uint32_t))
5024 goto end;
5028 /* Write the address map: offsets into the symbol record stream of
5029 S_PUB32 records, ordered by address. */
5031 if (num_entries > 0)
5033 qsort (sorted, num_entries, sizeof (struct public *),
5034 public_compare_addr);
5036 for (unsigned int i = 0; i < num_entries; i++)
5038 bfd_putl32 (sorted[i]->offset, int_buf);
5040 if (bfd_write (int_buf, sizeof (uint32_t), stream) !=
5041 sizeof (uint32_t))
5042 goto end;
5046 ret = true;
5048 end:
5049 free (buckets);
5051 while (publics_head)
5053 struct public *p = publics_head->next;
5055 free (publics_head);
5056 publics_head = p;
5059 free (sorted);
5061 return ret;
5064 /* The section header stream contains a copy of the section headers
5065 from the PE file, in the same format. */
5066 static bool
5067 create_section_header_stream (bfd *pdb, bfd *abfd, uint16_t *num)
5069 bfd *stream;
5070 unsigned int section_count;
5071 file_ptr scn_base;
5072 size_t len;
5073 char *buf;
5075 stream = add_stream (pdb, NULL, num);
5076 if (!stream)
5077 return false;
5079 section_count = abfd->section_count;
5081 /* Empty sections aren't output. */
5082 for (asection *sect = abfd->sections; sect; sect = sect->next)
5084 if (sect->size == 0)
5085 section_count--;
5088 if (section_count == 0)
5089 return true;
5091 /* Copy section table from output - it's already been written at this
5092 point. */
5094 scn_base = bfd_coff_filhsz (abfd) + bfd_coff_aoutsz (abfd);
5096 if (bfd_seek (abfd, scn_base, SEEK_SET) != 0)
5097 return false;
5099 len = section_count * sizeof (struct external_scnhdr);
5100 buf = xmalloc (len);
5102 if (bfd_read (buf, len, abfd) != len)
5104 free (buf);
5105 return false;
5108 if (bfd_write (buf, len, stream) != len)
5110 free (buf);
5111 return false;
5114 free (buf);
5116 return true;
5119 /* Populate the "/names" named stream, which contains the string table. */
5120 static bool
5121 populate_names_stream (bfd *stream, struct string_table *strings)
5123 char int_buf[sizeof (uint32_t)];
5124 struct string_table_header h;
5125 uint32_t num_strings = 0, num_buckets;
5126 struct string **buckets;
5128 bfd_putl32 (STRING_TABLE_SIGNATURE, &h.signature);
5129 bfd_putl32 (STRING_TABLE_VERSION, &h.version);
5131 if (bfd_write (&h, sizeof (h), stream) != sizeof (h))
5132 return false;
5134 bfd_putl32 (strings->strings_len, int_buf);
5136 if (bfd_write (int_buf, sizeof (uint32_t), stream) != sizeof (uint32_t))
5137 return false;
5139 int_buf[0] = 0;
5141 if (bfd_write (int_buf, 1, stream) != 1)
5142 return false;
5144 for (struct string *s = strings->strings_head; s; s = s->next)
5146 if (bfd_write (s->s, s->len, stream) != s->len)
5147 return false;
5149 if (bfd_write (int_buf, 1, stream) != 1)
5150 return false;
5152 num_strings++;
5155 num_buckets = num_strings * 2;
5157 buckets = xmalloc (sizeof (struct string *) * num_buckets);
5158 memset (buckets, 0, sizeof (struct string *) * num_buckets);
5160 for (struct string *s = strings->strings_head; s; s = s->next)
5162 uint32_t bucket_num = s->hash % num_buckets;
5164 while (buckets[bucket_num])
5166 bucket_num++;
5168 if (bucket_num == num_buckets)
5169 bucket_num = 0;
5172 buckets[bucket_num] = s;
5175 bfd_putl32 (num_buckets, int_buf);
5177 if (bfd_write (int_buf, sizeof (uint32_t), stream) != sizeof (uint32_t))
5179 free (buckets);
5180 return false;
5183 for (unsigned int i = 0; i < num_buckets; i++)
5185 if (buckets[i])
5186 bfd_putl32 (buckets[i]->offset, int_buf);
5187 else
5188 bfd_putl32 (0, int_buf);
5190 if (bfd_write (int_buf, sizeof (uint32_t), stream) !=
5191 sizeof (uint32_t))
5193 free (buckets);
5194 return false;
5198 free (buckets);
5200 bfd_putl32 (num_strings, int_buf);
5202 if (bfd_write (int_buf, sizeof (uint32_t), stream) != sizeof (uint32_t))
5203 return false;
5205 return true;
5208 /* Calculate the hash of a type_entry. */
5209 static hashval_t
5210 hash_type_entry (const void *p)
5212 const struct type_entry *e = (const struct type_entry *) p;
5213 uint16_t size = bfd_getl16 (e->data) + sizeof (uint16_t);
5215 return iterative_hash (e->data, size, 0);
5218 /* Compare a type_entry with a type. */
5219 static int
5220 eq_type_entry (const void *a, const void *b)
5222 const struct type_entry *e = (const struct type_entry *) a;
5223 uint16_t size_a = bfd_getl16 (e->data);
5224 uint16_t size_b = bfd_getl16 (b);
5226 if (size_a != size_b)
5227 return 0;
5229 return memcmp (e->data + sizeof (uint16_t),
5230 (const uint8_t *) b + sizeof (uint16_t), size_a) == 0;
5233 /* Create a PDB debugging file for the PE image file abfd with the build ID
5234 guid, stored at pdb_name. */
5235 bool
5236 create_pdb_file (bfd *abfd, const char *pdb_name, const unsigned char *guid)
5238 bfd *pdb;
5239 bool ret = false;
5240 bfd *info_stream, *dbi_stream, *names_stream, *sym_rec_stream,
5241 *publics_stream, *tpi_stream, *ipi_stream;
5242 uint16_t section_header_stream_num, sym_rec_stream_num, publics_stream_num;
5243 struct string_table strings;
5244 struct types types, ids;
5246 pdb = bfd_openw (pdb_name, "pdb");
5247 if (!pdb)
5249 einfo (_("%P: warning: cannot create PDB file: %E\n"));
5250 return false;
5253 strings.strings_head = NULL;
5254 strings.strings_tail = NULL;
5255 strings.strings_len = 1;
5256 strings.hashmap = htab_create_alloc (0, hash_string_table_entry,
5257 eq_string_table_entry, free,
5258 xcalloc, free);
5260 bfd_set_format (pdb, bfd_archive);
5262 if (!create_old_directory_stream (pdb))
5264 einfo (_("%P: warning: cannot create old directory stream "
5265 "in PDB file: %E\n"));
5266 goto end;
5269 info_stream = add_stream (pdb, NULL, NULL);
5271 if (!info_stream)
5273 einfo (_("%P: warning: cannot create info stream "
5274 "in PDB file: %E\n"));
5275 goto end;
5278 tpi_stream = add_stream (pdb, NULL, NULL);
5280 if (!tpi_stream)
5282 einfo (_("%P: warning: cannot create TPI stream "
5283 "in PDB file: %E\n"));
5284 goto end;
5287 dbi_stream = add_stream (pdb, NULL, NULL);
5289 if (!dbi_stream)
5291 einfo (_("%P: warning: cannot create DBI stream "
5292 "in PDB file: %E\n"));
5293 goto end;
5296 ipi_stream = add_stream (pdb, NULL, NULL);
5298 if (!ipi_stream)
5300 einfo (_("%P: warning: cannot create IPI stream "
5301 "in PDB file: %E\n"));
5302 goto end;
5305 names_stream = add_stream (pdb, "/names", NULL);
5307 if (!names_stream)
5309 einfo (_("%P: warning: cannot create /names stream "
5310 "in PDB file: %E\n"));
5311 goto end;
5314 sym_rec_stream = add_stream (pdb, NULL, &sym_rec_stream_num);
5316 if (!sym_rec_stream)
5318 einfo (_("%P: warning: cannot create symbol record stream "
5319 "in PDB file: %E\n"));
5320 goto end;
5323 publics_stream = add_stream (pdb, NULL, &publics_stream_num);
5325 if (!publics_stream)
5327 einfo (_("%P: warning: cannot create publics stream "
5328 "in PDB file: %E\n"));
5329 goto end;
5332 if (!create_section_header_stream (pdb, abfd, &section_header_stream_num))
5334 einfo (_("%P: warning: cannot create section header stream "
5335 "in PDB file: %E\n"));
5336 goto end;
5339 types.num_types = 0;
5340 types.hashmap = htab_create_alloc (0, hash_type_entry, eq_type_entry,
5341 free, xcalloc, free);
5342 types.first = types.last = NULL;
5344 ids.num_types = 0;
5345 ids.hashmap = htab_create_alloc (0, hash_type_entry, eq_type_entry,
5346 free, xcalloc, free);
5347 ids.first = ids.last = NULL;
5349 if (!populate_dbi_stream (dbi_stream, abfd, pdb, section_header_stream_num,
5350 sym_rec_stream_num, publics_stream_num,
5351 &strings, &types, &ids, sym_rec_stream, pdb_name))
5353 einfo (_("%P: warning: cannot populate DBI stream "
5354 "in PDB file: %E\n"));
5355 htab_delete (types.hashmap);
5356 htab_delete (ids.hashmap);
5357 goto end;
5360 if (!populate_type_stream (pdb, tpi_stream, &types))
5362 einfo (_("%P: warning: cannot populate TPI stream "
5363 "in PDB file: %E\n"));
5364 htab_delete (types.hashmap);
5365 htab_delete (ids.hashmap);
5366 goto end;
5369 htab_delete (types.hashmap);
5371 if (!populate_type_stream (pdb, ipi_stream, &ids))
5373 einfo (_("%P: warning: cannot populate IPI stream "
5374 "in PDB file: %E\n"));
5375 htab_delete (ids.hashmap);
5376 goto end;
5379 htab_delete (ids.hashmap);
5381 add_string ("", 0, &strings);
5383 if (!populate_names_stream (names_stream, &strings))
5385 einfo (_("%P: warning: cannot populate names stream "
5386 "in PDB file: %E\n"));
5387 goto end;
5390 if (!populate_publics_stream (publics_stream, abfd, sym_rec_stream))
5392 einfo (_("%P: warning: cannot populate publics stream "
5393 "in PDB file: %E\n"));
5394 goto end;
5397 if (!populate_info_stream (pdb, info_stream, guid))
5399 einfo (_("%P: warning: cannot populate info stream "
5400 "in PDB file: %E\n"));
5401 goto end;
5404 ret = true;
5406 end:
5407 bfd_close (pdb);
5409 htab_delete (strings.hashmap);
5411 return ret;