Reland "[ARM] __cxa_end_cleanup should be called instead of _UnwindResume."
This is relanding commit da1d1a08694bbfe0ea7a23ea094612436e8a2dd0 .
This patch additionally addresses failures found in buildbots & post review comments.
ARM EHABI[1] specifies the __cxa_end_cleanup to be called after cleanup.
It will call the UnwindResume.
__cxa_begin_cleanup will be called from libcxxabi while __cxa_end_cleanup is never called.
This will trigger a termination when a foreign exception is processed while UnwindResume is called
because the global state will be wrong due to the missing __cxa_end_cleanup call.
Additional test here: D109856
[1] https://github.com/ARM-software/abi-aa/blob/main/ehabi32/ehabi32.rst#941compiler-helper-functions
Reviewed By: logan
Differential Revision: https://reviews.llvm.org/D111703
GitOrigin-RevId: b6420e575f3bbb6b6df848c0284d6b60eeb07350
diff --git a/src/cxa_exception.cpp b/src/cxa_exception.cpp
index 35956f1..7044cb9 100644
--- a/src/cxa_exception.cpp
+++ b/src/cxa_exception.cpp
@@ -341,8 +341,10 @@
According to ARM EHABI 8.4.1, __cxa_end_cleanup() should not clobber any
register, thus we have to write this function in assembly so that we can save
{r1, r2, r3}. We don't have to save r0 because it is the return value and the
-first argument to _Unwind_Resume(). In addition, we are saving r4 in order to
-align the stack to 16 bytes, even though it is a callee-save register.
+first argument to _Unwind_Resume(). In addition, we are saving lr in order to
+align the stack to 16 bytes and lr will be used to identify the caller and its
+frame information. _Unwind_Resume never return and we need to keep the original
+lr so just branch to it.
*/
__attribute__((used)) static _Unwind_Exception *
__cxa_end_cleanup_impl()
@@ -372,18 +374,16 @@
return &exception_header->unwindHeader;
}
-asm (
- " .pushsection .text.__cxa_end_cleanup,\"ax\",%progbits\n"
+asm(" .pushsection .text.__cxa_end_cleanup,\"ax\",%progbits\n"
" .globl __cxa_end_cleanup\n"
" .type __cxa_end_cleanup,%function\n"
"__cxa_end_cleanup:\n"
- " push {r1, r2, r3, r4}\n"
+ " push {r1, r2, r3, lr}\n"
" bl __cxa_end_cleanup_impl\n"
" pop {r1, r2, r3, r4}\n"
- " bl _Unwind_Resume\n"
- " bl abort\n"
- " .popsection"
-);
+ " mov lr, r4\n"
+ " b _Unwind_Resume\n"
+ " .popsection");
#endif // defined(_LIBCXXABI_ARM_EHABI)
/*