Skip to content

Commit d0dfafd

Browse files
committed
unix: backport libffi aarch64 fixes
LLVM 19 yields a compile error without these patches. Strictly speaking we may only need the final one. However there are conflicts unless we take all patches to `src/aarch64/sysv.S`. So we just take the 3 of them.
1 parent 64ad969 commit d0dfafd

File tree

1 file changed

+362
-0
lines changed

1 file changed

+362
-0
lines changed

cpython-unix/build-libffi.sh

Lines changed: 362 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,368 @@ tar -xf libffi-${LIBFFI_VERSION}.tar.gz
1313

1414
pushd libffi-${LIBFFI_VERSION}
1515

16+
# Patches needed to fix compilation on aarch64. Will presumably be in libffi
17+
# 3.4.7 or 3.5.
18+
19+
# Commit f64141ee3f9e455a060bd09e9ab72b6c94653d7c.
20+
patch -p1 <<'EOF'
21+
diff --git a/src/aarch64/sysv.S b/src/aarch64/sysv.S
22+
index fdd0e8b..60cfa50 100644
23+
--- a/src/aarch64/sysv.S
24+
+++ b/src/aarch64/sysv.S
25+
@@ -68,7 +68,7 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
26+
#define BTI_J hint #36
27+
/*
28+
* The ELF Notes section needs to indicate if BTI is supported, as the first ELF loaded that doesn't
29+
- * declare this support disables it for the whole process.
30+
+ * declare this support disables it for memory region containing the loaded library.
31+
*/
32+
# define GNU_PROPERTY_AARCH64_BTI (1 << 0) /* Has Branch Target Identification */
33+
.text
34+
@@ -527,6 +527,7 @@ L(do_closure):
35+
#if defined(FFI_EXEC_STATIC_TRAMP)
36+
.align 4
37+
CNAME(ffi_closure_SYSV_V_alt):
38+
+ BTI_C
39+
/* See the comments above trampoline_code_table. */
40+
ldr x17, [sp, #8] /* Load closure in x17 */
41+
add sp, sp, #16 /* Restore the stack */
42+
@@ -541,6 +542,7 @@ CNAME(ffi_closure_SYSV_V_alt):
43+
44+
.align 4
45+
CNAME(ffi_closure_SYSV_alt):
46+
+ BTI_C
47+
/* See the comments above trampoline_code_table. */
48+
ldr x17, [sp, #8] /* Load closure in x17 */
49+
add sp, sp, #16 /* Restore the stack */
50+
diff --git a/testsuite/Makefile.am b/testsuite/Makefile.am
51+
index d286cf7..6ba98e1 100644
52+
--- a/testsuite/Makefile.am
53+
+++ b/testsuite/Makefile.am
54+
@@ -8,7 +8,7 @@ CLEANFILES = *.exe core* *.log *.sum
55+
56+
EXTRA_DIST = config/default.exp emscripten/build.sh emscripten/conftest.py \
57+
emscripten/node-tests.sh emscripten/test.html emscripten/test_libffi.py \
58+
- emscripten/build-tests.sh lib/libffi.exp lib/target-libpath.exp \
59+
+ emscripten/build-tests.sh lib/libffi.exp lib/target-libpath.exp \
60+
lib/wrapper.exp libffi.bhaible/Makefile libffi.bhaible/README \
61+
libffi.bhaible/alignof.h libffi.bhaible/bhaible.exp libffi.bhaible/test-call.c \
62+
libffi.bhaible/test-callback.c libffi.bhaible/testcases.c libffi.call/align_mixed.c \
63+
EOF
64+
65+
# Commit 45d284f2d066cc3a080c5be88e51b4d934349797.
66+
patch -p1 <<'EOF'
67+
diff --git a/configure.ac b/configure.ac
68+
index 816bfd6..b35a999 100644
69+
--- a/configure.ac
70+
+++ b/configure.ac
71+
@@ -189,17 +189,17 @@ AC_CACHE_CHECK([whether compiler supports pointer authentication],
72+
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[]], [[
73+
#ifdef __clang__
74+
# if __has_feature(ptrauth_calls)
75+
-# define HAVE_PTRAUTH 1
76+
+# define HAVE_ARM64E_PTRAUTH 1
77+
# endif
78+
#endif
79+
80+
-#ifndef HAVE_PTRAUTH
81+
+#ifndef HAVE_ARM64E_PTRAUTH
82+
# error Pointer authentication not supported
83+
#endif
84+
]])],[libffi_cv_as_ptrauth=yes],[libffi_cv_as_ptrauth=no])
85+
])
86+
if test "x$libffi_cv_as_ptrauth" = xyes; then
87+
- AC_DEFINE(HAVE_PTRAUTH, 1,
88+
+ AC_DEFINE(HAVE_ARM64E_PTRAUTH, 1,
89+
[Define if your compiler supports pointer authentication.])
90+
fi
91+
92+
diff --git a/include/ffi_cfi.h b/include/ffi_cfi.h
93+
index f4c292d..8565663 100644
94+
--- a/include/ffi_cfi.h
95+
+++ b/include/ffi_cfi.h
96+
@@ -49,6 +49,7 @@
97+
# define cfi_personality(enc, exp) .cfi_personality enc, exp
98+
# define cfi_lsda(enc, exp) .cfi_lsda enc, exp
99+
# define cfi_escape(...) .cfi_escape __VA_ARGS__
100+
+# define cfi_window_save .cfi_window_save
101+
102+
#else
103+
104+
@@ -71,6 +72,7 @@
105+
# define cfi_personality(enc, exp)
106+
# define cfi_lsda(enc, exp)
107+
# define cfi_escape(...)
108+
+# define cfi_window_save
109+
110+
#endif /* HAVE_AS_CFI_PSEUDO_OP */
111+
#endif /* FFI_CFI_H */
112+
diff --git a/src/aarch64/ffi.c b/src/aarch64/ffi.c
113+
index b13738e..964934d 100644
114+
--- a/src/aarch64/ffi.c
115+
+++ b/src/aarch64/ffi.c
116+
@@ -63,7 +63,7 @@ struct call_context
117+
#if FFI_EXEC_TRAMPOLINE_TABLE
118+
119+
#ifdef __MACH__
120+
-#ifdef HAVE_PTRAUTH
121+
+#ifdef HAVE_ARM64E_PTRAUTH
122+
#include <ptrauth.h>
123+
#endif
124+
#include <mach/vm_param.h>
125+
@@ -877,7 +877,7 @@ ffi_prep_closure_loc (ffi_closure *closure,
126+
127+
#if FFI_EXEC_TRAMPOLINE_TABLE
128+
# ifdef __MACH__
129+
-# ifdef HAVE_PTRAUTH
130+
+# ifdef HAVE_ARM64E_PTRAUTH
131+
codeloc = ptrauth_auth_data(codeloc, ptrauth_key_function_pointer, 0);
132+
# endif
133+
void **config = (void **)((uint8_t *)codeloc - PAGE_MAX_SIZE);
134+
diff --git a/src/aarch64/internal.h b/src/aarch64/internal.h
135+
index b5d102b..c39f9cb 100644
136+
--- a/src/aarch64/internal.h
137+
+++ b/src/aarch64/internal.h
138+
@@ -81,20 +81,62 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
139+
/* Helpers for writing assembly compatible with arm ptr auth */
140+
#ifdef LIBFFI_ASM
141+
142+
-#ifdef HAVE_PTRAUTH
143+
-#define SIGN_LR pacibsp
144+
-#define SIGN_LR_WITH_REG(x) pacib lr, x
145+
-#define AUTH_LR_AND_RET retab
146+
-#define AUTH_LR_WITH_REG(x) autib lr, x
147+
-#define BRANCH_AND_LINK_TO_REG blraaz
148+
-#define BRANCH_TO_REG braaz
149+
-#else
150+
-#define SIGN_LR
151+
-#define SIGN_LR_WITH_REG(x)
152+
-#define AUTH_LR_AND_RET ret
153+
-#define AUTH_LR_WITH_REG(x)
154+
-#define BRANCH_AND_LINK_TO_REG blr
155+
-#define BRANCH_TO_REG br
156+
-#endif
157+
-
158+
-#endif
159+
+ #if defined(HAVE_ARM64E_PTRAUTH)
160+
+ /* ARM64E ABI For Darwin */
161+
+ #define SIGN_LR pacibsp
162+
+ #define SIGN_LR_WITH_REG(x) pacib lr, x
163+
+ #define AUTH_LR_AND_RET retab
164+
+ #define AUTH_LR_WITH_REG(x) autib lr, x
165+
+ #define BRANCH_AND_LINK_TO_REG blraaz
166+
+ #define BRANCH_TO_REG braaz
167+
+ #define PAC_CFI_WINDOW_SAVE
168+
+ /* Linux PAC Support */
169+
+ #elif defined(__ARM_FEATURE_PAC_DEFAULT)
170+
+ #define GNU_PROPERTY_AARCH64_POINTER_AUTH (1 << 1)
171+
+ #define PAC_CFI_WINDOW_SAVE cfi_window_save
172+
+ #define TMP_REG x9
173+
+ #define BRANCH_TO_REG br
174+
+ #define BRANCH_AND_LINK_TO_REG blr
175+
+ #define SIGN_LR_LINUX_ONLY SIGN_LR
176+
+ /* Which key to sign with? */
177+
+ #if (__ARM_FEATURE_PAC_DEFAULT & 1) == 1
178+
+ /* Signed with A-key */
179+
+ #define SIGN_LR hint #25 /* paciasp */
180+
+ #define AUTH_LR hint #29 /* autiasp */
181+
+ #else
182+
+ /* Signed with B-key */
183+
+ #define SIGN_LR hint #27 /* pacibsp */
184+
+ #define AUTH_LR hint #31 /* autibsp */
185+
+ #endif /* __ARM_FEATURE_PAC_DEFAULT */
186+
+ #define AUTH_LR_WITH_REG(x) _auth_lr_with_reg x
187+
+.macro _auth_lr_with_reg modifier
188+
+ mov TMP_REG, sp
189+
+ mov sp, \modifier
190+
+ AUTH_LR
191+
+ mov sp, TMP_REG
192+
+.endm
193+
+ #define SIGN_LR_WITH_REG(x) _sign_lr_with_reg x
194+
+.macro _sign_lr_with_reg modifier
195+
+ mov TMP_REG, sp
196+
+ mov sp, \modifier
197+
+ SIGN_LR
198+
+ mov sp, TMP_REG
199+
+.endm
200+
+ #define AUTH_LR_AND_RET _auth_lr_and_ret modifier
201+
+.macro _auth_lr_and_ret modifier
202+
+ AUTH_LR
203+
+ ret
204+
+.endm
205+
+ #undef TMP_REG
206+
+
207+
+ /* No Pointer Auth */
208+
+ #else
209+
+ #define SIGN_LR
210+
+ #define SIGN_LR_WITH_REG(x)
211+
+ #define AUTH_LR_AND_RET ret
212+
+ #define AUTH_LR_WITH_REG(x)
213+
+ #define BRANCH_AND_LINK_TO_REG blr
214+
+ #define BRANCH_TO_REG br
215+
+ #define PAC_CFI_WINDOW_SAVE
216+
+ #endif /* HAVE_ARM64E_PTRAUTH */
217+
+#endif /* LIBFFI_ASM */
218+
diff --git a/src/aarch64/sysv.S b/src/aarch64/sysv.S
219+
index 60cfa50..6a9a561 100644
220+
--- a/src/aarch64/sysv.S
221+
+++ b/src/aarch64/sysv.S
222+
@@ -92,27 +92,27 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
223+
cfi_startproc
224+
CNAME(ffi_call_SYSV):
225+
BTI_C
226+
- /* Sign the lr with x1 since that is where it will be stored */
227+
+ PAC_CFI_WINDOW_SAVE
228+
+ /* Sign the lr with x1 since that is the CFA which is the modifer used in auth instructions */
229+
SIGN_LR_WITH_REG(x1)
230+
231+
- /* Use a stack frame allocated by our caller. */
232+
-#if defined(HAVE_PTRAUTH) && defined(__APPLE__)
233+
+#if defined(HAVE_ARM64E_PTRAUTH) && defined(__APPLE__)
234+
/* darwin's libunwind assumes that the cfa is the sp and that's the data
235+
* used to sign the lr. In order to allow unwinding through this
236+
* function it is necessary to point the cfa at the signing register.
237+
*/
238+
cfi_def_cfa(x1, 0);
239+
-#else
240+
- cfi_def_cfa(x1, 40);
241+
#endif
242+
+ /* Use a stack frame allocated by our caller. */
243+
stp x29, x30, [x1]
244+
+ cfi_def_cfa_register(x1)
245+
+ cfi_rel_offset (x29, 0)
246+
+ cfi_rel_offset (x30, 8)
247+
mov x9, sp
248+
str x9, [x1, #32]
249+
mov x29, x1
250+
- mov sp, x0
251+
cfi_def_cfa_register(x29)
252+
- cfi_rel_offset (x29, 0)
253+
- cfi_rel_offset (x30, 8)
254+
+ mov sp, x0
255+
256+
mov x9, x2 /* save fn */
257+
mov x8, x3 /* install structure return */
258+
@@ -326,6 +326,7 @@ CNAME(ffi_closure_SYSV_V):
259+
cfi_startproc
260+
BTI_C
261+
SIGN_LR
262+
+ PAC_CFI_WINDOW_SAVE
263+
stp x29, x30, [sp, #-ffi_closure_SYSV_FS]!
264+
cfi_adjust_cfa_offset (ffi_closure_SYSV_FS)
265+
cfi_rel_offset (x29, 0)
266+
@@ -351,6 +352,7 @@ CNAME(ffi_closure_SYSV_V):
267+
CNAME(ffi_closure_SYSV):
268+
BTI_C
269+
SIGN_LR
270+
+ PAC_CFI_WINDOW_SAVE
271+
stp x29, x30, [sp, #-ffi_closure_SYSV_FS]!
272+
cfi_adjust_cfa_offset (ffi_closure_SYSV_FS)
273+
cfi_rel_offset (x29, 0)
274+
@@ -648,6 +650,8 @@ CNAME(ffi_go_closure_SYSV_V):
275+
cfi_startproc
276+
CNAME(ffi_go_closure_SYSV):
277+
BTI_C
278+
+ SIGN_LR_LINUX_ONLY
279+
+ PAC_CFI_WINDOW_SAVE
280+
stp x29, x30, [sp, #-ffi_closure_SYSV_FS]!
281+
cfi_adjust_cfa_offset (ffi_closure_SYSV_FS)
282+
cfi_rel_offset (x29, 0)
283+
diff --git a/src/closures.c b/src/closures.c
284+
index 67a94a8..02cf78f 100644
285+
--- a/src/closures.c
286+
+++ b/src/closures.c
287+
@@ -164,7 +164,7 @@ ffi_tramp_is_present (__attribute__((unused)) void *ptr)
288+
289+
#include <mach/mach.h>
290+
#include <pthread.h>
291+
-#ifdef HAVE_PTRAUTH
292+
+#ifdef HAVE_ARM64E_PTRAUTH
293+
#include <ptrauth.h>
294+
#endif
295+
#include <stdio.h>
296+
@@ -223,7 +223,7 @@ ffi_trampoline_table_alloc (void)
297+
/* Remap the trampoline table on top of the placeholder page */
298+
trampoline_page = config_page + PAGE_MAX_SIZE;
299+
300+
-#ifdef HAVE_PTRAUTH
301+
+#ifdef HAVE_ARM64E_PTRAUTH
302+
trampoline_page_template = (vm_address_t)(uintptr_t)ptrauth_auth_data((void *)&ffi_closure_trampoline_table_page, ptrauth_key_function_pointer, 0);
303+
#else
304+
trampoline_page_template = (vm_address_t)&ffi_closure_trampoline_table_page;
305+
@@ -268,7 +268,7 @@ ffi_trampoline_table_alloc (void)
306+
ffi_trampoline_table_entry *entry = &table->free_list_pool[i];
307+
entry->trampoline =
308+
(void *) (trampoline_page + (i * FFI_TRAMPOLINE_SIZE));
309+
-#ifdef HAVE_PTRAUTH
310+
+#ifdef HAVE_ARM64E_PTRAUTH
311+
entry->trampoline = ptrauth_sign_unauthenticated(entry->trampoline, ptrauth_key_function_pointer, 0);
312+
#endif
313+
314+
EOF
315+
316+
# Commit 9c9e8368e49804c4f7c35ac9f0d7c1d0d533308b.
317+
patch -p1 <<'EOF'
318+
diff --git a/src/aarch64/internal.h b/src/aarch64/internal.h
319+
index c39f9cb..50fa5c1 100644
320+
--- a/src/aarch64/internal.h
321+
+++ b/src/aarch64/internal.h
322+
@@ -88,6 +88,7 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
323+
#define AUTH_LR_AND_RET retab
324+
#define AUTH_LR_WITH_REG(x) autib lr, x
325+
#define BRANCH_AND_LINK_TO_REG blraaz
326+
+ #define SIGN_LR_LINUX_ONLY
327+
#define BRANCH_TO_REG braaz
328+
#define PAC_CFI_WINDOW_SAVE
329+
/* Linux PAC Support */
330+
@@ -136,6 +137,7 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
331+
#define AUTH_LR_AND_RET ret
332+
#define AUTH_LR_WITH_REG(x)
333+
#define BRANCH_AND_LINK_TO_REG blr
334+
+ #define SIGN_LR_LINUX_ONLY
335+
#define BRANCH_TO_REG br
336+
#define PAC_CFI_WINDOW_SAVE
337+
#endif /* HAVE_ARM64E_PTRAUTH */
338+
EOF
339+
340+
# Commit 8308bed5b2423878aa20d7884a99cf2e30b8daf7.
341+
patch -p1 <<'EOF'
342+
diff --git a/src/aarch64/sysv.S b/src/aarch64/sysv.S
343+
index 6a9a561..e83bc65 100644
344+
--- a/src/aarch64/sysv.S
345+
+++ b/src/aarch64/sysv.S
346+
@@ -89,8 +89,8 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
347+
x5 closure
348+
*/
349+
350+
- cfi_startproc
351+
CNAME(ffi_call_SYSV):
352+
+ cfi_startproc
353+
BTI_C
354+
PAC_CFI_WINDOW_SAVE
355+
/* Sign the lr with x1 since that is the CFA which is the modifer used in auth instructions */
356+
@@ -348,8 +348,8 @@ CNAME(ffi_closure_SYSV_V):
357+
#endif
358+
359+
.align 4
360+
- cfi_startproc
361+
CNAME(ffi_closure_SYSV):
362+
+ cfi_startproc
363+
BTI_C
364+
SIGN_LR
365+
PAC_CFI_WINDOW_SAVE
366+
@@ -647,8 +647,8 @@ CNAME(ffi_go_closure_SYSV_V):
367+
#endif
368+
369+
.align 4
370+
- cfi_startproc
371+
CNAME(ffi_go_closure_SYSV):
372+
+ cfi_startproc
373+
BTI_C
374+
SIGN_LR_LINUX_ONLY
375+
PAC_CFI_WINDOW_SAVE
376+
EOF
377+
16378
EXTRA_CONFIGURE=
17379

18380
# mkostemp() was introduced in macOS 10.10 and libffi doesn't have

0 commit comments

Comments
 (0)