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 /* Create the substream which maps addresses in the image file to locations
596 in the original object files. */
598 create_section_contrib_substream (bfd *abfd, void **data, uint32_t *size)
600 unsigned int num_sc = 0;
601 struct section_contribution *sc;
606 for (bfd *in = coff_data (abfd)->link_info->input_bfds; in;
609 for (asection *s = in->sections; s; s = s->next)
611 if (s->size == 0 || discarded_section (s))
618 *size = sizeof (uint32_t) + (num_sc * sizeof (struct section_contribution));
619 *data = xmalloc (*size);
621 bfd_putl32 (SECTION_CONTRIB_VERSION_60, *data);
623 /* Read characteristics of outputted sections. */
625 sect_flags = xmalloc (sizeof (uint32_t) * abfd->section_count);
627 offset = bfd_coff_filhsz (abfd) + bfd_coff_aoutsz (abfd);
628 offset += offsetof (struct external_scnhdr, s_flags);
630 for (unsigned int i = 0; i < abfd->section_count; i++)
632 bfd_seek (abfd, offset, SEEK_SET);
634 if (bfd_bread (sect_flags + (i * sizeof (uint32_t)), sizeof (uint32_t),
635 abfd) != sizeof (uint32_t))
642 offset += sizeof (struct external_scnhdr);
646 (struct section_contribution *) ((uint8_t *) *data + sizeof (uint32_t));
649 for (bfd *in = coff_data (abfd)->link_info->input_bfds; in;
652 for (asection *s = in->sections; s; s = s->next)
656 if (s->size == 0 || discarded_section (s))
659 sect_num = find_section_number (abfd, s->output_section);
661 memcpy (&sc->characteristics,
662 sect_flags + ((sect_num - 1) * sizeof (uint32_t)),
665 bfd_putl16 (sect_num, &sc->section);
666 bfd_putl16 (0, &sc->padding1);
667 bfd_putl32 (s->output_offset, &sc->offset);
668 bfd_putl32 (s->size, &sc->size);
669 bfd_putl16 (mod_index, &sc->module_index);
670 bfd_putl16 (0, &sc->padding2);
671 bfd_putl32 (0, &sc->data_crc);
672 bfd_putl32 (0, &sc->reloc_crc);
685 /* Stream 4 is the debug information (DBI) stream. */
687 populate_dbi_stream (bfd *stream, bfd *abfd, bfd *pdb,
688 uint16_t section_header_stream_num,
689 uint16_t sym_rec_stream_num,
690 uint16_t publics_stream_num)
692 struct pdb_dbi_stream_header h;
693 struct optional_dbg_header opt;
695 uint32_t mod_info_size, sc_size;
697 if (!create_module_info_substream (abfd, pdb, &mod_info, &mod_info_size))
700 if (!create_section_contrib_substream (abfd, &sc, &sc_size))
706 bfd_putl32 (0xffffffff, &h.version_signature);
707 bfd_putl32 (DBI_STREAM_VERSION_70, &h.version_header);
708 bfd_putl32 (1, &h.age);
709 bfd_putl16 (0xffff, &h.global_stream_index);
710 bfd_putl16 (0x8e1d, &h.build_number); // MSVC 14.29
711 bfd_putl16 (publics_stream_num, &h.public_stream_index);
712 bfd_putl16 (0, &h.pdb_dll_version);
713 bfd_putl16 (sym_rec_stream_num, &h.sym_record_stream);
714 bfd_putl16 (0, &h.pdb_dll_rbld);
715 bfd_putl32 (mod_info_size, &h.mod_info_size);
716 bfd_putl32 (sc_size, &h.section_contribution_size);
717 bfd_putl32 (0, &h.section_map_size);
718 bfd_putl32 (0, &h.source_info_size);
719 bfd_putl32 (0, &h.type_server_map_size);
720 bfd_putl32 (0, &h.mfc_type_server_index);
721 bfd_putl32 (sizeof (opt), &h.optional_dbg_header_size);
722 bfd_putl32 (0, &h.ec_substream_size);
723 bfd_putl16 (0, &h.flags);
724 bfd_putl16 (get_arch_number (abfd), &h.machine);
725 bfd_putl32 (0, &h.padding);
727 if (bfd_bwrite (&h, sizeof (h), stream) != sizeof (h))
734 if (bfd_bwrite (mod_info, mod_info_size, stream) != mod_info_size)
743 if (bfd_bwrite (sc, sc_size, stream) != sc_size)
751 bfd_putl16 (0xffff, &opt.fpo_stream);
752 bfd_putl16 (0xffff, &opt.exception_stream);
753 bfd_putl16 (0xffff, &opt.fixup_stream);
754 bfd_putl16 (0xffff, &opt.omap_to_src_stream);
755 bfd_putl16 (0xffff, &opt.omap_from_src_stream);
756 bfd_putl16 (section_header_stream_num, &opt.section_header_stream);
757 bfd_putl16 (0xffff, &opt.token_map_stream);
758 bfd_putl16 (0xffff, &opt.xdata_stream);
759 bfd_putl16 (0xffff, &opt.pdata_stream);
760 bfd_putl16 (0xffff, &opt.new_fpo_stream);
761 bfd_putl16 (0xffff, &opt.orig_section_header_stream);
763 if (bfd_bwrite (&opt, sizeof (opt), stream) != sizeof (opt))
769 /* Used as parameter to qsort, to sort publics by hash. */
771 public_compare_hash (const void *s1, const void *s2)
773 const struct public *p1 = *(const struct public **) s1;
774 const struct public *p2 = *(const struct public **) s2;
776 if (p1->hash < p2->hash)
778 if (p1->hash > p2->hash)
784 /* Used as parameter to qsort, to sort publics by address. */
786 public_compare_addr (const void *s1, const void *s2)
788 const struct public *p1 = *(const struct public **) s1;
789 const struct public *p2 = *(const struct public **) s2;
791 if (p1->section < p2->section)
793 if (p1->section > p2->section)
796 if (p1->address < p2->address)
798 if (p1->address > p2->address)
804 /* The publics stream is a hash map of S_PUB32 records, which are stored
805 in the symbol record stream. Each S_PUB32 entry represents a symbol
806 from the point of view of the linker: a section index, an offset within
807 the section, and a mangled name. Compare with S_GDATA32 and S_GPROC32,
808 which are the same thing but generated by the compiler. */
810 populate_publics_stream (bfd *stream, bfd *abfd, bfd *sym_rec_stream)
812 struct publics_header header;
813 struct globals_hash_header hash_header;
814 const unsigned int num_buckets = 4096;
815 unsigned int num_entries = 0, filled_buckets = 0;
816 unsigned int buckets_size, sym_hash_size;
817 char int_buf[sizeof (uint32_t)];
818 struct public *publics_head = NULL, *publics_tail = NULL;
819 struct public **buckets;
820 struct public **sorted = NULL;
823 buckets = xmalloc (sizeof (struct public *) * num_buckets);
824 memset (buckets, 0, sizeof (struct public *) * num_buckets);
826 /* Loop through the global symbols in our input files, and write S_PUB32
827 records in the symbol record stream for those that make it into the
829 for (bfd *in = coff_data (abfd)->link_info->input_bfds; in;
832 for (unsigned int i = 0; i < in->symcount; i++)
834 struct bfd_symbol *sym = in->outsymbols[i];
836 if (sym->flags & BSF_GLOBAL)
839 uint16_t record_length;
840 const char *name = sym->name;
841 size_t name_len = strlen (name);
842 struct public *p = xmalloc (sizeof (struct public));
843 unsigned int padding = 0;
848 find_section_number (abfd, sym->section->output_section);
854 p->offset = bfd_tell (sym_rec_stream);
855 p->hash = calc_hash (name, name_len) % num_buckets;
856 p->section = section;
857 p->address = sym->section->output_offset + sym->value;
859 record_length = sizeof (struct pubsym) + name_len + 1;
861 if (record_length % 4)
862 padding = 4 - (record_length % 4);
864 /* Assume that all global symbols in executable sections
866 if (sym->section->flags & SEC_CODE)
867 flags = PUBSYM_FUNCTION;
869 bfd_putl16 (record_length + padding - sizeof (uint16_t),
871 bfd_putl16 (S_PUB32, &ps.record_type);
872 bfd_putl32 (flags, &ps.flags);
873 bfd_putl32 (p->address, &ps.offset);
874 bfd_putl16 (p->section, &ps.section);
876 if (bfd_bwrite (&ps, sizeof (struct pubsym), sym_rec_stream) !=
877 sizeof (struct pubsym))
880 if (bfd_bwrite (name, name_len + 1, sym_rec_stream) !=
884 for (unsigned int j = 0; j < padding; j++)
888 if (bfd_bwrite (&b, sizeof (uint8_t), sym_rec_stream) !=
896 publics_tail->next = p;
907 /* Create an array of pointers, sorted by hash value. */
909 sorted = xmalloc (sizeof (struct public *) * num_entries);
911 struct public *p = publics_head;
912 for (unsigned int i = 0; i < num_entries; i++)
918 qsort (sorted, num_entries, sizeof (struct public *),
919 public_compare_hash);
921 /* Populate the buckets. */
923 for (unsigned int i = 0; i < num_entries; i++)
925 if (!buckets[sorted[i]->hash])
927 buckets[sorted[i]->hash] = sorted[i];
931 sorted[i]->index = i;
935 buckets_size = num_buckets / 8;
936 buckets_size += sizeof (uint32_t);
937 buckets_size += filled_buckets * sizeof (uint32_t);
939 sym_hash_size = sizeof (hash_header);
940 sym_hash_size += num_entries * sizeof (struct hash_record);
941 sym_hash_size += buckets_size;
943 /* Output the publics header. */
945 bfd_putl32 (sym_hash_size, &header.sym_hash_size);
946 bfd_putl32 (num_entries * sizeof (uint32_t), &header.addr_map_size);
947 bfd_putl32 (0, &header.num_thunks);
948 bfd_putl32 (0, &header.thunks_size);
949 bfd_putl32 (0, &header.thunk_table);
950 bfd_putl32 (0, &header.thunk_table_offset);
951 bfd_putl32 (0, &header.num_sects);
953 if (bfd_bwrite (&header, sizeof (header), stream) != sizeof (header))
956 /* Output the global hash header. */
958 bfd_putl32 (GLOBALS_HASH_SIGNATURE, &hash_header.signature);
959 bfd_putl32 (GLOBALS_HASH_VERSION_70, &hash_header.version);
960 bfd_putl32 (num_entries * sizeof (struct hash_record),
961 &hash_header.entries_size);
962 bfd_putl32 (buckets_size, &hash_header.buckets_size);
964 if (bfd_bwrite (&hash_header, sizeof (hash_header), stream) !=
965 sizeof (hash_header))
968 /* Write the entries in hash order. */
970 for (unsigned int i = 0; i < num_entries; i++)
972 struct hash_record hr;
974 bfd_putl32 (sorted[i]->offset + 1, &hr.offset);
975 bfd_putl32 (1, &hr.reference);
977 if (bfd_bwrite (&hr, sizeof (hr), stream) != sizeof (hr))
981 /* Write the bitmap for filled and unfilled buckets. */
983 for (unsigned int i = 0; i < num_buckets; i += 8)
987 for (unsigned int j = 0; j < 8; j++)
993 if (bfd_bwrite (&v, sizeof (v), stream) != sizeof (v))
997 /* Add a 4-byte gap. */
999 bfd_putl32 (0, int_buf);
1001 if (bfd_bwrite (int_buf, sizeof (uint32_t), stream) != sizeof (uint32_t))
1004 /* Write the bucket offsets. */
1006 for (unsigned int i = 0; i < num_buckets; i++)
1010 /* 0xc is size of internal hash_record structure in
1011 Microsoft's parser. */
1012 bfd_putl32 (buckets[i]->index * 0xc, int_buf);
1014 if (bfd_bwrite (int_buf, sizeof (uint32_t), stream) !=
1020 /* Write the address map: offsets into the symbol record stream of
1021 S_PUB32 records, ordered by address. */
1023 if (num_entries > 0)
1025 qsort (sorted, num_entries, sizeof (struct public *),
1026 public_compare_addr);
1028 for (unsigned int i = 0; i < num_entries; i++)
1030 bfd_putl32 (sorted[i]->offset, int_buf);
1032 if (bfd_bwrite (int_buf, sizeof (uint32_t), stream) !=
1043 while (publics_head)
1045 struct public *p = publics_head->next;
1047 free (publics_head);
1056 /* The section header stream contains a copy of the section headers
1057 from the PE file, in the same format. */
1059 create_section_header_stream (bfd *pdb, bfd *abfd, uint16_t *num)
1062 unsigned int section_count;
1067 stream = add_stream (pdb, NULL, num);
1071 section_count = abfd->section_count;
1073 /* Empty sections aren't output. */
1074 for (asection *sect = abfd->sections; sect; sect = sect->next)
1076 if (sect->size == 0)
1080 if (section_count == 0)
1083 /* Copy section table from output - it's already been written at this
1086 scn_base = bfd_coff_filhsz (abfd) + bfd_coff_aoutsz (abfd);
1088 bfd_seek (abfd, scn_base, SEEK_SET);
1090 len = section_count * sizeof (struct external_scnhdr);
1091 buf = xmalloc (len);
1093 if (bfd_bread (buf, len, abfd) != len)
1099 if (bfd_bwrite (buf, len, stream) != len)
1110 /* Create a PDB debugging file for the PE image file abfd with the build ID
1111 guid, stored at pdb_name. */
1113 create_pdb_file (bfd *abfd, const char *pdb_name, const unsigned char *guid)
1117 bfd *info_stream, *dbi_stream, *names_stream, *sym_rec_stream,
1119 uint16_t section_header_stream_num, sym_rec_stream_num, publics_stream_num;
1121 pdb = bfd_openw (pdb_name, "pdb");
1124 einfo (_("%P: warning: cannot create PDB file: %E\n"));
1128 bfd_set_format (pdb, bfd_archive);
1130 if (!create_old_directory_stream (pdb))
1132 einfo (_("%P: warning: cannot create old directory stream "
1133 "in PDB file: %E\n"));
1137 info_stream = add_stream (pdb, NULL, NULL);
1141 einfo (_("%P: warning: cannot create info stream "
1142 "in PDB file: %E\n"));
1146 if (!create_type_stream (pdb))
1148 einfo (_("%P: warning: cannot create TPI stream "
1149 "in PDB file: %E\n"));
1153 dbi_stream = add_stream (pdb, NULL, NULL);
1157 einfo (_("%P: warning: cannot create DBI stream "
1158 "in PDB file: %E\n"));
1162 if (!create_type_stream (pdb))
1164 einfo (_("%P: warning: cannot create IPI stream "
1165 "in PDB file: %E\n"));
1169 names_stream = add_stream (pdb, "/names", NULL);
1173 einfo (_("%P: warning: cannot create /names stream "
1174 "in PDB file: %E\n"));
1178 sym_rec_stream = add_stream (pdb, NULL, &sym_rec_stream_num);
1180 if (!sym_rec_stream)
1182 einfo (_("%P: warning: cannot create symbol record stream "
1183 "in PDB file: %E\n"));
1187 publics_stream = add_stream (pdb, NULL, &publics_stream_num);
1189 if (!publics_stream)
1191 einfo (_("%P: warning: cannot create publics stream "
1192 "in PDB file: %E\n"));
1196 if (!create_section_header_stream (pdb, abfd, §ion_header_stream_num))
1198 einfo (_("%P: warning: cannot create section header stream "
1199 "in PDB file: %E\n"));
1203 if (!populate_dbi_stream (dbi_stream, abfd, pdb, section_header_stream_num,
1204 sym_rec_stream_num, publics_stream_num))
1206 einfo (_("%P: warning: cannot populate DBI stream "
1207 "in PDB file: %E\n"));
1211 if (!populate_publics_stream (publics_stream, abfd, sym_rec_stream))
1213 einfo (_("%P: warning: cannot populate publics stream "
1214 "in PDB file: %E\n"));
1218 if (!populate_info_stream (pdb, info_stream, guid))
1220 einfo (_("%P: warning: cannot populate info stream "
1221 "in PDB file: %E\n"));