Module 1 · Lesson 35 of 103

How the loader finds a library

After this lesson you can explain how ldconfig, the cache, rpath and RUNPATH decide which library the loader loads, and fix a library-not-found error.

  • Beginner to advanced
  • 6 min read
  • 8 min lab
What you will need

Ubuntu 24.04 (WSL2, VM or container); a Raspberry Pi for the embedded notes

Your place in the module
By the end you can 3 objectives
  • Explain how ldconfig builds the library cache and why LD_LIBRARY_PATH is a last resort.
  • State the loader's resolution order, from DT_RPATH and DT_RUNPATH through the cache to /lib and /usr/lib.
  • Embed an rpath with $ORIGIN so a program finds its private libraries without LD_LIBRARY_PATH.
Video coming soonHow the loader finds a library
The video for this lesson is being recorded. Read the lesson below in the meantime.
Recall first from Building from source and shared libraries
  • The source-build flow is ./configure && make && make install; --prefix sets the location and DESTDIR stages a root.
  • A shared library has three names: the real name libfoo.so.1.2.3, the soname libfoo.so.1, and the linker name libfoo.so.
  • The soname records only the major version, so a compatible minor upgrade is a drop-in replacement and a major bump signals an incompatible ABI change.
01

How the loader finds a library: ldconfig and the cache

4 min read

Once a shared library is built and installed, the loader still has to find it when a program starts. This lesson answers how ldconfig, the cache, rpath and RUNPATH decide which library the loader loads.

When a program starts, the loader reads every DT_NEEDED entry and must find a file for each. Scanning the filesystem for every name on every launch would be slow, so the loader consults a prebuilt cache at /etc/ld.so.cache. That cache maps a soname such as libfoo.so.1 to the full path of the file that provides it.

ldconfig builds the cache. It reads the list of directories in /etc/ld.so.conf and every file under /etc/ld.so.conf.d/, adds the standard /lib and /usr/lib (plus the 64-bit and multiarch equivalents), scans each for shared libraries, and writes the cache. It has a second job at the same time: it creates the soname symlinks, so installing libfoo.so.1.2.3 and running ldconfig automatically produces libfoo.so.1.

bash
sudo ldconfig                     # rebuild the cache and fix up soname symlinks
ldconfig -p | head                # print the cache contents
ldconfig -p | grep libfoo         # is libfoo.so.1 in the cache, and where
ldconfig builds the cache and fixes up soname symlinks
learner@embeddex:~/lab$ # Rebuild the cache and create the libfoo.so.1 symlink
learner@embeddex:~/lab$ sudo ldconfig
learner@embeddex:~/lab$ # Print the top of the cache contents
learner@embeddex:~/lab$ ldconfig -p | head
225 libs found in cache `/etc/ld.so.cache'
        libzstd.so.1 (libc6,x86-64) => /lib/x86_64-linux-gnu/libzstd.so.1
        libz.so.1 (libc6,x86-64) => /lib/x86_64-linux-gnu/libz.so.1
        libxxhash.so.0 (libc6,x86-64) => /lib/x86_64-linux-gnu/libxxhash.so.0
        libxtables.so.12 (libc6,x86-64) => /lib/x86_64-linux-gnu/libxtables.so.12
        libxml2.so.2 (libc6,x86-64) => /lib/x86_64-linux-gnu/libxml2.so.2
        libwrap.so.0 (libc6,x86-64) => /lib/x86_64-linux-gnu/libwrap.so.0
        libuv.so.1 (libc6,x86-64) => /lib/x86_64-linux-gnu/libuv.so.1
        libuuid.so.1 (libc6,x86-64) => /lib/x86_64-linux-gnu/libuuid.so.1
        libutil.so.1 (libc6,x86-64) => /lib/x86_64-linux-gnu/libutil.so.1
learner@embeddex:~/lab$ # Confirm libfoo.so.1 is cached, with its full path
learner@embeddex:~/lab$ ldconfig -p | grep libfoo
        libfoo.so.1 (libc6,x86-64) => /usr/local/lib/libfoo.so.1
learner@embeddex:~/lab$

You run ldconfig after installing a library into a standard directory, after removing one, and after adding a directory to the configuration. A common workflow is to drop a file such as /etc/ld.so.conf.d/yourapp.conf containing the path /opt/yourapp/lib, then run sudo ldconfig so the loader can find libraries there.

For a library you are testing in a scratch directory, you can avoid the system cache entirely with the environment variable LD_LIBRARY_PATH. It is a colon-separated list of directories the loader checks, and it is how the earlier ldd ./app example would run without installing anything.

bash
LD_LIBRARY_PATH=. ./app           # look in the current directory first
LD_LIBRARY_PATH finds a library outside the cache
learner@embeddex:~/lab$ # Without the variable the loader cannot find libprobe.so.1
learner@embeddex:~/lab$ ./app
./app: error while loading shared libraries: libprobe.so.1: cannot open shared object file: No s
uch file or directory
learner@embeddex:~/lab$ # Point the loader at the libs/ directory
learner@embeddex:~/lab$ LD_LIBRARY_PATH=libs ./app
libprobe loaded
learner@embeddex:~/lab$ # ldd shows libprobe.so.1 now resolves inside libs/
learner@embeddex:~/lab$ LD_LIBRARY_PATH=libs ldd ./app
        linux-vdso.so.1 (0x00007f6a82ece000)
        libprobe.so.1 => libs/libprobe.so.1 (0x00007f6a82ebc000)
        libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f6a82ca5000)
        /lib64/ld-linux-x86-64.so.2 (0x00007f6a82ed0000)
learner@embeddex:~/lab$

The catch is that LD_LIBRARY_PATH is an override, not a normal mechanism. It does not rebuild the cache and it is ignored for set-user-ID and set-group-ID programs as a security measure. The next section places it correctly in the full resolution order and explains why it should be your last resort, not your default.

Going deeperInside the cache, and the multiarch twist

/etc/ld.so.cache is a binary file, which is why ldconfig -p prints it rather than you reading it with cat. ldconfig builds it by scanning the configured directories and, where several versions of a library exist, keeping the latest minor version for each soname. On Debian and Ubuntu the standard directories are not simply /lib and /usr/lib but the multiarch paths such as /lib/x86_64-linux-gnu and /usr/lib/x86_64-linux-gnu, chosen so that 32-bit and 64-bit libraries with the same soname can coexist on one machine. When you add a directory, prefer a file in /etc/ld.so.conf.d/ over editing /etc/ld.so.conf directly, and remember that ldconfig -p is your first diagnostic whenever a program reports a library missing: if the soname is not in the cache, either the library is not installed or its directory is not configured.

Run ldconfig after installing a library

The most common "library not found" after a fresh install is simply that ldconfig has not been run. If ldd app reports libfoo.so.1 => not found, install the library into a standard directory and run sudo ldconfig, then check ldconfig -p | grep libfoo. Only if the library must stay in a non-standard place should you reach for LD_LIBRARY_PATH or an rpath.

ldconfig builds the cacheldconfigreads /etc/ld.so.conf.dscanlibrarydirswrite /etc/ld.so.cacheloaderreads cacheldconfigreads /etc/ld.so.conf.dscanlibrarydirswrite /etc/ld.so.cacheloaderreads cache
ldconfig reads the configured directories, builds /etc/ld.so.cache, and the loader consults the cache at run time.
02

rpath, RUNPATH and the order of resolution

4 min read

The loader resolves each DT_NEEDED name using this order, and it stops at the first match.

Step by stepThe loader's search order
  1. Path with a slash: if the recorded name itself contains a /, it is used as a path directly.

  2. DT_RPATH: directories embedded in the program or library, but only if no DT_RUNPATH is also present.

  3. LD_LIBRARY_PATH: the colon-separated directories from the environment, skipped for set-user-ID and set-group-ID programs.

  4. DT_RUNPATH: the modern form of embedded directories, searched after LD_LIBRARY_PATH.

  5. The cache: /etc/ld.so.cache, built by ldconfig.

  6. Defaults: /lib and /usr/lib, plus the 64-bit and multiarch equivalents.

The embedded directory, called an rpath, is how a program can name a non-standard location at build time so that it runs without any environment variable. You set it with the linker option -Wl,-rpath,<dir>.

bash
gcc main.c -L. -lfoo -Wl,-rpath,/opt/yourapp/lib -o app
An embedded rpath runs a program with no LD_LIBRARY_PATH
learner@embeddex:~/lab$ # Embed a hard-coded rpath into /opt/yourapp/lib
learner@embeddex:~/lab$ gcc main.c -L. -lfoo -Wl,-rpath,/opt/yourapp/lib -o app
learner@embeddex:~/lab$ # The rpath makes the program run with no environment variable
learner@embeddex:~/lab$ ./app
libfoo loaded
learner@embeddex:~/lab$ # Ubuntu 24.04 records the modern RUNPATH tag
learner@embeddex:~/lab$ readelf -d app | grep -E 'RPATH|RUNPATH'
 0x000000000000001d (RUNPATH)            Library runpath: [/opt/yourapp/lib]
learner@embeddex:~/lab$ # Use $ORIGIN to ship libraries in ../lib beside the program
learner@embeddex:~/lab$ gcc main.c -L. -lfoo -Wl,-rpath,'$ORIGIN/../lib' -o bin/app
learner@embeddex:~/lab$ # $ORIGIN expands to the program directory, so it runs too
learner@embeddex:~/lab$ bin/app
libfoo loaded
learner@embeddex:~/lab$

There is a historical subtlety in the two rpath flavours. The original ELF tag was DT_RPATH, which the loader checks before LD_LIBRARY_PATH. The newer tag DT_RUNPATH is checked after it. Upstream GNU ld emits the old DT_RPATH for a plain -rpath request unless you add --enable-new-dtags, but many distributions change that default. On Ubuntu 24.04, gcc ... -Wl,-rpath,/opt/x produces RUNPATH, as readelf -d app | grep -E 'RPATH|RUNPATH' shows; check your own toolchain the same way, because a cross toolchain may behave differently. The practical consequence: with an old DT_RPATH, the embedded path wins over your environment variable; with DT_RUNPATH, your environment variable wins over the embedded path. The special token $ORIGIN expands to the directory containing the program, which lets you ship an application with its libraries in a subdirectory next to it, a common pattern for self-contained embedded apps.

bash
gcc main.c -L. -lfoo -Wl,-rpath,'$ORIGIN/../lib' -o app

This is why LD_LIBRARY_PATH is a last resort rather than a normal tool. It can shadow the system library with a private copy, silently changing what a program loads, and it is ignored for privileged set-user-ID programs precisely because that shadowing is an attack vector. It does not record anything, so it must be set again for every shell and every run, which makes it brittle in scripts and services. The professional order of preference is: install the library properly and run ldconfig; if it must live in a custom location, embed an rpath with $ORIGIN; and only for a quick test or a one-off override use LD_LIBRARY_PATH.

Going deeperset-user-ID, AT_SECURE, and why the override is ignored

When the kernel runs a program whose set-user-ID bit is set, it marks the process as "secure" using a flag the dynamic linker reads as AT_SECURE. In that mode the loader deliberately ignores LD_LIBRARY_PATH and related variables such as LD_PRELOAD, because a user who can point a privileged program at a private library could make it run arbitrary code as another user. sudo adds its own layer: by default it resets the environment and drops LD_ variables before running the command, so LD_LIBRARY_PATH=... sudo cmd does not pass the variable through either. When you diagnose a privileged program that ignores your LD_LIBRARY_PATH, this is the reason, and the fix is a proper install plus ldconfig, not a bigger override.

The loader's search ordername has aslashuse as pathDT_RPATHif noRUNPATHLD_LIBRARY_PATHenvoverrideDT_RUNPATH/etc/ld.so.cache/lib,/usr/libname has aslashuse as pathDT_RPATHif noRUNPATHLD_LIBRARY_PATHenvoverrideDT_RUNPATH/etc/ld.so.cache/lib,/usr/lib
The loader's search order from the embedded path and environment variable to the cache and the default directories.

Key takeaways

  • ldconfig rebuilds /etc/ld.so.cache and the soname symlinks; the loader searches rpath, LD_LIBRARY_PATH, the cache, then /lib and /usr/lib.
  • LD_LIBRARY_PATH is a last resort override, ignored for set-user-ID programs, and never a substitute for a proper install.
  • The loader stops at the first match, checking DT_RPATH, then LD_LIBRARY_PATH, then DT_RUNPATH, then the cache, then /lib and /usr/lib.
  • Run ldconfig after installing a library, and use ldconfig -p as the first diagnostic when a program reports a missing library.
Reflect
  1. Explain to a colleague, in two sentences: explain how ldconfig builds the library cache and why LD_LIBRARY_PATH is a last resort.
  2. Which objective are you least sure about? Re-read that section, then do its exercise in the practice pack.
  3. What surprised you when you ran the commands yourself?
On a real board

Repeat the key commands on your Raspberry Pi or lab board over SSH or the serial console. Note anything that behaves differently from your PC, such as missing tools, a BusyBox shell or a read-only filesystem.

References and further reading

Official documentation first. Links checked when the lesson was published.

Find your next step

↑ ↓ to move · Enter to open · Esc to close