ffi.h 14 KB

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