1 | n/a | #ifdef __i386__ |
---|
2 | n/a | /* ----------------------------------------------------------------------- |
---|
3 | n/a | ffi.c - Copyright (c) 1996, 1998, 1999, 2001 Red Hat, Inc. |
---|
4 | n/a | Copyright (c) 2002 Ranjit Mathew |
---|
5 | n/a | Copyright (c) 2002 Bo Thorsen |
---|
6 | n/a | Copyright (c) 2002 Roger Sayle |
---|
7 | n/a | |
---|
8 | n/a | x86 Foreign Function Interface |
---|
9 | n/a | |
---|
10 | n/a | Permission is hereby granted, free of charge, to any person obtaining |
---|
11 | n/a | a copy of this software and associated documentation files (the |
---|
12 | n/a | ``Software''), to deal in the Software without restriction, including |
---|
13 | n/a | without limitation the rights to use, copy, modify, merge, publish, |
---|
14 | n/a | distribute, sublicense, and/or sell copies of the Software, and to |
---|
15 | n/a | permit persons to whom the Software is furnished to do so, subject to |
---|
16 | n/a | the following conditions: |
---|
17 | n/a | |
---|
18 | n/a | The above copyright notice and this permission notice shall be included |
---|
19 | n/a | in all copies or substantial portions of the Software. |
---|
20 | n/a | |
---|
21 | n/a | THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND, EXPRESS |
---|
22 | n/a | OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
---|
23 | n/a | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
---|
24 | n/a | IN NO EVENT SHALL CYGNUS SOLUTIONS BE LIABLE FOR ANY CLAIM, DAMAGES OR |
---|
25 | n/a | OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
---|
26 | n/a | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
---|
27 | n/a | OTHER DEALINGS IN THE SOFTWARE. |
---|
28 | n/a | ----------------------------------------------------------------------- */ |
---|
29 | n/a | |
---|
30 | n/a | #include <ffi.h> |
---|
31 | n/a | #include <ffi_common.h> |
---|
32 | n/a | |
---|
33 | n/a | #include <stdlib.h> |
---|
34 | n/a | |
---|
35 | n/a | /* ffi_prep_args is called by the assembly routine once stack space |
---|
36 | n/a | has been allocated for the function's arguments */ |
---|
37 | n/a | |
---|
38 | n/a | void ffi_prep_args(char *stack, extended_cif *ecif); |
---|
39 | n/a | |
---|
40 | n/a | void ffi_prep_args(char *stack, extended_cif *ecif) |
---|
41 | n/a | { |
---|
42 | n/a | register unsigned int i; |
---|
43 | n/a | register void **p_argv; |
---|
44 | n/a | register char *argp; |
---|
45 | n/a | register ffi_type **p_arg; |
---|
46 | n/a | |
---|
47 | n/a | argp = stack; |
---|
48 | n/a | |
---|
49 | n/a | if (ecif->cif->flags == FFI_TYPE_STRUCT) |
---|
50 | n/a | { |
---|
51 | n/a | *(void **) argp = ecif->rvalue; |
---|
52 | n/a | argp += 4; |
---|
53 | n/a | } |
---|
54 | n/a | |
---|
55 | n/a | p_argv = ecif->avalue; |
---|
56 | n/a | |
---|
57 | n/a | for (i = ecif->cif->nargs, p_arg = ecif->cif->arg_types; |
---|
58 | n/a | i != 0; |
---|
59 | n/a | i--, p_arg++) |
---|
60 | n/a | { |
---|
61 | n/a | size_t z; |
---|
62 | n/a | |
---|
63 | n/a | /* Align if necessary */ |
---|
64 | n/a | if ((sizeof(int) - 1) & (unsigned) argp) |
---|
65 | n/a | argp = (char *) ALIGN(argp, sizeof(int)); |
---|
66 | n/a | |
---|
67 | n/a | z = (*p_arg)->size; |
---|
68 | n/a | if (z < sizeof(int)) |
---|
69 | n/a | { |
---|
70 | n/a | z = sizeof(int); |
---|
71 | n/a | switch ((*p_arg)->type) |
---|
72 | n/a | { |
---|
73 | n/a | case FFI_TYPE_SINT8: |
---|
74 | n/a | *(signed int *) argp = (signed int)*(SINT8 *)(* p_argv); |
---|
75 | n/a | break; |
---|
76 | n/a | |
---|
77 | n/a | case FFI_TYPE_UINT8: |
---|
78 | n/a | *(unsigned int *) argp = (unsigned int)*(UINT8 *)(* p_argv); |
---|
79 | n/a | break; |
---|
80 | n/a | |
---|
81 | n/a | case FFI_TYPE_SINT16: |
---|
82 | n/a | *(signed int *) argp = (signed int)*(SINT16 *)(* p_argv); |
---|
83 | n/a | break; |
---|
84 | n/a | |
---|
85 | n/a | case FFI_TYPE_UINT16: |
---|
86 | n/a | *(unsigned int *) argp = (unsigned int)*(UINT16 *)(* p_argv); |
---|
87 | n/a | break; |
---|
88 | n/a | |
---|
89 | n/a | case FFI_TYPE_SINT32: |
---|
90 | n/a | *(signed int *) argp = (signed int)*(SINT32 *)(* p_argv); |
---|
91 | n/a | break; |
---|
92 | n/a | |
---|
93 | n/a | case FFI_TYPE_UINT32: |
---|
94 | n/a | *(unsigned int *) argp = (unsigned int)*(UINT32 *)(* p_argv); |
---|
95 | n/a | break; |
---|
96 | n/a | |
---|
97 | n/a | case FFI_TYPE_STRUCT: |
---|
98 | n/a | *(unsigned int *) argp = (unsigned int)*(UINT32 *)(* p_argv); |
---|
99 | n/a | break; |
---|
100 | n/a | |
---|
101 | n/a | default: |
---|
102 | n/a | FFI_ASSERT(0); |
---|
103 | n/a | } |
---|
104 | n/a | } |
---|
105 | n/a | else |
---|
106 | n/a | { |
---|
107 | n/a | memcpy(argp, *p_argv, z); |
---|
108 | n/a | } |
---|
109 | n/a | p_argv++; |
---|
110 | n/a | argp += z; |
---|
111 | n/a | } |
---|
112 | n/a | |
---|
113 | n/a | return; |
---|
114 | n/a | } |
---|
115 | n/a | |
---|
116 | n/a | /* Perform machine dependent cif processing */ |
---|
117 | n/a | ffi_status ffi_prep_cif_machdep(ffi_cif *cif) |
---|
118 | n/a | { |
---|
119 | n/a | /* Set the return type flag */ |
---|
120 | n/a | switch (cif->rtype->type) |
---|
121 | n/a | { |
---|
122 | n/a | case FFI_TYPE_VOID: |
---|
123 | n/a | #ifdef X86 |
---|
124 | n/a | case FFI_TYPE_STRUCT: |
---|
125 | n/a | case FFI_TYPE_UINT8: |
---|
126 | n/a | case FFI_TYPE_UINT16: |
---|
127 | n/a | case FFI_TYPE_SINT8: |
---|
128 | n/a | case FFI_TYPE_SINT16: |
---|
129 | n/a | #endif |
---|
130 | n/a | |
---|
131 | n/a | case FFI_TYPE_SINT64: |
---|
132 | n/a | case FFI_TYPE_FLOAT: |
---|
133 | n/a | case FFI_TYPE_DOUBLE: |
---|
134 | n/a | case FFI_TYPE_LONGDOUBLE: |
---|
135 | n/a | cif->flags = (unsigned) cif->rtype->type; |
---|
136 | n/a | break; |
---|
137 | n/a | |
---|
138 | n/a | case FFI_TYPE_UINT64: |
---|
139 | n/a | cif->flags = FFI_TYPE_SINT64; |
---|
140 | n/a | break; |
---|
141 | n/a | |
---|
142 | n/a | #ifndef X86 |
---|
143 | n/a | case FFI_TYPE_STRUCT: |
---|
144 | n/a | if (cif->rtype->size == 1) |
---|
145 | n/a | { |
---|
146 | n/a | cif->flags = FFI_TYPE_SINT8; /* same as char size */ |
---|
147 | n/a | } |
---|
148 | n/a | else if (cif->rtype->size == 2) |
---|
149 | n/a | { |
---|
150 | n/a | cif->flags = FFI_TYPE_SINT16; /* same as short size */ |
---|
151 | n/a | } |
---|
152 | n/a | else if (cif->rtype->size == 4) |
---|
153 | n/a | { |
---|
154 | n/a | cif->flags = FFI_TYPE_INT; /* same as int type */ |
---|
155 | n/a | } |
---|
156 | n/a | else if (cif->rtype->size == 8) |
---|
157 | n/a | { |
---|
158 | n/a | cif->flags = FFI_TYPE_SINT64; /* same as int64 type */ |
---|
159 | n/a | } |
---|
160 | n/a | else |
---|
161 | n/a | { |
---|
162 | n/a | cif->flags = FFI_TYPE_STRUCT; |
---|
163 | n/a | } |
---|
164 | n/a | break; |
---|
165 | n/a | #endif |
---|
166 | n/a | |
---|
167 | n/a | default: |
---|
168 | n/a | cif->flags = FFI_TYPE_INT; |
---|
169 | n/a | break; |
---|
170 | n/a | } |
---|
171 | n/a | |
---|
172 | n/a | #ifdef X86_DARWIN |
---|
173 | n/a | cif->bytes = (cif->bytes + 15) & ~0xF; |
---|
174 | n/a | #endif |
---|
175 | n/a | |
---|
176 | n/a | return FFI_OK; |
---|
177 | n/a | } |
---|
178 | n/a | |
---|
179 | n/a | extern void ffi_call_SYSV(void (*)(char *, extended_cif *), extended_cif *, |
---|
180 | n/a | unsigned, unsigned, unsigned *, void (*fn)()); |
---|
181 | n/a | |
---|
182 | n/a | #ifdef X86_WIN32 |
---|
183 | n/a | extern void ffi_call_STDCALL(void (*)(char *, extended_cif *), extended_cif *, |
---|
184 | n/a | unsigned, unsigned, unsigned *, void (*fn)()); |
---|
185 | n/a | |
---|
186 | n/a | #endif /* X86_WIN32 */ |
---|
187 | n/a | |
---|
188 | n/a | void ffi_call(ffi_cif *cif, void (*fn)(), void *rvalue, void **avalue) |
---|
189 | n/a | { |
---|
190 | n/a | extended_cif ecif; |
---|
191 | n/a | |
---|
192 | n/a | ecif.cif = cif; |
---|
193 | n/a | ecif.avalue = avalue; |
---|
194 | n/a | |
---|
195 | n/a | /* If the return value is a struct and we don't have a return */ |
---|
196 | n/a | /* value address then we need to make one */ |
---|
197 | n/a | |
---|
198 | n/a | if ((rvalue == NULL) && |
---|
199 | n/a | (cif->flags == FFI_TYPE_STRUCT)) |
---|
200 | n/a | { |
---|
201 | n/a | ecif.rvalue = alloca(cif->rtype->size); |
---|
202 | n/a | } |
---|
203 | n/a | else |
---|
204 | n/a | ecif.rvalue = rvalue; |
---|
205 | n/a | |
---|
206 | n/a | |
---|
207 | n/a | switch (cif->abi) |
---|
208 | n/a | { |
---|
209 | n/a | case FFI_SYSV: |
---|
210 | n/a | ffi_call_SYSV(ffi_prep_args, &ecif, cif->bytes, cif->flags, ecif.rvalue, |
---|
211 | n/a | fn); |
---|
212 | n/a | break; |
---|
213 | n/a | #ifdef X86_WIN32 |
---|
214 | n/a | case FFI_STDCALL: |
---|
215 | n/a | ffi_call_STDCALL(ffi_prep_args, &ecif, cif->bytes, cif->flags, |
---|
216 | n/a | ecif.rvalue, fn); |
---|
217 | n/a | break; |
---|
218 | n/a | #endif /* X86_WIN32 */ |
---|
219 | n/a | default: |
---|
220 | n/a | FFI_ASSERT(0); |
---|
221 | n/a | break; |
---|
222 | n/a | } |
---|
223 | n/a | } |
---|
224 | n/a | |
---|
225 | n/a | |
---|
226 | n/a | /** private members **/ |
---|
227 | n/a | |
---|
228 | n/a | static void ffi_prep_incoming_args_SYSV (char *stack, void **ret, |
---|
229 | n/a | void** args, ffi_cif* cif); |
---|
230 | n/a | void FFI_HIDDEN ffi_closure_SYSV (ffi_closure *) |
---|
231 | n/a | __attribute__ ((regparm(1))); |
---|
232 | n/a | unsigned int FFI_HIDDEN ffi_closure_SYSV_inner (ffi_closure *, void **, void *) |
---|
233 | n/a | __attribute__ ((regparm(1))); |
---|
234 | n/a | void FFI_HIDDEN ffi_closure_raw_SYSV (ffi_raw_closure *) |
---|
235 | n/a | __attribute__ ((regparm(1))); |
---|
236 | n/a | |
---|
237 | n/a | /* This function is jumped to by the trampoline */ |
---|
238 | n/a | |
---|
239 | n/a | unsigned int FFI_HIDDEN |
---|
240 | n/a | ffi_closure_SYSV_inner (closure, respp, args) |
---|
241 | n/a | ffi_closure *closure; |
---|
242 | n/a | void **respp; |
---|
243 | n/a | void *args; |
---|
244 | n/a | { |
---|
245 | n/a | // our various things... |
---|
246 | n/a | ffi_cif *cif; |
---|
247 | n/a | void **arg_area; |
---|
248 | n/a | |
---|
249 | n/a | cif = closure->cif; |
---|
250 | n/a | arg_area = (void**) alloca (cif->nargs * sizeof (void*)); |
---|
251 | n/a | |
---|
252 | n/a | /* this call will initialize ARG_AREA, such that each |
---|
253 | n/a | * element in that array points to the corresponding |
---|
254 | n/a | * value on the stack; and if the function returns |
---|
255 | n/a | * a structure, it will re-set RESP to point to the |
---|
256 | n/a | * structure return address. */ |
---|
257 | n/a | |
---|
258 | n/a | ffi_prep_incoming_args_SYSV(args, respp, arg_area, cif); |
---|
259 | n/a | |
---|
260 | n/a | (closure->fun) (cif, *respp, arg_area, closure->user_data); |
---|
261 | n/a | |
---|
262 | n/a | return cif->flags; |
---|
263 | n/a | } |
---|
264 | n/a | |
---|
265 | n/a | static void |
---|
266 | n/a | ffi_prep_incoming_args_SYSV(char *stack, void **rvalue, void **avalue, |
---|
267 | n/a | ffi_cif *cif) |
---|
268 | n/a | { |
---|
269 | n/a | register unsigned int i; |
---|
270 | n/a | register void **p_argv; |
---|
271 | n/a | register char *argp; |
---|
272 | n/a | register ffi_type **p_arg; |
---|
273 | n/a | |
---|
274 | n/a | argp = stack; |
---|
275 | n/a | |
---|
276 | n/a | if ( cif->flags == FFI_TYPE_STRUCT ) { |
---|
277 | n/a | *rvalue = *(void **) argp; |
---|
278 | n/a | argp += 4; |
---|
279 | n/a | } |
---|
280 | n/a | |
---|
281 | n/a | p_argv = avalue; |
---|
282 | n/a | |
---|
283 | n/a | for (i = cif->nargs, p_arg = cif->arg_types; (i != 0); i--, p_arg++) |
---|
284 | n/a | { |
---|
285 | n/a | size_t z; |
---|
286 | n/a | |
---|
287 | n/a | /* Align if necessary */ |
---|
288 | n/a | if ((sizeof(int) - 1) & (unsigned) argp) { |
---|
289 | n/a | argp = (char *) ALIGN(argp, sizeof(int)); |
---|
290 | n/a | } |
---|
291 | n/a | |
---|
292 | n/a | z = (*p_arg)->size; |
---|
293 | n/a | |
---|
294 | n/a | /* because we're little endian, this is what it turns into. */ |
---|
295 | n/a | |
---|
296 | n/a | *p_argv = (void*) argp; |
---|
297 | n/a | |
---|
298 | n/a | p_argv++; |
---|
299 | n/a | argp += z; |
---|
300 | n/a | } |
---|
301 | n/a | |
---|
302 | n/a | return; |
---|
303 | n/a | } |
---|
304 | n/a | |
---|
305 | n/a | /* How to make a trampoline. Derived from gcc/config/i386/i386.c. */ |
---|
306 | n/a | |
---|
307 | n/a | #define FFI_INIT_TRAMPOLINE(TRAMP,FUN,CTX) \ |
---|
308 | n/a | ({ unsigned char *__tramp = (unsigned char*)(TRAMP); \ |
---|
309 | n/a | unsigned int __fun = (unsigned int)(FUN); \ |
---|
310 | n/a | unsigned int __ctx = (unsigned int)(CTX); \ |
---|
311 | n/a | unsigned int __dis = __fun - (__ctx + FFI_TRAMPOLINE_SIZE); \ |
---|
312 | n/a | *(unsigned char*) &__tramp[0] = 0xb8; \ |
---|
313 | n/a | *(unsigned int*) &__tramp[1] = __ctx; /* movl __ctx, %eax */ \ |
---|
314 | n/a | *(unsigned char *) &__tramp[5] = 0xe9; \ |
---|
315 | n/a | *(unsigned int*) &__tramp[6] = __dis; /* jmp __fun */ \ |
---|
316 | n/a | }) |
---|
317 | n/a | |
---|
318 | n/a | |
---|
319 | n/a | /* the cif must already be prep'ed */ |
---|
320 | n/a | ffi_status |
---|
321 | n/a | ffi_prep_closure (ffi_closure* closure, |
---|
322 | n/a | ffi_cif* cif, |
---|
323 | n/a | void (*fun)(ffi_cif*,void*,void**,void*), |
---|
324 | n/a | void *user_data) |
---|
325 | n/a | { |
---|
326 | n/a | if (cif->abi != FFI_SYSV) |
---|
327 | n/a | return FFI_BAD_ABI; |
---|
328 | n/a | |
---|
329 | n/a | FFI_INIT_TRAMPOLINE (&closure->tramp[0], \ |
---|
330 | n/a | &ffi_closure_SYSV, \ |
---|
331 | n/a | (void*)closure); |
---|
332 | n/a | |
---|
333 | n/a | closure->cif = cif; |
---|
334 | n/a | closure->user_data = user_data; |
---|
335 | n/a | closure->fun = fun; |
---|
336 | n/a | |
---|
337 | n/a | return FFI_OK; |
---|
338 | n/a | } |
---|
339 | n/a | |
---|
340 | n/a | /* ------- Native raw API support -------------------------------- */ |
---|
341 | n/a | |
---|
342 | n/a | #if !FFI_NO_RAW_API |
---|
343 | n/a | |
---|
344 | n/a | ffi_status |
---|
345 | n/a | ffi_prep_raw_closure_loc (ffi_raw_closure* closure, |
---|
346 | n/a | ffi_cif* cif, |
---|
347 | n/a | void (*fun)(ffi_cif*,void*,ffi_raw*,void*), |
---|
348 | n/a | void *user_data, |
---|
349 | n/a | void *codeloc) |
---|
350 | n/a | { |
---|
351 | n/a | int i; |
---|
352 | n/a | |
---|
353 | n/a | FFI_ASSERT (cif->abi == FFI_SYSV); |
---|
354 | n/a | |
---|
355 | n/a | // we currently don't support certain kinds of arguments for raw |
---|
356 | n/a | // closures. This should be implemented by a separate assembly language |
---|
357 | n/a | // routine, since it would require argument processing, something we |
---|
358 | n/a | // don't do now for performance. |
---|
359 | n/a | |
---|
360 | n/a | for (i = cif->nargs-1; i >= 0; i--) |
---|
361 | n/a | { |
---|
362 | n/a | FFI_ASSERT (cif->arg_types[i]->type != FFI_TYPE_STRUCT); |
---|
363 | n/a | FFI_ASSERT (cif->arg_types[i]->type != FFI_TYPE_LONGDOUBLE); |
---|
364 | n/a | } |
---|
365 | n/a | |
---|
366 | n/a | |
---|
367 | n/a | FFI_INIT_TRAMPOLINE (&closure->tramp[0], &ffi_closure_raw_SYSV, |
---|
368 | n/a | codeloc); |
---|
369 | n/a | |
---|
370 | n/a | closure->cif = cif; |
---|
371 | n/a | closure->user_data = user_data; |
---|
372 | n/a | closure->fun = fun; |
---|
373 | n/a | |
---|
374 | n/a | return FFI_OK; |
---|
375 | n/a | } |
---|
376 | n/a | |
---|
377 | n/a | static void |
---|
378 | n/a | ffi_prep_args_raw(char *stack, extended_cif *ecif) |
---|
379 | n/a | { |
---|
380 | n/a | memcpy (stack, ecif->avalue, ecif->cif->bytes); |
---|
381 | n/a | } |
---|
382 | n/a | |
---|
383 | n/a | /* we borrow this routine from libffi (it must be changed, though, to |
---|
384 | n/a | * actually call the function passed in the first argument. as of |
---|
385 | n/a | * libffi-1.20, this is not the case.) |
---|
386 | n/a | */ |
---|
387 | n/a | |
---|
388 | n/a | extern void |
---|
389 | n/a | ffi_call_SYSV(void (*)(char *, extended_cif *), extended_cif *, unsigned, |
---|
390 | n/a | unsigned, unsigned *, void (*fn)()); |
---|
391 | n/a | |
---|
392 | n/a | #ifdef X86_WIN32 |
---|
393 | n/a | extern void |
---|
394 | n/a | ffi_call_STDCALL(void (*)(char *, extended_cif *), extended_cif *, unsigned, |
---|
395 | n/a | unsigned, unsigned *, void (*fn)()); |
---|
396 | n/a | #endif /* X86_WIN32 */ |
---|
397 | n/a | |
---|
398 | n/a | void |
---|
399 | n/a | ffi_raw_call(ffi_cif *cif, void (*fn)(), void *rvalue, ffi_raw *fake_avalue) |
---|
400 | n/a | { |
---|
401 | n/a | extended_cif ecif; |
---|
402 | n/a | void **avalue = (void **)fake_avalue; |
---|
403 | n/a | |
---|
404 | n/a | ecif.cif = cif; |
---|
405 | n/a | ecif.avalue = avalue; |
---|
406 | n/a | |
---|
407 | n/a | /* If the return value is a struct and we don't have a return */ |
---|
408 | n/a | /* value address then we need to make one */ |
---|
409 | n/a | |
---|
410 | n/a | if ((rvalue == NULL) && |
---|
411 | n/a | (cif->rtype->type == FFI_TYPE_STRUCT)) |
---|
412 | n/a | { |
---|
413 | n/a | ecif.rvalue = alloca(cif->rtype->size); |
---|
414 | n/a | } |
---|
415 | n/a | else |
---|
416 | n/a | ecif.rvalue = rvalue; |
---|
417 | n/a | |
---|
418 | n/a | |
---|
419 | n/a | switch (cif->abi) |
---|
420 | n/a | { |
---|
421 | n/a | case FFI_SYSV: |
---|
422 | n/a | ffi_call_SYSV(ffi_prep_args_raw, &ecif, cif->bytes, cif->flags, |
---|
423 | n/a | ecif.rvalue, fn); |
---|
424 | n/a | break; |
---|
425 | n/a | #ifdef X86_WIN32 |
---|
426 | n/a | case FFI_STDCALL: |
---|
427 | n/a | ffi_call_STDCALL(ffi_prep_args_raw, &ecif, cif->bytes, cif->flags, |
---|
428 | n/a | ecif.rvalue, fn); |
---|
429 | n/a | break; |
---|
430 | n/a | #endif /* X86_WIN32 */ |
---|
431 | n/a | default: |
---|
432 | n/a | FFI_ASSERT(0); |
---|
433 | n/a | break; |
---|
434 | n/a | } |
---|
435 | n/a | } |
---|
436 | n/a | |
---|
437 | n/a | #endif |
---|
438 | n/a | #endif // __i386__ |
---|