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