Search code examples
linuxgcclinkersharedrpath

What are the recommended GNU linker options to specify $ORIGIN in RPATH?


Assume my platform is vanilla (non-embedded) x86-64 Linux using GNU build toolchain (GCC, etc.).

To specify $ORIGIN in RPATH, I know about the linker option: -Wl,-rpath,'\$\$ORIGIN'.

Today, I discovered another option: -Wl,-z,origin.

Should I always include -Wl,-z,origin when using -Wl,-rpath,'\$\$ORIGIN'?

Official GNU ld docs, say:

Marks the object may contain $ORIGIN.

Related, but different: https://stackoverflow.com/questions/33853271/what-are-the-recommended-gnu-linker-options-to-specify-rpath


Solution

  • I know about the linker option: -Wl,-rpath,'\$\$ORIGIN'

    You know wrong: above option will not do what you want. The option you want is -Wl,-rpath='$ORIGIN'. The difference:

    echo "int main() { }" | gcc -xc - -Wl,-rpath,'\$\$ORIGIN' &&
    readelf -d a.out | grep ORIGIN
    
     0x000000000000000f (RPATH)              Library rpath: [\$\$ORIGIN]
    
    echo "int main() { }" | gcc -xc - -Wl,-rpath='$ORIGIN' &&
    readelf -d a.out | grep ORIGIN
    
     0x000000000000000f (RPATH)              Library rpath: [$ORIGIN]
    

    Should I always include -Wl,-z,origin

    The -Wl,-z,origin sets DF_ORIGIN in FLAGS_1 dynamic entry.

    As of current GLIBC trunk, nothing looks at the value of that flag, so if you target GLIBC (most Linux programs do), the answer is: it doesn't matter one bit whether you use -z origin or not.

    The answer may be different for other libc implementations. However, Solaris libc (where the whole $ORIGIN is coming from) also does not appear to require the DF_ORIGIN to be set, so it's probably a safe bet to ignore this completely.