ffi.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. /* -----------------------------------------------------------------*-C-*-
  2. libffi 3.4.6
  3. - Copyright (c) 2011, 2014, 2019, 2021, 2022, 2024 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 && !defined(FFI_STATIC_BUILD)
  118. # if defined FFI_BUILDING_DLL /* Building libffi.DLL with msvcc.sh */
  119. # define FFI_API __declspec(dllexport)
  120. # else /* Importing libffi.DLL */
  121. # define FFI_API __declspec(dllimport)
  122. # endif
  123. #else
  124. # define FFI_API
  125. #endif
  126. /* The externally visible type declarations also need the MSVC DLL
  127. decorations, or they will not be exported from the object file. */
  128. #if defined LIBFFI_HIDE_BASIC_TYPES
  129. # define FFI_EXTERN FFI_API
  130. #else
  131. # define FFI_EXTERN extern FFI_API
  132. #endif
  133. #ifndef LIBFFI_HIDE_BASIC_TYPES
  134. #if SCHAR_MAX == 127
  135. # define ffi_type_uchar ffi_type_uint8
  136. # define ffi_type_schar ffi_type_sint8
  137. #else
  138. #error "char size not supported"
  139. #endif
  140. #if SHRT_MAX == 32767
  141. # define ffi_type_ushort ffi_type_uint16
  142. # define ffi_type_sshort ffi_type_sint16
  143. #elif SHRT_MAX == 2147483647
  144. # define ffi_type_ushort ffi_type_uint32
  145. # define ffi_type_sshort ffi_type_sint32
  146. #else
  147. #error "short size not supported"
  148. #endif
  149. #if INT_MAX == 32767
  150. # define ffi_type_uint ffi_type_uint16
  151. # define ffi_type_sint ffi_type_sint16
  152. #elif INT_MAX == 2147483647
  153. # define ffi_type_uint ffi_type_uint32
  154. # define ffi_type_sint ffi_type_sint32
  155. #elif INT_MAX == 9223372036854775807
  156. # define ffi_type_uint ffi_type_uint64
  157. # define ffi_type_sint ffi_type_sint64
  158. #else
  159. #error "int size not supported"
  160. #endif
  161. #if LONG_MAX == 2147483647
  162. # if FFI_LONG_LONG_MAX != FFI_64_BIT_MAX
  163. #error "no 64-bit data type supported"
  164. # endif
  165. #elif LONG_MAX != FFI_64_BIT_MAX
  166. #error "long size not supported"
  167. #endif
  168. #if LONG_MAX == 2147483647
  169. # define ffi_type_ulong ffi_type_uint32
  170. # define ffi_type_slong ffi_type_sint32
  171. #elif LONG_MAX == FFI_64_BIT_MAX
  172. # define ffi_type_ulong ffi_type_uint64
  173. # define ffi_type_slong ffi_type_sint64
  174. #else
  175. #error "long size not supported"
  176. #endif
  177. /* These are defined in types.c. */
  178. FFI_EXTERN ffi_type ffi_type_void;
  179. FFI_EXTERN ffi_type ffi_type_uint8;
  180. FFI_EXTERN ffi_type ffi_type_sint8;
  181. FFI_EXTERN ffi_type ffi_type_uint16;
  182. FFI_EXTERN ffi_type ffi_type_sint16;
  183. FFI_EXTERN ffi_type ffi_type_uint32;
  184. FFI_EXTERN ffi_type ffi_type_sint32;
  185. FFI_EXTERN ffi_type ffi_type_uint64;
  186. FFI_EXTERN ffi_type ffi_type_sint64;
  187. FFI_EXTERN ffi_type ffi_type_float;
  188. FFI_EXTERN ffi_type ffi_type_double;
  189. FFI_EXTERN ffi_type ffi_type_pointer;
  190. FFI_EXTERN ffi_type ffi_type_longdouble;
  191. #ifdef FFI_TARGET_HAS_COMPLEX_TYPE
  192. FFI_EXTERN ffi_type ffi_type_complex_float;
  193. FFI_EXTERN ffi_type ffi_type_complex_double;
  194. FFI_EXTERN ffi_type ffi_type_complex_longdouble;
  195. #endif
  196. #endif /* LIBFFI_HIDE_BASIC_TYPES */
  197. typedef enum {
  198. FFI_OK = 0,
  199. FFI_BAD_TYPEDEF,
  200. FFI_BAD_ABI,
  201. FFI_BAD_ARGTYPE
  202. } ffi_status;
  203. typedef struct {
  204. ffi_abi abi;
  205. unsigned nargs;
  206. ffi_type **arg_types;
  207. ffi_type *rtype;
  208. unsigned bytes;
  209. unsigned flags;
  210. #ifdef FFI_EXTRA_CIF_FIELDS
  211. FFI_EXTRA_CIF_FIELDS;
  212. #endif
  213. } ffi_cif;
  214. /* ---- Definitions for the raw API -------------------------------------- */
  215. #ifndef FFI_SIZEOF_ARG
  216. # if LONG_MAX == 2147483647
  217. # define FFI_SIZEOF_ARG 4
  218. # elif LONG_MAX == FFI_64_BIT_MAX
  219. # define FFI_SIZEOF_ARG 8
  220. # endif
  221. #endif
  222. #ifndef FFI_SIZEOF_JAVA_RAW
  223. # define FFI_SIZEOF_JAVA_RAW FFI_SIZEOF_ARG
  224. #endif
  225. typedef union {
  226. ffi_sarg sint;
  227. ffi_arg uint;
  228. float flt;
  229. char data[FFI_SIZEOF_ARG];
  230. void* ptr;
  231. } ffi_raw;
  232. #if FFI_SIZEOF_JAVA_RAW == 4 && FFI_SIZEOF_ARG == 8
  233. /* This is a special case for mips64/n32 ABI (and perhaps others) where
  234. sizeof(void *) is 4 and FFI_SIZEOF_ARG is 8. */
  235. typedef union {
  236. signed int sint;
  237. unsigned int uint;
  238. float flt;
  239. char data[FFI_SIZEOF_JAVA_RAW];
  240. void* ptr;
  241. } ffi_java_raw;
  242. #else
  243. typedef ffi_raw ffi_java_raw;
  244. #endif
  245. FFI_API
  246. void ffi_raw_call (ffi_cif *cif,
  247. void (*fn)(void),
  248. void *rvalue,
  249. ffi_raw *avalue);
  250. FFI_API void ffi_ptrarray_to_raw (ffi_cif *cif, void **args, ffi_raw *raw);
  251. FFI_API void ffi_raw_to_ptrarray (ffi_cif *cif, ffi_raw *raw, void **args);
  252. FFI_API size_t ffi_raw_size (ffi_cif *cif);
  253. /* This is analogous to the raw API, except it uses Java parameter
  254. packing, even on 64-bit machines. I.e. on 64-bit machines longs
  255. and doubles are followed by an empty 64-bit word. */
  256. #if !FFI_NATIVE_RAW_API
  257. FFI_API
  258. void ffi_java_raw_call (ffi_cif *cif,
  259. void (*fn)(void),
  260. void *rvalue,
  261. ffi_java_raw *avalue) __attribute__((deprecated));
  262. #endif
  263. FFI_API
  264. void ffi_java_ptrarray_to_raw (ffi_cif *cif, void **args, ffi_java_raw *raw) __attribute__((deprecated));
  265. FFI_API
  266. void ffi_java_raw_to_ptrarray (ffi_cif *cif, ffi_java_raw *raw, void **args) __attribute__((deprecated));
  267. FFI_API
  268. size_t ffi_java_raw_size (ffi_cif *cif) __attribute__((deprecated));
  269. /* ---- Definitions for closures ----------------------------------------- */
  270. #if FFI_CLOSURES
  271. #ifdef _MSC_VER
  272. __declspec(align(8))
  273. #endif
  274. typedef struct {
  275. #if 0
  276. void *trampoline_table;
  277. void *trampoline_table_entry;
  278. #else
  279. union {
  280. char tramp[FFI_TRAMPOLINE_SIZE];
  281. void *ftramp;
  282. };
  283. #endif
  284. ffi_cif *cif;
  285. void (*fun)(ffi_cif*,void*,void**,void*);
  286. void *user_data;
  287. #if defined(_MSC_VER) && defined(_M_IX86)
  288. void *padding;
  289. #endif
  290. } ffi_closure
  291. #ifdef __GNUC__
  292. __attribute__((aligned (8)))
  293. #endif
  294. ;
  295. #ifndef __GNUC__
  296. # ifdef __sgi
  297. # pragma pack 0
  298. # endif
  299. #endif
  300. FFI_API void *ffi_closure_alloc (size_t size, void **code);
  301. FFI_API void ffi_closure_free (void *);
  302. FFI_API ffi_status
  303. ffi_prep_closure (ffi_closure*,
  304. ffi_cif *,
  305. void (*fun)(ffi_cif*,void*,void**,void*),
  306. void *user_data)
  307. #if defined(__GNUC__) && (((__GNUC__ * 100) + __GNUC_MINOR__) >= 405)
  308. __attribute__((deprecated ("use ffi_prep_closure_loc instead")))
  309. #elif defined(__GNUC__) && __GNUC__ >= 3
  310. __attribute__((deprecated))
  311. #endif
  312. ;
  313. FFI_API ffi_status
  314. ffi_prep_closure_loc (ffi_closure*,
  315. ffi_cif *,
  316. void (*fun)(ffi_cif*,void*,void**,void*),
  317. void *user_data,
  318. void *codeloc);
  319. #ifdef __sgi
  320. # pragma pack 8
  321. #endif
  322. typedef struct {
  323. #if 0
  324. void *trampoline_table;
  325. void *trampoline_table_entry;
  326. #else
  327. char tramp[FFI_TRAMPOLINE_SIZE];
  328. #endif
  329. ffi_cif *cif;
  330. #if !FFI_NATIVE_RAW_API
  331. /* If this is enabled, then a raw closure has the same layout
  332. as a regular closure. We use this to install an intermediate
  333. handler to do the translation, void** -> ffi_raw*. */
  334. void (*translate_args)(ffi_cif*,void*,void**,void*);
  335. void *this_closure;
  336. #endif
  337. void (*fun)(ffi_cif*,void*,ffi_raw*,void*);
  338. void *user_data;
  339. } ffi_raw_closure;
  340. typedef struct {
  341. #if 0
  342. void *trampoline_table;
  343. void *trampoline_table_entry;
  344. #else
  345. char tramp[FFI_TRAMPOLINE_SIZE];
  346. #endif
  347. ffi_cif *cif;
  348. #if !FFI_NATIVE_RAW_API
  349. /* If this is enabled, then a raw closure has the same layout
  350. as a regular closure. We use this to install an intermediate
  351. handler to do the translation, void** -> ffi_raw*. */
  352. void (*translate_args)(ffi_cif*,void*,void**,void*);
  353. void *this_closure;
  354. #endif
  355. void (*fun)(ffi_cif*,void*,ffi_java_raw*,void*);
  356. void *user_data;
  357. } ffi_java_raw_closure;
  358. FFI_API ffi_status
  359. ffi_prep_raw_closure (ffi_raw_closure*,
  360. ffi_cif *cif,
  361. void (*fun)(ffi_cif*,void*,ffi_raw*,void*),
  362. void *user_data);
  363. FFI_API ffi_status
  364. ffi_prep_raw_closure_loc (ffi_raw_closure*,
  365. ffi_cif *cif,
  366. void (*fun)(ffi_cif*,void*,ffi_raw*,void*),
  367. void *user_data,
  368. void *codeloc);
  369. #if !FFI_NATIVE_RAW_API
  370. FFI_API ffi_status
  371. ffi_prep_java_raw_closure (ffi_java_raw_closure*,
  372. ffi_cif *cif,
  373. void (*fun)(ffi_cif*,void*,ffi_java_raw*,void*),
  374. void *user_data) __attribute__((deprecated));
  375. FFI_API ffi_status
  376. ffi_prep_java_raw_closure_loc (ffi_java_raw_closure*,
  377. ffi_cif *cif,
  378. void (*fun)(ffi_cif*,void*,ffi_java_raw*,void*),
  379. void *user_data,
  380. void *codeloc) __attribute__((deprecated));
  381. #endif
  382. #endif /* FFI_CLOSURES */
  383. #ifdef FFI_GO_CLOSURES
  384. typedef struct {
  385. void *tramp;
  386. ffi_cif *cif;
  387. void (*fun)(ffi_cif*,void*,void**,void*);
  388. } ffi_go_closure;
  389. FFI_API ffi_status ffi_prep_go_closure (ffi_go_closure*, ffi_cif *,
  390. void (*fun)(ffi_cif*,void*,void**,void*));
  391. FFI_API void ffi_call_go (ffi_cif *cif, void (*fn)(void), void *rvalue,
  392. void **avalue, void *closure);
  393. #endif /* FFI_GO_CLOSURES */
  394. /* ---- Public interface definition -------------------------------------- */
  395. FFI_API
  396. ffi_status ffi_prep_cif(ffi_cif *cif,
  397. ffi_abi abi,
  398. unsigned int nargs,
  399. ffi_type *rtype,
  400. ffi_type **atypes);
  401. FFI_API
  402. ffi_status ffi_prep_cif_var(ffi_cif *cif,
  403. ffi_abi abi,
  404. unsigned int nfixedargs,
  405. unsigned int ntotalargs,
  406. ffi_type *rtype,
  407. ffi_type **atypes);
  408. FFI_API
  409. void ffi_call(ffi_cif *cif,
  410. void (*fn)(void),
  411. void *rvalue,
  412. void **avalue);
  413. FFI_API
  414. ffi_status ffi_get_struct_offsets (ffi_abi abi, ffi_type *struct_type,
  415. size_t *offsets);
  416. /* Convert between closure and function pointers. */
  417. #if defined(PA_LINUX) || defined(PA_HPUX)
  418. #define FFI_FN(f) ((void (*)(void))((unsigned int)(f) | 2))
  419. #define FFI_CL(f) ((void *)((unsigned int)(f) & ~3))
  420. #else
  421. #define FFI_FN(f) ((void (*)(void))f)
  422. #define FFI_CL(f) ((void *)(f))
  423. #endif
  424. /* ---- Definitions shared with assembly code ---------------------------- */
  425. #endif
  426. #ifdef __cplusplus
  427. }
  428. #endif
  429. #endif