[Sanitizer] force linking with static sanitizer runtimes on Darwin even if they are...
authorAlexey Samsonov <samsonov@google.com>
Wed, 21 Nov 2012 14:17:42 +0000 (14:17 +0000)
committerAlexey Samsonov <samsonov@google.com>
Wed, 21 Nov 2012 14:17:42 +0000 (14:17 +0000)
llvm-svn: 168428

clang/lib/Driver/ToolChains.cpp
clang/lib/Driver/ToolChains.h
clang/test/Driver/darwin-sanitizer-ld.c [new file with mode: 0644]

index a56d37b..94e3012 100644 (file)
@@ -261,16 +261,18 @@ void DarwinClang::AddLinkARCArgs(const ArgList &Args,
 
 void DarwinClang::AddLinkRuntimeLib(const ArgList &Args,
                                     ArgStringList &CmdArgs,
-                                    const char *DarwinStaticLib) const {
+                                    const char *DarwinStaticLib,
+                                    bool AlwaysLink) const {
   llvm::sys::Path P(getDriver().ResourceDir);
   P.appendComponent("lib");
   P.appendComponent("darwin");
   P.appendComponent(DarwinStaticLib);
 
   // For now, allow missing resource libraries to support developers who may
-  // not have compiler-rt checked out or integrated into their build.
+  // not have compiler-rt checked out or integrated into their build (unless
+  // we explicitly force linking with this library).
   bool Exists;
-  if (!llvm::sys::fs::exists(P.str(), Exists) && Exists)
+  if (AlwaysLink || (!llvm::sys::fs::exists(P.str(), Exists) && Exists))
     CmdArgs.push_back(Args.MakeArgString(P.str()));
 }
 
@@ -326,7 +328,7 @@ void DarwinClang::AddLinkRuntimeLibArgs(const ArgList &Args,
       getDriver().Diag(diag::err_drv_clang_unsupported_per_platform)
         << "-fsanitize=undefined";
     } else {
-      AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.ubsan_osx.a");
+      AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.ubsan_osx.a", true);
 
       // The Ubsan runtime library requires C++.
       AddCXXStdlibLibArgs(Args, CmdArgs);
@@ -343,7 +345,7 @@ void DarwinClang::AddLinkRuntimeLibArgs(const ArgList &Args,
       getDriver().Diag(diag::err_drv_clang_unsupported_per_platform)
         << "-fsanitize=address";
     } else {
-      AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.asan_osx.a");
+      AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.asan_osx.a", true);
 
       // The ASAN runtime library requires C++ and CoreFoundation.
       AddCXXStdlibLibArgs(Args, CmdArgs);
index 4c267e8..337ef20 100644 (file)
@@ -370,9 +370,10 @@ public:
 
   virtual void AddLinkRuntimeLibArgs(const ArgList &Args,
                                      ArgStringList &CmdArgs) const;
-  void AddLinkRuntimeLib(const ArgList &Args, ArgStringList &CmdArgs, 
-                         const char *DarwinStaticLib) const;
-  
+  void AddLinkRuntimeLib(const ArgList &Args, ArgStringList &CmdArgs,
+                         const char *DarwinStaticLib,
+                         bool AlwaysLink = false) const;
+
   virtual void AddCXXStdlibLibArgs(const ArgList &Args,
                                    ArgStringList &CmdArgs) const;
 
diff --git a/clang/test/Driver/darwin-sanitizer-ld.c b/clang/test/Driver/darwin-sanitizer-ld.c
new file mode 100644 (file)
index 0000000..65e4112
--- /dev/null
@@ -0,0 +1,40 @@
+// Test sanitizer link flags on Darwin.
+
+// RUN: %clang -no-canonical-prefixes -### -target x86_64-darwin \
+// RUN:   -fsanitize=address %s -o %t.o 2>&1 \
+// RUN:   | FileCheck --check-prefix=CHECK-ASAN %s
+
+// CHECK-ASAN: "{{.*}}ld" 
+// CHECK-ASAN: libclang_rt.asan_osx.a"
+// CHECK-ASAN: "-lstdc++"
+// CHECK-ASAN: "-framework" "CoreFoundation"
+
+// RUN: %clang -no-canonical-prefixes -### -target x86_64-darwin \
+// RUN:   -fPIC -shared -fsanitize=address %s -o %t.so 2>&1 \
+// RUN:   | FileCheck --check-prefix=CHECK-DYN-ASAN %s
+
+// CHECK-DYN-ASAN: "{{.*}}ld"
+// CHECK-DYN-ASAN: "-dylib"
+// CHECK-DYN-ASAN-NOT: libclang_rt.asan_osx.a
+// CHECK-DYN-ASAN: "-undefined"
+// CHECK-DYN-ASAN: "dynamic_lookup"
+// CHECK-DYN-ASAN-NOT: libclang_rt.asan_osx.a
+
+// RUN: %clang -no-canonical-prefixes -### -target x86_64-darwin \
+// RUN:   -fsanitize=undefined %s -o %t.o 2>&1 \
+// RUN:   | FileCheck --check-prefix=CHECK-UBSAN %s
+
+// CHECK-UBSAN: "{{.*}}ld" 
+// CHECK-UBSAN: libclang_rt.ubsan_osx.a"
+// CHECK-UBSAN: "-lstdc++"
+
+// RUN: %clang -no-canonical-prefixes -### -target x86_64-darwin \
+// RUN:   -fPIC -shared -fsanitize=undefined %s -o %t.so 2>&1 \
+// RUN:   | FileCheck --check-prefix=CHECK-DYN-UBSAN %s
+
+// CHECK-DYN-UBSAN: "{{.*}}ld"
+// CHECK-DYN-UBSAN: "-dylib"
+// CHECK-DYN-UBSAN-NOT: libclang_rt.ubsan_osx.a
+// CHECK-DYN-UBSAN: "-undefined"
+// CHECK-DYN-UBSAN: "dynamic_lookup"
+// CHECK-DYN-UBSAN-NOT: libclang_rt.ubsan_osx.a