Why ldd is unsafe on untrusted binaries
Inspect ELF dependency metadata with readelf or objdump instead of invoking the loader. Use LD_DEBUG only when you intentionally execute a trusted program.
Use readelf -d or objdump -p to inspect an unfamiliar ELF binary’s declared dependencies. Reserve ldd for binaries you trust. Its usual implementation invokes the dynamic loader in trace mode; it is not simply a file-format reader. Historical implementations and unusual ELF interpreters can cause target code to execute. The ldd manual explicitly warns against using it on untrusted executables.
Inspect without starting the program
readelf -d ./unknown-program
objdump -p ./unknown-program
Look for NEEDED entries, along with RPATH and RUNPATH search metadata. These commands parse the file rather than deliberately running its entry point or interpreter. Keep parsing tools current and isolate actively hostile files: parsing complex formats is still attack surface.
Here is a trusted, reproducible example. Save it as hello.c:
#include <stdio.h>
int main(void) { puts("hello"); return 0; }
gcc hello.c -o hello
readelf -d ./hello
objdump -p ./hello
Example Linux output, trimmed:
0x0000000000000001 (NEEDED) Shared library: [libc.so.6]
NEEDED libc.so.6
The first line is from readelf; the second is from objdump. GNU readelf documents -d as the dynamic-section view; GNU objdump documents the target-specific private headers selected by -p.
A NEEDED name is not a resolved path. It lists direct dependencies only, not everything those libraries require. A library loaded later through dlopen may not appear at all. To inspect a dependency tree without execution, the Gentoo pax-utils lddtree implementation recursively parses ELF metadata. If installed, run lddtree ./unknown-program. Its resolution reflects the filesystem and configuration it sees, which may differ from the production container.
Trace mode is still loader execution
For a trusted binary, this requests the loader’s dependency trace:
LD_TRACE_LOADED_OBJECTS=1 ./hello
It is not a safer substitute for ldd on an unknown binary. You still ask the kernel to execute the file using its declared interpreter. A malicious interpreter need not respect that variable. Behavior also depends on the platform’s loader; these examples concern Linux ELF and glibc.
Find the library that supplies a symbol
For the trusted example, request symbol lookup and binding diagnostics:
LD_DEBUG=symbols,bindings LD_BIND_NOW=1 ./hello 2>bindings.log
rg puts bindings.log
Real output, trimmed to the selected binding:
3691661: binding file ./hello [0] to /usr/lib/x86_64-linux-gnu/libc.so.6 [0]: normal symbol `puts' [GLIBC_2.2.5]
This says the running program bound puts to the displayed libc object, including the symbol version. symbols shows lookup attempts; bindings shows the selected definition. LD_BIND_NOW=1 requests eager resolution, so lazy calls do not remain unresolved until first use. This command executes the application and its initialization code. The glibc loader manual also describes secure-execution restrictions on these environment variables.
Go deeper
The linking dive explains the distinction between ELF metadata, loading a library and applying symbol relocations.