如何使用 clang 和安全堆栈标志编译可执行文件

How to compile an executable using clang and safe-stack flag

我正在尝试使用带有 -fsanitize=safe-stack 标志的 clang-3.7(也尝试过 3.8 (dev))编译一个简单的 hello-world 可执行文件。如此处所述 (http://clang.llvm.org/docs/SafeStack.html),我需要将此标志传递给编译器和链接器。

"To enable SafeStack, just pass -fsanitize=safe-stack flag to both compile and link command lines."

我尝试了以下命令来编译可执行文件:

clang-3.7 -fsanitize=safe-stack -o a.out -Wl,-fsanitize=safe-stack test.c

但是链接器告诉我,如果我将 -f 标志传递给链接器,我需要将它编译为共享库 (-shared)。

/usr/bin/ld: -f may not be used without -shared

如何使用 -fsanitize=safe-stack 标志编译 可执行文件

通过 "pass it to both the compile and link command lines" 文档意味着在编译时和 linking 时都传递它。它并不意味着使用 -Wl,它将它直接传递给 linker - -f 意味着与 linker 完全无关的东西。

在这种情况下,

clang-3.7 -fsanitize=safe-stack -o a.out test.c

足够了。如果您使用单独的命令执行来编译和 link,则需要同时传递它:

clang-3.7 -fsanitize=safe-stack -c -o test.o test.c
clang-3.7 -fsanitize=safe-stack -o a.out test.o