4

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) 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?

Sasha Nikolic
  • 760
  • 1
  • 8
  • 22
  • You want a library function to calculate the address of an object in that same library? Is there a reason you can't take the address of the object like you would for a pointer assignment? – Kirk Kelsey Jul 21 '10 at 13:47
  • 1
    I need to iterate all the symbols from a binary and get their absolute address's for some further calculations, so manually accessing each object is almost impossible for bigger binaries (not to mention the symbols from crt). so now i am able to get the offsets for all symbols with libelf and just need this base address to calculate the virtual address. – Sasha Nikolic Jul 23 '10 at 07:52

3 Answers3

8

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.

Employed Russian
  • 199,314
  • 34
  • 295
  • 362
  • And how do you get its size? I have a SO here: https://stackoverflow.com/q/71182137/996540 – amanjiang Feb 22 '22 at 09:10
  • "Both are GLIBC extensions" => `dladdr()` comes from Solaris originally, and nowadays is found almost everywhere (musl, bionic, dietlibc, FreeBSD, NetBSD, OpenBSD, macOS/iOS/etc). In fact it will be in the next release of POSIX. Only major Unix platform which still lacks it is AIX. `dl_iterate_phdr` also comes from Solaris originally, and is found on most ELF-based platforms (Linux, FreeBSD, etc) but generally missing on non-ELF platforms such as macOS – Simon Kissane Aug 25 '22 at 09:53
0

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

randoms
  • 2,793
  • 1
  • 31
  • 48
-1

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))
shoumikhin
  • 1,327
  • 15
  • 23