Skip to content
systemcoding.dev
Toolchains

Version a Shared Library With -Wl,-soname

Build a versioned shared library with -Wl,-soname, upgrade it in place so programs pick up a patch, and ship an ABI break under a new soname.

7 min read
Summarize in AI
Flat illustration of source code passing through a compiler and linker into a binary, with the GNU, LLVM and C++ logos.

A shared library that never changes needs no versioning. A shared library that does change, which is every library, needs a way to ship fixes to programs already installed and a way to ship incompatible changes without breaking them. The mechanism is small: one linker flag, -Wl,-soname, and a few symbolic links. This post uses it to build version 1.0.0 of a library, link a program, upgrade the library to 1.1.0 without touching the program, and then release 2.0.0 with a changed interface that lives next to the old one. It builds on the naming rules in shared library sonames, real names and linker names, and uses GCC 14 with binutils 2.42 on Linux x86-64. The flag is described under linker options in the GCC manual.

The flag

-Wl,-soname,NAME passes -soname NAME to the linker, which writes it into the library's dynamic section. Without the -Wl, prefix GCC would try to interpret -soname itself and complain. A commonly seen alternative form is -Wl,-h,NAME, which means the same thing. The name you give is what every program linked against the library will record, so choose it with care: it is lib<name>.so.<major>.

Version 1.0.0

The library computes the dot product of two arrays. The header is the public contract:

vec.hc
#ifndef VEC_H
#define VEC_H

double vec_dot(const double *a, const double *b, int n);

#endif
vec.cc
#include "vec.h"

double vec_dot(const double *a, const double *b, int n)
{
    double sum = 0;
    for (int i = 0; i < n; i++) {
        sum += a[i] * b[i];
    }
    return sum;
}

Build it, create the links and a program:

Shell
mkdir -p v1 && cd v1
gcc -O2 -fPIC -shared -Wl,-soname,libvec.so.1 -o libvec.so.1.0.0 ../vec.c
ln -sf libvec.so.1.0.0 libvec.so.1
ln -sf libvec.so.1 libvec.so
cd ..
app.cc
#include <stdio.h>
#include "vec.h"

int main(void)
{
    double a[] = {1, 2, 3}, b[] = {4, 5, 6};
    printf("dot = %.1f\n", vec_dot(a, b, 3));
    return 0;
}
Shell
gcc -O2 app.c -I. -Lv1 -lvec -o app -Wl,-rpath,'$ORIGIN/v1'
./app
readelf -d app | grep NEEDED
Text
dot = 32.0
 0x0000000000000001 (NEEDED)             Shared library: [libvec.so.1]
 0x0000000000000001 (NEEDED)             Shared library: [libc.so.6]

The program recorded libvec.so.1, the soname. The -rpath with $ORIGIN lets the demonstration run from the current directory without installing anything.

Version 1.1.0: a compatible change

Add a new function and fix nothing that callers depend on. The existing function stays as it was, so the interface is a superset:

vec.cc
#include "vec.h"
#include <math.h>

double vec_dot(const double *a, const double *b, int n)
{
    double sum = 0;
    for (int i = 0; i < n; i++) {
        sum += a[i] * b[i];
    }
    return sum;
}

double vec_norm(const double *a, int n)
{
    return sqrt(vec_dot(a, a, n));
}

Add double vec_norm(const double *a, int n); to the header, build it with the same soname and move the link:

Shell
mkdir -p v1.1 && cd v1.1
gcc -O2 -fPIC -shared -Wl,-soname,libvec.so.1 -o libvec.so.1.1.0 ../vec.c -lm
ln -sf libvec.so.1.1.0 libvec.so.1
cd ..

To simulate the install, replace the v1 directory's contents with the new file and link. The program is not rebuilt:

Shell
cp v1.1/libvec.so.1.1.0 v1/
ln -sf libvec.so.1.1.0 v1/libvec.so.1
./app
Text
dot = 32.0

The old program still runs, now with the new library behind it. Nothing in the program changed because it asks for libvec.so.1 and that name now points at 1.1.0. This is what the soname promises. The new vec_norm exists for programs built later.

Version 2.0.0: an incompatible change

Now change the signature of vec_dot to take a stride, which breaks every existing caller:

vec2.hc
#ifndef VEC2_H
#define VEC2_H

double vec_dot(const double *a, const double *b, int n, int stride);

#endif
vec2.cc
#include "vec2.h"

double vec_dot(const double *a, const double *b, int n, int stride)
{
    double sum = 0;
    for (int i = 0; i < n; i++) {
        sum += a[i * stride] * b[i * stride];
    }
    return sum;
}

This must carry a new major number. Build it with -Wl,-soname,libvec.so.2 and name the file libvec.so.2.0.0:

Shell
mkdir -p v2 && cd v2
gcc -O2 -fPIC -shared -Wl,-soname,libvec.so.2 -o libvec.so.2.0.0 ../vec2.c
ln -sf libvec.so.2.0.0 libvec.so.2
ln -sf libvec.so.2 libvec.so
cd ..

The old program is untouched because it asks for libvec.so.1, which still exists. A program built now against v2 records libvec.so.2:

Shell
ls v1 v2
Text
v1:
libvec.so  libvec.so.1  libvec.so.1.0.0  libvec.so.1.1.0

v2:
libvec.so  libvec.so.2  libvec.so.2.0.0

Both majors coexist on a machine. That is how Linux distributions keep old programs working while new ones use a new interface, and it is the reason packages like libssl1.1 and libssl3 can be installed together.

What an old program does against the new file

To see why the major number matters, force the experiment. Copy libvec.so.2.0.0 over the name the old program asks for, as if a careless package had kept the soname:

Shell
cp v2/libvec.so.2.0.0 v1/libvec.so.1.1.0
./app
Text
dot = 12.0

The program loads without any complaint, since a file called libvec.so.1 exists, and prints a wrong answer. It passed three arguments, and the new function read a fourth, stride, from a register that held whatever the caller left there. If it is zero, every term reads element zero and the dot product comes out as three times the product of the first elements, which is 12 for our arrays, a plausible number that is wrong. If it is a large number, the loop reads far outside the arrays and may crash. The output shown is one possible result, since the real value depends on the register contents, and the lesson is the absence of any error message. The loader checks names, not meaning, so the major number is the only protection you have.

This is also why distributions treat a soname bump as a rebuild event. When a library's major version changes, every package that links against it is rebuilt against the new soname and republished, and the old library package stays installed until nothing depends on it. You get the same safety in your own releases by keeping each major in its own directory or package, and by running the old test programs against every new minor build as a regression suite.

Notice how convincing that failure is. The number is a legitimate-looking double, the program exits with status zero, and no log line says anything is off. A test suite that only checks that the program starts would pass. A test that compares the answer would catch it, which is why a library's regression tests should run against the installed library through the soname and not against the build tree.

What goes wrong

Forgetting to bump the major number is the failure that matters. If version 2.0.0 had kept the soname libvec.so.1 and replaced the file, the old program would call vec_dot with three arguments and the new code would read a fourth from whatever register held garbage. The result is a wrong number or a crash with no error message, since the loader sees a matching name and loads it. No tool detects this for you. The defense is a check in the release process, such as comparing the exported symbol list between versions, then deciding if a removal or signature change needs the new number. A tool such as abidiff from libabigail compares two builds and reports changes.

The reverse mistake is bumping the major for every release. Programs then need relinking each time, defeating the purpose. Keep the major number for real breaks.

Programs linked against a library built without -soname do not get any of this, as covered earlier. Check your build with readelf -d on the real file before you publish.

Automating it in a Makefile

A short rule keeps the three names in step:

Makefilemakefile
NAME  := vec
MAJOR := 1
FULL  := 1.1.0

lib$(NAME).so.$(FULL): vec.c
	$(CC) -O2 -fPIC -shared -Wl,-soname,lib$(NAME).so.$(MAJOR) -o $@ $< -lm
	ln -sf $@ lib$(NAME).so.$(MAJOR)
	ln -sf lib$(NAME).so.$(MAJOR) lib$(NAME).so

Change FULL for every release and MAJOR only for incompatible ones. Everything else follows from those two variables.

When this is not enough

A single soname gives all-or-nothing compatibility. Large libraries such as glibc need finer control, where one file provides several versions of a function so that old programs get old behavior and new programs get new behavior. That is GNU symbol versioning, set with a version script passed to the linker. Reach for it when a library has many users you cannot rebuild and the break touches only a few functions.

Share

Keep reading

Get new posts by email

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

Unsubscribe in one click. No spam, ever.