| 1 |
/* ----------------------------------------------------------------------- |
| 2 |
prep_cif.c - Copyright (c) 1996, 1998 Red Hat, Inc. |
| 3 |
|
| 4 |
Permission is hereby granted, free of charge, to any person obtaining |
| 5 |
a copy of this software and associated documentation files (the |
| 6 |
``Software''), to deal in the Software without restriction, including |
| 7 |
without limitation the rights to use, copy, modify, merge, publish, |
| 8 |
distribute, sublicense, and/or sell copies of the Software, and to |
| 9 |
permit persons to whom the Software is furnished to do so, subject to |
| 10 |
the following conditions: |
| 11 |
|
| 12 |
The above copyright notice and this permission notice shall be included |
| 13 |
in all copies or substantial portions of the Software. |
| 14 |
|
| 15 |
THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 16 |
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 17 |
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 18 |
IN NO EVENT SHALL CYGNUS SOLUTIONS BE LIABLE FOR ANY CLAIM, DAMAGES OR |
| 19 |
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
| 20 |
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 21 |
OTHER DEALINGS IN THE SOFTWARE. |
| 22 |
----------------------------------------------------------------------- */ |
| 23 |
|
| 24 |
#include <ffi.h> |
| 25 |
#include <ffi_common.h> |
| 26 |
#include <stdlib.h> |
| 27 |
|
| 28 |
|
| 29 |
/* Round up to FFI_SIZEOF_ARG. */ |
| 30 |
|
| 31 |
#define STACK_ARG_SIZE(x) ALIGN(x, FFI_SIZEOF_ARG) |
| 32 |
|
| 33 |
/* Perform machine independent initialization of aggregate type |
| 34 |
specifications. */ |
| 35 |
|
| 36 |
static ffi_status initialize_aggregate(/*@out@*/ ffi_type *arg) |
| 37 |
{ |
| 38 |
ffi_type **ptr; |
| 39 |
|
| 40 |
FFI_ASSERT(arg != NULL); |
| 41 |
|
| 42 |
/*@-usedef@*/ |
| 43 |
|
| 44 |
FFI_ASSERT(arg->elements != NULL); |
| 45 |
FFI_ASSERT(arg->size == 0); |
| 46 |
FFI_ASSERT(arg->alignment == 0); |
| 47 |
|
| 48 |
ptr = &(arg->elements[0]); |
| 49 |
|
| 50 |
while ((*ptr) != NULL) |
| 51 |
{ |
| 52 |
if (((*ptr)->size == 0) && (initialize_aggregate((*ptr)) != FFI_OK)) |
| 53 |
return FFI_BAD_TYPEDEF; |
| 54 |
|
| 55 |
/* Perform a sanity check on the argument type */ |
| 56 |
FFI_ASSERT_VALID_TYPE(*ptr); |
| 57 |
|
| 58 |
arg->size = ALIGN(arg->size, (*ptr)->alignment); |
| 59 |
arg->size += (*ptr)->size; |
| 60 |
|
| 61 |
arg->alignment = (arg->alignment > (*ptr)->alignment) ? |
| 62 |
arg->alignment : (*ptr)->alignment; |
| 63 |
|
| 64 |
ptr++; |
| 65 |
} |
| 66 |
|
| 67 |
/* Structure size includes tail padding. This is important for |
| 68 |
structures that fit in one register on ABIs like the PowerPC64 |
| 69 |
Linux ABI that right justify small structs in a register. |
| 70 |
It's also needed for nested structure layout, for example |
| 71 |
struct A { long a; char b; }; struct B { struct A x; char y; }; |
| 72 |
should find y at an offset of 2*sizeof(long) and result in a |
| 73 |
total size of 3*sizeof(long). */ |
| 74 |
arg->size = ALIGN (arg->size, arg->alignment); |
| 75 |
|
| 76 |
if (arg->size == 0) |
| 77 |
return FFI_BAD_TYPEDEF; |
| 78 |
else |
| 79 |
return FFI_OK; |
| 80 |
|
| 81 |
/*@=usedef@*/ |
| 82 |
} |
| 83 |
|
| 84 |
/* Perform machine independent ffi_cif preparation, then call |
| 85 |
machine dependent routine. */ |
| 86 |
|
| 87 |
ffi_status ffi_prep_cif(/*@out@*/ /*@partial@*/ ffi_cif *cif, |
| 88 |
ffi_abi abi, unsigned int nargs, |
| 89 |
/*@dependent@*/ /*@out@*/ /*@partial@*/ ffi_type *rtype, |
| 90 |
/*@dependent@*/ ffi_type **atypes) |
| 91 |
{ |
| 92 |
unsigned bytes = 0; |
| 93 |
unsigned int i; |
| 94 |
ffi_type **ptr; |
| 95 |
|
| 96 |
FFI_ASSERT(cif != NULL); |
| 97 |
FFI_ASSERT((abi > FFI_FIRST_ABI) && (abi <= FFI_DEFAULT_ABI)); |
| 98 |
|
| 99 |
cif->abi = abi; |
| 100 |
cif->arg_types = atypes; |
| 101 |
cif->nargs = nargs; |
| 102 |
cif->rtype = rtype; |
| 103 |
|
| 104 |
cif->flags = 0; |
| 105 |
|
| 106 |
/* Initialize the return type if necessary */ |
| 107 |
/*@-usedef@*/ |
| 108 |
if ((cif->rtype->size == 0) && (initialize_aggregate(cif->rtype) != FFI_OK)) |
| 109 |
return FFI_BAD_TYPEDEF; |
| 110 |
/*@=usedef@*/ |
| 111 |
|
| 112 |
/* Perform a sanity check on the return type */ |
| 113 |
FFI_ASSERT_VALID_TYPE(cif->rtype); |
| 114 |
|
| 115 |
/* x86-64 and s390 stack space allocation is handled in prep_machdep. */ |
| 116 |
#if !defined M68K && !defined __x86_64__ && !defined S390 |
| 117 |
/* Make space for the return structure pointer */ |
| 118 |
if (cif->rtype->type == FFI_TYPE_STRUCT |
| 119 |
#ifdef _WIN32 |
| 120 |
&& (cif->rtype->size != 1) /* MSVC returns small structs in registers */ |
| 121 |
&& (cif->rtype->size != 2) |
| 122 |
&& (cif->rtype->size != 4) |
| 123 |
&& (cif->rtype->size != 8) |
| 124 |
#endif |
| 125 |
#ifdef SPARC |
| 126 |
&& (cif->abi != FFI_V9 || cif->rtype->size > 32) |
| 127 |
#endif |
| 128 |
) |
| 129 |
bytes = STACK_ARG_SIZE(sizeof(void*)); |
| 130 |
#endif |
| 131 |
|
| 132 |
for (ptr = cif->arg_types, i = cif->nargs; i > 0; i--, ptr++) |
| 133 |
{ |
| 134 |
|
| 135 |
/* Initialize any uninitialized aggregate type definitions */ |
| 136 |
if (((*ptr)->size == 0) && (initialize_aggregate((*ptr)) != FFI_OK)) |
| 137 |
return FFI_BAD_TYPEDEF; |
| 138 |
|
| 139 |
/* Perform a sanity check on the argument type, do this |
| 140 |
check after the initialization. */ |
| 141 |
FFI_ASSERT_VALID_TYPE(*ptr); |
| 142 |
|
| 143 |
#if !defined __x86_64__ && !defined S390 |
| 144 |
#ifdef SPARC |
| 145 |
if (((*ptr)->type == FFI_TYPE_STRUCT |
| 146 |
&& ((*ptr)->size > 16 || cif->abi != FFI_V9)) |
| 147 |
|| ((*ptr)->type == FFI_TYPE_LONGDOUBLE |
| 148 |
&& cif->abi != FFI_V9)) |
| 149 |
bytes += sizeof(void*); |
| 150 |
else |
| 151 |
#endif |
| 152 |
{ |
| 153 |
#if !defined(_MSC_VER) && !defined(__MINGW32__) |
| 154 |
/* Don't know if this is a libffi bug or not. At least on |
| 155 |
Windows with MSVC, function call parameters are *not* |
| 156 |
aligned in the same way as structure fields are, they are |
| 157 |
only aligned in integer boundaries. |
| 158 |
|
| 159 |
This doesn't do any harm for cdecl functions and closures, |
| 160 |
since the caller cleans up the stack, but it is wrong for |
| 161 |
stdcall functions where the callee cleans. |
| 162 |
*/ |
| 163 |
|
| 164 |
/* Add any padding if necessary */ |
| 165 |
if (((*ptr)->alignment - 1) & bytes) |
| 166 |
bytes = ALIGN(bytes, (*ptr)->alignment); |
| 167 |
|
| 168 |
#endif |
| 169 |
bytes += STACK_ARG_SIZE((*ptr)->size); |
| 170 |
} |
| 171 |
#endif |
| 172 |
} |
| 173 |
|
| 174 |
#ifdef _WIN64 |
| 175 |
/* Function call needs at least 40 bytes stack size, on win64 AMD64 */ |
| 176 |
if (bytes < 40) |
| 177 |
bytes = 40; |
| 178 |
#endif |
| 179 |
|
| 180 |
cif->bytes = bytes; |
| 181 |
|
| 182 |
/* Perform machine dependent cif processing */ |
| 183 |
return ffi_prep_cif_machdep(cif); |
| 184 |
} |