开发者

how can shared library get its own base address

I have the offset address's of all symbols (obtained with libelf executing on its own binary .so). Now, at runtime, I would need to calculate the absolutue address's of all those symbols and for that I would need to get the base address (where the shared library is loaded开发者_StackOverflow中文版) and do a calculation:

symbol_address = base_address + symbol_offset

How can a shared lib get its own base address? On Windows I would use the parameter passed to DllMain, is there some equivalent in linux?


On Linux, dladdr() on any symbol from libfoo.so will give you

  void *dli_fbase;      /* Load address of that object */

More info here.

Alternatively, dl_iterate_phdr can give you load address of every ELF image loaded into current process.

Both are GLIBC extensions. If you are not using GLIBC, do tell what you are using, so more appropriate answer can be given.


This is an old question, but still relevant.

I found this example code from ubuntu to be very useful. It will print all your shared libraries and their segments.

http://manpages.ubuntu.com/manpages/bionic/man3/dl_iterate_phdr.3.html


After some research I managed to find out the method of discovering the address of the library loading by its descriptor, which is returned by the dlopen() function. It is performed with the help of such macro:

#define  LIBRARY_ADDRESS_BY_HANDLE(dlhandle) ((NULL == dlhandle) ? NULL :  (void*)*(size_t const*)(dlhandle))
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜