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"
44 /* Add a new stream to the PDB archive, and return its BFD. */
46 add_stream (bfd
*pdb
, const char *name
, uint16_t *stream_num
)
51 stream
= bfd_create (name
? name
: "", pdb
);
55 if (!bfd_make_writable (stream
))
61 if (!pdb
->archive_head
)
63 bfd_set_archive_head (pdb
, stream
);
68 bfd
*b
= pdb
->archive_head
;
72 while (b
->archive_next
)
78 b
->archive_next
= stream
;
87 /* Stream 0 ought to be a copy of the MSF directory from the last
88 time the PDB file was written. Because we don't do incremental
89 writes this isn't applicable to us, but we fill it with a dummy
90 value so as not to confuse radare. */
92 create_old_directory_stream (bfd
*pdb
)
95 char buf
[sizeof (uint32_t)];
97 stream
= add_stream (pdb
, NULL
, NULL
);
103 return bfd_bwrite (buf
, sizeof (uint32_t), stream
) == sizeof (uint32_t);
106 /* Calculate the hash of a given string. */
108 calc_hash (const char *data
, size_t len
)
115 hash
^= data
[1] << 8;
116 hash
^= data
[2] << 16;
117 hash
^= data
[3] << 24;
126 hash
^= data
[1] << 8;
136 hash
^= (hash
>> 11);
138 return hash
^ (hash
>> 16);
141 /* Stream 1 is the PDB info stream - see
142 https://llvm.org/docs/PDB/PdbStream.html. */
144 populate_info_stream (bfd
*pdb
, bfd
*info_stream
, const unsigned char *guid
)
147 struct pdb_stream_70 h
;
148 uint32_t num_entries
, num_buckets
;
149 uint32_t names_length
, stream_num
;
150 char int_buf
[sizeof (uint32_t)];
158 struct hash_entry
**buckets
= NULL
;
162 bfd_putl32 (PDB_STREAM_VERSION_VC70
, &h
.version
);
163 bfd_putl32 (time (NULL
), &h
.signature
);
164 bfd_putl32 (1, &h
.age
);
166 bfd_putl32 (bfd_getb32 (guid
), h
.guid
);
167 bfd_putl16 (bfd_getb16 (&guid
[4]), &h
.guid
[4]);
168 bfd_putl16 (bfd_getb16 (&guid
[6]), &h
.guid
[6]);
169 memcpy (&h
.guid
[8], &guid
[8], 8);
171 if (bfd_bwrite (&h
, sizeof (h
), info_stream
) != sizeof (h
))
174 /* Write hash list of named streams. This is a "rollover" hash, i.e.
175 if a bucket is filled an entry gets placed in the next free
179 for (bfd
*b
= pdb
->archive_head
; b
; b
= b
->archive_next
)
181 if (strcmp (b
->filename
, ""))
185 num_buckets
= num_entries
* 2;
192 buckets
= xmalloc (sizeof (struct hash_entry
*) * num_buckets
);
193 memset (buckets
, 0, sizeof (struct hash_entry
*) * num_buckets
);
195 for (bfd
*b
= pdb
->archive_head
; b
; b
= b
->archive_next
)
197 if (strcmp (b
->filename
, ""))
199 size_t len
= strlen (b
->filename
);
200 uint32_t hash
= (uint16_t) calc_hash (b
->filename
, len
);
201 uint32_t bucket_num
= hash
% num_buckets
;
203 while (buckets
[bucket_num
])
207 if (bucket_num
== num_buckets
)
211 buckets
[bucket_num
] = xmalloc (sizeof (struct hash_entry
));
213 buckets
[bucket_num
]->offset
= names_length
;
214 buckets
[bucket_num
]->value
= stream_num
;
216 names_length
+= len
+ 1;
223 /* Write the strings list - the hash keys are indexes into this. */
225 bfd_putl32 (names_length
, int_buf
);
227 if (bfd_bwrite (int_buf
, sizeof (uint32_t), info_stream
) !=
231 for (bfd
*b
= pdb
->archive_head
; b
; b
= b
->archive_next
)
233 if (!strcmp (b
->filename
, ""))
236 size_t len
= strlen (b
->filename
) + 1;
238 if (bfd_bwrite (b
->filename
, len
, info_stream
) != len
)
242 /* Write the number of entries and buckets. */
244 bfd_putl32 (num_entries
, int_buf
);
246 if (bfd_bwrite (int_buf
, sizeof (uint32_t), info_stream
) !=
250 bfd_putl32 (num_buckets
, int_buf
);
252 if (bfd_bwrite (int_buf
, sizeof (uint32_t), info_stream
) !=
256 /* Write the present bitmap. */
258 bfd_putl32 ((num_buckets
+ 31) / 32, int_buf
);
260 if (bfd_bwrite (int_buf
, sizeof (uint32_t), info_stream
) !=
264 for (unsigned int i
= 0; i
< num_buckets
; i
+= 32)
268 for (unsigned int j
= 0; j
< 32; j
++)
270 if (i
+ j
>= num_buckets
)
277 bfd_putl32 (v
, int_buf
);
279 if (bfd_bwrite (int_buf
, sizeof (uint32_t), info_stream
) !=
284 /* Write the (empty) deleted bitmap. */
286 bfd_putl32 (0, int_buf
);
288 if (bfd_bwrite (int_buf
, sizeof (uint32_t), info_stream
) !=
292 /* Write the buckets. */
294 for (unsigned int i
= 0; i
< num_buckets
; i
++)
298 bfd_putl32 (buckets
[i
]->offset
, int_buf
);
300 if (bfd_bwrite (int_buf
, sizeof (uint32_t), info_stream
) !=
304 bfd_putl32 (buckets
[i
]->value
, int_buf
);
306 if (bfd_bwrite (int_buf
, sizeof (uint32_t), info_stream
) !=
312 bfd_putl32 (0, int_buf
);
314 if (bfd_bwrite (int_buf
, sizeof (uint32_t), info_stream
) !=
318 bfd_putl32 (PDB_STREAM_VERSION_VC140
, int_buf
);
320 if (bfd_bwrite (int_buf
, sizeof (uint32_t), info_stream
) !=
327 for (unsigned int i
= 0; i
< num_buckets
; i
++)
338 /* Stream 2 is the type information (TPI) stream, and stream 4 is
339 the ID information (IPI) stream. They differ only in which records
340 go in which stream. */
342 create_type_stream (bfd
*pdb
)
345 struct pdb_tpi_stream_header h
;
347 stream
= add_stream (pdb
, NULL
, NULL
);
351 bfd_putl32 (TPI_STREAM_VERSION_80
, &h
.version
);
352 bfd_putl32 (sizeof (h
), &h
.header_size
);
353 bfd_putl32 (TPI_FIRST_INDEX
, &h
.type_index_begin
);
354 bfd_putl32 (TPI_FIRST_INDEX
, &h
.type_index_end
);
355 bfd_putl32 (0, &h
.type_record_bytes
);
356 bfd_putl16 (0xffff, &h
.hash_stream_index
);
357 bfd_putl16 (0xffff, &h
.hash_aux_stream_index
);
358 bfd_putl32 (4, &h
.hash_key_size
);
359 bfd_putl32 (0x3ffff, &h
.num_hash_buckets
);
360 bfd_putl32 (0, &h
.hash_value_buffer_offset
);
361 bfd_putl32 (0, &h
.hash_value_buffer_length
);
362 bfd_putl32 (0, &h
.index_offset_buffer_offset
);
363 bfd_putl32 (0, &h
.index_offset_buffer_length
);
364 bfd_putl32 (0, &h
.hash_adj_buffer_offset
);
365 bfd_putl32 (0, &h
.hash_adj_buffer_length
);
367 if (bfd_bwrite (&h
, sizeof (h
), stream
) != sizeof (h
))
373 /* Return the PE architecture number for the image. */
375 get_arch_number (bfd
*abfd
)
377 if (abfd
->arch_info
->arch
!= bfd_arch_i386
)
380 if (abfd
->arch_info
->mach
& bfd_mach_x86_64
)
381 return IMAGE_FILE_MACHINE_AMD64
;
383 return IMAGE_FILE_MACHINE_I386
;
386 /* Populate the module stream, which consists of the transformed .debug$S
387 data for each object file. */
389 populate_module_stream (bfd
*stream
, uint32_t *sym_byte_size
)
391 uint8_t int_buf
[sizeof (uint32_t)];
393 *sym_byte_size
= sizeof (uint32_t);
395 /* Write the signature. */
397 bfd_putl32 (CV_SIGNATURE_C13
, int_buf
);
399 if (bfd_bwrite (int_buf
, sizeof (uint32_t), stream
) != sizeof (uint32_t))
402 /* Write the global refs size. */
404 bfd_putl32 (0, int_buf
);
406 if (bfd_bwrite (int_buf
, sizeof (uint32_t), stream
) != sizeof (uint32_t))
412 /* Create the module info substream within the DBI. */
414 create_module_info_substream (bfd
*abfd
, bfd
*pdb
, void **data
,
419 static const char linker_fn
[] = "* Linker *";
423 for (bfd
*in
= coff_data (abfd
)->link_info
->input_bfds
; in
;
426 size_t len
= sizeof (struct module_info
);
428 if (!strcmp (bfd_get_filename (in
), "dll stuff"))
430 len
+= sizeof (linker_fn
); /* Object name. */
431 len
++; /* Empty module name. */
433 else if (in
->my_archive
)
435 char *name
= lrealpath (bfd_get_filename (in
));
437 len
+= strlen (name
) + 1; /* Object name. */
441 name
= lrealpath (bfd_get_filename (in
->my_archive
));
443 len
+= strlen (name
) + 1; /* Archive name. */
449 char *name
= lrealpath (bfd_get_filename (in
));
450 size_t name_len
= strlen (name
) + 1;
452 len
+= name_len
; /* Object name. */
453 len
+= name_len
; /* And again as the archive name. */
459 len
+= 4 - (len
% 4);
464 *data
= xmalloc (*size
);
468 for (bfd
*in
= coff_data (abfd
)->link_info
->input_bfds
; in
;
471 struct module_info
*mod
= (struct module_info
*) ptr
;
474 uint32_t sym_byte_size
;
475 uint8_t *start
= ptr
;
477 stream
= add_stream (pdb
, NULL
, &stream_num
);
485 if (!populate_module_stream (stream
, &sym_byte_size
))
491 bfd_putl32 (0, &mod
->unused1
);
493 /* These are dummy values - MSVC copies the first section contribution
494 entry here, but doesn't seem to use it for anything. */
495 bfd_putl16 (0xffff, &mod
->sc
.section
);
496 bfd_putl16 (0, &mod
->sc
.padding1
);
497 bfd_putl32 (0, &mod
->sc
.offset
);
498 bfd_putl32 (0xffffffff, &mod
->sc
.size
);
499 bfd_putl32 (0, &mod
->sc
.characteristics
);
500 bfd_putl16 (0xffff, &mod
->sc
.module_index
);
501 bfd_putl16 (0, &mod
->sc
.padding2
);
502 bfd_putl32 (0, &mod
->sc
.data_crc
);
503 bfd_putl32 (0, &mod
->sc
.reloc_crc
);
505 bfd_putl16 (0, &mod
->flags
);
506 bfd_putl16 (stream_num
, &mod
->module_sym_stream
);
507 bfd_putl32 (sym_byte_size
, &mod
->sym_byte_size
);
508 bfd_putl32 (0, &mod
->c11_byte_size
);
509 bfd_putl32 (0, &mod
->c13_byte_size
);
510 bfd_putl16 (0, &mod
->source_file_count
);
511 bfd_putl16 (0, &mod
->padding
);
512 bfd_putl32 (0, &mod
->unused2
);
513 bfd_putl32 (0, &mod
->source_file_name_index
);
514 bfd_putl32 (0, &mod
->pdb_file_path_name_index
);
516 ptr
+= sizeof (struct module_info
);
518 if (!strcmp (bfd_get_filename (in
), "dll stuff"))
521 memcpy (ptr
, linker_fn
, sizeof (linker_fn
));
522 ptr
+= sizeof (linker_fn
);
524 /* Empty module name. */
528 else if (in
->my_archive
)
530 char *name
= lrealpath (bfd_get_filename (in
));
531 size_t name_len
= strlen (name
) + 1;
534 memcpy (ptr
, name
, name_len
);
539 name
= lrealpath (bfd_get_filename (in
->my_archive
));
540 name_len
= strlen (name
) + 1;
543 memcpy (ptr
, name
, name_len
);
550 char *name
= lrealpath (bfd_get_filename (in
));
551 size_t name_len
= strlen (name
) + 1;
554 memcpy (ptr
, name
, name_len
);
557 /* Object name again as archive name. */
558 memcpy (ptr
, name
, name_len
);
564 /* Pad to next four-byte boundary. */
566 if ((ptr
- start
) % 4)
568 memset (ptr
, 0, 4 - ((ptr
- start
) % 4));
569 ptr
+= 4 - ((ptr
- start
) % 4);
576 /* Return the index of a given output section. */
578 find_section_number (bfd
*abfd
, asection
*sect
)
582 for (asection
*s
= abfd
->sections
; s
; s
= s
->next
)
587 /* Empty sections aren't output. */
595 /* Stream 4 is the debug information (DBI) stream. */
597 populate_dbi_stream (bfd
*stream
, bfd
*abfd
, bfd
*pdb
,
598 uint16_t section_header_stream_num
,
599 uint16_t sym_rec_stream_num
,
600 uint16_t publics_stream_num
)
602 struct pdb_dbi_stream_header h
;
603 struct optional_dbg_header opt
;
605 uint32_t mod_info_size
;
607 if (!create_module_info_substream (abfd
, pdb
, &mod_info
, &mod_info_size
))
610 bfd_putl32 (0xffffffff, &h
.version_signature
);
611 bfd_putl32 (DBI_STREAM_VERSION_70
, &h
.version_header
);
612 bfd_putl32 (1, &h
.age
);
613 bfd_putl16 (0xffff, &h
.global_stream_index
);
614 bfd_putl16 (0x8e1d, &h
.build_number
); // MSVC 14.29
615 bfd_putl16 (publics_stream_num
, &h
.public_stream_index
);
616 bfd_putl16 (0, &h
.pdb_dll_version
);
617 bfd_putl16 (sym_rec_stream_num
, &h
.sym_record_stream
);
618 bfd_putl16 (0, &h
.pdb_dll_rbld
);
619 bfd_putl32 (mod_info_size
, &h
.mod_info_size
);
620 bfd_putl32 (0, &h
.section_contribution_size
);
621 bfd_putl32 (0, &h
.section_map_size
);
622 bfd_putl32 (0, &h
.source_info_size
);
623 bfd_putl32 (0, &h
.type_server_map_size
);
624 bfd_putl32 (0, &h
.mfc_type_server_index
);
625 bfd_putl32 (sizeof (opt
), &h
.optional_dbg_header_size
);
626 bfd_putl32 (0, &h
.ec_substream_size
);
627 bfd_putl16 (0, &h
.flags
);
628 bfd_putl16 (get_arch_number (abfd
), &h
.machine
);
629 bfd_putl32 (0, &h
.padding
);
631 if (bfd_bwrite (&h
, sizeof (h
), stream
) != sizeof (h
))
637 if (bfd_bwrite (mod_info
, mod_info_size
, stream
) != mod_info_size
)
645 bfd_putl16 (0xffff, &opt
.fpo_stream
);
646 bfd_putl16 (0xffff, &opt
.exception_stream
);
647 bfd_putl16 (0xffff, &opt
.fixup_stream
);
648 bfd_putl16 (0xffff, &opt
.omap_to_src_stream
);
649 bfd_putl16 (0xffff, &opt
.omap_from_src_stream
);
650 bfd_putl16 (section_header_stream_num
, &opt
.section_header_stream
);
651 bfd_putl16 (0xffff, &opt
.token_map_stream
);
652 bfd_putl16 (0xffff, &opt
.xdata_stream
);
653 bfd_putl16 (0xffff, &opt
.pdata_stream
);
654 bfd_putl16 (0xffff, &opt
.new_fpo_stream
);
655 bfd_putl16 (0xffff, &opt
.orig_section_header_stream
);
657 if (bfd_bwrite (&opt
, sizeof (opt
), stream
) != sizeof (opt
))
663 /* Used as parameter to qsort, to sort publics by hash. */
665 public_compare_hash (const void *s1
, const void *s2
)
667 const struct public *p1
= *(const struct public **) s1
;
668 const struct public *p2
= *(const struct public **) s2
;
670 if (p1
->hash
< p2
->hash
)
672 if (p1
->hash
> p2
->hash
)
678 /* Used as parameter to qsort, to sort publics by address. */
680 public_compare_addr (const void *s1
, const void *s2
)
682 const struct public *p1
= *(const struct public **) s1
;
683 const struct public *p2
= *(const struct public **) s2
;
685 if (p1
->section
< p2
->section
)
687 if (p1
->section
> p2
->section
)
690 if (p1
->address
< p2
->address
)
692 if (p1
->address
> p2
->address
)
698 /* The publics stream is a hash map of S_PUB32 records, which are stored
699 in the symbol record stream. Each S_PUB32 entry represents a symbol
700 from the point of view of the linker: a section index, an offset within
701 the section, and a mangled name. Compare with S_GDATA32 and S_GPROC32,
702 which are the same thing but generated by the compiler. */
704 populate_publics_stream (bfd
*stream
, bfd
*abfd
, bfd
*sym_rec_stream
)
706 struct publics_header header
;
707 struct globals_hash_header hash_header
;
708 const unsigned int num_buckets
= 4096;
709 unsigned int num_entries
= 0, filled_buckets
= 0;
710 unsigned int buckets_size
, sym_hash_size
;
711 char int_buf
[sizeof (uint32_t)];
712 struct public *publics_head
= NULL
, *publics_tail
= NULL
;
713 struct public **buckets
;
714 struct public **sorted
= NULL
;
717 buckets
= xmalloc (sizeof (struct public *) * num_buckets
);
718 memset (buckets
, 0, sizeof (struct public *) * num_buckets
);
720 /* Loop through the global symbols in our input files, and write S_PUB32
721 records in the symbol record stream for those that make it into the
723 for (bfd
*in
= coff_data (abfd
)->link_info
->input_bfds
; in
;
726 for (unsigned int i
= 0; i
< in
->symcount
; i
++)
728 struct bfd_symbol
*sym
= in
->outsymbols
[i
];
730 if (sym
->flags
& BSF_GLOBAL
)
733 uint16_t record_length
;
734 const char *name
= sym
->name
;
735 size_t name_len
= strlen (name
);
736 struct public *p
= xmalloc (sizeof (struct public));
737 unsigned int padding
= 0;
742 find_section_number (abfd
, sym
->section
->output_section
);
748 p
->offset
= bfd_tell (sym_rec_stream
);
749 p
->hash
= calc_hash (name
, name_len
) % num_buckets
;
750 p
->section
= section
;
751 p
->address
= sym
->section
->output_offset
+ sym
->value
;
753 record_length
= sizeof (struct pubsym
) + name_len
+ 1;
755 if (record_length
% 4)
756 padding
= 4 - (record_length
% 4);
758 /* Assume that all global symbols in executable sections
760 if (sym
->section
->flags
& SEC_CODE
)
761 flags
= PUBSYM_FUNCTION
;
763 bfd_putl16 (record_length
+ padding
- sizeof (uint16_t),
765 bfd_putl16 (S_PUB32
, &ps
.record_type
);
766 bfd_putl32 (flags
, &ps
.flags
);
767 bfd_putl32 (p
->address
, &ps
.offset
);
768 bfd_putl16 (p
->section
, &ps
.section
);
770 if (bfd_bwrite (&ps
, sizeof (struct pubsym
), sym_rec_stream
) !=
771 sizeof (struct pubsym
))
774 if (bfd_bwrite (name
, name_len
+ 1, sym_rec_stream
) !=
778 for (unsigned int j
= 0; j
< padding
; j
++)
782 if (bfd_bwrite (&b
, sizeof (uint8_t), sym_rec_stream
) !=
790 publics_tail
->next
= p
;
801 /* Create an array of pointers, sorted by hash value. */
803 sorted
= xmalloc (sizeof (struct public *) * num_entries
);
805 struct public *p
= publics_head
;
806 for (unsigned int i
= 0; i
< num_entries
; i
++)
812 qsort (sorted
, num_entries
, sizeof (struct public *),
813 public_compare_hash
);
815 /* Populate the buckets. */
817 for (unsigned int i
= 0; i
< num_entries
; i
++)
819 if (!buckets
[sorted
[i
]->hash
])
821 buckets
[sorted
[i
]->hash
] = sorted
[i
];
825 sorted
[i
]->index
= i
;
829 buckets_size
= num_buckets
/ 8;
830 buckets_size
+= sizeof (uint32_t);
831 buckets_size
+= filled_buckets
* sizeof (uint32_t);
833 sym_hash_size
= sizeof (hash_header
);
834 sym_hash_size
+= num_entries
* sizeof (struct hash_record
);
835 sym_hash_size
+= buckets_size
;
837 /* Output the publics header. */
839 bfd_putl32 (sym_hash_size
, &header
.sym_hash_size
);
840 bfd_putl32 (num_entries
* sizeof (uint32_t), &header
.addr_map_size
);
841 bfd_putl32 (0, &header
.num_thunks
);
842 bfd_putl32 (0, &header
.thunks_size
);
843 bfd_putl32 (0, &header
.thunk_table
);
844 bfd_putl32 (0, &header
.thunk_table_offset
);
845 bfd_putl32 (0, &header
.num_sects
);
847 if (bfd_bwrite (&header
, sizeof (header
), stream
) != sizeof (header
))
850 /* Output the global hash header. */
852 bfd_putl32 (GLOBALS_HASH_SIGNATURE
, &hash_header
.signature
);
853 bfd_putl32 (GLOBALS_HASH_VERSION_70
, &hash_header
.version
);
854 bfd_putl32 (num_entries
* sizeof (struct hash_record
),
855 &hash_header
.entries_size
);
856 bfd_putl32 (buckets_size
, &hash_header
.buckets_size
);
858 if (bfd_bwrite (&hash_header
, sizeof (hash_header
), stream
) !=
859 sizeof (hash_header
))
862 /* Write the entries in hash order. */
864 for (unsigned int i
= 0; i
< num_entries
; i
++)
866 struct hash_record hr
;
868 bfd_putl32 (sorted
[i
]->offset
+ 1, &hr
.offset
);
869 bfd_putl32 (1, &hr
.reference
);
871 if (bfd_bwrite (&hr
, sizeof (hr
), stream
) != sizeof (hr
))
875 /* Write the bitmap for filled and unfilled buckets. */
877 for (unsigned int i
= 0; i
< num_buckets
; i
+= 8)
881 for (unsigned int j
= 0; j
< 8; j
++)
887 if (bfd_bwrite (&v
, sizeof (v
), stream
) != sizeof (v
))
891 /* Add a 4-byte gap. */
893 bfd_putl32 (0, int_buf
);
895 if (bfd_bwrite (int_buf
, sizeof (uint32_t), stream
) != sizeof (uint32_t))
898 /* Write the bucket offsets. */
900 for (unsigned int i
= 0; i
< num_buckets
; i
++)
904 /* 0xc is size of internal hash_record structure in
905 Microsoft's parser. */
906 bfd_putl32 (buckets
[i
]->index
* 0xc, int_buf
);
908 if (bfd_bwrite (int_buf
, sizeof (uint32_t), stream
) !=
914 /* Write the address map: offsets into the symbol record stream of
915 S_PUB32 records, ordered by address. */
919 qsort (sorted
, num_entries
, sizeof (struct public *),
920 public_compare_addr
);
922 for (unsigned int i
= 0; i
< num_entries
; i
++)
924 bfd_putl32 (sorted
[i
]->offset
, int_buf
);
926 if (bfd_bwrite (int_buf
, sizeof (uint32_t), stream
) !=
939 struct public *p
= publics_head
->next
;
950 /* The section header stream contains a copy of the section headers
951 from the PE file, in the same format. */
953 create_section_header_stream (bfd
*pdb
, bfd
*abfd
, uint16_t *num
)
956 unsigned int section_count
;
961 stream
= add_stream (pdb
, NULL
, num
);
965 section_count
= abfd
->section_count
;
967 /* Empty sections aren't output. */
968 for (asection
*sect
= abfd
->sections
; sect
; sect
= sect
->next
)
974 if (section_count
== 0)
977 /* Copy section table from output - it's already been written at this
980 scn_base
= bfd_coff_filhsz (abfd
) + bfd_coff_aoutsz (abfd
);
982 bfd_seek (abfd
, scn_base
, SEEK_SET
);
984 len
= section_count
* sizeof (struct external_scnhdr
);
987 if (bfd_bread (buf
, len
, abfd
) != len
)
993 if (bfd_bwrite (buf
, len
, stream
) != len
)
1004 /* Create a PDB debugging file for the PE image file abfd with the build ID
1005 guid, stored at pdb_name. */
1007 create_pdb_file (bfd
*abfd
, const char *pdb_name
, const unsigned char *guid
)
1011 bfd
*info_stream
, *dbi_stream
, *names_stream
, *sym_rec_stream
,
1013 uint16_t section_header_stream_num
, sym_rec_stream_num
, publics_stream_num
;
1015 pdb
= bfd_openw (pdb_name
, "pdb");
1018 einfo (_("%P: warning: cannot create PDB file: %E\n"));
1022 bfd_set_format (pdb
, bfd_archive
);
1024 if (!create_old_directory_stream (pdb
))
1026 einfo (_("%P: warning: cannot create old directory stream "
1027 "in PDB file: %E\n"));
1031 info_stream
= add_stream (pdb
, NULL
, NULL
);
1035 einfo (_("%P: warning: cannot create info stream "
1036 "in PDB file: %E\n"));
1040 if (!create_type_stream (pdb
))
1042 einfo (_("%P: warning: cannot create TPI stream "
1043 "in PDB file: %E\n"));
1047 dbi_stream
= add_stream (pdb
, NULL
, NULL
);
1051 einfo (_("%P: warning: cannot create DBI stream "
1052 "in PDB file: %E\n"));
1056 if (!create_type_stream (pdb
))
1058 einfo (_("%P: warning: cannot create IPI stream "
1059 "in PDB file: %E\n"));
1063 names_stream
= add_stream (pdb
, "/names", NULL
);
1067 einfo (_("%P: warning: cannot create /names stream "
1068 "in PDB file: %E\n"));
1072 sym_rec_stream
= add_stream (pdb
, NULL
, &sym_rec_stream_num
);
1074 if (!sym_rec_stream
)
1076 einfo (_("%P: warning: cannot create symbol record stream "
1077 "in PDB file: %E\n"));
1081 publics_stream
= add_stream (pdb
, NULL
, &publics_stream_num
);
1083 if (!publics_stream
)
1085 einfo (_("%P: warning: cannot create publics stream "
1086 "in PDB file: %E\n"));
1090 if (!create_section_header_stream (pdb
, abfd
, §ion_header_stream_num
))
1092 einfo (_("%P: warning: cannot create section header stream "
1093 "in PDB file: %E\n"));
1097 if (!populate_dbi_stream (dbi_stream
, abfd
, pdb
, section_header_stream_num
,
1098 sym_rec_stream_num
, publics_stream_num
))
1100 einfo (_("%P: warning: cannot populate DBI stream "
1101 "in PDB file: %E\n"));
1105 if (!populate_publics_stream (publics_stream
, abfd
, sym_rec_stream
))
1107 einfo (_("%P: warning: cannot populate publics stream "
1108 "in PDB file: %E\n"));
1112 if (!populate_info_stream (pdb
, info_stream
, guid
))
1114 einfo (_("%P: warning: cannot populate info stream "
1115 "in PDB file: %E\n"));