You can use the proc filesystem to query shared libraries using /proc/<pid>/maps
. Consult the full reference for proc which shows some sample output that you can parse:
address perms offset dev inode pathname
...
35b1800000-35b1820000 r-xp 00000000 08:02 135522 /usr/lib64/ld-2.15.so
35b1a1f000-35b1a20000 r--p 0001f000 08:02 135522 /usr/lib64/ld-2.15.so
35b1a20000-35b1a21000 rw-p 00020000 08:02 135522 /usr/lib64/ld-2.15.so
35b1a21000-35b1a22000 rw-p 00000000 00:00 0
35b1c00000-35b1dac000 r-xp 00000000 08:02 135870 /usr/lib64/libc-2.15.so
35b1dac000-35b1fac000 ---p 001ac000 08:02 135870 /usr/lib64/libc-2.15.so
35b1fac000-35b1fb0000 r--p 001ac000 08:02 135870 /usr/lib64/libc-2.15.so
35b1fb0000-35b1fb2000 rw-p 001b0000 08:02 135870 /usr/lib64/libc-2.15.so
Taking the unique set of pathnames in the 6th column will give you all the actual shared libraries loaded in the given process.
Note that this will only give you the shared libraries that are active in the process at the time of invocation, and if you call this at startup, you will get the shared libraries that were resolved by the loader using the NEEDED
entries in the ELF. If the tool uses dyld
you will need to check the list again.
Note that if you are doing this for security purposes, the shared libraries will already be loaded by the time you check this list, so any potentially nefarious actions could have already occurred and been covered up. If you want to check shared libs for security reasons, you would need to parse the ELF NEEDED
entries and then resolve using PATH
from the environment.