1 This is libffi.info, produced by makeinfo version 5.1 from libffi.texi.
3 This manual is for Libffi, a portable foreign-function interface
6 Copyright (C) 2008, 2010, 2011 Red Hat, Inc.
8 Permission is granted to copy, distribute and/or modify this
9 document under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2, or (at
11 your option) any later version. A copy of the license is included
12 in the section entitled "GNU General Public License".
14 INFO-DIR-SECTION Development
16 * libffi: (libffi). Portable foreign-function interface library.
20 File: libffi.info, Node: Top, Next: Introduction, Up: (dir)
25 This manual is for Libffi, a portable foreign-function interface
28 Copyright (C) 2008, 2010, 2011 Red Hat, Inc.
30 Permission is granted to copy, distribute and/or modify this
31 document under the terms of the GNU General Public License as
32 published by the Free Software Foundation; either version 2, or (at
33 your option) any later version. A copy of the license is included
34 in the section entitled "GNU General Public License".
38 * Introduction:: What is libffi?
39 * Using libffi:: How to use libffi.
40 * Missing Features:: Things libffi can't do.
44 File: libffi.info, Node: Introduction, Next: Using libffi, Prev: Top, Up: Top
49 Compilers for high level languages generate code that follow certain
50 conventions. These conventions are necessary, in part, for separate
51 compilation to work. One such convention is the "calling convention".
52 The calling convention is a set of assumptions made by the compiler
53 about where function arguments will be found on entry to a function. A
54 calling convention also specifies where the return value for a function
55 is found. The calling convention is also sometimes called the "ABI" or
56 "Application Binary Interface".
58 Some programs may not know at the time of compilation what arguments
59 are to be passed to a function. For instance, an interpreter may be
60 told at run-time about the number and types of arguments used to call a
61 given function. 'Libffi' can be used in such programs to provide a
62 bridge from the interpreter program to compiled code.
64 The 'libffi' library provides a portable, high level programming
65 interface to various calling conventions. This allows a programmer to
66 call any function specified by a call interface description at run time.
68 FFI stands for Foreign Function Interface. A foreign function
69 interface is the popular name for the interface that allows code written
70 in one language to call code written in another language. The 'libffi'
71 library really only provides the lowest, machine dependent layer of a
72 fully featured foreign function interface. A layer must exist above
73 'libffi' that handles type conversions for values passed between the two
77 File: libffi.info, Node: Using libffi, Next: Missing Features, Prev: Introduction, Up: Top
84 * The Basics:: The basic libffi API.
85 * Simple Example:: A simple example.
86 * Types:: libffi type descriptions.
87 * Multiple ABIs:: Different passing styles on one platform.
88 * The Closure API:: Writing a generic function.
89 * Closure Example:: A closure example.
92 File: libffi.info, Node: The Basics, Next: Simple Example, Up: Using libffi
97 'Libffi' assumes that you have a pointer to the function you wish to
98 call and that you know the number and types of arguments to pass it, as
99 well as the return type of the function.
101 The first thing you must do is create an 'ffi_cif' object that
102 matches the signature of the function you wish to call. This is a
103 separate step because it is common to make multiple calls using a single
104 'ffi_cif'. The "cif" in 'ffi_cif' stands for Call InterFace. To
105 prepare a call interface object, use the function 'ffi_prep_cif'.
107 -- Function: ffi_status ffi_prep_cif (ffi_cif *CIF, ffi_abi ABI,
108 unsigned int NARGS, ffi_type *RTYPE, ffi_type **ARGTYPES)
109 This initializes CIF according to the given parameters.
111 ABI is the ABI to use; normally 'FFI_DEFAULT_ABI' is what you want.
112 *note Multiple ABIs:: for more information.
114 NARGS is the number of arguments that this function accepts.
116 RTYPE is a pointer to an 'ffi_type' structure that describes the
117 return type of the function. *Note Types::.
119 ARGTYPES is a vector of 'ffi_type' pointers. ARGTYPES must have
120 NARGS elements. If NARGS is 0, this argument is ignored.
122 'ffi_prep_cif' returns a 'libffi' status code, of type
123 'ffi_status'. This will be either 'FFI_OK' if everything worked
124 properly; 'FFI_BAD_TYPEDEF' if one of the 'ffi_type' objects is
125 incorrect; or 'FFI_BAD_ABI' if the ABI parameter is invalid.
127 If the function being called is variadic (varargs) then
128 'ffi_prep_cif_var' must be used instead of 'ffi_prep_cif'.
130 -- Function: ffi_status ffi_prep_cif_var (ffi_cif *CIF, ffi_abi varabi,
131 unsigned int NFIXEDARGS, unsigned int varntotalargs, ffi_type
132 *RTYPE, ffi_type **ARGTYPES)
133 This initializes CIF according to the given parameters for a call
134 to a variadic function. In general it's operation is the same as
135 for 'ffi_prep_cif' except that:
137 NFIXEDARGS is the number of fixed arguments, prior to any variadic
138 arguments. It must be greater than zero.
140 NTOTALARGS the total number of arguments, including variadic and
143 Note that, different cif's must be prepped for calls to the same
144 function when different numbers of arguments are passed.
146 Also note that a call to 'ffi_prep_cif_var' with
147 NFIXEDARGS=NOTOTALARGS is NOT equivalent to a call to
150 To call a function using an initialized 'ffi_cif', use the 'ffi_call'
153 -- Function: void ffi_call (ffi_cif *CIF, void *FN, void *RVALUE, void
155 This calls the function FN according to the description given in
156 CIF. CIF must have already been prepared using 'ffi_prep_cif'.
158 RVALUE is a pointer to a chunk of memory that will hold the result
159 of the function call. This must be large enough to hold the
160 result, no smaller than the system register size (generally 32 or
161 64 bits), and must be suitably aligned; it is the caller's
162 responsibility to ensure this. If CIF declares that the function
163 returns 'void' (using 'ffi_type_void'), then RVALUE is ignored.
165 AVALUES is a vector of 'void *' pointers that point to the memory
166 locations holding the argument values for a call. If CIF declares
167 that the function has no arguments (i.e., NARGS was 0), then
168 AVALUES is ignored. Note that argument values may be modified by
169 the callee (for instance, structs passed by value); the burden of
170 copying pass-by-value arguments is placed on the caller.
173 File: libffi.info, Node: Simple Example, Next: Types, Prev: The Basics, Up: Using libffi
178 Here is a trivial example that calls 'puts' a few times.
191 /* Initialize the argument info vectors */
192 args[0] = &ffi_type_pointer;
195 /* Initialize the cif */
196 if (ffi_prep_cif(&cif, FFI_DEFAULT_ABI, 1,
197 &ffi_type_sint, args) == FFI_OK)
200 ffi_call(&cif, puts, &rc, values);
201 /* rc now holds the result of the call to puts */
203 /* values holds a pointer to the function's arg, so to
204 call puts() again all we need to do is change the
207 ffi_call(&cif, puts, &rc, values);
214 File: libffi.info, Node: Types, Next: Multiple ABIs, Prev: Simple Example, Up: Using libffi
221 * Primitive Types:: Built-in types.
222 * Structures:: Structure types.
223 * Type Example:: Structure type example.
226 File: libffi.info, Node: Primitive Types, Next: Structures, Up: Types
228 2.3.1 Primitive Types
229 ---------------------
231 'Libffi' provides a number of built-in type descriptors that can be used
232 to describe argument and return types:
235 The type 'void'. This cannot be used for argument types, only for
239 An unsigned, 8-bit integer type.
242 A signed, 8-bit integer type.
245 An unsigned, 16-bit integer type.
248 A signed, 16-bit integer type.
251 An unsigned, 32-bit integer type.
254 A signed, 32-bit integer type.
257 An unsigned, 64-bit integer type.
260 A signed, 64-bit integer type.
269 The C 'unsigned char' type.
272 The C 'signed char' type. (Note that there is not an exact
273 equivalent to the C 'char' type in 'libffi'; ordinarily you should
274 either use 'ffi_type_schar' or 'ffi_type_uchar' depending on
275 whether 'char' is signed.)
278 The C 'unsigned short' type.
284 The C 'unsigned int' type.
290 The C 'unsigned long' type.
295 'ffi_type_longdouble'
296 On platforms that have a C 'long double' type, this is defined. On
297 other platforms, it is not.
300 A generic 'void *' pointer. You should use this for all pointers,
301 regardless of their real type.
303 Each of these is of type 'ffi_type', so you must take the address
304 when passing to 'ffi_prep_cif'.
307 File: libffi.info, Node: Structures, Next: Type Example, Prev: Primitive Types, Up: Types
312 Although 'libffi' has no special support for unions or bit-fields, it is
313 perfectly happy passing structures back and forth. You must first
314 describe the structure to 'libffi' by creating a new 'ffi_type' object
317 -- Data type: ffi_type
318 The 'ffi_type' has the following members:
320 This is set by 'libffi'; you should initialize it to zero.
322 'unsigned short alignment'
323 This is set by 'libffi'; you should initialize it to zero.
325 'unsigned short type'
326 For a structure, this should be set to 'FFI_TYPE_STRUCT'.
328 'ffi_type **elements'
329 This is a 'NULL'-terminated array of pointers to 'ffi_type'
330 objects. There is one element per field of the struct.
333 File: libffi.info, Node: Type Example, Prev: Structures, Up: Types
338 The following example initializes a 'ffi_type' object representing the
339 'tm' struct from Linux's 'time.h'.
341 Here is how the struct is defined:
353 /* Those are for future use. */
354 long int __tm_gmtoff__;
355 __const char *__tm_zone__;
358 Here is the corresponding code to describe this struct to 'libffi':
362 ffi_type *tm_type_elements[12];
365 tm_type.size = tm_type.alignment = 0;
366 tm_type.type = FFI_TYPE_STRUCT;
367 tm_type.elements = &tm_type_elements;
369 for (i = 0; i < 9; i++)
370 tm_type_elements[i] = &ffi_type_sint;
372 tm_type_elements[9] = &ffi_type_slong;
373 tm_type_elements[10] = &ffi_type_pointer;
374 tm_type_elements[11] = NULL;
376 /* tm_type can now be used to represent tm argument types and
377 return types for ffi_prep_cif() */
381 File: libffi.info, Node: Multiple ABIs, Next: The Closure API, Prev: Types, Up: Using libffi
386 A given platform may provide multiple different ABIs at once. For
387 instance, the x86 platform has both 'stdcall' and 'fastcall' functions.
389 'libffi' provides some support for this. However, this is
390 necessarily platform-specific.
393 File: libffi.info, Node: The Closure API, Next: Closure Example, Prev: Multiple ABIs, Up: Using libffi
398 'libffi' also provides a way to write a generic function - a function
399 that can accept and decode any combination of arguments. This can be
400 useful when writing an interpreter, or to provide wrappers for arbitrary
403 This facility is called the "closure API". Closures are not supported
404 on all platforms; you can check the 'FFI_CLOSURES' define to determine
405 whether they are supported on the current platform.
407 Because closures work by assembling a tiny function at runtime, they
408 require special allocation on platforms that have a non-executable heap.
409 Memory management for closures is handled by a pair of functions:
411 -- Function: void *ffi_closure_alloc (size_t SIZE, void **CODE)
412 Allocate a chunk of memory holding SIZE bytes. This returns a
413 pointer to the writable address, and sets *CODE to the
414 corresponding executable address.
416 SIZE should be sufficient to hold a 'ffi_closure' object.
418 -- Function: void ffi_closure_free (void *WRITABLE)
419 Free memory allocated using 'ffi_closure_alloc'. The argument is
420 the writable address that was returned.
422 Once you have allocated the memory for a closure, you must construct
423 a 'ffi_cif' describing the function call. Finally you can prepare the
426 -- Function: ffi_status ffi_prep_closure_loc (ffi_closure *CLOSURE,
427 ffi_cif *CIF, void (*FUN) (ffi_cif *CIF, void *RET, void
428 **ARGS, void *USER_DATA), void *USER_DATA, void *CODELOC)
429 Prepare a closure function.
431 CLOSURE is the address of a 'ffi_closure' object; this is the
432 writable address returned by 'ffi_closure_alloc'.
434 CIF is the 'ffi_cif' describing the function parameters.
436 USER_DATA is an arbitrary datum that is passed, uninterpreted, to
437 your closure function.
439 CODELOC is the executable address returned by 'ffi_closure_alloc'.
441 FUN is the function which will be called when the closure is
442 invoked. It is called with the arguments:
444 The 'ffi_cif' passed to 'ffi_prep_closure_loc'.
447 A pointer to the memory used for the function's return value.
448 FUN must fill this, unless the function is declared as
452 A vector of pointers to memory holding the arguments to the
456 The same USER_DATA that was passed to 'ffi_prep_closure_loc'.
458 'ffi_prep_closure_loc' will return 'FFI_OK' if everything went ok,
459 and something else on error.
461 After calling 'ffi_prep_closure_loc', you can cast CODELOC to the
462 appropriate pointer-to-function type.
464 You may see old code referring to 'ffi_prep_closure'. This function
465 is deprecated, as it cannot handle the need for separate writable and
466 executable addresses.
469 File: libffi.info, Node: Closure Example, Prev: The Closure API, Up: Using libffi
474 A trivial example that creates a new 'puts' by binding 'fputs' with
480 /* Acts like puts with the file given at time of enclosure. */
481 void puts_binding(ffi_cif *cif, void *ret, void* args[],
484 *(ffi_arg *)ret = fputs(*(char **)args[0], (FILE *)stream);
487 typedef int (*puts_t)(char *);
493 ffi_closure *closure;
498 /* Allocate closure and bound_puts */
499 closure = ffi_closure_alloc(sizeof(ffi_closure), &bound_puts);
503 /* Initialize the argument info vectors */
504 args[0] = &ffi_type_pointer;
506 /* Initialize the cif */
507 if (ffi_prep_cif(&cif, FFI_DEFAULT_ABI, 1,
508 &ffi_type_sint, args) == FFI_OK)
510 /* Initialize the closure, setting stream to stdout */
511 if (ffi_prep_closure_loc(closure, &cif, puts_binding,
512 stdout, bound_puts) == FFI_OK)
514 rc = ((puts_t)bound_puts)("Hello World!");
515 /* rc now holds the result of the call to fputs */
520 /* Deallocate both closure, and bound_puts */
521 ffi_closure_free(closure);
527 File: libffi.info, Node: Missing Features, Next: Index, Prev: Using libffi, Up: Top
532 'libffi' is missing a few features. We welcome patches to add support
537 * There is no support for bit fields in structures.
541 * The "raw" API is undocumented.
543 Note that variadic support is very new and tested on a relatively
544 small number of platforms.
547 File: libffi.info, Node: Index, Prev: Missing Features, Up: Top
555 * ABI: Introduction. (line 13)
556 * Application Binary Interface: Introduction. (line 13)
557 * calling convention: Introduction. (line 13)
558 * cif: The Basics. (line 14)
559 * closure API: The Closure API. (line 13)
560 * closures: The Closure API. (line 13)
561 * FFI: Introduction. (line 31)
562 * ffi_call: The Basics. (line 62)
563 * FFI_CLOSURES: The Closure API. (line 13)
564 * ffi_closure_alloc: The Closure API. (line 19)
565 * ffi_closure_free: The Closure API. (line 26)
566 * ffi_prep_cif: The Basics. (line 16)
567 * ffi_prep_cif_var: The Basics. (line 39)
568 * ffi_prep_closure_loc: The Closure API. (line 34)
569 * ffi_status: The Basics. (line 16)
570 * ffi_status <1>: The Basics. (line 39)
571 * ffi_status <2>: The Closure API. (line 34)
572 * ffi_type: Structures. (line 11)
573 * ffi_type <1>: Structures. (line 11)
574 * ffi_type_double: Primitive Types. (line 41)
575 * ffi_type_float: Primitive Types. (line 38)
576 * ffi_type_longdouble: Primitive Types. (line 71)
577 * ffi_type_pointer: Primitive Types. (line 75)
578 * ffi_type_schar: Primitive Types. (line 47)
579 * ffi_type_sint: Primitive Types. (line 62)
580 * ffi_type_sint16: Primitive Types. (line 23)
581 * ffi_type_sint32: Primitive Types. (line 29)
582 * ffi_type_sint64: Primitive Types. (line 35)
583 * ffi_type_sint8: Primitive Types. (line 17)
584 * ffi_type_slong: Primitive Types. (line 68)
585 * ffi_type_sshort: Primitive Types. (line 56)
586 * ffi_type_uchar: Primitive Types. (line 44)
587 * ffi_type_uint: Primitive Types. (line 59)
588 * ffi_type_uint16: Primitive Types. (line 20)
589 * ffi_type_uint32: Primitive Types. (line 26)
590 * ffi_type_uint64: Primitive Types. (line 32)
591 * ffi_type_uint8: Primitive Types. (line 14)
592 * ffi_type_ulong: Primitive Types. (line 65)
593 * ffi_type_ushort: Primitive Types. (line 53)
594 * ffi_type_void: Primitive Types. (line 10)
595 * Foreign Function Interface: Introduction. (line 31)
596 * void: The Basics. (line 62)
597 * void <1>: The Closure API. (line 19)
598 * void <2>: The Closure API. (line 26)
604 Node: Introduction
\7f1429
605 Node: Using libffi
\7f3061
606 Node: The Basics
\7f3547
607 Node: Simple Example
\7f7198
609 Node: Primitive Types
\7f8512
610 Node: Structures
\7f10333
611 Node: Type Example
\7f11207
612 Node: Multiple ABIs
\7f12473
613 Node: The Closure API
\7f12844
614 Node: Closure Example
\7f15788
615 Node: Missing Features
\7f17396