From c0a6edcf3b1a74c5946bfdfa0312106cc900b764 Mon Sep 17 00:00:00 2001 From: Ryan Prichard Date: Mon, 14 Mar 2022 16:00:16 -0700 Subject: [ARM] __cxa_end_cleanup: avoid clobbering r4 The fix for D111703 clobbered r4 both to: - Save/restore the original lr. - Load the address of _Unwind_Resume for LIBCXXABI_BAREMETAL. This patch saves and restores lr without clobbering any extra registers. For LIBCXXABI_BAREMETAL, it is still necessary to clobber one extra register to hold the address of _Unwind_Resume, but it seems better to use ip/r12 (intended for linker veneers/trampolines) than r4 for this purpose. The function also clobbers r0 for the _Unwind_Resume function's parameter, but that is unavoidable. Differential Revision: https://reviews.llvm.org/D121432 Bug: http://b/222341313 Bug: http://b/222364496 Test: device boots Change-Id: I4dd6452d705b49f595c9d600d01098e5325b0571 --- src/cxa_exception.cpp | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/src/cxa_exception.cpp b/src/cxa_exception.cpp index 8d30e5c..9e650b5 100644 --- a/src/cxa_exception.cpp +++ b/src/cxa_exception.cpp @@ -343,8 +343,11 @@ unwinding with _Unwind_Resume. 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(). The function also saves/restores r4 to +keep the stack aligned and to provide a temp register. _Unwind_Resume never +returns and we need to keep the original lr so just branch to it. When +targeting bare metal, the function also clobbers ip/r12 to hold the address of +_Unwind_Resume, which may be too far away for an ordinary branch. */ __attribute__((used)) static _Unwind_Exception * __cxa_end_cleanup_impl() @@ -374,20 +377,30 @@ __cxa_end_cleanup_impl() 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" +#if defined(__ARM_FEATURE_BTI_DEFAULT) + " bti\n" +#endif " push {r1, r2, r3, r4}\n" + " mov r4, lr\n" " bl __cxa_end_cleanup_impl\n" + " mov lr, r4\n" +#if defined(LIBCXXABI_BAREMETAL) + " ldr r4, =_Unwind_Resume\n" + " mov ip, r4\n" +#endif " pop {r1, r2, r3, r4}\n" - " bl _Unwind_Resume\n" - " bl abort\n" - " .popsection" -); -#endif // defined(_LIBCXXABI_ARM_EHABI) - +#if defined(LIBCXXABI_BAREMETAL) + " bx ip\n" +#else + " b _Unwind_Resume\n" +#endif + " .popsection"); +#endif // defined(_LIBCXXABI_ARM_EHABI) + /* This routine can catch foreign or native exceptions. If native, the exception can be a primary or dependent variety. This routine may remain blissfully -- cgit v1.2.3