Skip to content

Commit a59a8ae

Browse files
authored
[compiler-rt][ARM] Add missing PACBTI support to assembly aeabi functions (#142400)
Some of the aeabi functions were missing PACBTI support. The lack of it resulted in exceptions at runtime if the running environment had PAC and/or BTI enabled. This patch adds this support. This involves the addition of PACBTI instructions, depending on whether each of these features is enabled, plus the saving and restoring of the PAC code that resides in r12. Some of the common code has been put in preprocessor macros to reduce duplication, but not all, especially since some register saving and restoring is very specific to each context.
1 parent 902d689 commit a59a8ae

File tree

12 files changed

+149
-12
lines changed

12 files changed

+149
-12
lines changed

compiler-rt/lib/builtins/arm/aeabi_cdcmp.S

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,12 @@
2222
.syntax unified
2323
.p2align 2
2424
DEFINE_COMPILERRT_FUNCTION(__aeabi_cdcmpeq)
25+
PACBTI_LANDING
26+
#if defined(__ARM_FEATURE_PAC_DEFAULT)
27+
push {r0-r3, r12, lr}
28+
#else
2529
push {r0-r3, lr}
30+
#endif
2631
bl __aeabi_cdcmpeq_check_nan
2732
cmp r0, #1
2833
#if defined(USE_THUMB_1)
@@ -37,8 +42,13 @@ DEFINE_COMPILERRT_FUNCTION(__aeabi_cdcmpeq)
3742
movs r0, #0xF
3843
lsls r0, r0, #31
3944
pop {r0-r3, pc}
45+
#else
46+
#if defined(__ARM_FEATURE_PAC_DEFAULT)
47+
pop {r0-r3, r12, lr}
48+
aut r12, lr, sp
4049
#else
4150
pop {r0-r3, lr}
51+
#endif
4252

4353
// NaN has been ruled out, so __aeabi_cdcmple can't trap
4454
// Use "it ne" + unconditional branch to guarantee a supported relocation if
@@ -47,8 +57,8 @@ DEFINE_COMPILERRT_FUNCTION(__aeabi_cdcmpeq)
4757
bne __aeabi_cdcmple
4858

4959
#if defined(USE_THUMB_2)
50-
mov ip, #APSR_C
51-
msr APSR_nzcvq, ip
60+
mov r12, #APSR_C
61+
msr APSR_nzcvq, r12
5262
#else
5363
msr APSR_nzcvq, #APSR_C
5464
#endif
@@ -70,9 +80,14 @@ END_COMPILERRT_FUNCTION(__aeabi_cdcmpeq)
7080
.syntax unified
7181
.p2align 2
7282
DEFINE_COMPILERRT_FUNCTION(__aeabi_cdcmple)
83+
PACBTI_LANDING
7384
// Per the RTABI, this function must preserve r0-r11.
7485
// Save lr in the same instruction for compactness
86+
#if defined(__ARM_FEATURE_PAC_DEFAULT)
87+
push {r0-r3, r12, lr}
88+
#else
7589
push {r0-r3, lr}
90+
#endif
7691

7792
bl __aeabi_dcmplt
7893
cmp r0, #1
@@ -111,9 +126,14 @@ DEFINE_COMPILERRT_FUNCTION(__aeabi_cdcmple)
111126

112127
1:
113128
msr APSR_nzcvq, ip
129+
#if defined(__ARM_FEATURE_PAC_DEFAULT)
130+
pop {r0-r3, r12, lr}
131+
bxaut r12, lr, sp
132+
#else
114133
pop {r0-r3}
115134
POP_PC()
116135
#endif
136+
#endif
117137
END_COMPILERRT_FUNCTION(__aeabi_cdcmple)
118138

119139
// int __aeabi_cdrcmple(double a, double b) {
@@ -123,6 +143,9 @@ END_COMPILERRT_FUNCTION(__aeabi_cdcmple)
123143
.syntax unified
124144
.p2align 2
125145
DEFINE_COMPILERRT_FUNCTION(__aeabi_cdrcmple)
146+
#if defined(__ARM_FEATURE_BTI_DEFAULT)
147+
bti
148+
#endif
126149
// Swap r0 and r2
127150
mov ip, r0
128151
mov r0, r2

compiler-rt/lib/builtins/arm/aeabi_cfcmp.S

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,12 @@
2222
.syntax unified
2323
.p2align 2
2424
DEFINE_COMPILERRT_FUNCTION(__aeabi_cfcmpeq)
25+
PACBTI_LANDING
26+
#if defined(__ARM_FEATURE_PAC_DEFAULT)
27+
push {r0-r3, r12, lr}
28+
#else
2529
push {r0-r3, lr}
30+
#endif
2631
bl __aeabi_cfcmpeq_check_nan
2732
cmp r0, #1
2833
#if defined(USE_THUMB_1)
@@ -37,8 +42,13 @@ DEFINE_COMPILERRT_FUNCTION(__aeabi_cfcmpeq)
3742
movs r0, #0xF
3843
lsls r0, r0, #31
3944
pop {r0-r3, pc}
45+
#else
46+
#if defined(__ARM_FEATURE_PAC_DEFAULT)
47+
pop {r0-r3, r12, lr}
48+
aut r12, lr, sp
4049
#else
4150
pop {r0-r3, lr}
51+
#endif
4252

4353
// NaN has been ruled out, so __aeabi_cfcmple can't trap
4454
// Use "it ne" + unconditional branch to guarantee a supported relocation if
@@ -47,8 +57,8 @@ DEFINE_COMPILERRT_FUNCTION(__aeabi_cfcmpeq)
4757
bne __aeabi_cfcmple
4858

4959
#if defined(USE_THUMB_2)
50-
mov ip, #APSR_C
51-
msr APSR_nzcvq, ip
60+
mov r12, #APSR_C
61+
msr APSR_nzcvq, r12
5262
#else
5363
msr APSR_nzcvq, #APSR_C
5464
#endif
@@ -70,9 +80,14 @@ END_COMPILERRT_FUNCTION(__aeabi_cfcmpeq)
7080
.syntax unified
7181
.p2align 2
7282
DEFINE_COMPILERRT_FUNCTION(__aeabi_cfcmple)
83+
PACBTI_LANDING
7384
// Per the RTABI, this function must preserve r0-r11.
7485
// Save lr in the same instruction for compactness
86+
#if defined(__ARM_FEATURE_PAC_DEFAULT)
87+
push {r0-r3, r12, lr}
88+
#else
7589
push {r0-r3, lr}
90+
#endif
7691

7792
bl __aeabi_fcmplt
7893
cmp r0, #1
@@ -111,9 +126,14 @@ DEFINE_COMPILERRT_FUNCTION(__aeabi_cfcmple)
111126

112127
1:
113128
msr APSR_nzcvq, ip
129+
#if defined(__ARM_FEATURE_PAC_DEFAULT)
130+
pop {r0-r3, r12, lr}
131+
bxaut r12, lr, sp
132+
#else
114133
pop {r0-r3}
115134
POP_PC()
116135
#endif
136+
#endif
117137
END_COMPILERRT_FUNCTION(__aeabi_cfcmple)
118138

119139
// int __aeabi_cfrcmple(float a, float b) {
@@ -123,6 +143,9 @@ END_COMPILERRT_FUNCTION(__aeabi_cfcmple)
123143
.syntax unified
124144
.p2align 2
125145
DEFINE_COMPILERRT_FUNCTION(__aeabi_cfrcmple)
146+
#if defined(__ARM_FEATURE_BTI_DEFAULT)
147+
bti
148+
#endif
126149
// Swap r0 and r1
127150
mov ip, r0
128151
mov r0, r1

compiler-rt/lib/builtins/arm/aeabi_dcmp.S

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,34 @@
2525
# define CONVERT_DCMP_ARGS_TO_DF2_ARGS
2626
#endif
2727

28+
#if defined(__ARM_FEATURE_PAC_DEFAULT)
29+
# define PROLOGUE PACBTI_LANDING SEPARATOR \
30+
push { r12, lr }
31+
# define EPILOGUE pop { r12, lr } SEPARATOR \
32+
bxaut r12, lr, sp
33+
#elif defined(__ARM_FEATURE_BTI_DEFAULT)
34+
# define PROLOGUE PACBTI_LANDING SEPARATOR \
35+
push { r4, lr }
36+
# define EPILOGUE pop { r4, pc }
37+
#else
38+
# define PROLOGUE push { r4, lr }
39+
# define EPILOGUE pop { r4, pc }
40+
#endif
41+
2842
#define DEFINE_AEABI_DCMP(cond) \
2943
.syntax unified SEPARATOR \
3044
.p2align 2 SEPARATOR \
3145
DEFINE_COMPILERRT_FUNCTION(__aeabi_dcmp ## cond) \
32-
push { r4, lr } SEPARATOR \
46+
PROLOGUE SEPARATOR \
3347
CONVERT_DCMP_ARGS_TO_DF2_ARGS SEPARATOR \
3448
bl SYMBOL_NAME(__ ## cond ## df2) SEPARATOR \
3549
cmp r0, #0 SEPARATOR \
3650
b ## cond 1f SEPARATOR \
3751
movs r0, #0 SEPARATOR \
38-
pop { r4, pc } SEPARATOR \
52+
EPILOGUE SEPARATOR \
3953
1: SEPARATOR \
4054
movs r0, #1 SEPARATOR \
41-
pop { r4, pc } SEPARATOR \
55+
EPILOGUE SEPARATOR \
4256
END_COMPILERRT_FUNCTION(__aeabi_dcmp ## cond)
4357

4458
DEFINE_AEABI_DCMP(eq)

compiler-rt/lib/builtins/arm/aeabi_fcmp.S

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,34 @@
2525
# define CONVERT_FCMP_ARGS_TO_SF2_ARGS
2626
#endif
2727

28+
#if defined(__ARM_FEATURE_PAC_DEFAULT)
29+
# define PROLOGUE PACBTI_LANDING SEPARATOR \
30+
push { r12, lr }
31+
# define EPILOGUE pop { r12, lr } SEPARATOR \
32+
bxaut r12, lr, sp
33+
#elif defined(__ARM_FEATURE_BTI_DEFAULT)
34+
# define PROLOGUE PACBTI_LANDING SEPARATOR \
35+
push { r4, lr }
36+
# define EPILOGUE pop { r4, pc }
37+
#else
38+
# define PROLOGUE push { r4, lr }
39+
# define EPILOGUE pop { r4, pc }
40+
#endif
41+
2842
#define DEFINE_AEABI_FCMP(cond) \
2943
.syntax unified SEPARATOR \
3044
.p2align 2 SEPARATOR \
3145
DEFINE_COMPILERRT_FUNCTION(__aeabi_fcmp ## cond) \
32-
push { r4, lr } SEPARATOR \
46+
PROLOGUE SEPARATOR \
3347
CONVERT_FCMP_ARGS_TO_SF2_ARGS SEPARATOR \
3448
bl SYMBOL_NAME(__ ## cond ## sf2) SEPARATOR \
3549
cmp r0, #0 SEPARATOR \
3650
b ## cond 1f SEPARATOR \
3751
movs r0, #0 SEPARATOR \
38-
pop { r4, pc } SEPARATOR \
52+
EPILOGUE SEPARATOR \
3953
1: SEPARATOR \
4054
movs r0, #1 SEPARATOR \
41-
pop { r4, pc } SEPARATOR \
55+
EPILOGUE SEPARATOR \
4256
END_COMPILERRT_FUNCTION(__aeabi_fcmp ## cond)
4357

4458
DEFINE_AEABI_FCMP(eq)

compiler-rt/lib/builtins/arm/aeabi_idivmod.S

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,12 @@ DEFINE_COMPILERRT_FUNCTION(__aeabi_idivmod)
3131
subs r1, r1, r2
3232
JMP (r3)
3333
#else // defined(USE_THUMB_1)
34+
PACBTI_LANDING
35+
#if defined(__ARM_FEATURE_PAC_DEFAULT)
36+
push { r12, lr }
37+
#else
3438
push { lr }
39+
#endif
3540
sub sp, sp, #4
3641
mov r2, sp
3742
#if defined(__MINGW32__)
@@ -42,7 +47,12 @@ DEFINE_COMPILERRT_FUNCTION(__aeabi_idivmod)
4247
bl SYMBOL_NAME(__divmodsi4)
4348
ldr r1, [sp]
4449
add sp, sp, #4
50+
#if defined(__ARM_FEATURE_PAC_DEFAULT)
51+
pop { r12, lr }
52+
bxaut r12, lr, sp
53+
#else
4554
pop { pc }
55+
#endif
4656
#endif // defined(USE_THUMB_1)
4757
END_COMPILERRT_FUNCTION(__aeabi_idivmod)
4858

compiler-rt/lib/builtins/arm/aeabi_ldivmod.S

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,12 @@
2222
.syntax unified
2323
.p2align 2
2424
DEFINE_COMPILERRT_FUNCTION(__aeabi_ldivmod)
25+
PACBTI_LANDING
26+
#if defined(__ARM_FEATURE_PAC_DEFAULT)
27+
push {r6, r12, lr}
28+
#else
2529
push {r6, lr}
30+
#endif
2631
sub sp, sp, #16
2732
add r6, sp, #8
2833
str r6, [sp]
@@ -38,7 +43,12 @@ DEFINE_COMPILERRT_FUNCTION(__aeabi_ldivmod)
3843
ldr r2, [sp, #8]
3944
ldr r3, [sp, #12]
4045
add sp, sp, #16
46+
#if defined(__ARM_FEATURE_PAC_DEFAULT)
47+
pop {r6, r12, lr}
48+
bxaut r12, lr, sp
49+
#else
4150
pop {r6, pc}
51+
#endif
4252
END_COMPILERRT_FUNCTION(__aeabi_ldivmod)
4353

4454
NO_EXEC_STACK_DIRECTIVE

compiler-rt/lib/builtins/arm/aeabi_memcpy.S

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ DEFINE_COMPILERRT_FUNCTION(__aeabi_memcpy)
1818
bl memcpy
1919
pop {r7, pc}
2020
#else
21+
#if defined(__ARM_FEATURE_BTI_DEFAULT)
22+
bti
23+
#endif
2124
b memcpy
2225
#endif
2326
END_COMPILERRT_FUNCTION(__aeabi_memcpy)

compiler-rt/lib/builtins/arm/aeabi_memmove.S

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ DEFINE_COMPILERRT_FUNCTION(__aeabi_memmove)
1717
bl memmove
1818
pop {r7, pc}
1919
#else
20+
#if defined(__ARM_FEATURE_BTI_DEFAULT)
21+
bti
22+
#endif
2023
b memmove
2124
#endif
2225
END_COMPILERRT_FUNCTION(__aeabi_memmove)

compiler-rt/lib/builtins/arm/aeabi_memset.S

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
.syntax unified
1515
.p2align 2
1616
DEFINE_COMPILERRT_FUNCTION(__aeabi_memset)
17+
#if defined(__ARM_FEATURE_BTI_DEFAULT)
18+
bti
19+
#endif
1720
mov r3, r1
1821
mov r1, r2
1922
mov r2, r3
@@ -31,6 +34,9 @@ DEFINE_AEABI_FUNCTION_ALIAS(__aeabi_memset8, __aeabi_memset)
3134

3235
.p2align 2
3336
DEFINE_COMPILERRT_FUNCTION(__aeabi_memclr)
37+
#if defined(__ARM_FEATURE_BTI_DEFAULT)
38+
bti
39+
#endif
3440
mov r2, r1
3541
movs r1, #0
3642
#ifdef USE_THUMB_1

compiler-rt/lib/builtins/arm/aeabi_uidivmod.S

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,12 @@ LOCAL_LABEL(case_denom_larger):
3838
movs r0, #0
3939
JMP (lr)
4040
#else // defined(USE_THUMB_1)
41+
PACBTI_LANDING
42+
#if defined(__ARM_FEATURE_PAC_DEFAULT)
43+
push { r12, lr }
44+
#else
4145
push { lr }
46+
#endif
4247
sub sp, sp, #4
4348
mov r2, sp
4449
#if defined(__MINGW32__)
@@ -49,8 +54,13 @@ LOCAL_LABEL(case_denom_larger):
4954
bl SYMBOL_NAME(__udivmodsi4)
5055
ldr r1, [sp]
5156
add sp, sp, #4
57+
#if defined(__ARM_FEATURE_PAC_DEFAULT)
58+
pop { r12, lr }
59+
bxaut r12, lr, sp
60+
#else
5261
pop { pc }
5362
#endif
63+
#endif
5464
END_COMPILERRT_FUNCTION(__aeabi_uidivmod)
5565

5666
NO_EXEC_STACK_DIRECTIVE

compiler-rt/lib/builtins/arm/aeabi_uldivmod.S

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,12 @@
2222
.syntax unified
2323
.p2align 2
2424
DEFINE_COMPILERRT_FUNCTION(__aeabi_uldivmod)
25-
push {r6, lr}
25+
PACBTI_LANDING
26+
#if defined(__ARM_FEATURE_PAC_DEFAULT)
27+
push {r6, r12, lr}
28+
#else
29+
push {r6, lr}
30+
#endif
2631
sub sp, sp, #16
2732
add r6, sp, #8
2833
str r6, [sp]
@@ -38,7 +43,12 @@ DEFINE_COMPILERRT_FUNCTION(__aeabi_uldivmod)
3843
ldr r2, [sp, #8]
3944
ldr r3, [sp, #12]
4045
add sp, sp, #16
41-
pop {r6, pc}
46+
#if defined(__ARM_FEATURE_PAC_DEFAULT)
47+
pop {r6, r12, lr}
48+
bxaut r12, lr, sp
49+
#else
50+
pop {r6, pc}
51+
#endif
4252
END_COMPILERRT_FUNCTION(__aeabi_uldivmod)
4353

4454
NO_EXEC_STACK_DIRECTIVE

compiler-rt/lib/builtins/assembly.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,17 @@
194194
#else
195195
#define WIDE(op) op
196196
#endif
197+
198+
#if defined(__ARM_FEATURE_PAC_DEFAULT) && defined(__ARM_FEATURE_BTI_DEFAULT)
199+
#define PACBTI_LANDING pacbti r12, lr, sp
200+
#elif defined(__ARM_FEATURE_PAC_DEFAULT)
201+
#define PACBTI_LANDING pac r12, lr, sp
202+
#elif defined(__ARM_FEATURE_BTI_DEFAULT)
203+
#define PACBTI_LANDING bti
204+
#else
205+
#define PACBTI_LANDING
206+
#endif
207+
197208
#else // !defined(__arm)
198209
#define DECLARE_FUNC_ENCODING
199210
#define DEFINE_CODE_STATE

0 commit comments

Comments
 (0)