 diff -ur libffi-3.3/src/closures.c libffi-3.3.new/src/closures.c
 --- libffi-3.3/src/closures.c	2019-11-20 04:15:41.000000000 -0700
 +++ libffi-3.3.new/src/closures.c	2021-01-04 19:39:16.000000000 -0700
-@@ -146,13 +146,17 @@
- 
- #ifdef __MACH__
- 
-+#include <assert.h>
-+#include <dispatch/dispatch.h>
-+#include <dlfcn.h>
+@@ -148,10 +148,19 @@
+ 
  #include <mach/mach.h>
  #include <pthread.h>
 +#ifdef HAVE_PTRAUTH

+
 	/* Use a stack frame allocated by our caller.  */
-	cfi_def_cfa(x1, 32);
+#ifdef HAVE_PTRAUTH && defined(__APPLE__)
+	/* darwin's libunwind assumes that the cfa is the sp and that's the data
+	 * used to sign the lr.  In order to allow unwinding through this
+	 * function it is necessary to point the cfa at the signing register.

 
   /* free list tracking */
   uint16_t free_count;
@@ -180,6 +188,21 @@ struct ffi_trampoline_table_entry
 /* Total number of trampolines that fit in one trampoline table */
 #define FFI_TRAMPOLINE_COUNT (PAGE_MAX_SIZE / FFI_TRAMPOLINE_SIZE)
 
+/* The trampoline dylib has one page for the MACHO_HEADER and one page for the
+ * trampolines. iOS 12.0 and later, and macOS on Apple Silicon require that
+ * the entire dylib needs to be remapped as a unit.
+ *
+ * arm (legacy): Allocate two pages -- a config page and a placeholder for the trampolines
+ * arm64 (modern): Allocate three pages -- a config page and two placeholders for the trampoline dylib
+ */
+#ifdef FFI_TRAMPOLINE_WHOLE_DYLIB
+#define FFI_TRAMPOLINE_ALLOCATION_PAGE_COUNT 3
