Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 25 additions & 4 deletions compiler/src/dmd/backend/mscoffobj.d
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import dmd.backend.type;
import dmd.backend.mscoff;

import dmd.common.outbuffer;
import dmd.root.hash;

nothrow:
@safe:
Expand Down Expand Up @@ -649,8 +650,28 @@ void MsCoffObj_term(const(char)[] objfilename)
BIGOBJ_HEADER header = void;
IMAGE_FILE_HEADER header_old = void;

time_t f_timedat = 0;
time(&f_timedat);
// Use a hash of the object content instead of current timestamp for reproducible builds
uint calcDeterministicTimestamp()
{
uint hash = 0;

// Hash each segment
for (segidx_t seg = 1; seg < SegData.length; seg++)
{
seg_data* pseg = SegData[seg];
if (pseg.SDbuf && pseg.SDbuf.length())
{
uint segHash = calcHash(cast(const(ubyte)[])pseg.SDbuf.buf[0..pseg.SDbuf.length()]);

// Mix this segment's hash into the overall hash
hash = cast(uint)mixHash(hash, segHash);
}
}

return hash;
}

uint deterministicTimestamp = calcDeterministicTimestamp();
uint symtable_offset;

if (bigobj)
Expand All @@ -660,7 +681,7 @@ void MsCoffObj_term(const(char)[] objfilename)
header.Version = 2;
header.Machine = I64 ? IMAGE_FILE_MACHINE_AMD64 : IMAGE_FILE_MACHINE_I386;
header.NumberOfSections = scnhdr_cnt;
header.TimeDateStamp = cast(uint)f_timedat;
header.TimeDateStamp = deterministicTimestamp;
static immutable ubyte[16] uuid =
[ '\xc7', '\xa1', '\xba', '\xd1', '\xee', '\xba', '\xa9', '\x4b',
'\xaf', '\x20', '\xfa', '\xf6', '\x6a', '\xa4', '\xdc', '\xb8' ];
Expand All @@ -677,7 +698,7 @@ void MsCoffObj_term(const(char)[] objfilename)
{
header_old.Machine = I64 ? IMAGE_FILE_MACHINE_AMD64 : IMAGE_FILE_MACHINE_I386;
header_old.NumberOfSections = cast(ushort)scnhdr_cnt;
header_old.TimeDateStamp = cast(uint)f_timedat;
header_old.TimeDateStamp = deterministicTimestamp;
header_old.SizeOfOptionalHeader = 0;
header_old.Characteristics = 0;
foffset = (header_old).sizeof; // start after header
Expand Down
Loading