1 /* Support for generating PDB CodeView debugging files.
2 Copyright (C) 2022 Free Software Foundation, Inc.
4 This file is part of the GNU Binutils.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
26 #include "libiberty.h"
27 #include "coff/i386.h"
28 #include "coff/external.h"
29 #include "coff/internal.h"
49 uint32_t source_file_offset
;
56 struct string
*strings_head
;
57 struct string
*strings_tail
;
62 struct mod_source_files
65 struct string
**files
;
68 struct source_files_info
71 struct mod_source_files
*mods
;
76 struct type_entry
*next
;
86 struct type_entry
*first
;
87 struct type_entry
*last
;
90 static const uint32_t crc_table
[] =
92 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f,
93 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988,
94 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2,
95 0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7,
96 0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9,
97 0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172,
98 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, 0x42b2986c,
99 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59,
100 0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423,
101 0xcfba9599, 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924,
102 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, 0x01db7106,
103 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433,
104 0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d,
105 0x91646c97, 0xe6635c01, 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e,
106 0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950,
107 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65,
108 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7,
109 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0,
110 0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa,
111 0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
112 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81,
113 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a,
114 0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683, 0xe3630b12, 0x94643b84,
115 0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1,
116 0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb,
117 0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc,
118 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 0xd6d6a3e8, 0xa1d1937e,
119 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b,
120 0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55,
121 0x316e8eef, 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236,
122 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, 0xb2bd0b28,
123 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d,
124 0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f,
125 0x72076785, 0x05005713, 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38,
126 0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242,
127 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777,
128 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69,
129 0x616bffd3, 0x166ccf45, 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2,
130 0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc,
131 0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
132 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693,
133 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94,
134 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d
137 /* Add a new stream to the PDB archive, and return its BFD. */
139 add_stream (bfd
*pdb
, const char *name
, uint16_t *stream_num
)
144 stream
= bfd_create (name
? name
: "", pdb
);
148 if (!bfd_make_writable (stream
))
154 if (!pdb
->archive_head
)
156 bfd_set_archive_head (pdb
, stream
);
161 bfd
*b
= pdb
->archive_head
;
165 while (b
->archive_next
)
171 b
->archive_next
= stream
;
180 /* Stream 0 ought to be a copy of the MSF directory from the last
181 time the PDB file was written. Because we don't do incremental
182 writes this isn't applicable to us, but we fill it with a dummy
183 value so as not to confuse radare. */
185 create_old_directory_stream (bfd
*pdb
)
188 char buf
[sizeof (uint32_t)];
190 stream
= add_stream (pdb
, NULL
, NULL
);
196 return bfd_bwrite (buf
, sizeof (uint32_t), stream
) == sizeof (uint32_t);
199 /* Calculate the hash of a given string. */
201 calc_hash (const char *data
, size_t len
)
208 hash
^= data
[1] << 8;
209 hash
^= data
[2] << 16;
210 hash
^= data
[3] << 24;
219 hash
^= data
[1] << 8;
229 hash
^= (hash
>> 11);
231 return hash
^ (hash
>> 16);
234 /* Stream 1 is the PDB info stream - see
235 https://llvm.org/docs/PDB/PdbStream.html. */
237 populate_info_stream (bfd
*pdb
, bfd
*info_stream
, const unsigned char *guid
)
240 struct pdb_stream_70 h
;
241 uint32_t num_entries
, num_buckets
;
242 uint32_t names_length
, stream_num
;
243 char int_buf
[sizeof (uint32_t)];
251 struct hash_entry
**buckets
= NULL
;
255 bfd_putl32 (PDB_STREAM_VERSION_VC70
, &h
.version
);
256 bfd_putl32 (time (NULL
), &h
.signature
);
257 bfd_putl32 (1, &h
.age
);
259 bfd_putl32 (bfd_getb32 (guid
), h
.guid
);
260 bfd_putl16 (bfd_getb16 (&guid
[4]), &h
.guid
[4]);
261 bfd_putl16 (bfd_getb16 (&guid
[6]), &h
.guid
[6]);
262 memcpy (&h
.guid
[8], &guid
[8], 8);
264 if (bfd_bwrite (&h
, sizeof (h
), info_stream
) != sizeof (h
))
267 /* Write hash list of named streams. This is a "rollover" hash, i.e.
268 if a bucket is filled an entry gets placed in the next free
272 for (bfd
*b
= pdb
->archive_head
; b
; b
= b
->archive_next
)
274 if (strcmp (b
->filename
, ""))
278 num_buckets
= num_entries
* 2;
285 buckets
= xmalloc (sizeof (struct hash_entry
*) * num_buckets
);
286 memset (buckets
, 0, sizeof (struct hash_entry
*) * num_buckets
);
288 for (bfd
*b
= pdb
->archive_head
; b
; b
= b
->archive_next
)
290 if (strcmp (b
->filename
, ""))
292 size_t len
= strlen (b
->filename
);
293 uint32_t hash
= (uint16_t) calc_hash (b
->filename
, len
);
294 uint32_t bucket_num
= hash
% num_buckets
;
296 while (buckets
[bucket_num
])
300 if (bucket_num
== num_buckets
)
304 buckets
[bucket_num
] = xmalloc (sizeof (struct hash_entry
));
306 buckets
[bucket_num
]->offset
= names_length
;
307 buckets
[bucket_num
]->value
= stream_num
;
309 names_length
+= len
+ 1;
316 /* Write the strings list - the hash keys are indexes into this. */
318 bfd_putl32 (names_length
, int_buf
);
320 if (bfd_bwrite (int_buf
, sizeof (uint32_t), info_stream
) !=
324 for (bfd
*b
= pdb
->archive_head
; b
; b
= b
->archive_next
)
326 if (!strcmp (b
->filename
, ""))
329 size_t len
= strlen (b
->filename
) + 1;
331 if (bfd_bwrite (b
->filename
, len
, info_stream
) != len
)
335 /* Write the number of entries and buckets. */
337 bfd_putl32 (num_entries
, int_buf
);
339 if (bfd_bwrite (int_buf
, sizeof (uint32_t), info_stream
) !=
343 bfd_putl32 (num_buckets
, int_buf
);
345 if (bfd_bwrite (int_buf
, sizeof (uint32_t), info_stream
) !=
349 /* Write the present bitmap. */
351 bfd_putl32 ((num_buckets
+ 31) / 32, int_buf
);
353 if (bfd_bwrite (int_buf
, sizeof (uint32_t), info_stream
) !=
357 for (unsigned int i
= 0; i
< num_buckets
; i
+= 32)
361 for (unsigned int j
= 0; j
< 32; j
++)
363 if (i
+ j
>= num_buckets
)
370 bfd_putl32 (v
, int_buf
);
372 if (bfd_bwrite (int_buf
, sizeof (uint32_t), info_stream
) !=
377 /* Write the (empty) deleted bitmap. */
379 bfd_putl32 (0, int_buf
);
381 if (bfd_bwrite (int_buf
, sizeof (uint32_t), info_stream
) !=
385 /* Write the buckets. */
387 for (unsigned int i
= 0; i
< num_buckets
; i
++)
391 bfd_putl32 (buckets
[i
]->offset
, int_buf
);
393 if (bfd_bwrite (int_buf
, sizeof (uint32_t), info_stream
) !=
397 bfd_putl32 (buckets
[i
]->value
, int_buf
);
399 if (bfd_bwrite (int_buf
, sizeof (uint32_t), info_stream
) !=
405 bfd_putl32 (0, int_buf
);
407 if (bfd_bwrite (int_buf
, sizeof (uint32_t), info_stream
) !=
411 bfd_putl32 (PDB_STREAM_VERSION_VC140
, int_buf
);
413 if (bfd_bwrite (int_buf
, sizeof (uint32_t), info_stream
) !=
420 for (unsigned int i
= 0; i
< num_buckets
; i
++)
431 /* Calculate the CRC32 used for type hashes. */
433 crc32 (const uint8_t *data
, size_t len
)
439 crc
= (crc
>> 8) ^ crc_table
[(crc
& 0xff) ^ *data
];
448 /* Stream 2 is the type information (TPI) stream, and stream 4 is
449 the ID information (IPI) stream. They differ only in which records
450 go in which stream. */
452 populate_type_stream (bfd
*pdb
, bfd
*stream
, struct types
*types
)
454 struct pdb_tpi_stream_header h
;
455 struct type_entry
*e
;
456 uint32_t len
= 0, index_offset_len
, off
;
457 struct bfd
*hash_stream
= NULL
;
458 uint16_t hash_stream_index
;
460 static const uint32_t index_skip
= 0x2000;
464 index_offset_len
= 0;
468 uint32_t old_len
= len
;
470 len
+= sizeof (uint16_t) + bfd_getl16 (e
->data
);
472 if (old_len
== 0 || old_len
/ index_skip
!= len
/ index_skip
)
473 index_offset_len
+= sizeof (uint32_t) * 2;
478 /* Each type stream also has a stream which holds the hash value for each
479 type, along with a skip list to speed up searching. */
481 hash_stream
= add_stream (pdb
, "", &hash_stream_index
);
486 bfd_putl32 (TPI_STREAM_VERSION_80
, &h
.version
);
487 bfd_putl32 (sizeof (h
), &h
.header_size
);
488 bfd_putl32 (TPI_FIRST_INDEX
, &h
.type_index_begin
);
489 bfd_putl32 (TPI_FIRST_INDEX
+ types
->num_types
, &h
.type_index_end
);
490 bfd_putl32 (len
, &h
.type_record_bytes
);
491 bfd_putl16 (hash_stream_index
, &h
.hash_stream_index
);
492 bfd_putl16 (0xffff, &h
.hash_aux_stream_index
);
493 bfd_putl32 (sizeof (uint32_t), &h
.hash_key_size
);
494 bfd_putl32 (NUM_TPI_HASH_BUCKETS
, &h
.num_hash_buckets
);
495 bfd_putl32 (0, &h
.hash_value_buffer_offset
);
496 bfd_putl32 (types
->num_types
* sizeof (uint32_t),
497 &h
.hash_value_buffer_length
);
498 bfd_putl32 (types
->num_types
* sizeof (uint32_t),
499 &h
.index_offset_buffer_offset
);
500 bfd_putl32 (index_offset_len
, &h
.index_offset_buffer_length
);
501 bfd_putl32 ((types
->num_types
* sizeof (uint32_t)) + index_offset_len
,
502 &h
.hash_adj_buffer_offset
);
503 bfd_putl32 (0, &h
.hash_adj_buffer_length
);
505 if (bfd_bwrite (&h
, sizeof (h
), stream
) != sizeof (h
))
508 /* Write the type definitions into the main stream, and the hashes
509 into the hash stream. The hashes have already been calculated
516 uint8_t buf
[sizeof (uint32_t)];
519 size
= bfd_getl16 (e
->data
);
521 if (bfd_bwrite (e
->data
, size
+ sizeof (uint16_t), stream
)
522 != size
+ sizeof (uint16_t))
525 bfd_putl32 (e
->cv_hash
% NUM_TPI_HASH_BUCKETS
, buf
);
527 if (bfd_bwrite (buf
, sizeof (uint32_t), hash_stream
)
528 != sizeof (uint32_t))
534 /* Write the index offsets, i.e. the skip list, into the hash stream. We
535 copy MSVC here by writing a new entry for every 8192 bytes. */
542 uint32_t old_off
= off
;
543 uint16_t size
= bfd_getl16 (e
->data
);
545 off
+= size
+ sizeof (uint16_t);
547 if (old_off
== 0 || old_off
/ index_skip
!= len
/ index_skip
)
549 uint8_t buf
[sizeof (uint32_t)];
551 bfd_putl32 (TPI_FIRST_INDEX
+ e
->index
, buf
);
553 if (bfd_bwrite (buf
, sizeof (uint32_t), hash_stream
)
554 != sizeof (uint32_t))
557 bfd_putl32 (old_off
, buf
);
559 if (bfd_bwrite (buf
, sizeof (uint32_t), hash_stream
)
560 != sizeof (uint32_t))
570 /* Return the PE architecture number for the image. */
572 get_arch_number (bfd
*abfd
)
574 if (abfd
->arch_info
->arch
!= bfd_arch_i386
)
577 if (abfd
->arch_info
->mach
& bfd_mach_x86_64
)
578 return IMAGE_FILE_MACHINE_AMD64
;
580 return IMAGE_FILE_MACHINE_I386
;
583 /* Validate the DEBUG_S_FILECHKSMS entry within a module's .debug$S
584 section, and copy it to the module's symbol stream. */
586 copy_filechksms (uint8_t *data
, uint32_t size
, char *string_table
,
587 struct string_table
*strings
, uint8_t *out
,
588 struct mod_source_files
*mod_source
)
590 uint8_t *orig_data
= data
;
591 uint32_t orig_size
= size
;
592 uint16_t num_files
= 0;
593 struct string
**strptr
;
595 bfd_putl32 (DEBUG_S_FILECHKSMS
, out
);
596 out
+= sizeof (uint32_t);
598 bfd_putl32 (size
, out
);
599 out
+= sizeof (uint32_t);
601 /* Calculate the number of files, and check for any overflows. */
605 struct file_checksum
*fc
= (struct file_checksum
*) data
;
609 if (size
< sizeof (struct file_checksum
))
611 bfd_set_error (bfd_error_bad_value
);
615 len
= sizeof (struct file_checksum
) + fc
->checksum_length
;
619 bfd_set_error (bfd_error_bad_value
);
626 if (len
% sizeof (uint32_t))
627 padding
= sizeof (uint32_t) - (len
% sizeof (uint32_t));
633 bfd_set_error (bfd_error_bad_value
);
643 /* Add the files to mod_source, so that they'll appear in the source
649 uint16_t new_count
= num_files
+ mod_source
->files_count
;
651 mod_source
->files
= xrealloc (mod_source
->files
,
652 sizeof (struct string
*) * new_count
);
654 strptr
= mod_source
->files
+ mod_source
->files_count
;
656 mod_source
->files_count
+= num_files
;
659 /* Actually copy the data. */
666 struct file_checksum
*fc
= (struct file_checksum
*) data
;
670 struct string
*str
= NULL
;
672 string_off
= bfd_getl32 (&fc
->file_id
);
673 len
= sizeof (struct file_checksum
) + fc
->checksum_length
;
675 if (len
% sizeof (uint32_t))
676 padding
= sizeof (uint32_t) - (len
% sizeof (uint32_t));
680 /* Remap the "file ID", i.e. the offset in the module's string table,
681 so it points to the right place in the main string table. */
685 char *fn
= string_table
+ string_off
;
686 size_t fn_len
= strlen (fn
);
687 uint32_t hash
= calc_hash (fn
, fn_len
);
690 slot
= htab_find_slot_with_hash (strings
->hashmap
, fn
, hash
,
694 str
= (struct string
*) *slot
;
700 bfd_putl32 (str
? str
->offset
: 0, &fc
->file_id
);
702 memcpy (out
, data
, len
+ padding
);
704 data
+= len
+ padding
;
705 size
-= len
+ padding
;
706 out
+= len
+ padding
;
712 /* Add a string to the strings table, if it's not already there. */
714 add_string (char *str
, size_t len
, struct string_table
*strings
)
716 uint32_t hash
= calc_hash (str
, len
);
719 slot
= htab_find_slot_with_hash (strings
->hashmap
, str
, hash
, INSERT
);
725 *slot
= xmalloc (offsetof (struct string
, s
) + len
);
727 s
= (struct string
*) *slot
;
731 s
->offset
= strings
->strings_len
;
732 s
->source_file_offset
= 0xffffffff;
734 memcpy (s
->s
, str
, len
);
736 if (strings
->strings_tail
)
737 strings
->strings_tail
->next
= s
;
739 strings
->strings_head
= s
;
741 strings
->strings_tail
= s
;
743 strings
->strings_len
+= len
+ 1;
747 /* Return the hash of an entry in the string table. */
749 hash_string_table_entry (const void *p
)
751 const struct string
*s
= (const struct string
*) p
;
756 /* Compare an entry in the string table with a string. */
758 eq_string_table_entry (const void *a
, const void *b
)
760 const struct string
*s1
= (const struct string
*) a
;
761 const char *s2
= (const char *) b
;
762 size_t s2_len
= strlen (s2
);
764 if (s2_len
!= s1
->len
)
767 return memcmp (s1
->s
, s2
, s2_len
) == 0;
770 /* Parse the string table within the .debug$S section. */
772 parse_string_table (bfd_byte
*data
, size_t size
,
773 struct string_table
*strings
)
777 size_t len
= strnlen ((char *) data
, size
);
779 add_string ((char *) data
, len
, strings
);
790 /* Return the size of an extended value parameter, as used in
793 extended_value_len (uint16_t type
)
816 /* Parse the .debug$S section within an object file. */
818 handle_debugs_section (asection
*s
, bfd
*mod
, struct string_table
*strings
,
819 uint8_t **dataptr
, uint32_t *sizeptr
,
820 struct mod_source_files
*mod_source
,
823 bfd_byte
*data
= NULL
;
825 uint32_t c13_size
= 0;
826 char *string_table
= NULL
;
827 uint8_t *buf
, *bufptr
;
829 if (!bfd_get_full_section_contents (mod
, s
, &data
))
835 /* Resolve relocations. Addresses are stored within the .debug$S section as
836 a .secidx, .secrel32 pair. */
838 if (s
->flags
& SEC_RELOC
)
840 struct internal_reloc
*relocs
;
841 struct internal_syment
*symbols
;
843 unsigned int syment_count
;
845 struct external_syment
*ext
;
847 syment_count
= obj_raw_syment_count (mod
);
850 _bfd_coff_read_internal_relocs (mod
, s
, false, NULL
, true, NULL
);
852 symbols
= xmalloc (sizeof (struct internal_syment
) * syment_count
);
853 sectlist
= xmalloc (sizeof (asection
*) * syment_count
);
855 ext
= (struct external_syment
*) (coff_data (mod
)->external_syms
);
857 for (unsigned int i
= 0; i
< syment_count
; i
++)
859 bfd_coff_swap_sym_in (mod
, &ext
[i
], &symbols
[i
]);
864 for (asection
*sect
= mod
->sections
; sect
; sect
= sect
->next
)
866 for (unsigned int i
= 0; i
< syment_count
; i
++)
868 if (symbols
[i
].n_scnum
== sect_num
)
875 if (!bfd_coff_relocate_section (abfd
, coff_data (abfd
)->link_info
, mod
,
876 s
, data
, relocs
, symbols
, sectlist
))
888 if (bfd_getl32 (data
) != CV_SIGNATURE_C13
)
894 off
= sizeof (uint32_t);
898 while (off
+ sizeof (uint32_t) <= s
->size
)
902 type
= bfd_getl32 (data
+ off
);
904 off
+= sizeof (uint32_t);
906 if (off
+ sizeof (uint32_t) > s
->size
)
909 bfd_set_error (bfd_error_bad_value
);
913 size
= bfd_getl32 (data
+ off
);
915 off
+= sizeof (uint32_t);
917 if (off
+ size
> s
->size
)
920 bfd_set_error (bfd_error_bad_value
);
926 case DEBUG_S_FILECHKSMS
:
927 c13_size
+= sizeof (uint32_t) + sizeof (uint32_t) + size
;
929 if (c13_size
% sizeof (uint32_t))
930 c13_size
+= sizeof (uint32_t) - (c13_size
% sizeof (uint32_t));
934 case DEBUG_S_STRINGTABLE
:
935 parse_string_table (data
+ off
, size
, strings
);
937 string_table
= (char *) data
+ off
;
945 if (size
< sizeof (uint32_t) + sizeof (uint16_t))
948 bfd_set_error (bfd_error_bad_value
);
952 sect
= bfd_getl16 (data
+ off
+ sizeof (uint32_t));
954 /* Skip GC'd symbols. */
957 c13_size
+= sizeof (uint32_t) + sizeof (uint32_t) + size
;
959 if (c13_size
% sizeof (uint32_t))
961 sizeof (uint32_t) - (c13_size
% sizeof (uint32_t));
970 if (off
% sizeof (uint32_t))
971 off
+= sizeof (uint32_t) - (off
% sizeof (uint32_t));
982 buf
= xmalloc (c13_size
);
985 off
= sizeof (uint32_t);
987 while (off
+ sizeof (uint32_t) <= s
->size
)
991 type
= bfd_getl32 (data
+ off
);
992 off
+= sizeof (uint32_t);
994 size
= bfd_getl32 (data
+ off
);
995 off
+= sizeof (uint32_t);
999 case DEBUG_S_FILECHKSMS
:
1000 if (!copy_filechksms (data
+ off
, size
, string_table
,
1001 strings
, bufptr
, mod_source
))
1007 bufptr
+= sizeof (uint32_t) + sizeof (uint32_t) + size
;
1015 sect
= bfd_getl16 (data
+ off
+ sizeof (uint32_t));
1020 bfd_putl32 (type
, bufptr
);
1021 bufptr
+= sizeof (uint32_t);
1023 bfd_putl32 (size
, bufptr
);
1024 bufptr
+= sizeof (uint32_t);
1026 memcpy (bufptr
, data
+ off
, size
);
1036 if (off
% sizeof (uint32_t))
1037 off
+= sizeof (uint32_t) - (off
% sizeof (uint32_t));
1044 /* Append the C13 info to what's already there, if the module has
1045 multiple .debug$S sections. */
1047 *dataptr
= xrealloc (*dataptr
, *sizeptr
+ c13_size
);
1048 memcpy (*dataptr
+ *sizeptr
, buf
, c13_size
);
1057 *sizeptr
+= c13_size
;
1062 /* Remap the type number stored in data from the per-module numbering to
1063 that of the deduplicated output list. */
1065 remap_type (void *data
, struct type_entry
**map
,
1066 uint32_t type_num
, uint32_t num_types
)
1068 uint32_t type
= bfd_getl32 (data
);
1070 /* Ignore builtin types (those with IDs below 0x1000). */
1071 if (type
< TPI_FIRST_INDEX
)
1074 if (type
>= TPI_FIRST_INDEX
+ type_num
)
1076 einfo (_("%P: CodeView type %v references other type %v not yet "
1077 "declared\n"), TPI_FIRST_INDEX
+ type_num
, type
);
1081 if (type
>= TPI_FIRST_INDEX
+ num_types
)
1083 einfo (_("%P: CodeView type %v references out of range type %v\n"),
1084 TPI_FIRST_INDEX
+ type_num
, type
);
1088 type
= TPI_FIRST_INDEX
+ map
[type
- TPI_FIRST_INDEX
]->index
;
1089 bfd_putl32 (type
, data
);
1094 /* Determines whether the name of a struct, class, or union counts as
1095 "anonymous". Non-anonymous types have a hash based on just the name,
1096 rather than the whole structure. */
1098 is_name_anonymous (char *name
, size_t len
)
1100 static const char tag1
[] = "<unnamed-tag>";
1101 static const char tag2
[] = "__unnamed";
1102 static const char tag3
[] = "::<unnamed-tag>";
1103 static const char tag4
[] = "::__unnamed";
1105 if (len
== sizeof (tag1
) - 1 && !memcmp (name
, tag1
, sizeof (tag1
) - 1))
1108 if (len
== sizeof (tag2
) - 1 && !memcmp (name
, tag2
, sizeof (tag2
) - 1))
1111 if (len
>= sizeof (tag3
) - 1
1112 && !memcmp (name
+ len
- sizeof (tag3
) + 1, tag3
, sizeof (tag3
) - 1))
1115 if (len
>= sizeof (tag4
) - 1
1116 && !memcmp (name
+ len
- sizeof (tag4
) + 1, tag4
, sizeof (tag4
) - 1))
1122 /* Parse a type definition in the .debug$T section. We remap the numbers
1123 of any referenced types, and if the type is not a duplicate of one
1124 already seen add it to types (for TPI types) or ids (for IPI types). */
1126 handle_type (uint8_t *data
, struct type_entry
**map
, uint32_t type_num
,
1127 uint32_t num_types
, struct types
*types
,
1130 uint16_t size
, type
;
1133 bool other_hash
= false;
1138 size
= bfd_getl16 (data
) + sizeof (uint16_t);
1139 type
= bfd_getl16 (data
+ sizeof (uint16_t));
1145 struct lf_modifier
*mod
= (struct lf_modifier
*) data
;
1147 if (size
< offsetof (struct lf_modifier
, modifier
))
1149 einfo (_("%P: warning: truncated CodeView type record "
1154 if (!remap_type (&mod
->base_type
, map
, type_num
, num_types
))
1162 struct lf_pointer
*ptr
= (struct lf_pointer
*) data
;
1164 if (size
< offsetof (struct lf_pointer
, attributes
))
1166 einfo (_("%P: warning: truncated CodeView type record"
1171 if (!remap_type (&ptr
->base_type
, map
, type_num
, num_types
))
1179 struct lf_procedure
*proc
= (struct lf_procedure
*) data
;
1181 if (size
< sizeof (struct lf_procedure
))
1183 einfo (_("%P: warning: truncated CodeView type record"
1184 " LF_PROCEDURE\n"));
1188 if (!remap_type (&proc
->return_type
, map
, type_num
, num_types
))
1191 if (!remap_type (&proc
->arglist
, map
, type_num
, num_types
))
1199 struct lf_mfunction
*func
= (struct lf_mfunction
*) data
;
1201 if (size
< sizeof (struct lf_procedure
))
1203 einfo (_("%P: warning: truncated CodeView type record"
1204 " LF_MFUNCTION\n"));
1208 if (!remap_type (&func
->return_type
, map
, type_num
, num_types
))
1211 if (!remap_type (&func
->containing_class_type
, map
, type_num
,
1215 if (!remap_type (&func
->this_type
, map
, type_num
, num_types
))
1218 if (!remap_type (&func
->arglist
, map
, type_num
, num_types
))
1226 uint32_t num_entries
;
1227 struct lf_arglist
*al
= (struct lf_arglist
*) data
;
1229 if (size
< offsetof (struct lf_arglist
, args
))
1231 einfo (_("%P: warning: truncated CodeView type record"
1236 num_entries
= bfd_getl32 (&al
->num_entries
);
1238 if (size
< offsetof (struct lf_arglist
, args
)
1239 + (num_entries
* sizeof (uint32_t)))
1241 einfo (_("%P: warning: truncated CodeView type record"
1246 for (uint32_t i
= 0; i
< num_entries
; i
++)
1248 if (!remap_type (&al
->args
[i
], map
, type_num
, num_types
))
1257 uint16_t left
= size
- sizeof (uint16_t) - sizeof (uint16_t);
1258 uint8_t *ptr
= data
+ sizeof (uint16_t) + sizeof (uint16_t);
1264 if (left
< sizeof (uint16_t))
1266 einfo (_("%P: warning: truncated CodeView type record"
1267 " LF_FIELDLIST\n"));
1271 subtype
= bfd_getl16 (ptr
);
1277 struct lf_member
*mem
= (struct lf_member
*) ptr
;
1278 size_t name_len
, subtype_len
;
1280 if (left
< offsetof (struct lf_member
, name
))
1282 einfo (_("%P: warning: truncated CodeView type record"
1287 if (!remap_type (&mem
->type
, map
, type_num
, num_types
))
1292 left
- offsetof (struct lf_member
, name
));
1294 if (name_len
== left
- offsetof (struct lf_member
, name
))
1296 einfo (_("%P: warning: name for LF_MEMBER has no"
1297 " terminating zero\n"));
1303 subtype_len
= offsetof (struct lf_member
, name
) + name_len
;
1305 if (subtype_len
% 4 != 0)
1306 subtype_len
+= 4 - (subtype_len
% 4);
1308 if (left
< subtype_len
)
1310 einfo (_("%P: warning: truncated CodeView type record"
1311 " LF_FIELDLIST\n"));
1316 left
-= subtype_len
;
1323 struct lf_enumerate
*en
= (struct lf_enumerate
*) ptr
;
1324 size_t name_len
, subtype_len
;
1327 if (left
< offsetof (struct lf_enumerate
, name
))
1329 einfo (_("%P: warning: truncated CodeView type record"
1330 " LF_ENUMERATE\n"));
1334 subtype_len
= offsetof (struct lf_enumerate
, name
);
1336 val
= bfd_getl16 (&en
->value
);
1338 /* If val >= 0x8000, the actual value immediately follows. */
1341 unsigned int param_len
= extended_value_len (val
);
1345 einfo (_("%P: warning: unhandled type %v within"
1346 " LF_ENUMERATE\n"), val
);
1350 if (left
< subtype_len
+ param_len
)
1352 einfo (_("%P: warning: truncated CodeView type"
1353 " record LF_ENUMERATE\n"));
1357 subtype_len
+= param_len
;
1360 name_len
= strnlen ((char *) ptr
+ subtype_len
,
1361 left
- subtype_len
);
1363 if (name_len
== left
- offsetof (struct lf_enumerate
, name
))
1365 einfo (_("%P: warning: name for LF_ENUMERATE has no"
1366 " terminating zero\n"));
1372 subtype_len
+= name_len
;
1374 if (subtype_len
% 4 != 0)
1375 subtype_len
+= 4 - (subtype_len
% 4);
1377 if (left
< subtype_len
)
1379 einfo (_("%P: warning: truncated CodeView type record"
1380 " LF_ENUMERATE\n"));
1385 left
-= subtype_len
;
1392 struct lf_index
*ind
= (struct lf_index
*) ptr
;
1394 if (left
< sizeof (struct lf_index
))
1396 einfo (_("%P: warning: truncated CodeView type record"
1401 if (!remap_type (&ind
->index
, map
, type_num
, num_types
))
1404 ptr
+= sizeof (struct lf_index
);
1405 left
-= sizeof (struct lf_index
);
1412 struct lf_onemethod
*meth
= (struct lf_onemethod
*) ptr
;
1413 size_t name_len
, subtype_len
;
1415 if (left
< offsetof (struct lf_onemethod
, name
))
1417 einfo (_("%P: warning: truncated CodeView type record"
1418 " LF_ONEMETHOD\n"));
1422 if (!remap_type (&meth
->method_type
, map
, type_num
,
1427 strnlen (meth
->name
,
1428 left
- offsetof (struct lf_onemethod
, name
));
1430 if (name_len
== left
- offsetof (struct lf_onemethod
, name
))
1432 einfo (_("%P: warning: name for LF_ONEMETHOD has no"
1433 " terminating zero\n"));
1439 subtype_len
= offsetof (struct lf_onemethod
, name
)
1442 if (subtype_len
% 4 != 0)
1443 subtype_len
+= 4 - (subtype_len
% 4);
1445 if (left
< subtype_len
)
1447 einfo (_("%P: warning: truncated CodeView type record"
1448 " LF_FIELDLIST\n"));
1453 left
-= subtype_len
;
1460 struct lf_method
*meth
= (struct lf_method
*) ptr
;
1461 size_t name_len
, subtype_len
;
1463 if (left
< offsetof (struct lf_method
, name
))
1465 einfo (_("%P: warning: truncated CodeView type record"
1470 if (!remap_type (&meth
->method_list
, map
, type_num
,
1475 strnlen (meth
->name
,
1476 left
- offsetof (struct lf_method
, name
));
1478 if (name_len
== left
- offsetof (struct lf_method
, name
))
1480 einfo (_("%P: warning: name for LF_METHOD has no"
1481 " terminating zero\n"));
1487 subtype_len
= offsetof (struct lf_method
, name
) + name_len
;
1489 if (subtype_len
% 4 != 0)
1490 subtype_len
+= 4 - (subtype_len
% 4);
1492 if (left
< subtype_len
)
1494 einfo (_("%P: warning: truncated CodeView type record"
1495 " LF_FIELDLIST\n"));
1500 left
-= subtype_len
;
1507 struct lf_bclass
*bc
= (struct lf_bclass
*) ptr
;
1509 if (left
< sizeof (struct lf_bclass
))
1511 einfo (_("%P: warning: truncated CodeView type record"
1516 if (!remap_type (&bc
->base_class_type
, map
, type_num
,
1520 ptr
+= sizeof (struct lf_bclass
);
1521 left
-= sizeof (struct lf_bclass
);
1528 struct lf_vfunctab
*vft
= (struct lf_vfunctab
*) ptr
;
1530 if (left
< sizeof (struct lf_vfunctab
))
1532 einfo (_("%P: warning: truncated CodeView type record"
1537 if (!remap_type (&vft
->type
, map
, type_num
, num_types
))
1540 ptr
+= sizeof (struct lf_vfunctab
);
1541 left
-= sizeof (struct lf_vfunctab
);
1549 struct lf_vbclass
*vbc
= (struct lf_vbclass
*) ptr
;
1551 if (left
< sizeof (struct lf_vbclass
))
1553 einfo (_("%P: warning: truncated CodeView type record"
1554 " LF_VBCLASS/LF_IVBCLASS\n"));
1558 if (!remap_type (&vbc
->base_class_type
, map
, type_num
,
1562 if (!remap_type (&vbc
->virtual_base_pointer_type
, map
,
1563 type_num
, num_types
))
1566 ptr
+= sizeof (struct lf_vbclass
);
1567 left
-= sizeof (struct lf_vbclass
);
1574 struct lf_static_member
*st
=
1575 (struct lf_static_member
*) ptr
;
1576 size_t name_len
, subtype_len
;
1578 if (left
< offsetof (struct lf_static_member
, name
))
1580 einfo (_("%P: warning: truncated CodeView type record"
1585 if (!remap_type (&st
->type
, map
, type_num
, num_types
))
1590 left
- offsetof (struct lf_static_member
, name
));
1592 if (name_len
== left
1593 - offsetof (struct lf_static_member
, name
))
1595 einfo (_("%P: warning: name for LF_STMEMBER has no"
1596 " terminating zero\n"));
1602 subtype_len
= offsetof (struct lf_static_member
, name
)
1605 if (subtype_len
% 4 != 0)
1606 subtype_len
+= 4 - (subtype_len
% 4);
1608 if (left
< subtype_len
)
1610 einfo (_("%P: warning: truncated CodeView type record"
1611 " LF_FIELDLIST\n"));
1616 left
-= subtype_len
;
1623 struct lf_nest_type
*nest
= (struct lf_nest_type
*) ptr
;
1624 size_t name_len
, subtype_len
;
1626 if (left
< offsetof (struct lf_nest_type
, name
))
1628 einfo (_("%P: warning: truncated CodeView type record"
1633 if (!remap_type (&nest
->type
, map
, type_num
, num_types
))
1637 strnlen (nest
->name
,
1638 left
- offsetof (struct lf_nest_type
, name
));
1640 if (name_len
== left
- offsetof (struct lf_nest_type
, name
))
1642 einfo (_("%P: warning: name for LF_NESTTYPE has no"
1643 " terminating zero\n"));
1649 subtype_len
= offsetof (struct lf_nest_type
, name
)
1652 if (subtype_len
% 4 != 0)
1653 subtype_len
+= 4 - (subtype_len
% 4);
1655 if (left
< subtype_len
)
1657 einfo (_("%P: warning: truncated CodeView type record"
1658 " LF_FIELDLIST\n"));
1663 left
-= subtype_len
;
1669 einfo (_("%P: warning: unrecognized CodeView subtype %v\n"),
1680 struct lf_bitfield
*bf
= (struct lf_bitfield
*) data
;
1682 if (size
< offsetof (struct lf_bitfield
, length
))
1684 einfo (_("%P: warning: truncated CodeView type record"
1689 if (!remap_type (&bf
->base_type
, map
, type_num
, num_types
))
1697 struct lf_methodlist
*ml
= (struct lf_methodlist
*) data
;
1698 unsigned int num_entries
;
1700 if (size
< offsetof (struct lf_methodlist
, entries
))
1702 einfo (_("%P: warning: truncated CodeView type record"
1703 " LF_METHODLIST\n"));
1707 if ((size
- offsetof (struct lf_methodlist
, entries
))
1708 % sizeof (struct lf_methodlist_entry
))
1710 einfo (_("%P: warning: malformed CodeView type record"
1711 " LF_METHODLIST\n"));
1715 num_entries
= (size
- offsetof (struct lf_methodlist
, entries
))
1716 / sizeof (struct lf_methodlist_entry
);
1718 for (unsigned int i
= 0; i
< num_entries
; i
++)
1720 if (!remap_type (&ml
->entries
[i
].method_type
, map
,
1721 type_num
, num_types
))
1730 struct lf_array
*arr
= (struct lf_array
*) data
;
1732 if (size
< offsetof (struct lf_array
, length_in_bytes
))
1734 einfo (_("%P: warning: truncated CodeView type record"
1739 if (!remap_type (&arr
->element_type
, map
, type_num
, num_types
))
1742 if (!remap_type (&arr
->index_type
, map
, type_num
, num_types
))
1751 struct lf_class
*cl
= (struct lf_class
*) data
;
1755 if (size
< offsetof (struct lf_class
, name
))
1757 einfo (_("%P: warning: truncated CodeView type record"
1758 " LF_CLASS/LF_STRUCTURE\n"));
1762 if (!remap_type (&cl
->field_list
, map
, type_num
, num_types
))
1765 if (!remap_type (&cl
->derived_from
, map
, type_num
, num_types
))
1768 if (!remap_type (&cl
->vshape
, map
, type_num
, num_types
))
1771 name_len
= strnlen (cl
->name
, size
- offsetof (struct lf_class
, name
));
1773 if (name_len
== size
- offsetof (struct lf_class
, name
))
1775 einfo (_("%P: warning: name for LF_CLASS/LF_STRUCTURE has no"
1776 " terminating zero\n"));
1780 prop
= bfd_getl16 (&cl
->properties
);
1782 if (prop
& CV_PROP_HAS_UNIQUE_NAME
)
1784 /* Structure has another name following first one. */
1786 size_t len
= offsetof (struct lf_class
, name
) + name_len
+ 1;
1787 size_t unique_name_len
;
1789 unique_name_len
= strnlen (cl
->name
+ name_len
+ 1, size
- len
);
1791 if (unique_name_len
== size
- len
)
1793 einfo (_("%P: warning: unique name for LF_CLASS/LF_STRUCTURE"
1794 " has no terminating zero\n"));
1799 if (!(prop
& (CV_PROP_FORWARD_REF
| CV_PROP_SCOPED
))
1800 && !is_name_anonymous (cl
->name
, name_len
))
1803 cv_hash
= crc32 ((uint8_t *) cl
->name
, name_len
);
1811 struct lf_union
*un
= (struct lf_union
*) data
;
1815 if (size
< offsetof (struct lf_union
, name
))
1817 einfo (_("%P: warning: truncated CodeView type record"
1822 if (!remap_type (&un
->field_list
, map
, type_num
, num_types
))
1825 name_len
= strnlen (un
->name
, size
- offsetof (struct lf_union
, name
));
1827 if (name_len
== size
- offsetof (struct lf_union
, name
))
1829 einfo (_("%P: warning: name for LF_UNION has no"
1830 " terminating zero\n"));
1834 prop
= bfd_getl16 (&un
->properties
);
1836 if (prop
& CV_PROP_HAS_UNIQUE_NAME
)
1838 /* Structure has another name following first one. */
1840 size_t len
= offsetof (struct lf_union
, name
) + name_len
+ 1;
1841 size_t unique_name_len
;
1843 unique_name_len
= strnlen (un
->name
+ name_len
+ 1, size
- len
);
1845 if (unique_name_len
== size
- len
)
1847 einfo (_("%P: warning: unique name for LF_UNION has"
1848 " no terminating zero\n"));
1853 if (!(prop
& (CV_PROP_FORWARD_REF
| CV_PROP_SCOPED
))
1854 && !is_name_anonymous (un
->name
, name_len
))
1857 cv_hash
= crc32 ((uint8_t *) un
->name
, name_len
);
1865 struct lf_enum
*en
= (struct lf_enum
*) data
;
1869 if (size
< offsetof (struct lf_enum
, name
))
1871 einfo (_("%P: warning: truncated CodeView type record"
1876 if (!remap_type (&en
->underlying_type
, map
, type_num
, num_types
))
1879 if (!remap_type (&en
->field_list
, map
, type_num
, num_types
))
1882 name_len
= strnlen (en
->name
, size
- offsetof (struct lf_enum
, name
));
1884 if (name_len
== size
- offsetof (struct lf_enum
, name
))
1886 einfo (_("%P: warning: name for LF_ENUM has no"
1887 " terminating zero\n"));
1891 prop
= bfd_getl16 (&en
->properties
);
1893 if (prop
& CV_PROP_HAS_UNIQUE_NAME
)
1895 /* Structure has another name following first one. */
1897 size_t len
= offsetof (struct lf_enum
, name
) + name_len
+ 1;
1898 size_t unique_name_len
;
1900 unique_name_len
= strnlen (en
->name
+ name_len
+ 1, size
- len
);
1902 if (unique_name_len
== size
- len
)
1904 einfo (_("%P: warning: unique name for LF_ENUM has"
1905 " no terminating zero\n"));
1914 /* Does not reference any types, nothing to be done. */
1919 struct lf_string_id
*str
= (struct lf_string_id
*) data
;
1922 if (size
< offsetof (struct lf_string_id
, string
))
1924 einfo (_("%P: warning: truncated CodeView type record"
1925 " LF_STRING_ID\n"));
1929 if (!remap_type (&str
->substring
, map
, type_num
, num_types
))
1932 string_len
= strnlen (str
->string
,
1933 size
- offsetof (struct lf_string_id
, string
));
1935 if (string_len
== size
- offsetof (struct lf_string_id
, string
))
1937 einfo (_("%P: warning: string for LF_STRING_ID has no"
1938 " terminating zero\n"));
1947 case LF_SUBSTR_LIST
:
1949 uint32_t num_entries
;
1950 struct lf_arglist
*ssl
= (struct lf_arglist
*) data
;
1952 if (size
< offsetof (struct lf_arglist
, args
))
1954 einfo (_("%P: warning: truncated CodeView type record"
1955 " LF_SUBSTR_LIST\n"));
1959 num_entries
= bfd_getl32 (&ssl
->num_entries
);
1961 if (size
< offsetof (struct lf_arglist
, args
)
1962 + (num_entries
* sizeof (uint32_t)))
1964 einfo (_("%P: warning: truncated CodeView type record"
1965 " LF_SUBSTR_LIST\n"));
1969 for (uint32_t i
= 0; i
< num_entries
; i
++)
1971 if (!remap_type (&ssl
->args
[i
], map
, type_num
, num_types
))
1982 uint16_t num_entries
;
1983 struct lf_build_info
*bi
= (struct lf_build_info
*) data
;
1985 if (size
< offsetof (struct lf_build_info
, strings
))
1987 einfo (_("%P: warning: truncated CodeView type record"
1988 " LF_BUILDINFO\n"));
1992 num_entries
= bfd_getl16 (&bi
->count
);
1994 if (size
< offsetof (struct lf_build_info
, strings
)
1995 + (num_entries
* sizeof (uint32_t)))
1997 einfo (_("%P: warning: truncated CodeView type record"
1998 " LF_BUILDINFO\n"));
2002 for (uint32_t i
= 0; i
< num_entries
; i
++)
2004 if (!remap_type (&bi
->strings
[i
], map
, type_num
, num_types
))
2015 struct lf_func_id
*func
= (struct lf_func_id
*) data
;
2018 if (size
< offsetof (struct lf_func_id
, name
))
2020 einfo (_("%P: warning: truncated CodeView type record"
2025 if (!remap_type (&func
->parent_scope
, map
, type_num
, num_types
))
2028 if (!remap_type (&func
->function_type
, map
, type_num
, num_types
))
2031 name_len
= strnlen (func
->name
,
2032 size
- offsetof (struct lf_func_id
, name
));
2034 if (name_len
== size
- offsetof (struct lf_func_id
, name
))
2036 einfo (_("%P: warning: string for LF_FUNC_ID has no"
2037 " terminating zero\n"));
2048 struct lf_mfunc_id
*mfunc
= (struct lf_mfunc_id
*) data
;
2051 if (size
< offsetof (struct lf_mfunc_id
, name
))
2053 einfo (_("%P: warning: truncated CodeView type record"
2058 if (!remap_type (&mfunc
->parent_type
, map
, type_num
, num_types
))
2061 if (!remap_type (&mfunc
->function_type
, map
, type_num
, num_types
))
2064 name_len
= strnlen (mfunc
->name
,
2065 size
- offsetof (struct lf_mfunc_id
, name
));
2067 if (name_len
== size
- offsetof (struct lf_mfunc_id
, name
))
2069 einfo (_("%P: warning: string for LF_MFUNC_ID has no"
2070 " terminating zero\n"));
2080 einfo (_("%P: warning: unrecognized CodeView type %v\n"), type
);
2084 hash
= iterative_hash (data
, size
, 0);
2086 t
= ipi
? ids
: types
;
2088 slot
= htab_find_slot_with_hash (t
->hashmap
, data
, hash
, INSERT
);
2092 if (!*slot
) /* new entry */
2094 struct type_entry
*e
;
2096 *slot
= xmalloc (offsetof (struct type_entry
, data
) + size
);
2098 e
= (struct type_entry
*) *slot
;
2101 e
->index
= t
->num_types
;
2104 e
->cv_hash
= cv_hash
;
2106 e
->cv_hash
= crc32 (data
, size
);
2108 memcpy (e
->data
, data
, size
);
2121 else /* duplicate */
2123 map
[type_num
] = (struct type_entry
*) *slot
;
2129 /* Parse the .debug$T section of a module, and pass any type definitions
2130 found to handle_type. */
2132 handle_debugt_section (asection
*s
, bfd
*mod
, struct types
*types
,
2135 bfd_byte
*data
= NULL
;
2137 unsigned int num_types
= 0;
2138 struct type_entry
**map
;
2141 if (!bfd_get_full_section_contents (mod
, s
, &data
))
2147 if (bfd_getl32 (data
) != CV_SIGNATURE_C13
)
2153 off
= sizeof (uint32_t);
2155 while (off
+ sizeof (uint16_t) <= s
->size
)
2159 size
= bfd_getl16 (data
+ off
);
2160 off
+= sizeof (uint16_t);
2162 if (size
+ off
> s
->size
|| size
<= sizeof (uint16_t))
2165 bfd_set_error (bfd_error_bad_value
);
2179 map
= xcalloc (num_types
, sizeof (struct type_entry
*));
2181 off
= sizeof (uint32_t);
2184 while (off
+ sizeof (uint16_t) <= s
->size
)
2188 size
= bfd_getl16 (data
+ off
);
2190 if (!handle_type (data
+ off
, map
, type_num
, num_types
, types
, ids
))
2194 bfd_set_error (bfd_error_bad_value
);
2198 off
+= sizeof (uint16_t) + size
;
2208 /* Populate the module stream, which consists of the transformed .debug$S
2209 data for each object file. */
2211 populate_module_stream (bfd
*stream
, bfd
*mod
, uint32_t *sym_byte_size
,
2212 struct string_table
*strings
,
2213 uint32_t *c13_info_size
,
2214 struct mod_source_files
*mod_source
,
2215 bfd
*abfd
, struct types
*types
,
2218 uint8_t int_buf
[sizeof (uint32_t)];
2219 uint8_t *c13_info
= NULL
;
2221 *sym_byte_size
= sizeof (uint32_t);
2224 /* Process .debug$S section(s). */
2226 for (asection
*s
= mod
->sections
; s
; s
= s
->next
)
2228 if (!strcmp (s
->name
, ".debug$S") && s
->size
>= sizeof (uint32_t))
2230 if (!handle_debugs_section (s
, mod
, strings
, &c13_info
,
2231 c13_info_size
, mod_source
, abfd
))
2234 free (mod_source
->files
);
2238 else if (!strcmp (s
->name
, ".debug$T") && s
->size
>= sizeof (uint32_t))
2240 if (!handle_debugt_section (s
, mod
, types
, ids
))
2243 free (mod_source
->files
);
2249 /* Write the signature. */
2251 bfd_putl32 (CV_SIGNATURE_C13
, int_buf
);
2253 if (bfd_bwrite (int_buf
, sizeof (uint32_t), stream
) != sizeof (uint32_t))
2261 if (bfd_bwrite (c13_info
, *c13_info_size
, stream
) != *c13_info_size
)
2270 /* Write the global refs size. */
2272 bfd_putl32 (0, int_buf
);
2274 if (bfd_bwrite (int_buf
, sizeof (uint32_t), stream
) != sizeof (uint32_t))
2280 /* Create the module info substream within the DBI. */
2282 create_module_info_substream (bfd
*abfd
, bfd
*pdb
, void **data
,
2283 uint32_t *size
, struct string_table
*strings
,
2284 struct source_files_info
*source
,
2285 struct types
*types
, struct types
*ids
)
2288 unsigned int mod_num
;
2290 static const char linker_fn
[] = "* Linker *";
2294 for (bfd
*in
= coff_data (abfd
)->link_info
->input_bfds
; in
;
2297 size_t len
= sizeof (struct module_info
);
2299 if (!strcmp (bfd_get_filename (in
), "dll stuff"))
2301 len
+= sizeof (linker_fn
); /* Object name. */
2302 len
++; /* Empty module name. */
2304 else if (in
->my_archive
)
2306 char *name
= lrealpath (bfd_get_filename (in
));
2308 len
+= strlen (name
) + 1; /* Object name. */
2312 name
= lrealpath (bfd_get_filename (in
->my_archive
));
2314 len
+= strlen (name
) + 1; /* Archive name. */
2320 char *name
= lrealpath (bfd_get_filename (in
));
2321 size_t name_len
= strlen (name
) + 1;
2323 len
+= name_len
; /* Object name. */
2324 len
+= name_len
; /* And again as the archive name. */
2330 len
+= 4 - (len
% 4);
2334 source
->mod_count
++;
2337 *data
= xmalloc (*size
);
2341 source
->mods
= xmalloc (source
->mod_count
2342 * sizeof (struct mod_source_files
));
2343 memset (source
->mods
, 0,
2344 source
->mod_count
* sizeof (struct mod_source_files
));
2348 for (bfd
*in
= coff_data (abfd
)->link_info
->input_bfds
; in
;
2351 struct module_info
*mod
= (struct module_info
*) ptr
;
2352 uint16_t stream_num
;
2354 uint32_t sym_byte_size
, c13_info_size
;
2355 uint8_t *start
= ptr
;
2357 stream
= add_stream (pdb
, NULL
, &stream_num
);
2361 for (unsigned int i
= 0; i
< source
->mod_count
; i
++)
2363 free (source
->mods
[i
].files
);
2366 free (source
->mods
);
2371 if (!populate_module_stream (stream
, in
, &sym_byte_size
,
2372 strings
, &c13_info_size
,
2373 &source
->mods
[mod_num
], abfd
,
2376 for (unsigned int i
= 0; i
< source
->mod_count
; i
++)
2378 free (source
->mods
[i
].files
);
2381 free (source
->mods
);
2386 bfd_putl32 (0, &mod
->unused1
);
2388 /* These are dummy values - MSVC copies the first section contribution
2389 entry here, but doesn't seem to use it for anything. */
2390 bfd_putl16 (0xffff, &mod
->sc
.section
);
2391 bfd_putl16 (0, &mod
->sc
.padding1
);
2392 bfd_putl32 (0, &mod
->sc
.offset
);
2393 bfd_putl32 (0xffffffff, &mod
->sc
.size
);
2394 bfd_putl32 (0, &mod
->sc
.characteristics
);
2395 bfd_putl16 (0xffff, &mod
->sc
.module_index
);
2396 bfd_putl16 (0, &mod
->sc
.padding2
);
2397 bfd_putl32 (0, &mod
->sc
.data_crc
);
2398 bfd_putl32 (0, &mod
->sc
.reloc_crc
);
2400 bfd_putl16 (0, &mod
->flags
);
2401 bfd_putl16 (stream_num
, &mod
->module_sym_stream
);
2402 bfd_putl32 (sym_byte_size
, &mod
->sym_byte_size
);
2403 bfd_putl32 (0, &mod
->c11_byte_size
);
2404 bfd_putl32 (c13_info_size
, &mod
->c13_byte_size
);
2405 bfd_putl16 (0, &mod
->source_file_count
);
2406 bfd_putl16 (0, &mod
->padding
);
2407 bfd_putl32 (0, &mod
->unused2
);
2408 bfd_putl32 (0, &mod
->source_file_name_index
);
2409 bfd_putl32 (0, &mod
->pdb_file_path_name_index
);
2411 ptr
+= sizeof (struct module_info
);
2413 if (!strcmp (bfd_get_filename (in
), "dll stuff"))
2416 memcpy (ptr
, linker_fn
, sizeof (linker_fn
));
2417 ptr
+= sizeof (linker_fn
);
2419 /* Empty module name. */
2423 else if (in
->my_archive
)
2425 char *name
= lrealpath (bfd_get_filename (in
));
2426 size_t name_len
= strlen (name
) + 1;
2429 memcpy (ptr
, name
, name_len
);
2434 name
= lrealpath (bfd_get_filename (in
->my_archive
));
2435 name_len
= strlen (name
) + 1;
2438 memcpy (ptr
, name
, name_len
);
2445 char *name
= lrealpath (bfd_get_filename (in
));
2446 size_t name_len
= strlen (name
) + 1;
2449 memcpy (ptr
, name
, name_len
);
2452 /* Object name again as archive name. */
2453 memcpy (ptr
, name
, name_len
);
2459 /* Pad to next four-byte boundary. */
2461 if ((ptr
- start
) % 4)
2463 memset (ptr
, 0, 4 - ((ptr
- start
) % 4));
2464 ptr
+= 4 - ((ptr
- start
) % 4);
2473 /* Return the index of a given output section. */
2475 find_section_number (bfd
*abfd
, asection
*sect
)
2479 for (asection
*s
= abfd
->sections
; s
; s
= s
->next
)
2484 /* Empty sections aren't output. */
2492 /* Create the substream which maps addresses in the image file to locations
2493 in the original object files. */
2495 create_section_contrib_substream (bfd
*abfd
, void **data
, uint32_t *size
)
2497 unsigned int num_sc
= 0;
2498 struct section_contribution
*sc
;
2503 for (bfd
*in
= coff_data (abfd
)->link_info
->input_bfds
; in
;
2506 for (asection
*s
= in
->sections
; s
; s
= s
->next
)
2508 if (s
->size
== 0 || discarded_section (s
))
2515 *size
= sizeof (uint32_t) + (num_sc
* sizeof (struct section_contribution
));
2516 *data
= xmalloc (*size
);
2518 bfd_putl32 (SECTION_CONTRIB_VERSION_60
, *data
);
2520 /* Read characteristics of outputted sections. */
2522 sect_flags
= xmalloc (sizeof (uint32_t) * abfd
->section_count
);
2524 offset
= bfd_coff_filhsz (abfd
) + bfd_coff_aoutsz (abfd
);
2525 offset
+= offsetof (struct external_scnhdr
, s_flags
);
2527 for (unsigned int i
= 0; i
< abfd
->section_count
; i
++)
2529 bfd_seek (abfd
, offset
, SEEK_SET
);
2531 if (bfd_bread (sect_flags
+ (i
* sizeof (uint32_t)), sizeof (uint32_t),
2532 abfd
) != sizeof (uint32_t))
2539 offset
+= sizeof (struct external_scnhdr
);
2543 (struct section_contribution
*) ((uint8_t *) *data
+ sizeof (uint32_t));
2546 for (bfd
*in
= coff_data (abfd
)->link_info
->input_bfds
; in
;
2549 for (asection
*s
= in
->sections
; s
; s
= s
->next
)
2553 if (s
->size
== 0 || discarded_section (s
))
2556 sect_num
= find_section_number (abfd
, s
->output_section
);
2558 memcpy (&sc
->characteristics
,
2559 sect_flags
+ ((sect_num
- 1) * sizeof (uint32_t)),
2562 bfd_putl16 (sect_num
, &sc
->section
);
2563 bfd_putl16 (0, &sc
->padding1
);
2564 bfd_putl32 (s
->output_offset
, &sc
->offset
);
2565 bfd_putl32 (s
->size
, &sc
->size
);
2566 bfd_putl16 (mod_index
, &sc
->module_index
);
2567 bfd_putl16 (0, &sc
->padding2
);
2568 bfd_putl32 (0, &sc
->data_crc
);
2569 bfd_putl32 (0, &sc
->reloc_crc
);
2582 /* The source info substream lives within the DBI stream, and lists the
2583 source files for each object file (i.e. it's derived from the
2584 DEBUG_S_FILECHKSMS parts of the .debug$S sections). This is a bit
2585 superfluous, as the filenames are also available in the C13 parts of
2586 the module streams, but MSVC relies on it to work properly. */
2588 create_source_info_substream (void **data
, uint32_t *size
,
2589 struct source_files_info
*source
)
2591 uint16_t dedupe_source_files_count
= 0;
2592 uint16_t source_files_count
= 0;
2593 uint32_t strings_len
= 0;
2596 /* Loop through the source files, marking unique filenames. The pointers
2597 here are for entries in the main string table, and so have already
2598 been deduplicated. */
2600 for (uint16_t i
= 0; i
< source
->mod_count
; i
++)
2602 for (uint16_t j
= 0; j
< source
->mods
[i
].files_count
; j
++)
2604 if (source
->mods
[i
].files
[j
])
2606 if (source
->mods
[i
].files
[j
]->source_file_offset
== 0xffffffff)
2608 source
->mods
[i
].files
[j
]->source_file_offset
= strings_len
;
2609 strings_len
+= source
->mods
[i
].files
[j
]->len
+ 1;
2610 dedupe_source_files_count
++;
2613 source_files_count
++;
2618 *size
= sizeof (uint16_t) + sizeof (uint16_t);
2619 *size
+= (sizeof (uint16_t) + sizeof (uint16_t)) * source
->mod_count
;
2620 *size
+= sizeof (uint32_t) * source_files_count
;
2621 *size
+= strings_len
;
2623 *data
= xmalloc (*size
);
2625 ptr
= (uint8_t *) *data
;
2627 /* Write header (module count and source file count). */
2629 bfd_putl16 (source
->mod_count
, ptr
);
2630 ptr
+= sizeof (uint16_t);
2632 bfd_putl16 (dedupe_source_files_count
, ptr
);
2633 ptr
+= sizeof (uint16_t);
2635 /* Write "ModIndices". As the LLVM documentation puts it, "this array is
2636 present, but does not appear to be useful". */
2638 for (uint16_t i
= 0; i
< source
->mod_count
; i
++)
2640 bfd_putl16 (i
, ptr
);
2641 ptr
+= sizeof (uint16_t);
2644 /* Write source file count for each module. */
2646 for (uint16_t i
= 0; i
< source
->mod_count
; i
++)
2648 bfd_putl16 (source
->mods
[i
].files_count
, ptr
);
2649 ptr
+= sizeof (uint16_t);
2652 /* For each module, write the offsets within the string table
2653 for each source file. */
2655 for (uint16_t i
= 0; i
< source
->mod_count
; i
++)
2657 for (uint16_t j
= 0; j
< source
->mods
[i
].files_count
; j
++)
2659 if (source
->mods
[i
].files
[j
])
2661 bfd_putl32 (source
->mods
[i
].files
[j
]->source_file_offset
, ptr
);
2662 ptr
+= sizeof (uint32_t);
2667 /* Write the string table. We set source_file_offset to a dummy value for
2668 each entry we write, so we don't write duplicate filenames. */
2670 for (uint16_t i
= 0; i
< source
->mod_count
; i
++)
2672 for (uint16_t j
= 0; j
< source
->mods
[i
].files_count
; j
++)
2674 if (source
->mods
[i
].files
[j
]
2675 && source
->mods
[i
].files
[j
]->source_file_offset
!= 0xffffffff)
2677 memcpy (ptr
, source
->mods
[i
].files
[j
]->s
,
2678 source
->mods
[i
].files
[j
]->len
);
2679 ptr
+= source
->mods
[i
].files
[j
]->len
;
2684 source
->mods
[i
].files
[j
]->source_file_offset
= 0xffffffff;
2690 /* Stream 4 is the debug information (DBI) stream. */
2692 populate_dbi_stream (bfd
*stream
, bfd
*abfd
, bfd
*pdb
,
2693 uint16_t section_header_stream_num
,
2694 uint16_t sym_rec_stream_num
,
2695 uint16_t publics_stream_num
,
2696 struct string_table
*strings
,
2697 struct types
*types
,
2700 struct pdb_dbi_stream_header h
;
2701 struct optional_dbg_header opt
;
2702 void *mod_info
, *sc
, *source_info
;
2703 uint32_t mod_info_size
, sc_size
, source_info_size
;
2704 struct source_files_info source
;
2706 source
.mod_count
= 0;
2709 if (!create_module_info_substream (abfd
, pdb
, &mod_info
, &mod_info_size
,
2710 strings
, &source
, types
, ids
))
2713 if (!create_section_contrib_substream (abfd
, &sc
, &sc_size
))
2715 for (unsigned int i
= 0; i
< source
.mod_count
; i
++)
2717 free (source
.mods
[i
].files
);
2725 create_source_info_substream (&source_info
, &source_info_size
, &source
);
2727 for (unsigned int i
= 0; i
< source
.mod_count
; i
++)
2729 free (source
.mods
[i
].files
);
2733 bfd_putl32 (0xffffffff, &h
.version_signature
);
2734 bfd_putl32 (DBI_STREAM_VERSION_70
, &h
.version_header
);
2735 bfd_putl32 (1, &h
.age
);
2736 bfd_putl16 (0xffff, &h
.global_stream_index
);
2737 bfd_putl16 (0x8e1d, &h
.build_number
); // MSVC 14.29
2738 bfd_putl16 (publics_stream_num
, &h
.public_stream_index
);
2739 bfd_putl16 (0, &h
.pdb_dll_version
);
2740 bfd_putl16 (sym_rec_stream_num
, &h
.sym_record_stream
);
2741 bfd_putl16 (0, &h
.pdb_dll_rbld
);
2742 bfd_putl32 (mod_info_size
, &h
.mod_info_size
);
2743 bfd_putl32 (sc_size
, &h
.section_contribution_size
);
2744 bfd_putl32 (0, &h
.section_map_size
);
2745 bfd_putl32 (source_info_size
, &h
.source_info_size
);
2746 bfd_putl32 (0, &h
.type_server_map_size
);
2747 bfd_putl32 (0, &h
.mfc_type_server_index
);
2748 bfd_putl32 (sizeof (opt
), &h
.optional_dbg_header_size
);
2749 bfd_putl32 (0, &h
.ec_substream_size
);
2750 bfd_putl16 (0, &h
.flags
);
2751 bfd_putl16 (get_arch_number (abfd
), &h
.machine
);
2752 bfd_putl32 (0, &h
.padding
);
2754 if (bfd_bwrite (&h
, sizeof (h
), stream
) != sizeof (h
))
2762 if (bfd_bwrite (mod_info
, mod_info_size
, stream
) != mod_info_size
)
2772 if (bfd_bwrite (sc
, sc_size
, stream
) != sc_size
)
2781 if (bfd_bwrite (source_info
, source_info_size
, stream
) != source_info_size
)
2789 bfd_putl16 (0xffff, &opt
.fpo_stream
);
2790 bfd_putl16 (0xffff, &opt
.exception_stream
);
2791 bfd_putl16 (0xffff, &opt
.fixup_stream
);
2792 bfd_putl16 (0xffff, &opt
.omap_to_src_stream
);
2793 bfd_putl16 (0xffff, &opt
.omap_from_src_stream
);
2794 bfd_putl16 (section_header_stream_num
, &opt
.section_header_stream
);
2795 bfd_putl16 (0xffff, &opt
.token_map_stream
);
2796 bfd_putl16 (0xffff, &opt
.xdata_stream
);
2797 bfd_putl16 (0xffff, &opt
.pdata_stream
);
2798 bfd_putl16 (0xffff, &opt
.new_fpo_stream
);
2799 bfd_putl16 (0xffff, &opt
.orig_section_header_stream
);
2801 if (bfd_bwrite (&opt
, sizeof (opt
), stream
) != sizeof (opt
))
2807 /* Used as parameter to qsort, to sort publics by hash. */
2809 public_compare_hash (const void *s1
, const void *s2
)
2811 const struct public *p1
= *(const struct public **) s1
;
2812 const struct public *p2
= *(const struct public **) s2
;
2814 if (p1
->hash
< p2
->hash
)
2816 if (p1
->hash
> p2
->hash
)
2822 /* Used as parameter to qsort, to sort publics by address. */
2824 public_compare_addr (const void *s1
, const void *s2
)
2826 const struct public *p1
= *(const struct public **) s1
;
2827 const struct public *p2
= *(const struct public **) s2
;
2829 if (p1
->section
< p2
->section
)
2831 if (p1
->section
> p2
->section
)
2834 if (p1
->address
< p2
->address
)
2836 if (p1
->address
> p2
->address
)
2842 /* The publics stream is a hash map of S_PUB32 records, which are stored
2843 in the symbol record stream. Each S_PUB32 entry represents a symbol
2844 from the point of view of the linker: a section index, an offset within
2845 the section, and a mangled name. Compare with S_GDATA32 and S_GPROC32,
2846 which are the same thing but generated by the compiler. */
2848 populate_publics_stream (bfd
*stream
, bfd
*abfd
, bfd
*sym_rec_stream
)
2850 struct publics_header header
;
2851 struct globals_hash_header hash_header
;
2852 const unsigned int num_buckets
= 4096;
2853 unsigned int num_entries
= 0, filled_buckets
= 0;
2854 unsigned int buckets_size
, sym_hash_size
;
2855 char int_buf
[sizeof (uint32_t)];
2856 struct public *publics_head
= NULL
, *publics_tail
= NULL
;
2857 struct public **buckets
;
2858 struct public **sorted
= NULL
;
2861 buckets
= xmalloc (sizeof (struct public *) * num_buckets
);
2862 memset (buckets
, 0, sizeof (struct public *) * num_buckets
);
2864 /* Loop through the global symbols in our input files, and write S_PUB32
2865 records in the symbol record stream for those that make it into the
2867 for (bfd
*in
= coff_data (abfd
)->link_info
->input_bfds
; in
;
2870 if (!in
->outsymbols
)
2873 for (unsigned int i
= 0; i
< in
->symcount
; i
++)
2875 struct bfd_symbol
*sym
= in
->outsymbols
[i
];
2877 if (sym
->flags
& BSF_GLOBAL
)
2880 uint16_t record_length
;
2881 const char *name
= sym
->name
;
2882 size_t name_len
= strlen (name
);
2883 struct public *p
= xmalloc (sizeof (struct public));
2884 unsigned int padding
= 0;
2889 find_section_number (abfd
, sym
->section
->output_section
);
2895 p
->offset
= bfd_tell (sym_rec_stream
);
2896 p
->hash
= calc_hash (name
, name_len
) % num_buckets
;
2897 p
->section
= section
;
2898 p
->address
= sym
->section
->output_offset
+ sym
->value
;
2900 record_length
= sizeof (struct pubsym
) + name_len
+ 1;
2902 if (record_length
% 4)
2903 padding
= 4 - (record_length
% 4);
2905 /* Assume that all global symbols in executable sections
2907 if (sym
->section
->flags
& SEC_CODE
)
2908 flags
= PUBSYM_FUNCTION
;
2910 bfd_putl16 (record_length
+ padding
- sizeof (uint16_t),
2912 bfd_putl16 (S_PUB32
, &ps
.record_type
);
2913 bfd_putl32 (flags
, &ps
.flags
);
2914 bfd_putl32 (p
->address
, &ps
.offset
);
2915 bfd_putl16 (p
->section
, &ps
.section
);
2917 if (bfd_bwrite (&ps
, sizeof (struct pubsym
), sym_rec_stream
) !=
2918 sizeof (struct pubsym
))
2921 if (bfd_bwrite (name
, name_len
+ 1, sym_rec_stream
) !=
2925 for (unsigned int j
= 0; j
< padding
; j
++)
2929 if (bfd_bwrite (&b
, sizeof (uint8_t), sym_rec_stream
) !=
2937 publics_tail
->next
= p
;
2946 if (num_entries
> 0)
2948 /* Create an array of pointers, sorted by hash value. */
2950 sorted
= xmalloc (sizeof (struct public *) * num_entries
);
2952 struct public *p
= publics_head
;
2953 for (unsigned int i
= 0; i
< num_entries
; i
++)
2959 qsort (sorted
, num_entries
, sizeof (struct public *),
2960 public_compare_hash
);
2962 /* Populate the buckets. */
2964 for (unsigned int i
= 0; i
< num_entries
; i
++)
2966 if (!buckets
[sorted
[i
]->hash
])
2968 buckets
[sorted
[i
]->hash
] = sorted
[i
];
2972 sorted
[i
]->index
= i
;
2976 buckets_size
= num_buckets
/ 8;
2977 buckets_size
+= sizeof (uint32_t);
2978 buckets_size
+= filled_buckets
* sizeof (uint32_t);
2980 sym_hash_size
= sizeof (hash_header
);
2981 sym_hash_size
+= num_entries
* sizeof (struct hash_record
);
2982 sym_hash_size
+= buckets_size
;
2984 /* Output the publics header. */
2986 bfd_putl32 (sym_hash_size
, &header
.sym_hash_size
);
2987 bfd_putl32 (num_entries
* sizeof (uint32_t), &header
.addr_map_size
);
2988 bfd_putl32 (0, &header
.num_thunks
);
2989 bfd_putl32 (0, &header
.thunks_size
);
2990 bfd_putl32 (0, &header
.thunk_table
);
2991 bfd_putl32 (0, &header
.thunk_table_offset
);
2992 bfd_putl32 (0, &header
.num_sects
);
2994 if (bfd_bwrite (&header
, sizeof (header
), stream
) != sizeof (header
))
2997 /* Output the global hash header. */
2999 bfd_putl32 (GLOBALS_HASH_SIGNATURE
, &hash_header
.signature
);
3000 bfd_putl32 (GLOBALS_HASH_VERSION_70
, &hash_header
.version
);
3001 bfd_putl32 (num_entries
* sizeof (struct hash_record
),
3002 &hash_header
.entries_size
);
3003 bfd_putl32 (buckets_size
, &hash_header
.buckets_size
);
3005 if (bfd_bwrite (&hash_header
, sizeof (hash_header
), stream
) !=
3006 sizeof (hash_header
))
3009 /* Write the entries in hash order. */
3011 for (unsigned int i
= 0; i
< num_entries
; i
++)
3013 struct hash_record hr
;
3015 bfd_putl32 (sorted
[i
]->offset
+ 1, &hr
.offset
);
3016 bfd_putl32 (1, &hr
.reference
);
3018 if (bfd_bwrite (&hr
, sizeof (hr
), stream
) != sizeof (hr
))
3022 /* Write the bitmap for filled and unfilled buckets. */
3024 for (unsigned int i
= 0; i
< num_buckets
; i
+= 8)
3028 for (unsigned int j
= 0; j
< 8; j
++)
3034 if (bfd_bwrite (&v
, sizeof (v
), stream
) != sizeof (v
))
3038 /* Add a 4-byte gap. */
3040 bfd_putl32 (0, int_buf
);
3042 if (bfd_bwrite (int_buf
, sizeof (uint32_t), stream
) != sizeof (uint32_t))
3045 /* Write the bucket offsets. */
3047 for (unsigned int i
= 0; i
< num_buckets
; i
++)
3051 /* 0xc is size of internal hash_record structure in
3052 Microsoft's parser. */
3053 bfd_putl32 (buckets
[i
]->index
* 0xc, int_buf
);
3055 if (bfd_bwrite (int_buf
, sizeof (uint32_t), stream
) !=
3061 /* Write the address map: offsets into the symbol record stream of
3062 S_PUB32 records, ordered by address. */
3064 if (num_entries
> 0)
3066 qsort (sorted
, num_entries
, sizeof (struct public *),
3067 public_compare_addr
);
3069 for (unsigned int i
= 0; i
< num_entries
; i
++)
3071 bfd_putl32 (sorted
[i
]->offset
, int_buf
);
3073 if (bfd_bwrite (int_buf
, sizeof (uint32_t), stream
) !=
3084 while (publics_head
)
3086 struct public *p
= publics_head
->next
;
3088 free (publics_head
);
3097 /* The section header stream contains a copy of the section headers
3098 from the PE file, in the same format. */
3100 create_section_header_stream (bfd
*pdb
, bfd
*abfd
, uint16_t *num
)
3103 unsigned int section_count
;
3108 stream
= add_stream (pdb
, NULL
, num
);
3112 section_count
= abfd
->section_count
;
3114 /* Empty sections aren't output. */
3115 for (asection
*sect
= abfd
->sections
; sect
; sect
= sect
->next
)
3117 if (sect
->size
== 0)
3121 if (section_count
== 0)
3124 /* Copy section table from output - it's already been written at this
3127 scn_base
= bfd_coff_filhsz (abfd
) + bfd_coff_aoutsz (abfd
);
3129 bfd_seek (abfd
, scn_base
, SEEK_SET
);
3131 len
= section_count
* sizeof (struct external_scnhdr
);
3132 buf
= xmalloc (len
);
3134 if (bfd_bread (buf
, len
, abfd
) != len
)
3140 if (bfd_bwrite (buf
, len
, stream
) != len
)
3151 /* Populate the "/names" named stream, which contains the string table. */
3153 populate_names_stream (bfd
*stream
, struct string_table
*strings
)
3155 char int_buf
[sizeof (uint32_t)];
3156 struct string_table_header h
;
3157 uint32_t num_strings
= 0, num_buckets
;
3158 struct string
**buckets
;
3160 bfd_putl32 (STRING_TABLE_SIGNATURE
, &h
.signature
);
3161 bfd_putl32 (STRING_TABLE_VERSION
, &h
.version
);
3163 if (bfd_bwrite (&h
, sizeof (h
), stream
) != sizeof (h
))
3166 bfd_putl32 (strings
->strings_len
, int_buf
);
3168 if (bfd_bwrite (int_buf
, sizeof (uint32_t), stream
) != sizeof (uint32_t))
3173 if (bfd_bwrite (int_buf
, 1, stream
) != 1)
3176 for (struct string
*s
= strings
->strings_head
; s
; s
= s
->next
)
3178 if (bfd_bwrite (s
->s
, s
->len
, stream
) != s
->len
)
3181 if (bfd_bwrite (int_buf
, 1, stream
) != 1)
3187 num_buckets
= num_strings
* 2;
3189 buckets
= xmalloc (sizeof (struct string
*) * num_buckets
);
3190 memset (buckets
, 0, sizeof (struct string
*) * num_buckets
);
3192 for (struct string
*s
= strings
->strings_head
; s
; s
= s
->next
)
3194 uint32_t bucket_num
= s
->hash
% num_buckets
;
3196 while (buckets
[bucket_num
])
3200 if (bucket_num
== num_buckets
)
3204 buckets
[bucket_num
] = s
;
3207 bfd_putl32 (num_buckets
, int_buf
);
3209 if (bfd_bwrite (int_buf
, sizeof (uint32_t), stream
) != sizeof (uint32_t))
3215 for (unsigned int i
= 0; i
< num_buckets
; i
++)
3218 bfd_putl32 (buckets
[i
]->offset
, int_buf
);
3220 bfd_putl32 (0, int_buf
);
3222 if (bfd_bwrite (int_buf
, sizeof (uint32_t), stream
) !=
3232 bfd_putl32 (num_strings
, int_buf
);
3234 if (bfd_bwrite (int_buf
, sizeof (uint32_t), stream
) != sizeof (uint32_t))
3240 /* Calculate the hash of a type_entry. */
3242 hash_type_entry (const void *p
)
3244 const struct type_entry
*e
= (const struct type_entry
*) p
;
3245 uint16_t size
= bfd_getl16 (e
->data
) + sizeof (uint16_t);
3247 return iterative_hash (e
->data
, size
, 0);
3250 /* Compare a type_entry with a type. */
3252 eq_type_entry (const void *a
, const void *b
)
3254 const struct type_entry
*e
= (const struct type_entry
*) a
;
3255 uint16_t size_a
= bfd_getl16 (e
->data
);
3256 uint16_t size_b
= bfd_getl16 (b
);
3258 if (size_a
!= size_b
)
3261 return memcmp (e
->data
+ sizeof (uint16_t),
3262 (const uint8_t *) b
+ sizeof (uint16_t), size_a
) == 0;
3265 /* Create a PDB debugging file for the PE image file abfd with the build ID
3266 guid, stored at pdb_name. */
3268 create_pdb_file (bfd
*abfd
, const char *pdb_name
, const unsigned char *guid
)
3272 bfd
*info_stream
, *dbi_stream
, *names_stream
, *sym_rec_stream
,
3273 *publics_stream
, *tpi_stream
, *ipi_stream
;
3274 uint16_t section_header_stream_num
, sym_rec_stream_num
, publics_stream_num
;
3275 struct string_table strings
;
3276 struct types types
, ids
;
3278 pdb
= bfd_openw (pdb_name
, "pdb");
3281 einfo (_("%P: warning: cannot create PDB file: %E\n"));
3285 strings
.strings_head
= NULL
;
3286 strings
.strings_tail
= NULL
;
3287 strings
.strings_len
= 1;
3288 strings
.hashmap
= htab_create_alloc (0, hash_string_table_entry
,
3289 eq_string_table_entry
, free
,
3292 bfd_set_format (pdb
, bfd_archive
);
3294 if (!create_old_directory_stream (pdb
))
3296 einfo (_("%P: warning: cannot create old directory stream "
3297 "in PDB file: %E\n"));
3301 info_stream
= add_stream (pdb
, NULL
, NULL
);
3305 einfo (_("%P: warning: cannot create info stream "
3306 "in PDB file: %E\n"));
3310 tpi_stream
= add_stream (pdb
, NULL
, NULL
);
3314 einfo (_("%P: warning: cannot create TPI stream "
3315 "in PDB file: %E\n"));
3319 dbi_stream
= add_stream (pdb
, NULL
, NULL
);
3323 einfo (_("%P: warning: cannot create DBI stream "
3324 "in PDB file: %E\n"));
3328 ipi_stream
= add_stream (pdb
, NULL
, NULL
);
3332 einfo (_("%P: warning: cannot create IPI stream "
3333 "in PDB file: %E\n"));
3337 names_stream
= add_stream (pdb
, "/names", NULL
);
3341 einfo (_("%P: warning: cannot create /names stream "
3342 "in PDB file: %E\n"));
3346 sym_rec_stream
= add_stream (pdb
, NULL
, &sym_rec_stream_num
);
3348 if (!sym_rec_stream
)
3350 einfo (_("%P: warning: cannot create symbol record stream "
3351 "in PDB file: %E\n"));
3355 publics_stream
= add_stream (pdb
, NULL
, &publics_stream_num
);
3357 if (!publics_stream
)
3359 einfo (_("%P: warning: cannot create publics stream "
3360 "in PDB file: %E\n"));
3364 if (!create_section_header_stream (pdb
, abfd
, §ion_header_stream_num
))
3366 einfo (_("%P: warning: cannot create section header stream "
3367 "in PDB file: %E\n"));
3371 types
.num_types
= 0;
3372 types
.hashmap
= htab_create_alloc (0, hash_type_entry
, eq_type_entry
,
3373 free
, xcalloc
, free
);
3374 types
.first
= types
.last
= NULL
;
3377 ids
.hashmap
= htab_create_alloc (0, hash_type_entry
, eq_type_entry
,
3378 free
, xcalloc
, free
);
3379 ids
.first
= ids
.last
= NULL
;
3381 if (!populate_dbi_stream (dbi_stream
, abfd
, pdb
, section_header_stream_num
,
3382 sym_rec_stream_num
, publics_stream_num
,
3383 &strings
, &types
, &ids
))
3385 einfo (_("%P: warning: cannot populate DBI stream "
3386 "in PDB file: %E\n"));
3387 htab_delete (types
.hashmap
);
3388 htab_delete (ids
.hashmap
);
3392 if (!populate_type_stream (pdb
, tpi_stream
, &types
))
3394 einfo (_("%P: warning: cannot populate TPI stream "
3395 "in PDB file: %E\n"));
3396 htab_delete (types
.hashmap
);
3397 htab_delete (ids
.hashmap
);
3401 htab_delete (types
.hashmap
);
3403 if (!populate_type_stream (pdb
, ipi_stream
, &ids
))
3405 einfo (_("%P: warning: cannot populate IPI stream "
3406 "in PDB file: %E\n"));
3407 htab_delete (ids
.hashmap
);
3411 htab_delete (ids
.hashmap
);
3413 add_string ("", 0, &strings
);
3415 if (!populate_names_stream (names_stream
, &strings
))
3417 einfo (_("%P: warning: cannot populate names stream "
3418 "in PDB file: %E\n"));
3422 if (!populate_publics_stream (publics_stream
, abfd
, sym_rec_stream
))
3424 einfo (_("%P: warning: cannot populate publics stream "
3425 "in PDB file: %E\n"));
3429 if (!populate_info_stream (pdb
, info_stream
, guid
))
3431 einfo (_("%P: warning: cannot populate info stream "
3432 "in PDB file: %E\n"));
3441 htab_delete (strings
.hashmap
);