# Libffi

> Mediated Wiki article. Canonical URL: https://mediated.wiki/source/Libffi
> Markdown URL: https://mediated.wiki/source/Libffi.md
> Source: https://en.wikipedia.org/wiki/Libffi
> Source revision: 1325244426
> License: Creative Commons Attribution-ShareAlike 4.0 International (https://creativecommons.org/licenses/by-sa/4.0/)

{{Short description|Foreign function interface library}}
<!-- Release version update? Don't edit this page, just click on the version number! -->{{lowercase title}}
{{Infobox software
| name                   = libffi
| logo                   =
| screenshot             =
| caption                =
| developer              = Anthony Green
| released               = {{Start date and age|1996|10|07}}
| latest release version = {{wikidata|property|preferred|references|edit|P348|P548=Q2804309}}
| latest release date    = {{Start date and age|{{wikidata|qualifier|preferred|single|P348|P548=Q2804309|P577}}|df=yes}}
| programming language   = [C](/source/C_(programming_language)), [Assembly language](/source/Assembly_language)
| operating system       = [Unix-like](/source/Unix-like), [Microsoft Windows](/source/Microsoft_Windows), [OS X](/source/OS_X), [iOS](/source/iOS_(Apple)), [bare metal](/source/Bare_machine)
| genre                  = [Runtime library](/source/Runtime_library)
| license                = [MIT License](/source/MIT_License)<ref>{{cite web | url=https://github.com/atgreen/libffi/blob/master/LICENSE | title=Status | website=[GitHub](/source/GitHub) | date=25 April 2022 }}</ref>
}}
{{Portal|Free and open-source software}}
'''libffi''' is a [foreign function interface](/source/foreign_function_interface) library.  It provides a [C programming language](/source/C_programming_language) interface for calling natively compiled [functions](/source/Subroutine) given information about the target [function](/source/Subroutine) at [run time](/source/Run_time_(program_lifecycle_phase)) instead of [compile time](/source/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](/source/compiled) and [interpreted](/source/Interpreter_(computing)) language implementations. libffi may also be used to implement [plug-ins](/source/Plug-in_(computing)), where the plug-in's function signatures are not known at the time of creating the host application.

Notable users include [Python](/source/Python_(programming_language)), [Haskell](/source/Haskell_(programming_language)), [Dalvik](/source/Dalvik_virtual_machine), [F-Script](/source/F-Script), [PyPy](/source/PyPy), [PyObjC](/source/PyObjC), [RubyCocoa](/source/RubyCocoa), [JRuby](/source/JRuby), [Rubinius](/source/Rubinius), [MacRuby](/source/MacRuby), [gcj](/source/GNU_Compiler_for_Java), [GNU Smalltalk](/source/GNU_Smalltalk), [IcedTea](/source/IcedTea), [Cycript](/source/Cycript), [Pawn](/source/Pawn_(programming_language)), [Java Native Access](/source/Java_Native_Access), [Common Lisp](/source/Common_Lisp) (via CFFI), [Racket](/source/Racket_(programming_language)),<ref>{{Cite web |url=http://repository.readscheme.org/ftp/papers/sw2004/barzilay.pdf |title=Archived copy |access-date=2009-08-02 |archive-date=2009-09-02 |archive-url=https://web.archive.org/web/20090902152301/http://repository.readscheme.org/ftp/papers/sw2004/barzilay.pdf |url-status=dead }}</ref> [Embeddable Common Lisp](/source/Embeddable_Common_Lisp) and [Mozilla](/source/Mozilla).<ref>{{cite web | url=https://hg.mozilla.org/mozilla-central/file/2dc00d4b379a/js/ctypes/libffi | title=Mozilla-central @ 2dc00d4b379a files manifest }}</ref>

On [Mac OS X](/source/Mac_OS_X), libffi is commonly used with [BridgeSupport](/source/BridgeSupport), which provides [programming language](/source/programming_language) neutral descriptions of [framework](/source/Software_framework) interfaces, and [Nu](/source/Nu_(programming_language)) which binds direct [Objective-C](/source/Objective-C) access from [Lisp](/source/Lisp_(programming_language)).

libffi has been widely [ported](/source/porting) and is released under a [MIT license](/source/MIT_license).

== Background ==
Although the C programming language is ubiquitous among platforms, the ways function calls are implemented in machine code &ndash; the [calling convention](/source/calling_convention) &ndash; 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&ndash;OS combinations). Its C [API](/source/application_programming_interface), which is shared on all builds of libffi regardless of platform, [abstracts](/source/Abstraction_(computer_science)) over the complexity of loading code on different platforms. In addition to regular functions, it also supports C-style [variadic function](/source/variadic_function)s.

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.

== History ==
{{Missing information|section|year of gencall|date=January 2021}}
libffi, originally developed by Anthony Green, was inspired by the Gencall library from [Silicon Graphics](/source/Silicon_Graphics). Gencall was developed by Gianni Mariani, then employed by [SGI](/source/Silicon_Graphics), for the purpose of allowing calls to functions by address and creating a call frame for the particular calling convention ([MIPS](/source/MIPS_architecture) O32). Anthony Green refined the idea and extended it to other architectures and calling conventions and open sourcing libffi.

== Adoption ==
The libffi library is useful in building a bridge between interpreted and natively compiled code. Some notable users include:

=== D ===
:; [https://github.com/lycus/libffi-d libffi-d]
:: Bindings to the libffi library for D. Primarily used for the [https://github.com/lycus/mci MCI] (Managed Compiler Infrastructure).

=== F-Script ===
:;[F-Script](/source/F-Script)
:: Dynamically generates Cocoa classes written in F-Script.

=== Guile ===
:; [GNU Guile](/source/GNU_Guile)
:: libffi is used in Guile 1.9.8 and onwards

=== Haskell ===
:;[GHC](/source/Glasgow_Haskell_Compiler)
:: libffi has been used for the majority of the FFI performed by the GHC since late 2009.

=== Java ===
:; [OpenJDK](/source/OpenJDK)
:: The open-source implementation of the Java Platform Standard Edition uses libffi to bridge between the interpreter and native code for Zero-Assembly port.

:; [Dalvik](/source/Dalvik_virtual_machine)
:: Dalvik is the virtual machine which runs the Java platform on [Android](/source/Android_(operating_system)) mobile devices. libffi is used on Android ports for which no custom bridging code has been written.

:; [Java Native Access](/source/Java_Native_Access) (JNA)
:: The JNI-free way to call native code from Java.

:; [gcj](/source/GNU_Compiler_for_Java)
:: The runtime library for the GNU Compiler for the Java Programming Language uses libffi to handle calls back and forth between interpreted and natively compiled code. gcj was a part of the GCC, the [GNU Compiler Collection](/source/GNU_Compiler_Collection).

=== JavaScript ===
:; [http://inexdo.com/JSCocoa JSCocoa]
:: Call Objective-C code from javascript on Mac OS X and the iPhone (via the libffi-iphone port).

:; [Mozilla](/source/Mozilla)
:: libffi is used in the [js-ctypes](/source/js-ctypes) library (previously known as ctypes.jsm) to call C functions within [JavaScript](/source/JavaScript) code (available in [XULRunner](/source/XULRunner) applications, [Firefox](/source/Firefox) extensions etc).<ref>[https://developer.mozilla.org/en/js-ctypes https://developer.mozilla.org/en/js-ctypes] {{Webarchive|url=https://web.archive.org/web/20110629121742/https://developer.mozilla.org/en/js-ctypes |date=2011-06-29 }}, [https://developer.mozilla.org/en/js-ctypes/js-ctypes_reference js-ctypes reference] {{Webarchive|url=https://web.archive.org/web/20110629121815/https://developer.mozilla.org/en/js-ctypes/js-ctypes_reference |date=2011-06-29 }}</ref>

:; [https://github.com/rbranson/node-ffi node-ffi]
:: A [Node.js](/source/Node.js) addon for loading and calling dynamic libraries from JavaScript.

=== Lisp ===
:; cffi-libffi
:: The standard foreign function interface library for Common Lisp [http://common-lisp.net/project/cffi/ CFFI] includes the cffi-libffi system to include support for passing and returning structure arguments by value.

:; [https://github.com/pixie-lang/pixie pixie]
:: pixie (a [Clojure](/source/Clojure) inspired Lisp dialect) uses libffi as the default foreign function interface mechanism.

=== Perl ===
:; [https://metacpan.org/pod/FFI::Raw FFI::Raw]
:: A thin wrapper around libffi.
:; [https://metacpan.org/pod/FFI::Platypus FFI::Platypus]
:: Newer, more convenient wrapper around libffi.

=== Python ===
thumb|Calling a shared library from within a Python script.
:; [CPython](/source/CPython)
:: The default, most-widely used implementation of the Python programming language uses libffi in the standard ctypes library.

:; [PyObjC](/source/PyObjC)
:: Call Objective-C code from Python on Mac OS X.

=== Racket ===
:; [Racket](/source/Racket_(programming_language))
:: Call C code from this popular Scheme implementation.

=== Ruby ===
:; [https://docs.ruby-lang.org/en/3.4/Fiddle.html Fiddle]
:: A libffi wrapper in the Ruby Standard Library

:; [https://github.com/ffi/ffi Ruby-FFI]
:: A Foreign Function Interface extension for Ruby.

:; [RubyCocoa](/source/RubyCocoa)
:: Call Objective-C code from Ruby on Mac OS X.

== References ==
{{Reflist}}

== External links ==
*{{official website}}
*[http://moxielogic.org/blog Anthony Green's blog]

Category:C (programming language) libraries
Category:Free computer libraries

---
Adapted from the Wikipedia article [Libffi](https://en.wikipedia.org/wiki/Libffi) by Wikipedia contributors ([contributor history](https://en.wikipedia.org/wiki/Libffi?action=history)). Available under [Creative Commons Attribution-ShareAlike 4.0 International](https://creativecommons.org/licenses/by-sa/4.0/). Changes may have been made.
