devutils/ATFQ/book ATFQ git

Part II · Your system

23.1 Dynamic linking and ldd

alsolddshared libraryshared libraries.sold.soldconfigld_library_patherror while loading shared librariesdynamic linking

TL;DR

  • Most programs don't contain the libraries they use: at start-up, the dynamic loader finds the .so files they need and loads them.
  • ldd program lists those libraries and where each was found. "not found" marks the one that's missing.
  • The loader searches LD_LIBRARY_PATH first, then the folders ldconfig knows (from /etc/ld.so.conf.d/), then the standard ones.
ldd /usr/bin/curl
ldconfig -p | grep libssl

On your machine

alice@laptopexample
$ atfq 23.1
  PATH      /home/alice/.local/bin
            /usr/local/bin (doesn't exist)
            /usr/bin
            . ⚠ the current folder, wherever you are
  Repeated  /usr/bin (listed more than once: harmless, the first one wins)
  Libraries LD_LIBRARY_PATH=/opt/oracle/lib
  Runs      programs built for x86-64, glibc

An example: Alice's laptop, one of the test machines. In your terminal, this block shows your own machine, read live.

Pitfalls

Deep dive 2 questions

Why link dynamically?

One copy of a library serves every program, on disk and in RAM, and a security fix in the library fixes all of them at once. Static linking puts everything in the program: easier to copy to another machine, but bigger, and every program must be rebuilt to get a fix. Go programs are often linked that way.

What is the number in libfoo.so.1?

The major version, part of the library's "soname". A program asks for libfoo.so.1; the file is often libfoo.so.1.2.3, and libfoo.so.1 is a link to it. A new major version (.so.2) means an incompatible change, and it can be installed next to the old one.

Commands
ldd ldconfig objdump
Paths
/etc/ld.so.conf.d/
See also
23.2 ELF and "command not found"
21.4 syscalls and strace