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