How to Use LDD Command in Linux

Wondering how to use the LDD command in Linux?

Linux offers ample flexibility as it allows you to tune your system according to your needs. That means you can easily operate your system via the Terminal. Moreover, you can control various parameters and keep a check on the files shared and used by different programs on your system. 

One command, in particular, enables you to recognize the libraries used by a given program under inspection. This is the LDD command developed by Roland McGrath and Ulrich Drepper.

LDD (List Dynamic Dependencies) facilitates the user to identify the library dependencies that are being shared across various executable files.

This guide will help you learn how to use the LDD command in Linux.

So without further ado, let’s begin!

Types of Libraries.

A library is a well-defined set of functions, objects, variables, classes, type specifications, conditions, and subroutines that dictate the operations of files and programs, without having to maintain the source code.

There are three types of libraries:

  1. Dynamic Libraries: These are libraries used by programs that are compiled on a smaller scale. As the name implies, it is dynamically loaded at the time of execution, so a single copy is dynamically used to run multiple programs.

This reduces the memory consumption, thereby minimizing program size and improving performance. They usually have “.so”  extensions.

  1. Static Libraries: These libraries are used by complete programs. They link programs without recompiling the source code, thereby saving time. These libraries are separately included in the program files that require its functions. They are usually followed by a “.a extension.
  2. Shared Libraries: These are individually-installed libraries that are automatically used by programs upon installment. They may override specific parts of a pre-existing library when the program is performing certain tasks. Plus, they may be used at runtime with our code or a program.

Most executable files we use on a day-to-day basis contain shared libraries. LDD command lists the dynamic library dependencies. These libraries are reused and shared across programs to ease the development of programs and reduce their size and load on the computer’s memory.

In Linux, these files are in the form of a “.so extension, and they are stockpiled in /usr/lib* or simply /lib*.

Various distributions of Linux systems may package a different set of libraries for a program, making it work differently on different versions of the operating system. When a shared library for a program is not available on your system, you get an error that may look like this:

Sudo: error while loading shared libraries: libpthread.so.0:
Cannot open shared object file: no such file or directory.

We can then check the shared libraries for a particular program using the LDD command.

Installing the LDD Command.

The LDD command is inherently available in nearly every Linux distribution. In case it is not, you can use the following command to install it:

$ sudo apt-get install libc-bin

The $ ldd command has the following syntax:

$ ldd <options> <executable>

Or more generally:

$ ldd -v /path/to/program/executable

LDD is a basic, yet powerful command. It offers help and other variations using different flags. They are briefly described below.

–version: To display the ldd version currently in use.

-v --verbose: To display info intricately.

-u --unused: To display unused dependencies.

-d --data-relocs: To relocate and identify missing objects.

-r --function-relocs: To relocate data objects and functions and identify missing functions or objects.

-help: To provide help regarding command usage.

Using the LDD Command.

Now that you have completed the installation, you are ready to learn how to use the LDD command in Linux. We will demonstrate this along with how it helps identify shared libraries.

We will look into the shared libraries for Bash, the standard shell on Linux. It is responsible for executing commands and providing an interface between user and OS.

To use the LDD command, follow these steps:

  1. Start by opening the Command Terminal by pressing Ctrl + Alt + T on your keyboard.
  2. Next, locate the path using the which or whereis command followed by the LDD command by typing the following:
$ which bash
$ whereis bash
$ ldd /usr/bin/bash
LDD Command in Linux

As you can see, the shared library dependencies are visible. For a detailed and more intricate overview, use the -v flag with the $ ldd command as follows:

$ ldd -v /usr/bin/bash
LDD Command in Linux

Similarly, we can use the -u flag to display the unused dependencies.

$ ldd -u /usr/bin/bash
LDD Command in Linux

In the case shown above, there are no unused dependencies. It is always advised to keep tabs on which libraries are idle and consuming memory.  Failing to do so may load up your kernel with unused files, consequently hindering performance. It would be helpful to check out our article on how to free space on Ubuntu.

For the data and function relocations, we use the -d and -r  flags respectively alongside the $ ldd command.

$ ldd -d /usr/bin/bash
$ ldd -r /usr/bin/bash
LDD Command in Linux

These flags also help identify missing objects and functions.

Last, but not least, we can use the --help flag for a quick consult regarding the flags and their respective actions.

LDD Command in Linux

Limitations of the LDD Command.

It should be noted that the LDD command won’t work on programs that are not dynamically executable, “a.out” shared libraries or files that are outdated or were developed before the LDD came out.

Finally, it is advised that LDD commands should not be used with untrusted sources as it can result in abrupt errors and exceptions.

The manual page for LDD suggests an alternative command which is composed of objdump and grep utilities.

user@home ~/$objdump -p /path/program/grep NEEDED

LDD is one of those undermined instructions that, if used the right way, can make a substantial difference in your interaction with the OS. It is a powerful linking command with a specific purpose, and it definitely delivers. For more details on the command and its usage, you can head to the man page for the LDD command.

We hope this guide helped you learn how to use the LDD command in Linux. If you are having any troubles understanding something, or if you have suggestions on how to make our guides better, let us know in the comment section down below.

Author

Leave a Reply

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