I was trying call a function that complied with the host ELF executable file.
Here is the sample code:
#include <stdio.h>
#include <dlfcn.h>
// or with some tricks
// void __attribute__((visibility("default")))
void test_func(void)
{
printf("test_func in %s n",__FILE__);
}
int main()
{
void *handle = dlopen(0, RTLD_LAZY); // or RTLD_GLOBAL
printf("dlopen return %pn", handle);
void (*func)() = dlsym(handle, "test_func");
printf("dlsym return %pn", func);
if (func) (*func)();
return 0;
}
It failed with output like this:
$ ./a.out
dlopen return 0xe29dfbec1c7bc0d
dlsym return 0x0
I can read the symbol in the ELF. It hits my head that it must be some way to get the test_func
function and call it.
$ readelf -s a.out | grep test_func
40: 00000000000017c8 36 FUNC GLOBAL DEFAULT 13 test_func
Is there something I missing? Can it also work in Windows?