Here is my general implementation.
I initially did this with AsmJit , and then manually modified it to remove the dependency.
It works for both x86 and x64!
cdecl, stdcall!
"thiscall", V++, GCC, .
(V++, , 'this', GCC .)
!
:
, printf.
( ), -, , .
, .
O ( ), O ( ).
, .
#include <stddef.h>
size_t vbind(
void *( *f)(), size_t param_count,
unsigned char buffer[],
size_t const i, void *const bound[], unsigned int const n, bool const thiscall)
{
unsigned char *p = buffer;
unsigned char s = sizeof(void *);
unsigned char b = sizeof(int) == sizeof(void *) ? 2 : 3;
*p++ = 0x55;
if (b > 2) { *p++ = 0x48; } *p++ = 0x8B; *p++ = 0xEC;
if (b > 2)
{
*p++ = 0x48; *p++ = 0x89; *p++ = 0x4C; *p++ = 0x24; *p++ = 2 * s;
*p++ = 0x48; *p++ = 0x89; *p++ = 0x54; *p++ = 0x24; *p++ = 3 * s;
*p++ = 0x4C; *p++ = 0x89; *p++ = 0x44; *p++ = 0x24; *p++ = 4 * s;
*p++ = 0x4C; *p++ = 0x89; *p++ = 0x4C; *p++ = 0x24; *p++ = 5 * s;
}
if (b > 2) { *p++ = 0x48; } *p++ = 0xBA; *(*(size_t **)&p)++ = param_count;
if (b > 2) { *p++ = 0x48; } *p++ = 0x8B; *p++ = 0xC2;
if (b > 2) { *p++ = 0x48; } *p++ = 0xC1; *p++ = 0xE0; *p++ = b;
if (b > 2) { *p++ = 0x48; } *p++ = 0x2B; *p++ = 0xE0;
*p++ = 0x57;
*p++ = 0x56;
*p++ = 0x51;
*p++ = 0x9C;
if (b > 2) { *p++ = 0x48; } *p++ = 0xF7; *p++ = 0xD8;
if (b > 2) { *p++ = 0x48; } *p++ = 0x8D; *p++ = 0x7C; *p++ = 0x05; *p++ = 0x00;
if (b > 2) { *p++ = 0x48; } *p++ = 0x8D; *p++ = 0x75; *p++ = 2 * s;
if (b > 2) { *p++ = 0x48; } *p++ = 0xB9; *(*(size_t **)&p)++ = i;
if (b > 2) { *p++ = 0x48; } *p++ = 0x2B; *p++ = 0xD1;
*p++ = 0xFC;
*p++ = 0xF3; if (b > 2) { *p++ = 0x48; } *p++ = 0xA5;
for (unsigned int j = 0; j < n; j++)
{
unsigned int const o = j * sizeof(p);
if (b > 2) { *p++ = 0x48; } *p++ = 0xB8; *(*(void ***)&p)++ = bound[j];
if (b > 2) { *p++ = 0x48; } *p++ = 0x89; *p++ = 0x87; *(*(int **)&p)++ = o;
}
if (b > 2) { *p++ = 0x48; } *p++ = 0xB8; *(*(size_t **)&p)++ = n;
if (b > 2) { *p++ = 0x48; } *p++ = 0x2B; *p++ = 0xD0;
if (b > 2) { *p++ = 0x48; } *p++ = 0xC1; *p++ = 0xE0; *p++ = b;
if (b > 2) { *p++ = 0x48; } *p++ = 0x03; *p++ = 0xF8;
if (b > 2) { *p++ = 0x48; } *p++ = 0x8B; *p++ = 0xCA;
*p++ = 0xF3; if (b > 2) { *p++ = 0x48; } *p++ = 0xA5;
*p++ = 0x9D;
*p++ = 0x59;
*p++ = 0x5E;
*p++ = 0x5F;
if (b > 2)
{
*p++ = 0x48; *p++ = 0x8B; *p++ = 0x4C; *p++ = 0x24; *p++ = 0 * s;
*p++ = 0x48; *p++ = 0x8B; *p++ = 0x54; *p++ = 0x24; *p++ = 1 * s;
*p++ = 0x4C; *p++ = 0x8B; *p++ = 0x44; *p++ = 0x24; *p++ = 2 * s;
*p++ = 0x4C; *p++ = 0x8B; *p++ = 0x4C; *p++ = 0x24; *p++ = 3 * s;
*p++ = 0x48; *p++ = 0xB8; *(*(void *(***)())&p)++ = f;
*p++ = 0xFF; *p++ = 0xD0;
}
else
{
if (thiscall) { *p++ = 0x59; }
*p++ = 0xE8; *(*(ptrdiff_t **)&p)++ = (unsigned char *)f - p
#ifdef _MSC_VER
- s
#endif
;
}
if (b > 2) { *p++ = 0x48; } *p++ = 0x8B; *p++ = 0xE5;
*p++ = 0x5D;
*p++ = 0xC3;
return p - &buffer[0];
}
( Windows):
#include <assert.h>
#include <stdio.h>
#include <Windows.h>
void *__cdecl test(void *value, void *x, void *y, void *z, void *w, void *u)
{
if (u > 0) { test(value, x, y, z, w, (void *)((size_t)u - 1)); }
printf("Test called! %p %p %p %p %p %p\n", value, x, y, z, w, u);
return value;
}
struct Test
{
void *local;
void *operator()(void *value, void *x, void *y, void *z, void *w, void *u)
{
if (u > 0) { (*this)(value, x, y, z, w, (void *)((size_t)u - 1)); }
printf("Test::operator() called! %p %p %p %p %p %p %p\n", local, value, x, y, z, w, u);
return value;
}
};
int main()
{
unsigned char thunk[1024]; unsigned long old;
VirtualProtect(&thunk, sizeof(thunk), PAGE_EXECUTE_READWRITE, &old);
void *args[] = { (void *)0xBAADF00DBAADF001, (void *)0xBAADF00DBAADF002 };
void *(Test::*f)(void *value, void *x, void *y, void *z, void *w, void *u) = &Test::operator();
Test obj = { (void *)0x1234 };
assert(sizeof(f) == sizeof(void (*)()));
vbind(*(void *(**)())&f, 1 + 6, thunk, 1 + 1, args, sizeof(args) / sizeof(*args), true);
((void *(*)(void *, int, int, int, int))&thunk)(&obj, 3, 4, 5, 6);
vbind((void *(*)())test, 6, thunk, 1, args, sizeof(args) / sizeof(*args), false);
((void *(*)(int, int, int, int))&thunk)(3, 4, 5, 6);
}