ldd
is a command-line utility in Unix-like operating systems, including Linux, that prints the shared libraries required by a given executable or shared object file. It is particularly useful for diagnosing issues related to missing dependencies or to understand what libraries a program depends on.
Basic Syntax:
ldd <executable-or-shared-object>
Example Usage:
- Check the Shared Libraries of an Executable:
ldd /bin/ls
Output:
linux-vdso.so.1 (0x00007fff5c9fe000)
libselinux.so.1 => /lib/x86_64-linux-gnu/libselinux.so.1 (0x00007f5b3b2a2000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f5b3aec2000)
/lib64/ld-linux-x86-64.so.2 (0x00007f5b3b505000)
This output shows the shared libraries that /bin/ls
depends on and where they are located on the system.
- Check the Shared Libraries of a Custom Program:
Suppose you compiled a custom program calledmyprogram
:
ldd ./myprogram
Output might look like:
linux-vdso.so.1 (0x00007ffe3d1e3000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f0c59312000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f0c58f21000)
/lib64/ld-linux-x86-64.so.2 (0x00007f0c5955b000)
This shows which shared libraries myprogram
requires and their locations.
Interpreting ldd
Output:
- Shared Library Path: The arrow (
=>
) points to the actual file path of the shared library on the system. - Not Found: If
ldd
reportsnot found
for any libraries, it indicates that the required shared library is missing from the system’s library path, and the program will not run correctly until the missing library is installed.
Common Uses:
- Troubleshooting:
ldd
is commonly used to troubleshoot issues where a program fails to start due to missing dependencies. By identifying which libraries are missing, you can install the necessary packages. - Dependency Checking: Developers and system administrators can use
ldd
to verify that all required libraries are available before deploying software on a new system. - Security Auditing:
ldd
can help identify which libraries a program is linked against, which is useful in security auditing and vulnerability assessments.
Important Notes:
- Security Warning: Be cautious when using
ldd
on untrusted executables, as it might execute the code in the executable. For safer alternatives, consider usingobjdump
orreadelf
. - Dynamic Linking:
ldd
only shows the libraries dynamically linked to an executable. Statically linked libraries, where the library code is included directly in the executable, will not be listed byldd
.
Advanced Usage:
- Verbose Output: While
ldd
itself does not have a verbose option, combining it with tools likestrace
can provide more detailed information about how an application loads its libraries. - Cross-Architecture: If you are working with binaries for a different architecture (e.g., ARM on x86_64), use the appropriate
ldd
version or set up a chroot or container environment to analyze dependencies.
ldd
is a valuable tool for understanding the dependencies of executables and shared libraries in Unix-like systems, making it essential for troubleshooting, deployment, and security auditing tasks.