1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
|
#include <ffi.h>
#include <ffi_common.h>
#define FFI_TYPE_STRUCT_REGS FFI_TYPE_LAST
extern void ffi_call_SYSV(void *rvalue, unsigned rsize, unsigned flags,
void(*fn)(void), unsigned nbytes, extended_cif*);
extern void ffi_closure_SYSV(void) FFI_HIDDEN;
ffi_status ffi_prep_cif_machdep(ffi_cif *cif)
{
switch(cif->rtype->type) {
case FFI_TYPE_SINT8:
case FFI_TYPE_UINT8:
case FFI_TYPE_SINT16:
case FFI_TYPE_UINT16:
cif->flags = cif->rtype->type;
break;
case FFI_TYPE_VOID:
case FFI_TYPE_FLOAT:
cif->flags = FFI_TYPE_UINT32;
break;
case FFI_TYPE_DOUBLE:
case FFI_TYPE_UINT64:
case FFI_TYPE_SINT64:
cif->flags = FFI_TYPE_UINT64;
break;
case FFI_TYPE_STRUCT:
cif->flags = FFI_TYPE_STRUCT;
if (cif->rtype->size > 4 * 4) {
cif->flags = FFI_TYPE_STRUCT;
cif->bytes += 8;
}
break;
default:
cif->flags = FFI_TYPE_UINT32;
break;
}
cif->bytes = ALIGN(cif->bytes, 16);
return FFI_OK;
}
void ffi_prep_args(extended_cif *ecif, unsigned char* stack)
{
unsigned int i;
unsigned long *addr;
ffi_type **ptr;
union {
void **v;
char **c;
signed char **sc;
unsigned char **uc;
signed short **ss;
unsigned short **us;
unsigned int **i;
long long **ll;
float **f;
double **d;
} p_argv;
FFI_ASSERT (((unsigned long) stack & 0x7) == 0);
p_argv.v = ecif->avalue;
addr = (unsigned long*)stack;
if (ecif->cif->rtype->type == FFI_TYPE_STRUCT && ecif->cif->rtype->size > 16)
{
*addr++ = (unsigned long)ecif->rvalue;
}
for (i = ecif->cif->nargs, ptr = ecif->cif->arg_types;
i > 0;
i--, ptr++, p_argv.v++)
{
switch ((*ptr)->type)
{
case FFI_TYPE_SINT8:
*addr++ = **p_argv.sc;
break;
case FFI_TYPE_UINT8:
*addr++ = **p_argv.uc;
break;
case FFI_TYPE_SINT16:
*addr++ = **p_argv.ss;
break;
case FFI_TYPE_UINT16:
*addr++ = **p_argv.us;
break;
case FFI_TYPE_FLOAT:
case FFI_TYPE_INT:
case FFI_TYPE_UINT32:
case FFI_TYPE_SINT32:
case FFI_TYPE_POINTER:
*addr++ = **p_argv.i;
break;
case FFI_TYPE_DOUBLE:
case FFI_TYPE_UINT64:
case FFI_TYPE_SINT64:
if (((unsigned long)addr & 4) != 0)
addr++;
*(unsigned long long*)addr = **p_argv.ll;
addr += sizeof(unsigned long long) / sizeof (addr);
break;
case FFI_TYPE_STRUCT:
{
unsigned long offs;
unsigned long size;
if (((unsigned long)addr & 4) != 0 && (*ptr)->alignment > 4)
addr++;
offs = (unsigned long) addr - (unsigned long) stack;
size = (*ptr)->size;
if (offs < FFI_REGISTER_NARGS * 4
&& offs + size > FFI_REGISTER_NARGS * 4)
addr = (unsigned long*) (stack + FFI_REGISTER_NARGS * 4);
memcpy((char*) addr, *p_argv.c, size);
addr += (size + 3) / 4;
break;
}
default:
FFI_ASSERT(0);
}
}
}
void ffi_call(ffi_cif* cif, void(*fn)(void), void *rvalue, void **avalue)
{
extended_cif ecif;
unsigned long rsize = cif->rtype->size;
int flags = cif->flags;
void *alloc = NULL;
ecif.cif = cif;
ecif.avalue = avalue;
if (flags == FFI_TYPE_STRUCT && (rsize <= 16 || rvalue == NULL))
{
alloc = alloca(ALIGN(rsize, 4));
ecif.rvalue = alloc;
}
else
{
ecif.rvalue = rvalue;
}
if (cif->abi != FFI_SYSV)
FFI_ASSERT(0);
ffi_call_SYSV (ecif.rvalue, rsize, cif->flags, fn, cif->bytes, &ecif);
if (alloc != NULL && rvalue != NULL)
memcpy(rvalue, alloc, rsize);
}
extern void ffi_trampoline();
extern void ffi_cacheflush(void* start, void* end);
ffi_status
ffi_prep_closure_loc (ffi_closure* closure,
ffi_cif* cif,
void (*fun)(ffi_cif*, void*, void**, void*),
void *user_data,
void *codeloc)
{
memcpy(closure->tramp, ffi_trampoline, FFI_TRAMPOLINE_SIZE);
*(unsigned int*)(&closure->tramp[8]) = (unsigned int)ffi_closure_SYSV;
ffi_cacheflush(closure->tramp, closure->tramp + FFI_TRAMPOLINE_SIZE);
closure->cif = cif;
closure->fun = fun;
closure->user_data = user_data;
return FFI_OK;
}
long FFI_HIDDEN
ffi_closure_SYSV_inner(ffi_closure *closure, void **values, void *rvalue)
{
ffi_cif *cif;
ffi_type **arg_types;
void **avalue;
int i, areg;
cif = closure->cif;
if (cif->abi != FFI_SYSV)
return FFI_BAD_ABI;
areg = 0;
int rtype = cif->rtype->type;
if (rtype == FFI_TYPE_STRUCT && cif->rtype->size > 4 * 4)
{
rvalue = *values;
areg++;
}
cif = closure->cif;
arg_types = cif->arg_types;
avalue = alloca(cif->nargs * sizeof(void *));
for (i = 0; i < cif->nargs; i++)
{
if (arg_types[i]->alignment == 8 && (areg & 1) != 0)
areg++;
if (areg == FFI_REGISTER_NARGS)
areg += 4;
if (arg_types[i]->type == FFI_TYPE_STRUCT)
{
int numregs = ((arg_types[i]->size + 3) & ~3) / 4;
if (areg < FFI_REGISTER_NARGS && areg + numregs > FFI_REGISTER_NARGS)
areg = FFI_REGISTER_NARGS + 4;
}
avalue[i] = &values[areg];
areg += (arg_types[i]->size + 3) / 4;
}
(closure->fun)(cif, rvalue, avalue, closure->user_data);
return rtype;
}
|