1 /* -----------------------------------------------------------------*-C-*-
3 - Copyright (c) 2011, 2014, 2019, 2021 Anthony Green
4 - Copyright (c) 1996-2003, 2007, 2008 Red Hat, Inc.
6 Permission is hereby granted, free of charge, to any person
7 obtaining a copy of this software and associated documentation
8 files (the ``Software''), to deal in the Software without
9 restriction, including without limitation the rights to use, copy,
10 modify, merge, publish, distribute, sublicense, and/or sell copies
11 of the Software, and to permit persons to whom the Software is
12 furnished to do so, subject to the following conditions:
14 The above copyright notice and this permission notice shall be
15 included in all copies or substantial portions of the Software.
17 THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND,
18 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
21 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
22 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 DEALINGS IN THE SOFTWARE.
26 ----------------------------------------------------------------------- */
28 /* -------------------------------------------------------------------
29 Most of the API is documented in doc/libffi.texi.
31 The raw API is designed to bypass some of the argument packing and
32 unpacking on architectures for which it can be avoided. Routines
33 are provided to emulate the raw API if the underlying platform
34 doesn't allow faster implementation.
36 More details on the raw API can be found in:
38 http://gcc.gnu.org/ml/java/1999-q3/msg00138.html
42 http://gcc.gnu.org/ml/java/1999-q3/msg00174.html
43 -------------------------------------------------------------------- */
52 /* Specify which architecture libffi is configured for. */
57 /* ---- System configuration information --------------------------------- */
59 #include <ffitarget.h>
63 #if defined(_MSC_VER) && !defined(__clang__)
64 #define __attribute__(X)
70 /* LONG_LONG_MAX is not always defined (not if STRICT_ANSI, for example).
71 But we can find it either under the correct ANSI name, or under GNU
74 #define FFI_64_BIT_MAX 9223372036854775807
77 # define FFI_LONG_LONG_MAX LONG_LONG_MAX
80 # define FFI_LONG_LONG_MAX LLONG_MAX
81 # ifdef _AIX52 /* or newer has C99 LLONG_MAX */
82 # undef FFI_64_BIT_MAX
83 # define FFI_64_BIT_MAX 9223372036854775807LL
84 # endif /* _AIX52 or newer */
87 # define FFI_LONG_LONG_MAX __LONG_LONG_MAX__
89 # ifdef _AIX /* AIX 5.1 and earlier have LONGLONG_MAX */
91 # if defined (__IBMC__) || defined (__IBMCPP__)
92 # define FFI_LONG_LONG_MAX LONGLONG_MAX
94 # endif /* __PPC64__ */
95 # undef FFI_64_BIT_MAX
96 # define FFI_64_BIT_MAX 9223372036854775807LL
101 /* The closure code assumes that this works on pointers, i.e. a size_t
102 can hold a pointer. */
104 typedef struct _ffi_type
107 unsigned short alignment
;
109 struct _ffi_type
**elements
;
112 /* Need minimal decorations for DLLs to work on Windows. GCC has
113 autoimport and autoexport. Always mark externally visible symbols
114 as dllimport for MSVC clients, even if it means an extra indirection
115 when using the static version of the library.
116 Besides, as a workaround, they can define FFI_BUILDING if they
117 *know* they are going to link with the static library. */
119 # if defined FFI_BUILDING_DLL /* Building libffi.DLL with msvcc.sh */
120 # define FFI_API __declspec(dllexport)
121 # elif !defined FFI_BUILDING /* Importing libffi.DLL */
122 # define FFI_API __declspec(dllimport)
123 # else /* Building/linking static library */
130 /* The externally visible type declarations also need the MSVC DLL
131 decorations, or they will not be exported from the object file. */
132 #if defined LIBFFI_HIDE_BASIC_TYPES
133 # define FFI_EXTERN FFI_API
135 # define FFI_EXTERN extern FFI_API
138 #ifndef LIBFFI_HIDE_BASIC_TYPES
140 # define ffi_type_uchar ffi_type_uint8
141 # define ffi_type_schar ffi_type_sint8
143 #error "char size not supported"
146 #if SHRT_MAX == 32767
147 # define ffi_type_ushort ffi_type_uint16
148 # define ffi_type_sshort ffi_type_sint16
149 #elif SHRT_MAX == 2147483647
150 # define ffi_type_ushort ffi_type_uint32
151 # define ffi_type_sshort ffi_type_sint32
153 #error "short size not supported"
157 # define ffi_type_uint ffi_type_uint16
158 # define ffi_type_sint ffi_type_sint16
159 #elif INT_MAX == 2147483647
160 # define ffi_type_uint ffi_type_uint32
161 # define ffi_type_sint ffi_type_sint32
162 #elif INT_MAX == 9223372036854775807
163 # define ffi_type_uint ffi_type_uint64
164 # define ffi_type_sint ffi_type_sint64
166 #error "int size not supported"
169 #if LONG_MAX == 2147483647
170 # if FFI_LONG_LONG_MAX != FFI_64_BIT_MAX
171 #error "no 64-bit data type supported"
173 #elif LONG_MAX != FFI_64_BIT_MAX
174 #error "long size not supported"
177 #if LONG_MAX == 2147483647
178 # define ffi_type_ulong ffi_type_uint32
179 # define ffi_type_slong ffi_type_sint32
180 #elif LONG_MAX == FFI_64_BIT_MAX
181 # define ffi_type_ulong ffi_type_uint64
182 # define ffi_type_slong ffi_type_sint64
184 #error "long size not supported"
187 /* These are defined in types.c. */
188 FFI_EXTERN ffi_type ffi_type_void
;
189 FFI_EXTERN ffi_type ffi_type_uint8
;
190 FFI_EXTERN ffi_type ffi_type_sint8
;
191 FFI_EXTERN ffi_type ffi_type_uint16
;
192 FFI_EXTERN ffi_type ffi_type_sint16
;
193 FFI_EXTERN ffi_type ffi_type_uint32
;
194 FFI_EXTERN ffi_type ffi_type_sint32
;
195 FFI_EXTERN ffi_type ffi_type_uint64
;
196 FFI_EXTERN ffi_type ffi_type_sint64
;
197 FFI_EXTERN ffi_type ffi_type_float
;
198 FFI_EXTERN ffi_type ffi_type_double
;
199 FFI_EXTERN ffi_type ffi_type_pointer
;
201 #if @HAVE_LONG_DOUBLE@
202 FFI_EXTERN ffi_type ffi_type_longdouble
;
204 #define ffi_type_longdouble ffi_type_double
207 #ifdef FFI_TARGET_HAS_COMPLEX_TYPE
208 FFI_EXTERN ffi_type ffi_type_complex_float
;
209 FFI_EXTERN ffi_type ffi_type_complex_double
;
210 #if @HAVE_LONG_DOUBLE@
211 FFI_EXTERN ffi_type ffi_type_complex_longdouble
;
213 #define ffi_type_complex_longdouble ffi_type_complex_double
216 #endif /* LIBFFI_HIDE_BASIC_TYPES */
228 ffi_type
**arg_types
;
232 #ifdef FFI_EXTRA_CIF_FIELDS
233 FFI_EXTRA_CIF_FIELDS
;
237 /* ---- Definitions for the raw API -------------------------------------- */
239 #ifndef FFI_SIZEOF_ARG
240 # if LONG_MAX == 2147483647
241 # define FFI_SIZEOF_ARG 4
242 # elif LONG_MAX == FFI_64_BIT_MAX
243 # define FFI_SIZEOF_ARG 8
247 #ifndef FFI_SIZEOF_JAVA_RAW
248 # define FFI_SIZEOF_JAVA_RAW FFI_SIZEOF_ARG
255 char data
[FFI_SIZEOF_ARG
];
259 #if FFI_SIZEOF_JAVA_RAW == 4 && FFI_SIZEOF_ARG == 8
260 /* This is a special case for mips64/n32 ABI (and perhaps others) where
261 sizeof(void *) is 4 and FFI_SIZEOF_ARG is 8. */
266 char data
[FFI_SIZEOF_JAVA_RAW
];
270 typedef ffi_raw ffi_java_raw
;
275 void ffi_raw_call (ffi_cif
*cif
,
280 FFI_API
void ffi_ptrarray_to_raw (ffi_cif
*cif
, void **args
, ffi_raw
*raw
);
281 FFI_API
void ffi_raw_to_ptrarray (ffi_cif
*cif
, ffi_raw
*raw
, void **args
);
282 FFI_API
size_t ffi_raw_size (ffi_cif
*cif
);
284 /* This is analogous to the raw API, except it uses Java parameter
285 packing, even on 64-bit machines. I.e. on 64-bit machines longs
286 and doubles are followed by an empty 64-bit word. */
288 #if !FFI_NATIVE_RAW_API
290 void ffi_java_raw_call (ffi_cif
*cif
,
293 ffi_java_raw
*avalue
) __attribute__((deprecated
));
297 void ffi_java_ptrarray_to_raw (ffi_cif
*cif
, void **args
, ffi_java_raw
*raw
) __attribute__((deprecated
));
299 void ffi_java_raw_to_ptrarray (ffi_cif
*cif
, ffi_java_raw
*raw
, void **args
) __attribute__((deprecated
));
301 size_t ffi_java_raw_size (ffi_cif
*cif
) __attribute__((deprecated
));
303 /* ---- Definitions for closures ----------------------------------------- */
311 #if @FFI_EXEC_TRAMPOLINE_TABLE@
312 void *trampoline_table
;
313 void *trampoline_table_entry
;
316 char tramp
[FFI_TRAMPOLINE_SIZE
];
321 void (*fun
)(ffi_cif
*,void*,void**,void*);
325 __attribute__((aligned (8)))
335 FFI_API
void *ffi_closure_alloc (size_t size
, void **code
);
336 FFI_API
void ffi_closure_free (void *);
338 #if defined(PA_LINUX) || defined(PA_HPUX)
339 #define FFI_CLOSURE_PTR(X) ((void *)((unsigned int)(X) | 2))
340 #define FFI_RESTORE_PTR(X) ((void *)((unsigned int)(X) & ~3))
342 #define FFI_CLOSURE_PTR(X) (X)
343 #define FFI_RESTORE_PTR(X) (X)
347 ffi_prep_closure (ffi_closure
*,
349 void (*fun
)(ffi_cif
*,void*,void**,void*),
351 #if defined(__GNUC__) && (((__GNUC__ * 100) + __GNUC_MINOR__) >= 405)
352 __attribute__((deprecated ("use ffi_prep_closure_loc instead")))
353 #elif defined(__GNUC__) && __GNUC__ >= 3
354 __attribute__((deprecated
))
359 ffi_prep_closure_loc (ffi_closure
*,
361 void (*fun
)(ffi_cif
*,void*,void**,void*),
369 #if @FFI_EXEC_TRAMPOLINE_TABLE@
370 void *trampoline_table
;
371 void *trampoline_table_entry
;
373 char tramp
[FFI_TRAMPOLINE_SIZE
];
377 #if !FFI_NATIVE_RAW_API
379 /* If this is enabled, then a raw closure has the same layout
380 as a regular closure. We use this to install an intermediate
381 handler to do the transaltion, void** -> ffi_raw*. */
383 void (*translate_args
)(ffi_cif
*,void*,void**,void*);
388 void (*fun
)(ffi_cif
*,void*,ffi_raw
*,void*);
394 #if @FFI_EXEC_TRAMPOLINE_TABLE@
395 void *trampoline_table
;
396 void *trampoline_table_entry
;
398 char tramp
[FFI_TRAMPOLINE_SIZE
];
403 #if !FFI_NATIVE_RAW_API
405 /* If this is enabled, then a raw closure has the same layout
406 as a regular closure. We use this to install an intermediate
407 handler to do the translation, void** -> ffi_raw*. */
409 void (*translate_args
)(ffi_cif
*,void*,void**,void*);
414 void (*fun
)(ffi_cif
*,void*,ffi_java_raw
*,void*);
417 } ffi_java_raw_closure
;
420 ffi_prep_raw_closure (ffi_raw_closure
*,
422 void (*fun
)(ffi_cif
*,void*,ffi_raw
*,void*),
426 ffi_prep_raw_closure_loc (ffi_raw_closure
*,
428 void (*fun
)(ffi_cif
*,void*,ffi_raw
*,void*),
432 #if !FFI_NATIVE_RAW_API
434 ffi_prep_java_raw_closure (ffi_java_raw_closure
*,
436 void (*fun
)(ffi_cif
*,void*,ffi_java_raw
*,void*),
437 void *user_data
) __attribute__((deprecated
));
440 ffi_prep_java_raw_closure_loc (ffi_java_raw_closure
*,
442 void (*fun
)(ffi_cif
*,void*,ffi_java_raw
*,void*),
444 void *codeloc
) __attribute__((deprecated
));
447 #endif /* FFI_CLOSURES */
454 void (*fun
)(ffi_cif
*,void*,void**,void*);
457 FFI_API ffi_status
ffi_prep_go_closure (ffi_go_closure
*, ffi_cif
*,
458 void (*fun
)(ffi_cif
*,void*,void**,void*));
460 FFI_API
void ffi_call_go (ffi_cif
*cif
, void (*fn
)(void), void *rvalue
,
461 void **avalue
, void *closure
);
463 #endif /* FFI_GO_CLOSURES */
465 /* ---- Public interface definition -------------------------------------- */
468 ffi_status
ffi_prep_cif(ffi_cif
*cif
,
475 ffi_status
ffi_prep_cif_var(ffi_cif
*cif
,
477 unsigned int nfixedargs
,
478 unsigned int ntotalargs
,
483 void ffi_call(ffi_cif
*cif
,
489 ffi_status
ffi_get_struct_offsets (ffi_abi abi
, ffi_type
*struct_type
,
492 /* Useful for eliminating compiler warnings. */
493 #define FFI_FN(f) ((void (*)(void))f)
495 /* ---- Definitions shared with assembly code ---------------------------- */
499 /* If these change, update src/mips/ffitarget.h. */
500 #define FFI_TYPE_VOID 0
501 #define FFI_TYPE_INT 1
502 #define FFI_TYPE_FLOAT 2
503 #define FFI_TYPE_DOUBLE 3
504 #if @HAVE_LONG_DOUBLE@
505 #define FFI_TYPE_LONGDOUBLE 4
507 #define FFI_TYPE_LONGDOUBLE FFI_TYPE_DOUBLE
509 #define FFI_TYPE_UINT8 5
510 #define FFI_TYPE_SINT8 6
511 #define FFI_TYPE_UINT16 7
512 #define FFI_TYPE_SINT16 8
513 #define FFI_TYPE_UINT32 9
514 #define FFI_TYPE_SINT32 10
515 #define FFI_TYPE_UINT64 11
516 #define FFI_TYPE_SINT64 12
517 #define FFI_TYPE_STRUCT 13
518 #define FFI_TYPE_POINTER 14
519 #define FFI_TYPE_COMPLEX 15
521 /* This should always refer to the last type code (for sanity checks). */
522 #define FFI_TYPE_LAST FFI_TYPE_COMPLEX