Calling C functions at runtime with Inline Assembly
Posted: Tue Aug 03, 2010 1:40 am
Hi,
Am working on a project where I have to invoke c functions at runtime, without knowing type of arguments it takes. So I have function pointer and variable size list of arguments. The only way that I could think of to do this with using inline assembly.
Using assembly, I was able to push the arguments onto the stack and then invoke this function.
Here is the sample code, for linux x86.
---------------------------------------------------------------------------------------------------------------------------
void* dlib;
int count = 4;
float v[]={2.34, 3.45, 4.56, 5.67};
dlib = dlopen ("Lib path", RTLD_NOW);
void *function;
function = dlsym(dlib, "fun");
for (int i = count - 1; i >= 0; i-- )
{
asm ("pushl %0": : "g" (v) );
}
asm( "call %0": : "g" (function));
------------------------------------------------------------------------------------------------------------------------------------
This is working fine for all the data types except for double.
So here my question is, does anyone know how to invoke function with double data type arguments? Do I need to use a different instruction other than pushl, I used to push other data types on to the stack.
Regards,
Javahar
Am working on a project where I have to invoke c functions at runtime, without knowing type of arguments it takes. So I have function pointer and variable size list of arguments. The only way that I could think of to do this with using inline assembly.
Using assembly, I was able to push the arguments onto the stack and then invoke this function.
Here is the sample code, for linux x86.
---------------------------------------------------------------------------------------------------------------------------
void* dlib;
int count = 4;
float v[]={2.34, 3.45, 4.56, 5.67};
dlib = dlopen ("Lib path", RTLD_NOW);
void *function;
function = dlsym(dlib, "fun");
for (int i = count - 1; i >= 0; i-- )
{
asm ("pushl %0": : "g" (v) );
}
asm( "call %0": : "g" (function));
------------------------------------------------------------------------------------------------------------------------------------
This is working fine for all the data types except for double.
So here my question is, does anyone know how to invoke function with double data type arguments? Do I need to use a different instruction other than pushl, I used to push other data types on to the stack.
Regards,
Javahar