I've got an issue I am not able to solve. Looked up everything I found so far. My problem is, I create a dyn library in my program a want to dlopen it and dlsym a method out of that lib.
It seems that dlopen works but dlsym return me the error "undefined symbol: method"
where "method" is the name of the method I passed to dlsym.
Here is how I create the library:
execl("/usr/bin/gcc", "gcc", "-fPIC", "-Wall", "-g", "-c", "userinput.c", NULL);
and:
execl("/usr/bin/gcc", "gcc", "-ggdb3", "-shared",
"-Wl,-soname,libuserinput.so.1", "-o", "libuserinput.so.1.0",
"userinput.o", "-lc", NULL);
This should work as there is a library after running my code.
I open the library like this:
static void *my_load_dyn (const char *lib) {
static void *handle;
handle = dlopen ("./libuserinput.so.1.0", RTLD_NOW | RTLD_DEEPBIND);
if (handle == NULL) {
printf ("error at dlopen(): %s\n", dlerror ());
exit (EXIT_FAILURE);
}
return handle;
}
/* load func from dyn lib"*/
static void *my_load_func (void *handle, char *func) {
void *funcptr = dlsym (handle, func);
if (funcptr == NULL) {
printf ("error at dlsym(): %s\n", dlerror ());
exit (EXIT_FAILURE);
}
return funcptr;
}
and call those functions like this:
void *libhandle;
void (*userMethod) (unsigned char *d);
libhandle = my_load_dyn(LIBUSERINPUT);
userMethod = my_load_func(libhandle, "testMethod");
(*userMethod)(d);
EDIT: here is the code from the userinput.c:
#include <stdio.h>
#include <unistd.h>
void testMethod(unsigned char *d)
{
d[0] = 'Z';
}
It is generated in my programm and also compiled and linked in the running programm