Skip to content

Commit 32b6986

Browse files
mstorsjotstellar
authored andcommitted
[LLD] [COFF] Pick timestamps from the SOURCE_DATE_EPOCH variable (llvm#81326)
The SOURCE_DATE_EPOCH environment variable can be set in order to get reproducible build. When linking PE/COFF modules with LLD, the timestamp field is set to the current time, unless either the /timestamp: or /Brepro option is set. If neither of them is set, check the SOURCE_DATE_EPOCH variable, before resorting to using the actual current date and time. See https://reproducible-builds.org/docs/source-date-epoch/ for reference on the use of this variable. (cherry picked from commit 0df8aed)
1 parent efaa39f commit 32b6986

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

lld/COFF/Driver.cpp

+9-1
Original file line numberDiff line numberDiff line change
@@ -1825,7 +1825,15 @@ void LinkerDriver::linkerMain(ArrayRef<const char *> argsArr) {
18251825
}
18261826
} else {
18271827
config->repro = false;
1828-
config->timestamp = time(nullptr);
1828+
if (std::optional<std::string> epoch =
1829+
Process::GetEnv("SOURCE_DATE_EPOCH")) {
1830+
StringRef value(*epoch);
1831+
if (value.getAsInteger(0, config->timestamp))
1832+
fatal(Twine("invalid SOURCE_DATE_EPOCH timestamp: ") + value +
1833+
". Expected 32-bit integer");
1834+
} else {
1835+
config->timestamp = time(nullptr);
1836+
}
18291837
}
18301838

18311839
// Handle /alternatename

lld/test/COFF/timestamp.test

+18
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,19 @@ RUN: yaml2obj %p/Inputs/generic.yaml -o %t.obj
33
RUN: lld-link %t.obj /debug /Brepro /entry:main /nodefaultlib /out:%t.1.exe
44
RUN: lld-link %t.obj /debug /Brepro /entry:main /nodefaultlib /out:%t.2.exe
55
RUN: lld-link %t.obj /debug /timestamp:0 /entry:main /nodefaultlib /out:%t.3.exe
6+
RUN: env SOURCE_DATE_EPOCH=0 lld-link %t.obj /debug /entry:main /nodefaultlib /out:%t.4.exe
7+
RUN: lld-link %t.obj /debug /timestamp:4294967295 /entry:main /nodefaultlib /out:%t.5.exe
8+
RUN: env SOURCE_DATE_EPOCH=4294967295 lld-link %t.obj /debug /entry:main /nodefaultlib /out:%t.6.exe
9+
RUN: env SOURCE_DATE_EPOCH=12345 lld-link %t.obj /debug /timestamp:0 /entry:main /nodefaultlib /out:%t.7.exe
10+
RUN: env LLD_IN_TEST=1 not lld-link %t.obj /debug /timestamp:4294967296 /entry:main /nodefaultlib /out:%t.8.exe 2>&1 | FileCheck %s --check-prefix=ERROR
11+
RUN: env SOURCE_DATE_EPOCH=4294967296 env LLD_IN_TEST=1 not lld-link %t.obj /debug /entry:main /nodefaultlib /out:%t.9.exe 2>&1 | FileCheck %s --check-prefix=ERROR2
612
RUN: llvm-readobj --file-headers --coff-debug-directory %t.1.exe | FileCheck %s --check-prefix=HASH
713
RUN: llvm-readobj --file-headers --coff-debug-directory %t.2.exe | FileCheck %s --check-prefix=HASH
814
RUN: llvm-readobj --file-headers --coff-debug-directory %t.3.exe | FileCheck %s --check-prefix=ZERO
15+
RUN: llvm-readobj --file-headers --coff-debug-directory %t.4.exe | FileCheck %s --check-prefix=ZERO
16+
RUN: llvm-readobj --file-headers --coff-debug-directory %t.5.exe | FileCheck %s --check-prefix=MAX
17+
RUN: llvm-readobj --file-headers --coff-debug-directory %t.6.exe | FileCheck %s --check-prefix=MAX
18+
RUN: llvm-readobj --file-headers --coff-debug-directory %t.7.exe | FileCheck %s --check-prefix=ZERO
919

1020
HASH: ImageFileHeader {
1121
HASH: TimeDateStamp: [[STAMP:.*]]
@@ -16,3 +26,11 @@ ZERO: ImageFileHeader {
1626
ZERO: TimeDateStamp: 1970-01-01 00:00:00 (0x0)
1727
ZERO: DebugDirectory [
1828
ZERO: TimeDateStamp: 1970-01-01 00:00:00 (0x0)
29+
30+
MAX: ImageFileHeader {
31+
MAX: TimeDateStamp: 2106-02-07 06:28:15 (0xFFFFFFFF)
32+
MAX: DebugDirectory [
33+
MAX: TimeDateStamp: 2106-02-07 06:28:15 (0xFFFFFFFF)
34+
35+
ERROR: error: invalid timestamp: 4294967296. Expected 32-bit integer
36+
ERROR2: error: invalid SOURCE_DATE_EPOCH timestamp: 4294967296. Expected 32-bit integer

0 commit comments

Comments
 (0)