Debian Codegraph @

src/powerpc/ffi.c

raw
1 /* -----------------------------------------------------------------------
2 ffi.c - Copyright (C) 2013 IBM
3 Copyright (C) 2011 Anthony Green
4 Copyright (C) 2011 Kyle Moffett
5 Copyright (C) 2008 Red Hat, Inc
6 Copyright (C) 2007, 2008 Free Software Foundation, Inc
7 Copyright (c) 1998 Geoffrey Keating
8
9 PowerPC 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, EXPRESS
23 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
25 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR
26 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
27 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
28 OTHER DEALINGS IN THE SOFTWARE.
29 ----------------------------------------------------------------------- */
30
31 #include "ffi.h"
32 #include "ffi_common.h"
33 #include "ffi_powerpc.h"
34 #include "internal.h"
35 #include <tramp.h>
36
37 #if HAVE_LONG_DOUBLE_VARIANT
38 /* Adjust ffi_type_longdouble. */
39 void FFI_HIDDEN
40 ffi_prep_types (ffi_abi abi)
41 {
42 # if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
43 # ifdef POWERPC64
44 ffi_prep_types_linux64 (abi);
45 # else
46 ffi_prep_types_sysv (abi);
47 # endif
48 # endif
49 }
50 #endif
51
52 /* Perform machine dependent cif processing */
53 ffi_status FFI_HIDDEN
54 ffi_prep_cif_machdep (ffi_cif *cif)
55 {
56 #ifdef POWERPC64
57 return ffi_prep_cif_linux64 (cif);
58 #else
59 return ffi_prep_cif_sysv (cif);
60 #endif
61 }
62
63 ffi_status FFI_HIDDEN
64 ffi_prep_cif_machdep_var (ffi_cif *cif,
65 unsigned int nfixedargs MAYBE_UNUSED,
66 unsigned int ntotalargs MAYBE_UNUSED)
67 {
68 #ifdef POWERPC64
69 return ffi_prep_cif_linux64_var (cif, nfixedargs, ntotalargs);
70 #else
71 return ffi_prep_cif_sysv (cif);
72 #endif
73 }
74
75 static void
76 ffi_call_int (ffi_cif *cif,
77 void (*fn) (void),
78 void *rvalue,
79 void **avalue,
80 void *closure)
81 {
82 /* The final SYSV ABI says that structures smaller or equal 8 bytes
83 are returned in r3/r4. A draft ABI used by linux instead returns
84 them in memory.
85
86 We bounce-buffer SYSV small struct return values so that sysv.S
87 can write r3 and r4 to memory without worrying about struct size.
88
89 For ELFv2 ABI, use a bounce buffer for homogeneous structs too,
90 for similar reasons. This bounce buffer must be aligned to 16
91 bytes for use with homogeneous structs of vectors (float128). */
92 float128 smst_buffer[8];
93 extended_cif ecif;
94
95 ecif.cif = cif;
96 ecif.avalue = avalue;
97
98 ecif.rvalue = rvalue;
99 if ((cif->flags & FLAG_RETURNS_SMST) != 0)
100 ecif.rvalue = smst_buffer;
101 /* Ensure that we have a valid struct return value.
102 FIXME: Isn't this just papering over a user problem? */
103 else if (!rvalue && cif->rtype->type == FFI_TYPE_STRUCT)
104 ecif.rvalue = alloca (cif->rtype->size);
105
106 #ifdef POWERPC64
107 ffi_call_LINUX64 (&ecif, fn, ecif.rvalue, cif->flags, closure,
108 -(long) cif->bytes);
109 #else
110 ffi_call_SYSV (&ecif, fn, ecif.rvalue, cif->flags, closure, -cif->bytes);
111 #endif
112
113 /* Check for a bounce-buffered return value */
114 if (rvalue && ecif.rvalue == smst_buffer)
115 {
116 unsigned int rsize = cif->rtype->size;
117 #ifndef __LITTLE_ENDIAN__
118 /* The SYSV ABI returns a structure of up to 4 bytes in size
119 left-padded in r3. */
120 # ifndef POWERPC64
121 if (rsize <= 4)
122 memcpy (rvalue, (char *) smst_buffer + 4 - rsize, rsize);
123 else
124 # endif
125 /* The SYSV ABI returns a structure of up to 8 bytes in size
126 left-padded in r3/r4, and the ELFv2 ABI similarly returns a
127 structure of up to 8 bytes in size left-padded in r3. But
128 note that a structure of a single float is not paddded. */
129 if (rsize <= 8 && (cif->flags & FLAG_RETURNS_FP) == 0)
130 memcpy (rvalue, (char *) smst_buffer + 8 - rsize, rsize);
131 else
132 #endif
133 memcpy (rvalue, smst_buffer, rsize);
134 }
135 }
136
137 void
138 ffi_call (ffi_cif *cif, void (*fn) (void), void *rvalue, void **avalue)
139 {
140 ffi_call_int (cif, fn, rvalue, avalue, NULL);
141 }
142
143 void
144 ffi_call_go (ffi_cif *cif, void (*fn) (void), void *rvalue, void **avalue,
145 void *closure)
146 {
147 ffi_call_int (cif, fn, rvalue, avalue, closure);
148 }
149
150 ffi_status
151 ffi_prep_closure_loc (ffi_closure *closure,
152 ffi_cif *cif,
153 void (*fun) (ffi_cif *, void *, void **, void *),
154 void *user_data,
155 void *codeloc)
156 {
157 #ifdef POWERPC64
158 return ffi_prep_closure_loc_linux64 (closure, cif, fun, user_data, codeloc);
159 #else
160 return ffi_prep_closure_loc_sysv (closure, cif, fun, user_data, codeloc);
161 #endif
162 }
163
164 ffi_status
165 ffi_prep_go_closure (ffi_go_closure *closure,
166 ffi_cif *cif,
167 void (*fun) (ffi_cif *, void *, void **, void *))
168 {
169 #ifdef POWERPC64
170 closure->tramp = ffi_go_closure_linux64;
171 #else
172 closure->tramp = ffi_go_closure_sysv;
173 #endif
174 closure->cif = cif;
175 closure->fun = fun;
176 return FFI_OK;
177 }
178
179 #ifdef FFI_EXEC_STATIC_TRAMP
180 void *
181 ffi_tramp_arch (size_t *tramp_size, size_t *map_size)
182 {
183 extern void *trampoline_code_table;
184 *tramp_size = PPC_TRAMP_SIZE;
185 *map_size = PPC_TRAMP_MAP_SIZE;
186 #if defined (_CALL_AIX) || _CALL_ELF == 1
187 /* The caller wants the entry point address of the trampoline code,
188 not the address of the function descriptor. */
189 return *(void **)trampoline_code_table;
190 #else
191 return &trampoline_code_table;
192 #endif
193 }
194 #endif