Commit 6a0c428997

Andrew Kelley <superjoe30@gmail.com>
2017-10-03 06:57:02
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
1 parent b505462
Changed files (2)
src
std
special
compiler_rt
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 = "";
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;
     }