Friday, June 6, 2014

ldd/objdump - finding shared library dependencies


What is ldd?
ldd stands for 'list dynamic dependencies'. It is a shell script that displays shared libraries required by a unix/linux program or a shared library.

ldd internals

Copy-pasted from http://linux.die.net/man/1/ldd :


In the usual case, ldd invokes the standard dynamic linker 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. A safer alternative when dealing with untrusted executables is:

$ objdump -p /path/to/program | grep NEEDED

"

LDD source code: http://stuff.mit.edu/afs/sipb/project/phone-project/bin/arm-linux-ldd
(is this latest?)

ldd command line options 
 
babu@babu-VirtualBox:~$ ldd --help
Usage: ldd [OPTION]... FILE...
      --help              print this help and exit
      --version           print version information and exit
  -d, --data-relocs       process data relocations
  -r, --function-relocs   process data and function relocations
  -u, --unused            print unused direct dependencies
  -v, --verbose           print all information
For bug reporting instructions, please see:
<https://bugs.launchpad.net/ubuntu/+source/eglibc/+bugs>.
babu@babu-VirtualBox:~$ 

Sample Usage

Here are a few sample outputs:
babu@babu-VirtualBox:~$ ldd /bin/lsmod
            linux-gate.so.1 =>  (0xb77ad000)
            libkmod.so.2 => /lib/i386-linux-gnu/libkmod.so.2 (0xb7765000)
            libc.so.6 => /lib/i386-linux-gnu/libc.so.6 (0xb75b1000)
            /lib/ld-linux.so.2 (0xb77ae000)
babu@babu-VirtualBox:~$ 

babu@babu-VirtualBox:~$ ldd /lib/i386-linux-gnu/libc.so.6
            /lib/ld-linux.so.2 (0xb76e4000)
            linux-gate.so.1 =>  (0xb76e3000)
babu@babu-VirtualBox:~$ 

In the above outputs, the first field displays the shared library name & the second field (after ==>) displays the path of the shared library. In case the shared library is not found in the system, it displays "not found" in this field.

Objdump way of finding dependencies
 
babu@babu-VirtualBox:~$ objdump -p /bin/lsmod | grep NEEDED  
  NEEDED               libkmod.so.2
  NEEDED               libc.so.6
babu@babu-VirtualBox:~$

As we can see, objdump too is useful, but it misses out other details ldd displays. So, I prefer ldd over objdump to view shared library dependencies.

References:

No comments:

UA-48797665-1