Skip to content

Commit

Permalink
fix mingw build and crashing bugs for Python Windows ARM64 (libffi#496)
Browse files Browse the repository at this point in the history
* fix mingw build and crashing bugs for Python Windows ARM64

* Fix issues found in PR review
  • Loading branch information
paulmon authored and atgreen committed Aug 7, 2019
1 parent e0b4f84 commit c2a6859
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 15 deletions.
7 changes: 7 additions & 0 deletions .appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ platform:
- x64
- x86
- arm
- arm64

environment:
global:
Expand All @@ -30,6 +31,12 @@ install:
$env:HOST="i686-pc-cygwin"
$env:MSVCC="/cygdrive/c/projects/libffi/msvcc.sh"
$env:SRC_ARCHITECTURE="x86"
} ElseIf ($env:Platform -Match "arm64") {
$env:VCVARS_PLATFORM="x86_arm64"
$env:BUILD="i686-pc-cygwin"
$env:HOST="aarch64-w64-cygwin"
$env:MSVCC="/cygdrive/c/projects/libffi/msvcc.sh -marm64"
$env:SRC_ARCHITECTURE="aarch64"
} ElseIf ($env:Platform -Match "arm") {
$env:VCVARS_PLATFORM="x86_arm"
$env:BUILD="i686-pc-cygwin"
Expand Down
1 change: 1 addition & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ noinst_HEADERS = \

EXTRA_libffi_la_SOURCES = \
src/aarch64/ffi.c src/aarch64/sysv.S \
src/aarch64/win64_armasm.S \
src/alpha/ffi.c src/alpha/osf.S \
src/arc/ffi.c src/arc/arcompact.S \
src/arm/ffi.c src/arm/sysv.S \
Expand Down
8 changes: 8 additions & 0 deletions configure.host
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
# THIS TABLE IS SORTED. KEEP IT THAT WAY.
# Most of the time we can define all the variables all at once...
case "${host}" in
aarch64*-*-cygwin* | aarch64*-*-mingw* | aarch64*-*-win* )
TARGET=ARM_WIN64; TARGETDIR=aarch64
MSVC=1
;;

aarch64*-*-*)
TARGET=AARCH64; TARGETDIR=aarch64
SOURCES="ffi.c sysv.S"
Expand Down Expand Up @@ -250,6 +255,9 @@ case "${TARGET}" in
ARM_WIN32)
SOURCES="ffi.c sysv_msvc_arm32.S"
;;
ARM_WIN64)
SOURCES="ffi.c win64_armasm.S"
;;
MIPS)
SOURCES="ffi.c o32.S n32.S"
;;
Expand Down
3 changes: 1 addition & 2 deletions msvc_build/aarch64/aarch64_include/ffi.h
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ FFI_EXTERN ffi_type ffi_type_float;
FFI_EXTERN ffi_type ffi_type_double;
FFI_EXTERN ffi_type ffi_type_pointer;

#ifndef _M_ARM64
#ifndef _M_ARM64
FFI_EXTERN ffi_type ffi_type_longdouble;
#else
#define ffi_type_longdouble ffi_type_double
Expand Down Expand Up @@ -227,7 +227,6 @@ typedef struct {
ffi_type *rtype;
unsigned bytes;
unsigned flags;
unsigned isVariadic;
#ifdef FFI_EXTRA_CIF_FIELDS
FFI_EXTRA_CIF_FIELDS;
#endif
Expand Down
11 changes: 11 additions & 0 deletions msvcc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ do
safeseh=
shift 1
;;
-marm64)
ml='armasm64'
safeseh=
shift 1
;;
-clang-cl)
cl="clang-cl"
shift 1
Expand Down Expand Up @@ -299,6 +304,10 @@ if [ -n "$assembly" ]; then
defines="$defines -D_M_ARM"
fi

if [ $ml = "armasm64" ]; then
defines="$defines -D_M_ARM64"
fi

if test -n "$verbose"; then
echo "$cl -nologo -EP $includes $defines $src > $ppsrc"
fi
Expand All @@ -307,6 +316,8 @@ if [ -n "$assembly" ]; then
output="$(echo $output | sed 's%/F[dpa][^ ]*%%g')"
if [ $ml = "armasm" ]; then
args="-nologo -g -oldit $armasm_output $ppsrc -errorReport:prompt"
elif [ $ml = "armasm64" ]; then
args="-nologo -g $armasm_output $ppsrc -errorReport:prompt"
else
args="-nologo $safeseh $single $output $ppsrc"
fi
Expand Down
12 changes: 9 additions & 3 deletions src/aarch64/ffi.c
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,7 @@ ffi_call_int (ffi_cif *cif, void (*fn)(void), void *orig_rvalue,
{
int elems = 4 - (h & 3);
#ifdef _M_ARM64 /* for handling armasm calling convention */
if (cif->isVariadic)
if (cif->is_variadic)
{
if (state.ngrn + elems <= N_X_ARG_REG)
{
Expand Down Expand Up @@ -808,7 +808,13 @@ ffi_prep_closure_loc (ffi_closure *closure,
ffi_clear_cache(tramp, tramp + FFI_TRAMPOLINE_SIZE);

/* Also flush the cache for code mapping. */
#ifdef _M_ARM64
// Not using dlmalloc.c for Windows ARM64 builds
// so calling ffi_data_to_code_pointer() isn't necessary
unsigned char *tramp_code = tramp;
#else
unsigned char *tramp_code = ffi_data_to_code_pointer (tramp);
#endif
ffi_clear_cache (tramp_code, tramp_code + FFI_TRAMPOLINE_SIZE);
#endif

Expand Down Expand Up @@ -909,7 +915,7 @@ ffi_closure_SYSV_inner (ffi_cif *cif,
{
n = 4 - (h & 3);
#ifdef _M_ARM64 /* for handling armasm calling convention */
if (cif->isVariadic)
if (cif->is_variadic)
{
if (state.ngrn + n <= N_X_ARG_REG)
{
Expand Down Expand Up @@ -948,7 +954,7 @@ ffi_closure_SYSV_inner (ffi_cif *cif,
avalue[i] = allocate_to_stack(&state, stack,
ty->alignment, s);
}
#ifdef _M_ARM64 /* for handling armasm calling convention */
#ifdef _M_ARM64 /* for handling armasm calling convention */
}
#endif /* for handling armasm calling convention */
}
Expand Down
4 changes: 4 additions & 0 deletions src/aarch64/ffitarget.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ typedef enum ffi_abi
#define FFI_TRAMPOLINE_CLOSURE_OFFSET FFI_TRAMPOLINE_SIZE
#endif

#ifdef _M_ARM64
#define FFI_EXTRA_CIF_FIELDS unsigned is_variadic
#endif

/* ---- Internal ---- */

#if defined (__APPLE__)
Expand Down
18 changes: 10 additions & 8 deletions src/aarch64/win64_armasm.S
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,14 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
x5 closure
*/

NESTED_ENTRY ffi_call_SYSV
/* Use a stack frame allocated by our caller. */
PROLOG_NOP stp x29, x30, [x1, #32]
/* For unwind information, Windows has to store fp and lr */
NESTED_ENTRY ffi_call_SYSV_fake

/* For unwind information, Windows has to store fp and lr */
PROLOG_SAVE_REG_PAIR x29, x30, #-32!


ALTERNATE_ENTRY ffi_call_SYSV
/* Use a stack frame allocated by our caller. */
stp x29, x30, [x1]
mov x29, x1
mov sp, x0

Expand Down Expand Up @@ -97,8 +99,8 @@ ffi_call_SYSV_L1

/* Partially deconstruct the stack frame. */
mov sp, x29
ldp x29, x30, [x29, #32]
ldp x29, x30, [x29]

/* Save the return value as directed. */
adr x5, ffi_call_SYSV_return
and w4, w4, #AARCH64_RET_MASK
Expand Down Expand Up @@ -177,7 +179,7 @@ ffi_call_SYSV_return
nop


NESTED_END ffi_call_SYSV
NESTED_END ffi_call_SYSV_fake


/* ffi_closure_SYSV
Expand Down
4 changes: 2 additions & 2 deletions src/prep_cif.c
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ ffi_status FFI_HIDDEN ffi_prep_cif_core(ffi_cif *cif, ffi_abi abi,
cif->rtype = rtype;

cif->flags = 0;
#ifdef _M_ARM64
cif->isVariadic = isvariadic;
#ifdef _M_ARM64
cif->is_variadic = isvariadic;
#endif
#if HAVE_LONG_DOUBLE_VARIANT
ffi_prep_types (abi);
Expand Down

0 comments on commit c2a6859

Please sign in to comment.