Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading multiple shared libraries with different versions

I have an executable on Linux that loads libfoo.so.1 (that's a SONAME) as one of its dependencies (via another shared library). It also links to another system library, which, in turn, links to a system version, libfoo.so.2. As a result, both libfoo.so.1 and libfoo.so.2 are loaded during execution, and code that was supposed to call functions from library with version 1 ends up calling (binary-incompatible) functions from a newer system library with version 2, because some symbols stay the same. The result is usually stack smashing and a subsequent segfault.

Now, the library which links against the older version is a closed-source third-party library, and I can't control what version of libfoo it compiles against. Assuming that, the only other option left is rebuilding a bunch of system libraries that currently link with libfoo.so.2 to link with libfoo.so.1.

Is there any way to avoid replacing system libraries wiith local copies that link to older libfoo? Can I load both libraries and have the code calling correct version of symbols? So I need some special symbol-level versioning?

like image 542
Alex B Avatar asked Oct 23 '08 00:10

Alex B


People also ask

Which is the correct way to use multiple versions of same library?

You need to look into class loaders. It's pretty tricky, but here's a good explanation for you. You'll probably have to unload the old jar, load the new jar, run whatever the new function are, then unload that newer jar and reload the older jar.

How are shared libraries loaded?

Static Libraries are linked into a compiled executable (or another library). After the compilation, the new artifact contains the static library's content. Shared Libraries are loaded by the executable (or other shared library) at runtime.

Are shared and dynamic libraries the same?

Dynamic and shared libraries are usually the same. But in your case, it looks as if you are doing something special. In the shared library case, you specify the shared library at compile-time. When the app is started, the operating system will load the shared library before the application starts.

What is Ashared library?

A shared library or shared object is a file that is intended to be shared by multiple programs. Symbols used by a program are loaded from shared libraries into memory at load time or runtime.


1 Answers

You may be able to do some version script tricks:

http://sunsite.ualberta.ca/Documentation/Gnu/binutils-2.9.1/html_node/ld_26.html

This may require that you write a wrapper around your lib that pulls in libfoo.so.1 that exports some symbols explicitly and masks all others as local. For example:

MYSYMS { global: foo1; foo2; local: *; };

and use this when you link that wrapper like:

gcc -shared -Wl,--version-script,mysyms.map -o mylib wrapper.o -lfoo -L/path/to/foo.so.1

This should make libfoo.so.1's symbols local to the wrapper and not available to the main exe.

like image 76
Todd Avatar answered Oct 17 '22 09:10

Todd