c - Clang 的 "-z"选项是什么,为什么需要它?

标签 c macos linker clang

考虑以下 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/

相关文章:

macos - 在 Mac OSX 10.8.4 上,我在哪里可以找到 Go 安装?

c++ - 在 Mac 上,Rcpp 没有 lib 文件夹

c - 没有存档文件的静态链接如何工作?

ios - 链接器命令失败,退出代码为 1......ios 项目错误

c - 如何处理 C 中未使用的数组空间

c - gcc 中的 atan2() 奇怪行为,w_atan2.c : No such file or directory

c - 如何将 int 和 double 作为输入而不是 char?

c - 避免 TIME_WAIT

python - .py 形式的文件未读入 C

linker - WSA启动链接错误