Skip to content

Commit 045632a

Browse files
committed
Make fixupobj work on Windows
This fixes a regression with recent cosmocc toolchain releases that causes fixupobj to fail with "ifunc ftruncate #1 failed" on Windows
1 parent 9a10ada commit 045632a

File tree

7 files changed

+38
-15
lines changed

7 files changed

+38
-15
lines changed

build/bootstrap/fixupobj

35.6 KB
Binary file not shown.

libc/nexgen32e/x86compiler.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@
6161
#define _X86_CC_AVX512BF16 0
6262
#endif
6363

64+
#ifdef __AVX512FP16__
65+
#define _X86_CC_AVX512FP16 1
66+
#else
67+
#define _X86_CC_AVX512FP16 0
68+
#endif
69+
6470
#ifdef __AVX512VBMI__
6571
#define _X86_CC_AVX512VBMI 1
6672
#else

libc/nexgen32e/x86feature.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
#define X86_AVX512VL 7H, EBX, 31, 0
4343
#define X86_AVX512_4FMAPS 7H, EDX, 3, 0
4444
#define X86_AVX512_4VNNIW 7H, EDX, 2, _X86_CC_AVX5124VNNIW
45+
#define X86_AVX512_FP16 7H, EDX, 23, _X86_CC_AVX512FP16
4546
#define X86_AVX512_BF16 7H, EAX, 5, _X86_CC_AVX512BF16
4647
#define X86_AVX512_BITALG 7H, ECX, 12, 0
4748
#define X86_AVX512_VBMI2 7H, ECX, 6, 0

tool/build/fixupobj.c

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include "libc/log/log.h"
3434
#include "libc/macros.internal.h"
3535
#include "libc/mem/gc.h"
36+
#include "libc/mem/mem.h"
3637
#include "libc/runtime/runtime.h"
3738
#include "libc/serialize.h"
3839
#include "libc/stdalign.internal.h"
@@ -71,6 +72,21 @@ static wontreturn void Die(const char *reason) {
7172
exit(1);
7273
}
7374

75+
static wontreturn void DieOom(void) {
76+
Die("out of memory");
77+
}
78+
79+
static void *Malloc(size_t n) {
80+
void *p;
81+
if (!(p = malloc(n))) DieOom();
82+
return p;
83+
}
84+
85+
static void *Realloc(void *p, size_t n) {
86+
if (!(p = realloc(p, n))) DieOom();
87+
return p;
88+
}
89+
7490
static wontreturn void SysExit(const char *func) {
7591
const char *errstr;
7692
if (!(errstr = _strerdoc(errno))) errstr = "EUNKNOWN";
@@ -515,10 +531,7 @@ static void GenerateIfuncInit(void) {
515531
// remap file so it has more space
516532
if (elf->e_shnum + 2 > 65535) Die("too many sections");
517533
size_t reserve_size = esize + 32 * 1024 * 1024;
518-
if (ftruncate(fildes, reserve_size)) SysExit("ifunc ftruncate #1");
519-
elf = mmap((char *)elf, reserve_size, PROT_READ | PROT_WRITE,
520-
MAP_FIXED | MAP_SHARED, fildes, 0);
521-
if (elf == MAP_FAILED) SysExit("ifunc mmap");
534+
elf = Realloc(elf, reserve_size);
522535

523536
// duplicate section name strings table to end of file
524537
Elf64_Shdr *shdrstr_shdr = (Elf64_Shdr *)((char *)elf + elf->e_shoff +
@@ -591,8 +604,6 @@ static void GenerateIfuncInit(void) {
591604
memcpy((char *)elf + esize, code, code_i);
592605
esize += code_i;
593606
unassert(esize == code_shdr->sh_offset + code_shdr->sh_size);
594-
595-
if (ftruncate(fildes, esize)) SysExit("ifunc ftruncate #1");
596607
}
597608

598609
// when __attribute__((__target_clones__(...))) is used, static binaries
@@ -622,9 +633,9 @@ static void FixupObject(void) {
622633
SysExit("lseek");
623634
}
624635
if (esize) {
625-
if ((elf = mmap((void *)0x032100000000, esize, PROT_READ | PROT_WRITE,
626-
MAP_FIXED | MAP_SHARED, fildes, 0)) == MAP_FAILED) {
627-
SysExit("mmap");
636+
elf = Malloc(esize);
637+
if (pread(fildes, elf, esize, 0) != esize) {
638+
SysExit("pread");
628639
}
629640
if (!IsElf64Binary(elf, esize)) {
630641
Die("not an elf64 binary");
@@ -654,13 +665,10 @@ static void FixupObject(void) {
654665
PurgeIfuncSections();
655666
RelinkZipFiles();
656667
}
657-
if (msync(elf, esize, MS_ASYNC | MS_INVALIDATE)) {
658-
SysExit("msync");
668+
if (pwrite(fildes, elf, esize, 0) != esize) {
669+
SysExit("pwrite");
659670
}
660671
}
661-
if (munmap(elf, esize)) {
662-
SysExit("munmap");
663-
}
664672
}
665673
if (close(fildes)) {
666674
SysExit("close");

tool/emacs/cosmo-c-types.el

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,13 @@
220220
"__m256i"
221221
"__m256_u"
222222
"__m256d_u"
223-
"__m256i_u"))
223+
"__m256i_u"
224+
"__m512"
225+
"__m512d"
226+
"__m512i"
227+
"__m512_u"
228+
"__m512d_u"
229+
"__m512i_u"))
224230

225231
)
226232
(concat "\\_<"

tool/emacs/cosmo-cpp-constants.el

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
"__AVX512CD__"
5656
"__AVX512DQ__"
5757
"__AVX512BF16__"
58+
"__AVX512FP16__"
5859
"__AVX512IFMA__"
5960
"__AVX5124VNNIW__"
6061
"__AVX512VBMI__"

tool/viz/cpuid.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ int main(int argc, char *argv[]) {
188188
CANIUSE(AVX512VL);
189189
CANIUSE(AVX512_4FMAPS);
190190
CANIUSE(AVX512_4VNNIW);
191+
CANIUSE(AVX512_FP16);
191192
CANIUSE(AVX512_BF16);
192193
CANIUSE(AVX512_BITALG);
193194
CANIUSE(AVX512_VBMI2);

0 commit comments

Comments
 (0)