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.
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.
Shared Libraries are loaded by the executable (or other shared library) at runtime.
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.
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.
On a recent linux, you can use dl_iterate_phdr
to find out the addresses of the shared libs.
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With