I am trying to write a program that will run a function from a dynamic library with its name, arguments and their types inputted by the user. I can get a void* pointer to this function using dlsym(func_name)
, however I do not know the types of arguments and their amount beforehand, so I can't dereference this pointer the normal way.
If I copy the byte representations of all the arguments into a char *
array, can I call a function with this array as its arguments? For example when calling printf in assembly I can push the arguments and the format string to the stack and call printf. Maybe I can do something similar in C, or use assembly insertions?
For example, lets say the function is void func(int, double);
and I am given arguments int a
and double b
. Then I would create an array:
char buf[sizeof(int) + sizeof(double)]
memcpy(buf, &a, sizeof(int))
memcpy(buf + sizeof(int), &b, sizeof(double))
And use it as the arguments pushed onto a stack.
The user inputs the name of the function, a string describing its type, and the arguments themselves. For example:
func_name
viid
5
6
2.34
func_name is the name of the function which will be used in dlsym. viid means that the function has void func_name(int, int, double)
type (first char is the return type, the rest are arguments. v = void, i = int, d = double). The rest are arguments.