How do I thunk an arbitrary function with an arbitrary (fixed) number of arguments, on x86 and x64?
(I don’t need floating-point, SSE, or the like. The arguments are all integers or pointers.)
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Here’s my generic implementation.
I initially made it with AsmJit, then modified it by hand to remove the dependency.
It works for both x86 and x64!
It works for both cdecl and stdcall!
It should also work for “thiscall”, both on VC++ and GCC, but I haven’t tested it.
(VC++ would probably not touch the ‘this’ pointer, whereas GCC would treat it as the first argument.)
It can bind an arbitrary number of arguments at any position in the parameter list!
Just beware:
It does not work for variadic functions, like
printf.Doing so would either require you to provide the number of arguments dynamically (which is painful) or would require you to store the return-pointers somewhere other than the stack, which is complicated.
It was not designed for ultra-high performance, but it should still be fast enough.
The speed is O(total parameter count), not O(bound parameter count).
Scroll to the right to see the assembly code.
Example (for Windows):