考虑以下 Clang 特定于链接器的选项:
-Wl,<arg> Pass the comma separated arguments in <arg> to the linker
-Xlinker <arg> Pass <arg> to the linker
-z <arg> Pass -z <arg> to the linker
Pass -z <arg> to the linker
是什么意思以及如何-z <arg>
不同于 -Xlinker <arg>
?我有时会看到
-z <arg>
与 -Wl,<arg>
一起使用喜欢:-Wl,-z,norelro
但是为什么需要这样做,如果参数为
-Wl
应该只用逗号分隔吗?
最佳答案
why is -z used with -Wl
以下 3 个命令完全等效:
clang main.c -o a.out -Wl,-z,norelro
clang main.c -o a.out -z norelro
clang main.c -o a.out -Xlinker -z -Xlinker norelro
他们都通过
-z norelro
在最后的链接阶段连接到链接器。第一种形式是许多编译器的标准形式。第二种形式稍微短一点,但真的不应该使用(因为使用“标准”形式总是更好)。仅支持与 GCC 兼容的第三种形式。这是不必要的冗长。
更新:
Couldn't we just write
-Wl,norelro
不:那将通过一个“裸体”
norelro
链接器的选项,它将把它当作一个输入文件,并会提示不存在这样的输入文件。传递 -z norelro
之间(显然)是有区别的。而只是 norelro
.Also, consider
clang ./noaslr.c -Wl,-z,-no_pie ...
那只是一个虚假的命令。
-no-pie
是链接器选项,不应以 -z
为前缀.它也不是 -z
的有效选项值选项。更新 2:
Where can I see possible values for the -z linker option?
在
ld
的手册页中在您所针对的操作系统上。例如,在 Linux 上最新的 BFD ld
understands以下 -z
选项:bndplt
, call-nop-prefix=...
, combreloc
等等等等。Also, do you have any clue why man ld on macOS doesn't show the -z linker option?
可能是因为它根本不支持该选项。
关于c - Clang 的 "-z"选项是什么,为什么需要它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58553597/