objcopy embeds the current time and ignores SOURCE_DATE_EPOCH making the output unreproducible

Message ID 0c22e147-aa0c-12ae-24e0-4f770948054d@debian.org
State Unresolved
Headers
Series objcopy embeds the current time and ignores SOURCE_DATE_EPOCH making the output unreproducible |

Checks

Context Check Description
snail/binutils-gdb-check warning Git am fail log

Commit Message

Matthias Klose July 19, 2023, 10:54 a.m. UTC
  This is forwarded from https://bugs.debian.org/1040450

is this suitable upstream, or should it be kept as a local patch? 
SOURCE_DATE_EPOCH is also respected in GCC upstream.


Hi,

steps to reproduce the unreproducibility:

     $ objcopy /usr/lib/systemd/boot/efi/linuxaa64.efi.stub bootaa64_1
     $ objcopy /usr/lib/systemd/boot/efi/linuxaa64.efi.stub bootaa64_2
     $ cmp bootaa64_1 bootaa64_2
     bootaa64_1 bootaa64_2 differ: byte 137, line 1

The resulting bootaa64.efi will have the local time embedded which makes it
unreproducible unless faketime is used. Setting SOURCE_DATE_EPOCH or adding
--enable-deterministic-archives does not make a difference.

I identified the part of the code that generates this timestamp and pasted a
preliminary patch fixing this issue to the end of this email. With that patch,
setting SOURCE_DATE_EPOCH=0 results in a file with:

$ objdump -x bootaa64.efi
[...]
Time/Date		Thu Jan  1 01:00:00 1970

Since building binutils takes 7.4 hours on my machine and since I have never
interacted with binutils upstream before, I'd appreciate if you could take it
from here and see that this problem gets fixed in binutils upstream the way
that its developers like to see this fixed.

Thanks!

cheers, josch
  

Comments

Nick Clifton July 19, 2023, 11:02 a.m. UTC | #1
Hi Matthias,

> This is forwarded from https://bugs.debian.org/1040450
> 
> is this suitable upstream, or should it be kept as a local patch? SOURCE_DATE_EPOCH is also respected in GCC upstream.

It is suitable for upstream.  (Plus I happen to know that Fedora would
like to have this functionality as well).

I would suggest a couple of small changes to the patch however:

   * Formatting fixes.  (Especially for the positioning of the curly braces).

   * Updating the description of the --timestamp command line option in
     ld/ld.texi to mention that if SOURCE_DATE_EPOCH is defined in the
    environment then this will be used instead of the current time.

Cheers
   Nick
  

Patch

--- a/bfd/peXXigen.c
+++ b/bfd/peXXigen.c
@@ -847,9 +847,17 @@  _bfd_XXi_only_swap_filehdr_out (bfd * ab

    /* Use a real timestamp by default, unless the no-insert-timestamp
       option was chosen.  */
-  if ((pe_data (abfd)->timestamp) == -1)
-    H_PUT_32 (abfd, time (0), filehdr_out->f_timdat);
-  else
+  if ((pe_data (abfd)->timestamp) == -1) {
+    time_t now;
+    char *source_date_epoch;
+    source_date_epoch = getenv("SOURCE_DATE_EPOCH");
+    if (source_date_epoch) {
+      now = (time_t)strtoll(source_date_epoch, NULL, 10);
+    } else {
+      now = time(NULL);
+    }
+    H_PUT_32 (abfd, now, filehdr_out->f_timdat);
+  } else
      H_PUT_32 (abfd, pe_data (abfd)->timestamp, filehdr_out->f_timdat);

    PUT_FILEHDR_SYMPTR (abfd, filehdr_in->f_symptr,