From 6a0c4289974dbe27d8980425e7e6061078ecf896 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Tue, 3 Oct 2017 00:57:02 -0400 Subject: [PATCH] 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 b5054625093ef22b3f228199b6fbf70e1c50b703 See #302 --- src/codegen.cpp | 12 ++++++++++-- std/special/compiler_rt/index.zig | 31 ++++++++++++++++++++++--------- 2 files changed, 32 insertions(+), 11 deletions(-) diff --git a/src/codegen.cpp b/src/codegen.cpp index efcf6ea3e3ef..5889ba316a78 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -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 = ""; diff --git a/std/special/compiler_rt/index.zig b/std/special/compiler_rt/index.zig index f27e3069f011..4a4d71fde959 100644 --- a/std/special/compiler_rt/index.zig +++ b/std/special/compiler_rt/index.zig @@ -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 @@ -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; @@ -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; }