How to use the command 'ldd' (with examples)
- Linux
- December 17, 2024
The ldd
command in Linux is used to display the shared library dependencies of a given binary. Shared libraries are crucial components in Unix-like operating systems as they provide functionalities to various applications without requiring each app to include and compile the same code repeatedly. For developers and system administrators, understanding these dependencies is vital for debugging, optimizing, and ensuring software runs correctly across different systems. It’s important to note that ldd
should not be used on untrusted binaries due to potential security risks. When working with untrusted binaries, objdump
or similar safer methods should be utilized instead.
Use case 1: Display shared library dependencies of a binary
Code:
ldd path/to/binary
Motivation:
The primary motivation for using this command is to quickly inspect the shared libraries required by a binary. This information is indispensable when diagnosing issues related to missing libraries, ensuring that all dependencies are installed for a binary to execute successfully, or when preparing to migrate applications between environments.
Explanation:
ldd
: The command itself, indicating that we want to list the dynamic dependencies.path/to/binary
: The specific path to the binary file you wish to examine. This argument tellsldd
which application you are interested in, providing you with the list of associated shared libraries.
Example Output:
linux-vdso.so.1 => (0x00007fffbf9fe000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f7f98a4a000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f7f986b6000)
/lib64/ld-linux-x86-64.so.2 (0x00005564b6049000)
Use case 2: Display all information about dependencies
Code:
ldd --verbose path/to/binary
Motivation:
Sometimes, a basic report of shared library dependencies is insufficient. Users may need greater insight into their binary’s dependencies, especially when debugging complex issues or verifying the exact nature of the connections between different binaries and their libraries.
Explanation:
ldd
: As before, signifies the use of theldd
command.--verbose
: This option triggers detailed output, providing a comprehensive look into the binary’s dependencies.path/to/binary
: As previously mentioned, the specific binary file you are evaluating.
Example Output:
ldd (Ubuntu GLIBC 2.31-0ubuntu9.7) 2.31
ldd: mode: symbolic
linux-vdso.so.1 => (0x00007fffbf9fe000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f7f98a4a000)
... (additional verbose information)
/lib64/ld-linux-x86-64.so.2 (0x00005564b6049000)
Use case 3: Display unused direct dependencies
Code:
ldd --unused path/to/binary
Motivation:
Modern software applications may include many dependencies that are not actively used but still present due to broad linking or programming convenience. Identifying and possibly removing these unused dependencies can significantly streamline processes and reduce security risks associated with unnecessary bloat.
Explanation:
ldd
: The primary command for listing dynamic dependencies.--unused
: This switch is employed to filter output to only include dependencies that exist but aren’t utilized directly by the binary.path/to/binary
: Specifies the particular binary being scrutinized.
Example Output:
Unused direct dependencies:
/lib/x86_64-linux-gnu/libunused.so.0
Use case 4: Report missing data objects and perform data relocations
Code:
ldd --data-relocs path/to/binary
Motivation:
In the process of linking and executing binaries, data relocations must sometimes be performed to adapt pointers within the code, ensuring that all external resources and libraries are correctly addressed. Tracking missing data objects and performing these relocations helps to mitigate runtime errors and stabilize execution environments.
Explanation:
ldd
: Again, the command used to display dependencies.--data-relocs
: This flag activates the capability to report any missing data objects and automatically execute necessary relocations to address these deficiencies.path/to/binary
: The target binary for relocation and missing object reporting.
Example Output:
/lib/x86_64-linux-gnu/libblah.so.0 => not found (data relocation failed)
/lib/x86_64-linux-gnu/libfoo.so.0 => not found (data relocation required)
Use case 5: Report missing data objects and functions, and perform relocations for both
Code:
ldd --function-relocs path/to/binary
Motivation:
Beyond data objects, functions within dynamically loaded shared libraries also require proper linking to function correctly. This use case ensures that both data and function relocations are addressed, providing a comprehensive approach to dependencies management and preventing function-related runtime errors.
Explanation:
ldd
: The command remains consistent in listing dependencies.--function-relocs
: This option expands the scope to include both data and function relocations, offering a complete relocation solution.path/to/binary
: Indicates the particular binary under examination.
Example Output:
/lib/x86_64-linux-gnu/libsomefunc.so.0 => not found (function and data relocation needed)
/lib/x86_64-linux-gnu/libanother.so.0 => found but relocation incomplete
Conclusion:
The ldd
command is a powerful tool for Linux users to ensure their binaries are correctly linked with the necessary shared libraries. Each use case demonstrates a unique facet of this tool’s capabilities — from basic dependency revelations to more advanced diagnostics involving data and function relocations. By leveraging these insights, developers and system administrators can guarantee that applications run smoothly and securely across various systems.