+static bfd_boolean
+pecoff_checksum_contents (bfd *abfd,
+ void (*process) (const void *, size_t, void *),
+ void *arg)
+{
+ file_ptr filepos = (file_ptr) 0;
+
+ while (1)
+ {
+ unsigned char b;
+ int status;
+
+ if (bfd_seek (abfd, filepos, SEEK_SET) != 0)
+ return 0;
+
+ status = bfd_bread (&b, (bfd_size_type) 1, abfd);
+ if (status < 1)
+ {
+ break;
+ }
+
+ (*process) (&b, 1, arg);
+ filepos += 1;
+ }
+
+ return TRUE;
+}
+
+static bfd_boolean
+write_build_id (bfd *abfd)
+{
+ struct pe_tdata *t = pe_data (abfd);
+ asection *asec;
+ struct bfd_link_order *link_order = NULL;
+ unsigned char *contents;
+ bfd_size_type size;
+ bfd_size_type build_id_size;
+ unsigned char *build_id;
+
+ /* Find the section the .build-id output section has been merged info. */
+ for (asec = abfd->sections; asec != NULL; asec = asec->next)
+ {
+ struct bfd_link_order *l = NULL;
+ for (l = asec->map_head.link_order; l != NULL; l = l->next)
+ {
+ if ((l->type == bfd_indirect_link_order))
+ {
+ if (l->u.indirect.section == t->build_id.sec)
+ {
+ link_order = l;
+ break;
+ }
+ }
+ }
+
+ if (link_order)
+ break;
+ }
+
+ if (!link_order)
+ {
+ einfo (_("%P: warning: .build-id section discarded,"
+ " --build-id ignored.\n"));
+ return TRUE;
+ }
+
+ if (t->build_id.sec->contents == NULL)
+ t->build_id.sec->contents = (unsigned char *) xmalloc (t->build_id.sec->size);
+ contents = t->build_id.sec->contents;
+ size = t->build_id.sec->size;
+
+ build_id_size = compute_build_id_size (t->build_id.style);
+ build_id = xmalloc (build_id_size);
+ generate_build_id (abfd, t->build_id.style, pecoff_checksum_contents, build_id, build_id_size);
+
+ bfd_vma ib = pe_data (link_info.output_bfd)->pe_opthdr.ImageBase;
+
+ /* Construct a debug directory entry which points to an immediately following CodeView record. */
+ struct internal_IMAGE_DEBUG_DIRECTORY idd;
+ idd.Characteristics = 0;
+ idd.TimeDateStamp = 0;
+ idd.MajorVersion = 0;
+ idd.MinorVersion = 0;
+ idd.Type = PE_IMAGE_DEBUG_TYPE_CODEVIEW;
+ idd.SizeOfData = sizeof (CV_INFO_PDB70) + 1;
+ idd.AddressOfRawData = asec->vma - ib + link_order->offset
+ + sizeof (struct external_IMAGE_DEBUG_DIRECTORY);
+ idd.PointerToRawData = asec->filepos + link_order->offset
+ + sizeof (struct external_IMAGE_DEBUG_DIRECTORY);
+
+ struct external_IMAGE_DEBUG_DIRECTORY *ext = (struct external_IMAGE_DEBUG_DIRECTORY *)contents;
+ _bfd_XXi_swap_debugdir_out (abfd, &idd, ext);
+
+ /* Write the debug directory enttry */
+ if (bfd_seek (abfd, asec->filepos + link_order->offset, SEEK_SET) != 0)
+ return 0;
+
+ if ((bfd_bwrite (contents, size, abfd) != size))
+ return 0;
+
+ /* Construct the CodeView record. */
+ CODEVIEW_INFO cvinfo;
+ cvinfo.CVSignature = CVINFO_PDB70_CVSIGNATURE;
+ cvinfo.Age = 1;
+
+ /* Zero pad or truncate the generated build_id to fit in the CodeView record. */
+ memset (&(cvinfo.Signature), 0, CV_INFO_SIGNATURE_LENGTH);
+ memcpy (&(cvinfo.Signature), build_id, (build_id_size > CV_INFO_SIGNATURE_LENGTH)
+ ? CV_INFO_SIGNATURE_LENGTH : build_id_size);
+
+ free (build_id);
+
+ /* Write the codeview record. */
+ if (_bfd_XXi_write_codeview_record (abfd, idd.PointerToRawData, &cvinfo) == 0)
+ return 0;
+
+ /* Record the location of the debug directory in the data directory. */
+ pe_data (link_info.output_bfd)->pe_opthdr.DataDirectory[PE_DEBUG_DATA].VirtualAddress
+ = asec->vma - ib + link_order->offset;
+ pe_data (link_info.output_bfd)->pe_opthdr.DataDirectory[PE_DEBUG_DATA].Size
+ = sizeof (struct external_IMAGE_DEBUG_DIRECTORY);
+
+ return TRUE;
+}
+
+/* Make .build-id section, and set up coff_tdata->build_id. */
+static bfd_boolean
+setup_build_id (bfd *ibfd)
+{
+ asection *s;
+ flagword flags;
+
+ if (!validate_build_id_style (emit_build_id))
+ {
+ einfo ("%P: warning: unrecognized --build-id style ignored.\n");
+ return FALSE;
+ }
+
+ flags = (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_IN_MEMORY
+ | SEC_LINKER_CREATED | SEC_READONLY | SEC_DATA);
+ s = bfd_make_section_anyway_with_flags (ibfd, ".build-id", flags);
+ if (s != NULL)
+ {
+ struct pe_tdata *t = pe_data (link_info.output_bfd);
+ t->build_id.after_write_object_contents = &write_build_id;
+ t->build_id.style = emit_build_id;
+ t->build_id.sec = s;
+
+ /* Section is a fixed size:
+ One IMAGE_DEBUG_DIRECTORY entry, of type IMAGE_DEBUG_TYPE_CODEVIEW,
+ pointing at a CV_INFO_PDB70 record containing the build-id, with a
+ null byte for PdbFileName. */
+ s->size = sizeof (struct external_IMAGE_DEBUG_DIRECTORY)
+ + sizeof (CV_INFO_PDB70) + 1;
+
+ return TRUE;
+ }
+
+ einfo ("%P: warning: Cannot create .build-id section,"
+ " --build-id ignored.\n");
+ return FALSE;
+}
+