Skip to content

Commit 825706b

Browse files
committed
Revert "[compiler-rt] [builtins] Remove unused/misnamed x86 chkstk functions"
This reverts parts of commit 885d7b7, 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.
1 parent d2f0b27 commit 825706b

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed

compiler-rt/lib/builtins/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,7 @@ if (NOT MSVC)
388388
if (WIN32)
389389
set(i386_SOURCES
390390
${i386_SOURCES}
391+
i386/chkstk.S
391392
i386/chkstk2.S
392393
)
393394
endif()
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
2+
// See https://llvm.org/LICENSE.txt for license information.
3+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4+
5+
#include "../assembly.h"
6+
7+
// _chkstk routine
8+
// This routine is windows specific
9+
// http://msdn.microsoft.com/en-us/library/ms648426.aspx
10+
//
11+
// This function does not decrement %esp at the end.
12+
13+
// GCC after 4.6 generates calls to "___chkstk_ms". For other variants of
14+
// this function, which do decrement %esp, see chkstk2.S.
15+
16+
#ifdef __i386__
17+
18+
.text
19+
.balign 4
20+
DEFINE_COMPILERRT_FUNCTION(__chkstk_ms)
21+
push %ecx
22+
push %eax
23+
cmp $0x1000,%eax
24+
lea 12(%esp),%ecx
25+
jb 1f
26+
2:
27+
sub $0x1000,%ecx
28+
test %ecx,(%ecx)
29+
sub $0x1000,%eax
30+
cmp $0x1000,%eax
31+
ja 2b
32+
1:
33+
sub %eax,%ecx
34+
test %ecx,(%ecx)
35+
pop %eax
36+
pop %ecx
37+
ret
38+
END_COMPILERRT_FUNCTION(__chkstk_ms)
39+
40+
#endif // __i386__

compiler-rt/lib/builtins/i386/chkstk2.S

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,26 @@
1111
// This routine is windows specific
1212
// http://msdn.microsoft.com/en-us/library/ms648426.aspx
1313

14+
// Clang on i386 mingw generates calls to "_alloca" (which gets decorated to
15+
// "__alloca").
16+
//
17+
// GCC before 4.6 generated calls a symbol which after decoration is named
18+
// "___chkstk", with three leading underscores. We provide that here as well.
19+
//
20+
// MSVC produces calls to the symbol "__chkstk", with two leading underscores.
21+
// That one has the same signature as this one - but we don't provide that
22+
// symbol here. (If we'd do that, we should do it in a separate object file
23+
// to avoid potential symbol collisions - see
24+
// commit 248aeac1ad2cf4f583490dd1312a5b448d2bb8cc for details.)
25+
//
26+
// GCC after 4.6 generates calls to "___chkstk_ms", which does not decrement
27+
// %esp - that function is defined in chkstk.S.
28+
1429
.text
1530
.balign 4
1631
DEFINE_COMPILERRT_FUNCTION(_alloca) // _chkstk and _alloca are the same function
32+
// This gets decorated into "___chkstk"; GCC < 4.6 references this symbol.
33+
DEFINE_COMPILERRT_FUNCTION(__chkstk)
1734
push %ecx
1835
cmp $0x1000,%eax
1936
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
3451
push (%eax) // push return address onto the stack
3552
sub %esp,%eax // restore the original value in eax
3653
ret
54+
END_COMPILERRT_FUNCTION(__chkstk)
3755
END_COMPILERRT_FUNCTION(_alloca)
3856

3957
#endif // __i386__

0 commit comments

Comments
 (0)