Commit 415723b40ba76f4a9df9b709db1cde0c65162072

Anthony Green 2016-02-20T06:49:19

Merge pull request #104 from joshtriplett/efi64 Support the Windows/EFI calling convention on all x86-64 targets

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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
diff --git a/configure.host b/configure.host
index c6f6a02..a4a22b7 100644
--- a/configure.host
+++ b/configure.host
@@ -246,7 +246,7 @@ case "${TARGET}" in
 	SOURCES="ffi.c sysv.S"
 	;;
   X86_64)
-	SOURCES="ffi64.c unix64.S"
+	SOURCES="ffi64.c unix64.S ffiw64.c win64.S"
 	;;
   X86_WIN64)
 	SOURCES="ffiw64.c win64.S"
diff --git a/src/x86/asmnames.h b/src/x86/asmnames.h
new file mode 100644
index 0000000..7551021
--- /dev/null
+++ b/src/x86/asmnames.h
@@ -0,0 +1,30 @@
+#ifndef ASMNAMES_H
+#define ASMNAMES_H
+
+#define C2(X, Y)  X ## Y
+#define C1(X, Y)  C2(X, Y)
+#ifdef __USER_LABEL_PREFIX__
+# define C(X)     C1(__USER_LABEL_PREFIX__, X)
+#else
+# define C(X)     X
+#endif
+
+#ifdef __APPLE__
+# define L(X)     C1(L, X)
+#else
+# define L(X)     C1(.L, X)
+#endif
+
+#if defined(__ELF__) && defined(__PIC__)
+# define PLT(X)	  X@PLT
+#else
+# define PLT(X)	  X
+#endif
+
+#ifdef __ELF__
+# define ENDF(X)  .type	X,@function; .size X, . - X
+#else
+# define ENDF(X)
+#endif
+
+#endif /* ASMNAMES_H */
diff --git a/src/x86/ffi64.c b/src/x86/ffi64.c
index 131b5e3..f52749e 100644
--- a/src/x86/ffi64.c
+++ b/src/x86/ffi64.c
@@ -388,6 +388,9 @@ examine_argument (ffi_type *type, enum x86_64_reg_class classes[MAX_CLASSES],
 
 /* Perform machine dependent cif processing.  */
 
+extern ffi_status
+ffi_prep_cif_machdep_efi64(ffi_cif *cif);
+
 ffi_status
 ffi_prep_cif_machdep (ffi_cif *cif)
 {
@@ -396,6 +399,8 @@ ffi_prep_cif_machdep (ffi_cif *cif)
   size_t bytes, n, rtype_size;
   ffi_type *rtype;
 
+  if (cif->abi == FFI_EFI64)
+    return ffi_prep_cif_machdep_efi64(cif);
   if (cif->abi != FFI_UNIX64)
     return FFI_BAD_ABI;
 
@@ -657,22 +662,41 @@ ffi_call_int (ffi_cif *cif, void (*fn)(void), void *rvalue,
 		   flags, rvalue, fn);
 }
 
+extern void
+ffi_call_efi64(ffi_cif *cif, void (*fn)(void), void *rvalue, void **avalue);
+
 void
 ffi_call (ffi_cif *cif, void (*fn)(void), void *rvalue, void **avalue)
 {
+  if (cif->abi == FFI_EFI64)
+    return ffi_call_efi64(cif, fn, rvalue, avalue);
   ffi_call_int (cif, fn, rvalue, avalue, NULL);
 }
 
+extern void
+ffi_call_go_efi64(ffi_cif *cif, void (*fn)(void), void *rvalue,
+		  void **avalue, void *closure);
+
 void
 ffi_call_go (ffi_cif *cif, void (*fn)(void), void *rvalue,
 	     void **avalue, void *closure)
 {
+  if (cif->abi == FFI_EFI64)
+    ffi_call_go_efi64(cif, fn, rvalue, avalue, closure);
   ffi_call_int (cif, fn, rvalue, avalue, closure);
 }
 
+
 extern void ffi_closure_unix64(void) FFI_HIDDEN;
 extern void ffi_closure_unix64_sse(void) FFI_HIDDEN;
 
+extern ffi_status
+ffi_prep_closure_loc_efi64(ffi_closure* closure,
+			   ffi_cif* cif,
+			   void (*fun)(ffi_cif*, void*, void**, void*),
+			   void *user_data,
+			   void *codeloc);
+
 ffi_status
 ffi_prep_closure_loc (ffi_closure* closure,
 		      ffi_cif* cif,
@@ -691,6 +715,8 @@ ffi_prep_closure_loc (ffi_closure* closure,
   void (*dest)(void);
   char *tramp = closure->tramp;
 
+  if (cif->abi == FFI_EFI64)
+    return ffi_prep_closure_loc_efi64(closure, cif, fun, user_data, codeloc);
   if (cif->abi != FFI_UNIX64)
     return FFI_BAD_ABI;
 
@@ -805,10 +831,16 @@ ffi_closure_unix64_inner(ffi_cif *cif,
 extern void ffi_go_closure_unix64(void) FFI_HIDDEN;
 extern void ffi_go_closure_unix64_sse(void) FFI_HIDDEN;
 
+extern ffi_status
+ffi_prep_go_closure_efi64(ffi_go_closure* closure, ffi_cif* cif,
+			  void (*fun)(ffi_cif*, void*, void**, void*));
+
 ffi_status
 ffi_prep_go_closure (ffi_go_closure* closure, ffi_cif* cif,
 		     void (*fun)(ffi_cif*, void*, void**, void*))
 {
+  if (cif->abi == FFI_EFI64)
+    return ffi_prep_go_closure_efi64(closure, cif, fun);
   if (cif->abi != FFI_UNIX64)
     return FFI_BAD_ABI;
 
diff --git a/src/x86/ffitarget.h b/src/x86/ffitarget.h
index 8c1dcac..25e3f4f 100644
--- a/src/x86/ffitarget.h
+++ b/src/x86/ffitarget.h
@@ -87,6 +87,8 @@ typedef enum ffi_abi {
 #elif defined(X86_64) || (defined (__x86_64__) && defined (X86_DARWIN))
   FFI_FIRST_ABI = 1,
   FFI_UNIX64,
+  FFI_WIN64,
+  FFI_EFI64 = FFI_WIN64,
   FFI_LAST_ABI,
   FFI_DEFAULT_ABI = FFI_UNIX64
 
diff --git a/src/x86/ffiw64.c b/src/x86/ffiw64.c
index 8a33a6c..0029be0 100644
--- a/src/x86/ffiw64.c
+++ b/src/x86/ffiw64.c
@@ -30,6 +30,10 @@
 #include <stdint.h>
 
 #ifdef X86_WIN64
+#define EFI64(name) name
+#else
+#define EFI64(name) name##_efi64
+#endif
 
 struct win64_call_frame
 {
@@ -44,7 +48,7 @@ extern void ffi_call_win64 (void *stack, struct win64_call_frame *,
 			    void *closure) FFI_HIDDEN;
 
 ffi_status
-ffi_prep_cif_machdep (ffi_cif *cif)
+EFI64(ffi_prep_cif_machdep)(ffi_cif *cif)
 {
   int flags, n;
 
@@ -159,13 +163,13 @@ ffi_call_int (ffi_cif *cif, void (*fn)(void), void *rvalue,
 }
 
 void
-ffi_call (ffi_cif *cif, void (*fn)(void), void *rvalue, void **avalue)
+EFI64(ffi_call)(ffi_cif *cif, void (*fn)(void), void *rvalue, void **avalue)
 {
   ffi_call_int (cif, fn, rvalue, avalue, NULL);
 }
 
 void
-ffi_call_go (ffi_cif *cif, void (*fn)(void), void *rvalue,
+EFI64(ffi_call_go)(ffi_cif *cif, void (*fn)(void), void *rvalue,
 	     void **avalue, void *closure)
 {
   ffi_call_int (cif, fn, rvalue, avalue, closure);
@@ -176,7 +180,7 @@ extern void ffi_closure_win64(void) FFI_HIDDEN;
 extern void ffi_go_closure_win64(void) FFI_HIDDEN;
 
 ffi_status
-ffi_prep_closure_loc (ffi_closure* closure,
+EFI64(ffi_prep_closure_loc)(ffi_closure* closure,
 		      ffi_cif* cif,
 		      void (*fun)(ffi_cif*, void*, void**, void*),
 		      void *user_data,
@@ -190,7 +194,7 @@ ffi_prep_closure_loc (ffi_closure* closure,
     /* nopl  (%rax) */
     0x0f, 0x1f, 0x00
   };
-  unsigned char *tramp = closure->tramp;
+  char *tramp = closure->tramp;
 
   if (cif->abi != FFI_WIN64)
     return FFI_BAD_ABI;
@@ -206,7 +210,7 @@ ffi_prep_closure_loc (ffi_closure* closure,
 }
 
 ffi_status
-ffi_prep_go_closure (ffi_go_closure* closure, ffi_cif* cif,
+EFI64(ffi_prep_go_closure)(ffi_go_closure* closure, ffi_cif* cif,
 		     void (*fun)(ffi_cif*, void*, void**, void*))
 {
   if (cif->abi != FFI_WIN64)
@@ -277,5 +281,3 @@ ffi_closure_win64_inner(ffi_cif *cif,
   fun (cif, rvalue, avalue, user_data);
   return flags;
 }
-
-#endif /* X86_WIN64 */
diff --git a/src/x86/unix64.S b/src/x86/unix64.S
index c83010c..129aba5 100644
--- a/src/x86/unix64.S
+++ b/src/x86/unix64.S
@@ -31,31 +31,10 @@
 #include <fficonfig.h>
 #include <ffi.h>
 #include "internal64.h"
+#include "asmnames.h"
 
 	.text
 
-#define C2(X, Y)  X ## Y
-#define C1(X, Y)  C2(X, Y)
-#ifdef __USER_LABEL_PREFIX__
-# define C(X)     C1(__USER_LABEL_PREFIX__, X)
-#else
-# define C(X)     X
-#endif
-
-#ifdef __APPLE__
-# define L(X)     C1(L, X)
-#else
-# define L(X)     C1(.L, X)
-#endif
-
-#ifdef __ELF__
-# define PLT(X)	  X@PLT
-# define ENDF(X)  .type	X,@function; .size X, . - X
-#else
-# define PLT(X)	  X
-# define ENDF(X)
-#endif
-
 /* This macro allows the safe creation of jump tables without an
    actual table.  The entry points into the table are all 8 bytes.
    The use of ORG asserts that we're at the correct location.  */
diff --git a/src/x86/win64.S b/src/x86/win64.S
index a5a20b6..9d4f8b9 100644
--- a/src/x86/win64.S
+++ b/src/x86/win64.S
@@ -2,20 +2,24 @@
 #include <fficonfig.h>
 #include <ffi.h>
 #include <ffi_cfi.h>
+#include "asmnames.h"
 
 #if defined(HAVE_AS_CFI_PSEUDO_OP)
         .cfi_sections   .debug_frame
 #endif
 
+#ifdef X86_WIN64
+#define SEH(...) __VA_ARGS__
 #define arg0	%rcx
 #define arg1	%rdx
 #define arg2	%r8
 #define arg3	%r9
-
-#ifdef SYMBOL_UNDERSCORE
-#define SYMBOL_NAME(name) _##name
 #else
-#define SYMBOL_NAME(name) name
+#define SEH(...)
+#define arg0	%rdi
+#define arg1	%rsi
+#define arg2	%rdx
+#define arg3	%rcx
 #endif
 
 .macro E which
@@ -34,7 +38,7 @@
 	.align	8
 	.globl	ffi_call_win64
 
-	.seh_proc ffi_call_win64
+	SEH(.seh_proc ffi_call_win64)
 ffi_call_win64:
 	cfi_startproc
 	/* Set up the local stack frame and install it in rbp/rsp.  */
@@ -44,9 +48,9 @@ ffi_call_win64:
 	movq	arg1, %rbp
 	cfi_def_cfa(%rbp, 16)
 	cfi_rel_offset(%rbp, 0)
-	.seh_pushreg %rbp
-	.seh_setframe %rbp, 0
-	.seh_endprologue
+	SEH(.seh_pushreg %rbp)
+	SEH(.seh_setframe %rbp, 0)
+	SEH(.seh_endprologue)
 	movq	arg0, %rsp
 
 	movq	arg2, %r10
@@ -97,7 +101,7 @@ E FFI_TYPE_DOUBLE
 	movsd	%xmm0, (%r8)
 	epilogue
 E FFI_TYPE_LONGDOUBLE
-	call	abort
+	call	PLT(C(abort))
 E FFI_TYPE_UINT8
 	movzbl	%al, %eax
 	movq	%rax, (%r8)
@@ -132,7 +136,7 @@ E FFI_TYPE_POINTER
 	movq	%rax, (%r8)
 	epilogue
 E FFI_TYPE_COMPLEX
-	call	abort
+	call	PLT(C(abort))
 E FFI_TYPE_SMALL_STRUCT_1B
 	movb	%al, (%r8)
 	epilogue
@@ -144,12 +148,12 @@ E FFI_TYPE_SMALL_STRUCT_4B
 	epilogue
 
 	.align	8
-99:	call	abort
+99:	call	PLT(C(abort))
 
 .purgem epilogue
 
 	cfi_endproc
-	.seh_endproc
+	SEH(.seh_endproc)
 
 
 /* 32 bytes of outgoing register stack space, 8 bytes of alignment,
@@ -161,33 +165,33 @@ E FFI_TYPE_SMALL_STRUCT_4B
 	.align	8
 	.globl	ffi_go_closure_win64
 
-	.seh_proc ffi_go_closure_win64
+	SEH(.seh_proc ffi_go_closure_win64)
 ffi_go_closure_win64:
 	cfi_startproc
 	/* Save all integer arguments into the incoming reg stack space.  */
-	movq	arg0, 8(%rsp)
-	movq	arg1, 16(%rsp)
-	movq	arg2, 24(%rsp)
-	movq	arg3, 32(%rsp)
+	movq	%rcx, 8(%rsp)
+	movq	%rdx, 16(%rsp)
+	movq	%r8, 24(%rsp)
+	movq	%r9, 32(%rsp)
 
 	movq	8(%r10), arg0			/* load cif */
 	movq	16(%r10), arg1			/* load fun */
 	movq	%r10, arg2			/* closure is user_data */
 	jmp	0f
 	cfi_endproc
-	.seh_endproc
+	SEH(.seh_endproc)
 
 	.align	8
 	.globl	ffi_closure_win64
 
-	.seh_proc ffi_closure_win64
+	SEH(.seh_proc ffi_closure_win64)
 ffi_closure_win64:
 	cfi_startproc
 	/* Save all integer arguments into the incoming reg stack space.  */
-	movq	arg0, 8(%rsp)
-	movq	arg1, 16(%rsp)
-	movq	arg2, 24(%rsp)
-	movq	arg3, 32(%rsp)
+	movq	%rcx, 8(%rsp)
+	movq	%rdx, 16(%rsp)
+	movq	%r8, 24(%rsp)
+	movq	%r9, 32(%rsp)
 
 	movq	FFI_TRAMPOLINE_SIZE(%r10), arg0		/* load cif */
 	movq	FFI_TRAMPOLINE_SIZE+8(%r10), arg1	/* load fun */
@@ -195,8 +199,8 @@ ffi_closure_win64:
 0:
 	subq	$ffi_clo_FS, %rsp
 	cfi_adjust_cfa_offset(ffi_clo_FS)
-	.seh_stackalloc ffi_clo_FS
-	.seh_endprologue
+	SEH(.seh_stackalloc ffi_clo_FS)
+	SEH(.seh_endprologue)
 
 	/* Save all sse arguments into the stack frame.  */
 	movsd	%xmm0, ffi_clo_OFF_X(%rsp)
@@ -216,4 +220,4 @@ ffi_closure_win64:
 	ret
 
 	cfi_endproc
-	.seh_endproc
+	SEH(.seh_endproc)