/Modules/_ctypes/libffi_osx/README

http://unladen-swallow.googlecode.com/ · #! · 500 lines · 353 code · 147 blank · 0 comment · 0 complexity · dd0edecd243634e1925c0ce2134aaa45 MD5 · raw file

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