设置RPATH for python无法正常工作

时间:2017-04-25 16:43:34

标签: python elf

我尝试使用patchelf更改python的RPATH以指向本地安装的新更新库。但是python仍然无法找到新的库。

相反,如果我设置LD_LIBRARY_PATH环境变量,那么python工作正常。

我正在做以下事情 patchelf --set-rpath /home/user/new_lblas/ /home/user/.local/python

new_lblas指向更新的软件包,如intel-lapack,blas等。

如果我现在python -c 'import numpy',则会发现libblas.so.3gf没有找到。 但是,如果我设置环境变量LD_LIBRARY_PATH=/home/user/new_lblas,那么python中的import numpy可以正常工作。

有人可以帮助弄清楚我在做什么错误吗?

执行patchelf后readelf -d /home/user/.local/python的输出如下所示

     Dynamic section at offset 0x270 contains 26 entries:
     Tag        Type                         Name/Value
    0x000000000000001d (RUNPATH)            Library runpath: [/home/user/new_lblas]
    0x0000000000000001 (NEEDED)             Shared library: [libpthread.so.0]
    0x0000000000000001 (NEEDED)             Shared library: [libdl.so.2]
    0x0000000000000001 (NEEDED)             Shared library: [libutil.so.1]
    0x0000000000000001 (NEEDED)             Shared library: [librt.so.1]
    0x0000000000000001 (NEEDED)             Shared library: [libm.so.6]
    0x0000000000000001 (NEEDED)             Shared library: [libc.so.6]
    0x000000000000000c (INIT)               0x41b700
    0x000000000000000d (FINI)               0x569238
    0x000000006ffffef5 (GNU_HASH)           0x4146b0
    0x0000000000000005 (STRTAB)             0x3ff460
    0x0000000000000006 (SYMTAB)             0x4085c0
    0x000000000000000a (STRSZ)              37214 (bytes)
    0x000000000000000b (SYMENT)             24 (bytes)
    0x0000000000000015 (DEBUG)              0x0
    0x0000000000000003 (PLTGOT)             0x7f9fe8
    0x0000000000000002 (PLTRELSZ)           8040 (bytes)

在做patchelf之前,ELF标题如下所示

    Dynamic section at offset 0x1f9e00 contains 25 entries:                                           
      Tag        Type                         Name/Value                                              
     0x0000000000000001 (NEEDED)             Shared library: [libpthread.so.0]                        
     0x0000000000000001 (NEEDED)             Shared library: [libdl.so.2]                             
     0x0000000000000001 (NEEDED)             Shared library: [libutil.so.1]                           
     0x0000000000000001 (NEEDED)             Shared library: [librt.so.1]                             
     0x0000000000000001 (NEEDED)             Shared library: [libm.so.6]                              
     0x0000000000000001 (NEEDED)             Shared library: [libc.so.6]                              
     0x000000000000000c (INIT)               0x41b700                                                 
     0x000000000000000d (FINI)               0x569238                                                 
     0x000000006ffffef5 (GNU_HASH)           0x400298                                                 
     0x0000000000000005 (STRTAB)             0x40f6a8                                                 
     0x0000000000000006 (SYMTAB)             0x4035b8                                                 
     0x000000000000000a (STRSZ)              36637 (bytes)                                            
     0x000000000000000b (SYMENT)             24 (bytes)                                               
     0x0000000000000015 (DEBUG)              0x0                                                      
     0x0000000000000003 (PLTGOT)             0x7f9fe8                                                 
     0x0000000000000002 (PLTRELSZ)           8040 (bytes)                                             
     0x0000000000000014 (PLTREL)             RELA                                                     

1 个答案:

答案 0 :(得分:2)

至少有3种方法可以设置动态链接器搜索路径:

  1. export LD_LIBRARY_PATH=/home/user/new_lblas
  2. 重写二进制文件以设置值为DT_RPATH
  3. /home/user/new_lblas动态代码
  4. 重写二进制文件以设置值为DT_RUNPATH的{​​{1}}动态代码。
  5. 上面的#1和#2是等价的,但是#3(你正在做的事情)不是。区别很小:/home/user/new_lblas影响二进制所有共享库,但RPATH仅影响 二进制文件,不影响 / em>递归地应用于此二进制文件加载的共享库。

    这解释了为什么RUNPATH有效,但LD_LIBRARY_PATH没有。

    看看patchelf man page,您似乎想要:

    RUNPATH