Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Linux compile link

WinChua edited this page May 9, 2020 · 8 revisions

rpath

  • 相关文档查看: man ld.so; man ld
  • 操作: patchelf
  • 相关变量:
    • ORIGIN: gcc -Wl,-rpath, '$ORIGIN/../lib' main.c
      ORIGIN的作用是将rpath的搜索路径设置为可执行文件
      运行时相对路径, 类似于bash的
      cur_dir=`dirname $0`
      cd ${cur_dir}
    
    eg
    % gcc main.c && patchelf --print-needed a.out
    libc.so.6
    % patchelf --add-needed libhello.so a.out
    % patchelf --print-needed a.out
    libhello.so
    libc.so.6
    % ldd -r a.out
        linux-vdso.so.1 =>  (0x00007ffdd8372000)
      /$LIB/libonion.so => /lib64/libonion.so (0x00007f15dd624000)
        libhello.so => not found
        libc.so.6 => /lib64/libc.so.6 (0x00007f15dd147000)
        libdl.so.2 => /lib64/libdl.so.2 (0x00007f15dcf43000)
        /lib64/ld-linux-x86-64.so.2 (0x00007f15dd50b000)
    % mkdir lib
    % touch lib/libhello.so
    % ldd -r a.out
        linux-vdso.so.1 =>  (0x00007ffd95da5000)
        /$LIB/libonion.so => /lib64/libonion.so (0x00007f37f2681000)
        libhello.so => not found
        libc.so.6 => /lib64/libc.so.6 (0x00007f37f21a4000)
        libdl.so.2 => /lib64/libdl.so.2 (0x00007f37f1fa0000)
        /lib64/ld-linux-x86-64.so.2 (0x00007f37f2568000)
    % patchelf --set-rpath '$ORIGIN/lib' a.out
    % patchelf --print-rpath a.out
    $ORIGIN/lib
    % ldd -r a.out
    ./a.out: error while loading shared libraries: /home/winchua/benchmark/tool/testrpath/./lib/libhello.so: file too short
    % echo 'void hello(){}' > lib/hello.c && gcc -shared lib/hello.c -o lib/libhello.so
    % ldd -r a.out
        linux-vdso.so.1 =>  (0x00007ffc326d6000)
        /$LIB/libonion.so => /lib64/libonion.so (0x00007f9526b4e000)
        libhello.so => /home/winchua/benchmark/tool/testrpath/./lib/libhello.so (0x00007f9526833000)
        libc.so.6 => /lib64/libc.so.6 (0x00007f952646f000)
        libdl.so.2 => /lib64/libdl.so.2 (0x00007f952626b000)
        /lib64/ld-linux-x86-64.so.2 (0x00007f9526a35000)
    gcc -Wl,-rpath,'$ORIGIN/lib' main.c

Clone this wiki locally