Finding .so files without setting LD_LIBRARY_PATH

Jussi Piitulainen jussi.piitulainen at helsinki.fi
Thu May 12 00:55:14 EDT 2016


eryk sun writes:

> On Wed, May 11, 2016 at 10:39 PM, Paul Smith wrote:
>> Hi all.  I have a locally-built version of Python (2.7.11) that I'm
>> copying around to different systems, running all different versions of
>> GNU/Linux.
> ...
>> What I'd like to do is have a way of setting the library path that
>> Python uses when it tries to load .so files (for example in the ssl
>> module which loads lib-dynload/_ssl.so which links to libssl.so and
>> libcrypto.so), WITHOUT setting LD_LIBRARY_PATH in the environment that
>> Python passes to its children.
>
> An ELF header can contain either an RPATH or a RUNPATH to extend the
> library search path at load time. RPATH is searched before
> LD_LIBRARY_PATH, while RUNPATH is searched after LD_LIBRARY_PATH. Note
> that the Linux loader only uses an embedded RUNPATH when searching for
> the immediate dependencies of an object. Use an RPATH instead if you
> need to extend the path that dlopen searches at runtime (e.g. for
> loading shared libraries via ctypes).
>
> Paths can be defined relative to the object using ${ORIGIN}. For
> example, the following configures a Python build to add a relative
> RPATH:
>
>     $ ./configure LDFLAGS='-Wl,-rpath,\$${ORIGIN}/lib'
>     $ make
>
> Or a RUNPATH:
>
>     $ ./configure LDFLAGS='-Wl,-rpath,\$${ORIGIN}/lib,--enable-new-dtags'
>     $ make
>
> Here's the resulting RPATH in the ELF header:
>
>     $ readelf -d python | grep RPATH
>      0x000000000000000f (RPATH)              Library rpath: [${ORIGIN}/lib]

There's a tool (GPLv3+) called patchelf that can set these in an ELF
binary:

https://nixos.org/patchelf.html
https://github.com/NixOS/patchelf



More information about the Python-list mailing list