Skip to content
systemcoding.dev
Toolchains

List a binary's runtime libraries with ldd and know its risks

ldd lists the shared libraries a program will load, but on glibc it can run the program, so use readelf -d for binaries you do not trust.

2 min read
Summarize in AI

ldd answers the question every "cannot open shared object file" error raises: which libraries does this program want, and where are they found?

Shell
ldd ./app
Text
	linux-vdso.so.1 (0x00007ffd5a9f2000)
	libfoo.so.1 => not found
	libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f3a1c400000)
	/lib64/ld-linux-x86-64.so.2 (0x00007f3a1c6a5000)

not found is the line you came for: the loader's search failed for libfoo.so.1. The addresses change on every run because of ASLR.

Now the part the one-line description leaves out. On glibc, ldd is a shell script that asks the dynamic loader to resolve the program, and for some binaries it does that by running them with LD_TRACE_LOADED_OBJECTS=1. The ldd man page warns that a program with a hostile interpreter or a crafted ELF can run its own code at that moment. So do not run ldd on a file you would not run.

A safe way to read the direct dependencies is to read the file instead of loading it:

Shell
readelf -d ./app | grep NEEDED
Text
 0x0000000000000001 (NEEDED)             Shared library: [libfoo.so.1]
 0x0000000000000001 (NEEDED)             Shared library: [libc.so.6]

That shows only the direct list, not the full tree, and gives no paths, because nothing was resolved. For a cross-compiled binary ldd cannot work at all, since the host cannot load a foreign architecture, and readelf or the cross toolchain's -d option is the answer. The caveat is in the ldd man page.

Share

More snippets

Systems Programming

Get the page size with sysconf(_SC_PAGESIZE)

Read the memory page size at run time with sysconf(_SC_PAGESIZE) instead of hard-coding 4096, which is wrong on arm64 kernels built with 16K or 64K pages.

1 min read

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.