2

另一个问题中,我在移植代码时遇到了问题:

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 位平台,许多人告诉我,由于 32 位和 64 位之间的调用转换差异以及可移植性问题,我应该使用setcontext()makecontext()函数集。

好吧,我真的在网上找不到任何有用的文档,或者至少不是我需要实现的那种,所以,我怎样才能使用这些函数将参数压入堆栈,调用泛型函数指针,获取返回值然后恢复寄存器?

4

2 回答 2

2

最后我正在使用 libffi 。

于 2010-04-07T10:48:02.120 回答
1

The Wikipedia page has a decent example.

This is not the solution you are looking for. makecontext doesn't take an array but a variable argument list. So, in order to call it you need a function to convert an array to an argument list. Since that is what you want makecontext for, by the time you can use it you have already solved your problem.

I don't know what the solution is, but this is a dead end.

于 2010-03-26T00:38:00.790 回答