1This directory contains the libffi package, which is not part of GCC but
2shipped with GCC as convenience.
3
4Copied without changes from CPython 2.7 head (e04e1f253ed8).
5
6Status
7======
8
9libffi-2.00 has not been released yet! This is a development snapshot!
10
11libffi-1.20 was released on October 5, 1998. Check the libffi web
12page for updates: <URL:http://sources.redhat.com/libffi/>.
13
14
15What is libffi?
16===============
17
18Compilers for high level languages generate code that follow certain
19conventions. These conventions are necessary, in part, for separate
20compilation to work. One such convention is the "calling
21convention". The "calling convention" is essentially a set of
22assumptions made by the compiler about where function arguments will
23be found on entry to a function. A "calling convention" also specifies
24where the return value for a function is found.
25
26Some programs may not know at the time of compilation what arguments
27are to be passed to a function. For instance, an interpreter may be
28told at run-time about the number and types of arguments used to call
29a given function. Libffi can be used in such programs to provide a
30bridge from the interpreter program to compiled code.
31
32The libffi library provides a portable, high level programming
33interface to various calling conventions. This allows a programmer to
34call any function specified by a call interface description at run
35time.
36
37Ffi stands for Foreign Function Interface. A foreign function
38interface is the popular name for the interface that allows code
39written in one language to call code written in another language. The
40libffi library really only provides the lowest, machine dependent
41layer of a fully featured foreign function interface. A layer must
42exist above libffi that handles type conversions for values passed
43between the two languages.
44
45
46Supported Platforms and Prerequisites
47=====================================
48
49Libffi has been ported to:
50
51 SunOS 4.1.3 & Solaris 2.x (SPARC-V8, SPARC-V9)
52
53 Irix 5.3 & 6.2 (System V/o32 & n32)
54
55 Intel x86 - Linux (System V ABI)
56
57 Alpha - Linux and OSF/1
58
59 m68k - Linux (System V ABI)
60
61 PowerPC - Linux (System V ABI, Darwin, AIX)
62
63 ARM - Linux (System V ABI)
64
65Libffi has been tested with the egcs 1.0.2 gcc compiler. Chances are
66that other versions will work. Libffi has also been built and tested
67with the SGI compiler tools.
68
69On PowerPC, the tests failed (see the note below).
70
71You must use GNU make to build libffi. SGI's make will not work.
72Sun's probably won't either.
73
74If you port libffi to another platform, please let me know! I assume
75that some will be easy (x86 NetBSD), and others will be more difficult
76(HP).
77
78
79Installing libffi
80=================
81
82[Note: before actually performing any of these installation steps,
83 you may wish to read the "Platform Specific Notes" below.]
84
85First you must configure the distribution for your particular
86system. Go to the directory you wish to build libffi in and run the
87"configure" program found in the root directory of the libffi source
88distribution.
89
90You may want to tell configure where to install the libffi library and
91header files. To do that, use the --prefix configure switch. Libffi
92will install under /usr/local by default.
93
94If you want to enable extra run-time debugging checks use the the
95--enable-debug configure switch. This is useful when your program dies
96mysteriously while using libffi.
97
98Another useful configure switch is --enable-purify-safety. Using this
99will add some extra code which will suppress certain warnings when you
100are using Purify with libffi. Only use this switch when using
101Purify, as it will slow down the library.
102
103Configure has many other options. Use "configure --help" to see them all.
104
105Once configure has finished, type "make". Note that you must be using
106GNU make. SGI's make will not work. Sun's probably won't either.
107You can ftp GNU make from prep.ai.mit.edu:/pub/gnu.
108
109To ensure that libffi is working as advertised, type "make test".
110
111To install the library and header files, type "make install".
112
113
114Using libffi
115============
116
117 The Basics
118 ----------
119
120Libffi assumes that you have a pointer to the function you wish to
121call and that you know the number and types of arguments to pass it,
122as well as the return type of the function.
123
124The first thing you must do is create an ffi_cif object that matches
125the signature of the function you wish to call. The cif in ffi_cif
126stands for Call InterFace. To prepare a call interface object, use the
127following function:
128
129ffi_status ffi_prep_cif(ffi_cif *cif, ffi_abi abi,
130 unsigned int nargs,
131 ffi_type *rtype, ffi_type **atypes);
132
133 CIF is a pointer to the call interface object you wish
134 to initialize.
135
136 ABI is an enum that specifies the calling convention
137 to use for the call. FFI_DEFAULT_ABI defaults
138 to the system's native calling convention. Other
139 ABI's may be used with care. They are system
140 specific.
141
142 NARGS is the number of arguments this function accepts.
143 libffi does not yet support vararg functions.
144
145 RTYPE is a pointer to an ffi_type structure that represents
146 the return type of the function. Ffi_type objects
147 describe the types of values. libffi provides
148 ffi_type objects for many of the native C types:
149 signed int, unsigned int, signed char, unsigned char,
150 etc. There is also a pointer ffi_type object and
151 a void ffi_type. Use &ffi_type_void for functions that
152 don't return values.
153
154 ATYPES is a vector of ffi_type pointers. ARGS must be NARGS long.
155 If NARGS is 0, this is ignored.
156
157
158ffi_prep_cif will return a status code that you are responsible
159for checking. It will be one of the following:
160
161 FFI_OK - All is good.
162
163 FFI_BAD_TYPEDEF - One of the ffi_type objects that ffi_prep_cif
164 came across is bad.
165
166
167Before making the call, the VALUES vector should be initialized
168with pointers to the appropriate argument values.
169
170To call the the function using the initialized ffi_cif, use the
171ffi_call function:
172
173void ffi_call(ffi_cif *cif, void *fn, void *rvalue, void **avalues);
174
175 CIF is a pointer to the ffi_cif initialized specifically
176 for this function.
177
178 FN is a pointer to the function you want to call.
179
180 RVALUE is a pointer to a chunk of memory that is to hold the
181 result of the function call. Currently, it must be
182 at least one word in size (except for the n32 version
183 under Irix 6.x, which must be a pointer to an 8 byte
184 aligned value (a long long). It must also be at least
185 word aligned (depending on the return type, and the
186 system's alignment requirements). If RTYPE is
187 &ffi_type_void, this is ignored. If RVALUE is NULL,
188 the return value is discarded.
189
190 AVALUES is a vector of void* that point to the memory locations
191 holding the argument values for a call.
192 If NARGS is 0, this is ignored.
193
194
195If you are expecting a return value from FN it will have been stored
196at RVALUE.
197
198
199
200 An Example
201 ----------
202
203Here is a trivial example that calls puts() a few times.
204
205 #include <stdio.h>
206 #include <ffi.h>
207
208 int main()
209 {
210 ffi_cif cif;
211 ffi_type *args[1];
212 void *values[1];
213 char *s;
214 int rc;
215
216 /* Initialize the argument info vectors */
217 args[0] = &ffi_type_uint;
218 values[0] = &s;
219
220 /* Initialize the cif */
221 if (ffi_prep_cif(&cif, FFI_DEFAULT_ABI, 1,
222 &ffi_type_uint, args) == FFI_OK)
223 {
224 s = "Hello World!";
225 ffi_call(&cif, puts, &rc, values);
226 /* rc now holds the result of the call to puts */
227
228 /* values holds a pointer to the function's arg, so to
229 call puts() again all we need to do is change the
230 value of s */
231 s = "This is cool!";
232 ffi_call(&cif, puts, &rc, values);
233 }
234
235 return 0;
236 }
237
238
239
240 Aggregate Types
241 ---------------
242
243Although libffi has no special support for unions or bit-fields, it is
244perfectly happy passing structures back and forth. You must first
245describe the structure to libffi by creating a new ffi_type object
246for it. Here is the definition of ffi_type:
247
248 typedef struct _ffi_type
249 {
250 unsigned size;
251 short alignment;
252 short type;
253 struct _ffi_type **elements;
254 } ffi_type;
255
256All structures must have type set to FFI_TYPE_STRUCT. You may set
257size and alignment to 0. These will be calculated and reset to the
258appropriate values by ffi_prep_cif().
259
260elements is a NULL terminated array of pointers to ffi_type objects
261that describe the type of the structure elements. These may, in turn,
262be structure elements.
263
264The following example initializes a ffi_type object representing the
265tm struct from Linux's time.h:
266
267 struct tm {
268 int tm_sec;
269 int tm_min;
270 int tm_hour;
271 int tm_mday;
272 int tm_mon;
273 int tm_year;
274 int tm_wday;
275 int tm_yday;
276 int tm_isdst;
277 /* Those are for future use. */
278 long int __tm_gmtoff__;
279 __const char *__tm_zone__;
280 };
281
282 {
283 ffi_type tm_type;
284 ffi_type *tm_type_elements[12];
285 int i;
286
287 tm_type.size = tm_type.alignment = 0;
288 tm_type.elements = &tm_type_elements;
289
290 for (i = 0; i < 9; i++)
291 tm_type_elements[i] = &ffi_type_sint;
292
293 tm_type_elements[9] = &ffi_type_slong;
294 tm_type_elements[10] = &ffi_type_pointer;
295 tm_type_elements[11] = NULL;
296
297 /* tm_type can now be used to represent tm argument types and
298 return types for ffi_prep_cif() */
299 }
300
301
302
303Platform Specific Notes
304=======================
305
306 Intel x86
307 ---------
308
309There are no known problems with the x86 port.
310
311 Sun SPARC - SunOS 4.1.3 & Solaris 2.x
312 -------------------------------------
313
314You must use GNU Make to build libffi on Sun platforms.
315
316 MIPS - Irix 5.3 & 6.x
317 ---------------------
318
319Irix 6.2 and better supports three different calling conventions: o32,
320n32 and n64. Currently, libffi only supports both o32 and n32 under
321Irix 6.x, but only o32 under Irix 5.3. Libffi will automatically be
322configured for whichever calling convention it was built for.
323
324By default, the configure script will try to build libffi with the GNU
325development tools. To build libffi with the SGI development tools, set
326the environment variable CC to either "cc -32" or "cc -n32" before
327running configure under Irix 6.x (depending on whether you want an o32
328or n32 library), or just "cc" for Irix 5.3.
329
330With the n32 calling convention, when returning structures smaller
331than 16 bytes, be sure to provide an RVALUE that is 8 byte aligned.
332Here's one way of forcing this:
333
334 double struct_storage[2];
335 my_small_struct *s = (my_small_struct *) struct_storage;
336 /* Use s for RVALUE */
337
338If you don't do this you are liable to get spurious bus errors.
339
340"long long" values are not supported yet.
341
342You must use GNU Make to build libffi on SGI platforms.
343
344 ARM - System V ABI
345 ------------------
346
347The ARM port was performed on a NetWinder running ARM Linux ELF
348(2.0.31) and gcc 2.8.1.
349
350
351
352 PowerPC System V ABI
353 --------------------
354
355There are two `System V ABI's which libffi implements for PowerPC.
356They differ only in how small structures are returned from functions.
357
358In the FFI_SYSV version, structures that are 8 bytes or smaller are
359returned in registers. This is what GCC does when it is configured
360for solaris, and is what the System V ABI I have (dated September
3611995) says.
362
363In the FFI_GCC_SYSV version, all structures are returned the same way:
364by passing a pointer as the first argument to the function. This is
365what GCC does when it is configured for linux or a generic sysv
366target.
367
368EGCS 1.0.1 (and probably other versions of EGCS/GCC) also has a
369inconsistency with the SysV ABI: When a procedure is called with many
370floating-point arguments, some of them get put on the stack. They are
371all supposed to be stored in double-precision format, even if they are
372only single-precision, but EGCS stores single-precision arguments as
373single-precision anyway. This causes one test to fail (the `many
374arguments' test).
375
376
377What's With The Crazy Comments?
378===============================
379
380You might notice a number of cryptic comments in the code, delimited
381by /*@ and @*/. These are annotations read by the program LCLint, a
382tool for statically checking C programs. You can read all about it at
383<http://larch-www.lcs.mit.edu:8001/larch/lclint/index.html>.
384
385
386History
387=======
388
3891.20 Oct-5-98
390 Raffaele Sena produces ARM port.
391
3921.19 Oct-5-98
393 Fixed x86 long double and long long return support.
394 m68k bug fixes from Andreas Schwab.
395 Patch for DU assembler compatibility for the Alpha from Richard
396 Henderson.
397
3981.18 Apr-17-98
399 Bug fixes and MIPS configuration changes.
400
4011.17 Feb-24-98
402 Bug fixes and m68k port from Andreas Schwab. PowerPC port from
403 Geoffrey Keating. Various bug x86, Sparc and MIPS bug fixes.
404
4051.16 Feb-11-98
406 Richard Henderson produces Alpha port.
407
4081.15 Dec-4-97
409 Fixed an n32 ABI bug. New libtool, auto* support.
410
4111.14 May-13-97
412 libtool is now used to generate shared and static libraries.
413 Fixed a minor portability problem reported by Russ McManus
414 <mcmanr@eq.gs.com>.
415
4161.13 Dec-2-96
417 Added --enable-purify-safety to keep Purify from complaining
418 about certain low level code.
419 Sparc fix for calling functions with < 6 args.
420 Linux x86 a.out fix.
421
4221.12 Nov-22-96
423 Added missing ffi_type_void, needed for supporting void return
424 types. Fixed test case for non MIPS machines. Cygnus Support
425 is now Cygnus Solutions.
426
4271.11 Oct-30-96
428 Added notes about GNU make.
429
4301.10 Oct-29-96
431 Added configuration fix for non GNU compilers.
432
4331.09 Oct-29-96
434 Added --enable-debug configure switch. Clean-ups based on LCLint
435 feedback. ffi_mips.h is always installed. Many configuration
436 fixes. Fixed ffitest.c for sparc builds.
437
4381.08 Oct-15-96
439 Fixed n32 problem. Many clean-ups.
440
4411.07 Oct-14-96
442 Gordon Irlam rewrites v8.S again. Bug fixes.
443
4441.06 Oct-14-96
445 Gordon Irlam improved the sparc port.
446
4471.05 Oct-14-96
448 Interface changes based on feedback.
449
4501.04 Oct-11-96
451 Sparc port complete (modulo struct passing bug).
452
4531.03 Oct-10-96
454 Passing struct args, and returning struct values works for
455 all architectures/calling conventions. Expanded tests.
456
4571.02 Oct-9-96
458 Added SGI n32 support. Fixed bugs in both o32 and Linux support.
459 Added "make test".
460
4611.01 Oct-8-96
462 Fixed float passing bug in mips version. Restructured some
463 of the code. Builds cleanly with SGI tools.
464
4651.00 Oct-7-96
466 First release. No public announcement.
467
468
469Authors & Credits
470=================
471
472libffi was written by Anthony Green <green@cygnus.com>.
473
474Portions of libffi were derived from Gianni Mariani's free gencall
475library for Silicon Graphics machines.
476
477The closure mechanism was designed and implemented by Kresten Krab
478Thorup.
479
480The Sparc port was derived from code contributed by the fine folks at
481Visible Decisions Inc <http://www.vdi.com>. Further enhancements were
482made by Gordon Irlam at Cygnus Solutions <http://www.cygnus.com>.
483
484The Alpha port was written by Richard Henderson at Cygnus Solutions.
485
486Andreas Schwab ported libffi to m68k Linux and provided a number of
487bug fixes.
488
489Geoffrey Keating ported libffi to the PowerPC.
490
491Raffaele Sena ported libffi to the ARM.
492
493Jesper Skov and Andrew Haley both did more than their fair share of
494stepping through the code and tracking down bugs.
495
496Thanks also to Tom Tromey for bug fixes and configuration help.
497
498Thanks to Jim Blandy, who provided some useful feedback on the libffi
499interface.
500
501If you have a problem, or have found a bug, please send a note to
502green@cygnus.com.
503