Fix hardcoded dynamic linking for executable

I have an executable file called reg with the following shared library dependencies:

[terminal]$ ldd ./reg
linux-vdso.so.1 => (0x00007ffc40d90000)
libpthread.so.0 => /lib64/libpthread.so.0 (0x0000003be0c00000)
/usr/dep/packages/opt/intel/mkl/10.0.2.018/lib/em64t/libmkl_intel_lp64.so => not found

When executing the binary I get the following:

[terminal]$ ./reg
./reg: error while loading shared libraries: /usr/dep/packages/opt/intel/mkl/10.0.2.018/lib/em64t/libmkl_intel_lp64.so: cannot open shared object file: No such file or directory.

The thing is that I don’t have the administrative rights to create the specified directory path and place the library there. Moreover, I don’t have the source code so I can’t re-compile it either but I do have libmkl_intel_lp64.so library stored somewhere else. I tried using LD_PRELOAD environment variable but it still needs that library in that specific location. Is there a solution how could I solve this problem?

Thank you!

Answer

I don’t have the same binary you have but I made a little test and it
seems that patchelf can work here. I have a hello binary compiled
with -Wl,-rpath=/home/ja/c/hello-puts/make/lib and libtest.so as a
dependency:

$ ldd hello
        linux-vdso.so.1 (0x00007ffedb4f0000)
        libtest.so => /home/ja/c/hello-puts/make/lib/libtest.so (0x00007f04a2437000)
        libc.so.6 => /lib64/libc.so.6 (0x00007f04a200f000)
        /lib64/ld-linux-x86-64.so.2 (0x0000564a42e36000)

I run patchelf with --make-needed-absolute using patchelf from https://github.com/dezgeg/patchelf/:

$ patchelf --make-needed-absolute hello
$ ldd hello
        linux-vdso.so.1 (0x00007fff9baa3000)
        /home/ja/c/hello-puts/make/lib/libtest.so (0x00007f81bd0e2000)
        libc.so.6 => /lib64/libc.so.6 (0x00007f81bccba000)
        /lib64/ld-linux-x86-64.so.2 (0x0000556714bb5000)

I think that’s what you have. I copied hello to other machine and:

$ ldd ./hello
        linux-vdso.so.1 =>  (0x00007fff92e7d000)
        /home/ja/c/hello-puts/make/lib/libtest.so => not found
        libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007ff381c9b000)
        /lib64/ld-linux-x86-64.so.2 (0x00007ff382065000)

I first removed a needed libtest.so dependency:

$ patchelf --remove-needed /home/ja/c/hello-puts/make/lib/libtest.so hello
$ ldd hello
        linux-vdso.so.1 =>  (0x00007ffdcedfb000)
        libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f60705c5000)
        /lib64/ld-linux-x86-64.so.2 (0x00007f607098f000)

I tried to run hello, it started but due to lazy binding done by the
interpreter only the first line of the expected output was shown:

$ ./hello
hello world
./hello: symbol lookup error: ./hello: undefined symbol: foo

I added libtest.so again but without an absolute path:

$ patchelf --add-needed libtest.so hello
$ ldd hello
        linux-vdso.so.1 =>  (0x00007ffda155c000)
        libtest.so => not found
        libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007ffbdb8c3000)
        /lib64/ld-linux-x86-64.so.2 (0x00007ffbdbc8d000)

I copied libtest.so to $PWD and was able to start hello:

$ LD_LIBRARY_PATH=. ./hello
hello world
inside foo()

Attribution
Source : Link , Question Author : Donatas , Answer Author : Arkadiusz Drabczyk

Leave a Comment