I’ve created shared library (will be used like plugin). There are a lot of functions like a
extern "C" long __attribute__ ((__cdecl__)) SumAgrs(long X, long Y, long Z, long *Out)
{
*Out = X + Y + Z;
return 0;
}
I would like to call functions from this library in C++(GCC, Linux), but not in compile time. When I use inline assembler, the “push” instruction corrupt local variables, and i have no idea how to fix it.
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));
Result binary file contains something like call *24(%esp). So my pushl change %esp and call cause segmentation fault. But how to avoid this behavior?
thx
Look at libffi: “A Portable Foreign Function Interface Library”
“The libffi library provides a portable, high level programming interface to various calling conventions. This allows a programmer to call any function specified by a call interface description at run-time.”
http://sourceware.org/libffi/