Subject: Explanation of "-soname"
To: None <tech-toolchain@NetBSD.ORG>
From: Todd Vierling <tv@pobox.com>
List: tech-toolchain
Date: 07/17/1998 23:12:21
I was asked by someone in private e-mail about the reason for needing
"-soname" in shared libraries on ELF systems.  I thought the explanation
would be good to share.

:      -soname library-name
:              This option and its libraryname argument are ignored.

It's not ignored for the binutils ld, used on ELF systems.  :>

Here's what you have:

libraryname.so (symlink to libraryname.so.1.2)
libraryname.so.1 (symlink to libraryname.so.1.2)
libraryname.so.1.2

OK, let's say you link `program' with -lraryname (<g>).  The ELF ld looks
for libraryname.so, and lo and behold, there it is.  It takes the -soname
stored in the library, or if there is no -soname, libraryname.so, and
encodes that into the list of libraries needed by `program'.

Okay, let's now look at how `program' finds its libraries.  It looks for
libc, libutil, blah, blah, libraryname.  `Program' was linked against
libraryname 1.2, but it doesn't know that (ELF binaries don't know about
version numbering) - it only knows the name `libraryname.so', or whatever
was in the -soname argument. 

You go upgrade libraryname to a new major version, and you have:

libraryname.so (symlink to libraryname.so.6.9)
libraryname.so.1 (symlink to libraryname.so.1.2)
libraryname.so.1.2
libraryname.so.6 (symlink to libraryname.so.6.9)
libraryname.so.6.9

This new libraryname is not binary compatible with version 1, but you
haven't recompiled/linked `program'.  You run `program' and it goes hunting
for its libraries.  If `program' chooses `libraryname.so', you're SOL - it
isn't compatible.

That's why you encode the name `libraryname.so.1' into the library:  so that
`program' knows which version to fetch in case the major version gets
bumped.

And you don't use `-soname libraryname.so.1.2', because that gives you the
opposite problem; if you upgrade to libraryname.so.1.3 and delete
libraryname.so.1.2, it won't be able to find its library anymore.  That's
why the major-only symlink is there:  to provide a reference point for a
given major version of the library, even if the minor version changes.

-- 
-- Todd Vierling (Personal tv@pobox.com; Bus. todd_vierling@xn.xerox.com)