![修复可执行文件的硬编码动态链接](https://linux22.com/image/134897/%E4%BF%AE%E5%A4%8D%E5%8F%AF%E6%89%A7%E8%A1%8C%E6%96%87%E4%BB%B6%E7%9A%84%E7%A1%AC%E7%BC%96%E7%A0%81%E5%8A%A8%E6%80%81%E9%93%BE%E6%8E%A5.png)
我有一个名为的可执行文件注册具有以下共享库依赖项:
[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
执行二进制文件时,我得到以下信息:
[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.
问题是我没有管理权限来创建指定的目录路径并将库放置在那里。此外,我没有源代码,所以我也无法重新编译它,但我有libmkl_intel_lp64.so库存储在其他地方。我尝试使用LD_预加载环境变量,但它仍然需要该库位于该特定位置。有什么办法可以解决这个问题吗?
谢谢你!
答案1
我没有与您相同的二进制文件,但我做了一些测试,似乎patchelf
可以在这里工作。我有一个使用和作为依赖项hello
编译的二进制文件:-Wl,-rpath=/home/ja/c/hello-puts/make/lib
libtest.so
$ 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)
我patchelf
使用from--make-needed-absolute
运行patchelf
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)
我想这就是你所拥有的。我复制hello
到其他机器上并且:
$ 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)
我首先删除了所需的libtest.so
依赖项:
$ 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)
我尝试运行hello
,它启动了,但由于解释器完成的惰性绑定,仅显示了预期输出的第一行:
$ ./hello
hello world
./hello: symbol lookup error: ./hello: undefined symbol: foo
我libtest.so
再次添加但没有绝对路径:
$ 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)
我复制libtest.so
到$PWD
并能够启动hello
:
$ LD_LIBRARY_PATH=. ./hello
hello world
inside foo()