8 LDD Command Examples in Linux

LDD Command Linux

ldd is a Linux command line utility that is used in case a user wants to know the shared library dependencies of an executable or even that of a shared library. You might have noticed many files starting with lib* in /lib and /usr/lib directories of your Linux machine. These files are called libraries. Library is a collection of resources such as subroutines/functions, classes, values or type specifications. A library makes it possible for a program to use common routines without the administrative overhead of maintaining their source code, or the processing overhead of compiling them each time the program is compiled.

There are two types of libraries:

Static libraries: static libraries for complete programs that do not depend on external libraries to run. The feature of statically linked programs is that they work without installing any prerequisites. The static library is end with *.a extension and these libraries are included (a separate copy) into the programs that require its functions.

Dynamic libraries: dynamic libraries for tiny programs in size, These libraries end with .so extension, Another feature of using dynamic linking when many programs are running, It can share one copy of a library rather than occupying memory with many copies of the same code. So the recent programs use dynamic linking. In this article, we will go through the commands ldd which is used to manage the shared libraries.

Shared libraries

When we make a program, we need many pieces of code that someone else has already written to perform routine or specialized functions for us. These pieces of code are stored in shared libraries. To use them, we link them with our code, either when we build the program or when we run the program.

Syntax and Options

The ldd command prints shared object dependencies. The command's syntax is:

ldd [OPTION]... FILE...

We can use ldd command switches that can be inserted into the
[OPTION] spot in the above command:

  • -v : Print all information.
  • -d : process data relocation.
  • -r : process data and function relocation.
  • -u : print unused direct dependencies.

Please note down the following points before going through the command:

- The file, ld-linux.so is the dynamic linker or loader which checks for the desired link or library cache for the requested program and loads it.

- The cache file, /etc/ld.so.cache contains a list of libraries found in the directories specified in /etc/ld.so.conf. This helps to provide faster dynamic linking.

- The file /etc/ld.so.conf specifies the directories where to search for libraries

1) Display dependencies of the command

We will display the dependencies of cp command.

$ ldd /bin/cp
    Output:
    linux-vdso.so.1 =>  (0x00007fffaf3ff000)
    libselinux.so.1 => /lib64/libselinux.so.1 (0x0000003a06a00000)
    librt.so.1 => /lib64/librt.so.1 (0x0000003a06200000)
    libacl.so.1 => /lib64/libacl.so.1 (0x0000003a13000000)
    libattr.so.1 => /lib64/libattr.so.1 (0x0000003a0ea00000)
    libc.so.6 => /lib64/libc.so.6 (0x0000003a05200000)
    libdl.so.2 => /lib64/libdl.so.2 (0x0000003a05a00000)
    /lib64/ld-linux-x86-64.so.2 (0x0000003a04a00000)
    libpthread.so.0 => /lib64/libpthread.so.0 (0x0000003a05600000)

2) Display dependencies of the command with details

We will display the dependencies of cp command with more details using -v option.

$ ldd -v /bin/cp
    Output:
     linux-vdso.so.1 =>  (0x00007fff473ff000)
        libselinux.so.1 => /lib64/libselinux.so.1 (0x0000003a06a00000)
        librt.so.1 => /lib64/librt.so.1 (0x0000003a06200000)
        libacl.so.1 => /lib64/libacl.so.1 (0x0000003a13000000)
        libattr.so.1 => /lib64/libattr.so.1 (0x0000003a0ea00000)
        libc.so.6 => /lib64/libc.so.6 (0x0000003a05200000)
        libdl.so.2 => /lib64/libdl.so.2 (0x0000003a05a00000)
        /lib64/ld-linux-x86-64.so.2 (0x0000003a04a00000)
        libpthread.so.0 => /lib64/libpthread.so.0 (0x0000003a05600000)

        Version information:
        /bin/cp:
                librt.so.1 (GLIBC_2.2.5) => /lib64/librt.so.1
                libattr.so.1 (ATTR_1.1) => /lib64/libattr.so.1
                libacl.so.1 (ACL_1.2) => /lib64/libacl.so.1
                libacl.so.1 (ACL_1.0) => /lib64/libacl.so.1
                libc.so.6 (GLIBC_2.6) => /lib64/libc.so.6
                libc.so.6 (GLIBC_2.4) => /lib64/libc.so.6
                libc.so.6 (GLIBC_2.3) => /lib64/libc.so.6
                libc.so.6 (GLIBC_2.3.4) => /lib64/libc.so.6
                libc.so.6 (GLIBC_2.2.5) => /lib64/libc.so.6
        /lib64/libselinux.so.1:
                libdl.so.2 (GLIBC_2.2.5) => /lib64/libdl.so.2
                ld-linux-x86-64.so.2 (GLIBC_2.3) => /lib64/ld-linux-x86-64.so.2
                libc.so.6 (GLIBC_2.4) => /lib64/libc.so.6
                libc.so.6 (GLIBC_2.8) => /lib64/libc.so.6
                libc.so.6 (GLIBC_2.3) => /lib64/libc.so.6
                libc.so.6 (GLIBC_2.3.4) => /lib64/libc.so.6
                libc.so.6 (GLIBC_2.2.5) => /lib64/libc.so.6
        /lib64/librt.so.1:
                libpthread.so.0 (GLIBC_2.2.5) => /lib64/libpthread.so.0
                libpthread.so.0 (GLIBC_PRIVATE) => /lib64/libpthread.so.0
                libc.so.6 (GLIBC_2.3.2) => /lib64/libc.so.6
                libc.so.6 (GLIBC_2.2.5) => /lib64/libc.so.6
                libc.so.6 (GLIBC_PRIVATE) => /lib64/libc.so.6
        /lib64/libacl.so.1:
                libattr.so.1 (ATTR_1.0) => /lib64/libattr.so.1
                libc.so.6 (GLIBC_2.4) => /lib64/libc.so.6
                libc.so.6 (GLIBC_2.3.4) => /lib64/libc.so.6
                libc.so.6 (GLIBC_2.2.5) => /lib64/libc.so.6
        /lib64/libattr.so.1:
                libc.so.6 (GLIBC_2.4) => /lib64/libc.so.6
                libc.so.6 (GLIBC_2.2.5) => /lib64/libc.so.6
        /lib64/libc.so.6:
                ld-linux-x86-64.so.2 (GLIBC_PRIVATE) => /lib64/ld-linux-x86-64.so.2
                ld-linux-x86-64.so.2 (GLIBC_2.3) => /lib64/ld-linux-x86-64.so.2
        /lib64/libdl.so.2:
                ld-linux-x86-64.so.2 (GLIBC_PRIVATE) => /lib64/ld-linux-x86-64.so.2
                libc.so.6 (GLIBC_PRIVATE) => /lib64/libc.so.6
                libc.so.6 (GLIBC_2.2.5) => /lib64/libc.so.6
        /lib64/libpthread.so.0:
                ld-linux-x86-64.so.2 (GLIBC_2.3) => /lib64/ld-linux-x86-64.so.2
                ld-linux-x86-64.so.2 (GLIBC_2.2.5) => /lib64/ld-linux-x86-64.so.2
                ld-linux-x86-64.so.2 (GLIBC_PRIVATE) => /lib64/ld-linux-x86-64.so.2
                libc.so.6 (GLIBC_2.3.2) => /lib64/libc.so.6
                libc.so.6 (GLIBC_PRIVATE) => /lib64/libc.so.6
                libc.so.6 (GLIBC_2.2.5) => /lib64/libc.so.6

3) Display unused direct dependencies of the command

We can display unused direct dependencies of cp command using -u option.

$ ldd -u /bin/cp
    Output:
     Unused direct dependencies:

        /lib64/libselinux.so.1
        /lib64/librt.so.1
        /lib64/libacl.so.1
        /lib64/libattr.so.1

4) Display ldd works only on dynamic executables

We will display ldd works only on dynamic executables using -r option.

$  ldd -r /smart/pycharm-community-2017.3.3/bin/pycharm.sh
    Output:
     not a dynamic executable

The output displayed a clear message state that the supplied file is not a dynamic executable.

5) ldd with standard command line executable

When we try ldd on a standard command line executable like ls, We need full path to the dynamic executable.

$ ldd ls
    Output:
    ldd: ./ls: No such file or directory

We see that ldd states that it cannot find ls.

$ ldd /bin/ls
    Output:
    linux-vdso.so.1 =>  (0x00007fff5cbea000)
    libselinux.so.1 => /lib64/libselinux.so.1 (0x0000003a06a00000)
    librt.so.1 => /lib64/librt.so.1 (0x0000003a06200000)
    libcap.so.2 => /lib64/libcap.so.2 (0x0000003a07600000)
    libacl.so.1 => /lib64/libacl.so.1 (0x0000003a13000000)
    libc.so.6 => /lib64/libc.so.6 (0x0000003a05200000)
    libdl.so.2 => /lib64/libdl.so.2 (0x0000003a05a00000)
    /lib64/ld-linux-x86-64.so.2 (0x0000003a04a00000)
    libpthread.so.0 => /lib64/libpthread.so.0 (0x0000003a05600000)
    libattr.so.1 => /lib64/libattr.so.1 (0x0000003a0ea00000)

But with the absolute path, ldd worked fine.

6) Know a given executable daemon supports TCP Wrapper

To determine whether a given executable daemon supports TCP Wrapper or not, run the following command.

$ sudo ldd /usr/sbin/sshd | grep libwrap
    Output:
    libwrap.so.0 => /lib64/libwrap.so.0 (0x00007f1cc2ac6000)

The output indicates that the OpenSSH (sshd) daemon supports TCP Wrapper.

7) ldd with missing dependency

We can use the ldd command when an executable is failing because of a missing dependency. Once we found a missing dependency, we can install it or update the cache with the ldconfig command.

$ sudo ldd /bin/mv
libacl.so.1 => /lib/libacl.so.1 (0×40016000)
libc.so.6 => /lib/libc.so.6 (0x4001c000)
libattr.so.1 => /lib/libattr.so.1 (0×40141000)
/lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0×40000000)

We will perform relocations and report any missing objects (ELF only) by typing below command.

$ sudo ldd -d path/to/executable_file

We will perform relocations for both data objects and functions, and report any missing objects or functions (ELF only) by typing below command.

$ sudo ldd -r path/to/executable_file

Common shared library related errors

1) Missing library error

You may encounter missing library error even though the mentioned libraries are available in the new installation path “/opt/newinstall/lib”. This is because the system is not aware of this directory to check for libraries. This can be fixed in any of the two ways.

a. Execute the following command,

$ ldconfig -n /opt/newinstall/lib

b. You can see the following include line in /etc/ld.so.conf file:

include ld.so.conf.d/*.conf

So, create a file in /etc/ld.so.sonf.d folder, say newinstall.conf with the following content.

/opt/newinstall/lib

Then, execute:

$ ldconfig

2) Dynamic linker error, can’t map cache files

This may be due to the corrupted cache file. This can be resolved by rebuilding the cache file using ldconfig.

$ ldconfig

ldconfig Command

ldconfig creates the necessary links and cache (for use by the run-time linker, ld.so) to the most recent shared libraries found in the directories specified on the command line, in the file/etc/ld.so.conf, and in the trusted directories (/usr/lib and /lib).

For Examples:

Execute the following command to set up the correct links for the shared binaries and rebuild the cache.

$ ldconfig –v

Execute the following command after the installation of a new shared library will properly update the shared library symbolic links in /lib.

$ ldconfig -n /lib

The following command will print the current cache.

$ ldconfig -p

Thanks for reading my article and please leave your comments.

Read Also:

Ahmed Abdalhamid 4:39 am

About Ahmed Abdalhamid

Ahmed is communication engineer since 2001, he had a lot of skills. He has worked Linux system administrator, Python developer for 7 years and now he is Data scientist since 2015. He thinks Linux and Python are the greatest inventions of mankind.

Author's All Posts
Like to become part of Linoxide Team and contribute tips? Contact us here.

Comments

Your email address will not be published. Required fields are marked *

All comments are subject to moderation.