我已经创建了共享库(将像插件一样使用)。有很多功能,例如

extern "C" long __attribute__ ((__cdecl__)) SumAgrs(long X, long Y, long Z, long *Out)
{
    *Out = X + Y + Z;
    return 0;
}

我想在C++(GCC,Linux)中从此库中调用函数,但不能在编译时调用。当我使用内联汇编程序时,“推”指令破坏了局部变量,并且我不知道如何解决它。
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));

结果二进制文件包含类似于的电话* 24(%esp)。因此,我的 pushl 更改%esp 调用会导致分段错误。但是如何避免这种行为呢?

谢谢

最佳答案

看一下libffi:“可移植的外部函数接口(interface)库”

“libffi库为各种调用约定提供了一个可移植的高级编程接口(interface)。这使程序员可以在运行时调用由调用接口(interface)描述指定的任何函数。”

http://sourceware.org/libffi/

关于c++ - 使用内联汇编器从GCC中的共享库中调用函数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9401778/

10-11 13:58