Using ldd to Display Shared Library Dependencies (with examples)

Using ldd to Display Shared Library Dependencies (with examples)

The ldd command is a useful tool for displaying the shared library dependencies of a binary. It provides information about which dynamic libraries a given binary depends on. This can be helpful in troubleshooting issues related to missing or incompatible libraries.

Let’s explore some different use cases of the ldd command, along with code examples, motivations, explanations for each argument, and example outputs.

1: Display shared library dependencies of a binary

ldd path/to/binary

Motivation: This use case allows us to quickly identify the shared library dependencies of a binary. By running ldd on a binary, we can determine which dynamic libraries the binary relies on.

Explanation for the argument:

  • path/to/binary: The path to the binary for which we want to display the shared library dependencies.

Example output:

	linux-vdso.so.1 (0x00007fffbafdf000)
	libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f2e6216c000)
	/lib64/ld-linux-x86-64.so.2 (0x00007f2e62503000)

2: Display all information about dependencies

ldd --verbose path/to/binary

Motivation: With this use case, we can obtain more detailed information about the shared library dependencies. The --verbose option displays additional information, such as the version of each library and the library search path.

Explanation for the argument:

  • --verbose: Provides more detailed information about the dependencies.
  • path/to/binary: The path to the binary for which we want to display the shared library dependencies.

Example output:

	linux-vdso.so.1 (0x00007fffbafdf000)
	libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f2e6216c000)
	/lib64/ld-linux-x86-64.so.2 (0x00007f2e62503000)

Version information:
/path/to/binary:
	libc.so.6 (GLIBC_2.2.5) => /lib/x86_64-linux-gnu/libc.so.6

3: Display unused direct dependencies

ldd --unused path/to/binary

Motivation: This use case helps identify any unused direct dependencies of a binary. It shows which libraries are listed as dependencies but are not actually used by the binary. This information can be valuable for optimizing and cleaning up dependencies.

Explanation for the argument:

  • --unused: Displays the unused direct dependencies.
  • path/to/binary: The path to the binary for which we want to display the shared library dependencies.

Example output:

	libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f2e6216c000)
	/lib64/ld-linux-x86-64.so.2 (0x00007f2e62503000)

4: Report missing data objects and perform data relocations

ldd --data-relocs path/to/binary

Motivation: This use case is useful when we suspect that a binary might have missing data objects or when we want to perform data relocations. Running ldd with the --data-relocs option reports any missing data objects and performs the necessary relocations.

Explanation for the argument:

  • --data-relocs: Reports missing data objects and performs data relocations.
  • path/to/binary: The path to the binary for which we want to display the shared library dependencies.

Example output:

	ERROR: /lib/x86_64-linux-gnu/libc.so.6: version `GLIBC_2.32' not found
		(required by path/to/binary)

5: Report missing data objects and functions, and perform relocations for both

ldd --function-relocs path/to/binary

Motivation: When dealing with a binary that relies on both data objects and functions from shared libraries, this use case allows us to report any missing data objects or functions. It performs relocations for both types of dependencies, ensuring that the binary can execute properly.

Explanation for the argument:

  • --function-relocs: Reports missing data objects and functions and performs relocations for both.
  • path/to/binary: The path to the binary for which we want to display the shared library dependencies.

Example output:

	ERROR: /lib/x86_64-linux-gnu/libc.so.6: version `GLIBC_2.32' not found
		(required by path/to/binary)

By understanding and utilizing these different use cases of the ldd command, we can effectively analyze and troubleshoot shared library dependencies in a binary. Whether we need to identify dependencies, obtain more detailed information, find unused dependencies, or handle missing data objects and functions, ldd provides valuable insights for ensuring the proper functioning of binaries.

Tags :

Related Posts

How to use the command 'laydown' (with examples)

How to use the command 'laydown' (with examples)

The ’laydown’ command is a command line application designed to help users prepare for their daily standup meetings.

Read More
How to use the command 'systemd-id128' (with examples)

How to use the command 'systemd-id128' (with examples)

The systemd-id128 command is used to generate and print sd-128 identifiers.

Read More
How to use the command `k6` (with examples)

How to use the command `k6` (with examples)

k6 is an open source load testing tool and SaaS (Software as a Service) platform designed for engineering teams.

Read More