Skip to content

Commit

Permalink
use __chkstk_ms compiler-rt functions for __chkstk
Browse files Browse the repository at this point in the history
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
  • Loading branch information
andrewrk committed Oct 3, 2017
1 parent b505462 commit 6a0c428
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 11 deletions.
12 changes: 10 additions & 2 deletions src/codegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5008,8 +5008,16 @@ static void init(CodeGen *g) {
const char *target_specific_cpu_args;
const char *target_specific_features;
if (g->is_native_target) {
target_specific_cpu_args = ZigLLVMGetHostCPUName();
target_specific_features = ZigLLVMGetNativeFeatures();
// LLVM creates invalid binaries on Windows sometimes.
// See https://github.com/zig-lang/zig/issues/508
// As a workaround we do not use target native features on Windows.
if (g->zig_target.os == ZigLLVM_Win32) {
target_specific_cpu_args = "";
target_specific_features = "";
} else {
target_specific_cpu_args = ZigLLVMGetHostCPUName();
target_specific_features = ZigLLVMGetNativeFeatures();
}
} else {
target_specific_cpu_args = "";
target_specific_features = "";
Expand Down
31 changes: 22 additions & 9 deletions std/special/compiler_rt/index.zig
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,9 @@ export nakedcc fn _chkstk() align(4) {
@setGlobalLinkage(_chkstk, strong_linkage);
asm volatile (
\\ push %%ecx
\\ push %%eax
\\ cmp $0x1000,%%eax
\\ lea 8(%%esp),%%ecx // esp before calling this routine -> ecx
\\ lea 12(%%esp),%%ecx
\\ jb 1f
\\ 2:
\\ sub $0x1000,%%ecx
Expand All @@ -141,12 +142,8 @@ export nakedcc fn _chkstk() align(4) {
\\ 1:
\\ sub %%eax,%%ecx
\\ test %%ecx,(%%ecx)
\\
\\ lea 4(%%esp),%%eax // load pointer to the return address into eax
\\ mov %%ecx,%%esp // install the new top of stack pointer into esp
\\ mov -4(%%eax),%%ecx // restore ecx
\\ push (%%eax) // push return address onto the stack
\\ sub %%esp,%%eax // restore the original value in eax
\\ pop %%eax
\\ pop %%ecx
\\ ret
);
unreachable;
Expand All @@ -159,13 +156,29 @@ export nakedcc fn _chkstk() align(4) {
// the implementation from disassembled ntdll seems to depend on
// thread local storage. So we have given up this safety check
// and simply have `ret`.
export nakedcc fn __chkstk() align(8) {
export nakedcc fn __chkstk() align(4) {
@setDebugSafety(this, false);

if (win64_nocrt) {
@setGlobalLinkage(__chkstk, strong_linkage);
asm volatile (
\\ ret
\\ push %%rcx
\\ push %%rax
\\ cmp $0x1000,%%rax
\\ lea 24(%%rsp),%%rcx
\\ jb 1f
\\2:
\\ sub $0x1000,%%rcx
\\ test %%rcx,(%%rcx)
\\ sub $0x1000,%%rax
\\ cmp $0x1000,%%rax
\\ ja 2b
\\1:
\\ sub %%rax,%%rcx
\\ test %%rcx,(%%rcx)
\\ pop %%rax
\\ pop %%rcx
\\ ret
);
unreachable;
}
Expand Down

0 comments on commit 6a0c428

Please sign in to comment.