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

♀尐吖头ヾ 提交于 2021-01-27 12:10:36

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/33853344/what-are-the-recommended-gnu-linker-options-to-specify-origin-in-rpath

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!