Saturday, 20 March 2010

Libraries - Your Own and The System's - Part 2

This post will be about system libraries - such as those you get in development packages in most Linux distributions - and the mechanisms used to allow you to use multiple versions of the same library.

System libraries

This is the sort of stuff that should be explained to new programmers on Linux, but doesn't really seem to be. *-dev/*-devel packages on Linux were kind of confusing to me at first, and this stuff can be tricky to explain, so if you get lost, the example in the next section should make things clearer.

As you should know by now, some (most) programs use shared libraries. These libraries expose symbols that are associated with a certain block of code or data. These symbols need to be visible at two distinct stages of linking.

The first stage is at link-time. The linker (e.g. ld) needs to be able to see some evidence of a symbol to know that it's there. It can then put information about where to get the code/data associated with the symbol into the executable. The second stage is at run-time. The dynamic linker (on Linux, usually has to use the information in the executable to find the appropriately named library that contains the symbol, so that it can load the associated code/data into memory (unless it's already been loaded into memory by another program...), ready to be executed.

Because of this, the shared library that the dynamic linker sees at run-time has to be compatible with the one the linker saw at link-time. If a function in the library is updated for a bugfix, everything'll work just fine (unless the program depended on the bug - a major problem on Windows, according to some Windows programmers I know), but if its API is changed (i.e. a function is redefined, or simply given a different name) then the dynamic linker won't be able to find the relevant symbol and the program will simply crash or - even worse - start executing a function that does something different that it expects, which when you think about it is unlikely to end well.

To overcome this problem, the name of the library searched for by the dynamic linker is different to the name searched for by the link-time linker*. When you specify -lX at link-time, the linker searches for a file named For example, on my system, (the library that contains definitions of standard C mathematical functions in <math.h>, such as cos and sin) is located at /usr/lib/, so passing -lm to the linker makes it search for this file. However, is not a normal file, but a symlink. There are usually two "levels" of symlinking when it comes to shared libraries:

$ ls {/usr,}/lib/*
lrwxrwxrwx 1 root root 14 2010-01-13 09:47 /lib/ ->
lrwxrwxrwx 1 root root 14 2010-01-13 09:47 /usr/lib/ -> /lib/

The real library is /lib/ This means you can develop by just linking with, and you'll automatically use the latest version of the library.

There is, of course, another aspect of the naming scheme to consider. When was compiled, somewhere on the (probably huge) command-line would have been an option like This soname is the filename that the link-time linker should tell the dynamic linker to search for. This way, if you get a bugfix version of a library like, your symlink will be updated to point to this newer library. All your programs that want to use will now use the new code, and none of them need to be recompiled.

Additionally, you could update to, and programs compiled against would still work so long as there's a on your system - it would still point to the latest version that's compatible with version 6, and any program that wants to use version 7 is free to do so. And then by updating the symlink, all your compilations will be done against the newer version of the library.

An example

So, let's look at small do-it-yourself example. We'll use from my last post.

The first thing we need to do is compile is our library, and I assume you have func1.o and func2.o handy from the last post. We'll compile version 1.0.1 of libexample, and we'll tell the dynamic linker to use version 1, which will then be a symlink to the latest version 1 of the library. So delete if it's still in the current directory, and:

gcc func1.o func2.o -o -shared -Wl, -fPIC
$ ln -sv
`' -> `'
$ ln -sv
`' -> `'
$ ls*
lrwxrwxrwx 1 john john 15 2010-03-23 21:54 ->*
lrwxrwxrwx 1 john john 19 2010-03-23 21:54 ->*
-rwxr-xr-x 1 john john 6.7K 2010-03-23 21:39*

We now have our chain of shared libraries we need in order to use our versioning scheme. When we compile an application against the library, we tell it to look for in the current directory:

$ gcc program.c -o program-shared -I. -L. -lexample

However, if we look at the dynamic dependencies of program-shared:

$ ldd program-shared => (0xb78d5000) => not found => /lib/tls/i686/cmov/ (0xb776f000)
/lib/ (0xb78d6000)

We see it depends on - there's no mention of the more general or the more specific So if we make an incompatible, there's no problem - we redefine the symlink to point to so that any new compilations take place against our updated library, but old programs compiled against will still be able to find it, and will still work. Plus, if we update libexample.1.0.1 to, we can simply redefine the symlink libexample.1 to point to libexample.1.0.2, and programs using the old library will use new bugfixes.

How Linux development packages are arranged

So, now that you've got the general principle of this method, understanding Linux development packages should be a cinch.

When a user gets the standard package, they get all the versioned library files. The Debian libssh-2 package here, for example, will give you the following shared library files:

  • /usr/lib/

  • /usr/lib/

While the development package just gives you:

  • /usr/lib/

Along with all the header files you need and a static version of the library.

My next post will cover the behaviour of the dynamic linker (including how you can change it) such as where it searches for run-time libraries and when it resolves undefined symbols, as well as how you can use your own custom functions as a "front-end" to your system's library functions.

* I couldn't think of a better name for it, and it's normally just called "the linker", but that would just be confusing. Oh well.

No comments:

Post a Comment