Finding the load address of a shared library in Linux

gimmeamilk picture gimmeamilk · Jul 8, 2011 · Viewed 9.6k times · Source

At runtime I need to print out an address, and then find which function that address is part of. The functions are in a shared library so are not at a fixed address. My map file obviously just shows the relative offsets for each shared library func. Is it possible at runtime to query where a library has been loaded, so that I can subtract that value from my address to get the correct map file offset?

Currently I'm doing a slightly hacky approch whereby I also print out the address of one function in the library, then find that function in the map file to figure out where the load address must be. I would rather have a generic method that didn't require you to name a reference function.

(GDB is not available in my setup). Thanks.

Answer

bmargulies picture bmargulies · Jul 8, 2011

On a recent linux, you can use dl_iterate_phdr to find out the addresses of the shared libs.