Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding the load address of a shared library in Linux

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.

like image 362
gimmeamilk Avatar asked Jul 08 '11 13:07

gimmeamilk


People also ask

How do you check if a shared library is loaded in Linux?

To find the list of processes and their loaded libraries, use "genld -ld" command. The -l option reports the lists of loaded objects for each process running on the system.

Where are shared libraries loaded?

Shared Libraries are loaded by the executable (or other shared library) at runtime.

How do I see shared libraries in Linux?

We know that a shared library is a library that can be linked to any program at runtime. In order to view all the shared libraries used by an executable we make use of the Linux command utility known as ldd. We can easily locate the shared libraries on a Linux machine, as they usually start with lib* prefix.

How do I find the library path in Linux?

By default, libraries are located in /usr/local/lib, /usr/local/lib64, /usr/lib and /usr/lib64; system startup libraries are in /lib and /lib64. Programmers can, however, install libraries in custom locations. The library path can be defined in /etc/ld.


2 Answers

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

like image 101
bmargulies Avatar answered Oct 13 '22 05:10

bmargulies


dladdr does this, end of. :-)

More comprehnsively, dladdr will take an address and work out which library and symbol it corresponds to... and then it'll give you the name of the library, the name of the symbol, and the base addresses of each. Personally I think that's nifty, it's also making my current debugging job a lit easier.

Hope this helps!

like image 30
scruffy_brit Avatar answered Oct 13 '22 05:10

scruffy_brit