ffi.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. /* -----------------------------------------------------------------*-C-*-
  2. libffi 3.3 - Copyright (c) 2011, 2014, 2019 Anthony Green
  3. - Copyright (c) 1996-2003, 2007, 2008 Red Hat, Inc.
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the ``Software''), to deal in the Software without
  7. restriction, including without limitation the rights to use, copy,
  8. modify, merge, publish, distribute, sublicense, and/or sell copies
  9. of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be
  12. included in all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND,
  14. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  15. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  16. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  17. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  18. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. DEALINGS IN THE SOFTWARE.
  21. ----------------------------------------------------------------------- */
  22. /* -------------------------------------------------------------------
  23. Most of the API is documented in doc/libffi.texi.
  24. The raw API is designed to bypass some of the argument packing and
  25. unpacking on architectures for which it can be avoided. Routines
  26. are provided to emulate the raw API if the underlying platform
  27. doesn't allow faster implementation.
  28. More details on the raw API can be found in:
  29. http://gcc.gnu.org/ml/java/1999-q3/msg00138.html
  30. and
  31. http://gcc.gnu.org/ml/java/1999-q3/msg00174.html
  32. -------------------------------------------------------------------- */
  33. #ifndef LIBFFI_H
  34. #define LIBFFI_H
  35. #ifdef __cplusplus
  36. extern "C" {
  37. #endif
  38. /* Specify which architecture libffi is configured for. */
  39. #ifndef AARCH64
  40. #define AARCH64
  41. #endif
  42. /* ---- System configuration information --------------------------------- */
  43. #include <ffitarget.h>
  44. #ifndef LIBFFI_ASM
  45. #if defined(_MSC_VER) && !defined(__clang__)
  46. #define __attribute__(X)
  47. #endif
  48. #include <stddef.h>
  49. #include <limits.h>
  50. /* LONG_LONG_MAX is not always defined (not if STRICT_ANSI, for example).
  51. But we can find it either under the correct ANSI name, or under GNU
  52. C's internal name. */
  53. #define FFI_64_BIT_MAX 9223372036854775807
  54. #ifdef LONG_LONG_MAX
  55. # define FFI_LONG_LONG_MAX LONG_LONG_MAX
  56. #else
  57. # ifdef LLONG_MAX
  58. # define FFI_LONG_LONG_MAX LLONG_MAX
  59. # ifdef _AIX52 /* or newer has C99 LLONG_MAX */
  60. # undef FFI_64_BIT_MAX
  61. # define FFI_64_BIT_MAX 9223372036854775807LL
  62. # endif /* _AIX52 or newer */
  63. # else
  64. # ifdef __GNUC__
  65. # define FFI_LONG_LONG_MAX __LONG_LONG_MAX__
  66. # endif
  67. # ifdef _AIX /* AIX 5.1 and earlier have LONGLONG_MAX */
  68. # ifndef __PPC64__
  69. # if defined (__IBMC__) || defined (__IBMCPP__)
  70. # define FFI_LONG_LONG_MAX LONGLONG_MAX
  71. # endif
  72. # endif /* __PPC64__ */
  73. # undef FFI_64_BIT_MAX
  74. # define FFI_64_BIT_MAX 9223372036854775807LL
  75. # endif
  76. # endif
  77. #endif
  78. /* The closure code assumes that this works on pointers, i.e. a size_t
  79. can hold a pointer. */
  80. typedef struct _ffi_type
  81. {
  82. size_t size;
  83. unsigned short alignment;
  84. unsigned short type;
  85. struct _ffi_type **elements;
  86. } ffi_type;
  87. /* Need minimal decorations for DLLs to work on Windows. GCC has
  88. autoimport and autoexport. Always mark externally visible symbols
  89. as dllimport for MSVC clients, even if it means an extra indirection
  90. when using the static version of the library.
  91. Besides, as a workaround, they can define FFI_BUILDING if they
  92. *know* they are going to link with the static library. */
  93. #if defined _MSC_VER
  94. # if defined FFI_BUILDING_DLL /* Building libffi.DLL with msvcc.sh */
  95. # define FFI_API __declspec(dllexport)
  96. # elif !defined FFI_BUILDING /* Importing libffi.DLL */
  97. # define FFI_API __declspec(dllimport)
  98. # else /* Building/linking static library */
  99. # define FFI_API
  100. # endif
  101. #else
  102. # define FFI_API
  103. #endif
  104. /* The externally visible type declarations also need the MSVC DLL
  105. decorations, or they will not be exported from the object file. */
  106. #if defined LIBFFI_HIDE_BASIC_TYPES
  107. # define FFI_EXTERN FFI_API
  108. #else
  109. # define FFI_EXTERN extern FFI_API
  110. #endif
  111. #ifndef LIBFFI_HIDE_BASIC_TYPES
  112. #if SCHAR_MAX == 127
  113. # define ffi_type_uchar ffi_type_uint8
  114. # define ffi_type_schar ffi_type_sint8
  115. #else
  116. #error "char size not supported"
  117. #endif
  118. #if SHRT_MAX == 32767
  119. # define ffi_type_ushort ffi_type_uint16
  120. # define ffi_type_sshort ffi_type_sint16
  121. #elif SHRT_MAX == 2147483647
  122. # define ffi_type_ushort ffi_type_uint32
  123. # define ffi_type_sshort ffi_type_sint32
  124. #else
  125. #error "short size not supported"
  126. #endif
  127. #if INT_MAX == 32767
  128. # define ffi_type_uint ffi_type_uint16
  129. # define ffi_type_sint ffi_type_sint16
  130. #elif INT_MAX == 2147483647
  131. # define ffi_type_uint ffi_type_uint32
  132. # define ffi_type_sint ffi_type_sint32
  133. #elif INT_MAX == 9223372036854775807
  134. # define ffi_type_uint ffi_type_uint64
  135. # define ffi_type_sint ffi_type_sint64
  136. #else
  137. #error "int size not supported"
  138. #endif
  139. #if LONG_MAX == 2147483647
  140. # if FFI_LONG_LONG_MAX != FFI_64_BIT_MAX
  141. #error "no 64-bit data type supported"
  142. # endif
  143. #elif LONG_MAX != FFI_64_BIT_MAX
  144. #error "long size not supported"
  145. #endif
  146. #if LONG_MAX == 2147483647
  147. # define ffi_type_ulong ffi_type_uint32
  148. # define ffi_type_slong ffi_type_sint32
  149. #elif LONG_MAX == FFI_64_BIT_MAX
  150. # define ffi_type_ulong ffi_type_uint64
  151. # define ffi_type_slong ffi_type_sint64
  152. #else
  153. #error "long size not supported"
  154. #endif
  155. /* These are defined in types.c. */
  156. FFI_EXTERN ffi_type ffi_type_void;
  157. FFI_EXTERN ffi_type ffi_type_uint8;
  158. FFI_EXTERN ffi_type ffi_type_sint8;
  159. FFI_EXTERN ffi_type ffi_type_uint16;
  160. FFI_EXTERN ffi_type ffi_type_sint16;
  161. FFI_EXTERN ffi_type ffi_type_uint32;
  162. FFI_EXTERN ffi_type ffi_type_sint32;
  163. FFI_EXTERN ffi_type ffi_type_uint64;
  164. FFI_EXTERN ffi_type ffi_type_sint64;
  165. FFI_EXTERN ffi_type ffi_type_float;
  166. FFI_EXTERN ffi_type ffi_type_double;
  167. FFI_EXTERN ffi_type ffi_type_pointer;
  168. #if 0
  169. FFI_EXTERN ffi_type ffi_type_longdouble;
  170. #else
  171. #define ffi_type_longdouble ffi_type_double
  172. #endif
  173. #ifdef FFI_TARGET_HAS_COMPLEX_TYPE
  174. FFI_EXTERN ffi_type ffi_type_complex_float;
  175. FFI_EXTERN ffi_type ffi_type_complex_double;
  176. #if 0
  177. FFI_EXTERN ffi_type ffi_type_complex_longdouble;
  178. #else
  179. #define ffi_type_complex_longdouble ffi_type_complex_double
  180. #endif
  181. #endif
  182. #endif /* LIBFFI_HIDE_BASIC_TYPES */
  183. typedef enum {
  184. FFI_OK = 0,
  185. FFI_BAD_TYPEDEF,
  186. FFI_BAD_ABI
  187. } ffi_status;
  188. typedef struct {
  189. ffi_abi abi;
  190. unsigned nargs;
  191. ffi_type **arg_types;
  192. ffi_type *rtype;
  193. unsigned bytes;
  194. unsigned flags;
  195. #ifdef FFI_EXTRA_CIF_FIELDS
  196. FFI_EXTRA_CIF_FIELDS;
  197. #endif
  198. } ffi_cif;
  199. /* ---- Definitions for the raw API -------------------------------------- */
  200. #ifndef FFI_SIZEOF_ARG
  201. # if LONG_MAX == 2147483647
  202. # define FFI_SIZEOF_ARG 4
  203. # elif LONG_MAX == FFI_64_BIT_MAX
  204. # define FFI_SIZEOF_ARG 8
  205. # endif
  206. #endif
  207. #ifndef FFI_SIZEOF_JAVA_RAW
  208. # define FFI_SIZEOF_JAVA_RAW FFI_SIZEOF_ARG
  209. #endif
  210. typedef union {
  211. ffi_sarg sint;
  212. ffi_arg uint;
  213. float flt;
  214. char data[FFI_SIZEOF_ARG];
  215. void* ptr;
  216. } ffi_raw;
  217. #if FFI_SIZEOF_JAVA_RAW == 4 && FFI_SIZEOF_ARG == 8
  218. /* This is a special case for mips64/n32 ABI (and perhaps others) where
  219. sizeof(void *) is 4 and FFI_SIZEOF_ARG is 8. */
  220. typedef union {
  221. signed int sint;
  222. unsigned int uint;
  223. float flt;
  224. char data[FFI_SIZEOF_JAVA_RAW];
  225. void* ptr;
  226. } ffi_java_raw;
  227. #else
  228. typedef ffi_raw ffi_java_raw;
  229. #endif
  230. FFI_API
  231. void ffi_raw_call (ffi_cif *cif,
  232. void (*fn)(void),
  233. void *rvalue,
  234. ffi_raw *avalue);
  235. FFI_API void ffi_ptrarray_to_raw (ffi_cif *cif, void **args, ffi_raw *raw);
  236. FFI_API void ffi_raw_to_ptrarray (ffi_cif *cif, ffi_raw *raw, void **args);
  237. FFI_API size_t ffi_raw_size (ffi_cif *cif);
  238. /* This is analogous to the raw API, except it uses Java parameter
  239. packing, even on 64-bit machines. I.e. on 64-bit machines longs
  240. and doubles are followed by an empty 64-bit word. */
  241. #if !FFI_NATIVE_RAW_API
  242. FFI_API
  243. void ffi_java_raw_call (ffi_cif *cif,
  244. void (*fn)(void),
  245. void *rvalue,
  246. ffi_java_raw *avalue) __attribute__((deprecated));
  247. #endif
  248. FFI_API
  249. void ffi_java_ptrarray_to_raw (ffi_cif *cif, void **args, ffi_java_raw *raw) __attribute__((deprecated));
  250. FFI_API
  251. void ffi_java_raw_to_ptrarray (ffi_cif *cif, ffi_java_raw *raw, void **args) __attribute__((deprecated));
  252. FFI_API
  253. size_t ffi_java_raw_size (ffi_cif *cif) __attribute__((deprecated));
  254. /* ---- Definitions for closures ----------------------------------------- */
  255. #if FFI_CLOSURES
  256. #ifdef _MSC_VER
  257. __declspec(align(8))
  258. #endif
  259. typedef struct {
  260. #if 1
  261. void *trampoline_table;
  262. void *trampoline_table_entry;
  263. #else
  264. char tramp[FFI_TRAMPOLINE_SIZE];
  265. #endif
  266. ffi_cif *cif;
  267. void (*fun)(ffi_cif*,void*,void**,void*);
  268. void *user_data;
  269. } ffi_closure
  270. #ifdef __GNUC__
  271. __attribute__((aligned (8)))
  272. #endif
  273. ;
  274. #ifndef __GNUC__
  275. # ifdef __sgi
  276. # pragma pack 0
  277. # endif
  278. #endif
  279. FFI_API void *ffi_closure_alloc (size_t size, void **code);
  280. FFI_API void ffi_closure_free (void *);
  281. FFI_API ffi_status
  282. ffi_prep_closure (ffi_closure*,
  283. ffi_cif *,
  284. void (*fun)(ffi_cif*,void*,void**,void*),
  285. void *user_data)
  286. #if defined(__GNUC__) && (((__GNUC__ * 100) + __GNUC_MINOR__) >= 405)
  287. __attribute__((deprecated ("use ffi_prep_closure_loc instead")))
  288. #elif defined(__GNUC__) && __GNUC__ >= 3
  289. __attribute__((deprecated))
  290. #endif
  291. ;
  292. FFI_API ffi_status
  293. ffi_prep_closure_loc (ffi_closure*,
  294. ffi_cif *,
  295. void (*fun)(ffi_cif*,void*,void**,void*),
  296. void *user_data,
  297. void*codeloc);
  298. #ifdef __sgi
  299. # pragma pack 8
  300. #endif
  301. typedef struct {
  302. #if 1
  303. void *trampoline_table;
  304. void *trampoline_table_entry;
  305. #else
  306. char tramp[FFI_TRAMPOLINE_SIZE];
  307. #endif
  308. ffi_cif *cif;
  309. #if !FFI_NATIVE_RAW_API
  310. /* If this is enabled, then a raw closure has the same layout
  311. as a regular closure. We use this to install an intermediate
  312. handler to do the transaltion, void** -> ffi_raw*. */
  313. void (*translate_args)(ffi_cif*,void*,void**,void*);
  314. void *this_closure;
  315. #endif
  316. void (*fun)(ffi_cif*,void*,ffi_raw*,void*);
  317. void *user_data;
  318. } ffi_raw_closure;
  319. typedef struct {
  320. #if 1
  321. void *trampoline_table;
  322. void *trampoline_table_entry;
  323. #else
  324. char tramp[FFI_TRAMPOLINE_SIZE];
  325. #endif
  326. ffi_cif *cif;
  327. #if !FFI_NATIVE_RAW_API
  328. /* If this is enabled, then a raw closure has the same layout
  329. as a regular closure. We use this to install an intermediate
  330. handler to do the translation, void** -> ffi_raw*. */
  331. void (*translate_args)(ffi_cif*,void*,void**,void*);
  332. void *this_closure;
  333. #endif
  334. void (*fun)(ffi_cif*,void*,ffi_java_raw*,void*);
  335. void *user_data;
  336. } ffi_java_raw_closure;
  337. FFI_API ffi_status
  338. ffi_prep_raw_closure (ffi_raw_closure*,
  339. ffi_cif *cif,
  340. void (*fun)(ffi_cif*,void*,ffi_raw*,void*),
  341. void *user_data);
  342. FFI_API ffi_status
  343. ffi_prep_raw_closure_loc (ffi_raw_closure*,
  344. ffi_cif *cif,
  345. void (*fun)(ffi_cif*,void*,ffi_raw*,void*),
  346. void *user_data,
  347. void *codeloc);
  348. #if !FFI_NATIVE_RAW_API
  349. FFI_API ffi_status
  350. ffi_prep_java_raw_closure (ffi_java_raw_closure*,
  351. ffi_cif *cif,
  352. void (*fun)(ffi_cif*,void*,ffi_java_raw*,void*),
  353. void *user_data) __attribute__((deprecated));
  354. FFI_API ffi_status
  355. ffi_prep_java_raw_closure_loc (ffi_java_raw_closure*,
  356. ffi_cif *cif,
  357. void (*fun)(ffi_cif*,void*,ffi_java_raw*,void*),
  358. void *user_data,
  359. void *codeloc) __attribute__((deprecated));
  360. #endif
  361. #endif /* FFI_CLOSURES */
  362. #if FFI_GO_CLOSURES
  363. typedef struct {
  364. void *tramp;
  365. ffi_cif *cif;
  366. void (*fun)(ffi_cif*,void*,void**,void*);
  367. } ffi_go_closure;
  368. FFI_API ffi_status ffi_prep_go_closure (ffi_go_closure*, ffi_cif *,
  369. void (*fun)(ffi_cif*,void*,void**,void*));
  370. FFI_API void ffi_call_go (ffi_cif *cif, void (*fn)(void), void *rvalue,
  371. void **avalue, void *closure);
  372. #endif /* FFI_GO_CLOSURES */
  373. /* ---- Public interface definition -------------------------------------- */
  374. FFI_API
  375. ffi_status ffi_prep_cif(ffi_cif *cif,
  376. ffi_abi abi,
  377. unsigned int nargs,
  378. ffi_type *rtype,
  379. ffi_type **atypes);
  380. FFI_API
  381. ffi_status ffi_prep_cif_var(ffi_cif *cif,
  382. ffi_abi abi,
  383. unsigned int nfixedargs,
  384. unsigned int ntotalargs,
  385. ffi_type *rtype,
  386. ffi_type **atypes);
  387. FFI_API
  388. void ffi_call(ffi_cif *cif,
  389. void (*fn)(void),
  390. void *rvalue,
  391. void **avalue);
  392. FFI_API
  393. ffi_status ffi_get_struct_offsets (ffi_abi abi, ffi_type *struct_type,
  394. size_t *offsets);
  395. /* Useful for eliminating compiler warnings. */
  396. #define FFI_FN(f) ((void (*)(void))f)
  397. /* ---- Definitions shared with assembly code ---------------------------- */
  398. #endif
  399. /* If these change, update src/mips/ffitarget.h. */
  400. #define FFI_TYPE_VOID 0
  401. #define FFI_TYPE_INT 1
  402. #define FFI_TYPE_FLOAT 2
  403. #define FFI_TYPE_DOUBLE 3
  404. #if 0
  405. #define FFI_TYPE_LONGDOUBLE 4
  406. #else
  407. #define FFI_TYPE_LONGDOUBLE FFI_TYPE_DOUBLE
  408. #endif
  409. #define FFI_TYPE_UINT8 5
  410. #define FFI_TYPE_SINT8 6
  411. #define FFI_TYPE_UINT16 7
  412. #define FFI_TYPE_SINT16 8
  413. #define FFI_TYPE_UINT32 9
  414. #define FFI_TYPE_SINT32 10
  415. #define FFI_TYPE_UINT64 11
  416. #define FFI_TYPE_SINT64 12
  417. #define FFI_TYPE_STRUCT 13
  418. #define FFI_TYPE_POINTER 14
  419. #define FFI_TYPE_COMPLEX 15
  420. /* This should always refer to the last type code (for sanity checks). */
  421. #define FFI_TYPE_LAST FFI_TYPE_COMPLEX
  422. #ifdef __cplusplus
  423. }
  424. #endif
  425. #endif