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:
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:
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; doneold-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:
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$ORIGINexpands to an empty variable. - Modern GNU ld writes
RUNPATH(DT_RUNPATH) by default; old toolchains wroteRPATH(DT_RPATH). The difference:RPATHis searched beforeLD_LIBRARY_PATHand also applies to the libraries' own dependencies;RUNPATHis searched afterLD_LIBRARY_PATHand applies only to the file it is in.-Wl,--disable-new-dtagsgives the oldRPATH. lddruns the loader in a listing mode and prints where each library resolves (it hides the load addresses here). Never runlddon an untrusted binary: for some binaries it can end up executing code.readelf -dis the safe way to readNEEDEDandRUNPATH.
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:
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 hwcapsfind 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:
DT_RPATHof the requesting file (only if it has noDT_RUNPATH)LD_LIBRARY_PATH(ignored for setuid programs)DT_RUNPATHof the requesting file- the cache
/etc/ld.so.cache(built byldconfigfrom/etc/ld.so.conf) - the default directories (
/lib,/usr/liband 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?
libgreet.solibgreet.so.1.2.0libgreet.so.1(the soname)- the full path of the library
What does $ORIGIN mean in an rpath?
- The user's home directory
- The directory that contains the executable (or library) being loaded
- The current working directory
- The root of the source tree
When should a library's soname change from .so.1 to .so.2?
- On every release
- When the ABI changes incompatibly (functions removed or changed)
- When the file gets bigger
- Never