Skip to content

Commit 3e5fb51

Browse files
committed
[OpenMP][AArch64] Fix branch protection in microtasks
Start __kmp_invoke_microtask with BTI in order to identify the function as a valid branch target. Also add the BTI marker to z_Linux_asm.S. With this patch, libomp.so can now be generated with DT_AARCH64_BTI_PLT when built with -mbranch-protection=standard. The implementation is based on the code available in compiler-rt.
1 parent beb37e2 commit 3e5fb51

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

openmp/runtime/src/z_Linux_asm.S

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,27 @@
1717

1818
#include "kmp_config.h"
1919

20+
#if KMP_OS_LINUX
21+
// BTI and PAC gnu property note
22+
#define NT_GNU_PROPERTY_TYPE_0 5
23+
#define GNU_PROPERTY_AARCH64_FEATURE_1_AND 0xc0000000
24+
#define GNU_PROPERTY_AARCH64_FEATURE_1_BTI 1
25+
#define GNU_PROPERTY_AARCH64_FEATURE_1_PAC 2
26+
27+
# define GNU_PROPERTY(type, value) \
28+
.pushsection .note.gnu.property, "a"; \
29+
.p2align 3; \
30+
.word 4; \
31+
.word 16; \
32+
.word NT_GNU_PROPERTY_TYPE_0; \
33+
.asciz "GNU"; \
34+
.word type; \
35+
.word 4; \
36+
.word value; \
37+
.word 0; \
38+
.popsection
39+
#endif
40+
2041
#if KMP_ARCH_X86 || KMP_ARCH_X86_64
2142

2243
# if KMP_MIC
@@ -176,6 +197,25 @@ KMP_PREFIX_UNDERSCORE(\proc):
176197
.endm
177198
# endif // KMP_OS_DARWIN
178199

200+
# if defined(__ARM_FEATURE_BTI_DEFAULT)
201+
# define BTI_FLAG GNU_PROPERTY_AARCH64_FEATURE_1_BTI
202+
# else
203+
# define BTI_FLAG 0
204+
# endif
205+
# if __ARM_FEATURE_PAC_DEFAULT & 3
206+
# define PAC_FLAG GNU_PROPERTY_AARCH64_FEATURE_1_PAC
207+
# else
208+
# define PAC_FLAG 0
209+
# endif
210+
211+
# if (BTI_FLAG | PAC_FLAG) != 0
212+
# define BTI_C hint #34
213+
# define GNU_PROPERTY_BTI_PAC \
214+
GNU_PROPERTY(GNU_PROPERTY_AARCH64_FEATURE_1_AND, BTI_FLAG | PAC_FLAG)
215+
# else
216+
# define BTI_C
217+
# define GNU_PROPERTY_BTI_PAC
218+
# endif
179219
#endif // (KMP_OS_LINUX || KMP_OS_DARWIN || KMP_OS_WINDOWS) && (KMP_ARCH_AARCH64 || KMP_ARCH_AARCH64_32 || KMP_ARCH_ARM)
180220

181221
.macro COMMON name, size, align_power
@@ -1296,6 +1336,7 @@ __tid = 8
12961336
// mark_begin;
12971337
.text
12981338
PROC __kmp_invoke_microtask
1339+
BTI_C
12991340

13001341
stp x29, x30, [sp, #-16]!
13011342
# if OMPT_SUPPORT
@@ -2472,3 +2513,7 @@ __kmp_unnamed_critical_addr:
24722513
.4byte .gomp_critical_user_
24732514
.size __kmp_unnamed_critical_addr, 4
24742515
#endif
2516+
2517+
#if KMP_OS_LINUX && (KMP_ARCH_AARCH64 || KMP_ARCH_AARCH64_32)
2518+
GNU_PROPERTY_BTI_PAC
2519+
#endif

0 commit comments

Comments
 (0)