[compiler-rt] [compiler-rt][ARM] Only use bxaut when the target has pacbti (PR #146057)

John Brawn via llvm-commits llvm-commits at lists.llvm.org
Fri Jun 27 03:49:21 PDT 2025


https://github.com/john-brawn-arm created https://github.com/llvm/llvm-project/pull/146057

Most pacbti instructions are a nop when the target does not have pacbti, and thus safe to execute, but bxaut is an undefined instruction. When we don't have pacbti (e.g. if we're compiling compiler-rt with -mbranch-protection=standard in order to be forward-compatible with pacbti while still working on targets without it) then we need to use separate aut and bx instructions.

>From 98e3ca09682a1b8cc00e2d8deeb9a4b5ca076a70 Mon Sep 17 00:00:00 2001
From: John Brawn <john.brawn at arm.com>
Date: Thu, 26 Jun 2025 16:47:03 +0100
Subject: [PATCH] [compiler-rt][ARM] Only use bxaut when the target has pacbti

Most pacbti instructions are a nop when the target does not have
pacbti, and thus safe to execute, but bxaut is an undefined
instruction. When we don't have pacbti (e.g. if we're compiling
compiler-rt with -mbranch-protection=standard in order to be
forward-compatible with pacbti while still working on targets without
it) then we need to use separate aut and bx instructions.
---
 compiler-rt/lib/builtins/arm/aeabi_cdcmp.S    | 2 +-
 compiler-rt/lib/builtins/arm/aeabi_cfcmp.S    | 2 +-
 compiler-rt/lib/builtins/arm/aeabi_dcmp.S     | 2 +-
 compiler-rt/lib/builtins/arm/aeabi_fcmp.S     | 2 +-
 compiler-rt/lib/builtins/arm/aeabi_idivmod.S  | 2 +-
 compiler-rt/lib/builtins/arm/aeabi_ldivmod.S  | 2 +-
 compiler-rt/lib/builtins/arm/aeabi_uidivmod.S | 2 +-
 compiler-rt/lib/builtins/arm/aeabi_uldivmod.S | 2 +-
 compiler-rt/lib/builtins/assembly.h           | 6 ++++++
 9 files changed, 14 insertions(+), 8 deletions(-)

diff --git a/compiler-rt/lib/builtins/arm/aeabi_cdcmp.S b/compiler-rt/lib/builtins/arm/aeabi_cdcmp.S
index 19311396b2015..0271afe7844f2 100644
--- a/compiler-rt/lib/builtins/arm/aeabi_cdcmp.S
+++ b/compiler-rt/lib/builtins/arm/aeabi_cdcmp.S
@@ -128,7 +128,7 @@ DEFINE_COMPILERRT_FUNCTION(__aeabi_cdcmple)
         msr APSR_nzcvq, ip
 #if defined(__ARM_FEATURE_PAC_DEFAULT)
         pop {r0-r3, r12, lr}
-        bxaut r12, lr, sp
+        PAC_RETURN
 #else
         pop {r0-r3}
         POP_PC()
diff --git a/compiler-rt/lib/builtins/arm/aeabi_cfcmp.S b/compiler-rt/lib/builtins/arm/aeabi_cfcmp.S
index ca382968cf5d9..5cb7d16042f42 100644
--- a/compiler-rt/lib/builtins/arm/aeabi_cfcmp.S
+++ b/compiler-rt/lib/builtins/arm/aeabi_cfcmp.S
@@ -128,7 +128,7 @@ DEFINE_COMPILERRT_FUNCTION(__aeabi_cfcmple)
         msr APSR_nzcvq, ip
 #if defined(__ARM_FEATURE_PAC_DEFAULT)
         pop {r0-r3, r12, lr}
-        bxaut r12, lr, sp
+        PAC_RETURN
 #else
         pop {r0-r3}
         POP_PC()
diff --git a/compiler-rt/lib/builtins/arm/aeabi_dcmp.S b/compiler-rt/lib/builtins/arm/aeabi_dcmp.S
index 4d1cc77d9a28b..2ed13789d2f38 100644
--- a/compiler-rt/lib/builtins/arm/aeabi_dcmp.S
+++ b/compiler-rt/lib/builtins/arm/aeabi_dcmp.S
@@ -29,7 +29,7 @@
 #  define PROLOGUE PACBTI_LANDING        SEPARATOR \
                    push      { r12, lr }
 #  define EPILOGUE pop       { r12, lr } SEPARATOR \
-                   bxaut     r12, lr, sp
+                   PAC_RETURN
 #elif defined(__ARM_FEATURE_BTI_DEFAULT)
 #  define PROLOGUE PACBTI_LANDING        SEPARATOR \
                    push      { r4, lr }
diff --git a/compiler-rt/lib/builtins/arm/aeabi_fcmp.S b/compiler-rt/lib/builtins/arm/aeabi_fcmp.S
index 190a111d3447a..0e3d399a552b4 100644
--- a/compiler-rt/lib/builtins/arm/aeabi_fcmp.S
+++ b/compiler-rt/lib/builtins/arm/aeabi_fcmp.S
@@ -29,7 +29,7 @@
 #  define PROLOGUE PACBTI_LANDING        SEPARATOR \
                    push      { r12, lr }
 #  define EPILOGUE pop       { r12, lr } SEPARATOR \
-                   bxaut     r12, lr, sp
+                   PAC_RETURN
 #elif defined(__ARM_FEATURE_BTI_DEFAULT)
 #  define PROLOGUE PACBTI_LANDING        SEPARATOR \
                    push      { r4, lr }
diff --git a/compiler-rt/lib/builtins/arm/aeabi_idivmod.S b/compiler-rt/lib/builtins/arm/aeabi_idivmod.S
index 4b82c946b1eb7..d95b1db0b459a 100644
--- a/compiler-rt/lib/builtins/arm/aeabi_idivmod.S
+++ b/compiler-rt/lib/builtins/arm/aeabi_idivmod.S
@@ -49,7 +49,7 @@ DEFINE_COMPILERRT_FUNCTION(__aeabi_idivmod)
         add     sp, sp, #4
 #if defined(__ARM_FEATURE_PAC_DEFAULT)
         pop     { r12, lr }
-        bxaut   r12, lr, sp
+        PAC_RETURN
 #else
         pop     { pc }
 #endif
diff --git a/compiler-rt/lib/builtins/arm/aeabi_ldivmod.S b/compiler-rt/lib/builtins/arm/aeabi_ldivmod.S
index c11ead68ff8fb..4fbfe3f1d2656 100644
--- a/compiler-rt/lib/builtins/arm/aeabi_ldivmod.S
+++ b/compiler-rt/lib/builtins/arm/aeabi_ldivmod.S
@@ -45,7 +45,7 @@ DEFINE_COMPILERRT_FUNCTION(__aeabi_ldivmod)
         add     sp, sp, #16
 #if defined(__ARM_FEATURE_PAC_DEFAULT)
         pop     {r6, r12, lr}
-        bxaut   r12, lr, sp
+        PAC_RETURN
 #else
         pop     {r6, pc}
 #endif
diff --git a/compiler-rt/lib/builtins/arm/aeabi_uidivmod.S b/compiler-rt/lib/builtins/arm/aeabi_uidivmod.S
index db2eecb3234f7..f77beab774115 100644
--- a/compiler-rt/lib/builtins/arm/aeabi_uidivmod.S
+++ b/compiler-rt/lib/builtins/arm/aeabi_uidivmod.S
@@ -56,7 +56,7 @@ LOCAL_LABEL(case_denom_larger):
         add     sp, sp, #4
 #if defined(__ARM_FEATURE_PAC_DEFAULT)
         pop     { r12, lr }
-        bxaut   r12, lr, sp
+        PAC_RETURN
 #else
         pop     { pc }
 #endif
diff --git a/compiler-rt/lib/builtins/arm/aeabi_uldivmod.S b/compiler-rt/lib/builtins/arm/aeabi_uldivmod.S
index 66da6a39ef5df..e019ba721b75d 100644
--- a/compiler-rt/lib/builtins/arm/aeabi_uldivmod.S
+++ b/compiler-rt/lib/builtins/arm/aeabi_uldivmod.S
@@ -45,7 +45,7 @@ DEFINE_COMPILERRT_FUNCTION(__aeabi_uldivmod)
         add	sp, sp, #16
 #if defined(__ARM_FEATURE_PAC_DEFAULT)
         pop     {r6, r12, lr}
-        bxaut   r12, lr, sp
+        PAC_RETURN
 #else
         pop     {r6, pc}
 #endif
diff --git a/compiler-rt/lib/builtins/assembly.h b/compiler-rt/lib/builtins/assembly.h
index 714bcd4d5c1f6..89372f18c84b2 100644
--- a/compiler-rt/lib/builtins/assembly.h
+++ b/compiler-rt/lib/builtins/assembly.h
@@ -205,6 +205,12 @@
 #define PACBTI_LANDING
 #endif
 
+#if defined(__ARM_FEATURE_PAUTH)
+#define PAC_RETURN bxaut r12, lr, sp
+#else
+#define PAC_RETURN aut r12, lr, sp SEPARATOR bx lr
+#endif
+
 #else // !defined(__arm)
 #define DECLARE_FUNC_ENCODING
 #define DEFINE_CODE_STATE



More information about the llvm-commits mailing list