在 another question我在移植代码时遇到问题:
unsigned long stack[] = { 1, 23, 33, 43 };
/* save all the registers and the stack pointer */
unsigned long esp;
asm __volatile__ ( "pusha" );
asm __volatile__ ( "mov %%esp, %0" :"=m" (esp));
for( i = 0; i < sizeof(stack); i++ ){
unsigned long val = stack[i];
asm __volatile__ ( "push %0" :: "m"(val) );
}
unsigned long ret = function_pointer();
/* restore registers and stack pointer */
asm __volatile__ ( "mov %0, %%esp" :: "m" (esp) );
asm __volatile__ ( "popa" );
对于 64 位平台,许多人告诉我应该使用 setcontext() 和 makecontext() 函数 set 来代替,因为 32 和 64 之间的调用转换差异位和可移植性问题。
好吧,我真的在网上找不到任何有用的文档,或者至少不是我需要实现这个的那种,那么,我如何使用这些函数将参数插入堆栈,调用通用函数指针,获取返回值然后恢复寄存器?
最佳答案
最后我使用了 libffi 。
关于c++ - setcontext 和 makecontext 调用通用函数指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2520413/