【发布时间】:2012-02-22 19:41:12
【问题描述】:
我已经创建了共享库(将像插件一样使用)。有很多函数,比如a
extern "C" long __attribute__ ((__cdecl__)) SumAgrs(long X, long Y, long Z, long *Out)
{
*Out = X + Y + Z;
return 0;
}
我想在 C++(GCC,Linux)中调用这个库中的函数,但不是在编译时。当我使用内联汇编程序时,“push”指令会破坏局部变量,我不知道如何修复它。
typedef int (*FARPROC)();
void *dl_handle = dlopen("plugin.so", RTLD_LAZY);
FARPROC proc = (FARPROC)dlsym(dl_handle, "SumAgrs");
long result;
asm("leal %0, %%eax\n\r" \
"pushl %%eax" : : "m" (result));
asm("pushl $10");
asm("pushl $15");
asm("pushl $20");
asm("call *%0" : : "m" (proc));
结果二进制文件包含类似 call *24(%esp) 的内容。所以我的 pushl 更改 %esp 和 call 导致分段错误。但是如何避免这种行为呢?
谢谢
【问题讨论】:
标签: c++ gcc assembly call shared