Skip to content

Commit 32292ae

Browse files
committed
Fix build determinism issue
1 parent 81dd963 commit 32292ae

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

tool/build/lib/elfwriter_zip.c

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,13 @@ static int DetermineVersionNeededToExtract(int method) {
6363
}
6464
}
6565

66+
static int NormalizeMode(int mode) {
67+
int res = mode & S_IFMT;
68+
if (mode & 0111)
69+
res |= 0111;
70+
return res | 0644;
71+
}
72+
6673
static unsigned char *EmitZipLfileHdr(unsigned char *p, const void *name,
6774
size_t namesize, uint32_t crc,
6875
uint8_t era, uint16_t gflags,
@@ -87,10 +94,10 @@ static unsigned char *EmitZipLfileHdr(unsigned char *p, const void *name,
8794
static void EmitZipCdirHdr(unsigned char *p, const void *name, size_t namesize,
8895
uint32_t crc, uint8_t era, uint16_t gflags,
8996
uint16_t method, uint16_t mtime, uint16_t mdate,
90-
uint16_t iattrs, uint16_t dosmode, uint16_t unixmode,
91-
size_t compsize, size_t uncompsize,
92-
size_t commentsize, struct timespec mtim,
93-
struct timespec atim, struct timespec ctim) {
97+
uint16_t iattrs, uint16_t unixmode, size_t compsize,
98+
size_t uncompsize, size_t commentsize,
99+
struct timespec mtim, struct timespec atim,
100+
struct timespec ctim) {
94101
uint64_t mt, at, ct;
95102
p = WRITE32LE(p, kZipCfileHdrMagic);
96103
*p++ = kZipCosmopolitanVersion;
@@ -111,8 +118,8 @@ static void EmitZipCdirHdr(unsigned char *p, const void *name, size_t namesize,
111118
p = WRITE16LE(p, commentsize);
112119
p = WRITE16LE(p, 0); /* disk */
113120
p = WRITE16LE(p, iattrs);
114-
p = WRITE16LE(p, dosmode);
115-
p = WRITE16LE(p, unixmode);
121+
p = WRITE16LE(p, 0);
122+
p = WRITE16LE(p, NormalizeMode(unixmode));
116123
p = WRITE32LE(p, 0); /* RELOCATE ME (kZipCfileOffsetOffset) */
117124
/* 46 */
118125
memcpy(p, name, namesize);
@@ -142,8 +149,8 @@ void elfwriter_zip(struct ElfWriter *elf, const char *symbol, const char *cname,
142149
uint32_t crc;
143150
unsigned char *lfile, *cfile;
144151
struct ElfWriterSymRef lfilesym;
152+
uint16_t method, gflags, mtime, mdate, iattrs;
145153
size_t lfilehdrsize, uncompsize, compsize, commentsize;
146-
uint16_t method, gflags, mtime, mdate, iattrs, dosmode;
147154

148155
CHECK_NE(0, mtim.tv_sec);
149156

@@ -168,7 +175,6 @@ void elfwriter_zip(struct ElfWriter *elf, const char *symbol, const char *cname,
168175
if (S_ISREG(mode) && istext(data, size)) {
169176
iattrs |= kZipIattrText;
170177
}
171-
dosmode = !(mode & 0200) ? kNtFileAttributeReadonly : 0;
172178
method = ShouldCompress(name, namesize, data, size, nocompress)
173179
? kZipCompressionDeflate
174180
: kZipCompressionNone;
@@ -215,8 +221,8 @@ void elfwriter_zip(struct ElfWriter *elf, const char *symbol, const char *cname,
215221
elfwriter_startsection(elf, ".zip.cdir", SHT_PROGBITS, 0);
216222
EmitZipCdirHdr(
217223
(cfile = elfwriter_reserve(elf, ZIP_CFILE_HDR_SIZE + namesize)), name,
218-
namesize, crc, era, gflags, method, mtime, mdate, iattrs, dosmode, mode,
219-
compsize, uncompsize, commentsize, mtim, atim, ctim);
224+
namesize, crc, era, gflags, method, mtime, mdate, iattrs, mode, compsize,
225+
uncompsize, commentsize, mtim, atim, ctim);
220226
elfwriter_appendsym(elf, gc(xasprintf("%s%s", "zip+cdir:", name)),
221227
ELF64_ST_INFO(STB_LOCAL, STT_OBJECT), STV_DEFAULT, 0,
222228
ZIP_CFILE_HDR_SIZE + namesize);

0 commit comments

Comments
 (0)