Revert r218541 - Don't link in sanitizer runtimes if -nostdlib/-nodefaultlibs is...
authorAlexey Samsonov <vonosmas@gmail.com>
Thu, 23 Oct 2014 00:46:10 +0000 (00:46 +0000)
committerAlexey Samsonov <vonosmas@gmail.com>
Thu, 23 Oct 2014 00:46:10 +0000 (00:46 +0000)
This is a sad thing to do, but all the alternatives look ugly.

Looks like there are legitimate cases when users may want to link
with sanitizer runtimes *and* -nodefaultlibs (and ensure they provide
replacements for system libraries). For example, this happens in libc++
test suite.

"-nodefaultlibs" is told to link only the libraries explicitly provided
by the user, and providing "-fsanitize=address" is a clear indication of
intention to link with ASan runtime.
We can't easily introduce analogue of "-print-libgcc-name": linking with
sanitizers runtimes is not trivial: some runtimes are split into several
archive libraries, which are required to be wrapped in
-whole-archive/-no-whole-archive.

If "-fsanitize=whatever" and "-nodefaultlibs" are provided, system library
dependencies of sanitizer runtimes (-lc/-ldl/-lpthread/-lrt) will *not* be
linked, and user would have to link them in manually. Note that this can
cause problems, as failing to provide "-lrt" might lead to crashes in runtime
during ASan initialization. But looks like we should bite this bullet.

See r218541 review thread for the discussion.

llvm-svn: 220455

clang/lib/Driver/Tools.cpp
clang/test/Driver/sanitizer-ld.c

index 6e7060f..4a125f5 100644 (file)
@@ -2267,10 +2267,6 @@ collectSanitizerRuntimes(const ToolChain &TC, const ArgList &Args,
 // C runtime, etc). Returns true if sanitizer system deps need to be linked in.
 static bool addSanitizerRuntimes(const ToolChain &TC, const ArgList &Args,
                                  ArgStringList &CmdArgs) {
-  // Don't link in any sanitizer runtimes if we have no system libraries.
-  if (Args.hasArg(options::OPT_nostdlib) ||
-      Args.hasArg(options::OPT_nodefaultlibs))
-    return false;
   SmallVector<StringRef, 4> SharedRuntimes, StaticRuntimes,
       HelperStaticRuntimes;
   collectSanitizerRuntimes(TC, Args, SharedRuntimes, StaticRuntimes,
index a7ef4c7..dc8e93e 100644 (file)
 // CHECK-LSAN-ASAN-LINUX-NOT: libclang_rt.lsan
 // CHECK-LSAN-ASAN-LINUX: libclang_rt.asan-x86_64
 // CHECK-LSAN-ASAN-LINUX-NOT: libclang_rt.lsan
-
-// RUN: %clang -nostdlib -fsanitize=address %s -### -o %t.o 2>&1 \
-// RUN:     -target x86_64-unknown-linux \
-// RUN:     --sysroot=%S/Inputs/basic_linux_tree \
-// RUN:   | FileCheck --check-prefix=CHECK-NOSTDLIB %s
-// CHECK-NOSTDLIB: "{{.*}}ld{{(.exe)?}}"
-// CHECK-NOSTDLIB-NOT: libclang_rt.asan