Debian Codegraph @

src/x86/ffi.c

raw
1 /* -----------------------------------------------------------------------
2 ffi.c - Copyright (c) 2017, 2022 Anthony Green
3 Copyright (c) 1996, 1998, 1999, 2001, 2007, 2008 Red Hat, Inc.
4 Copyright (c) 2002 Ranjit Mathew
5 Copyright (c) 2002 Bo Thorsen
6 Copyright (c) 2002 Roger Sayle
7 Copyright (C) 2008, 2010 Free Software Foundation, Inc.
8
9 x86 Foreign Function Interface
10
11 Permission is hereby granted, free of charge, to any person obtaining
12 a copy of this software and associated documentation files (the
13 ``Software''), to deal in the Software without restriction, including
14 without limitation the rights to use, copy, modify, merge, publish,
15 distribute, sublicense, and/or sell copies of the Software, and to
16 permit persons to whom the Software is furnished to do so, subject to
17 the following conditions:
18
19 The above copyright notice and this permission notice shall be included
20 in all copies or substantial portions of the Software.
21
22 THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND,
23 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
26 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
27 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29 DEALINGS IN THE SOFTWARE.
30 ----------------------------------------------------------------------- */
31
32 #if defined(__i386__) || defined(_M_IX86)
33 #include <ffi.h>
34 #include <ffi_common.h>
35 #include <stdint.h>
36 #include <stdlib.h>
37 #include <tramp.h>
38 #include "internal.h"
39
40 /* Force FFI_TYPE_LONGDOUBLE to be different than FFI_TYPE_DOUBLE;
41 all further uses in this file will refer to the 80-bit type. */
42 #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
43 # if FFI_TYPE_LONGDOUBLE != 4
44 # error FFI_TYPE_LONGDOUBLE out of date
45 # endif
46 #else
47 # undef FFI_TYPE_LONGDOUBLE
48 # define FFI_TYPE_LONGDOUBLE 4
49 #endif
50
51 #if defined(__GNUC__) && !defined(__declspec)
52 # define __declspec(x) __attribute__((x))
53 #endif
54
55 #if defined(_MSC_VER) && defined(_M_IX86)
56 /* Stack is not 16-byte aligned on Windows. */
57 #define STACK_ALIGN(bytes) (bytes)
58 #else
59 #define STACK_ALIGN(bytes) FFI_ALIGN (bytes, 16)
60 #endif
61
62 /* Perform machine dependent cif processing. */
63 ffi_status FFI_HIDDEN
64 ffi_prep_cif_machdep(ffi_cif *cif)
65 {
66 size_t bytes = 0;
67 int i, n, flags, cabi = cif->abi;
68
69 switch (cabi)
70 {
71 case FFI_SYSV:
72 case FFI_STDCALL:
73 case FFI_THISCALL:
74 case FFI_FASTCALL:
75 case FFI_MS_CDECL:
76 case FFI_PASCAL:
77 case FFI_REGISTER:
78 break;
79 default:
80 return FFI_BAD_ABI;
81 }
82
83 switch (cif->rtype->type)
84 {
85 case FFI_TYPE_VOID:
86 flags = X86_RET_VOID;
87 break;
88 case FFI_TYPE_FLOAT:
89 flags = X86_RET_FLOAT;
90 break;
91 case FFI_TYPE_DOUBLE:
92 flags = X86_RET_DOUBLE;
93 break;
94 case FFI_TYPE_LONGDOUBLE:
95 flags = X86_RET_LDOUBLE;
96 break;
97 case FFI_TYPE_UINT8:
98 flags = X86_RET_UINT8;
99 break;
100 case FFI_TYPE_UINT16:
101 flags = X86_RET_UINT16;
102 break;
103 case FFI_TYPE_SINT8:
104 flags = X86_RET_SINT8;
105 break;
106 case FFI_TYPE_SINT16:
107 flags = X86_RET_SINT16;
108 break;
109 case FFI_TYPE_INT:
110 case FFI_TYPE_SINT32:
111 case FFI_TYPE_UINT32:
112 case FFI_TYPE_POINTER:
113 flags = X86_RET_INT32;
114 break;
115 case FFI_TYPE_SINT64:
116 case FFI_TYPE_UINT64:
117 flags = X86_RET_INT64;
118 break;
119 case FFI_TYPE_STRUCT:
120 {
121 #if defined(X86_WIN32) || defined(X86_DARWIN)
122 size_t size = cif->rtype->size;
123 if (size == 1)
124 flags = X86_RET_STRUCT_1B;
125 else if (size == 2)
126 flags = X86_RET_STRUCT_2B;
127 else if (size == 4)
128 flags = X86_RET_INT32;
129 else if (size == 8)
130 flags = X86_RET_INT64;
131 else
132 #endif
133 {
134 do_struct:
135 switch (cabi)
136 {
137 case FFI_THISCALL:
138 case FFI_FASTCALL:
139 case FFI_STDCALL:
140 case FFI_MS_CDECL:
141 flags = X86_RET_STRUCTARG;
142 break;
143 default:
144 flags = X86_RET_STRUCTPOP;
145 break;
146 }
147 /* Allocate space for return value pointer. */
148 bytes += FFI_ALIGN (sizeof(void*), FFI_SIZEOF_ARG);
149 }
150 }
151 break;
152 case FFI_TYPE_COMPLEX:
153 switch (cif->rtype->elements[0]->type)
154 {
155 case FFI_TYPE_DOUBLE:
156 case FFI_TYPE_LONGDOUBLE:
157 case FFI_TYPE_SINT64:
158 case FFI_TYPE_UINT64:
159 goto do_struct;
160 case FFI_TYPE_FLOAT:
161 case FFI_TYPE_INT:
162 case FFI_TYPE_SINT32:
163 case FFI_TYPE_UINT32:
164 flags = X86_RET_INT64;
165 break;
166 case FFI_TYPE_SINT16:
167 case FFI_TYPE_UINT16:
168 flags = X86_RET_INT32;
169 break;
170 case FFI_TYPE_SINT8:
171 case FFI_TYPE_UINT8:
172 flags = X86_RET_STRUCT_2B;
173 break;
174 default:
175 return FFI_BAD_TYPEDEF;
176 }
177 break;
178 default:
179 return FFI_BAD_TYPEDEF;
180 }
181 cif->flags = flags;
182
183 for (i = 0, n = cif->nargs; i < n; i++)
184 {
185 ffi_type *t = cif->arg_types[i];
186
187 #if defined(X86_WIN32)
188 if (cabi == FFI_STDCALL)
189 bytes = FFI_ALIGN (bytes, FFI_SIZEOF_ARG);
190 else
191 #endif
192 bytes = FFI_ALIGN (bytes, t->alignment);
193 bytes += FFI_ALIGN (t->size, FFI_SIZEOF_ARG);
194 }
195 cif->bytes = bytes;
196
197 return FFI_OK;
198 }
199
200 static ffi_arg
201 extend_basic_type(void *arg, int type)
202 {
203 switch (type)
204 {
205 case FFI_TYPE_SINT8:
206 return *(SINT8 *)arg;
207 case FFI_TYPE_UINT8:
208 return *(UINT8 *)arg;
209 case FFI_TYPE_SINT16:
210 return *(SINT16 *)arg;
211 case FFI_TYPE_UINT16:
212 return *(UINT16 *)arg;
213
214 case FFI_TYPE_SINT32:
215 case FFI_TYPE_UINT32:
216 case FFI_TYPE_POINTER:
217 case FFI_TYPE_FLOAT:
218 return *(UINT32 *)arg;
219
220 default:
221 abort();
222 }
223 }
224
225 struct call_frame
226 {
227 void *ebp; /* 0 */
228 void *retaddr; /* 4 */
229 void (*fn)(void); /* 8 */
230 int flags; /* 12 */
231 void *rvalue; /* 16 */
232 unsigned regs[3]; /* 20-28 */
233 };
234
235 struct abi_params
236 {
237 int dir; /* parameter growth direction */
238 int static_chain; /* the static chain register used by gcc */
239 int nregs; /* number of register parameters */
240 int regs[3];
241 };
242
243 static const struct abi_params abi_params[FFI_LAST_ABI] = {
244 [FFI_SYSV] = { 1, R_ECX, 0 },
245 [FFI_THISCALL] = { 1, R_EAX, 1, { R_ECX } },
246 [FFI_FASTCALL] = { 1, R_EAX, 2, { R_ECX, R_EDX } },
247 [FFI_STDCALL] = { 1, R_ECX, 0 },
248 [FFI_PASCAL] = { -1, R_ECX, 0 },
249 /* ??? No defined static chain; gcc does not support REGISTER. */
250 [FFI_REGISTER] = { -1, R_ECX, 3, { R_EAX, R_EDX, R_ECX } },
251 [FFI_MS_CDECL] = { 1, R_ECX, 0 }
252 };
253
254 #ifdef HAVE_FASTCALL
255 #ifdef _MSC_VER
256 #define FFI_DECLARE_FASTCALL __fastcall
257 #else
258 #define FFI_DECLARE_FASTCALL __declspec(fastcall)
259 #endif
260 #else
261 #define FFI_DECLARE_FASTCALL
262 #endif
263
264 extern void FFI_DECLARE_FASTCALL ffi_call_i386(struct call_frame *, char *) FFI_HIDDEN;
265
266 /* We perform some black magic here to use some of the parent's stack frame in
267 * ffi_call_i386() that breaks with the MSVC compiler with the /RTCs or /GZ
268 * flags. Disable the 'Stack frame run time error checking' for this function
269 * so we don't hit weird exceptions in debug builds. */
270 #if defined(_MSC_VER)
271 #pragma runtime_checks("s", off)
272 #endif
273 /* n.b. ffi_call_unix64 will steal the alloca'd `stack` variable here for use
274 _as its own stack_ - so we need to compile this function without ASAN */
275 FFI_ASAN_NO_SANITIZE
276 static void
277 ffi_call_int (ffi_cif *cif, void (*fn)(void), void *rvalue,
278 void **avalue, void *closure)
279 {
280 size_t rsize, bytes;
281 struct call_frame *frame;
282 char *stack, *argp;
283 ffi_type **arg_types;
284 int flags, cabi, i, n, dir, narg_reg;
285 const struct abi_params *pabi;
286
287 flags = cif->flags;
288 cabi = cif->abi;
289 pabi = &abi_params[cabi];
290 dir = pabi->dir;
291
292 rsize = 0;
293 if (rvalue == NULL)
294 {
295 switch (flags)
296 {
297 case X86_RET_FLOAT:
298 case X86_RET_DOUBLE:
299 case X86_RET_LDOUBLE:
300 case X86_RET_STRUCTPOP:
301 case X86_RET_STRUCTARG:
302 /* The float cases need to pop the 387 stack.
303 The struct cases need to pass a valid pointer to the callee. */
304 rsize = cif->rtype->size;
305 break;
306 default:
307 /* We can pretend that the callee returns nothing. */
308 flags = X86_RET_VOID;
309 break;
310 }
311 }
312
313 bytes = STACK_ALIGN (cif->bytes);
314 stack = alloca(bytes + sizeof(*frame) + rsize);
315 argp = (dir < 0 ? stack + bytes : stack);
316 frame = (struct call_frame *)(stack + bytes);
317 if (rsize)
318 rvalue = frame + 1;
319
320 frame->fn = fn;
321 frame->flags = flags;
322 frame->rvalue = rvalue;
323 frame->regs[pabi->static_chain] = (unsigned)closure;
324
325 narg_reg = 0;
326 switch (flags)
327 {
328 case X86_RET_STRUCTARG:
329 /* The pointer is passed as the first argument. */
330 if (pabi->nregs > 0)
331 {
332 frame->regs[pabi->regs[0]] = (unsigned)rvalue;
333 narg_reg = 1;
334 break;
335 }
336 /* fallthru */
337 case X86_RET_STRUCTPOP:
338 *(void **)argp = rvalue;
339 argp += sizeof(void *);
340 break;
341 }
342
343 arg_types = cif->arg_types;
344 for (i = 0, n = cif->nargs; i < n; i++)
345 {
346 ffi_type *ty = arg_types[i];
347 void *valp = avalue[i];
348 size_t z = ty->size;
349 int t = ty->type;
350
351 if (z <= FFI_SIZEOF_ARG && t != FFI_TYPE_STRUCT)
352 {
353 ffi_arg val = extend_basic_type (valp, t);
354
355 if (t != FFI_TYPE_FLOAT && narg_reg < pabi->nregs)
356 frame->regs[pabi->regs[narg_reg++]] = val;
357 else if (dir < 0)
358 {
359 argp -= 4;
360 *(ffi_arg *)argp = val;
361 }
362 else
363 {
364 *(ffi_arg *)argp = val;
365 argp += 4;
366 }
367 }
368 else
369 {
370 size_t za = FFI_ALIGN (z, FFI_SIZEOF_ARG);
371 size_t align = FFI_SIZEOF_ARG;
372
373 /* Issue 434: For thiscall and fastcall, if the paramter passed
374 as 64-bit integer or struct, all following integer parameters
375 will be passed on stack. */
376 if ((cabi == FFI_THISCALL || cabi == FFI_FASTCALL)
377 && (t == FFI_TYPE_SINT64
378 || t == FFI_TYPE_UINT64
379 || t == FFI_TYPE_STRUCT))
380 narg_reg = 2;
381
382 /* Alignment rules for arguments are quite complex. Vectors and
383 structures with 16 byte alignment get it. Note that long double
384 on Darwin does have 16 byte alignment, and does not get this
385 alignment if passed directly; a structure with a long double
386 inside, however, would get 16 byte alignment. Since libffi does
387 not support vectors, we need non concern ourselves with other
388 cases. */
389 if (t == FFI_TYPE_STRUCT && ty->alignment >= 16)
390 align = 16;
391
392 if (dir < 0)
393 {
394 /* ??? These reverse argument ABIs are probably too old
395 to have cared about alignment. Someone should check. */
396 argp -= za;
397 memcpy (argp, valp, z);
398 }
399 else
400 {
401 argp = (char *)FFI_ALIGN (argp, align);
402 memcpy (argp, valp, z);
403 argp += za;
404 }
405 }
406 }
407 FFI_ASSERT (dir > 0 || argp == stack);
408
409 ffi_call_i386 (frame, stack);
410 }
411 #if defined(_MSC_VER)
412 #pragma runtime_checks("s", restore)
413 #endif
414
415 void
416 ffi_call (ffi_cif *cif, void (*fn)(void), void *rvalue, void **avalue)
417 {
418 ffi_call_int (cif, fn, rvalue, avalue, NULL);
419 }
420
421 #ifdef FFI_GO_CLOSURES
422 void
423 ffi_call_go (ffi_cif *cif, void (*fn)(void), void *rvalue,
424 void **avalue, void *closure)
425 {
426 ffi_call_int (cif, fn, rvalue, avalue, closure);
427 }
428 #endif
429
430 /** private members **/
431
432 void FFI_HIDDEN ffi_closure_i386(void);
433 void FFI_HIDDEN ffi_closure_STDCALL(void);
434 void FFI_HIDDEN ffi_closure_REGISTER(void);
435 #if defined(FFI_EXEC_STATIC_TRAMP)
436 void FFI_HIDDEN ffi_closure_i386_alt(void);
437 void FFI_HIDDEN ffi_closure_STDCALL_alt(void);
438 void FFI_HIDDEN ffi_closure_REGISTER_alt(void);
439 #endif
440
441 struct closure_frame
442 {
443 unsigned rettemp[4]; /* 0 */
444 unsigned regs[3]; /* 16-24 */
445 ffi_cif *cif; /* 28 */
446 void (*fun)(ffi_cif*,void*,void**,void*); /* 32 */
447 void *user_data; /* 36 */
448 };
449
450 int FFI_HIDDEN FFI_DECLARE_FASTCALL
451 ffi_closure_inner (struct closure_frame *frame, char *stack)
452 {
453 ffi_cif *cif = frame->cif;
454 int cabi, i, n, flags, dir, narg_reg;
455 const struct abi_params *pabi;
456 ffi_type **arg_types;
457 char *argp;
458 void *rvalue;
459 void **avalue;
460
461 cabi = cif->abi;
462 flags = cif->flags;
463 narg_reg = 0;
464 rvalue = frame->rettemp;
465 pabi = &abi_params[cabi];
466 dir = pabi->dir;
467 argp = (dir < 0 ? stack + STACK_ALIGN (cif->bytes) : stack);
468
469 switch (flags)
470 {
471 case X86_RET_STRUCTARG:
472 if (pabi->nregs > 0)
473 {
474 rvalue = (void *)frame->regs[pabi->regs[0]];
475 narg_reg = 1;
476 frame->rettemp[0] = (unsigned)rvalue;
477 break;
478 }
479 /* fallthru */
480 case X86_RET_STRUCTPOP:
481 rvalue = *(void **)argp;
482 argp += sizeof(void *);
483 frame->rettemp[0] = (unsigned)rvalue;
484 break;
485 }
486
487 n = cif->nargs;
488 avalue = alloca(sizeof(void *) * n);
489
490 arg_types = cif->arg_types;
491 for (i = 0; i < n; ++i)
492 {
493 ffi_type *ty = arg_types[i];
494 size_t z = ty->size;
495 int t = ty->type;
496 void *valp;
497
498 if (z <= FFI_SIZEOF_ARG && t != FFI_TYPE_STRUCT)
499 {
500 if (t != FFI_TYPE_FLOAT && narg_reg < pabi->nregs)
501 valp = &frame->regs[pabi->regs[narg_reg++]];
502 else if (dir < 0)
503 {
504 argp -= 4;
505 valp = argp;
506 }
507 else
508 {
509 valp = argp;
510 argp += 4;
511 }
512 }
513 else
514 {
515 size_t za = FFI_ALIGN (z, FFI_SIZEOF_ARG);
516 size_t align = FFI_SIZEOF_ARG;
517
518 /* See the comment in ffi_call_int. */
519 if (t == FFI_TYPE_STRUCT && ty->alignment >= 16)
520 align = 16;
521
522 /* Issue 434: For thiscall and fastcall, if the paramter passed
523 as 64-bit integer or struct, all following integer parameters
524 will be passed on stack. */
525 if ((cabi == FFI_THISCALL || cabi == FFI_FASTCALL)
526 && (t == FFI_TYPE_SINT64
527 || t == FFI_TYPE_UINT64
528 || t == FFI_TYPE_STRUCT))
529 narg_reg = 2;
530
531 if (dir < 0)
532 {
533 /* ??? These reverse argument ABIs are probably too old
534 to have cared about alignment. Someone should check. */
535 argp -= za;
536 valp = argp;
537 }
538 else
539 {
540 argp = (char *)FFI_ALIGN (argp, align);
541 valp = argp;
542 argp += za;
543 }
544 }
545
546 avalue[i] = valp;
547 }
548
549 frame->fun (cif, rvalue, avalue, frame->user_data);
550
551 switch (cabi)
552 {
553 case FFI_STDCALL:
554 return flags | (cif->bytes << X86_RET_POP_SHIFT);
555 case FFI_THISCALL:
556 case FFI_FASTCALL:
557 return flags | ((cif->bytes - (narg_reg * FFI_SIZEOF_ARG))
558 << X86_RET_POP_SHIFT);
559 default:
560 return flags;
561 }
562 }
563
564 ffi_status
565 ffi_prep_closure_loc (ffi_closure* closure,
566 ffi_cif* cif,
567 void (*fun)(ffi_cif*,void*,void**,void*),
568 void *user_data,
569 void *codeloc)
570 {
571 char *tramp = closure->tramp;
572 void (*dest)(void);
573 int op = 0xb8; /* movl imm, %eax */
574
575 switch (cif->abi)
576 {
577 case FFI_SYSV:
578 case FFI_MS_CDECL:
579 dest = ffi_closure_i386;
580 break;
581 case FFI_STDCALL:
582 case FFI_THISCALL:
583 case FFI_FASTCALL:
584 case FFI_PASCAL:
585 dest = ffi_closure_STDCALL;
586 break;
587 case FFI_REGISTER:
588 dest = ffi_closure_REGISTER;
589 op = 0x68; /* pushl imm */
590 break;
591 default:
592 return FFI_BAD_ABI;
593 }
594
595 #if defined(FFI_EXEC_STATIC_TRAMP)
596 if (ffi_tramp_is_present(closure))
597 {
598 /* Initialize the static trampoline's parameters. */
599 if (dest == ffi_closure_i386)
600 dest = ffi_closure_i386_alt;
601 else if (dest == ffi_closure_STDCALL)
602 dest = ffi_closure_STDCALL_alt;
603 else
604 dest = ffi_closure_REGISTER_alt;
605 ffi_tramp_set_parms (closure->ftramp, dest, closure);
606 goto out;
607 }
608 #endif
609
610 /* Initialize the dynamic trampoline. */
611 /* endbr32. */
612 *(UINT32 *) tramp = 0xfb1e0ff3;
613
614 /* movl or pushl immediate. */
615 tramp[4] = op;
616 *(void **)(tramp + 5) = codeloc;
617
618 /* jmp dest */
619 tramp[9] = 0xe9;
620 *(unsigned *)(tramp + 10) = (unsigned)dest - ((unsigned)codeloc + 14);
621
622 #if defined(FFI_EXEC_STATIC_TRAMP)
623 out:
624 #endif
625 closure->cif = cif;
626 closure->fun = fun;
627 closure->user_data = user_data;
628
629 return FFI_OK;
630 }
631
632 #ifdef FFI_GO_CLOSURES
633
634 void FFI_HIDDEN ffi_go_closure_EAX(void);
635 void FFI_HIDDEN ffi_go_closure_ECX(void);
636 void FFI_HIDDEN ffi_go_closure_STDCALL(void);
637
638 ffi_status
639 ffi_prep_go_closure (ffi_go_closure* closure, ffi_cif* cif,
640 void (*fun)(ffi_cif*,void*,void**,void*))
641 {
642 void (*dest)(void);
643
644 switch (cif->abi)
645 {
646 case FFI_SYSV:
647 case FFI_MS_CDECL:
648 dest = ffi_go_closure_ECX;
649 break;
650 case FFI_THISCALL:
651 case FFI_FASTCALL:
652 dest = ffi_go_closure_EAX;
653 break;
654 case FFI_STDCALL:
655 case FFI_PASCAL:
656 dest = ffi_go_closure_STDCALL;
657 break;
658 case FFI_REGISTER:
659 default:
660 return FFI_BAD_ABI;
661 }
662
663 closure->tramp = dest;
664 closure->cif = cif;
665 closure->fun = fun;
666
667 return FFI_OK;
668 }
669
670 #endif /* FFI_GO_CLOSURES */
671
672 /* ------- Native raw API support -------------------------------- */
673
674 #if !FFI_NO_RAW_API
675
676 void FFI_HIDDEN ffi_closure_raw_SYSV(void);
677 void FFI_HIDDEN ffi_closure_raw_THISCALL(void);
678
679 ffi_status
680 ffi_prep_raw_closure_loc (ffi_raw_closure *closure,
681 ffi_cif *cif,
682 void (*fun)(ffi_cif*,void*,ffi_raw*,void*),
683 void *user_data,
684 void *codeloc)
685 {
686 char *tramp = closure->tramp;
687 void (*dest)(void);
688 int i;
689
690 /* We currently don't support certain kinds of arguments for raw
691 closures. This should be implemented by a separate assembly
692 language routine, since it would require argument processing,
693 something we don't do now for performance. */
694 for (i = cif->nargs-1; i >= 0; i--)
695 switch (cif->arg_types[i]->type)
696 {
697 case FFI_TYPE_STRUCT:
698 case FFI_TYPE_LONGDOUBLE:
699 return FFI_BAD_TYPEDEF;
700 }
701
702 switch (cif->abi)
703 {
704 case FFI_THISCALL:
705 dest = ffi_closure_raw_THISCALL;
706 break;
707 case FFI_SYSV:
708 dest = ffi_closure_raw_SYSV;
709 break;
710 default:
711 return FFI_BAD_ABI;
712 }
713
714 /* movl imm, %eax. */
715 tramp[0] = 0xb8;
716 *(void **)(tramp + 1) = codeloc;
717
718 /* jmp dest */
719 tramp[5] = 0xe9;
720 *(unsigned *)(tramp + 6) = (unsigned)dest - ((unsigned)codeloc + 10);
721
722 closure->cif = cif;
723 closure->fun = fun;
724 closure->user_data = user_data;
725
726 return FFI_OK;
727 }
728
729 void
730 ffi_raw_call(ffi_cif *cif, void (*fn)(void), void *rvalue, ffi_raw *avalue)
731 {
732 size_t rsize, bytes;
733 struct call_frame *frame;
734 char *stack, *argp;
735 ffi_type **arg_types;
736 int flags, cabi, i, n, narg_reg;
737 const struct abi_params *pabi;
738
739 flags = cif->flags;
740 cabi = cif->abi;
741 pabi = &abi_params[cabi];
742
743 rsize = 0;
744 if (rvalue == NULL)
745 {
746 switch (flags)
747 {
748 case X86_RET_FLOAT:
749 case X86_RET_DOUBLE:
750 case X86_RET_LDOUBLE:
751 case X86_RET_STRUCTPOP:
752 case X86_RET_STRUCTARG:
753 /* The float cases need to pop the 387 stack.
754 The struct cases need to pass a valid pointer to the callee. */
755 rsize = cif->rtype->size;
756 break;
757 default:
758 /* We can pretend that the callee returns nothing. */
759 flags = X86_RET_VOID;
760 break;
761 }
762 }
763
764 bytes = STACK_ALIGN (cif->bytes);
765 argp = stack =
766 (void *)((uintptr_t)alloca(bytes + sizeof(*frame) + rsize + 15) & ~16);
767 frame = (struct call_frame *)(stack + bytes);
768 if (rsize)
769 rvalue = frame + 1;
770
771 frame->fn = fn;
772 frame->flags = flags;
773 frame->rvalue = rvalue;
774
775 narg_reg = 0;
776 switch (flags)
777 {
778 case X86_RET_STRUCTARG:
779 /* The pointer is passed as the first argument. */
780 if (pabi->nregs > 0)
781 {
782 frame->regs[pabi->regs[0]] = (unsigned)rvalue;
783 narg_reg = 1;
784 break;
785 }
786 /* fallthru */
787 case X86_RET_STRUCTPOP:
788 *(void **)argp = rvalue;
789 argp += sizeof(void *);
790 bytes -= sizeof(void *);
791 break;
792 }
793
794 arg_types = cif->arg_types;
795 for (i = 0, n = cif->nargs; narg_reg < pabi->nregs && i < n; i++)
796 {
797 ffi_type *ty = arg_types[i];
798 size_t z = ty->size;
799 int t = ty->type;
800
801 if (z <= FFI_SIZEOF_ARG && t != FFI_TYPE_STRUCT && t != FFI_TYPE_FLOAT)
802 {
803 ffi_arg val = extend_basic_type (avalue, t);
804 frame->regs[pabi->regs[narg_reg++]] = val;
805 z = FFI_SIZEOF_ARG;
806 }
807 else
808 {
809 memcpy (argp, avalue, z);
810 z = FFI_ALIGN (z, FFI_SIZEOF_ARG);
811 argp += z;
812 }
813 avalue += z;
814 bytes -= z;
815 }
816 if (i < n)
817 memcpy (argp, avalue, bytes);
818
819 ffi_call_i386 (frame, stack);
820 }
821 #endif /* !FFI_NO_RAW_API */
822
823 #if defined(FFI_EXEC_STATIC_TRAMP)
824 void *
825 ffi_tramp_arch (size_t *tramp_size, size_t *map_size)
826 {
827 extern void *trampoline_code_table;
828
829 *map_size = X86_TRAMP_MAP_SIZE;
830 *tramp_size = X86_TRAMP_SIZE;
831 return &trampoline_code_table;
832 }
833 #endif
834
835 #endif /* __i386__ */