PatchELF 修改linux下elf文件library搜索路径

http://nixos.org/patchelf.html

wget http://nixos.org/releases/patchelf/patchelf-0.8/patchelf-0.8.tar.bz2
tar -zxf patchelf-0.8.tar.bz2
cd patchelf-0.8
./configure --prefix=/usr
make && make install

修改前

 readelf -d cc1

Dynamic section at offset 0xd49728 contains 28 entries:
  Tag        Type                         Name/Value
 0x0000000000000001 (NEEDED)             Shared library: [libcloog-isl.so.4]
 0x0000000000000001 (NEEDED)             Shared library: [libdl.so.2]
 0x0000000000000001 (NEEDED)             Shared library: [libm.so.6]
 0x0000000000000001 (NEEDED)             Shared library: [libc.so.6]
 0x0000000000000001 (NEEDED)             Shared library: [ld-linux-x86-64.so.2]
 0x000000000000000c (INIT)               0x4f8e18
 0x000000000000000d (FINI)               0xd36e1c
 0x0000000000000019 (INIT_ARRAY)         0x1344ab8
 0x000000000000001b (INIT_ARRAYSZ)       224 (bytes)
 0x000000000000001a (FINI_ARRAY)         0x1344b98
 0x000000000000001c (FINI_ARRAYSZ)       8 (bytes)
 0x000000006ffffef5 (GNU_HASH)           0x400298
 0x0000000000000005 (STRTAB)             0x46ca58
 0x0000000000000006 (SYMTAB)             0x419a30
 0x000000000000000a (STRSZ)              529715 (bytes)
 0x000000000000000b (SYMENT)             24 (bytes)
 0x0000000000000015 (DEBUG)              0x0
 0x0000000000000003 (PLTGOT)             0x134a1e8
 0x0000000000000002 (PLTRELSZ)           9312 (bytes)
 0x0000000000000014 (PLTREL)             RELA
 0x0000000000000017 (JMPREL)             0x4f69b8
 0x0000000000000007 (RELA)               0x4f4ed0
 0x0000000000000008 (RELASZ)             6888 (bytes)
 0x0000000000000009 (RELAENT)            24 (bytes)
 0x000000006ffffffe (VERNEED)            0x4f4e40
 0x000000006fffffff (VERNEEDNUM)         3
 0x000000006ffffff0 (VERSYM)             0x4edf8c
 0x0000000000000000 (NULL)               0x0
# ldd cc1
        linux-vdso.so.1 =>  (0x00007fff20dff000)
        libcloog-isl.so.4 => not found
        libdl.so.2 => /lib64/libdl.so.2 (0x000000320d400000)
        libm.so.6 => /lib64/libm.so.6 (0x000000320d000000)
        libc.so.6 => /lib64/libc.so.6 (0x000000320c800000)
        /lib64/ld-linux-x86-64.so.2 (0x000000320c400000)

利用patchelf修改路径

patchelf  --set-rpath /opt/gcc-4.8.5/builddir/cloog-install/lib/ cc1

查看最新信息

# readelf  -d cc1

Dynamic section at offset 0x270 contains 29 entries:
  Tag        Type                         Name/Value
 0x000000000000001d (RUNPATH)            Library runpath: [/opt/gcc-4.8.5/builddir/cloog-install/lib/]
 0x0000000000000001 (NEEDED)             Shared library: [libcloog-isl.so.4]
 0x0000000000000001 (NEEDED)             Shared library: [libdl.so.2]
 0x0000000000000001 (NEEDED)             Shared library: [libm.so.6]
 0x0000000000000001 (NEEDED)             Shared library: [libc.so.6]
 0x0000000000000001 (NEEDED)             Shared library: [ld-linux-x86-64.so.2]
 0x000000000000000c (INIT)               0x4f8e18
 0x000000000000000d (FINI)               0xd36e1c
 0x0000000000000019 (INIT_ARRAY)         0x1344ab8
 0x000000000000001b (INIT_ARRAYSZ)       224 (bytes)
 0x000000000000001a (FINI_ARRAY)         0x1344b98
 0x000000000000001c (FINI_ARRAYSZ)       8 (bytes)
 0x000000006ffffef5 (GNU_HASH)           0x4d3a18
 0x0000000000000005 (STRTAB)             0x3ff490
 0x0000000000000006 (SYMTAB)             0x4809f0
 0x000000000000000a (STRSZ)              529758 (bytes)
 0x000000000000000b (SYMENT)             24 (bytes)
 0x0000000000000015 (DEBUG)              0x0
 0x0000000000000003 (PLTGOT)             0x134a1e8
 0x0000000000000002 (PLTRELSZ)           9312 (bytes)
 0x0000000000000014 (PLTREL)             RELA
 0x0000000000000017 (JMPREL)             0x4f69b8
 0x0000000000000007 (RELA)               0x4f4ed0
 0x0000000000000008 (RELASZ)             6888 (bytes)
 0x0000000000000009 (RELAENT)            24 (bytes)
 0x000000006ffffffe (VERNEED)            0x4f4e40
 0x000000006fffffff (VERNEEDNUM)         3
 0x000000006ffffff0 (VERSYM)             0x4edf8c
 0x0000000000000000 (NULL)               0x0
# ldd cc1
        linux-vdso.so.1 =>  (0x00007fff8e3ff000)
        libcloog-isl.so.4 => /opt/gcc-4.8.5/builddir/cloog-install/lib/libcloog-isl.so.4 (0x00007fdaac630000)
        libdl.so.2 => /lib64/libdl.so.2 (0x000000320d400000)
        libm.so.6 => /lib64/libm.so.6 (0x000000320d000000)
        libc.so.6 => /lib64/libc.so.6 (0x000000320c800000)
        /lib64/ld-linux-x86-64.so.2 (0x000000320c400000)
        libgmp.so.3 => /usr/lib64/libgmp.so.3 (0x000000320e000000)

注:

    才发现RHEL 已经自带了chrpath工具,直接使用即可. ( yum install chrpath)

    不过chrpath 有个缺陷,如果当前系统为x86_64,则修改i386 elf会报错,patchelf则无此问题!


你可能感兴趣的:(PatchELF 修改linux下elf文件library搜索路径)