Skip to content

Commit 6a0c428

Browse files
committed
use __chkstk_ms compiler-rt functions for __chkstk
I had to revert the target native features thing because there is still some incorrect behavior with f128. Reopens #508 partially reverts b505462 See #302
1 parent b505462 commit 6a0c428

File tree

2 files changed

+32
-11
lines changed

2 files changed

+32
-11
lines changed

src/codegen.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5008,8 +5008,16 @@ static void init(CodeGen *g) {
50085008
const char *target_specific_cpu_args;
50095009
const char *target_specific_features;
50105010
if (g->is_native_target) {
5011-
target_specific_cpu_args = ZigLLVMGetHostCPUName();
5012-
target_specific_features = ZigLLVMGetNativeFeatures();
5011+
// LLVM creates invalid binaries on Windows sometimes.
5012+
// See https://github.com/zig-lang/zig/issues/508
5013+
// As a workaround we do not use target native features on Windows.
5014+
if (g->zig_target.os == ZigLLVM_Win32) {
5015+
target_specific_cpu_args = "";
5016+
target_specific_features = "";
5017+
} else {
5018+
target_specific_cpu_args = ZigLLVMGetHostCPUName();
5019+
target_specific_features = ZigLLVMGetNativeFeatures();
5020+
}
50135021
} else {
50145022
target_specific_cpu_args = "";
50155023
target_specific_features = "";

std/special/compiler_rt/index.zig

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,9 @@ export nakedcc fn _chkstk() align(4) {
129129
@setGlobalLinkage(_chkstk, strong_linkage);
130130
asm volatile (
131131
\\ push %%ecx
132+
\\ push %%eax
132133
\\ cmp $0x1000,%%eax
133-
\\ lea 8(%%esp),%%ecx // esp before calling this routine -> ecx
134+
\\ lea 12(%%esp),%%ecx
134135
\\ jb 1f
135136
\\ 2:
136137
\\ sub $0x1000,%%ecx
@@ -141,12 +142,8 @@ export nakedcc fn _chkstk() align(4) {
141142
\\ 1:
142143
\\ sub %%eax,%%ecx
143144
\\ test %%ecx,(%%ecx)
144-
\\
145-
\\ lea 4(%%esp),%%eax // load pointer to the return address into eax
146-
\\ mov %%ecx,%%esp // install the new top of stack pointer into esp
147-
\\ mov -4(%%eax),%%ecx // restore ecx
148-
\\ push (%%eax) // push return address onto the stack
149-
\\ sub %%esp,%%eax // restore the original value in eax
145+
\\ pop %%eax
146+
\\ pop %%ecx
150147
\\ ret
151148
);
152149
unreachable;
@@ -159,13 +156,29 @@ export nakedcc fn _chkstk() align(4) {
159156
// the implementation from disassembled ntdll seems to depend on
160157
// thread local storage. So we have given up this safety check
161158
// and simply have `ret`.
162-
export nakedcc fn __chkstk() align(8) {
159+
export nakedcc fn __chkstk() align(4) {
163160
@setDebugSafety(this, false);
164161

165162
if (win64_nocrt) {
166163
@setGlobalLinkage(__chkstk, strong_linkage);
167164
asm volatile (
168-
\\ ret
165+
\\ push %%rcx
166+
\\ push %%rax
167+
\\ cmp $0x1000,%%rax
168+
\\ lea 24(%%rsp),%%rcx
169+
\\ jb 1f
170+
\\2:
171+
\\ sub $0x1000,%%rcx
172+
\\ test %%rcx,(%%rcx)
173+
\\ sub $0x1000,%%rax
174+
\\ cmp $0x1000,%%rax
175+
\\ ja 2b
176+
\\1:
177+
\\ sub %%rax,%%rcx
178+
\\ test %%rcx,(%%rcx)
179+
\\ pop %%rax
180+
\\ pop %%rcx
181+
\\ ret
169182
);
170183
unreachable;
171184
}

0 commit comments

Comments
 (0)