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
;
74 /* Add a new stream to the PDB archive, and return its BFD. */
76 add_stream (bfd
*pdb
, const char *name
, uint16_t *stream_num
)
81 stream
= bfd_create (name
? name
: "", pdb
);
85 if (!bfd_make_writable (stream
))
91 if (!pdb
->archive_head
)
93 bfd_set_archive_head (pdb
, stream
);
98 bfd
*b
= pdb
->archive_head
;
102 while (b
->archive_next
)
108 b
->archive_next
= stream
;
117 /* Stream 0 ought to be a copy of the MSF directory from the last
118 time the PDB file was written. Because we don't do incremental
119 writes this isn't applicable to us, but we fill it with a dummy
120 value so as not to confuse radare. */
122 create_old_directory_stream (bfd
*pdb
)
125 char buf
[sizeof (uint32_t)];
127 stream
= add_stream (pdb
, NULL
, NULL
);
133 return bfd_bwrite (buf
, sizeof (uint32_t), stream
) == sizeof (uint32_t);
136 /* Calculate the hash of a given string. */
138 calc_hash (const char *data
, size_t len
)
145 hash
^= data
[1] << 8;
146 hash
^= data
[2] << 16;
147 hash
^= data
[3] << 24;
156 hash
^= data
[1] << 8;
166 hash
^= (hash
>> 11);
168 return hash
^ (hash
>> 16);
171 /* Stream 1 is the PDB info stream - see
172 https://llvm.org/docs/PDB/PdbStream.html. */
174 populate_info_stream (bfd
*pdb
, bfd
*info_stream
, const unsigned char *guid
)
177 struct pdb_stream_70 h
;
178 uint32_t num_entries
, num_buckets
;
179 uint32_t names_length
, stream_num
;
180 char int_buf
[sizeof (uint32_t)];
188 struct hash_entry
**buckets
= NULL
;
192 bfd_putl32 (PDB_STREAM_VERSION_VC70
, &h
.version
);
193 bfd_putl32 (time (NULL
), &h
.signature
);
194 bfd_putl32 (1, &h
.age
);
196 bfd_putl32 (bfd_getb32 (guid
), h
.guid
);
197 bfd_putl16 (bfd_getb16 (&guid
[4]), &h
.guid
[4]);
198 bfd_putl16 (bfd_getb16 (&guid
[6]), &h
.guid
[6]);
199 memcpy (&h
.guid
[8], &guid
[8], 8);
201 if (bfd_bwrite (&h
, sizeof (h
), info_stream
) != sizeof (h
))
204 /* Write hash list of named streams. This is a "rollover" hash, i.e.
205 if a bucket is filled an entry gets placed in the next free
209 for (bfd
*b
= pdb
->archive_head
; b
; b
= b
->archive_next
)
211 if (strcmp (b
->filename
, ""))
215 num_buckets
= num_entries
* 2;
222 buckets
= xmalloc (sizeof (struct hash_entry
*) * num_buckets
);
223 memset (buckets
, 0, sizeof (struct hash_entry
*) * num_buckets
);
225 for (bfd
*b
= pdb
->archive_head
; b
; b
= b
->archive_next
)
227 if (strcmp (b
->filename
, ""))
229 size_t len
= strlen (b
->filename
);
230 uint32_t hash
= (uint16_t) calc_hash (b
->filename
, len
);
231 uint32_t bucket_num
= hash
% num_buckets
;
233 while (buckets
[bucket_num
])
237 if (bucket_num
== num_buckets
)
241 buckets
[bucket_num
] = xmalloc (sizeof (struct hash_entry
));
243 buckets
[bucket_num
]->offset
= names_length
;
244 buckets
[bucket_num
]->value
= stream_num
;
246 names_length
+= len
+ 1;
253 /* Write the strings list - the hash keys are indexes into this. */
255 bfd_putl32 (names_length
, int_buf
);
257 if (bfd_bwrite (int_buf
, sizeof (uint32_t), info_stream
) !=
261 for (bfd
*b
= pdb
->archive_head
; b
; b
= b
->archive_next
)
263 if (!strcmp (b
->filename
, ""))
266 size_t len
= strlen (b
->filename
) + 1;
268 if (bfd_bwrite (b
->filename
, len
, info_stream
) != len
)
272 /* Write the number of entries and buckets. */
274 bfd_putl32 (num_entries
, int_buf
);
276 if (bfd_bwrite (int_buf
, sizeof (uint32_t), info_stream
) !=
280 bfd_putl32 (num_buckets
, int_buf
);
282 if (bfd_bwrite (int_buf
, sizeof (uint32_t), info_stream
) !=
286 /* Write the present bitmap. */
288 bfd_putl32 ((num_buckets
+ 31) / 32, int_buf
);
290 if (bfd_bwrite (int_buf
, sizeof (uint32_t), info_stream
) !=
294 for (unsigned int i
= 0; i
< num_buckets
; i
+= 32)
298 for (unsigned int j
= 0; j
< 32; j
++)
300 if (i
+ j
>= num_buckets
)
307 bfd_putl32 (v
, int_buf
);
309 if (bfd_bwrite (int_buf
, sizeof (uint32_t), info_stream
) !=
314 /* Write the (empty) deleted bitmap. */
316 bfd_putl32 (0, int_buf
);
318 if (bfd_bwrite (int_buf
, sizeof (uint32_t), info_stream
) !=
322 /* Write the buckets. */
324 for (unsigned int i
= 0; i
< num_buckets
; i
++)
328 bfd_putl32 (buckets
[i
]->offset
, int_buf
);
330 if (bfd_bwrite (int_buf
, sizeof (uint32_t), info_stream
) !=
334 bfd_putl32 (buckets
[i
]->value
, int_buf
);
336 if (bfd_bwrite (int_buf
, sizeof (uint32_t), info_stream
) !=
342 bfd_putl32 (0, int_buf
);
344 if (bfd_bwrite (int_buf
, sizeof (uint32_t), info_stream
) !=
348 bfd_putl32 (PDB_STREAM_VERSION_VC140
, int_buf
);
350 if (bfd_bwrite (int_buf
, sizeof (uint32_t), info_stream
) !=
357 for (unsigned int i
= 0; i
< num_buckets
; i
++)
368 /* Stream 2 is the type information (TPI) stream, and stream 4 is
369 the ID information (IPI) stream. They differ only in which records
370 go in which stream. */
372 create_type_stream (bfd
*pdb
)
375 struct pdb_tpi_stream_header h
;
377 stream
= add_stream (pdb
, NULL
, NULL
);
381 bfd_putl32 (TPI_STREAM_VERSION_80
, &h
.version
);
382 bfd_putl32 (sizeof (h
), &h
.header_size
);
383 bfd_putl32 (TPI_FIRST_INDEX
, &h
.type_index_begin
);
384 bfd_putl32 (TPI_FIRST_INDEX
, &h
.type_index_end
);
385 bfd_putl32 (0, &h
.type_record_bytes
);
386 bfd_putl16 (0xffff, &h
.hash_stream_index
);
387 bfd_putl16 (0xffff, &h
.hash_aux_stream_index
);
388 bfd_putl32 (4, &h
.hash_key_size
);
389 bfd_putl32 (0x3ffff, &h
.num_hash_buckets
);
390 bfd_putl32 (0, &h
.hash_value_buffer_offset
);
391 bfd_putl32 (0, &h
.hash_value_buffer_length
);
392 bfd_putl32 (0, &h
.index_offset_buffer_offset
);
393 bfd_putl32 (0, &h
.index_offset_buffer_length
);
394 bfd_putl32 (0, &h
.hash_adj_buffer_offset
);
395 bfd_putl32 (0, &h
.hash_adj_buffer_length
);
397 if (bfd_bwrite (&h
, sizeof (h
), stream
) != sizeof (h
))
403 /* Return the PE architecture number for the image. */
405 get_arch_number (bfd
*abfd
)
407 if (abfd
->arch_info
->arch
!= bfd_arch_i386
)
410 if (abfd
->arch_info
->mach
& bfd_mach_x86_64
)
411 return IMAGE_FILE_MACHINE_AMD64
;
413 return IMAGE_FILE_MACHINE_I386
;
416 /* Validate the DEBUG_S_FILECHKSMS entry within a module's .debug$S
417 section, and copy it to the module's symbol stream. */
419 copy_filechksms (uint8_t *data
, uint32_t size
, char *string_table
,
420 struct string_table
*strings
, uint8_t *out
,
421 struct mod_source_files
*mod_source
)
423 uint8_t *orig_data
= data
;
424 uint32_t orig_size
= size
;
425 uint16_t num_files
= 0;
426 struct string
**strptr
;
428 bfd_putl32 (DEBUG_S_FILECHKSMS
, out
);
429 out
+= sizeof (uint32_t);
431 bfd_putl32 (size
, out
);
432 out
+= sizeof (uint32_t);
434 /* Calculate the number of files, and check for any overflows. */
438 struct file_checksum
*fc
= (struct file_checksum
*) data
;
442 if (size
< sizeof (struct file_checksum
))
444 bfd_set_error (bfd_error_bad_value
);
448 len
= sizeof (struct file_checksum
) + fc
->checksum_length
;
452 bfd_set_error (bfd_error_bad_value
);
459 if (len
% sizeof (uint32_t))
460 padding
= sizeof (uint32_t) - (len
% sizeof (uint32_t));
466 bfd_set_error (bfd_error_bad_value
);
476 /* Add the files to mod_source, so that they'll appear in the source
482 uint16_t new_count
= num_files
+ mod_source
->files_count
;
484 mod_source
->files
= xrealloc (mod_source
->files
,
485 sizeof (struct string
*) * new_count
);
487 strptr
= mod_source
->files
+ mod_source
->files_count
;
489 mod_source
->files_count
+= num_files
;
492 /* Actually copy the data. */
499 struct file_checksum
*fc
= (struct file_checksum
*) data
;
503 struct string
*str
= NULL
;
505 string_off
= bfd_getl32 (&fc
->file_id
);
506 len
= sizeof (struct file_checksum
) + fc
->checksum_length
;
508 if (len
% sizeof (uint32_t))
509 padding
= sizeof (uint32_t) - (len
% sizeof (uint32_t));
513 /* Remap the "file ID", i.e. the offset in the module's string table,
514 so it points to the right place in the main string table. */
518 char *fn
= string_table
+ string_off
;
519 size_t fn_len
= strlen (fn
);
520 uint32_t hash
= calc_hash (fn
, fn_len
);
523 slot
= htab_find_slot_with_hash (strings
->hashmap
, fn
, hash
,
527 str
= (struct string
*) *slot
;
533 bfd_putl32 (str
? str
->offset
: 0, &fc
->file_id
);
535 memcpy (out
, data
, len
+ padding
);
537 data
+= len
+ padding
;
538 size
-= len
+ padding
;
539 out
+= len
+ padding
;
545 /* Add a string to the strings table, if it's not already there. */
547 add_string (char *str
, size_t len
, struct string_table
*strings
)
549 uint32_t hash
= calc_hash (str
, len
);
552 slot
= htab_find_slot_with_hash (strings
->hashmap
, str
, hash
, INSERT
);
558 *slot
= xmalloc (offsetof (struct string
, s
) + len
);
560 s
= (struct string
*) *slot
;
564 s
->offset
= strings
->strings_len
;
565 s
->source_file_offset
= 0xffffffff;
567 memcpy (s
->s
, str
, len
);
569 if (strings
->strings_tail
)
570 strings
->strings_tail
->next
= s
;
572 strings
->strings_head
= s
;
574 strings
->strings_tail
= s
;
576 strings
->strings_len
+= len
+ 1;
580 /* Return the hash of an entry in the string table. */
582 hash_string_table_entry (const void *p
)
584 const struct string
*s
= (const struct string
*) p
;
589 /* Compare an entry in the string table with a string. */
591 eq_string_table_entry (const void *a
, const void *b
)
593 const struct string
*s1
= (const struct string
*) a
;
594 const char *s2
= (const char *) b
;
595 size_t s2_len
= strlen (s2
);
597 if (s2_len
!= s1
->len
)
600 return memcmp (s1
->s
, s2
, s2_len
) == 0;
603 /* Parse the string table within the .debug$S section. */
605 parse_string_table (bfd_byte
*data
, size_t size
,
606 struct string_table
*strings
)
610 size_t len
= strnlen ((char *) data
, size
);
612 add_string ((char *) data
, len
, strings
);
623 /* Parse the .debug$S section within an object file. */
625 handle_debugs_section (asection
*s
, bfd
*mod
, struct string_table
*strings
,
626 uint8_t **dataptr
, uint32_t *sizeptr
,
627 struct mod_source_files
*mod_source
)
629 bfd_byte
*data
= NULL
;
631 uint32_t c13_size
= 0;
632 char *string_table
= NULL
;
633 uint8_t *buf
, *bufptr
;
635 if (!bfd_get_full_section_contents (mod
, s
, &data
))
641 if (bfd_getl32 (data
) != CV_SIGNATURE_C13
)
647 off
= sizeof (uint32_t);
651 while (off
+ sizeof (uint32_t) <= s
->size
)
655 type
= bfd_getl32 (data
+ off
);
657 off
+= sizeof (uint32_t);
659 if (off
+ sizeof (uint32_t) > s
->size
)
662 bfd_set_error (bfd_error_bad_value
);
666 size
= bfd_getl32 (data
+ off
);
668 off
+= sizeof (uint32_t);
670 if (off
+ size
> s
->size
)
673 bfd_set_error (bfd_error_bad_value
);
679 case DEBUG_S_FILECHKSMS
:
680 c13_size
+= sizeof (uint32_t) + sizeof (uint32_t) + size
;
682 if (c13_size
% sizeof (uint32_t))
683 c13_size
+= sizeof (uint32_t) - (c13_size
% sizeof (uint32_t));
687 case DEBUG_S_STRINGTABLE
:
688 parse_string_table (data
+ off
, size
, strings
);
690 string_table
= (char *) data
+ off
;
697 if (off
% sizeof (uint32_t))
698 off
+= sizeof (uint32_t) - (off
% sizeof (uint32_t));
709 buf
= xmalloc (c13_size
);
712 off
= sizeof (uint32_t);
714 while (off
+ sizeof (uint32_t) <= s
->size
)
718 type
= bfd_getl32 (data
+ off
);
719 off
+= sizeof (uint32_t);
721 size
= bfd_getl32 (data
+ off
);
722 off
+= sizeof (uint32_t);
726 case DEBUG_S_FILECHKSMS
:
727 if (!copy_filechksms (data
+ off
, size
, string_table
,
728 strings
, bufptr
, mod_source
))
734 bufptr
+= sizeof (uint32_t) + sizeof (uint32_t) + size
;
741 if (off
% sizeof (uint32_t))
742 off
+= sizeof (uint32_t) - (off
% sizeof (uint32_t));
749 /* Append the C13 info to what's already there, if the module has
750 multiple .debug$S sections. */
752 *dataptr
= xrealloc (*dataptr
, *sizeptr
+ c13_size
);
753 memcpy (*dataptr
+ *sizeptr
, buf
, c13_size
);
762 *sizeptr
+= c13_size
;
767 /* Populate the module stream, which consists of the transformed .debug$S
768 data for each object file. */
770 populate_module_stream (bfd
*stream
, bfd
*mod
, uint32_t *sym_byte_size
,
771 struct string_table
*strings
,
772 uint32_t *c13_info_size
,
773 struct mod_source_files
*mod_source
)
775 uint8_t int_buf
[sizeof (uint32_t)];
776 uint8_t *c13_info
= NULL
;
778 *sym_byte_size
= sizeof (uint32_t);
781 /* Process .debug$S section(s). */
783 for (asection
*s
= mod
->sections
; s
; s
= s
->next
)
785 if (!strcmp (s
->name
, ".debug$S") && s
->size
>= sizeof (uint32_t))
787 if (!handle_debugs_section (s
, mod
, strings
, &c13_info
,
788 c13_info_size
, mod_source
))
791 free (mod_source
->files
);
797 /* Write the signature. */
799 bfd_putl32 (CV_SIGNATURE_C13
, int_buf
);
801 if (bfd_bwrite (int_buf
, sizeof (uint32_t), stream
) != sizeof (uint32_t))
809 if (bfd_bwrite (c13_info
, *c13_info_size
, stream
) != *c13_info_size
)
818 /* Write the global refs size. */
820 bfd_putl32 (0, int_buf
);
822 if (bfd_bwrite (int_buf
, sizeof (uint32_t), stream
) != sizeof (uint32_t))
828 /* Create the module info substream within the DBI. */
830 create_module_info_substream (bfd
*abfd
, bfd
*pdb
, void **data
,
831 uint32_t *size
, struct string_table
*strings
,
832 struct source_files_info
*source
)
835 unsigned int mod_num
;
837 static const char linker_fn
[] = "* Linker *";
841 for (bfd
*in
= coff_data (abfd
)->link_info
->input_bfds
; in
;
844 size_t len
= sizeof (struct module_info
);
846 if (!strcmp (bfd_get_filename (in
), "dll stuff"))
848 len
+= sizeof (linker_fn
); /* Object name. */
849 len
++; /* Empty module name. */
851 else if (in
->my_archive
)
853 char *name
= lrealpath (bfd_get_filename (in
));
855 len
+= strlen (name
) + 1; /* Object name. */
859 name
= lrealpath (bfd_get_filename (in
->my_archive
));
861 len
+= strlen (name
) + 1; /* Archive name. */
867 char *name
= lrealpath (bfd_get_filename (in
));
868 size_t name_len
= strlen (name
) + 1;
870 len
+= name_len
; /* Object name. */
871 len
+= name_len
; /* And again as the archive name. */
877 len
+= 4 - (len
% 4);
884 *data
= xmalloc (*size
);
888 source
->mods
= xmalloc (source
->mod_count
889 * sizeof (struct mod_source_files
));
890 memset (source
->mods
, 0,
891 source
->mod_count
* sizeof (struct mod_source_files
));
895 for (bfd
*in
= coff_data (abfd
)->link_info
->input_bfds
; in
;
898 struct module_info
*mod
= (struct module_info
*) ptr
;
901 uint32_t sym_byte_size
, c13_info_size
;
902 uint8_t *start
= ptr
;
904 stream
= add_stream (pdb
, NULL
, &stream_num
);
908 for (unsigned int i
= 0; i
< source
->mod_count
; i
++)
910 free (source
->mods
[i
].files
);
918 if (!populate_module_stream (stream
, in
, &sym_byte_size
,
919 strings
, &c13_info_size
,
920 &source
->mods
[mod_num
]))
922 for (unsigned int i
= 0; i
< source
->mod_count
; i
++)
924 free (source
->mods
[i
].files
);
932 bfd_putl32 (0, &mod
->unused1
);
934 /* These are dummy values - MSVC copies the first section contribution
935 entry here, but doesn't seem to use it for anything. */
936 bfd_putl16 (0xffff, &mod
->sc
.section
);
937 bfd_putl16 (0, &mod
->sc
.padding1
);
938 bfd_putl32 (0, &mod
->sc
.offset
);
939 bfd_putl32 (0xffffffff, &mod
->sc
.size
);
940 bfd_putl32 (0, &mod
->sc
.characteristics
);
941 bfd_putl16 (0xffff, &mod
->sc
.module_index
);
942 bfd_putl16 (0, &mod
->sc
.padding2
);
943 bfd_putl32 (0, &mod
->sc
.data_crc
);
944 bfd_putl32 (0, &mod
->sc
.reloc_crc
);
946 bfd_putl16 (0, &mod
->flags
);
947 bfd_putl16 (stream_num
, &mod
->module_sym_stream
);
948 bfd_putl32 (sym_byte_size
, &mod
->sym_byte_size
);
949 bfd_putl32 (0, &mod
->c11_byte_size
);
950 bfd_putl32 (c13_info_size
, &mod
->c13_byte_size
);
951 bfd_putl16 (0, &mod
->source_file_count
);
952 bfd_putl16 (0, &mod
->padding
);
953 bfd_putl32 (0, &mod
->unused2
);
954 bfd_putl32 (0, &mod
->source_file_name_index
);
955 bfd_putl32 (0, &mod
->pdb_file_path_name_index
);
957 ptr
+= sizeof (struct module_info
);
959 if (!strcmp (bfd_get_filename (in
), "dll stuff"))
962 memcpy (ptr
, linker_fn
, sizeof (linker_fn
));
963 ptr
+= sizeof (linker_fn
);
965 /* Empty module name. */
969 else if (in
->my_archive
)
971 char *name
= lrealpath (bfd_get_filename (in
));
972 size_t name_len
= strlen (name
) + 1;
975 memcpy (ptr
, name
, name_len
);
980 name
= lrealpath (bfd_get_filename (in
->my_archive
));
981 name_len
= strlen (name
) + 1;
984 memcpy (ptr
, name
, name_len
);
991 char *name
= lrealpath (bfd_get_filename (in
));
992 size_t name_len
= strlen (name
) + 1;
995 memcpy (ptr
, name
, name_len
);
998 /* Object name again as archive name. */
999 memcpy (ptr
, name
, name_len
);
1005 /* Pad to next four-byte boundary. */
1007 if ((ptr
- start
) % 4)
1009 memset (ptr
, 0, 4 - ((ptr
- start
) % 4));
1010 ptr
+= 4 - ((ptr
- start
) % 4);
1019 /* Return the index of a given output section. */
1021 find_section_number (bfd
*abfd
, asection
*sect
)
1025 for (asection
*s
= abfd
->sections
; s
; s
= s
->next
)
1030 /* Empty sections aren't output. */
1038 /* Create the substream which maps addresses in the image file to locations
1039 in the original object files. */
1041 create_section_contrib_substream (bfd
*abfd
, void **data
, uint32_t *size
)
1043 unsigned int num_sc
= 0;
1044 struct section_contribution
*sc
;
1049 for (bfd
*in
= coff_data (abfd
)->link_info
->input_bfds
; in
;
1052 for (asection
*s
= in
->sections
; s
; s
= s
->next
)
1054 if (s
->size
== 0 || discarded_section (s
))
1061 *size
= sizeof (uint32_t) + (num_sc
* sizeof (struct section_contribution
));
1062 *data
= xmalloc (*size
);
1064 bfd_putl32 (SECTION_CONTRIB_VERSION_60
, *data
);
1066 /* Read characteristics of outputted sections. */
1068 sect_flags
= xmalloc (sizeof (uint32_t) * abfd
->section_count
);
1070 offset
= bfd_coff_filhsz (abfd
) + bfd_coff_aoutsz (abfd
);
1071 offset
+= offsetof (struct external_scnhdr
, s_flags
);
1073 for (unsigned int i
= 0; i
< abfd
->section_count
; i
++)
1075 bfd_seek (abfd
, offset
, SEEK_SET
);
1077 if (bfd_bread (sect_flags
+ (i
* sizeof (uint32_t)), sizeof (uint32_t),
1078 abfd
) != sizeof (uint32_t))
1085 offset
+= sizeof (struct external_scnhdr
);
1089 (struct section_contribution
*) ((uint8_t *) *data
+ sizeof (uint32_t));
1092 for (bfd
*in
= coff_data (abfd
)->link_info
->input_bfds
; in
;
1095 for (asection
*s
= in
->sections
; s
; s
= s
->next
)
1099 if (s
->size
== 0 || discarded_section (s
))
1102 sect_num
= find_section_number (abfd
, s
->output_section
);
1104 memcpy (&sc
->characteristics
,
1105 sect_flags
+ ((sect_num
- 1) * sizeof (uint32_t)),
1108 bfd_putl16 (sect_num
, &sc
->section
);
1109 bfd_putl16 (0, &sc
->padding1
);
1110 bfd_putl32 (s
->output_offset
, &sc
->offset
);
1111 bfd_putl32 (s
->size
, &sc
->size
);
1112 bfd_putl16 (mod_index
, &sc
->module_index
);
1113 bfd_putl16 (0, &sc
->padding2
);
1114 bfd_putl32 (0, &sc
->data_crc
);
1115 bfd_putl32 (0, &sc
->reloc_crc
);
1128 /* The source info substream lives within the DBI stream, and lists the
1129 source files for each object file (i.e. it's derived from the
1130 DEBUG_S_FILECHKSMS parts of the .debug$S sections). This is a bit
1131 superfluous, as the filenames are also available in the C13 parts of
1132 the module streams, but MSVC relies on it to work properly. */
1134 create_source_info_substream (void **data
, uint32_t *size
,
1135 struct source_files_info
*source
)
1137 uint16_t dedupe_source_files_count
= 0;
1138 uint16_t source_files_count
= 0;
1139 uint32_t strings_len
= 0;
1142 /* Loop through the source files, marking unique filenames. The pointers
1143 here are for entries in the main string table, and so have already
1144 been deduplicated. */
1146 for (uint16_t i
= 0; i
< source
->mod_count
; i
++)
1148 for (uint16_t j
= 0; j
< source
->mods
[i
].files_count
; j
++)
1150 if (source
->mods
[i
].files
[j
])
1152 if (source
->mods
[i
].files
[j
]->source_file_offset
== 0xffffffff)
1154 source
->mods
[i
].files
[j
]->source_file_offset
= strings_len
;
1155 strings_len
+= source
->mods
[i
].files
[j
]->len
+ 1;
1156 dedupe_source_files_count
++;
1159 source_files_count
++;
1164 *size
= sizeof (uint16_t) + sizeof (uint16_t);
1165 *size
+= (sizeof (uint16_t) + sizeof (uint16_t)) * source
->mod_count
;
1166 *size
+= sizeof (uint32_t) * source_files_count
;
1167 *size
+= strings_len
;
1169 *data
= xmalloc (*size
);
1171 ptr
= (uint8_t *) *data
;
1173 /* Write header (module count and source file count). */
1175 bfd_putl16 (source
->mod_count
, ptr
);
1176 ptr
+= sizeof (uint16_t);
1178 bfd_putl16 (dedupe_source_files_count
, ptr
);
1179 ptr
+= sizeof (uint16_t);
1181 /* Write "ModIndices". As the LLVM documentation puts it, "this array is
1182 present, but does not appear to be useful". */
1184 for (uint16_t i
= 0; i
< source
->mod_count
; i
++)
1186 bfd_putl16 (i
, ptr
);
1187 ptr
+= sizeof (uint16_t);
1190 /* Write source file count for each module. */
1192 for (uint16_t i
= 0; i
< source
->mod_count
; i
++)
1194 bfd_putl16 (source
->mods
[i
].files_count
, ptr
);
1195 ptr
+= sizeof (uint16_t);
1198 /* For each module, write the offsets within the string table
1199 for each source file. */
1201 for (uint16_t i
= 0; i
< source
->mod_count
; i
++)
1203 for (uint16_t j
= 0; j
< source
->mods
[i
].files_count
; j
++)
1205 if (source
->mods
[i
].files
[j
])
1207 bfd_putl32 (source
->mods
[i
].files
[j
]->source_file_offset
, ptr
);
1208 ptr
+= sizeof (uint32_t);
1213 /* Write the string table. We set source_file_offset to a dummy value for
1214 each entry we write, so we don't write duplicate filenames. */
1216 for (uint16_t i
= 0; i
< source
->mod_count
; i
++)
1218 for (uint16_t j
= 0; j
< source
->mods
[i
].files_count
; j
++)
1220 if (source
->mods
[i
].files
[j
]
1221 && source
->mods
[i
].files
[j
]->source_file_offset
!= 0xffffffff)
1223 memcpy (ptr
, source
->mods
[i
].files
[j
]->s
,
1224 source
->mods
[i
].files
[j
]->len
);
1225 ptr
+= source
->mods
[i
].files
[j
]->len
;
1230 source
->mods
[i
].files
[j
]->source_file_offset
= 0xffffffff;
1236 /* Stream 4 is the debug information (DBI) stream. */
1238 populate_dbi_stream (bfd
*stream
, bfd
*abfd
, bfd
*pdb
,
1239 uint16_t section_header_stream_num
,
1240 uint16_t sym_rec_stream_num
,
1241 uint16_t publics_stream_num
,
1242 struct string_table
*strings
)
1244 struct pdb_dbi_stream_header h
;
1245 struct optional_dbg_header opt
;
1246 void *mod_info
, *sc
, *source_info
;
1247 uint32_t mod_info_size
, sc_size
, source_info_size
;
1248 struct source_files_info source
;
1250 source
.mod_count
= 0;
1253 if (!create_module_info_substream (abfd
, pdb
, &mod_info
, &mod_info_size
,
1257 if (!create_section_contrib_substream (abfd
, &sc
, &sc_size
))
1259 for (unsigned int i
= 0; i
< source
.mod_count
; i
++)
1261 free (source
.mods
[i
].files
);
1269 create_source_info_substream (&source_info
, &source_info_size
, &source
);
1271 for (unsigned int i
= 0; i
< source
.mod_count
; i
++)
1273 free (source
.mods
[i
].files
);
1277 bfd_putl32 (0xffffffff, &h
.version_signature
);
1278 bfd_putl32 (DBI_STREAM_VERSION_70
, &h
.version_header
);
1279 bfd_putl32 (1, &h
.age
);
1280 bfd_putl16 (0xffff, &h
.global_stream_index
);
1281 bfd_putl16 (0x8e1d, &h
.build_number
); // MSVC 14.29
1282 bfd_putl16 (publics_stream_num
, &h
.public_stream_index
);
1283 bfd_putl16 (0, &h
.pdb_dll_version
);
1284 bfd_putl16 (sym_rec_stream_num
, &h
.sym_record_stream
);
1285 bfd_putl16 (0, &h
.pdb_dll_rbld
);
1286 bfd_putl32 (mod_info_size
, &h
.mod_info_size
);
1287 bfd_putl32 (sc_size
, &h
.section_contribution_size
);
1288 bfd_putl32 (0, &h
.section_map_size
);
1289 bfd_putl32 (source_info_size
, &h
.source_info_size
);
1290 bfd_putl32 (0, &h
.type_server_map_size
);
1291 bfd_putl32 (0, &h
.mfc_type_server_index
);
1292 bfd_putl32 (sizeof (opt
), &h
.optional_dbg_header_size
);
1293 bfd_putl32 (0, &h
.ec_substream_size
);
1294 bfd_putl16 (0, &h
.flags
);
1295 bfd_putl16 (get_arch_number (abfd
), &h
.machine
);
1296 bfd_putl32 (0, &h
.padding
);
1298 if (bfd_bwrite (&h
, sizeof (h
), stream
) != sizeof (h
))
1306 if (bfd_bwrite (mod_info
, mod_info_size
, stream
) != mod_info_size
)
1316 if (bfd_bwrite (sc
, sc_size
, stream
) != sc_size
)
1325 if (bfd_bwrite (source_info
, source_info_size
, stream
) != source_info_size
)
1333 bfd_putl16 (0xffff, &opt
.fpo_stream
);
1334 bfd_putl16 (0xffff, &opt
.exception_stream
);
1335 bfd_putl16 (0xffff, &opt
.fixup_stream
);
1336 bfd_putl16 (0xffff, &opt
.omap_to_src_stream
);
1337 bfd_putl16 (0xffff, &opt
.omap_from_src_stream
);
1338 bfd_putl16 (section_header_stream_num
, &opt
.section_header_stream
);
1339 bfd_putl16 (0xffff, &opt
.token_map_stream
);
1340 bfd_putl16 (0xffff, &opt
.xdata_stream
);
1341 bfd_putl16 (0xffff, &opt
.pdata_stream
);
1342 bfd_putl16 (0xffff, &opt
.new_fpo_stream
);
1343 bfd_putl16 (0xffff, &opt
.orig_section_header_stream
);
1345 if (bfd_bwrite (&opt
, sizeof (opt
), stream
) != sizeof (opt
))
1351 /* Used as parameter to qsort, to sort publics by hash. */
1353 public_compare_hash (const void *s1
, const void *s2
)
1355 const struct public *p1
= *(const struct public **) s1
;
1356 const struct public *p2
= *(const struct public **) s2
;
1358 if (p1
->hash
< p2
->hash
)
1360 if (p1
->hash
> p2
->hash
)
1366 /* Used as parameter to qsort, to sort publics by address. */
1368 public_compare_addr (const void *s1
, const void *s2
)
1370 const struct public *p1
= *(const struct public **) s1
;
1371 const struct public *p2
= *(const struct public **) s2
;
1373 if (p1
->section
< p2
->section
)
1375 if (p1
->section
> p2
->section
)
1378 if (p1
->address
< p2
->address
)
1380 if (p1
->address
> p2
->address
)
1386 /* The publics stream is a hash map of S_PUB32 records, which are stored
1387 in the symbol record stream. Each S_PUB32 entry represents a symbol
1388 from the point of view of the linker: a section index, an offset within
1389 the section, and a mangled name. Compare with S_GDATA32 and S_GPROC32,
1390 which are the same thing but generated by the compiler. */
1392 populate_publics_stream (bfd
*stream
, bfd
*abfd
, bfd
*sym_rec_stream
)
1394 struct publics_header header
;
1395 struct globals_hash_header hash_header
;
1396 const unsigned int num_buckets
= 4096;
1397 unsigned int num_entries
= 0, filled_buckets
= 0;
1398 unsigned int buckets_size
, sym_hash_size
;
1399 char int_buf
[sizeof (uint32_t)];
1400 struct public *publics_head
= NULL
, *publics_tail
= NULL
;
1401 struct public **buckets
;
1402 struct public **sorted
= NULL
;
1405 buckets
= xmalloc (sizeof (struct public *) * num_buckets
);
1406 memset (buckets
, 0, sizeof (struct public *) * num_buckets
);
1408 /* Loop through the global symbols in our input files, and write S_PUB32
1409 records in the symbol record stream for those that make it into the
1411 for (bfd
*in
= coff_data (abfd
)->link_info
->input_bfds
; in
;
1414 if (!in
->outsymbols
)
1417 for (unsigned int i
= 0; i
< in
->symcount
; i
++)
1419 struct bfd_symbol
*sym
= in
->outsymbols
[i
];
1421 if (sym
->flags
& BSF_GLOBAL
)
1424 uint16_t record_length
;
1425 const char *name
= sym
->name
;
1426 size_t name_len
= strlen (name
);
1427 struct public *p
= xmalloc (sizeof (struct public));
1428 unsigned int padding
= 0;
1433 find_section_number (abfd
, sym
->section
->output_section
);
1439 p
->offset
= bfd_tell (sym_rec_stream
);
1440 p
->hash
= calc_hash (name
, name_len
) % num_buckets
;
1441 p
->section
= section
;
1442 p
->address
= sym
->section
->output_offset
+ sym
->value
;
1444 record_length
= sizeof (struct pubsym
) + name_len
+ 1;
1446 if (record_length
% 4)
1447 padding
= 4 - (record_length
% 4);
1449 /* Assume that all global symbols in executable sections
1451 if (sym
->section
->flags
& SEC_CODE
)
1452 flags
= PUBSYM_FUNCTION
;
1454 bfd_putl16 (record_length
+ padding
- sizeof (uint16_t),
1456 bfd_putl16 (S_PUB32
, &ps
.record_type
);
1457 bfd_putl32 (flags
, &ps
.flags
);
1458 bfd_putl32 (p
->address
, &ps
.offset
);
1459 bfd_putl16 (p
->section
, &ps
.section
);
1461 if (bfd_bwrite (&ps
, sizeof (struct pubsym
), sym_rec_stream
) !=
1462 sizeof (struct pubsym
))
1465 if (bfd_bwrite (name
, name_len
+ 1, sym_rec_stream
) !=
1469 for (unsigned int j
= 0; j
< padding
; j
++)
1473 if (bfd_bwrite (&b
, sizeof (uint8_t), sym_rec_stream
) !=
1481 publics_tail
->next
= p
;
1490 if (num_entries
> 0)
1492 /* Create an array of pointers, sorted by hash value. */
1494 sorted
= xmalloc (sizeof (struct public *) * num_entries
);
1496 struct public *p
= publics_head
;
1497 for (unsigned int i
= 0; i
< num_entries
; i
++)
1503 qsort (sorted
, num_entries
, sizeof (struct public *),
1504 public_compare_hash
);
1506 /* Populate the buckets. */
1508 for (unsigned int i
= 0; i
< num_entries
; i
++)
1510 if (!buckets
[sorted
[i
]->hash
])
1512 buckets
[sorted
[i
]->hash
] = sorted
[i
];
1516 sorted
[i
]->index
= i
;
1520 buckets_size
= num_buckets
/ 8;
1521 buckets_size
+= sizeof (uint32_t);
1522 buckets_size
+= filled_buckets
* sizeof (uint32_t);
1524 sym_hash_size
= sizeof (hash_header
);
1525 sym_hash_size
+= num_entries
* sizeof (struct hash_record
);
1526 sym_hash_size
+= buckets_size
;
1528 /* Output the publics header. */
1530 bfd_putl32 (sym_hash_size
, &header
.sym_hash_size
);
1531 bfd_putl32 (num_entries
* sizeof (uint32_t), &header
.addr_map_size
);
1532 bfd_putl32 (0, &header
.num_thunks
);
1533 bfd_putl32 (0, &header
.thunks_size
);
1534 bfd_putl32 (0, &header
.thunk_table
);
1535 bfd_putl32 (0, &header
.thunk_table_offset
);
1536 bfd_putl32 (0, &header
.num_sects
);
1538 if (bfd_bwrite (&header
, sizeof (header
), stream
) != sizeof (header
))
1541 /* Output the global hash header. */
1543 bfd_putl32 (GLOBALS_HASH_SIGNATURE
, &hash_header
.signature
);
1544 bfd_putl32 (GLOBALS_HASH_VERSION_70
, &hash_header
.version
);
1545 bfd_putl32 (num_entries
* sizeof (struct hash_record
),
1546 &hash_header
.entries_size
);
1547 bfd_putl32 (buckets_size
, &hash_header
.buckets_size
);
1549 if (bfd_bwrite (&hash_header
, sizeof (hash_header
), stream
) !=
1550 sizeof (hash_header
))
1553 /* Write the entries in hash order. */
1555 for (unsigned int i
= 0; i
< num_entries
; i
++)
1557 struct hash_record hr
;
1559 bfd_putl32 (sorted
[i
]->offset
+ 1, &hr
.offset
);
1560 bfd_putl32 (1, &hr
.reference
);
1562 if (bfd_bwrite (&hr
, sizeof (hr
), stream
) != sizeof (hr
))
1566 /* Write the bitmap for filled and unfilled buckets. */
1568 for (unsigned int i
= 0; i
< num_buckets
; i
+= 8)
1572 for (unsigned int j
= 0; j
< 8; j
++)
1578 if (bfd_bwrite (&v
, sizeof (v
), stream
) != sizeof (v
))
1582 /* Add a 4-byte gap. */
1584 bfd_putl32 (0, int_buf
);
1586 if (bfd_bwrite (int_buf
, sizeof (uint32_t), stream
) != sizeof (uint32_t))
1589 /* Write the bucket offsets. */
1591 for (unsigned int i
= 0; i
< num_buckets
; i
++)
1595 /* 0xc is size of internal hash_record structure in
1596 Microsoft's parser. */
1597 bfd_putl32 (buckets
[i
]->index
* 0xc, int_buf
);
1599 if (bfd_bwrite (int_buf
, sizeof (uint32_t), stream
) !=
1605 /* Write the address map: offsets into the symbol record stream of
1606 S_PUB32 records, ordered by address. */
1608 if (num_entries
> 0)
1610 qsort (sorted
, num_entries
, sizeof (struct public *),
1611 public_compare_addr
);
1613 for (unsigned int i
= 0; i
< num_entries
; i
++)
1615 bfd_putl32 (sorted
[i
]->offset
, int_buf
);
1617 if (bfd_bwrite (int_buf
, sizeof (uint32_t), stream
) !=
1628 while (publics_head
)
1630 struct public *p
= publics_head
->next
;
1632 free (publics_head
);
1641 /* The section header stream contains a copy of the section headers
1642 from the PE file, in the same format. */
1644 create_section_header_stream (bfd
*pdb
, bfd
*abfd
, uint16_t *num
)
1647 unsigned int section_count
;
1652 stream
= add_stream (pdb
, NULL
, num
);
1656 section_count
= abfd
->section_count
;
1658 /* Empty sections aren't output. */
1659 for (asection
*sect
= abfd
->sections
; sect
; sect
= sect
->next
)
1661 if (sect
->size
== 0)
1665 if (section_count
== 0)
1668 /* Copy section table from output - it's already been written at this
1671 scn_base
= bfd_coff_filhsz (abfd
) + bfd_coff_aoutsz (abfd
);
1673 bfd_seek (abfd
, scn_base
, SEEK_SET
);
1675 len
= section_count
* sizeof (struct external_scnhdr
);
1676 buf
= xmalloc (len
);
1678 if (bfd_bread (buf
, len
, abfd
) != len
)
1684 if (bfd_bwrite (buf
, len
, stream
) != len
)
1695 /* Populate the "/names" named stream, which contains the string table. */
1697 populate_names_stream (bfd
*stream
, struct string_table
*strings
)
1699 char int_buf
[sizeof (uint32_t)];
1700 struct string_table_header h
;
1701 uint32_t num_strings
= 0, num_buckets
;
1702 struct string
**buckets
;
1704 bfd_putl32 (STRING_TABLE_SIGNATURE
, &h
.signature
);
1705 bfd_putl32 (STRING_TABLE_VERSION
, &h
.version
);
1707 if (bfd_bwrite (&h
, sizeof (h
), stream
) != sizeof (h
))
1710 bfd_putl32 (strings
->strings_len
, int_buf
);
1712 if (bfd_bwrite (int_buf
, sizeof (uint32_t), stream
) != sizeof (uint32_t))
1717 if (bfd_bwrite (int_buf
, 1, stream
) != 1)
1720 for (struct string
*s
= strings
->strings_head
; s
; s
= s
->next
)
1722 if (bfd_bwrite (s
->s
, s
->len
, stream
) != s
->len
)
1725 if (bfd_bwrite (int_buf
, 1, stream
) != 1)
1731 num_buckets
= num_strings
* 2;
1733 buckets
= xmalloc (sizeof (struct string
*) * num_buckets
);
1734 memset (buckets
, 0, sizeof (struct string
*) * num_buckets
);
1736 for (struct string
*s
= strings
->strings_head
; s
; s
= s
->next
)
1738 uint32_t bucket_num
= s
->hash
% num_buckets
;
1740 while (buckets
[bucket_num
])
1744 if (bucket_num
== num_buckets
)
1748 buckets
[bucket_num
] = s
;
1751 bfd_putl32 (num_buckets
, int_buf
);
1753 if (bfd_bwrite (int_buf
, sizeof (uint32_t), stream
) != sizeof (uint32_t))
1759 for (unsigned int i
= 0; i
< num_buckets
; i
++)
1762 bfd_putl32 (buckets
[i
]->offset
, int_buf
);
1764 bfd_putl32 (0, int_buf
);
1766 if (bfd_bwrite (int_buf
, sizeof (uint32_t), stream
) !=
1776 bfd_putl32 (num_strings
, int_buf
);
1778 if (bfd_bwrite (int_buf
, sizeof (uint32_t), stream
) != sizeof (uint32_t))
1784 /* Create a PDB debugging file for the PE image file abfd with the build ID
1785 guid, stored at pdb_name. */
1787 create_pdb_file (bfd
*abfd
, const char *pdb_name
, const unsigned char *guid
)
1791 bfd
*info_stream
, *dbi_stream
, *names_stream
, *sym_rec_stream
,
1793 uint16_t section_header_stream_num
, sym_rec_stream_num
, publics_stream_num
;
1794 struct string_table strings
;
1796 pdb
= bfd_openw (pdb_name
, "pdb");
1799 einfo (_("%P: warning: cannot create PDB file: %E\n"));
1803 strings
.strings_head
= NULL
;
1804 strings
.strings_tail
= NULL
;
1805 strings
.strings_len
= 1;
1806 strings
.hashmap
= htab_create_alloc (0, hash_string_table_entry
,
1807 eq_string_table_entry
, free
,
1810 bfd_set_format (pdb
, bfd_archive
);
1812 if (!create_old_directory_stream (pdb
))
1814 einfo (_("%P: warning: cannot create old directory stream "
1815 "in PDB file: %E\n"));
1819 info_stream
= add_stream (pdb
, NULL
, NULL
);
1823 einfo (_("%P: warning: cannot create info stream "
1824 "in PDB file: %E\n"));
1828 if (!create_type_stream (pdb
))
1830 einfo (_("%P: warning: cannot create TPI stream "
1831 "in PDB file: %E\n"));
1835 dbi_stream
= add_stream (pdb
, NULL
, NULL
);
1839 einfo (_("%P: warning: cannot create DBI stream "
1840 "in PDB file: %E\n"));
1844 if (!create_type_stream (pdb
))
1846 einfo (_("%P: warning: cannot create IPI stream "
1847 "in PDB file: %E\n"));
1851 names_stream
= add_stream (pdb
, "/names", NULL
);
1855 einfo (_("%P: warning: cannot create /names stream "
1856 "in PDB file: %E\n"));
1860 sym_rec_stream
= add_stream (pdb
, NULL
, &sym_rec_stream_num
);
1862 if (!sym_rec_stream
)
1864 einfo (_("%P: warning: cannot create symbol record stream "
1865 "in PDB file: %E\n"));
1869 publics_stream
= add_stream (pdb
, NULL
, &publics_stream_num
);
1871 if (!publics_stream
)
1873 einfo (_("%P: warning: cannot create publics stream "
1874 "in PDB file: %E\n"));
1878 if (!create_section_header_stream (pdb
, abfd
, §ion_header_stream_num
))
1880 einfo (_("%P: warning: cannot create section header stream "
1881 "in PDB file: %E\n"));
1885 if (!populate_dbi_stream (dbi_stream
, abfd
, pdb
, section_header_stream_num
,
1886 sym_rec_stream_num
, publics_stream_num
,
1889 einfo (_("%P: warning: cannot populate DBI stream "
1890 "in PDB file: %E\n"));
1894 add_string ("", 0, &strings
);
1896 if (!populate_names_stream (names_stream
, &strings
))
1898 einfo (_("%P: warning: cannot populate names stream "
1899 "in PDB file: %E\n"));
1903 if (!populate_publics_stream (publics_stream
, abfd
, sym_rec_stream
))
1905 einfo (_("%P: warning: cannot populate publics stream "
1906 "in PDB file: %E\n"));
1910 if (!populate_info_stream (pdb
, info_stream
, guid
))
1912 einfo (_("%P: warning: cannot populate info stream "
1913 "in PDB file: %E\n"));
1922 htab_delete (strings
.hashmap
);