-
Notifications
You must be signed in to change notification settings - Fork 0
/
calling_conventions.h
523 lines (371 loc) · 17.7 KB
/
calling_conventions.h
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
/*
Copyright 2010-2016 Luigi Auriemma
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
http://www.gnu.org/licenses/gpl-2.0.txt
*/
/*
x86 32bit calling conventions 0.2.2
------------------------------------
quick library for calling the 32bit functions (mainly dumped functions)
that use the following calling conventions:
cdecl, stdcall, thiscall, msfastcall, borland, pascal, watcom, safecall,
syscall, optlink and clarion.
there is also an experimental support for 64bit functions but has
not been tested.
compatible with gcc and mingw only which grants its working on both
Windows and *nix x86 systems.
the functions have been written in this "easy-to-use" way just
for being used in any project other than QuickBMS for which were
created and with the maximum compatibility in mind.
everything done here is necessary to preserve the stack and
allowing the correct assignment of the arguments to the registers.
note that all these functions have not been tested at 100%, my
only doubt is about the preserved registers at return in some
of them (like syscall).
how to use:
#include "calling_conventions.h"
ret = cdecl_call(original_32bit_function, 3, "arg1", arg2, 0x3);
how to use 64bit registers and functions:
#define CALLCONV64 // IA64 must be defined
#include "calling_conventions.h"
ret = cdecl_call64(original_64bit_function, 3, "arg1", arg2, 0x3);
limitations:
compatible with gcc and mingw only, some doubts about values
different than the return type (I choosed long because usually it
automatically fits the size of the system) and about arguments (for
example a 64bit argument using the 32bit calling convention will
not work)
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <stdint.h>
//#define CALLCONV64 // activate 64bit functions and registers
#define CALLCONV_INIT va_list ap; \
long i, \
ret, \
argv[argc]; \
\
va_start(ap, argc); \
for(i = 0; i < argc; i++) { \
argv[i] = va_arg(ap, long); \
} \
va_end(ap);
#define CALLCONV_ASM __asm__ __volatile__
// 32bit registers
// this is a project for reverse engineering stuff so I want to support
// the 32bit dumped code on both x86 and x86_64
#if defined(i386) || defined(IA64)
// too complex, not implemented yet:
// http://blogs.msdn.com/b/vcblog/archive/2013/07/12/introducing-vector-calling-convention.aspx
// long vectorcall_call(void *func, long argc, ...);
// this is just an experiment
// the first argument is the register to return, the next 6 arguments are assigned to the registers
// and then there are the stack arguments. so remember that argc does not correspond to the real
// number of arguments, if you have only one stack argument you must have argc set to 1 + 6 + 1 and 1 + 6
// NULL arguments before your one
long usercall_call(void *func, long argc, ...) {
CALLCONV_INIT
long ret_type = -1; // from 0 to 5 for covering the 6 registers, -1 for void
if(argc > 0) ret_type = argv[0];
int tmp_argc = argc - (1 + 6);
for(i = tmp_argc - 1; i >= 0; i--) { // right->left
CALLCONV_ASM("push %0" :: "g"(argv[1 + 6 + i]));
}
// eax ecx edx ebx esi edi
if(argc > 6) CALLCONV_ASM("movl %0, %%edi" :: "g"(argv[6]) : "%eax", "%ecx", "%edx", "%ebx", "%esi" );
if(argc > 5) CALLCONV_ASM("movl %0, %%esi" :: "g"(argv[5]) : "%eax", "%ecx", "%edx", "%ebx", "%edi" );
if(argc > 4) CALLCONV_ASM("movl %0, %%ebx" :: "g"(argv[4]) : "%eax", "%ecx", "%edx", "%esi", "%edi" );
if(argc > 3) CALLCONV_ASM("movl %0, %%edx" :: "g"(argv[3]) : "%eax", "%ecx", "%ebx", "%esi", "%edi" );
if(argc > 2) CALLCONV_ASM("movl %0, %%ecx" :: "g"(argv[2]) : "%eax", "%edx", "%ebx", "%esi", "%edi" );
if(argc > 1) CALLCONV_ASM("movl %0, %%eax" :: "g"(argv[1]) : "%ecx", "%edx", "%ebx", "%esi", "%edi" );
CALLCONV_ASM("call *%0" :: "g"(func));
switch(ret_type) {
case 0: CALLCONV_ASM("movl %%eax, %0" : "=g"(ret)); break;
case 1: CALLCONV_ASM("movl %%ecx, %0" : "=g"(ret)); break;
case 2: CALLCONV_ASM("movl %%edx, %0" : "=g"(ret)); break;
case 3: CALLCONV_ASM("movl %%ebx, %0" : "=g"(ret)); break;
case 4: CALLCONV_ASM("movl %%esi, %0" : "=g"(ret)); break;
case 5: CALLCONV_ASM("movl %%edi, %0" : "=g"(ret)); break;
default: ret = 0; break;
}
return(ret);
}
long cdecl_call(void *func, long argc, ...) {
CALLCONV_INIT
for(i = argc - 1; i >= 0; i--) { // right->left
CALLCONV_ASM("push %0" :: "g"(argv[i]));
}
CALLCONV_ASM("call *%0" :: "g"(func));
CALLCONV_ASM("add %0, %%esp" :: "g"(argc * 4));
CALLCONV_ASM("movl %%eax, %0" : "=g"(ret));
return(ret);
}
long stdcall_call(void *func, long argc, ...) {
CALLCONV_INIT
for(i = argc - 1; i >= 0; i--) { // right->left
CALLCONV_ASM("push %0" :: "g"(argv[i]));
}
CALLCONV_ASM("call *%0" :: "g"(func));
CALLCONV_ASM("movl %%eax, %0" : "=g"(ret));
return(ret);
}
long thiscall_call(void *func, long argc, ...) {
CALLCONV_INIT
for(i = argc - 1; i >= 1; i--) { // right->left
CALLCONV_ASM("push %0" :: "g"(argv[i]));
}
if(argc > 0) CALLCONV_ASM("movl %0, %%ecx" :: "g"(argv[0]));
CALLCONV_ASM("call *%0" :: "g"(func) : "%ecx");
CALLCONV_ASM("movl %%eax, %0" : "=g"(ret));
return(ret);
}
long msfastcall_call(void *func, long argc, ...) {
CALLCONV_INIT
for(i = argc - 1; i >= 2; i--) { // right->left
CALLCONV_ASM("push %0" :: "g"(argv[i]));
}
if(argc > 1) CALLCONV_ASM("movl %0, %%edx" :: "g"(argv[1]) : "%ecx" );
if(argc > 0) CALLCONV_ASM("movl %0, %%ecx" :: "g"(argv[0]) : "%edx");
CALLCONV_ASM("call *%0" :: "g"(func) : "%ecx","%edx");
CALLCONV_ASM("movl %%eax, %0" : "=g"(ret));
return(ret);
}
// borland, delphi, register
long borland_call(void *func, long argc, ...) {
CALLCONV_INIT
for(i = 3; i < argc; i++) { // left->right
CALLCONV_ASM("push %0" :: "g"(argv[i]));
}
if(argc > 2) CALLCONV_ASM("movl %0, %%ecx" :: "g"(argv[2]) : "%eax","%edx" );
if(argc > 1) CALLCONV_ASM("movl %0, %%edx" :: "g"(argv[1]) : "%eax", "%ecx");
if(argc > 0) CALLCONV_ASM("movl %0, %%eax" :: "g"(argv[0]) : "%edx","%ecx");
CALLCONV_ASM("call *%0" :: "g"(func) : "%eax","%edx","%ecx");
CALLCONV_ASM("movl %%eax, %0" : "=g"(ret));
return(ret);
}
long pascal_call(void *func, long argc, ...) {
CALLCONV_INIT
for(i = 0; i < argc; i++) { // left->right
CALLCONV_ASM("push %0" :: "g"(argv[i]));
}
CALLCONV_ASM("call *%0" :: "g"(func));
CALLCONV_ASM("movl %%eax, %0" : "=g"(ret));
return(ret);
}
long watcom_call(void *func, long argc, ...) {
CALLCONV_INIT
for(i = argc - 1; i >= 4; i--) { // right->left
CALLCONV_ASM("push %0" :: "g"(argv[i]));
}
if(argc > 3) CALLCONV_ASM("movl %0, %%ecx" :: "g"(argv[3]) : "%eax","%edx","%ebx" );
if(argc > 2) CALLCONV_ASM("movl %0, %%ebx" :: "g"(argv[2]) : "%eax","%edx", "%ecx");
if(argc > 1) CALLCONV_ASM("movl %0, %%edx" :: "g"(argv[1]) : "%eax", "%ebx","%ecx");
if(argc > 0) CALLCONV_ASM("movl %0, %%eax" :: "g"(argv[0]) : "%edx","%ebx","%ecx");
CALLCONV_ASM("call *%0" :: "g"(func) : "%eax","%edx","%ebx","%ecx");
CALLCONV_ASM("movl %%eax, %0" : "=g"(ret));
return(ret);
}
long safecall_call(void *func, long argc, ...) {
CALLCONV_INIT
ret = 0;
CALLCONV_ASM("push %0" :: "g"(&ret));
for(i = argc - 1; i >= 0; i--) { // right->left
CALLCONV_ASM("push %0" :: "g"(argv[i]));
}
CALLCONV_ASM("call *%0" :: "g"(func));
// eax is 0 if all ok or another value in case of exceptions
return(ret);
}
long syscall_call(void *func, long argc, ...) {
CALLCONV_INIT
for(i = argc - 1; i >= 0; i--) { // right->left
CALLCONV_ASM("push %0" :: "g"(argv[i]));
}
CALLCONV_ASM("movl %0, %%eax" :: "g"(argc)); // from Wikipedia but it looks false
CALLCONV_ASM("call *%0" :: "g"(func) : "%eax"); // note that is not clear the thing of
CALLCONV_ASM("add %0, %%esp" :: "g"(argc * 4));
CALLCONV_ASM("movl %%eax, %0" : "=g"(ret)); // preservation but should be ok like in cdecl
return(ret);
}
// optlink, visualage
long optlink_call(void *func, long argc, ...) {
CALLCONV_INIT
for(i = argc - 1; i >= 3; i--) { // right->left
CALLCONV_ASM("push %0" :: "g"(argv[i]));
}
if(argc > 2) CALLCONV_ASM("movl %0, %%ecx" :: "g"(argv[2]) : "%eax","%edx" );
if(argc > 1) CALLCONV_ASM("movl %0, %%edx" :: "g"(argv[1]) : "%eax", "%ecx");
if(argc > 0) CALLCONV_ASM("movl %0, %%eax" :: "g"(argv[0]) : "%edx","%ecx");
CALLCONV_ASM("call *%0" :: "g"(func) : "%eax","%edx","%ecx");
CALLCONV_ASM("add %0, %%esp" :: "g"(argc * 4));
CALLCONV_ASM("movl %%eax, %0" : "=g"(ret));
return(ret);
}
// clarion, TopSpeed, JPI
long clarion_call(void *func, long argc, ...) {
CALLCONV_INIT
for(i = 4; i < argc; i++) { // left->right
CALLCONV_ASM("push %0" :: "g"(argv[i]));
}
if(argc > 3) CALLCONV_ASM("movl %0, %%edx" :: "g"(argv[3]) : "%eax","%ebx","%ecx" );
if(argc > 2) CALLCONV_ASM("movl %0, %%ecx" :: "g"(argv[2]) : "%eax","%ebx", "%edx");
if(argc > 1) CALLCONV_ASM("movl %0, %%ebx" :: "g"(argv[1]) : "%eax", "%ecx","%edx");
if(argc > 0) CALLCONV_ASM("movl %0, %%eax" :: "g"(argv[0]) : "%ebx","%ecx","%edx");
CALLCONV_ASM("call *%0" :: "g"(func) : "%eax","%ebx","%ecx","%edx");
CALLCONV_ASM("movl %%eax, %0" : "=g"(ret));
//CALLCONV_ASM("movl %%edx, %0" : "=g"(ret)); // for pointers
return(ret);
}
/*
#else
#define cdecl_call (func, argc, ...) __cdecl__ func(__VA_ARGS__)
#define stdcall_call (func, argc, ...) __stdcall__ func(__VA_ARGS__)
#define thiscall_call (func, argc, ...) func(__VA_ARGS__)
#define msfastcall_call (func, argc, ...) func(__VA_ARGS__)
#define borland_call (func, argc, ...) func(__VA_ARGS__)
#define pascal_call (func, argc, ...) func(__VA_ARGS__)
#define watcom_call (func, argc, ...) func(__VA_ARGS__)
#define safecall_call (func, argc, ...) func(__VA_ARGS__)
#define syscall_call (func, argc, ...) func(__VA_ARGS__)
#define optlink_call (func, argc, ...) func(__VA_ARGS__)
#define clarion_call (func, argc, ...) func(__VA_ARGS__)
*/
#endif
// 64bit
// non tested, I have just replaced %e with %r and movl with movq
#if defined(IA64) && defined(CALLCONV64)
long cdecl_call64(void *func, long argc, ...) {
CALLCONV_INIT
for(i = argc - 1; i >= 0; i--) { // right->left
CALLCONV_ASM("push %0" :: "g"(argv[i]));
}
CALLCONV_ASM("call *%0" :: "g"(func));
CALLCONV_ASM("add %0, %%rsp" :: "g"(argc * 4));
CALLCONV_ASM("movq %%rax, %0" : "=g"(ret));
return(ret);
}
long stdcall_call64(void *func, long argc, ...) {
CALLCONV_INIT
for(i = argc - 1; i >= 0; i--) { // right->left
CALLCONV_ASM("push %0" :: "g"(argv[i]));
}
CALLCONV_ASM("call *%0" :: "g"(func));
CALLCONV_ASM("movq %%rax, %0" : "=g"(ret));
return(ret);
}
long thiscall_call64(void *func, long argc, ...) {
CALLCONV_INIT
for(i = argc - 1; i >= 1; i--) { // right->left
CALLCONV_ASM("push %0" :: "g"(argv[i]));
}
if(argc > 0) CALLCONV_ASM("movq %0, %%rcx" :: "g"(argv[0]));
CALLCONV_ASM("call *%0" :: "g"(func) : "%rcx");
CALLCONV_ASM("movq %%rax, %0" : "=g"(ret));
return(ret);
}
long msfastcall_call64(void *func, long argc, ...) {
CALLCONV_INIT
for(i = argc - 1; i >= 2; i--) { // right->left
CALLCONV_ASM("push %0" :: "g"(argv[i]));
}
if(argc > 1) CALLCONV_ASM("movq %0, %%rdx" :: "g"(argv[1]) : "%rcx" );
if(argc > 0) CALLCONV_ASM("movq %0, %%rcx" :: "g"(argv[0]) : "%rdx");
CALLCONV_ASM("call *%0" :: "g"(func) : "%rcx","%rdx");
CALLCONV_ASM("movq %%rax, %0" : "=g"(ret));
return(ret);
}
long borland_call64(void *func, long argc, ...) {
CALLCONV_INIT
for(i = 3; i < argc; i++) { // left->right
CALLCONV_ASM("push %0" :: "g"(argv[i]));
}
if(argc > 2) CALLCONV_ASM("movq %0, %%rcx" :: "g"(argv[2]) : "%rax","%rdx" );
if(argc > 1) CALLCONV_ASM("movq %0, %%rdx" :: "g"(argv[1]) : "%rax", "%rcx");
if(argc > 0) CALLCONV_ASM("movq %0, %%rax" :: "g"(argv[0]) : "%rdx","%rcx");
CALLCONV_ASM("call *%0" :: "g"(func) : "%rax","%rdx","%rcx");
CALLCONV_ASM("movq %%rax, %0" : "=g"(ret));
return(ret);
}
long pascal_call64(void *func, long argc, ...) {
CALLCONV_INIT
for(i = 0; i < argc; i++) { // left->right
CALLCONV_ASM("push %0" :: "g"(argv[i]));
}
CALLCONV_ASM("call *%0" :: "g"(func));
CALLCONV_ASM("movq %%rax, %0" : "=g"(ret));
return(ret);
}
long watcom_call64(void *func, long argc, ...) {
CALLCONV_INIT
for(i = argc - 1; i >= 4; i--) { // right->left
CALLCONV_ASM("push %0" :: "g"(argv[i]));
}
if(argc > 3) CALLCONV_ASM("movq %0, %%rcx" :: "g"(argv[3]) : "%rax","%rdx","%rbx" );
if(argc > 2) CALLCONV_ASM("movq %0, %%rbx" :: "g"(argv[2]) : "%rax","%rdx", "%rcx");
if(argc > 1) CALLCONV_ASM("movq %0, %%rdx" :: "g"(argv[1]) : "%rax", "%rbx","%rcx");
if(argc > 0) CALLCONV_ASM("movq %0, %%rax" :: "g"(argv[0]) : "%rdx","%rbx","%rcx");
CALLCONV_ASM("call *%0" :: "g"(func) : "%rax","%rdx","%rbx","%rcx");
CALLCONV_ASM("movq %%rax, %0" : "=g"(ret));
return(ret);
}
long safecall_call64(void *func, long argc, ...) {
CALLCONV_INIT
ret = 0;
CALLCONV_ASM("push %0" :: "g"(&ret));
for(i = argc - 1; i >= 0; i--) { // right->left
CALLCONV_ASM("push %0" :: "g"(argv[i]));
}
CALLCONV_ASM("call *%0" :: "g"(func));
// eax is 0 if all ok or another value in case of exceptions
return(ret);
}
long syscall_call64(void *func, long argc, ...) {
CALLCONV_INIT
for(i = argc - 1; i >= 0; i--) { // right->left
CALLCONV_ASM("push %0" :: "g"(argv[i]));
}
CALLCONV_ASM("movq %0, %%rax" :: "g"(argc)); // from Wikipedia but it looks false
CALLCONV_ASM("call *%0" :: "g"(func) : "%rax"); // note that is not clear the thing of
CALLCONV_ASM("add %0, %%rsp" :: "g"(argc * 4));
CALLCONV_ASM("movq %%rax, %0" : "=g"(ret)); // preservation but should be ok like in cdecl
return(ret);
}
long optlink_call64(void *func, long argc, ...) {
CALLCONV_INIT
for(i = argc - 1; i >= 3; i--) { // right->left
CALLCONV_ASM("push %0" :: "g"(argv[i]));
}
if(argc > 2) CALLCONV_ASM("movq %0, %%rcx" :: "g"(argv[2]) : "%rax","%rdx" );
if(argc > 1) CALLCONV_ASM("movq %0, %%rdx" :: "g"(argv[1]) : "%rax", "%rcx");
if(argc > 0) CALLCONV_ASM("movq %0, %%rax" :: "g"(argv[0]) : "%rdx","%rcx");
CALLCONV_ASM("call *%0" :: "g"(func) : "%rax","%rdx","%rcx");
CALLCONV_ASM("add %0, %%rsp" :: "g"(argc * 4));
CALLCONV_ASM("movq %%rax, %0" : "=g"(ret));
return(ret);
}
long clarion_call64(void *func, long argc, ...) {
CALLCONV_INIT
for(i = 4; i < argc; i++) { // left->right
CALLCONV_ASM("push %0" :: "g"(argv[i]));
}
if(argc > 3) CALLCONV_ASM("movq %0, %%rdx" :: "g"(argv[3]) : "%rax","%rbx","%rcx" );
if(argc > 2) CALLCONV_ASM("movq %0, %%rcx" :: "g"(argv[2]) : "%rax","%rbx", "%rdx");
if(argc > 1) CALLCONV_ASM("movq %0, %%rbx" :: "g"(argv[1]) : "%rax", "%rcx","%rdx");
if(argc > 0) CALLCONV_ASM("movq %0, %%rax" :: "g"(argv[0]) : "%rbx","%rcx","%rdx");
CALLCONV_ASM("call *%0" :: "g"(func) : "%rax","%rbx","%rcx","%rdx");
CALLCONV_ASM("movq %%rax, %0" : "=g"(ret));
//CALLCONV_ASM("movq %%rdx, %0" : "=g"(ret)); // for pointers
return(ret);
}
#endif