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"
55 struct string
*strings_head
;
56 struct string
*strings_tail
;
61 /* Add a new stream to the PDB archive, and return its BFD. */
63 add_stream (bfd
*pdb
, const char *name
, uint16_t *stream_num
)
68 stream
= bfd_create (name
? name
: "", pdb
);
72 if (!bfd_make_writable (stream
))
78 if (!pdb
->archive_head
)
80 bfd_set_archive_head (pdb
, stream
);
85 bfd
*b
= pdb
->archive_head
;
89 while (b
->archive_next
)
95 b
->archive_next
= stream
;
104 /* Stream 0 ought to be a copy of the MSF directory from the last
105 time the PDB file was written. Because we don't do incremental
106 writes this isn't applicable to us, but we fill it with a dummy
107 value so as not to confuse radare. */
109 create_old_directory_stream (bfd
*pdb
)
112 char buf
[sizeof (uint32_t)];
114 stream
= add_stream (pdb
, NULL
, NULL
);
120 return bfd_bwrite (buf
, sizeof (uint32_t), stream
) == sizeof (uint32_t);
123 /* Calculate the hash of a given string. */
125 calc_hash (const char *data
, size_t len
)
132 hash
^= data
[1] << 8;
133 hash
^= data
[2] << 16;
134 hash
^= data
[3] << 24;
143 hash
^= data
[1] << 8;
153 hash
^= (hash
>> 11);
155 return hash
^ (hash
>> 16);
158 /* Stream 1 is the PDB info stream - see
159 https://llvm.org/docs/PDB/PdbStream.html. */
161 populate_info_stream (bfd
*pdb
, bfd
*info_stream
, const unsigned char *guid
)
164 struct pdb_stream_70 h
;
165 uint32_t num_entries
, num_buckets
;
166 uint32_t names_length
, stream_num
;
167 char int_buf
[sizeof (uint32_t)];
175 struct hash_entry
**buckets
= NULL
;
179 bfd_putl32 (PDB_STREAM_VERSION_VC70
, &h
.version
);
180 bfd_putl32 (time (NULL
), &h
.signature
);
181 bfd_putl32 (1, &h
.age
);
183 bfd_putl32 (bfd_getb32 (guid
), h
.guid
);
184 bfd_putl16 (bfd_getb16 (&guid
[4]), &h
.guid
[4]);
185 bfd_putl16 (bfd_getb16 (&guid
[6]), &h
.guid
[6]);
186 memcpy (&h
.guid
[8], &guid
[8], 8);
188 if (bfd_bwrite (&h
, sizeof (h
), info_stream
) != sizeof (h
))
191 /* Write hash list of named streams. This is a "rollover" hash, i.e.
192 if a bucket is filled an entry gets placed in the next free
196 for (bfd
*b
= pdb
->archive_head
; b
; b
= b
->archive_next
)
198 if (strcmp (b
->filename
, ""))
202 num_buckets
= num_entries
* 2;
209 buckets
= xmalloc (sizeof (struct hash_entry
*) * num_buckets
);
210 memset (buckets
, 0, sizeof (struct hash_entry
*) * num_buckets
);
212 for (bfd
*b
= pdb
->archive_head
; b
; b
= b
->archive_next
)
214 if (strcmp (b
->filename
, ""))
216 size_t len
= strlen (b
->filename
);
217 uint32_t hash
= (uint16_t) calc_hash (b
->filename
, len
);
218 uint32_t bucket_num
= hash
% num_buckets
;
220 while (buckets
[bucket_num
])
224 if (bucket_num
== num_buckets
)
228 buckets
[bucket_num
] = xmalloc (sizeof (struct hash_entry
));
230 buckets
[bucket_num
]->offset
= names_length
;
231 buckets
[bucket_num
]->value
= stream_num
;
233 names_length
+= len
+ 1;
240 /* Write the strings list - the hash keys are indexes into this. */
242 bfd_putl32 (names_length
, int_buf
);
244 if (bfd_bwrite (int_buf
, sizeof (uint32_t), info_stream
) !=
248 for (bfd
*b
= pdb
->archive_head
; b
; b
= b
->archive_next
)
250 if (!strcmp (b
->filename
, ""))
253 size_t len
= strlen (b
->filename
) + 1;
255 if (bfd_bwrite (b
->filename
, len
, info_stream
) != len
)
259 /* Write the number of entries and buckets. */
261 bfd_putl32 (num_entries
, int_buf
);
263 if (bfd_bwrite (int_buf
, sizeof (uint32_t), info_stream
) !=
267 bfd_putl32 (num_buckets
, int_buf
);
269 if (bfd_bwrite (int_buf
, sizeof (uint32_t), info_stream
) !=
273 /* Write the present bitmap. */
275 bfd_putl32 ((num_buckets
+ 31) / 32, int_buf
);
277 if (bfd_bwrite (int_buf
, sizeof (uint32_t), info_stream
) !=
281 for (unsigned int i
= 0; i
< num_buckets
; i
+= 32)
285 for (unsigned int j
= 0; j
< 32; j
++)
287 if (i
+ j
>= num_buckets
)
294 bfd_putl32 (v
, int_buf
);
296 if (bfd_bwrite (int_buf
, sizeof (uint32_t), info_stream
) !=
301 /* Write the (empty) deleted bitmap. */
303 bfd_putl32 (0, int_buf
);
305 if (bfd_bwrite (int_buf
, sizeof (uint32_t), info_stream
) !=
309 /* Write the buckets. */
311 for (unsigned int i
= 0; i
< num_buckets
; i
++)
315 bfd_putl32 (buckets
[i
]->offset
, int_buf
);
317 if (bfd_bwrite (int_buf
, sizeof (uint32_t), info_stream
) !=
321 bfd_putl32 (buckets
[i
]->value
, int_buf
);
323 if (bfd_bwrite (int_buf
, sizeof (uint32_t), info_stream
) !=
329 bfd_putl32 (0, int_buf
);
331 if (bfd_bwrite (int_buf
, sizeof (uint32_t), info_stream
) !=
335 bfd_putl32 (PDB_STREAM_VERSION_VC140
, int_buf
);
337 if (bfd_bwrite (int_buf
, sizeof (uint32_t), info_stream
) !=
344 for (unsigned int i
= 0; i
< num_buckets
; i
++)
355 /* Stream 2 is the type information (TPI) stream, and stream 4 is
356 the ID information (IPI) stream. They differ only in which records
357 go in which stream. */
359 create_type_stream (bfd
*pdb
)
362 struct pdb_tpi_stream_header h
;
364 stream
= add_stream (pdb
, NULL
, NULL
);
368 bfd_putl32 (TPI_STREAM_VERSION_80
, &h
.version
);
369 bfd_putl32 (sizeof (h
), &h
.header_size
);
370 bfd_putl32 (TPI_FIRST_INDEX
, &h
.type_index_begin
);
371 bfd_putl32 (TPI_FIRST_INDEX
, &h
.type_index_end
);
372 bfd_putl32 (0, &h
.type_record_bytes
);
373 bfd_putl16 (0xffff, &h
.hash_stream_index
);
374 bfd_putl16 (0xffff, &h
.hash_aux_stream_index
);
375 bfd_putl32 (4, &h
.hash_key_size
);
376 bfd_putl32 (0x3ffff, &h
.num_hash_buckets
);
377 bfd_putl32 (0, &h
.hash_value_buffer_offset
);
378 bfd_putl32 (0, &h
.hash_value_buffer_length
);
379 bfd_putl32 (0, &h
.index_offset_buffer_offset
);
380 bfd_putl32 (0, &h
.index_offset_buffer_length
);
381 bfd_putl32 (0, &h
.hash_adj_buffer_offset
);
382 bfd_putl32 (0, &h
.hash_adj_buffer_length
);
384 if (bfd_bwrite (&h
, sizeof (h
), stream
) != sizeof (h
))
390 /* Return the PE architecture number for the image. */
392 get_arch_number (bfd
*abfd
)
394 if (abfd
->arch_info
->arch
!= bfd_arch_i386
)
397 if (abfd
->arch_info
->mach
& bfd_mach_x86_64
)
398 return IMAGE_FILE_MACHINE_AMD64
;
400 return IMAGE_FILE_MACHINE_I386
;
403 /* Add a string to the strings table, if it's not already there. */
405 add_string (char *str
, size_t len
, struct string_table
*strings
)
407 uint32_t hash
= calc_hash (str
, len
);
410 slot
= htab_find_slot_with_hash (strings
->hashmap
, str
, hash
, INSERT
);
416 *slot
= xmalloc (offsetof (struct string
, s
) + len
);
418 s
= (struct string
*) *slot
;
422 s
->offset
= strings
->strings_len
;
424 memcpy (s
->s
, str
, len
);
426 if (strings
->strings_tail
)
427 strings
->strings_tail
->next
= s
;
429 strings
->strings_head
= s
;
431 strings
->strings_tail
= s
;
433 strings
->strings_len
+= len
+ 1;
437 /* Return the hash of an entry in the string table. */
439 hash_string_table_entry (const void *p
)
441 const struct string
*s
= (const struct string
*) p
;
446 /* Compare an entry in the string table with a string. */
448 eq_string_table_entry (const void *a
, const void *b
)
450 const struct string
*s1
= (const struct string
*) a
;
451 const char *s2
= (const char *) b
;
452 size_t s2_len
= strlen (s2
);
454 if (s2_len
!= s1
->len
)
457 return memcmp (s1
->s
, s2
, s2_len
) == 0;
460 /* Parse the string table within the .debug$S section. */
462 parse_string_table (bfd_byte
*data
, size_t size
,
463 struct string_table
*strings
)
467 size_t len
= strnlen ((char *) data
, size
);
469 add_string ((char *) data
, len
, strings
);
480 /* Parse the .debug$S section within an object file. */
482 handle_debugs_section (asection
*s
, bfd
*mod
, struct string_table
*strings
)
484 bfd_byte
*data
= NULL
;
487 if (!bfd_get_full_section_contents (mod
, s
, &data
))
493 if (bfd_getl32 (data
) != CV_SIGNATURE_C13
)
499 off
= sizeof (uint32_t);
501 while (off
+ sizeof (uint32_t) <= s
->size
)
505 type
= bfd_getl32 (data
+ off
);
507 off
+= sizeof (uint32_t);
509 if (off
+ sizeof (uint32_t) > s
->size
)
512 bfd_set_error (bfd_error_bad_value
);
516 size
= bfd_getl32 (data
+ off
);
518 off
+= sizeof (uint32_t);
520 if (off
+ size
> s
->size
)
523 bfd_set_error (bfd_error_bad_value
);
529 case DEBUG_S_STRINGTABLE
:
530 parse_string_table (data
+ off
, size
, strings
);
537 if (off
% sizeof (uint32_t))
538 off
+= sizeof (uint32_t) - (off
% sizeof (uint32_t));
546 /* Populate the module stream, which consists of the transformed .debug$S
547 data for each object file. */
549 populate_module_stream (bfd
*stream
, bfd
*mod
, uint32_t *sym_byte_size
,
550 struct string_table
*strings
)
552 uint8_t int_buf
[sizeof (uint32_t)];
554 *sym_byte_size
= sizeof (uint32_t);
556 /* Process .debug$S section(s). */
558 for (asection
*s
= mod
->sections
; s
; s
= s
->next
)
560 if (!strcmp (s
->name
, ".debug$S") && s
->size
>= sizeof (uint32_t))
562 if (!handle_debugs_section (s
, mod
, strings
))
567 /* Write the signature. */
569 bfd_putl32 (CV_SIGNATURE_C13
, int_buf
);
571 if (bfd_bwrite (int_buf
, sizeof (uint32_t), stream
) != sizeof (uint32_t))
574 /* Write the global refs size. */
576 bfd_putl32 (0, int_buf
);
578 if (bfd_bwrite (int_buf
, sizeof (uint32_t), stream
) != sizeof (uint32_t))
584 /* Create the module info substream within the DBI. */
586 create_module_info_substream (bfd
*abfd
, bfd
*pdb
, void **data
,
587 uint32_t *size
, struct string_table
*strings
)
591 static const char linker_fn
[] = "* Linker *";
595 for (bfd
*in
= coff_data (abfd
)->link_info
->input_bfds
; in
;
598 size_t len
= sizeof (struct module_info
);
600 if (!strcmp (bfd_get_filename (in
), "dll stuff"))
602 len
+= sizeof (linker_fn
); /* Object name. */
603 len
++; /* Empty module name. */
605 else if (in
->my_archive
)
607 char *name
= lrealpath (bfd_get_filename (in
));
609 len
+= strlen (name
) + 1; /* Object name. */
613 name
= lrealpath (bfd_get_filename (in
->my_archive
));
615 len
+= strlen (name
) + 1; /* Archive name. */
621 char *name
= lrealpath (bfd_get_filename (in
));
622 size_t name_len
= strlen (name
) + 1;
624 len
+= name_len
; /* Object name. */
625 len
+= name_len
; /* And again as the archive name. */
631 len
+= 4 - (len
% 4);
636 *data
= xmalloc (*size
);
640 for (bfd
*in
= coff_data (abfd
)->link_info
->input_bfds
; in
;
643 struct module_info
*mod
= (struct module_info
*) ptr
;
646 uint32_t sym_byte_size
;
647 uint8_t *start
= ptr
;
649 stream
= add_stream (pdb
, NULL
, &stream_num
);
657 if (!populate_module_stream (stream
, in
, &sym_byte_size
,
664 bfd_putl32 (0, &mod
->unused1
);
666 /* These are dummy values - MSVC copies the first section contribution
667 entry here, but doesn't seem to use it for anything. */
668 bfd_putl16 (0xffff, &mod
->sc
.section
);
669 bfd_putl16 (0, &mod
->sc
.padding1
);
670 bfd_putl32 (0, &mod
->sc
.offset
);
671 bfd_putl32 (0xffffffff, &mod
->sc
.size
);
672 bfd_putl32 (0, &mod
->sc
.characteristics
);
673 bfd_putl16 (0xffff, &mod
->sc
.module_index
);
674 bfd_putl16 (0, &mod
->sc
.padding2
);
675 bfd_putl32 (0, &mod
->sc
.data_crc
);
676 bfd_putl32 (0, &mod
->sc
.reloc_crc
);
678 bfd_putl16 (0, &mod
->flags
);
679 bfd_putl16 (stream_num
, &mod
->module_sym_stream
);
680 bfd_putl32 (sym_byte_size
, &mod
->sym_byte_size
);
681 bfd_putl32 (0, &mod
->c11_byte_size
);
682 bfd_putl32 (0, &mod
->c13_byte_size
);
683 bfd_putl16 (0, &mod
->source_file_count
);
684 bfd_putl16 (0, &mod
->padding
);
685 bfd_putl32 (0, &mod
->unused2
);
686 bfd_putl32 (0, &mod
->source_file_name_index
);
687 bfd_putl32 (0, &mod
->pdb_file_path_name_index
);
689 ptr
+= sizeof (struct module_info
);
691 if (!strcmp (bfd_get_filename (in
), "dll stuff"))
694 memcpy (ptr
, linker_fn
, sizeof (linker_fn
));
695 ptr
+= sizeof (linker_fn
);
697 /* Empty module name. */
701 else if (in
->my_archive
)
703 char *name
= lrealpath (bfd_get_filename (in
));
704 size_t name_len
= strlen (name
) + 1;
707 memcpy (ptr
, name
, name_len
);
712 name
= lrealpath (bfd_get_filename (in
->my_archive
));
713 name_len
= strlen (name
) + 1;
716 memcpy (ptr
, name
, name_len
);
723 char *name
= lrealpath (bfd_get_filename (in
));
724 size_t name_len
= strlen (name
) + 1;
727 memcpy (ptr
, name
, name_len
);
730 /* Object name again as archive name. */
731 memcpy (ptr
, name
, name_len
);
737 /* Pad to next four-byte boundary. */
739 if ((ptr
- start
) % 4)
741 memset (ptr
, 0, 4 - ((ptr
- start
) % 4));
742 ptr
+= 4 - ((ptr
- start
) % 4);
749 /* Return the index of a given output section. */
751 find_section_number (bfd
*abfd
, asection
*sect
)
755 for (asection
*s
= abfd
->sections
; s
; s
= s
->next
)
760 /* Empty sections aren't output. */
768 /* Create the substream which maps addresses in the image file to locations
769 in the original object files. */
771 create_section_contrib_substream (bfd
*abfd
, void **data
, uint32_t *size
)
773 unsigned int num_sc
= 0;
774 struct section_contribution
*sc
;
779 for (bfd
*in
= coff_data (abfd
)->link_info
->input_bfds
; in
;
782 for (asection
*s
= in
->sections
; s
; s
= s
->next
)
784 if (s
->size
== 0 || discarded_section (s
))
791 *size
= sizeof (uint32_t) + (num_sc
* sizeof (struct section_contribution
));
792 *data
= xmalloc (*size
);
794 bfd_putl32 (SECTION_CONTRIB_VERSION_60
, *data
);
796 /* Read characteristics of outputted sections. */
798 sect_flags
= xmalloc (sizeof (uint32_t) * abfd
->section_count
);
800 offset
= bfd_coff_filhsz (abfd
) + bfd_coff_aoutsz (abfd
);
801 offset
+= offsetof (struct external_scnhdr
, s_flags
);
803 for (unsigned int i
= 0; i
< abfd
->section_count
; i
++)
805 bfd_seek (abfd
, offset
, SEEK_SET
);
807 if (bfd_bread (sect_flags
+ (i
* sizeof (uint32_t)), sizeof (uint32_t),
808 abfd
) != sizeof (uint32_t))
815 offset
+= sizeof (struct external_scnhdr
);
819 (struct section_contribution
*) ((uint8_t *) *data
+ sizeof (uint32_t));
822 for (bfd
*in
= coff_data (abfd
)->link_info
->input_bfds
; in
;
825 for (asection
*s
= in
->sections
; s
; s
= s
->next
)
829 if (s
->size
== 0 || discarded_section (s
))
832 sect_num
= find_section_number (abfd
, s
->output_section
);
834 memcpy (&sc
->characteristics
,
835 sect_flags
+ ((sect_num
- 1) * sizeof (uint32_t)),
838 bfd_putl16 (sect_num
, &sc
->section
);
839 bfd_putl16 (0, &sc
->padding1
);
840 bfd_putl32 (s
->output_offset
, &sc
->offset
);
841 bfd_putl32 (s
->size
, &sc
->size
);
842 bfd_putl16 (mod_index
, &sc
->module_index
);
843 bfd_putl16 (0, &sc
->padding2
);
844 bfd_putl32 (0, &sc
->data_crc
);
845 bfd_putl32 (0, &sc
->reloc_crc
);
858 /* Stream 4 is the debug information (DBI) stream. */
860 populate_dbi_stream (bfd
*stream
, bfd
*abfd
, bfd
*pdb
,
861 uint16_t section_header_stream_num
,
862 uint16_t sym_rec_stream_num
,
863 uint16_t publics_stream_num
,
864 struct string_table
*strings
)
866 struct pdb_dbi_stream_header h
;
867 struct optional_dbg_header opt
;
869 uint32_t mod_info_size
, sc_size
;
871 if (!create_module_info_substream (abfd
, pdb
, &mod_info
, &mod_info_size
,
875 if (!create_section_contrib_substream (abfd
, &sc
, &sc_size
))
881 bfd_putl32 (0xffffffff, &h
.version_signature
);
882 bfd_putl32 (DBI_STREAM_VERSION_70
, &h
.version_header
);
883 bfd_putl32 (1, &h
.age
);
884 bfd_putl16 (0xffff, &h
.global_stream_index
);
885 bfd_putl16 (0x8e1d, &h
.build_number
); // MSVC 14.29
886 bfd_putl16 (publics_stream_num
, &h
.public_stream_index
);
887 bfd_putl16 (0, &h
.pdb_dll_version
);
888 bfd_putl16 (sym_rec_stream_num
, &h
.sym_record_stream
);
889 bfd_putl16 (0, &h
.pdb_dll_rbld
);
890 bfd_putl32 (mod_info_size
, &h
.mod_info_size
);
891 bfd_putl32 (sc_size
, &h
.section_contribution_size
);
892 bfd_putl32 (0, &h
.section_map_size
);
893 bfd_putl32 (0, &h
.source_info_size
);
894 bfd_putl32 (0, &h
.type_server_map_size
);
895 bfd_putl32 (0, &h
.mfc_type_server_index
);
896 bfd_putl32 (sizeof (opt
), &h
.optional_dbg_header_size
);
897 bfd_putl32 (0, &h
.ec_substream_size
);
898 bfd_putl16 (0, &h
.flags
);
899 bfd_putl16 (get_arch_number (abfd
), &h
.machine
);
900 bfd_putl32 (0, &h
.padding
);
902 if (bfd_bwrite (&h
, sizeof (h
), stream
) != sizeof (h
))
909 if (bfd_bwrite (mod_info
, mod_info_size
, stream
) != mod_info_size
)
918 if (bfd_bwrite (sc
, sc_size
, stream
) != sc_size
)
926 bfd_putl16 (0xffff, &opt
.fpo_stream
);
927 bfd_putl16 (0xffff, &opt
.exception_stream
);
928 bfd_putl16 (0xffff, &opt
.fixup_stream
);
929 bfd_putl16 (0xffff, &opt
.omap_to_src_stream
);
930 bfd_putl16 (0xffff, &opt
.omap_from_src_stream
);
931 bfd_putl16 (section_header_stream_num
, &opt
.section_header_stream
);
932 bfd_putl16 (0xffff, &opt
.token_map_stream
);
933 bfd_putl16 (0xffff, &opt
.xdata_stream
);
934 bfd_putl16 (0xffff, &opt
.pdata_stream
);
935 bfd_putl16 (0xffff, &opt
.new_fpo_stream
);
936 bfd_putl16 (0xffff, &opt
.orig_section_header_stream
);
938 if (bfd_bwrite (&opt
, sizeof (opt
), stream
) != sizeof (opt
))
944 /* Used as parameter to qsort, to sort publics by hash. */
946 public_compare_hash (const void *s1
, const void *s2
)
948 const struct public *p1
= *(const struct public **) s1
;
949 const struct public *p2
= *(const struct public **) s2
;
951 if (p1
->hash
< p2
->hash
)
953 if (p1
->hash
> p2
->hash
)
959 /* Used as parameter to qsort, to sort publics by address. */
961 public_compare_addr (const void *s1
, const void *s2
)
963 const struct public *p1
= *(const struct public **) s1
;
964 const struct public *p2
= *(const struct public **) s2
;
966 if (p1
->section
< p2
->section
)
968 if (p1
->section
> p2
->section
)
971 if (p1
->address
< p2
->address
)
973 if (p1
->address
> p2
->address
)
979 /* The publics stream is a hash map of S_PUB32 records, which are stored
980 in the symbol record stream. Each S_PUB32 entry represents a symbol
981 from the point of view of the linker: a section index, an offset within
982 the section, and a mangled name. Compare with S_GDATA32 and S_GPROC32,
983 which are the same thing but generated by the compiler. */
985 populate_publics_stream (bfd
*stream
, bfd
*abfd
, bfd
*sym_rec_stream
)
987 struct publics_header header
;
988 struct globals_hash_header hash_header
;
989 const unsigned int num_buckets
= 4096;
990 unsigned int num_entries
= 0, filled_buckets
= 0;
991 unsigned int buckets_size
, sym_hash_size
;
992 char int_buf
[sizeof (uint32_t)];
993 struct public *publics_head
= NULL
, *publics_tail
= NULL
;
994 struct public **buckets
;
995 struct public **sorted
= NULL
;
998 buckets
= xmalloc (sizeof (struct public *) * num_buckets
);
999 memset (buckets
, 0, sizeof (struct public *) * num_buckets
);
1001 /* Loop through the global symbols in our input files, and write S_PUB32
1002 records in the symbol record stream for those that make it into the
1004 for (bfd
*in
= coff_data (abfd
)->link_info
->input_bfds
; in
;
1007 for (unsigned int i
= 0; i
< in
->symcount
; i
++)
1009 struct bfd_symbol
*sym
= in
->outsymbols
[i
];
1011 if (sym
->flags
& BSF_GLOBAL
)
1014 uint16_t record_length
;
1015 const char *name
= sym
->name
;
1016 size_t name_len
= strlen (name
);
1017 struct public *p
= xmalloc (sizeof (struct public));
1018 unsigned int padding
= 0;
1023 find_section_number (abfd
, sym
->section
->output_section
);
1029 p
->offset
= bfd_tell (sym_rec_stream
);
1030 p
->hash
= calc_hash (name
, name_len
) % num_buckets
;
1031 p
->section
= section
;
1032 p
->address
= sym
->section
->output_offset
+ sym
->value
;
1034 record_length
= sizeof (struct pubsym
) + name_len
+ 1;
1036 if (record_length
% 4)
1037 padding
= 4 - (record_length
% 4);
1039 /* Assume that all global symbols in executable sections
1041 if (sym
->section
->flags
& SEC_CODE
)
1042 flags
= PUBSYM_FUNCTION
;
1044 bfd_putl16 (record_length
+ padding
- sizeof (uint16_t),
1046 bfd_putl16 (S_PUB32
, &ps
.record_type
);
1047 bfd_putl32 (flags
, &ps
.flags
);
1048 bfd_putl32 (p
->address
, &ps
.offset
);
1049 bfd_putl16 (p
->section
, &ps
.section
);
1051 if (bfd_bwrite (&ps
, sizeof (struct pubsym
), sym_rec_stream
) !=
1052 sizeof (struct pubsym
))
1055 if (bfd_bwrite (name
, name_len
+ 1, sym_rec_stream
) !=
1059 for (unsigned int j
= 0; j
< padding
; j
++)
1063 if (bfd_bwrite (&b
, sizeof (uint8_t), sym_rec_stream
) !=
1071 publics_tail
->next
= p
;
1080 if (num_entries
> 0)
1082 /* Create an array of pointers, sorted by hash value. */
1084 sorted
= xmalloc (sizeof (struct public *) * num_entries
);
1086 struct public *p
= publics_head
;
1087 for (unsigned int i
= 0; i
< num_entries
; i
++)
1093 qsort (sorted
, num_entries
, sizeof (struct public *),
1094 public_compare_hash
);
1096 /* Populate the buckets. */
1098 for (unsigned int i
= 0; i
< num_entries
; i
++)
1100 if (!buckets
[sorted
[i
]->hash
])
1102 buckets
[sorted
[i
]->hash
] = sorted
[i
];
1106 sorted
[i
]->index
= i
;
1110 buckets_size
= num_buckets
/ 8;
1111 buckets_size
+= sizeof (uint32_t);
1112 buckets_size
+= filled_buckets
* sizeof (uint32_t);
1114 sym_hash_size
= sizeof (hash_header
);
1115 sym_hash_size
+= num_entries
* sizeof (struct hash_record
);
1116 sym_hash_size
+= buckets_size
;
1118 /* Output the publics header. */
1120 bfd_putl32 (sym_hash_size
, &header
.sym_hash_size
);
1121 bfd_putl32 (num_entries
* sizeof (uint32_t), &header
.addr_map_size
);
1122 bfd_putl32 (0, &header
.num_thunks
);
1123 bfd_putl32 (0, &header
.thunks_size
);
1124 bfd_putl32 (0, &header
.thunk_table
);
1125 bfd_putl32 (0, &header
.thunk_table_offset
);
1126 bfd_putl32 (0, &header
.num_sects
);
1128 if (bfd_bwrite (&header
, sizeof (header
), stream
) != sizeof (header
))
1131 /* Output the global hash header. */
1133 bfd_putl32 (GLOBALS_HASH_SIGNATURE
, &hash_header
.signature
);
1134 bfd_putl32 (GLOBALS_HASH_VERSION_70
, &hash_header
.version
);
1135 bfd_putl32 (num_entries
* sizeof (struct hash_record
),
1136 &hash_header
.entries_size
);
1137 bfd_putl32 (buckets_size
, &hash_header
.buckets_size
);
1139 if (bfd_bwrite (&hash_header
, sizeof (hash_header
), stream
) !=
1140 sizeof (hash_header
))
1143 /* Write the entries in hash order. */
1145 for (unsigned int i
= 0; i
< num_entries
; i
++)
1147 struct hash_record hr
;
1149 bfd_putl32 (sorted
[i
]->offset
+ 1, &hr
.offset
);
1150 bfd_putl32 (1, &hr
.reference
);
1152 if (bfd_bwrite (&hr
, sizeof (hr
), stream
) != sizeof (hr
))
1156 /* Write the bitmap for filled and unfilled buckets. */
1158 for (unsigned int i
= 0; i
< num_buckets
; i
+= 8)
1162 for (unsigned int j
= 0; j
< 8; j
++)
1168 if (bfd_bwrite (&v
, sizeof (v
), stream
) != sizeof (v
))
1172 /* Add a 4-byte gap. */
1174 bfd_putl32 (0, int_buf
);
1176 if (bfd_bwrite (int_buf
, sizeof (uint32_t), stream
) != sizeof (uint32_t))
1179 /* Write the bucket offsets. */
1181 for (unsigned int i
= 0; i
< num_buckets
; i
++)
1185 /* 0xc is size of internal hash_record structure in
1186 Microsoft's parser. */
1187 bfd_putl32 (buckets
[i
]->index
* 0xc, int_buf
);
1189 if (bfd_bwrite (int_buf
, sizeof (uint32_t), stream
) !=
1195 /* Write the address map: offsets into the symbol record stream of
1196 S_PUB32 records, ordered by address. */
1198 if (num_entries
> 0)
1200 qsort (sorted
, num_entries
, sizeof (struct public *),
1201 public_compare_addr
);
1203 for (unsigned int i
= 0; i
< num_entries
; i
++)
1205 bfd_putl32 (sorted
[i
]->offset
, int_buf
);
1207 if (bfd_bwrite (int_buf
, sizeof (uint32_t), stream
) !=
1218 while (publics_head
)
1220 struct public *p
= publics_head
->next
;
1222 free (publics_head
);
1231 /* The section header stream contains a copy of the section headers
1232 from the PE file, in the same format. */
1234 create_section_header_stream (bfd
*pdb
, bfd
*abfd
, uint16_t *num
)
1237 unsigned int section_count
;
1242 stream
= add_stream (pdb
, NULL
, num
);
1246 section_count
= abfd
->section_count
;
1248 /* Empty sections aren't output. */
1249 for (asection
*sect
= abfd
->sections
; sect
; sect
= sect
->next
)
1251 if (sect
->size
== 0)
1255 if (section_count
== 0)
1258 /* Copy section table from output - it's already been written at this
1261 scn_base
= bfd_coff_filhsz (abfd
) + bfd_coff_aoutsz (abfd
);
1263 bfd_seek (abfd
, scn_base
, SEEK_SET
);
1265 len
= section_count
* sizeof (struct external_scnhdr
);
1266 buf
= xmalloc (len
);
1268 if (bfd_bread (buf
, len
, abfd
) != len
)
1274 if (bfd_bwrite (buf
, len
, stream
) != len
)
1285 /* Populate the "/names" named stream, which contains the string table. */
1287 populate_names_stream (bfd
*stream
, struct string_table
*strings
)
1289 char int_buf
[sizeof (uint32_t)];
1290 struct string_table_header h
;
1291 uint32_t num_strings
= 0, num_buckets
;
1292 struct string
**buckets
;
1294 bfd_putl32 (STRING_TABLE_SIGNATURE
, &h
.signature
);
1295 bfd_putl32 (STRING_TABLE_VERSION
, &h
.version
);
1297 if (bfd_bwrite (&h
, sizeof (h
), stream
) != sizeof (h
))
1300 bfd_putl32 (strings
->strings_len
, int_buf
);
1302 if (bfd_bwrite (int_buf
, sizeof (uint32_t), stream
) != sizeof (uint32_t))
1307 if (bfd_bwrite (int_buf
, 1, stream
) != 1)
1310 for (struct string
*s
= strings
->strings_head
; s
; s
= s
->next
)
1312 if (bfd_bwrite (s
->s
, s
->len
, stream
) != s
->len
)
1315 if (bfd_bwrite (int_buf
, 1, stream
) != 1)
1321 num_buckets
= num_strings
* 2;
1323 buckets
= xmalloc (sizeof (struct string
*) * num_buckets
);
1324 memset (buckets
, 0, sizeof (struct string
*) * num_buckets
);
1326 for (struct string
*s
= strings
->strings_head
; s
; s
= s
->next
)
1328 uint32_t bucket_num
= s
->hash
% num_buckets
;
1330 while (buckets
[bucket_num
])
1334 if (bucket_num
== num_buckets
)
1338 buckets
[bucket_num
] = s
;
1341 bfd_putl32 (num_buckets
, int_buf
);
1343 if (bfd_bwrite (int_buf
, sizeof (uint32_t), stream
) != sizeof (uint32_t))
1349 for (unsigned int i
= 0; i
< num_buckets
; i
++)
1352 bfd_putl32 (buckets
[i
]->offset
, int_buf
);
1354 bfd_putl32 (0, int_buf
);
1356 if (bfd_bwrite (int_buf
, sizeof (uint32_t), stream
) !=
1366 bfd_putl32 (num_strings
, int_buf
);
1368 if (bfd_bwrite (int_buf
, sizeof (uint32_t), stream
) != sizeof (uint32_t))
1374 /* Create a PDB debugging file for the PE image file abfd with the build ID
1375 guid, stored at pdb_name. */
1377 create_pdb_file (bfd
*abfd
, const char *pdb_name
, const unsigned char *guid
)
1381 bfd
*info_stream
, *dbi_stream
, *names_stream
, *sym_rec_stream
,
1383 uint16_t section_header_stream_num
, sym_rec_stream_num
, publics_stream_num
;
1384 struct string_table strings
;
1386 pdb
= bfd_openw (pdb_name
, "pdb");
1389 einfo (_("%P: warning: cannot create PDB file: %E\n"));
1393 strings
.strings_head
= NULL
;
1394 strings
.strings_tail
= NULL
;
1395 strings
.strings_len
= 1;
1396 strings
.hashmap
= htab_create_alloc (0, hash_string_table_entry
,
1397 eq_string_table_entry
, free
,
1400 bfd_set_format (pdb
, bfd_archive
);
1402 if (!create_old_directory_stream (pdb
))
1404 einfo (_("%P: warning: cannot create old directory stream "
1405 "in PDB file: %E\n"));
1409 info_stream
= add_stream (pdb
, NULL
, NULL
);
1413 einfo (_("%P: warning: cannot create info stream "
1414 "in PDB file: %E\n"));
1418 if (!create_type_stream (pdb
))
1420 einfo (_("%P: warning: cannot create TPI stream "
1421 "in PDB file: %E\n"));
1425 dbi_stream
= add_stream (pdb
, NULL
, NULL
);
1429 einfo (_("%P: warning: cannot create DBI stream "
1430 "in PDB file: %E\n"));
1434 if (!create_type_stream (pdb
))
1436 einfo (_("%P: warning: cannot create IPI stream "
1437 "in PDB file: %E\n"));
1441 names_stream
= add_stream (pdb
, "/names", NULL
);
1445 einfo (_("%P: warning: cannot create /names stream "
1446 "in PDB file: %E\n"));
1450 sym_rec_stream
= add_stream (pdb
, NULL
, &sym_rec_stream_num
);
1452 if (!sym_rec_stream
)
1454 einfo (_("%P: warning: cannot create symbol record stream "
1455 "in PDB file: %E\n"));
1459 publics_stream
= add_stream (pdb
, NULL
, &publics_stream_num
);
1461 if (!publics_stream
)
1463 einfo (_("%P: warning: cannot create publics stream "
1464 "in PDB file: %E\n"));
1468 if (!create_section_header_stream (pdb
, abfd
, §ion_header_stream_num
))
1470 einfo (_("%P: warning: cannot create section header stream "
1471 "in PDB file: %E\n"));
1475 if (!populate_dbi_stream (dbi_stream
, abfd
, pdb
, section_header_stream_num
,
1476 sym_rec_stream_num
, publics_stream_num
,
1479 einfo (_("%P: warning: cannot populate DBI stream "
1480 "in PDB file: %E\n"));
1484 add_string ("", 0, &strings
);
1486 if (!populate_names_stream (names_stream
, &strings
))
1488 einfo (_("%P: warning: cannot populate names stream "
1489 "in PDB file: %E\n"));
1493 if (!populate_publics_stream (publics_stream
, abfd
, sym_rec_stream
))
1495 einfo (_("%P: warning: cannot populate publics stream "
1496 "in PDB file: %E\n"));
1500 if (!populate_info_stream (pdb
, info_stream
, guid
))
1502 einfo (_("%P: warning: cannot populate info stream "
1503 "in PDB file: %E\n"));
1512 htab_delete (strings
.hashmap
);