ffi 1.9.25 → 1.15.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +212 -0
- data/Gemfile +3 -4
- data/README.md +38 -18
- data/Rakefile +55 -146
- data/ext/ffi_c/AbstractMemory.c +29 -34
- data/ext/ffi_c/Buffer.c +4 -9
- data/ext/ffi_c/Call.c +16 -49
- data/ext/ffi_c/Call.h +4 -7
- data/ext/ffi_c/ClosurePool.c +75 -25
- data/ext/ffi_c/ClosurePool.h +3 -1
- data/ext/ffi_c/DynamicLibrary.c +2 -7
- data/ext/ffi_c/Function.c +47 -131
- data/ext/ffi_c/Function.h +0 -4
- data/ext/ffi_c/FunctionInfo.c +3 -8
- data/ext/ffi_c/LastError.c +49 -8
- data/ext/ffi_c/LongDouble.c +12 -10
- data/ext/ffi_c/LongDouble.h +0 -4
- data/ext/ffi_c/MemoryPointer.c +3 -8
- data/ext/ffi_c/MemoryPointer.h +0 -4
- data/ext/ffi_c/MethodHandle.c +21 -31
- data/ext/ffi_c/MethodHandle.h +3 -2
- data/ext/ffi_c/Platform.c +3 -54
- data/ext/ffi_c/Pointer.c +25 -26
- data/ext/ffi_c/Pointer.h +0 -4
- data/ext/ffi_c/Struct.c +49 -56
- data/ext/ffi_c/Struct.h +12 -6
- data/ext/ffi_c/StructByValue.c +2 -7
- data/ext/ffi_c/StructLayout.c +22 -20
- data/ext/ffi_c/Thread.c +4 -228
- data/ext/ffi_c/Thread.h +1 -20
- data/ext/ffi_c/Type.c +1 -19
- data/ext/ffi_c/Type.h +0 -1
- data/ext/ffi_c/Types.c +7 -8
- data/ext/ffi_c/Types.h +3 -4
- data/ext/ffi_c/Variadic.c +23 -24
- data/ext/ffi_c/compat.h +4 -0
- data/ext/ffi_c/extconf.rb +53 -30
- data/ext/ffi_c/ffi.c +3 -8
- data/ext/ffi_c/libffi.darwin.mk +1 -1
- data/ext/ffi_c/libffi/.appveyor.yml +29 -13
- data/ext/ffi_c/libffi/.gitattributes +4 -0
- data/ext/ffi_c/libffi/.travis.yml +51 -2
- data/ext/ffi_c/libffi/.travis/bfin-sim.exp +58 -0
- data/ext/ffi_c/libffi/.travis/build-cross-in-container.sh +14 -0
- data/ext/ffi_c/libffi/.travis/build-in-container.sh +12 -0
- data/ext/ffi_c/libffi/.travis/build.sh +116 -8
- data/ext/ffi_c/libffi/.travis/install.sh +65 -16
- data/ext/ffi_c/libffi/.travis/m32r-sim.exp +58 -0
- data/ext/ffi_c/libffi/.travis/moxie-sim.exp +1 -1
- data/ext/ffi_c/libffi/.travis/or1k-sim.exp +58 -0
- data/ext/ffi_c/libffi/.travis/powerpc-eabisim.exp +58 -0
- data/ext/ffi_c/libffi/.travis/site.exp +10 -1
- data/ext/ffi_c/libffi/.travis/wine-sim.exp +55 -0
- data/ext/ffi_c/libffi/{ChangeLog.libffi-3.1 → ChangeLog.old} +1407 -0
- data/ext/ffi_c/libffi/LICENSE +1 -1
- data/ext/ffi_c/libffi/LICENSE-BUILDTOOLS +5 -4
- data/ext/ffi_c/libffi/Makefile.am +56 -72
- data/ext/ffi_c/libffi/README.md +33 -8
- data/ext/ffi_c/libffi/config.guess +552 -331
- data/ext/ffi_c/libffi/config.sub +1321 -1306
- data/ext/ffi_c/libffi/configure.ac +37 -12
- data/ext/ffi_c/libffi/configure.host +56 -27
- data/ext/ffi_c/libffi/doc/Makefile.am +3 -0
- data/ext/ffi_c/libffi/doc/libffi.texi +997 -0
- data/ext/ffi_c/libffi/doc/version.texi +4 -0
- data/ext/ffi_c/libffi/generate-darwin-source-and-headers.py +2 -4
- data/ext/ffi_c/libffi/include/ffi.h.in +19 -7
- data/ext/ffi_c/libffi/include/ffi_common.h +5 -1
- data/ext/ffi_c/libffi/libffi.map.in +8 -12
- data/ext/ffi_c/libffi/libffi.xcodeproj/project.pbxproj +2 -48
- data/ext/ffi_c/libffi/libtool-version +1 -1
- data/ext/ffi_c/libffi/m4/asmcfi.m4 +1 -1
- data/ext/ffi_c/libffi/m4/ax_append_flag.m4 +5 -26
- data/ext/ffi_c/libffi/m4/ax_check_compile_flag.m4 +5 -26
- data/ext/ffi_c/libffi/m4/ax_compiler_vendor.m4 +2 -1
- data/ext/ffi_c/libffi/m4/ax_configure_args.m4 +5 -26
- data/ext/ffi_c/libffi/m4/ax_gcc_archflag.m4 +7 -3
- data/ext/ffi_c/libffi/make_sunver.pl +333 -0
- data/ext/ffi_c/libffi/msvc_build/aarch64/Ffi_staticLib.sln +33 -0
- data/ext/ffi_c/libffi/msvc_build/aarch64/Ffi_staticLib.vcxproj +130 -0
- data/ext/ffi_c/libffi/msvc_build/aarch64/Ffi_staticLib.vcxproj.filters +57 -0
- data/ext/ffi_c/libffi/msvc_build/aarch64/Ffi_staticLib.vcxproj.user +4 -0
- data/ext/ffi_c/libffi/msvc_build/aarch64/aarch64_include/ffi.h +511 -0
- data/ext/ffi_c/libffi/msvcc.sh +38 -13
- data/ext/ffi_c/libffi/src/aarch64/ffi.c +153 -69
- data/ext/ffi_c/libffi/src/aarch64/ffitarget.h +19 -3
- data/ext/ffi_c/libffi/src/aarch64/internal.h +1 -0
- data/ext/ffi_c/libffi/src/aarch64/sysv.S +22 -9
- data/ext/ffi_c/libffi/src/aarch64/win64_armasm.S +506 -0
- data/ext/ffi_c/libffi/src/arm/ffi.c +62 -5
- data/ext/ffi_c/libffi/src/arm/ffitarget.h +8 -1
- data/ext/ffi_c/libffi/src/arm/sysv.S +6 -4
- data/ext/ffi_c/libffi/src/arm/sysv_msvc_arm32.S +311 -0
- data/ext/ffi_c/libffi/src/closures.c +69 -14
- data/ext/ffi_c/libffi/src/csky/ffi.c +395 -0
- data/ext/ffi_c/libffi/src/csky/ffitarget.h +63 -0
- data/ext/ffi_c/libffi/src/csky/sysv.S +371 -0
- data/ext/ffi_c/libffi/src/dlmalloc.c +1 -1
- data/ext/ffi_c/libffi/src/frv/ffi.c +1 -1
- data/ext/ffi_c/libffi/src/kvx/asm.h +5 -0
- data/ext/ffi_c/libffi/src/kvx/ffi.c +273 -0
- data/ext/ffi_c/libffi/src/kvx/ffitarget.h +75 -0
- data/ext/ffi_c/libffi/src/kvx/sysv.S +127 -0
- data/ext/ffi_c/libffi/src/metag/ffi.c +1 -1
- data/ext/ffi_c/libffi/src/mips/ffi.c +5 -1
- data/ext/ffi_c/libffi/src/mips/ffitarget.h +1 -1
- data/ext/ffi_c/libffi/src/mips/o32.S +2 -0
- data/ext/ffi_c/libffi/src/moxie/ffi.c +1 -1
- data/ext/ffi_c/libffi/src/pa/ffi.c +46 -91
- data/ext/ffi_c/libffi/src/pa/ffitarget.h +1 -6
- data/ext/ffi_c/libffi/src/pa/hpux32.S +4 -2
- data/ext/ffi_c/libffi/src/pa/linux.S +27 -4
- data/ext/ffi_c/libffi/src/powerpc/ffi.c +6 -4
- data/ext/ffi_c/libffi/src/powerpc/ffi_darwin.c +13 -1
- data/ext/ffi_c/libffi/src/powerpc/ffi_linux64.c +211 -32
- data/ext/ffi_c/libffi/src/powerpc/ffi_powerpc.h +18 -7
- data/ext/ffi_c/libffi/src/powerpc/ffitarget.h +10 -4
- data/ext/ffi_c/libffi/src/powerpc/linux64.S +91 -28
- data/ext/ffi_c/libffi/src/powerpc/linux64_closure.S +81 -5
- data/ext/ffi_c/libffi/src/powerpc/sysv.S +5 -7
- data/ext/ffi_c/libffi/src/prep_cif.c +4 -2
- data/ext/ffi_c/libffi/src/riscv/ffi.c +42 -6
- data/ext/ffi_c/libffi/src/riscv/ffitarget.h +1 -0
- data/ext/ffi_c/libffi/src/riscv/sysv.S +86 -7
- data/ext/ffi_c/libffi/src/x86/ffi.c +29 -12
- data/ext/ffi_c/libffi/src/x86/ffi64.c +22 -11
- data/ext/ffi_c/libffi/src/x86/ffitarget.h +15 -2
- data/ext/ffi_c/libffi/src/x86/ffiw64.c +20 -10
- data/ext/ffi_c/libffi/src/x86/sysv.S +105 -10
- data/ext/ffi_c/libffi/src/x86/sysv_intel.S +995 -0
- data/ext/ffi_c/libffi/src/x86/unix64.S +100 -4
- data/ext/ffi_c/libffi/src/x86/win64.S +12 -3
- data/ext/ffi_c/libffi/src/x86/win64_intel.S +3 -2
- data/ext/ffi_c/libffi/testsuite/Makefile.am +114 -109
- data/ext/ffi_c/libffi/testsuite/lib/libffi.exp +32 -8
- data/ext/ffi_c/libffi/testsuite/libffi.bhaible/bhaible.exp +7 -2
- data/ext/ffi_c/libffi/testsuite/libffi.bhaible/test-call.c +4 -4
- data/ext/ffi_c/libffi/testsuite/libffi.bhaible/test-callback.c +2 -2
- data/ext/ffi_c/libffi/testsuite/libffi.call/call.exp +12 -1
- data/ext/ffi_c/libffi/testsuite/libffi.call/float2.c +9 -8
- data/ext/ffi_c/libffi/testsuite/libffi.call/va_1.c +1 -1
- data/ext/ffi_c/libffi/testsuite/libffi.closures/closure.exp +67 -0
- data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/closure_fn0.c +0 -0
- data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/closure_fn1.c +0 -0
- data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/closure_fn2.c +0 -0
- data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/closure_fn3.c +0 -0
- data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/closure_fn4.c +0 -0
- data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/closure_fn5.c +0 -0
- data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/closure_fn6.c +0 -0
- data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/closure_loc_fn0.c +0 -0
- data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/closure_simple.c +0 -0
- data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_12byte.c +0 -0
- data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_16byte.c +0 -0
- data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_18byte.c +0 -0
- data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_19byte.c +0 -0
- data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_1_1byte.c +0 -0
- data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_20byte.c +0 -0
- data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_20byte1.c +0 -0
- data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_24byte.c +0 -0
- data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_2byte.c +0 -0
- data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_3_1byte.c +0 -0
- data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_3byte1.c +0 -0
- data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_3byte2.c +0 -0
- data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_3float.c +0 -0
- data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_4_1byte.c +0 -0
- data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_4byte.c +0 -0
- data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_5_1_byte.c +0 -0
- data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_5byte.c +0 -0
- data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_64byte.c +0 -0
- data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_6_1_byte.c +0 -0
- data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_6byte.c +0 -0
- data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_7_1_byte.c +0 -0
- data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_7byte.c +0 -0
- data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_8byte.c +0 -0
- data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_9byte1.c +0 -0
- data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_9byte2.c +0 -0
- data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_align_double.c +0 -0
- data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_align_float.c +0 -0
- data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_align_longdouble.c +0 -0
- data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_align_longdouble_split.c +0 -0
- data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_align_longdouble_split2.c +0 -0
- data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_align_pointer.c +0 -0
- data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_align_sint16.c +0 -0
- data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_align_sint32.c +0 -0
- data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_align_sint64.c +0 -0
- data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_align_uint16.c +0 -0
- data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_align_uint32.c +0 -0
- data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_align_uint64.c +0 -0
- data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_dbls_struct.c +0 -0
- data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_double.c +0 -0
- data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_double_va.c +0 -0
- data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_float.c +0 -0
- data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_longdouble.c +0 -0
- data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_longdouble_va.c +0 -0
- data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_many_mixed_args.c +0 -0
- data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_many_mixed_float_double.c +0 -0
- data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_multi_schar.c +0 -0
- data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_multi_sshort.c +0 -0
- data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_multi_sshortchar.c +0 -0
- data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_multi_uchar.c +0 -0
- data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_multi_ushort.c +0 -0
- data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_multi_ushortchar.c +0 -0
- data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_pointer.c +0 -0
- data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_pointer_stack.c +0 -0
- data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_schar.c +0 -0
- data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_sint.c +0 -0
- data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_sshort.c +0 -0
- data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_struct_va1.c +0 -0
- data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_uchar.c +0 -0
- data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_uchar_va.c +0 -0
- data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_uint.c +0 -0
- data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_uint_va.c +0 -0
- data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_ulong_va.c +0 -0
- data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_ulonglong.c +0 -0
- data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_ushort.c +0 -0
- data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_ushort_va.c +0 -0
- data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/err_bad_abi.c +0 -0
- data/ext/ffi_c/libffi/testsuite/libffi.closures/ffitest.h +138 -0
- data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/huge_struct.c +3 -1
- data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/nested_struct.c +0 -0
- data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/nested_struct1.c +0 -0
- data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/nested_struct10.c +0 -0
- data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/nested_struct11.c +0 -0
- data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/nested_struct2.c +0 -0
- data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/nested_struct3.c +0 -0
- data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/nested_struct4.c +0 -0
- data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/nested_struct5.c +0 -0
- data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/nested_struct6.c +0 -0
- data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/nested_struct7.c +0 -0
- data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/nested_struct8.c +0 -0
- data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/nested_struct9.c +0 -0
- data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/problem1.c +0 -0
- data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/stret_large.c +0 -0
- data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/stret_large2.c +0 -0
- data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/stret_medium.c +0 -0
- data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/stret_medium2.c +0 -0
- data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/testclosure.c +0 -0
- data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/unwindtest.cc +0 -0
- data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/unwindtest_ffi_call.cc +0 -0
- data/ext/ffi_c/rbffi.h +0 -2
- data/ffi.gemspec +13 -6
- data/lib/ffi.rb +10 -2
- data/lib/ffi/abstract_memory.rb +44 -0
- data/lib/ffi/autopointer.rb +1 -1
- data/lib/ffi/data_converter.rb +67 -0
- data/lib/ffi/ffi.rb +3 -0
- data/lib/ffi/io.rb +3 -3
- data/lib/ffi/library.rb +12 -8
- data/lib/ffi/managedstruct.rb +2 -2
- data/lib/ffi/platform.rb +34 -10
- data/lib/ffi/platform/aarch64-darwin/types.conf +130 -0
- data/lib/ffi/platform/aarch64-freebsd/types.conf +128 -0
- data/lib/ffi/platform/aarch64-freebsd12/types.conf +128 -0
- data/lib/ffi/platform/aarch64-linux/types.conf +81 -81
- data/lib/ffi/platform/aarch64-openbsd/types.conf +134 -0
- data/lib/ffi/platform/arm-freebsd/types.conf +152 -0
- data/lib/ffi/platform/arm-freebsd12/types.conf +152 -0
- data/lib/ffi/platform/arm-linux/types.conf +110 -82
- data/lib/ffi/platform/i386-cygwin/types.conf +1 -1
- data/lib/ffi/platform/i386-darwin/types.conf +63 -63
- data/lib/ffi/platform/i386-freebsd/types.conf +89 -89
- data/lib/ffi/platform/i386-freebsd12/types.conf +152 -0
- data/lib/ffi/platform/i386-gnu/types.conf +84 -84
- data/lib/ffi/platform/i386-linux/types.conf +77 -77
- data/lib/ffi/platform/i386-netbsd/types.conf +87 -87
- data/lib/ffi/platform/i386-openbsd/types.conf +89 -89
- data/lib/ffi/platform/i386-solaris/types.conf +96 -96
- data/lib/ffi/platform/i386-windows/types.conf +43 -96
- data/lib/ffi/platform/ia64-linux/types.conf +79 -79
- data/lib/ffi/platform/mips-linux/types.conf +79 -79
- data/lib/ffi/platform/mips64-linux/types.conf +81 -81
- data/lib/ffi/platform/mips64el-linux/types.conf +81 -81
- data/lib/ffi/platform/mipsel-linux/types.conf +79 -79
- data/lib/ffi/platform/mipsisa32r6-linux/types.conf +79 -79
- data/lib/ffi/platform/mipsisa32r6el-linux/types.conf +79 -79
- data/lib/ffi/platform/mipsisa64r6-linux/types.conf +81 -81
- data/lib/ffi/platform/mipsisa64r6el-linux/types.conf +81 -81
- data/lib/ffi/platform/powerpc-aix/types.conf +155 -155
- data/lib/ffi/platform/powerpc-darwin/types.conf +63 -63
- data/lib/ffi/platform/powerpc-linux/types.conf +108 -78
- data/lib/ffi/platform/powerpc-openbsd/types.conf +156 -0
- data/lib/ffi/platform/powerpc64-linux/types.conf +81 -81
- data/lib/ffi/platform/powerpc64le-linux/types.conf +100 -0
- data/lib/ffi/platform/riscv64-linux/types.conf +104 -0
- data/lib/ffi/platform/s390-linux/types.conf +79 -79
- data/lib/ffi/platform/s390x-linux/types.conf +79 -79
- data/lib/ffi/platform/sparc-linux/types.conf +79 -79
- data/lib/ffi/platform/sparc-solaris/types.conf +103 -103
- data/lib/ffi/platform/sparc64-linux/types.conf +79 -79
- data/lib/ffi/platform/sparcv9-openbsd/types.conf +156 -0
- data/lib/ffi/platform/sparcv9-solaris/types.conf +103 -103
- data/lib/ffi/platform/x86_64-cygwin/types.conf +1 -1
- data/lib/ffi/platform/x86_64-darwin/types.conf +88 -84
- data/lib/ffi/platform/x86_64-dragonflybsd/types.conf +130 -0
- data/lib/ffi/platform/x86_64-freebsd/types.conf +90 -90
- data/lib/ffi/platform/x86_64-freebsd12/types.conf +158 -0
- data/lib/ffi/platform/x86_64-haiku/types.conf +117 -0
- data/lib/ffi/platform/x86_64-linux/types.conf +107 -77
- data/lib/ffi/platform/x86_64-msys/types.conf +119 -0
- data/lib/ffi/platform/x86_64-netbsd/types.conf +89 -89
- data/lib/ffi/platform/x86_64-openbsd/types.conf +86 -86
- data/lib/ffi/platform/x86_64-solaris/types.conf +96 -96
- data/lib/ffi/platform/x86_64-windows/types.conf +42 -110
- data/lib/ffi/pointer.rb +26 -20
- data/lib/ffi/struct.rb +13 -68
- data/lib/ffi/struct_by_reference.rb +72 -0
- data/lib/ffi/struct_layout.rb +96 -0
- data/lib/ffi/tools/const_generator.rb +5 -4
- data/lib/ffi/tools/generator.rb +47 -2
- data/lib/ffi/tools/generator_task.rb +13 -17
- data/lib/ffi/tools/struct_generator.rb +4 -4
- data/lib/ffi/tools/types_generator.rb +7 -4
- data/lib/ffi/types.rb +1 -1
- data/lib/ffi/variadic.rb +1 -10
- data/lib/ffi/version.rb +1 -1
- data/rakelib/ffi_gem_helper.rb +65 -0
- data/samples/getlogin.rb +1 -1
- data/samples/getpid.rb +1 -1
- data/samples/gettimeofday.rb +8 -8
- data/samples/hello.rb +2 -1
- data/samples/inotify.rb +1 -1
- data/samples/pty.rb +1 -2
- data/samples/qsort.rb +0 -1
- metadata +160 -122
- data/.gitignore +0 -22
- data/.gitmodules +0 -3
- data/.travis.yml +0 -52
- data/.yardopts +0 -5
- data/appveyor.yml +0 -22
- data/ext/ffi_c/DataConverter.c +0 -91
- data/ext/ffi_c/StructByReference.c +0 -190
- data/ext/ffi_c/StructByReference.h +0 -50
- data/ext/ffi_c/libffi/ChangeLog.libffi +0 -584
- data/ext/ffi_c/libffi/ChangeLog.libgcj +0 -40
- data/ext/ffi_c/libffi/ChangeLog.v1 +0 -764
- data/ext/ffi_c/win32/stdbool.h +0 -8
- data/ext/ffi_c/win32/stdint.h +0 -201
- data/samples/sample_helper.rb +0 -6
@@ -2,7 +2,7 @@ dnl Process this with autoconf to create configure
|
|
2
2
|
|
3
3
|
AC_PREREQ(2.68)
|
4
4
|
|
5
|
-
AC_INIT([libffi], [3.3
|
5
|
+
AC_INIT([libffi], [3.3], [http://github.com/libffi/libffi/issues])
|
6
6
|
AC_CONFIG_HEADERS([fficonfig.h])
|
7
7
|
|
8
8
|
AC_CANONICAL_SYSTEM
|
@@ -63,6 +63,9 @@ EOF
|
|
63
63
|
|
64
64
|
AM_MAINTAINER_MODE
|
65
65
|
|
66
|
+
AC_CHECK_HEADERS(sys/memfd.h)
|
67
|
+
AC_CHECK_FUNCS([memfd_create])
|
68
|
+
|
66
69
|
AC_CHECK_HEADERS(sys/mman.h)
|
67
70
|
AC_CHECK_FUNCS([mmap mkostemp])
|
68
71
|
AC_FUNC_MMAP_BLACKLIST
|
@@ -176,6 +179,28 @@ case "$TARGET" in
|
|
176
179
|
;;
|
177
180
|
esac
|
178
181
|
|
182
|
+
AC_CACHE_CHECK([whether compiler supports pointer authentication],
|
183
|
+
libffi_cv_as_ptrauth, [
|
184
|
+
libffi_cv_as_ptrauth=unknown
|
185
|
+
AC_TRY_COMPILE(,[
|
186
|
+
#ifdef __clang__
|
187
|
+
# if __has_feature(ptrauth_calls)
|
188
|
+
# define HAVE_PTRAUTH 1
|
189
|
+
# endif
|
190
|
+
#endif
|
191
|
+
|
192
|
+
#ifndef HAVE_PTRAUTH
|
193
|
+
# error Pointer authentication not supported
|
194
|
+
#endif
|
195
|
+
],
|
196
|
+
[libffi_cv_as_ptrauth=yes],
|
197
|
+
[libffi_cv_as_ptrauth=no])
|
198
|
+
])
|
199
|
+
if test "x$libffi_cv_as_ptrauth" = xyes; then
|
200
|
+
AC_DEFINE(HAVE_PTRAUTH, 1,
|
201
|
+
[Define if your compiler supports pointer authentication.])
|
202
|
+
fi
|
203
|
+
|
179
204
|
# On PaX enable kernels that have MPROTECT enable we can't use PROT_EXEC.
|
180
205
|
AC_ARG_ENABLE(pax_emutramp,
|
181
206
|
[ --enable-pax_emutramp enable pax emulated trampolines, for we can't use PROT_EXEC],
|
@@ -238,19 +263,15 @@ EOF
|
|
238
263
|
fi
|
239
264
|
|
240
265
|
if test "x$GCC" = "xyes"; then
|
266
|
+
AX_CHECK_COMPILE_FLAG(-fno-lto, libffi_cv_no_lto=-fno-lto)
|
267
|
+
|
241
268
|
AC_CACHE_CHECK([whether .eh_frame section should be read-only],
|
242
269
|
libffi_cv_ro_eh_frame, [
|
243
|
-
libffi_cv_ro_eh_frame=
|
270
|
+
libffi_cv_ro_eh_frame=yes
|
244
271
|
echo 'extern void foo (void); void bar (void) { foo (); foo (); }' > conftest.c
|
245
|
-
if $CC $CFLAGS -c -fpic -fexceptions -o conftest.o conftest.c > /dev/null 2>&1; then
|
246
|
-
|
247
|
-
|
248
|
-
if test "x$libffi_eh_frame_line" != "x"; then
|
249
|
-
libffi_test_line=`expr $libffi_eh_frame_line + 1`p
|
250
|
-
sed -n $libffi_test_line conftest.dump > conftest.line
|
251
|
-
if grep READONLY conftest.line > /dev/null; then
|
252
|
-
libffi_cv_ro_eh_frame=yes
|
253
|
-
fi
|
272
|
+
if $CC $CFLAGS -c -fpic -fexceptions $libffi_cv_no_lto -o conftest.o conftest.c > /dev/null 2>&1; then
|
273
|
+
if readelf -WS conftest.o | grep -q -n 'eh_frame .* WA'; then
|
274
|
+
libffi_cv_ro_eh_frame=no
|
254
275
|
fi
|
255
276
|
fi
|
256
277
|
rm -f conftest.*
|
@@ -270,7 +291,7 @@ if test "x$GCC" = "xyes"; then
|
|
270
291
|
echo 'int __attribute__ ((visibility ("hidden"))) foo (void) { return 1 ; }' > conftest.c
|
271
292
|
libffi_cv_hidden_visibility_attribute=no
|
272
293
|
if AC_TRY_COMMAND(${CC-cc} -Werror -S conftest.c -o conftest.s 1>&AS_MESSAGE_LOG_FD); then
|
273
|
-
if
|
294
|
+
if egrep '(\.hidden|\.private_extern).*foo' conftest.s >/dev/null; then
|
274
295
|
libffi_cv_hidden_visibility_attribute=yes
|
275
296
|
fi
|
276
297
|
fi
|
@@ -292,7 +313,11 @@ AM_CONDITIONAL(BUILD_DOCS, [test x$enable_docs = xyes])
|
|
292
313
|
AH_BOTTOM([
|
293
314
|
#ifdef HAVE_HIDDEN_VISIBILITY_ATTRIBUTE
|
294
315
|
#ifdef LIBFFI_ASM
|
316
|
+
#ifdef __APPLE__
|
317
|
+
#define FFI_HIDDEN(name) .private_extern name
|
318
|
+
#else
|
295
319
|
#define FFI_HIDDEN(name) .hidden name
|
320
|
+
#endif
|
296
321
|
#else
|
297
322
|
#define FFI_HIDDEN __attribute__ ((visibility ("hidden")))
|
298
323
|
#endif
|
@@ -6,6 +6,13 @@
|
|
6
6
|
# THIS TABLE IS SORTED. KEEP IT THAT WAY.
|
7
7
|
# Most of the time we can define all the variables all at once...
|
8
8
|
case "${host}" in
|
9
|
+
aarch64*-*-cygwin* | aarch64*-*-mingw* | aarch64*-*-win* )
|
10
|
+
TARGET=ARM_WIN64; TARGETDIR=aarch64
|
11
|
+
if test "${ax_cv_c_compiler_vendor}" = "microsoft"; then
|
12
|
+
MSVC=1
|
13
|
+
fi
|
14
|
+
;;
|
15
|
+
|
9
16
|
aarch64*-*-*)
|
10
17
|
TARGET=AARCH64; TARGETDIR=aarch64
|
11
18
|
SOURCES="ffi.c sysv.S"
|
@@ -23,6 +30,11 @@ case "${host}" in
|
|
23
30
|
SOURCES="ffi.c arcompact.S"
|
24
31
|
;;
|
25
32
|
|
33
|
+
arm*-*-cygwin* | arm*-*-mingw* | arm*-*-win* )
|
34
|
+
TARGET=ARM_WIN32; TARGETDIR=arm
|
35
|
+
MSVC=1
|
36
|
+
;;
|
37
|
+
|
26
38
|
arm*-*-*)
|
27
39
|
TARGET=ARM; TARGETDIR=arm
|
28
40
|
SOURCES="ffi.c sysv.S"
|
@@ -43,6 +55,11 @@ case "${host}" in
|
|
43
55
|
SOURCES="ffi.c sysv.S"
|
44
56
|
;;
|
45
57
|
|
58
|
+
csky-*-*)
|
59
|
+
TARGET=CSKY; TARGETDIR=csky
|
60
|
+
SOURCES="ffi.c sysv.S"
|
61
|
+
;;
|
62
|
+
|
46
63
|
frv-*-*)
|
47
64
|
TARGET=FRV; TARGETDIR=frv
|
48
65
|
SOURCES="ffi.c eabi.S"
|
@@ -64,7 +81,7 @@ case "${host}" in
|
|
64
81
|
TARGET=X86_FREEBSD; TARGETDIR=x86
|
65
82
|
;;
|
66
83
|
|
67
|
-
i?86-*-cygwin* | i?86-*-mingw* | i?86-*-os2* | i?86-*-interix* \
|
84
|
+
i?86-*-cygwin* | i?86-*-mingw* | i?86-*-win* | i?86-*-os2* | i?86-*-interix* \
|
68
85
|
| x86_64-*-cygwin* | x86_64-*-mingw* | x86_64-*-win* )
|
69
86
|
TARGETDIR=x86
|
70
87
|
if test $ac_cv_sizeof_size_t = 4; then
|
@@ -74,12 +91,6 @@ case "${host}" in
|
|
74
91
|
fi
|
75
92
|
if test "${ax_cv_c_compiler_vendor}" = "microsoft"; then
|
76
93
|
MSVC=1
|
77
|
-
if test $ac_cv_sizeof_size_t = 4; then
|
78
|
-
# libffi does not support microsoft tools for 32-bit windows
|
79
|
-
# hosts. Try porting src/x86/sysv.S to intel assembly
|
80
|
-
# format.
|
81
|
-
UNSUPPORTED=1
|
82
|
-
fi
|
83
94
|
fi
|
84
95
|
# All mingw/cygwin/win32 builds require -no-undefined for sharedlib.
|
85
96
|
# We must also check with_cross_host to decide if this is a native
|
@@ -104,22 +115,14 @@ case "${host}" in
|
|
104
115
|
i?86-*-* | x86_64-*-* | amd64-*)
|
105
116
|
TARGETDIR=x86
|
106
117
|
if test $ac_cv_sizeof_size_t = 4; then
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
TARGET_X32=yes
|
116
|
-
TARGET=X86_64
|
117
|
-
else
|
118
|
-
TARGET=X86;
|
119
|
-
fi
|
120
|
-
rm -f conftest.*
|
121
|
-
;;
|
122
|
-
esac
|
118
|
+
echo 'int foo (void) { return __x86_64__; }' > conftest.c
|
119
|
+
if $CC $CFLAGS -Werror -S conftest.c -o conftest.s > /dev/null 2>&1; then
|
120
|
+
TARGET_X32=yes
|
121
|
+
TARGET=X86_64
|
122
|
+
else
|
123
|
+
TARGET=X86;
|
124
|
+
fi
|
125
|
+
rm -f conftest.*
|
123
126
|
else
|
124
127
|
TARGET=X86_64;
|
125
128
|
fi
|
@@ -130,6 +133,11 @@ case "${host}" in
|
|
130
133
|
SOURCES="ffi.c unix.S"
|
131
134
|
;;
|
132
135
|
|
136
|
+
kvx-*-*)
|
137
|
+
TARGET=KVX; TARGETDIR=kvx
|
138
|
+
SOURCES="ffi.c sysv.S"
|
139
|
+
;;
|
140
|
+
|
133
141
|
m32r*-*-*)
|
134
142
|
TARGET=M32R; TARGETDIR=m32r
|
135
143
|
SOURCES="ffi.c sysv.S"
|
@@ -163,7 +171,7 @@ case "${host}" in
|
|
163
171
|
mips-sgi-irix5.* | mips-sgi-irix6.* | mips*-*-rtems*)
|
164
172
|
TARGET=MIPS; TARGETDIR=mips
|
165
173
|
;;
|
166
|
-
mips*-*linux* | mips*-*-openbsd*)
|
174
|
+
mips*-*linux* | mips*-*-openbsd* | mips*-*-freebsd*)
|
167
175
|
# Support 128-bit long double for NewABI.
|
168
176
|
HAVE_LONG_DOUBLE='defined(__mips64)'
|
169
177
|
TARGET=MIPS; TARGETDIR=mips
|
@@ -174,7 +182,7 @@ case "${host}" in
|
|
174
182
|
SOURCES="ffi.c sysv.S"
|
175
183
|
;;
|
176
184
|
|
177
|
-
or1k
|
185
|
+
or1k*-*-*)
|
178
186
|
TARGET=OR1K; TARGETDIR=or1k
|
179
187
|
SOURCES="ffi.c sysv.S"
|
180
188
|
;;
|
@@ -186,6 +194,9 @@ case "${host}" in
|
|
186
194
|
powerpc-*-amigaos*)
|
187
195
|
TARGET=POWERPC; TARGETDIR=powerpc
|
188
196
|
;;
|
197
|
+
powerpc-*-eabi*)
|
198
|
+
TARGET=POWERPC; TARGETDIR=powerpc
|
199
|
+
;;
|
189
200
|
powerpc-*-beos*)
|
190
201
|
TARGET=POWERPC; TARGETDIR=powerpc
|
191
202
|
;;
|
@@ -199,7 +210,11 @@ case "${host}" in
|
|
199
210
|
TARGET=POWERPC_FREEBSD; TARGETDIR=powerpc
|
200
211
|
HAVE_LONG_DOUBLE_VARIANT=1
|
201
212
|
;;
|
202
|
-
|
213
|
+
powerpcspe-*-freebsd*)
|
214
|
+
TARGET=POWERPC_FREEBSD; TARGETDIR=powerpc
|
215
|
+
CFLAGS="$CFLAGS -D__NO_FPRS__"
|
216
|
+
;;
|
217
|
+
powerpc64-*-freebsd* | powerpc64le-*-freebsd*)
|
203
218
|
TARGET=POWERPC; TARGETDIR=powerpc
|
204
219
|
;;
|
205
220
|
powerpc*-*-rtems*)
|
@@ -248,6 +263,16 @@ esac
|
|
248
263
|
|
249
264
|
# ... but some of the cases above share configury.
|
250
265
|
case "${TARGET}" in
|
266
|
+
ARM_WIN32)
|
267
|
+
SOURCES="ffi.c sysv_msvc_arm32.S"
|
268
|
+
;;
|
269
|
+
ARM_WIN64)
|
270
|
+
if test "$MSVC" = 1; then
|
271
|
+
SOURCES="ffi.c win64_armasm.S"
|
272
|
+
else
|
273
|
+
SOURCES="ffi.c sysv.S"
|
274
|
+
fi
|
275
|
+
;;
|
251
276
|
MIPS)
|
252
277
|
SOURCES="ffi.c o32.S n32.S"
|
253
278
|
;;
|
@@ -265,7 +290,11 @@ case "${TARGET}" in
|
|
265
290
|
SOURCES="ffi.c ffi_sysv.c sysv.S ppc_closure.S"
|
266
291
|
;;
|
267
292
|
X86 | X86_DARWIN | X86_FREEBSD | X86_WIN32)
|
268
|
-
|
293
|
+
if test "$MSVC" = 1; then
|
294
|
+
SOURCES="ffi.c sysv_intel.S"
|
295
|
+
else
|
296
|
+
SOURCES="ffi.c sysv.S"
|
297
|
+
fi
|
269
298
|
;;
|
270
299
|
X86_64)
|
271
300
|
if test x"$TARGET_X32" = xyes; then
|
@@ -0,0 +1,997 @@
|
|
1
|
+
\input texinfo @c -*-texinfo-*-
|
2
|
+
@c %**start of header
|
3
|
+
@setfilename libffi.info
|
4
|
+
@include version.texi
|
5
|
+
@settitle libffi: the portable foreign function interface library
|
6
|
+
@setchapternewpage off
|
7
|
+
@c %**end of header
|
8
|
+
|
9
|
+
@c Merge the standard indexes into a single one.
|
10
|
+
@syncodeindex fn cp
|
11
|
+
@syncodeindex vr cp
|
12
|
+
@syncodeindex ky cp
|
13
|
+
@syncodeindex pg cp
|
14
|
+
@syncodeindex tp cp
|
15
|
+
|
16
|
+
@copying
|
17
|
+
|
18
|
+
This manual is for libffi, a portable foreign function interface
|
19
|
+
library.
|
20
|
+
|
21
|
+
Copyright @copyright{} 2008--2019 Anthony Green and Red Hat, Inc.
|
22
|
+
|
23
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
24
|
+
a copy of this software and associated documentation files (the
|
25
|
+
``Software''), to deal in the Software without restriction, including
|
26
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
27
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
28
|
+
permit persons to whom the Software is furnished to do so, subject to
|
29
|
+
the following conditions:
|
30
|
+
|
31
|
+
The above copyright notice and this permission notice shall be
|
32
|
+
included in all copies or substantial portions of the Software.
|
33
|
+
|
34
|
+
THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND,
|
35
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
36
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
37
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
38
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
39
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
40
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
41
|
+
|
42
|
+
@end copying
|
43
|
+
|
44
|
+
@dircategory Development
|
45
|
+
@direntry
|
46
|
+
* libffi: (libffi). Portable foreign function interface library.
|
47
|
+
@end direntry
|
48
|
+
|
49
|
+
@titlepage
|
50
|
+
@title libffi: a foreign function interface library
|
51
|
+
@subtitle For Version @value{VERSION} of libffi
|
52
|
+
@author Anthony Green
|
53
|
+
@page
|
54
|
+
@vskip 0pt plus 1filll
|
55
|
+
@insertcopying
|
56
|
+
@end titlepage
|
57
|
+
|
58
|
+
|
59
|
+
@ifnottex
|
60
|
+
@node Top
|
61
|
+
@top libffi
|
62
|
+
|
63
|
+
@insertcopying
|
64
|
+
|
65
|
+
@menu
|
66
|
+
* Introduction:: What is libffi?
|
67
|
+
* Using libffi:: How to use libffi.
|
68
|
+
* Missing Features:: Things libffi can't do.
|
69
|
+
* Index:: Index.
|
70
|
+
@end menu
|
71
|
+
|
72
|
+
@end ifnottex
|
73
|
+
|
74
|
+
|
75
|
+
@node Introduction
|
76
|
+
@chapter What is libffi?
|
77
|
+
|
78
|
+
Compilers for high level languages generate code that follow certain
|
79
|
+
conventions. These conventions are necessary, in part, for separate
|
80
|
+
compilation to work. One such convention is the @dfn{calling
|
81
|
+
convention}. The calling convention is a set of assumptions made by
|
82
|
+
the compiler about where function arguments will be found on entry to
|
83
|
+
a function. A calling convention also specifies where the return
|
84
|
+
value for a function is found. The calling convention is also
|
85
|
+
sometimes called the @dfn{ABI} or @dfn{Application Binary Interface}.
|
86
|
+
@cindex calling convention
|
87
|
+
@cindex ABI
|
88
|
+
@cindex Application Binary Interface
|
89
|
+
|
90
|
+
Some programs may not know at the time of compilation what arguments
|
91
|
+
are to be passed to a function. For instance, an interpreter may be
|
92
|
+
told at run-time about the number and types of arguments used to call
|
93
|
+
a given function. @samp{Libffi} can be used in such programs to
|
94
|
+
provide a bridge from the interpreter program to compiled code.
|
95
|
+
|
96
|
+
The @samp{libffi} library provides a portable, high level programming
|
97
|
+
interface to various calling conventions. This allows a programmer to
|
98
|
+
call any function specified by a call interface description at run
|
99
|
+
time.
|
100
|
+
|
101
|
+
@acronym{FFI} stands for Foreign Function Interface. A foreign
|
102
|
+
function interface is the popular name for the interface that allows
|
103
|
+
code written in one language to call code written in another language.
|
104
|
+
The @samp{libffi} library really only provides the lowest, machine
|
105
|
+
dependent layer of a fully featured foreign function interface. A
|
106
|
+
layer must exist above @samp{libffi} that handles type conversions for
|
107
|
+
values passed between the two languages.
|
108
|
+
@cindex FFI
|
109
|
+
@cindex Foreign Function Interface
|
110
|
+
|
111
|
+
|
112
|
+
@node Using libffi
|
113
|
+
@chapter Using libffi
|
114
|
+
|
115
|
+
@menu
|
116
|
+
* The Basics:: The basic libffi API.
|
117
|
+
* Simple Example:: A simple example.
|
118
|
+
* Types:: libffi type descriptions.
|
119
|
+
* Multiple ABIs:: Different passing styles on one platform.
|
120
|
+
* The Closure API:: Writing a generic function.
|
121
|
+
* Closure Example:: A closure example.
|
122
|
+
* Thread Safety:: Thread safety.
|
123
|
+
@end menu
|
124
|
+
|
125
|
+
|
126
|
+
@node The Basics
|
127
|
+
@section The Basics
|
128
|
+
|
129
|
+
@samp{Libffi} assumes that you have a pointer to the function you wish
|
130
|
+
to call and that you know the number and types of arguments to pass
|
131
|
+
it, as well as the return type of the function.
|
132
|
+
|
133
|
+
The first thing you must do is create an @code{ffi_cif} object that
|
134
|
+
matches the signature of the function you wish to call. This is a
|
135
|
+
separate step because it is common to make multiple calls using a
|
136
|
+
single @code{ffi_cif}. The @dfn{cif} in @code{ffi_cif} stands for
|
137
|
+
Call InterFace. To prepare a call interface object, use the function
|
138
|
+
@code{ffi_prep_cif}.
|
139
|
+
@cindex cif
|
140
|
+
|
141
|
+
@findex ffi_prep_cif
|
142
|
+
@defun ffi_status ffi_prep_cif (ffi_cif *@var{cif}, ffi_abi @var{abi}, unsigned int @var{nargs}, ffi_type *@var{rtype}, ffi_type **@var{argtypes})
|
143
|
+
This initializes @var{cif} according to the given parameters.
|
144
|
+
|
145
|
+
@var{abi} is the ABI to use; normally @code{FFI_DEFAULT_ABI} is what
|
146
|
+
you want. @ref{Multiple ABIs} for more information.
|
147
|
+
|
148
|
+
@var{nargs} is the number of arguments that this function accepts.
|
149
|
+
|
150
|
+
@var{rtype} is a pointer to an @code{ffi_type} structure that
|
151
|
+
describes the return type of the function. @xref{Types}.
|
152
|
+
|
153
|
+
@var{argtypes} is a vector of @code{ffi_type} pointers.
|
154
|
+
@var{argtypes} must have @var{nargs} elements. If @var{nargs} is 0,
|
155
|
+
this argument is ignored.
|
156
|
+
|
157
|
+
@code{ffi_prep_cif} returns a @code{libffi} status code, of type
|
158
|
+
@code{ffi_status}. This will be either @code{FFI_OK} if everything
|
159
|
+
worked properly; @code{FFI_BAD_TYPEDEF} if one of the @code{ffi_type}
|
160
|
+
objects is incorrect; or @code{FFI_BAD_ABI} if the @var{abi} parameter
|
161
|
+
is invalid.
|
162
|
+
@end defun
|
163
|
+
|
164
|
+
If the function being called is variadic (varargs) then
|
165
|
+
@code{ffi_prep_cif_var} must be used instead of @code{ffi_prep_cif}.
|
166
|
+
|
167
|
+
@findex ffi_prep_cif_var
|
168
|
+
@defun ffi_status ffi_prep_cif_var (ffi_cif *@var{cif}, ffi_abi @var{abi}, unsigned int @var{nfixedargs}, unsigned int @var{ntotalargs}, ffi_type *@var{rtype}, ffi_type **@var{argtypes})
|
169
|
+
This initializes @var{cif} according to the given parameters for
|
170
|
+
a call to a variadic function. In general its operation is the
|
171
|
+
same as for @code{ffi_prep_cif} except that:
|
172
|
+
|
173
|
+
@var{nfixedargs} is the number of fixed arguments, prior to any
|
174
|
+
variadic arguments. It must be greater than zero.
|
175
|
+
|
176
|
+
@var{ntotalargs} the total number of arguments, including variadic
|
177
|
+
and fixed arguments. @var{argtypes} must have this many elements.
|
178
|
+
|
179
|
+
Note that, different cif's must be prepped for calls to the same
|
180
|
+
function when different numbers of arguments are passed.
|
181
|
+
|
182
|
+
Also note that a call to @code{ffi_prep_cif_var} with
|
183
|
+
@var{nfixedargs}=@var{nototalargs} is NOT equivalent to a call to
|
184
|
+
@code{ffi_prep_cif}.
|
185
|
+
|
186
|
+
@end defun
|
187
|
+
|
188
|
+
Note that the resulting @code{ffi_cif} holds pointers to all the
|
189
|
+
@code{ffi_type} objects that were used during initialization. You
|
190
|
+
must ensure that these type objects have a lifetime at least as long
|
191
|
+
as that of the @code{ffi_cif}.
|
192
|
+
|
193
|
+
To call a function using an initialized @code{ffi_cif}, use the
|
194
|
+
@code{ffi_call} function:
|
195
|
+
|
196
|
+
@findex ffi_call
|
197
|
+
@defun void ffi_call (ffi_cif *@var{cif}, void *@var{fn}, void *@var{rvalue}, void **@var{avalues})
|
198
|
+
This calls the function @var{fn} according to the description given in
|
199
|
+
@var{cif}. @var{cif} must have already been prepared using
|
200
|
+
@code{ffi_prep_cif}.
|
201
|
+
|
202
|
+
@var{rvalue} is a pointer to a chunk of memory that will hold the
|
203
|
+
result of the function call. This must be large enough to hold the
|
204
|
+
result, no smaller than the system register size (generally 32 or 64
|
205
|
+
bits), and must be suitably aligned; it is the caller's responsibility
|
206
|
+
to ensure this. If @var{cif} declares that the function returns
|
207
|
+
@code{void} (using @code{ffi_type_void}), then @var{rvalue} is
|
208
|
+
ignored.
|
209
|
+
|
210
|
+
In most situations, @samp{libffi} will handle promotion according to
|
211
|
+
the ABI. However, for historical reasons, there is a special case
|
212
|
+
with return values that must be handled by your code. In particular,
|
213
|
+
for integral (not @code{struct}) types that are narrower than the
|
214
|
+
system register size, the return value will be widened by
|
215
|
+
@samp{libffi}. @samp{libffi} provides a type, @code{ffi_arg}, that
|
216
|
+
can be used as the return type. For example, if the CIF was defined
|
217
|
+
with a return type of @code{char}, @samp{libffi} will try to store a
|
218
|
+
full @code{ffi_arg} into the return value.
|
219
|
+
|
220
|
+
@var{avalues} is a vector of @code{void *} pointers that point to the
|
221
|
+
memory locations holding the argument values for a call. If @var{cif}
|
222
|
+
declares that the function has no arguments (i.e., @var{nargs} was 0),
|
223
|
+
then @var{avalues} is ignored. Note that argument values may be
|
224
|
+
modified by the callee (for instance, structs passed by value); the
|
225
|
+
burden of copying pass-by-value arguments is placed on the caller.
|
226
|
+
|
227
|
+
Note that while the return value must be register-sized, arguments
|
228
|
+
should exactly match their declared type. For example, if an argument
|
229
|
+
is a @code{short}, then the entry in @var{avalues} should point to an
|
230
|
+
object declared as @code{short}; but if the return type is
|
231
|
+
@code{short}, then @var{rvalue} should point to an object declared as
|
232
|
+
a larger type -- usually @code{ffi_arg}.
|
233
|
+
@end defun
|
234
|
+
|
235
|
+
|
236
|
+
@node Simple Example
|
237
|
+
@section Simple Example
|
238
|
+
|
239
|
+
Here is a trivial example that calls @code{puts} a few times.
|
240
|
+
|
241
|
+
@example
|
242
|
+
#include <stdio.h>
|
243
|
+
#include <ffi.h>
|
244
|
+
|
245
|
+
int main()
|
246
|
+
@{
|
247
|
+
ffi_cif cif;
|
248
|
+
ffi_type *args[1];
|
249
|
+
void *values[1];
|
250
|
+
char *s;
|
251
|
+
ffi_arg rc;
|
252
|
+
|
253
|
+
/* Initialize the argument info vectors */
|
254
|
+
args[0] = &ffi_type_pointer;
|
255
|
+
values[0] = &s;
|
256
|
+
|
257
|
+
/* Initialize the cif */
|
258
|
+
if (ffi_prep_cif(&cif, FFI_DEFAULT_ABI, 1,
|
259
|
+
&ffi_type_sint, args) == FFI_OK)
|
260
|
+
@{
|
261
|
+
s = "Hello World!";
|
262
|
+
ffi_call(&cif, puts, &rc, values);
|
263
|
+
/* rc now holds the result of the call to puts */
|
264
|
+
|
265
|
+
/* values holds a pointer to the function's arg, so to
|
266
|
+
call puts() again all we need to do is change the
|
267
|
+
value of s */
|
268
|
+
s = "This is cool!";
|
269
|
+
ffi_call(&cif, puts, &rc, values);
|
270
|
+
@}
|
271
|
+
|
272
|
+
return 0;
|
273
|
+
@}
|
274
|
+
@end example
|
275
|
+
|
276
|
+
|
277
|
+
@node Types
|
278
|
+
@section Types
|
279
|
+
|
280
|
+
@menu
|
281
|
+
* Primitive Types:: Built-in types.
|
282
|
+
* Structures:: Structure types.
|
283
|
+
* Size and Alignment:: Size and alignment of types.
|
284
|
+
* Arrays Unions Enums:: Arrays, unions, and enumerations.
|
285
|
+
* Type Example:: Structure type example.
|
286
|
+
* Complex:: Complex types.
|
287
|
+
* Complex Type Example:: Complex type example.
|
288
|
+
@end menu
|
289
|
+
|
290
|
+
@node Primitive Types
|
291
|
+
@subsection Primitive Types
|
292
|
+
|
293
|
+
@code{Libffi} provides a number of built-in type descriptors that can
|
294
|
+
be used to describe argument and return types:
|
295
|
+
|
296
|
+
@table @code
|
297
|
+
@item ffi_type_void
|
298
|
+
@tindex ffi_type_void
|
299
|
+
The type @code{void}. This cannot be used for argument types, only
|
300
|
+
for return values.
|
301
|
+
|
302
|
+
@item ffi_type_uint8
|
303
|
+
@tindex ffi_type_uint8
|
304
|
+
An unsigned, 8-bit integer type.
|
305
|
+
|
306
|
+
@item ffi_type_sint8
|
307
|
+
@tindex ffi_type_sint8
|
308
|
+
A signed, 8-bit integer type.
|
309
|
+
|
310
|
+
@item ffi_type_uint16
|
311
|
+
@tindex ffi_type_uint16
|
312
|
+
An unsigned, 16-bit integer type.
|
313
|
+
|
314
|
+
@item ffi_type_sint16
|
315
|
+
@tindex ffi_type_sint16
|
316
|
+
A signed, 16-bit integer type.
|
317
|
+
|
318
|
+
@item ffi_type_uint32
|
319
|
+
@tindex ffi_type_uint32
|
320
|
+
An unsigned, 32-bit integer type.
|
321
|
+
|
322
|
+
@item ffi_type_sint32
|
323
|
+
@tindex ffi_type_sint32
|
324
|
+
A signed, 32-bit integer type.
|
325
|
+
|
326
|
+
@item ffi_type_uint64
|
327
|
+
@tindex ffi_type_uint64
|
328
|
+
An unsigned, 64-bit integer type.
|
329
|
+
|
330
|
+
@item ffi_type_sint64
|
331
|
+
@tindex ffi_type_sint64
|
332
|
+
A signed, 64-bit integer type.
|
333
|
+
|
334
|
+
@item ffi_type_float
|
335
|
+
@tindex ffi_type_float
|
336
|
+
The C @code{float} type.
|
337
|
+
|
338
|
+
@item ffi_type_double
|
339
|
+
@tindex ffi_type_double
|
340
|
+
The C @code{double} type.
|
341
|
+
|
342
|
+
@item ffi_type_uchar
|
343
|
+
@tindex ffi_type_uchar
|
344
|
+
The C @code{unsigned char} type.
|
345
|
+
|
346
|
+
@item ffi_type_schar
|
347
|
+
@tindex ffi_type_schar
|
348
|
+
The C @code{signed char} type. (Note that there is not an exact
|
349
|
+
equivalent to the C @code{char} type in @code{libffi}; ordinarily you
|
350
|
+
should either use @code{ffi_type_schar} or @code{ffi_type_uchar}
|
351
|
+
depending on whether @code{char} is signed.)
|
352
|
+
|
353
|
+
@item ffi_type_ushort
|
354
|
+
@tindex ffi_type_ushort
|
355
|
+
The C @code{unsigned short} type.
|
356
|
+
|
357
|
+
@item ffi_type_sshort
|
358
|
+
@tindex ffi_type_sshort
|
359
|
+
The C @code{short} type.
|
360
|
+
|
361
|
+
@item ffi_type_uint
|
362
|
+
@tindex ffi_type_uint
|
363
|
+
The C @code{unsigned int} type.
|
364
|
+
|
365
|
+
@item ffi_type_sint
|
366
|
+
@tindex ffi_type_sint
|
367
|
+
The C @code{int} type.
|
368
|
+
|
369
|
+
@item ffi_type_ulong
|
370
|
+
@tindex ffi_type_ulong
|
371
|
+
The C @code{unsigned long} type.
|
372
|
+
|
373
|
+
@item ffi_type_slong
|
374
|
+
@tindex ffi_type_slong
|
375
|
+
The C @code{long} type.
|
376
|
+
|
377
|
+
@item ffi_type_longdouble
|
378
|
+
@tindex ffi_type_longdouble
|
379
|
+
On platforms that have a C @code{long double} type, this is defined.
|
380
|
+
On other platforms, it is not.
|
381
|
+
|
382
|
+
@item ffi_type_pointer
|
383
|
+
@tindex ffi_type_pointer
|
384
|
+
A generic @code{void *} pointer. You should use this for all
|
385
|
+
pointers, regardless of their real type.
|
386
|
+
|
387
|
+
@item ffi_type_complex_float
|
388
|
+
@tindex ffi_type_complex_float
|
389
|
+
The C @code{_Complex float} type.
|
390
|
+
|
391
|
+
@item ffi_type_complex_double
|
392
|
+
@tindex ffi_type_complex_double
|
393
|
+
The C @code{_Complex double} type.
|
394
|
+
|
395
|
+
@item ffi_type_complex_longdouble
|
396
|
+
@tindex ffi_type_complex_longdouble
|
397
|
+
The C @code{_Complex long double} type.
|
398
|
+
On platforms that have a C @code{long double} type, this is defined.
|
399
|
+
On other platforms, it is not.
|
400
|
+
@end table
|
401
|
+
|
402
|
+
Each of these is of type @code{ffi_type}, so you must take the address
|
403
|
+
when passing to @code{ffi_prep_cif}.
|
404
|
+
|
405
|
+
|
406
|
+
@node Structures
|
407
|
+
@subsection Structures
|
408
|
+
|
409
|
+
@samp{libffi} is perfectly happy passing structures back and forth.
|
410
|
+
You must first describe the structure to @samp{libffi} by creating a
|
411
|
+
new @code{ffi_type} object for it.
|
412
|
+
|
413
|
+
@tindex ffi_type
|
414
|
+
@deftp {Data type} ffi_type
|
415
|
+
The @code{ffi_type} has the following members:
|
416
|
+
@table @code
|
417
|
+
@item size_t size
|
418
|
+
This is set by @code{libffi}; you should initialize it to zero.
|
419
|
+
|
420
|
+
@item unsigned short alignment
|
421
|
+
This is set by @code{libffi}; you should initialize it to zero.
|
422
|
+
|
423
|
+
@item unsigned short type
|
424
|
+
For a structure, this should be set to @code{FFI_TYPE_STRUCT}.
|
425
|
+
|
426
|
+
@item ffi_type **elements
|
427
|
+
This is a @samp{NULL}-terminated array of pointers to @code{ffi_type}
|
428
|
+
objects. There is one element per field of the struct.
|
429
|
+
|
430
|
+
Note that @samp{libffi} has no special support for bit-fields. You
|
431
|
+
must manage these manually.
|
432
|
+
@end table
|
433
|
+
@end deftp
|
434
|
+
|
435
|
+
The @code{size} and @code{alignment} fields will be filled in by
|
436
|
+
@code{ffi_prep_cif} or @code{ffi_prep_cif_var}, as needed.
|
437
|
+
|
438
|
+
@node Size and Alignment
|
439
|
+
@subsection Size and Alignment
|
440
|
+
|
441
|
+
@code{libffi} will set the @code{size} and @code{alignment} fields of
|
442
|
+
an @code{ffi_type} object for you. It does so using its knowledge of
|
443
|
+
the ABI.
|
444
|
+
|
445
|
+
You might expect that you can simply read these fields for a type that
|
446
|
+
has been laid out by @code{libffi}. However, there are some caveats.
|
447
|
+
|
448
|
+
@itemize @bullet
|
449
|
+
@item
|
450
|
+
The size or alignment of some of the built-in types may vary depending
|
451
|
+
on the chosen ABI.
|
452
|
+
|
453
|
+
@item
|
454
|
+
The size and alignment of a new structure type will not be set by
|
455
|
+
@code{libffi} until it has been passed to @code{ffi_prep_cif} or
|
456
|
+
@code{ffi_get_struct_offsets}.
|
457
|
+
|
458
|
+
@item
|
459
|
+
A structure type cannot be shared across ABIs. Instead each ABI needs
|
460
|
+
its own copy of the structure type.
|
461
|
+
@end itemize
|
462
|
+
|
463
|
+
So, before examining these fields, it is safest to pass the
|
464
|
+
@code{ffi_type} object to @code{ffi_prep_cif} or
|
465
|
+
@code{ffi_get_struct_offsets} first. This function will do all the
|
466
|
+
needed setup.
|
467
|
+
|
468
|
+
@example
|
469
|
+
ffi_type *desired_type;
|
470
|
+
ffi_abi desired_abi;
|
471
|
+
@dots{}
|
472
|
+
ffi_cif cif;
|
473
|
+
if (ffi_prep_cif (&cif, desired_abi, 0, desired_type, NULL) == FFI_OK)
|
474
|
+
@{
|
475
|
+
size_t size = desired_type->size;
|
476
|
+
unsigned short alignment = desired_type->alignment;
|
477
|
+
@}
|
478
|
+
@end example
|
479
|
+
|
480
|
+
@code{libffi} also provides a way to get the offsets of the members of
|
481
|
+
a structure.
|
482
|
+
|
483
|
+
@findex ffi_get_struct_offsets
|
484
|
+
@defun ffi_status ffi_get_struct_offsets (ffi_abi abi, ffi_type *struct_type, size_t *offsets)
|
485
|
+
Compute the offset of each element of the given structure type.
|
486
|
+
@var{abi} is the ABI to use; this is needed because in some cases the
|
487
|
+
layout depends on the ABI.
|
488
|
+
|
489
|
+
@var{offsets} is an out parameter. The caller is responsible for
|
490
|
+
providing enough space for all the results to be written -- one
|
491
|
+
element per element type in @var{struct_type}. If @var{offsets} is
|
492
|
+
@code{NULL}, then the type will be laid out but not otherwise
|
493
|
+
modified. This can be useful for accessing the type's size or layout,
|
494
|
+
as mentioned above.
|
495
|
+
|
496
|
+
This function returns @code{FFI_OK} on success; @code{FFI_BAD_ABI} if
|
497
|
+
@var{abi} is invalid; or @code{FFI_BAD_TYPEDEF} if @var{struct_type}
|
498
|
+
is invalid in some way. Note that only @code{FFI_STRUCT} types are
|
499
|
+
valid here.
|
500
|
+
@end defun
|
501
|
+
|
502
|
+
@node Arrays Unions Enums
|
503
|
+
@subsection Arrays, Unions, and Enumerations
|
504
|
+
|
505
|
+
@subsubsection Arrays
|
506
|
+
|
507
|
+
@samp{libffi} does not have direct support for arrays or unions.
|
508
|
+
However, they can be emulated using structures.
|
509
|
+
|
510
|
+
To emulate an array, simply create an @code{ffi_type} using
|
511
|
+
@code{FFI_TYPE_STRUCT} with as many members as there are elements in
|
512
|
+
the array.
|
513
|
+
|
514
|
+
@example
|
515
|
+
ffi_type array_type;
|
516
|
+
ffi_type **elements
|
517
|
+
int i;
|
518
|
+
|
519
|
+
elements = malloc ((n + 1) * sizeof (ffi_type *));
|
520
|
+
for (i = 0; i < n; ++i)
|
521
|
+
elements[i] = array_element_type;
|
522
|
+
elements[n] = NULL;
|
523
|
+
|
524
|
+
array_type.size = array_type.alignment = 0;
|
525
|
+
array_type.type = FFI_TYPE_STRUCT;
|
526
|
+
array_type.elements = elements;
|
527
|
+
@end example
|
528
|
+
|
529
|
+
Note that arrays cannot be passed or returned by value in C --
|
530
|
+
structure types created like this should only be used to refer to
|
531
|
+
members of real @code{FFI_TYPE_STRUCT} objects.
|
532
|
+
|
533
|
+
However, a phony array type like this will not cause any errors from
|
534
|
+
@samp{libffi} if you use it as an argument or return type. This may
|
535
|
+
be confusing.
|
536
|
+
|
537
|
+
@subsubsection Unions
|
538
|
+
|
539
|
+
A union can also be emulated using @code{FFI_TYPE_STRUCT}. In this
|
540
|
+
case, however, you must make sure that the size and alignment match
|
541
|
+
the real requirements of the union.
|
542
|
+
|
543
|
+
One simple way to do this is to ensue that each element type is laid
|
544
|
+
out. Then, give the new structure type a single element; the size of
|
545
|
+
the largest element; and the largest alignment seen as well.
|
546
|
+
|
547
|
+
This example uses the @code{ffi_prep_cif} trick to ensure that each
|
548
|
+
element type is laid out.
|
549
|
+
|
550
|
+
@example
|
551
|
+
ffi_abi desired_abi;
|
552
|
+
ffi_type union_type;
|
553
|
+
ffi_type **union_elements;
|
554
|
+
|
555
|
+
int i;
|
556
|
+
ffi_type element_types[2];
|
557
|
+
|
558
|
+
element_types[1] = NULL;
|
559
|
+
|
560
|
+
union_type.size = union_type.alignment = 0;
|
561
|
+
union_type.type = FFI_TYPE_STRUCT;
|
562
|
+
union_type.elements = element_types;
|
563
|
+
|
564
|
+
for (i = 0; union_elements[i]; ++i)
|
565
|
+
@{
|
566
|
+
ffi_cif cif;
|
567
|
+
if (ffi_prep_cif (&cif, desired_abi, 0, union_elements[i], NULL) == FFI_OK)
|
568
|
+
@{
|
569
|
+
if (union_elements[i]->size > union_type.size)
|
570
|
+
@{
|
571
|
+
union_type.size = union_elements[i];
|
572
|
+
size = union_elements[i]->size;
|
573
|
+
@}
|
574
|
+
if (union_elements[i]->alignment > union_type.alignment)
|
575
|
+
union_type.alignment = union_elements[i]->alignment;
|
576
|
+
@}
|
577
|
+
@}
|
578
|
+
@end example
|
579
|
+
|
580
|
+
@subsubsection Enumerations
|
581
|
+
|
582
|
+
@code{libffi} does not have any special support for C @code{enum}s.
|
583
|
+
Although any given @code{enum} is implemented using a specific
|
584
|
+
underlying integral type, exactly which type will be used cannot be
|
585
|
+
determined by @code{libffi} -- it may depend on the values in the
|
586
|
+
enumeration or on compiler flags such as @option{-fshort-enums}.
|
587
|
+
@xref{Structures unions enumerations and bit-fields implementation, , , gcc},
|
588
|
+
for more information about how GCC handles enumerations.
|
589
|
+
|
590
|
+
@node Type Example
|
591
|
+
@subsection Type Example
|
592
|
+
|
593
|
+
The following example initializes a @code{ffi_type} object
|
594
|
+
representing the @code{tm} struct from Linux's @file{time.h}.
|
595
|
+
|
596
|
+
Here is how the struct is defined:
|
597
|
+
|
598
|
+
@example
|
599
|
+
struct tm @{
|
600
|
+
int tm_sec;
|
601
|
+
int tm_min;
|
602
|
+
int tm_hour;
|
603
|
+
int tm_mday;
|
604
|
+
int tm_mon;
|
605
|
+
int tm_year;
|
606
|
+
int tm_wday;
|
607
|
+
int tm_yday;
|
608
|
+
int tm_isdst;
|
609
|
+
/* Those are for future use. */
|
610
|
+
long int __tm_gmtoff__;
|
611
|
+
__const char *__tm_zone__;
|
612
|
+
@};
|
613
|
+
@end example
|
614
|
+
|
615
|
+
Here is the corresponding code to describe this struct to
|
616
|
+
@code{libffi}:
|
617
|
+
|
618
|
+
@example
|
619
|
+
@{
|
620
|
+
ffi_type tm_type;
|
621
|
+
ffi_type *tm_type_elements[12];
|
622
|
+
int i;
|
623
|
+
|
624
|
+
tm_type.size = tm_type.alignment = 0;
|
625
|
+
tm_type.type = FFI_TYPE_STRUCT;
|
626
|
+
tm_type.elements = &tm_type_elements;
|
627
|
+
|
628
|
+
for (i = 0; i < 9; i++)
|
629
|
+
tm_type_elements[i] = &ffi_type_sint;
|
630
|
+
|
631
|
+
tm_type_elements[9] = &ffi_type_slong;
|
632
|
+
tm_type_elements[10] = &ffi_type_pointer;
|
633
|
+
tm_type_elements[11] = NULL;
|
634
|
+
|
635
|
+
/* tm_type can now be used to represent tm argument types and
|
636
|
+
return types for ffi_prep_cif() */
|
637
|
+
@}
|
638
|
+
@end example
|
639
|
+
|
640
|
+
@node Complex
|
641
|
+
@subsection Complex Types
|
642
|
+
|
643
|
+
@samp{libffi} supports the complex types defined by the C99
|
644
|
+
standard (@code{_Complex float}, @code{_Complex double} and
|
645
|
+
@code{_Complex long double} with the built-in type descriptors
|
646
|
+
@code{ffi_type_complex_float}, @code{ffi_type_complex_double} and
|
647
|
+
@code{ffi_type_complex_longdouble}.
|
648
|
+
|
649
|
+
Custom complex types like @code{_Complex int} can also be used.
|
650
|
+
An @code{ffi_type} object has to be defined to describe the
|
651
|
+
complex type to @samp{libffi}.
|
652
|
+
|
653
|
+
@tindex ffi_type
|
654
|
+
@deftp {Data type} ffi_type
|
655
|
+
@table @code
|
656
|
+
@item size_t size
|
657
|
+
This must be manually set to the size of the complex type.
|
658
|
+
|
659
|
+
@item unsigned short alignment
|
660
|
+
This must be manually set to the alignment of the complex type.
|
661
|
+
|
662
|
+
@item unsigned short type
|
663
|
+
For a complex type, this must be set to @code{FFI_TYPE_COMPLEX}.
|
664
|
+
|
665
|
+
@item ffi_type **elements
|
666
|
+
|
667
|
+
This is a @samp{NULL}-terminated array of pointers to
|
668
|
+
@code{ffi_type} objects. The first element is set to the
|
669
|
+
@code{ffi_type} of the complex's base type. The second element
|
670
|
+
must be set to @code{NULL}.
|
671
|
+
@end table
|
672
|
+
@end deftp
|
673
|
+
|
674
|
+
The section @ref{Complex Type Example} shows a way to determine
|
675
|
+
the @code{size} and @code{alignment} members in a platform
|
676
|
+
independent way.
|
677
|
+
|
678
|
+
For platforms that have no complex support in @code{libffi} yet,
|
679
|
+
the functions @code{ffi_prep_cif} and @code{ffi_prep_args} abort
|
680
|
+
the program if they encounter a complex type.
|
681
|
+
|
682
|
+
@node Complex Type Example
|
683
|
+
@subsection Complex Type Example
|
684
|
+
|
685
|
+
This example demonstrates how to use complex types:
|
686
|
+
|
687
|
+
@example
|
688
|
+
#include <stdio.h>
|
689
|
+
#include <ffi.h>
|
690
|
+
#include <complex.h>
|
691
|
+
|
692
|
+
void complex_fn(_Complex float cf,
|
693
|
+
_Complex double cd,
|
694
|
+
_Complex long double cld)
|
695
|
+
@{
|
696
|
+
printf("cf=%f+%fi\ncd=%f+%fi\ncld=%f+%fi\n",
|
697
|
+
(float)creal (cf), (float)cimag (cf),
|
698
|
+
(float)creal (cd), (float)cimag (cd),
|
699
|
+
(float)creal (cld), (float)cimag (cld));
|
700
|
+
@}
|
701
|
+
|
702
|
+
int main()
|
703
|
+
@{
|
704
|
+
ffi_cif cif;
|
705
|
+
ffi_type *args[3];
|
706
|
+
void *values[3];
|
707
|
+
_Complex float cf;
|
708
|
+
_Complex double cd;
|
709
|
+
_Complex long double cld;
|
710
|
+
|
711
|
+
/* Initialize the argument info vectors */
|
712
|
+
args[0] = &ffi_type_complex_float;
|
713
|
+
args[1] = &ffi_type_complex_double;
|
714
|
+
args[2] = &ffi_type_complex_longdouble;
|
715
|
+
values[0] = &cf;
|
716
|
+
values[1] = &cd;
|
717
|
+
values[2] = &cld;
|
718
|
+
|
719
|
+
/* Initialize the cif */
|
720
|
+
if (ffi_prep_cif(&cif, FFI_DEFAULT_ABI, 3,
|
721
|
+
&ffi_type_void, args) == FFI_OK)
|
722
|
+
@{
|
723
|
+
cf = 1.0 + 20.0 * I;
|
724
|
+
cd = 300.0 + 4000.0 * I;
|
725
|
+
cld = 50000.0 + 600000.0 * I;
|
726
|
+
/* Call the function */
|
727
|
+
ffi_call(&cif, (void (*)(void))complex_fn, 0, values);
|
728
|
+
@}
|
729
|
+
|
730
|
+
return 0;
|
731
|
+
@}
|
732
|
+
@end example
|
733
|
+
|
734
|
+
This is an example for defining a custom complex type descriptor
|
735
|
+
for compilers that support them:
|
736
|
+
|
737
|
+
@example
|
738
|
+
/*
|
739
|
+
* This macro can be used to define new complex type descriptors
|
740
|
+
* in a platform independent way.
|
741
|
+
*
|
742
|
+
* name: Name of the new descriptor is ffi_type_complex_<name>.
|
743
|
+
* type: The C base type of the complex type.
|
744
|
+
*/
|
745
|
+
#define FFI_COMPLEX_TYPEDEF(name, type, ffitype) \
|
746
|
+
static ffi_type *ffi_elements_complex_##name [2] = @{ \
|
747
|
+
(ffi_type *)(&ffitype), NULL \
|
748
|
+
@}; \
|
749
|
+
struct struct_align_complex_##name @{ \
|
750
|
+
char c; \
|
751
|
+
_Complex type x; \
|
752
|
+
@}; \
|
753
|
+
ffi_type ffi_type_complex_##name = @{ \
|
754
|
+
sizeof(_Complex type), \
|
755
|
+
offsetof(struct struct_align_complex_##name, x), \
|
756
|
+
FFI_TYPE_COMPLEX, \
|
757
|
+
(ffi_type **)ffi_elements_complex_##name \
|
758
|
+
@}
|
759
|
+
|
760
|
+
/* Define new complex type descriptors using the macro: */
|
761
|
+
/* ffi_type_complex_sint */
|
762
|
+
FFI_COMPLEX_TYPEDEF(sint, int, ffi_type_sint);
|
763
|
+
/* ffi_type_complex_uchar */
|
764
|
+
FFI_COMPLEX_TYPEDEF(uchar, unsigned char, ffi_type_uint8);
|
765
|
+
@end example
|
766
|
+
|
767
|
+
The new type descriptors can then be used like one of the built-in
|
768
|
+
type descriptors in the previous example.
|
769
|
+
|
770
|
+
@node Multiple ABIs
|
771
|
+
@section Multiple ABIs
|
772
|
+
|
773
|
+
A given platform may provide multiple different ABIs at once. For
|
774
|
+
instance, the x86 platform has both @samp{stdcall} and @samp{fastcall}
|
775
|
+
functions.
|
776
|
+
|
777
|
+
@code{libffi} provides some support for this. However, this is
|
778
|
+
necessarily platform-specific.
|
779
|
+
|
780
|
+
@c FIXME: document the platforms
|
781
|
+
|
782
|
+
@node The Closure API
|
783
|
+
@section The Closure API
|
784
|
+
|
785
|
+
@code{libffi} also provides a way to write a generic function -- a
|
786
|
+
function that can accept and decode any combination of arguments.
|
787
|
+
This can be useful when writing an interpreter, or to provide wrappers
|
788
|
+
for arbitrary functions.
|
789
|
+
|
790
|
+
This facility is called the @dfn{closure API}. Closures are not
|
791
|
+
supported on all platforms; you can check the @code{FFI_CLOSURES}
|
792
|
+
define to determine whether they are supported on the current
|
793
|
+
platform.
|
794
|
+
@cindex closures
|
795
|
+
@cindex closure API
|
796
|
+
@findex FFI_CLOSURES
|
797
|
+
|
798
|
+
Because closures work by assembling a tiny function at runtime, they
|
799
|
+
require special allocation on platforms that have a non-executable
|
800
|
+
heap. Memory management for closures is handled by a pair of
|
801
|
+
functions:
|
802
|
+
|
803
|
+
@findex ffi_closure_alloc
|
804
|
+
@defun void *ffi_closure_alloc (size_t @var{size}, void **@var{code})
|
805
|
+
Allocate a chunk of memory holding @var{size} bytes. This returns a
|
806
|
+
pointer to the writable address, and sets *@var{code} to the
|
807
|
+
corresponding executable address.
|
808
|
+
|
809
|
+
@var{size} should be sufficient to hold a @code{ffi_closure} object.
|
810
|
+
@end defun
|
811
|
+
|
812
|
+
@findex ffi_closure_free
|
813
|
+
@defun void ffi_closure_free (void *@var{writable})
|
814
|
+
Free memory allocated using @code{ffi_closure_alloc}. The argument is
|
815
|
+
the writable address that was returned.
|
816
|
+
@end defun
|
817
|
+
|
818
|
+
|
819
|
+
Once you have allocated the memory for a closure, you must construct a
|
820
|
+
@code{ffi_cif} describing the function call. Finally you can prepare
|
821
|
+
the closure function:
|
822
|
+
|
823
|
+
@findex ffi_prep_closure_loc
|
824
|
+
@defun ffi_status ffi_prep_closure_loc (ffi_closure *@var{closure}, ffi_cif *@var{cif}, void (*@var{fun}) (ffi_cif *@var{cif}, void *@var{ret}, void **@var{args}, void *@var{user_data}), void *@var{user_data}, void *@var{codeloc})
|
825
|
+
Prepare a closure function. The arguments to
|
826
|
+
@code{ffi_prep_closure_loc} are:
|
827
|
+
|
828
|
+
@table @var
|
829
|
+
@item closure
|
830
|
+
The address of a @code{ffi_closure} object; this is the writable
|
831
|
+
address returned by @code{ffi_closure_alloc}.
|
832
|
+
|
833
|
+
@item cif
|
834
|
+
The @code{ffi_cif} describing the function parameters. Note that this
|
835
|
+
object, and the types to which it refers, must be kept alive until the
|
836
|
+
closure itself is freed.
|
837
|
+
|
838
|
+
@item user_data
|
839
|
+
An arbitrary datum that is passed, uninterpreted, to your closure
|
840
|
+
function.
|
841
|
+
|
842
|
+
@item codeloc
|
843
|
+
The executable address returned by @code{ffi_closure_alloc}.
|
844
|
+
|
845
|
+
@item fun
|
846
|
+
The function which will be called when the closure is invoked. It is
|
847
|
+
called with the arguments:
|
848
|
+
|
849
|
+
@table @var
|
850
|
+
@item cif
|
851
|
+
The @code{ffi_cif} passed to @code{ffi_prep_closure_loc}.
|
852
|
+
|
853
|
+
@item ret
|
854
|
+
A pointer to the memory used for the function's return value.
|
855
|
+
|
856
|
+
If the function is declared as returning @code{void}, then this value
|
857
|
+
is garbage and should not be used.
|
858
|
+
|
859
|
+
Otherwise, @var{fun} must fill the object to which this points,
|
860
|
+
following the same special promotion behavior as @code{ffi_call}.
|
861
|
+
That is, in most cases, @var{ret} points to an object of exactly the
|
862
|
+
size of the type specified when @var{cif} was constructed. However,
|
863
|
+
integral types narrower than the system register size are widened. In
|
864
|
+
these cases your program may assume that @var{ret} points to an
|
865
|
+
@code{ffi_arg} object.
|
866
|
+
|
867
|
+
@item args
|
868
|
+
A vector of pointers to memory holding the arguments to the function.
|
869
|
+
|
870
|
+
@item user_data
|
871
|
+
The same @var{user_data} that was passed to
|
872
|
+
@code{ffi_prep_closure_loc}.
|
873
|
+
@end table
|
874
|
+
@end table
|
875
|
+
|
876
|
+
@code{ffi_prep_closure_loc} will return @code{FFI_OK} if everything
|
877
|
+
went ok, and one of the other @code{ffi_status} values on error.
|
878
|
+
|
879
|
+
After calling @code{ffi_prep_closure_loc}, you can cast @var{codeloc}
|
880
|
+
to the appropriate pointer-to-function type.
|
881
|
+
@end defun
|
882
|
+
|
883
|
+
You may see old code referring to @code{ffi_prep_closure}. This
|
884
|
+
function is deprecated, as it cannot handle the need for separate
|
885
|
+
writable and executable addresses.
|
886
|
+
|
887
|
+
@node Closure Example
|
888
|
+
@section Closure Example
|
889
|
+
|
890
|
+
A trivial example that creates a new @code{puts} by binding
|
891
|
+
@code{fputs} with @code{stdout}.
|
892
|
+
|
893
|
+
@example
|
894
|
+
#include <stdio.h>
|
895
|
+
#include <ffi.h>
|
896
|
+
|
897
|
+
/* Acts like puts with the file given at time of enclosure. */
|
898
|
+
void puts_binding(ffi_cif *cif, void *ret, void* args[],
|
899
|
+
void *stream)
|
900
|
+
@{
|
901
|
+
*(ffi_arg *)ret = fputs(*(char **)args[0], (FILE *)stream);
|
902
|
+
@}
|
903
|
+
|
904
|
+
typedef int (*puts_t)(char *);
|
905
|
+
|
906
|
+
int main()
|
907
|
+
@{
|
908
|
+
ffi_cif cif;
|
909
|
+
ffi_type *args[1];
|
910
|
+
ffi_closure *closure;
|
911
|
+
|
912
|
+
void *bound_puts;
|
913
|
+
int rc;
|
914
|
+
|
915
|
+
/* Allocate closure and bound_puts */
|
916
|
+
closure = ffi_closure_alloc(sizeof(ffi_closure), &bound_puts);
|
917
|
+
|
918
|
+
if (closure)
|
919
|
+
@{
|
920
|
+
/* Initialize the argument info vectors */
|
921
|
+
args[0] = &ffi_type_pointer;
|
922
|
+
|
923
|
+
/* Initialize the cif */
|
924
|
+
if (ffi_prep_cif(&cif, FFI_DEFAULT_ABI, 1,
|
925
|
+
&ffi_type_sint, args) == FFI_OK)
|
926
|
+
@{
|
927
|
+
/* Initialize the closure, setting stream to stdout */
|
928
|
+
if (ffi_prep_closure_loc(closure, &cif, puts_binding,
|
929
|
+
stdout, bound_puts) == FFI_OK)
|
930
|
+
@{
|
931
|
+
rc = ((puts_t)bound_puts)("Hello World!");
|
932
|
+
/* rc now holds the result of the call to fputs */
|
933
|
+
@}
|
934
|
+
@}
|
935
|
+
@}
|
936
|
+
|
937
|
+
/* Deallocate both closure, and bound_puts */
|
938
|
+
ffi_closure_free(closure);
|
939
|
+
|
940
|
+
return 0;
|
941
|
+
@}
|
942
|
+
|
943
|
+
@end example
|
944
|
+
|
945
|
+
@node Thread Safety
|
946
|
+
@section Thread Safety
|
947
|
+
|
948
|
+
@code{libffi} is not completely thread-safe. However, many parts are,
|
949
|
+
and if you follow some simple rules, you can use it safely in a
|
950
|
+
multi-threaded program.
|
951
|
+
|
952
|
+
@itemize @bullet
|
953
|
+
@item
|
954
|
+
@code{ffi_prep_cif} may modify the @code{ffi_type} objects passed to
|
955
|
+
it. It is best to ensure that only a single thread prepares a given
|
956
|
+
@code{ffi_cif} at a time.
|
957
|
+
|
958
|
+
@item
|
959
|
+
On some platforms, @code{ffi_prep_cif} may modify the size and
|
960
|
+
alignment of some types, depending on the chosen ABI. On these
|
961
|
+
platforms, if you switch between ABIs, you must ensure that there is
|
962
|
+
only one call to @code{ffi_prep_cif} at a time.
|
963
|
+
|
964
|
+
Currently the only affected platform is PowerPC and the only affected
|
965
|
+
type is @code{long double}.
|
966
|
+
@end itemize
|
967
|
+
|
968
|
+
@node Missing Features
|
969
|
+
@chapter Missing Features
|
970
|
+
|
971
|
+
@code{libffi} is missing a few features. We welcome patches to add
|
972
|
+
support for these.
|
973
|
+
|
974
|
+
@itemize @bullet
|
975
|
+
@item
|
976
|
+
Variadic closures.
|
977
|
+
|
978
|
+
@item
|
979
|
+
There is no support for bit fields in structures.
|
980
|
+
|
981
|
+
@item
|
982
|
+
The ``raw'' API is undocumented.
|
983
|
+
@c anything else?
|
984
|
+
|
985
|
+
@item
|
986
|
+
The Go API is undocumented.
|
987
|
+
@end itemize
|
988
|
+
|
989
|
+
Note that variadic support is very new and tested on a relatively
|
990
|
+
small number of platforms.
|
991
|
+
|
992
|
+
@node Index
|
993
|
+
@unnumbered Index
|
994
|
+
|
995
|
+
@printindex cp
|
996
|
+
|
997
|
+
@bye
|