TExcelTAD
Home › Build Fundamentals: From Source to Binary › Sonames, rpath & the Loader

Sonames, rpath & the Loader

The previous lesson ended with a program that only started with LD_LIBRARY_PATH=. set. Real libraries solve two more problems: which version of a library a program needs, and where the loader should look for it. On Linux the answers are the soname and the rpath/runpath.

Three names for one library

A versioned shared library on Linux has three names:

Name Example Used by
real name (the file) libgreet.so.1.2.0 the file system; full version
soname libgreet.so.1 the loader: recorded in programs at link time; the major version
linker name libgreet.so the linker: what -lgreet looks for; a symlink for development

The soname is stored inside the library with -Wl,-soname,…. When a program links against the library, the linker copies the soname (not the file name) into the program's NEEDED list:

bash · try it yourself
mkdir -p ver/lib && cd ver
printf '#include <stdio.h>\nvoid greet(const char *w) { printf("Hello, %%s (libgreet 1.2.0)\\n", w); }\n' > greet.c
gcc -fPIC -shared -Wl,-soname,libgreet.so.1 greet.c -o lib/libgreet.so.1.2.0
readelf -d lib/libgreet.so.1.2.0 | grep SONAME
/usr/sbin/ldconfig -n lib             # creates the soname symlink
ln -s libgreet.so.1 lib/libgreet.so   # the linker name, by hand
find lib -type l -printf '%f -> %l\n' | sort
printf 'void greet(const char *);\nint main(void) { greet("versioned app"); return 0; }\n' > main.c
gcc main.c -Llib -lgreet -o app
readelf -d app | grep NEEDED
LD_LIBRARY_PATH=lib ./app
 0x000000000000000e (SONAME)             Library soname: [libgreet.so.1]
libgreet.so -> libgreet.so.1
libgreet.so.1 -> libgreet.so.1.2.0
 0x0000000000000001 (NEEDED)             Shared library: [libgreet.so.1]
 0x0000000000000001 (NEEDED)             Shared library: [libc.so.6]
Hello, versioned app (libgreet 1.2.0)

ldconfig -n <dir> reads the soname from each library in the directory and creates the libgreet.so.1 -> libgreet.so.1.2.0 link. Run as root without -n, it does the same for the system directories and rebuilds the loader cache /etc/ld.so.cache; package managers call it after installing libraries. The linker-name link is created by -dev packages, because only developers need it.

Why the soname matters: coexisting versions

Bug-fix releases keep the soname: libgreet.so.1.2.1 replaces 1.2.0, the libgreet.so.1 link moves, and every program picks up the fix. An incompatible release (removed or changed functions) bumps the soname to libgreet.so.2. Old programs still ask for .so.1, new ones for .so.2, and both libraries can be installed side by side:

bash · try it yourself
mkdir -p ver/lib && cd ver
printf '#include <stdio.h>\nvoid greet(const char *w) { printf("v1: hello, %%s\\n", w); }\n' > g1.c
printf '#include <stdio.h>\nvoid greet(const char *w, int times) { while (times--) printf("v2: hello, %%s\\n", w); }\n' > g2.c
gcc -fPIC -shared -Wl,-soname,libgreet.so.1 g1.c -o lib/libgreet.so.1.0.0
ln -s libgreet.so.1.0.0 lib/libgreet.so.1 && ln -s libgreet.so.1 lib/libgreet.so
printf 'void greet(const char *);\nint main(void) { greet("old app"); return 0; }\n' > old.c
gcc old.c -Llib -lgreet -o old-app
# the 2.0 release: new soname, new linker-name link
gcc -fPIC -shared -Wl,-soname,libgreet.so.2 g2.c -o lib/libgreet.so.2.0.0
ln -s libgreet.so.2.0.0 lib/libgreet.so.2 && ln -sf libgreet.so.2 lib/libgreet.so
printf 'void greet(const char *, int);\nint main(void) { greet("new app", 2); return 0; }\n' > new.c
gcc new.c -Llib -lgreet -o new-app
for p in old-app new-app; do printf '%s needs ' $p; readelf -d $p | grep -o 'libgreet[^]]*'; LD_LIBRARY_PATH=lib ./$p; done
old-app needs libgreet.so.1
v1: hello, old app
new-app needs libgreet.so.2
v2: hello, new app
v2: hello, new app

Without a soname, the linker records the file name instead, and replacing libgreet.so with an incompatible version silently breaks every old program. Always set a soname on a shared library you distribute. CMake does it from the VERSION and SOVERSION target properties.

rpath and runpath: telling the loader where to look

Setting LD_LIBRARY_PATH for every run is fragile. Instead, store a search path inside the executable with -Wl,-rpath,<dir>. The special token $ORIGIN means "the directory containing this executable", which makes a relocatable layout like bin/app + lib/libgreet.so.1 work wherever the folder is copied:

bash · try it yourself
mkdir -p dist/bin dist/lib && cd dist
printf '#include <stdio.h>\nvoid greet(const char *w) { printf("Hello, %%s\\n", w); }\n' > greet.c
gcc -fPIC -shared -Wl,-soname,libgreet.so.1 greet.c -o lib/libgreet.so.1
ln -s libgreet.so.1 lib/libgreet.so
printf 'void greet(const char *);\nint main(void) { greet("relocatable app"); return 0; }\n' > main.c
gcc main.c -Llib -lgreet -Wl,-rpath,'$ORIGIN/../lib' -o bin/app
readelf -d bin/app | grep -E 'NEEDED|RUNPATH'
bin/app
cd .. && mv dist moved && moved/bin/app
ldd moved/bin/app | sed 's/ (0x[0-9a-f]*)//'
 0x0000000000000001 (NEEDED)             Shared library: [libgreet.so.1]
 0x0000000000000001 (NEEDED)             Shared library: [libc.so.6]
 0x000000000000001d (RUNPATH)            Library runpath: [$ORIGIN/../lib]
Hello, relocatable app
Hello, relocatable app
	linux-vdso.so.1
	libgreet.so.1 => /home/learner/moved/bin/../lib/libgreet.so.1
	libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6
	/lib64/ld-linux-x86-64.so.2
  • Quote '$ORIGIN' in the shell, or $ORIGIN expands to an empty variable.
  • Modern GNU ld writes RUNPATH (DT_RUNPATH) by default; old toolchains wrote RPATH (DT_RPATH). The difference: RPATH is searched before LD_LIBRARY_PATH and also applies to the libraries' own dependencies; RUNPATH is searched after LD_LIBRARY_PATH and applies only to the file it is in. -Wl,--disable-new-dtags gives the old RPATH.
  • ldd runs the loader in a listing mode and prints where each library resolves (it hides the load addresses here). Never run ldd on an untrusted binary: for some binaries it can end up executing code. readelf -d is the safe way to read NEEDED and RUNPATH.

Watching the loader search

The loader can explain itself: LD_DEBUG=libs prints every directory it tries. The sed removes the process id at the start of each line, and the grep keeps the most useful lines:

bash · try it yourself
mkdir -p dist/bin dist/lib && cd dist
printf 'void greet(void) {}\n' > greet.c
gcc -fPIC -shared -Wl,-soname,libgreet.so.1 greet.c -o lib/libgreet.so.1
ln -s libgreet.so.1 lib/libgreet.so
printf 'void greet(void);\nint main(void) { greet(); return 0; }\n' > main.c
gcc main.c -Llib -lgreet -Wl,-rpath,'$ORIGIN/../lib' -o bin/app
LD_DEBUG=libs bin/app 2>&1 | sed 's/^ *[0-9]*:\s*//' | grep -E '^(find library|trying file|search cache)' | grep -v hwcaps
find library=libgreet.so.1 [0]; searching
trying file=/home/learner/dist/bin/../lib/libgreet.so.1
find library=libc.so.6 [0]; searching
trying file=/home/learner/dist/bin/../lib/libc.so.6
search cache=/etc/ld.so.cache
trying file=/lib/x86_64-linux-gnu/libc.so.6

For libgreet.so.1 the first RUNPATH directory has it. For libc.so.6 the loader tries the RUNPATH directory, fails, consults the ldconfig cache and finds it there. (The grep -v hides lookups in glibc-hwcaps subdirectories, which depend on your CPU.)

The full search order

For a NEEDED entry without a slash, glibc's loader searches:

  1. DT_RPATH of the requesting file (only if it has no DT_RUNPATH)
  2. LD_LIBRARY_PATH (ignored for setuid programs)
  3. DT_RUNPATH of the requesting file
  4. the cache /etc/ld.so.cache (built by ldconfig from /etc/ld.so.conf)
  5. the default directories (/lib, /usr/lib and their 64-bit equivalents)

Test yourself

What does a program record in its NEEDED entry when it links against a library built with -Wl,-soname,libgreet.so.1?

  1. libgreet.so
  2. libgreet.so.1.2.0
  3. libgreet.so.1 (the soname)
  4. the full path of the library

What does $ORIGIN mean in an rpath?

  1. The user's home directory
  2. The directory that contains the executable (or library) being loaded
  3. The current working directory
  4. The root of the source tree

When should a library's soname change from .so.1 to .so.2?

  1. On every release
  2. When the ABI changes incompatibly (functions removed or changed)
  3. When the file gets bigger
  4. Never