Commit a1130f37712c03957c9b0adf316cd006fa92a60b

Anthony Green 2022-09-12T08:53:06

Add static trampoline support for Cygwin

diff --git a/configure.ac b/configure.ac
index 8edb7b6..75cc455 100644
--- a/configure.ac
+++ b/configure.ac
@@ -392,6 +392,12 @@ AC_ARG_ENABLE(exec-static-tramp,
 if test "$enable_exec_static_tramp" != no; then
 case "$target" in
      *-cygwin*)
+       # Only define static trampolines if we are using the cygwin runtime.
+       # Will this need to be changed for mingw?
+       if test "x$GCC" = "xyes"; then
+         AC_DEFINE(FFI_EXEC_STATIC_TRAMP, 1,
+                   [Define this if you want statically defined trampolines])
+       fi
      ;;
      *arm*-*-linux-* | aarch64*-*-linux-* | i*86-*-linux-* | x86_64-*-linux-* | loongarch*-*-linux-*)
        AC_DEFINE(FFI_EXEC_STATIC_TRAMP, 1,
diff --git a/src/closures.c b/src/closures.c
index 6cbe2f0..e2bc651 100644
--- a/src/closures.c
+++ b/src/closures.c
@@ -1,5 +1,5 @@
 /* -----------------------------------------------------------------------
-   closures.c - Copyright (c) 2019 Anthony Green
+   closures.c - Copyright (c) 2019, 2022 Anthony Green
                 Copyright (c) 2007, 2009, 2010 Red Hat, Inc.
                 Copyright (C) 2007, 2009, 2010 Free Software Foundation, Inc
                 Copyright (c) 2011 Plausible Labs Cooperative, Inc.
@@ -133,7 +133,7 @@ ffi_tramp_is_present (__attribute__((unused)) void *ptr)
 #  define FFI_MMAP_EXEC_WRIT 1
 #  define HAVE_MNTENT 1
 # endif
-# if defined(_WIN32) || defined(__OS2__)
+# if defined(__CYGWIN__) || defined(_WIN32) || defined(__OS2__)
 /* Windows systems may have Data Execution Protection (DEP) enabled,
    which requires the use of VirtualMalloc/VirtualFree to alloc/free
    executable memory. */
diff --git a/src/tramp.c b/src/tramp.c
index 265aeaa..19707fb 100644
--- a/src/tramp.c
+++ b/src/tramp.c
@@ -1,5 +1,6 @@
 /* -----------------------------------------------------------------------
    tramp.c - Copyright (c) 2020 Madhavan T. Venkataraman
+             Copyright (c) 2022 Anthony Green
 
    API and support functions for managing statically defined closure
    trampolines.
@@ -34,7 +35,7 @@
  * Add support for other OSes later. For now, it is just Linux.
  */
 
-#if defined __linux__
+#if defined (__linux__) || defined (__CYGWIN__)
 #ifdef __linux__
 #define _GNU_SOURCE 1
 #endif
@@ -50,7 +51,10 @@
 #include <linux/limits.h>
 #include <linux/types.h>
 #endif
-#endif /* __linux__ */
+#ifdef __CYGWIN__
+#include <linux/limits.h>
+#endif
+#endif
 
 /*
  * Each architecture defines static code for a trampoline code table. The
@@ -191,7 +195,7 @@ static struct tramp_globals tramp_globals;
  */
 static int tramp_table_alloc (void);
 
-#if defined __linux__
+#if defined (__linux__) || defined (__CYGWIN__)
 
 static int
 ffi_tramp_get_libffi (void)
@@ -245,9 +249,9 @@ ffi_tramp_get_libffi (void)
   return 1;
 }
 
-#endif /* __linux__ */
+#endif /* defined (__linux__) || defined (__CYGWIN__) */
 
-#if defined __linux__
+#if defined (__linux__) || defined (__CYGWIN__)
 
 #if defined HAVE_MKSTEMP
 
@@ -294,11 +298,11 @@ ffi_tramp_get_temp_file (void)
 
 #endif /* defined HAVE_MKSTEMP */
 
-#endif /* __linux__ */
+#endif /* defined (__linux__) || defined (__CYGWIN__) */
 
 /* ------------------------ OS-specific Initialization ----------------------*/
 
-#if defined __linux__
+#if defined (__linux__) || defined (__CYGWIN__)
 
 static int
 ffi_tramp_init_os (void)
@@ -308,11 +312,11 @@ ffi_tramp_init_os (void)
   return ffi_tramp_get_temp_file ();
 }
 
-#endif /* __linux__ */
+#endif /* defined (__linux__) || defined (__CYGWIN__) */
 
 /* --------------------------- OS-specific Locking -------------------------*/
 
-#if defined __linux__
+#if defined (__linux__) || defined (__CYGWIN__)
 
 static pthread_mutex_t tramp_globals_mutex = PTHREAD_MUTEX_INITIALIZER;
 
@@ -328,7 +332,7 @@ ffi_tramp_unlock()
   pthread_mutex_unlock (&tramp_globals_mutex);
 }
 
-#endif /* __linux__ */
+#endif /* defined (__linux__) || defined (__CYGWIN__) */
 
 /* ------------------------ OS-specific Memory Mapping ----------------------*/
 
@@ -347,7 +351,7 @@ ffi_tramp_unlock()
  * sizeof (struct tramp_parm) cannot exceed the size of a parameter block.
  */
 
-#if defined __linux__
+#if defined (__linux__) || defined (__CYGWIN__)
 
 static int
 tramp_table_map (struct tramp_table *table)
@@ -384,7 +388,7 @@ tramp_table_unmap (struct tramp_table *table)
   (void) munmap (table->parm_table, tramp_globals.map_size);
 }
 
-#endif /* __linux__ */
+#endif /* defined (__linux__) || defined (__CYGWIN__) */
 
 /* ------------------------ Trampoline Initialization ----------------------*/