libffi in 10.15+ has different usage (presumably for arm64)
1 files changed, 105 insertions(+), 83 deletions(-)

M method_dispatch.m
M method_dispatch.m +105 -83
@@ 11,11 11,13 @@ 
  *   flt_args	bitfield: There are float arguments at these positions.
  *   dbl_args	bitfield: There are double arguments at these positions.
  */
+
 #import <Foundation/NSHost.h>
 #import <CoreFoundation/CoreFoundation.h>
 #import  <Foundation/NSString.h>
 #include <sys/mman.h>
 #include "piobjc.h"
+#include <ffi/ffi.h>
 
 extern void low_create_pike_object(ffi_cif* cif, void* resp, void** args, void* userdata);
 extern void low_f_objc_dynamic_instance_method(ffi_cif* cif, void* resp, void** args, void* userdata);

          
@@ 367,6 369,7 @@ printf("arg type %d is null\n", i);
 void * make_create_stub(struct program * prog)
 {
   static ffi_cif* init_cif = NULL;
+  void * codeloc;
   ffi_closure * closure = NULL;
   ffi_status rv;
   ffi_type** cl_arg_types;

          
@@ 400,15 403,14 @@ void * make_create_stub(struct program *
     }
   }
 
-  closure = mmap(NULL, sizeof(ffi_closure), PROT_READ | PROT_WRITE,
-             MAP_ANON | MAP_PRIVATE, -1, 0);  
+  closure = ffi_closure_alloc(sizeof(ffi_closure), &codeloc);
   
   if(closure == ((void*)-1))
   {
     Pike_error("quick_make_stub: out of memory\n");
   }  
 
-  rv = ffi_prep_closure(closure, init_cif, disp, prog);
+  rv = ffi_prep_closure_loc(closure, init_cif, disp, prog, codeloc);
 
   if(rv != FFI_OK)
   {

          
@@ 416,11 418,13 @@ void * make_create_stub(struct program *
     Pike_error("Cannot create FFI closure.\n");
   }
 
+#if 0
   // Ensure that the closure will execute on all architectures.
   if (mprotect(closure, sizeof(closure), PROT_READ | PROT_EXEC) == -1)
   {
     Pike_error("quick_make_stub: unable to make executable\n");
   }
+#endif /* 0 */
 
   return (void *)closure;
 }

          
@@ 428,6 432,7 @@ void * make_create_stub(struct program *
 void * make_class_stub(struct program * prog)
 {
   static ffi_cif* init_cif = NULL;
+  void * codeloc;
   ffi_closure * closure = NULL;
   ffi_status rv;
   ffi_type** cl_arg_types;

          
@@ 460,15 465,14 @@ void * make_class_stub(struct program * 
     }
   }
 
-  closure = mmap(NULL, sizeof(ffi_closure), PROT_READ | PROT_WRITE,
-             MAP_ANON | MAP_PRIVATE, -1, 0);  
+  closure = ffi_closure_alloc(sizeof(ffi_closure), &codeloc);
   
   if(closure == ((void*)-1))
   {
     Pike_error("quick_make_stub: out of memory\n");
   }  
 
-  rv = ffi_prep_closure(closure, init_cif, disp, prog);
+  rv = ffi_prep_closure_loc(closure, init_cif, disp, prog, codeloc);
 
   if(rv != FFI_OK)
   {

          
@@ 476,11 480,13 @@ void * make_class_stub(struct program * 
     Pike_error("Cannot create FFI closure.\n");
   }
 
+#if 0
   // Ensure that the closure will execute on all architectures.
   if (mprotect(closure, sizeof(closure), PROT_READ | PROT_EXEC) == -1)
   {
     Pike_error("quick_make_stub: unable to make executable\n");
   }
+#endif /* 0 */
 
   return (void *)closure;
 }

          
@@ 490,6 496,7 @@ void * make_class_func_stub(struct progr
 {
   int z;
   static ffi_cif* init_cif = NULL;
+  void * codeloc;
   ffi_closure * closure = NULL;
   ffi_status rv;
   ffi_type** cl_arg_types;

          
@@ 596,15 603,14 @@ void * make_class_func_stub(struct progr
     }
   }
 
-  closure = mmap(NULL, sizeof(ffi_closure), PROT_READ | PROT_WRITE,
-             MAP_ANON | MAP_PRIVATE, -1, 0);  
+  closure = ffi_closure_alloc(sizeof(ffi_closure), &codeloc);
   
   if(closure == ((void*)-1))
   {
     Pike_error("quick_make_stub: out of memory\n");
   }  
 
-  rv = ffi_prep_closure(closure, init_cif, disp, dta);
+  rv = ffi_prep_closure_loc(closure, init_cif, disp, dta, codeloc);
 
   if(rv != FFI_OK)
   {

          
@@ 612,11 618,13 @@ void * make_class_func_stub(struct progr
     Pike_error("Cannot create FFI closure.\n");
   }
 
+#if 0
   // Ensure that the closure will execute on all architectures.
   if (mprotect(closure, sizeof(closure), PROT_READ | PROT_EXEC) == -1)
   {
     Pike_error("quick_make_stub: unable to make executable\n");
   }
+#endif /* 0 */
 
   return (void *)closure;
 }

          
@@ 625,6 633,7 @@ void * make_class_func_stub(struct progr
 void * make_dynamic_method_stub(char * function_name)
 {
   static ffi_cif* init_cif = NULL;
+  void * codeloc;
   ffi_closure * closure = NULL;
   ffi_status rv;
   ffi_type** cl_arg_types;

          
@@ 657,15 666,14 @@ void * make_dynamic_method_stub(char * f
     }
   }
 
-  closure = mmap(NULL, sizeof(ffi_closure), PROT_READ | PROT_WRITE,
-             MAP_ANON | MAP_PRIVATE, -1, 0);  
+  closure = ffi_closure_alloc(sizeof(ffi_closure), &codeloc);
   
   if(closure == ((void*)-1))
   {
     Pike_error("quick_make_stub: out of memory\n");
   }
   
-  rv = ffi_prep_closure(closure, init_cif, disp, strdup(function_name));
+  rv = ffi_prep_closure_loc(closure, init_cif, disp, strdup(function_name), codeloc);
 
   if(rv != FFI_OK)
   {

          
@@ 673,11 681,13 @@ void * make_dynamic_method_stub(char * f
     Pike_error("Cannot create FFI closure.\n");
   }
 
+#if 0
   // Ensure that the closure will execute on all architectures.
   if (mprotect(closure, sizeof(closure), PROT_READ | PROT_EXEC) == -1)
   {
     Pike_error("quick_make_stub: unable to make executable\n");
   }
+#endif /* 0 */
 
   return (void *)closure;
 }

          
@@ 687,6 697,7 @@ void * make_dynamic_method_stub(char * f
 void * quick_make_stub(void * dta, void * func)
 {
   static ffi_cif* init_cif = NULL;
+  void * codeloc;
   ffi_closure * closure = NULL;
   ffi_status rv;
   ffi_type** cl_arg_types;

          
@@ 714,15 725,14 @@ void * quick_make_stub(void * dta, void 
     }
   }
 
-  closure = mmap(NULL, sizeof(ffi_closure), PROT_READ | PROT_WRITE,
-             MAP_ANON | MAP_PRIVATE, -1, 0);  
+  closure = ffi_closure_alloc(sizeof(ffi_closure), &codeloc);
   
   if(closure == ((void*)-1))
   {
     Pike_error("quick_make_stub: out of memory\n");
   }
   
-  rv = ffi_prep_closure(closure, init_cif, func, dta);
+  rv = ffi_prep_closure_loc(closure, init_cif, func, dta, codeloc);
 
   if(rv != FFI_OK)
   {

          
@@ 730,11 740,13 @@ void * quick_make_stub(void * dta, void 
     Pike_error("Cannot create FFI closure.\n");
   }
 
+#if 0
   // Ensure that the closure will execute on all architectures.
   if (mprotect(closure, sizeof(closure), PROT_READ | PROT_EXEC) == -1)
   {
     Pike_error("quick_make_stub: unable to make executable\n");
   }
+#endif /* 0 */
 
   return (void *)closure;
 }

          
@@ 744,6 756,7 @@ void * quick_make_stub(void * dta, void 
 void * make_static_stub(void * dta, void * func)
 {
   static ffi_cif* init_cif = NULL;
+  void * codeloc;
   ffi_closure * closure = NULL;
   ffi_status rv;
   ffi_type** cl_arg_types;

          
@@ 771,21 784,22 @@ void * make_static_stub(void * dta, void
     }
   }
 
-  closure = mmap(NULL, sizeof(ffi_closure), PROT_READ | PROT_WRITE,
-             MAP_ANON | MAP_PRIVATE, -1, 0);  
+  closure = ffi_closure_alloc(sizeof(ffi_closure), &codeloc);
   
   if(closure == ((void*)-1))
   {
     Pike_error("quick_make_stub: out of memory\n");
   }
 
-  rv = ffi_prep_closure(closure, init_cif, func, dta);
+  rv = ffi_prep_closure_loc(closure, init_cif, func, dta, codeloc);
 
+#if 0
   // Ensure that the closure will execute on all architectures.
   if (mprotect(closure, sizeof(closure), PROT_READ | PROT_EXEC) == -1)
   {
     Pike_error("quick_make_stub: unable to make executable\n");
   }
+#endif /* 0 */
 
   if(rv != FFI_OK)
   {

          
@@ 805,6 819,73 @@ void * make_static_stub(void * dta, void
 void * make_static_stub(void * dta, void * func)
 {
   static ffi_cif* init_cif;
+  void * codeloc;
+  ffi_closure * closure = NULL;
+  ffi_status rv;
+  ffi_type** cl_arg_types;
+  ffi_type* cl_ret_type;
+  const char* rettype;
+  
+    cl_arg_types = malloc(sizeof(ffi_type *)*2);
+    if(cl_arg_types == NULL)
+    {
+      Pike_error("make_static_stub: out of memory\n");
+    }
+    
+//    cl_arg_types[0] = &ffi_type_pointer;
+    cl_arg_types[0] = &ffi_type_uint32;
+
+    init_cif = malloc(sizeof(ffi_cif));
+
+    if(init_cif == NULL) {
+      Pike_error("make_static_stub: out of memory\n");
+    }
+
+    rv = ffi_prep_cif(init_cif, FFI_DEFAULT_ABI, 1, &ffi_type_void, cl_arg_types);
+    if(rv != FFI_OK)
+    {
+      free(init_cif);
+      Pike_error("Cannot create FFI interface.\n");
+    }
+ 
+// Allocate a page to hold the closure with read and write permissions.
+      if ((closure = ffi_closure_alloc(sizeof(ffi_closure), &codeloc))
+           == (void*)-1)
+      {
+		  LOG("error mmap%s\n", "");
+          // Check errno and handle the error.
+      }
+//  closure = malloc(sizeof(ffi_closure));
+  
+  if(closure == NULL)
+  {
+    Pike_error("quick_make_stub: out of memory\n");
+  }
+  
+  rv = ffi_prep_closure_loc(closure, init_cif, func, dta, codeloc);
+
+  if(rv != FFI_OK)
+  {
+    free(closure);
+    Pike_error("Cannot create FFI closure.\n");
+  }
+  
+  // Ensure that the closure will execute on all architectures.
+  if (mprotect(closure, sizeof(closure), PROT_READ | PROT_EXEC) == -1)
+  {
+	  LOG("error mprotect%s\n", "");
+      // Check errno and handle the error.
+  }
+  
+    return (void *)closure;
+}
+
+
+
+ffi_closure * make_static_stub3(void * dta, void * func)
+{
+  static ffi_cif* init_cif;
+  void * codeloc;
   ffi_closure * closure = NULL;
   ffi_status rv;
   ffi_type** cl_arg_types;

          
@@ 830,8 911,8 @@ void * make_static_stub(void * dta, void
     }
  
 // Allocate a page to hold the closure with read and write permissions.
-      if ((closure = mmap(NULL, sizeof(ffi_closure), PROT_READ | PROT_WRITE,
-          MAP_ANON | MAP_PRIVATE, -1, 0)) == (void*)-1)
+      if ((closure = ffi_closure_alloc(sizeof(ffi_closure), &codeloc)
+          ) == (void*)-1)
       {
 		  LOG("error mmap%s\n", "");
           // Check errno and handle the error.

          
@@ 843,82 924,23 @@ void * make_static_stub(void * dta, void
     Pike_error("quick_make_stub: out of memory\n");
   }
   
-  rv = ffi_prep_closure(closure, init_cif, func, dta);
+  rv = ffi_prep_closure_loc(closure, init_cif, func, dta, codeloc);
 
   if(rv != FFI_OK)
   {
     free(closure);
     Pike_error("Cannot create FFI closure.\n");
   }
-  
+ 
+#if 0 
   // Ensure that the closure will execute on all architectures.
   if (mprotect(closure, sizeof(closure), PROT_READ | PROT_EXEC) == -1)
   {
 	  LOG("error mprotect%s\n", "");
       // Check errno and handle the error.
   }
-  
-    return (void *)closure;
-}
-
-
-
-ffi_closure * make_static_stub3(void * dta, void * func)
-{
-  static ffi_cif* init_cif;
-  ffi_closure * closure = NULL;
-  ffi_status rv;
-  ffi_type** cl_arg_types;
-  ffi_type* cl_ret_type;
-  const char* rettype;
-  
-    cl_arg_types = malloc(sizeof(ffi_type *)*2);
-    if(cl_arg_types == NULL)
-    {
-      Pike_error("quick_make_stub: out of memory\n");
-    }
-    
-//    cl_arg_types[0] = &ffi_type_pointer;
-    cl_arg_types[0] = &ffi_type_uint32;
-
-    init_cif = malloc(sizeof(ffi_cif));
+#endif /* 0 */
 
-    rv = ffi_prep_cif(init_cif, FFI_DEFAULT_ABI, 1, &ffi_type_void, cl_arg_types);
-    if(rv != FFI_OK)
-    {
-      free(init_cif);
-      Pike_error("Cannot create FFI interface.\n");
-    }
- 
-// Allocate a page to hold the closure with read and write permissions.
-      if ((closure = mmap(NULL, sizeof(ffi_closure), PROT_READ | PROT_WRITE,
-          MAP_ANON | MAP_PRIVATE, -1, 0)) == (void*)-1)
-      {
-		  LOG("error mmap%s\n", "");
-          // Check errno and handle the error.
-      }
-//  closure = malloc(sizeof(ffi_closure));
-  
-  if(closure == NULL)
-  {
-    Pike_error("quick_make_stub: out of memory\n");
-  }
-  
-  rv = ffi_prep_closure(closure, init_cif, func, dta);
-
-  if(rv != FFI_OK)
-  {
-    free(closure);
-    Pike_error("Cannot create FFI closure.\n");
-  }
-  
-  // Ensure that the closure will execute on all architectures.
-  if (mprotect(closure, sizeof(closure), PROT_READ | PROT_EXEC) == -1)
-  {
-	  LOG("error mprotect%s\n", "");
-      // Check errno and handle the error.
-  }
-  
    return closure;
 }