|Initial release||October 7, 1996|
3.4.2 / June 28, 2021
|Written in||C, Assembly language|
|Operating system||Unix-like, Microsoft Windows, OS X, iOS, Bare Metal|
libffi is a foreign function interface library. It provides a C programming language interface for calling natively compiled functions given information about the target function at run time instead of compile time. It also implements the opposite functionality: libffi can produce a pointer to a function that can accept and decode any combination of arguments defined at run time.
libffi is most often used as a bridging technology between compiled and interpreted language implementations. libffi may also be used to implement plug-ins, where the plug-in's function signatures are not known at the time of creating the host application.
Notable users include Python, Haskell, Dalvik, F-Script, PyPy, PyObjC, RubyCocoa, JRuby, Rubinius, MacRuby, gcj, GNU Smalltalk, IcedTea, Cycript, Pawn, Squeak, Java Native Access, Common Lisp (via CFFI), Racket, Embeddable Common Lisp and Mozilla.
Although the C programming language is ubiquitous among platforms, the ways function calls are implemented in machine code – the calling convention – vary. When one wants to load a subroutine dynamically at run-time, a knowledge of these conventions is required.
libffi has knowledge of the calling convention on many platforms (processor–OS combinations). Its C API, which is shared on all builds of libffi regardless of platform, abstracts over the complexity of loading code on different platforms. In addition to regular functions, it also supports C-style variadic functions.
The C calling convention is not only used by the C language: due to the amount of existing code written in C, most newer compiled languages also allow writing and calling functions in such a convention. As a result, libffi is able to interact with some functions written in these languages too.
This section is missing information about year of gencall.(January 2021)
libffi, originally developed by Anthony Green, was inspired by the Gencall library from Silicon Graphics. Gencall was developed by Gianni Mariani, then employed by SGI, for the purpose of allowing calls to functions by address and creating a call frame for the particular calling convention (MIPS O32). Anthony Green refined the idea and extended it to other architectures and calling conventions and open sourcing libffi.
The libffi library is useful in building a bridge between interpreted and natively compiled code. Some notable users include:
- Dynamically generates Cocoa classes written in F-Script.
- GNU Guile
- libffi is used in Guile 1.9.8 and onwards
- libffi has been used for the majority of the FFI performed by the GHC since late 2009.
- The open-source implementation of the Java Platform Standard Edition uses libffi to bridge between the interpreter and native code for Zero-Assembly port.
- Java Native Access (JNA)
- The JNI-free way to call native code from Java.
- The standard foreign function interface library for Common Lisp CFFI includes the cffi-libffi system to include support for passing and returning structure arguments by value.
- The default, most-widely used implementation of the Python programming language uses libffi in the standard ctypes library.
- Call Objective-C code from Python on Mac OS X.
- Call C code from this popular Scheme implementation (also used as the implementation platform for Paul Graham's new Lisp, Arc).
- A libffi wrapper in the Ruby Standard Library
- A Foreign Function Interface extension for Ruby.
- Call Objective-C code from Ruby on Mac OS X.