How to know which dynamic libraries are needed by

2019-05-26 20:19发布

问题:

Is there any tool that reading the headers prints the name of the dynamic libraries required by a Linux executable to run?

I need it to know if there are some weird dependencies (i.e. not very standard) in a binary that I've just built from the source (it's the Python branch of GDB) or it's mostly statically linked. I think that would be easier than reading the makefiles...

回答1:

/usr/bin/ldd is your friend. Usage:

ldd /bin/ls

Sample output:

linux-vdso.so.1 =>  (0x00007ffd14f79000)
libselinux.so.1 => /lib/x86_64-linux-gnu/libselinux.so.1 (0x00007f2d875fc000)
libacl.so.1 => /lib/x86_64-linux-gnu/libacl.so.1 (0x00007f2d873f4000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f2d8702f000)
libpcre.so.3 => /lib/x86_64-linux-gnu/libpcre.so.3 (0x00007f2d86df1000)
libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f2d86bed000)
/lib64/ld-linux-x86-64.so.2 (0x00007f2d8781f000)
libattr.so.1 => /lib/x86_64-linux-gnu/libattr.so.1 (0x00007f2d869e8000)


回答2:

readelf -d $executable | grep 'NEEDED'

Can be used if you can't run the executable, e.g. if it was cross compiled, or if you don't trust it:

In the usual case, ldd invokes the standard dynamic linker (see ld.so(8)) with the LD_TRACE_LOADED_OBJECTS environment variable set to 1, which causes the linker to display the library dependencies. Be aware, however, that in some circumstances, some versions of ldd may attempt to obtain the dependency information by directly executing the program. Thus, you should never employ ldd on an untrusted executable, since this may result in the execution of arbitrary code.

Example:

readelf -d /bin/ls | grep 'NEEDED'

Sample ouptut:

 0x0000000000000001 (NEEDED)             Shared library: [libselinux.so.1]
 0x0000000000000001 (NEEDED)             Shared library: [libacl.so.1]
 0x0000000000000001 (NEEDED)             Shared library: [libc.so.6]

Note that libraries can depend on other libraries, so you then need:

$ locate libselinux.so.1
/lib/i386-linux-gnu/libselinux.so.1
/lib/x86_64-linux-gnu/libselinux.so.1
/mnt/debootstrap/lib/x86_64-linux-gnu/libselinux.so.1

Choose one, and repeat:

readelf -d /lib/x86_64-linux-gnu/libselinux.so.1 | grep 'NEEDED'

Sample output:

0x0000000000000001 (NEEDED)             Shared library: [libpcre.so.3]
0x0000000000000001 (NEEDED)             Shared library: [libdl.so.2]
0x0000000000000001 (NEEDED)             Shared library: [libc.so.6]
0x0000000000000001 (NEEDED)             Shared library: [ld-linux-x86-64.so.2]

And so on.

/proc/<pid>/maps for running processes

This is useful to find all the libraries currently being used by running executables. E.g.:

sudo awk '/\.so/{print $6}' /proc/1/maps | sort -u

shows all currently loaded dynamic dependencies of init (PID 1):

/lib/x86_64-linux-gnu/ld-2.23.so
/lib/x86_64-linux-gnu/libapparmor.so.1.4.0
/lib/x86_64-linux-gnu/libaudit.so.1.0.0
/lib/x86_64-linux-gnu/libblkid.so.1.1.0
/lib/x86_64-linux-gnu/libc-2.23.so
/lib/x86_64-linux-gnu/libcap.so.2.24
/lib/x86_64-linux-gnu/libdl-2.23.so
/lib/x86_64-linux-gnu/libkmod.so.2.3.0
/lib/x86_64-linux-gnu/libmount.so.1.1.0
/lib/x86_64-linux-gnu/libpam.so.0.83.1
/lib/x86_64-linux-gnu/libpcre.so.3.13.2
/lib/x86_64-linux-gnu/libpthread-2.23.so
/lib/x86_64-linux-gnu/librt-2.23.so
/lib/x86_64-linux-gnu/libseccomp.so.2.2.3
/lib/x86_64-linux-gnu/libselinux.so.1
/lib/x86_64-linux-gnu/libuuid.so.1.3.0

This method also shows libraries opened with dlopen, tested with this minimal setup hacked up with a sleep(1000) on Ubuntu 18.04.

See also:

  • Determine direct shared object dependencies of a Linux binary?
  • How can I find the dynamic libraries required by an ELF Binary in C++?
  • https://unix.stackexchange.com/questions/120015/how-to-find-out-the-dynamic-libraries-executables-loads-when-run


标签: header elf dll