I have a shared library in which there are functions that I want to load using dlsym. However, the names of the functions are passed into the library, via an entry call. Naturally I want to use dlsym to a pointer to the function whose name is passed in. To do this I need to know the base address of the library, so I use dladdr. Here's the code (i've left out the error checking and such to make it easier to read):
Code:
void FindWithdladdr(void) { return; }
void LibEntry(char *funcname)
{
Dl_info info;
...
dladdr(&FindWithdladdr,&info);
funcptr = dlsym(info.dli_fbase,funcname);
...
}
This code actually crashes and the reason is that info.dli_fbase is not the correct address. I know this because using gdb I got the address of the library when I opened it with dlopen (it happened to be 0x87840c0). However, in info.dli_fbase I got a different address (0x4024d000). If I use gdb to put 0x87840c0 into info.dli_fbase then the code works perfectly.
Does anyone have any idea what might be going wrong?