1 /* -----------------------------------------------------------------------
2 prep_cif.c - Copyright (c) 1996, 1998 Red Hat, Inc.
3
4 Permission is hereby granted, free of charge, to any person obtaining
5 a copy of this software and associated documentation files (the
6 ``Software''), to deal in the Software without restriction, including
7 without limitation the rights to use, copy, modify, merge, publish,
8 distribute, sublicense, and/or sell copies of the Software, and to
9 permit persons to whom the Software is furnished to do so, subject to
10 the following conditions:
11
12 The above copyright notice and this permission notice shall be included
13 in all copies or substantial portions of the Software.
14
15 THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND, EXPRESS
16 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 IN NO EVENT SHALL CYGNUS SOLUTIONS BE LIABLE FOR ANY CLAIM, DAMAGES OR
19 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21 OTHER DEALINGS IN THE SOFTWARE.
22 ----------------------------------------------------------------------- */
23
24 #include <ffi.h>
25 #include <ffi_common.h>
26
27 #include <stdbool.h>
28 #include <stdlib.h>
29
30 /* Round up to FFI_SIZEOF_ARG. */
31 #define STACK_ARG_SIZE(x) ALIGN(x, FFI_SIZEOF_ARG)
32
33 /* Perform machine independent initialization of aggregate type
34 specifications. */
35
36 static ffi_status
initialize_aggregate(ffi_type * arg)37 initialize_aggregate(
38 /*@out@*/ ffi_type* arg)
39 {
40 /*@-usedef@*/
41 ffi_type** ptr;
42
43 if (arg == NULL || arg->elements == NULL ||
44 arg->size != 0 || arg->alignment != 0)
45 return FFI_BAD_TYPEDEF;
46
47 ptr = &(arg->elements[0]);
48
49 while ((*ptr) != NULL)
50 {
51 if (((*ptr)->size == 0) && (initialize_aggregate(*ptr) != FFI_OK))
52 return FFI_BAD_TYPEDEF;
53
54 /* Perform a sanity check on the argument type */
55 FFI_ASSERT_VALID_TYPE(*ptr);
56
57 #ifdef POWERPC_DARWIN
58 int curalign = (*ptr)->alignment;
59
60 if (ptr != &(arg->elements[0]))
61 {
62 if (curalign > 4 && curalign != 16)
63 curalign = 4;
64 }
65
66 arg->size = ALIGN(arg->size, curalign);
67 arg->size += (*ptr)->size;
68 arg->alignment = (arg->alignment > curalign) ?
69 arg->alignment : curalign;
70 #else
71 arg->size = ALIGN(arg->size, (*ptr)->alignment);
72 arg->size += (*ptr)->size;
73 arg->alignment = (arg->alignment > (*ptr)->alignment) ?
74 arg->alignment : (*ptr)->alignment;
75 #endif
76
77 ptr++;
78 }
79
80 /* Structure size includes tail padding. This is important for
81 structures that fit in one register on ABIs like the PowerPC64
82 Linux ABI that right justify small structs in a register.
83 It's also needed for nested structure layout, for example
84 struct A { long a; char b; }; struct B { struct A x; char y; };
85 should find y at an offset of 2*sizeof(long) and result in a
86 total size of 3*sizeof(long). */
87 arg->size = ALIGN(arg->size, arg->alignment);
88
89 if (arg->size == 0)
90 return FFI_BAD_TYPEDEF;
91
92 return FFI_OK;
93
94 /*@=usedef@*/
95 }
96
97 #ifndef __CRIS__
98 /* The CRIS ABI specifies structure elements to have byte
99 alignment only, so it completely overrides this functions,
100 which assumes "natural" alignment and padding. */
101
102 /* Perform machine independent ffi_cif preparation, then call
103 machine dependent routine. */
104
105 #if defined(X86_DARWIN) && !defined __x86_64__
106
107 static inline bool
struct_on_stack(int size)108 struct_on_stack(
109 int size)
110 {
111 if (size > 8)
112 return true;
113
114 /* This is not what the ABI says, but is what is really implemented */
115 switch (size)
116 {
117 case 1:
118 case 2:
119 case 4:
120 case 8:
121 return false;
122
123 default:
124 return true;
125 }
126 }
127
128 #endif // defined(X86_DARWIN) && !defined __x86_64__
129
130 // Arguments' ffi_type->alignment must be nonzero.
131 ffi_status
ffi_prep_cif(ffi_cif * cif,ffi_abi abi,unsigned int nargs,ffi_type * rtype,ffi_type ** atypes)132 ffi_prep_cif(
133 /*@out@*/ /*@partial@*/ ffi_cif* cif,
134 ffi_abi abi,
135 unsigned int nargs,
136 /*@dependent@*/ /*@out@*/ /*@partial@*/ ffi_type* rtype,
137 /*@dependent@*/ ffi_type** atypes)
138 {
139 unsigned int bytes = 0;
140 unsigned int i;
141 ffi_type** ptr;
142
143 if (cif == NULL)
144 return FFI_BAD_TYPEDEF;
145
146 if (abi <= FFI_FIRST_ABI || abi > FFI_DEFAULT_ABI)
147 return FFI_BAD_ABI;
148
149 cif->abi = abi;
150 cif->arg_types = atypes;
151 cif->nargs = nargs;
152 cif->rtype = rtype;
153 cif->flags = 0;
154
155 /* Initialize the return type if necessary */
156 /*@-usedef@*/
157 if ((cif->rtype->size == 0) && (initialize_aggregate(cif->rtype) != FFI_OK))
158 return FFI_BAD_TYPEDEF;
159 /*@=usedef@*/
160
161 /* Perform a sanity check on the return type */
162 FFI_ASSERT_VALID_TYPE(cif->rtype);
163
164 /* x86-64 and s390 stack space allocation is handled in prep_machdep. */
165 #if !defined M68K && !defined __x86_64__ && !defined S390 && !defined PA
166 /* Make space for the return structure pointer */
167 if (cif->rtype->type == FFI_TYPE_STRUCT
168 #ifdef SPARC
169 && (cif->abi != FFI_V9 || cif->rtype->size > 32)
170 #endif
171 #ifdef X86_DARWIN
172 && (struct_on_stack(cif->rtype->size))
173 #endif
174 )
175 bytes = STACK_ARG_SIZE(sizeof(void*));
176 #endif
177
178 for (ptr = cif->arg_types, i = cif->nargs; i > 0; i--, ptr++)
179 {
180 /* Initialize any uninitialized aggregate type definitions */
181 if (((*ptr)->size == 0) && (initialize_aggregate((*ptr)) != FFI_OK))
182 return FFI_BAD_TYPEDEF;
183
184 if ((*ptr)->alignment == 0)
185 return FFI_BAD_TYPEDEF;
186
187 /* Perform a sanity check on the argument type, do this
188 check after the initialization. */
189 FFI_ASSERT_VALID_TYPE(*ptr);
190
191 #if defined(X86_DARWIN)
192 {
193 int align = (*ptr)->alignment;
194
195 if (align > 4)
196 align = 4;
197
198 if ((align - 1) & bytes)
199 bytes = ALIGN(bytes, align);
200
201 bytes += STACK_ARG_SIZE((*ptr)->size);
202 }
203 #elif !defined __x86_64__ && !defined S390 && !defined PA
204 #ifdef SPARC
205 if (((*ptr)->type == FFI_TYPE_STRUCT
206 && ((*ptr)->size > 16 || cif->abi != FFI_V9))
207 || ((*ptr)->type == FFI_TYPE_LONGDOUBLE
208 && cif->abi != FFI_V9))
209 bytes += sizeof(void*);
210 else
211 #endif
212 {
213 /* Add any padding if necessary */
214 if (((*ptr)->alignment - 1) & bytes)
215 bytes = ALIGN(bytes, (*ptr)->alignment);
216
217 bytes += STACK_ARG_SIZE((*ptr)->size);
218 }
219 #endif
220 }
221
222 cif->bytes = bytes;
223
224 /* Perform machine dependent cif processing */
225 return ffi_prep_cif_machdep(cif);
226 }
227 #endif /* not __CRIS__ */
228