Gentoo Logo
Gentoo Spaceship




Note: Due to technical difficulties, the Archives are currently not up to date. GMANE provides an alternative service for most mailing lists.
c.f. bug 424647
List Archive: gentoo-alt
Navigation:
Lists: gentoo-alt: < Prev By Thread Next > < Prev By Date Next >
Headers:
To: gentoo-alt@g.o
From: Fabian Groffen <grobian@g.o>
Subject: [PREFIX] dynamic linking: moving the locations
Date: Wed, 11 Apr 2007 21:10:59 +0200
Let's start with the code: :)


[ra:/var/tmp] % cat test.c 
#include <openssl/opensslv.h>
#include <openssl/md5.h>
#include <stdio.h>

int main() {
        MD5_CTX c;
        unsigned char md[MD5_DIGEST_LENGTH];

        printf("%s\n", OPENSSL_VERSION_TEXT);
        MD5_Init(&c);
        MD5_Update(&c, "test", 4);
        MD5_Final(md, &c);
}
[ra:/var/tmp] % gcc -o test test.c -lssl
[ra:/var/tmp] % ldd test
        libssl.so.0.9.8 =>       /export/scratch0/GentooPrefix/usr/lib/libssl.so.0.9.8
[ra:/var/tmp] % ./test 
OpenSSL 0.9.8e 23 Feb 2007

Ok, working binary, linked to openssl 0.9.8 in
/export/scratch0/GentooPrefix/usr/lib.  Now check this little utility I
just checked in:

[ra:/var/tmp] % chrpath test
test: RPATH=/export/scratch0/GentooPrefix/usr/lib/gcc/i386-pc-solaris2.10/4.1.2:/export/scratch0/GentooPrefix/lib:/export/scratch0/GentooPrefix/usr/lib

Ok, this is the rpath that our wrapper injects.

[ra:/var/tmp] % chrpath -r /usr/lib/gcc/i386-pc-solaris2.10/4.1.2:/lib:/usr/lib
test
test: RPATH=/export/scratch0/GentooPrefix/usr/lib/gcc/i386-pc-solaris2.10/4.1.2:/export/scratch0/GentooPrefix/lib:/export/scratch0/GentooPrefix/usr/lib
test: new RPATH: /usr/lib/gcc/i386-pc-solaris2.10/4.1.2:/lib:/usr/lib

So, we changed the rpath record.  Let's see if it's for real:

[ra:/var/tmp] % ldd test
        libssl.so.0.9.8 =>       (file not found)

Don't believe it?

[ra:/var/tmp] % ./test 
libc.so.1: test: fatal: libssl.so.0.9.8: open failed: No such file or
directory
Killed

So, can we fix it also again?

[ra:/var/tmp] % chrpath -r /export/scratch0/GentooPrefix/usr/lib/gcc/i386-pc-solaris2.10/4.1.2:/export/scratch0/GentooPrefix/lib:/export/scratch0/GentooPrefix/usr/lib
test
test: RPATH=/usr/lib/gcc/i386-pc-solaris2.10/4.1.2:/lib:/usr/lib
test: new RPATH: /export/scratch0/GentooPrefix/usr/lib/gcc/i386-pc-solaris2.10/4.1.2:/export/scratch0/GentooPrefix/lib:/export/scratch0/GentooPrefix/usr/lib

[ra:/var/tmp] % ./test 
OpenSSL 0.9.8e 23 Feb 2007

Yes we can!
This was Solaris, Linux works the same.  Now let's check Darwin.

Same test program.

[tefnut:/var/tmp] % gcc -o test test.c -lssl -lcrypto
[tefnut:/var/tmp] % ./test 
OpenSSL 0.9.8e 23 Feb 2007
[tefnut:/var/tmp] % otool -L test
test:
        /Library/Gentoo/usr/lib/libssl.0.9.8.dylib (compatibility version 0.9.8, current version 0.9.8)
        /Library/Gentoo/usr/lib/libcrypto.0.9.8.dylib (compatibility version 0.9.8, current version 0.9.8)

Ok, prefix paths.  Darwin is MACH-O, which stores the full filename to
the library in this case, so no rpath is there.  chrpath doesn't work
here for that reason, but on Darwin, there is the install_name_tool
tool.

[tefnut:/var/tmp] % install_name_tool -change /Library/Gentoo/usr/lib/libssl.0.9.8.dylib /usr/lib/libssl.0.9.8.dylib test
[tefnut:/var/tmp] % otool -L testtest:
        /usr/lib/libssl.0.9.8.dylib (compatibility version 0.9.8, current version 0.9.8)
        /Library/Gentoo/usr/lib/libcrypto.0.9.8.dylib (compatibility version 0.9.8, current version 0.9.8)
[tefnut:/var/tmp] % ./test 
dyld: Library not loaded: /usr/lib/libssl.0.9.8.dylib
  Referenced from: /private/var/tmp/./test
  Reason: image not found
Trace/BPT trap

Fix it?

[tefnut:/var/tmp] % install_name_tool -change /usr/lib/libssl.0.9.8.dylib /Library/Gentoo/usr/lib/libssl.0.9.8.dylib test
[tefnut:/var/tmp] % ./test
OpenSSL 0.9.8e 23 Feb 2007

Yes! :)


So, install_name_tool does it a bit different than chrpath, but with
some scripting both can do the same: change the location libraries are
looked for.  What does this mean?  Well, let's look at this:

[tefnut:/var/tmp] % install_name_tool -change /Library/Gentoo/usr/lib/libssl.0.9.8.dylib /var/tmp/libssl.0.9.8.dylib test
[tefnut:/var/tmp] % cp /Library/Gentoo/usr/lib/libssl.0.9.8.dylib .
[tefnut:/var/tmp] % ./test 
OpenSSL 0.9.8e 23 Feb 2007
[tefnut:/var/tmp] % otool -L test
test:
        /var/tmp/libssl.0.9.8.dylib (compatibility version 0.9.8, current version 0.9.8)
        /Library/Gentoo/usr/lib/libcrypto.0.9.8.dylib (compatibility version 0.9.8, current version 0.9.8)

Now suppose there is a server on the interweb that has this openssl
installed, and I want to have it on my system.  That system uses prefix
/X/Y, while I use /A/B.  What can we do?  Simply scan all binaries and
libraries and replace the paths from /X/Y/ to /A/B!

Possible uses:
- rescue your system after a major f****up
- binhost kind of idea to avoid compiling all the time, even though
  prefixes aren't the same
- bootstrap your system by injecting everything your need at once,
  including a compiler, then emerge world.
- inject prefix/rpath _after_ a library has been merged without a
  ldwrapper-ed linker...

So, does anyone know if a similar trick can be played on AIX?  Marshall, you maybe?


-- 
Fabian Groffen
Gentoo on a different level

-- 
gentoo-alt@g.o mailing list


Replies:
Re: [PREFIX] dynamic linking: moving the locations
-- Armando Di Cianno
Navigation:
Lists: gentoo-alt: < Prev By Thread Next > < Prev By Date Next >
Previous by thread:
[prefix][linux] permission problem merging apr-util on linux
Next by thread:
Re: [PREFIX] dynamic linking: moving the locations
Previous by date:
Re: [prefix][aix] debianutils succeeded!
Next by date:
Re: [PREFIX] dynamic linking: moving the locations


Updated Jun 17, 2009

Summary: Archive of the gentoo-alt mailing list.

Donate to support our development efforts.

Copyright 2001-2013 Gentoo Foundation, Inc. Questions, Comments? Contact us.