[compiler-rt] Revert "[compiler-rt] [builtins] Remove unused/misnamed x86 chkstk functions" (PR #167303)

Martin Storsjö via llvm-commits llvm-commits at lists.llvm.org
Mon Nov 10 03:45:33 PST 2025


https://github.com/mstorsjo created https://github.com/llvm/llvm-project/pull/167303

This reverts parts of commit 885d7b759b5c166c07c07f4c58c6e0ba110fb0c2, and adds verbose comments explaining all the variants of this function, for clarity for future readers.

It turns out that those functions actually weren't misnamed or unused after all: Apparently Clang doesn't match GCC when it comes to what stack probe function is referenced on i386 mingw. GCC < 4.6 references a symbol named "___chkstk", with three leading underscores, and GCC >= 4.6 references "___chkstk_ms".

Restore these functions, to allow linking object files built with GCC with compiler-rt.


>From 8477fa0475d9e7cdde6f0c248d60ece99418991b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Martin=20Storsj=C3=B6?= <martin at martin.st>
Date: Mon, 10 Nov 2025 13:30:49 +0200
Subject: [PATCH 1/2] Revert "[compiler-rt] Rename the now lone i386/chkstk2.S
 to i386/chkstk.S"

This reverts commit 1f9eff100ce8faea1284d68b779d844c6e019b77.

This is done in preparation of reverting parts of
885d7b759b5c166c07c07f4c58c6e0ba110fb0c2.
---
 compiler-rt/lib/builtins/CMakeLists.txt               | 2 +-
 compiler-rt/lib/builtins/i386/{chkstk.S => chkstk2.S} | 0
 2 files changed, 1 insertion(+), 1 deletion(-)
 rename compiler-rt/lib/builtins/i386/{chkstk.S => chkstk2.S} (100%)

diff --git a/compiler-rt/lib/builtins/CMakeLists.txt b/compiler-rt/lib/builtins/CMakeLists.txt
index 6c226aa7d2d48..8a80b95ac31ab 100644
--- a/compiler-rt/lib/builtins/CMakeLists.txt
+++ b/compiler-rt/lib/builtins/CMakeLists.txt
@@ -388,7 +388,7 @@ if (NOT MSVC)
   if (WIN32)
     set(i386_SOURCES
       ${i386_SOURCES}
-      i386/chkstk.S
+      i386/chkstk2.S
     )
   endif()
 else () # MSVC
diff --git a/compiler-rt/lib/builtins/i386/chkstk.S b/compiler-rt/lib/builtins/i386/chkstk2.S
similarity index 100%
rename from compiler-rt/lib/builtins/i386/chkstk.S
rename to compiler-rt/lib/builtins/i386/chkstk2.S

>From df5bfadc0f78667255a958d57a7794a931696fb6 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Martin=20Storsj=C3=B6?= <martin at martin.st>
Date: Mon, 10 Nov 2025 13:32:58 +0200
Subject: [PATCH 2/2] Revert "[compiler-rt] [builtins] Remove unused/misnamed
 x86 chkstk functions"

This reverts parts of commit 885d7b759b5c166c07c07f4c58c6e0ba110fb0c2,
and adds verbose comments explaining all the variants of this
function, for clarity for future readers.

It turns out that those functions actually weren't misnamed or
unused after all: Apparently Clang doesn't match GCC when it comes
to what stack probe function is referenced on i386 mingw. GCC < 4.6
references a symbol named "___chkstk", with three leading underscores,
and GCC >= 4.6 references "___chkstk_ms".

Restore these functions, to allow linking object files built with
GCC with compiler-rt.
---
 compiler-rt/lib/builtins/CMakeLists.txt |  1 +
 compiler-rt/lib/builtins/i386/chkstk.S  | 40 +++++++++++++++++++++++++
 compiler-rt/lib/builtins/i386/chkstk2.S | 18 +++++++++++
 3 files changed, 59 insertions(+)
 create mode 100644 compiler-rt/lib/builtins/i386/chkstk.S

diff --git a/compiler-rt/lib/builtins/CMakeLists.txt b/compiler-rt/lib/builtins/CMakeLists.txt
index 8a80b95ac31ab..02e6ecfbdb60e 100644
--- a/compiler-rt/lib/builtins/CMakeLists.txt
+++ b/compiler-rt/lib/builtins/CMakeLists.txt
@@ -388,6 +388,7 @@ if (NOT MSVC)
   if (WIN32)
     set(i386_SOURCES
       ${i386_SOURCES}
+      i386/chkstk.S
       i386/chkstk2.S
     )
   endif()
diff --git a/compiler-rt/lib/builtins/i386/chkstk.S b/compiler-rt/lib/builtins/i386/chkstk.S
new file mode 100644
index 0000000000000..8ae7d39d66aba
--- /dev/null
+++ b/compiler-rt/lib/builtins/i386/chkstk.S
@@ -0,0 +1,40 @@
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+
+#include "../assembly.h"
+
+// _chkstk routine
+// This routine is windows specific
+// http://msdn.microsoft.com/en-us/library/ms648426.aspx
+//
+// This function does not decrement %esp at the end.
+
+// GCC after 4.6 generates calls to "___chkstk_ms". For other variants of
+// this function, which do decrement %esp, see chkstk2.S.
+
+#ifdef __i386__
+
+.text
+.balign 4
+DEFINE_COMPILERRT_FUNCTION(__chkstk_ms)
+        push   %ecx
+        push   %eax
+        cmp    $0x1000,%eax
+        lea    12(%esp),%ecx
+        jb     1f
+2:
+        sub    $0x1000,%ecx
+        test   %ecx,(%ecx)
+        sub    $0x1000,%eax
+        cmp    $0x1000,%eax
+        ja     2b
+1:
+        sub    %eax,%ecx
+        test   %ecx,(%ecx)
+        pop    %eax
+        pop    %ecx
+        ret
+END_COMPILERRT_FUNCTION(__chkstk_ms)
+
+#endif // __i386__
diff --git a/compiler-rt/lib/builtins/i386/chkstk2.S b/compiler-rt/lib/builtins/i386/chkstk2.S
index cdd9a4c2a5752..034b6edc6f1a4 100644
--- a/compiler-rt/lib/builtins/i386/chkstk2.S
+++ b/compiler-rt/lib/builtins/i386/chkstk2.S
@@ -11,9 +11,26 @@
 // This routine is windows specific
 // http://msdn.microsoft.com/en-us/library/ms648426.aspx
 
+// Clang on i386 mingw generates calls to "_alloca" (which gets decorated to
+// "__alloca").
+//
+// GCC before 4.6 generated calls a symbol which after decoration is named
+// "___chkstk", with three leading underscores. We provide that here as well.
+//
+// MSVC produces calls to the symbol "__chkstk", with two leading underscores.
+// That one has the same signature as this one - but we don't provide that
+// symbol here. (If we'd do that, we should do it in a separate object file
+// to avoid potential symbol collisions - see
+// commit 248aeac1ad2cf4f583490dd1312a5b448d2bb8cc for details.)
+//
+// GCC after 4.6 generates calls to "___chkstk_ms", which does not decrement
+// %esp - that function is defined in chkstk.S.
+
 .text
 .balign 4
 DEFINE_COMPILERRT_FUNCTION(_alloca) // _chkstk and _alloca are the same function
+// This gets decorated into "___chkstk"; GCC < 4.6 references this symbol.
+DEFINE_COMPILERRT_FUNCTION(__chkstk)
         push   %ecx
         cmp    $0x1000,%eax
         lea    8(%esp),%ecx     // esp before calling this routine -> ecx
@@ -34,6 +51,7 @@ DEFINE_COMPILERRT_FUNCTION(_alloca) // _chkstk and _alloca are the same function
         push   (%eax)           // push return address onto the stack
         sub    %esp,%eax        // restore the original value in eax
         ret
+END_COMPILERRT_FUNCTION(__chkstk)
 END_COMPILERRT_FUNCTION(_alloca)
 
 #endif // __i386__



More information about the llvm-commits mailing list