【发布时间】:2016-12-07 11:23:31
【问题描述】:
我想 thunk(通过调整堆栈)__stdcall 函数。例如,调用者 think is 有一个指向回调函数的指针,其原型如下:
int Func(int a);
我想编写一个 thunk(将作为回调传递)来调整对以下函数原型的调用:
int Func(char* ptr, int a);
我知道当我的 thunk 将被调用时,堆栈布局是:
------------------
| a |
------------------
| return address | <-- Top of the stack
------------------
我想将卡住调整为:
------------------
| a |
------------------
------------------
| ptr |
------------------
| return address | <-- Top of the stack
------------------
然后相对跳转到相关函数(这样当被调用函数返回时栈是平衡的)
这是我最后的工作(当然它必须被打包并动态加载到可执行内存),感谢 Jester 帮助汇编十六进制代码以获得所需的指令!
struct Thunk
{
BYTE m_popRet;
BYTE m_pushPtr;
DWORD m_ptr;
BYTE m_pushRet;
BYTE m_jmp;
DWORD m_relProc;
BOOL Init(DWORD_PTR proc, void* ptr)
{
m_popRet = 0x58;
m_pushPtr = 0x68;
m_ptr = PtrToUlong(ptr);
m_pushRet = 0x50;
m_jmp = 0xE9;
m_relProc = DWORD((INT_PTR)proc - ((INT_PTR)this + sizeof Thunk));
return TRUE;
}
}
【问题讨论】:
标签: c++ assembly x86 calling-convention