]> granicus.if.org Git - clang/commitdiff
when -faddress-sanitizer is present, add required flags to the linker command (linux...
authorKostya Serebryany <kcc@google.com>
Wed, 30 Nov 2011 01:39:16 +0000 (01:39 +0000)
committerKostya Serebryany <kcc@google.com>
Wed, 30 Nov 2011 01:39:16 +0000 (01:39 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@145467 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Driver/Tools.cpp

index 04c77c00be9bdcdb13523d61ebd1198fb6d76e39..e17bcb79f2458d5cad2d9fa9ca7a89eac45ca4d8 100644 (file)
@@ -1090,6 +1090,27 @@ static bool UseRelaxAll(Compilation &C, const ArgList &Args) {
     RelaxDefault);
 }
 
+/// If AddressSanitizer is enabled, add appropriate linker flags (Linux).
+/// This needs to be called before we add the C run-time (malloc, etc).
+static void addAsanRTLinux(const ToolChain &TC, const ArgList &Args,
+                      ArgStringList &CmdArgs) {
+  // Add asan linker flags when linking an executable, but not a shared object.
+  if (Args.hasArg(options::OPT_shared) ||
+      !Args.hasFlag(options::OPT_faddress_sanitizer,
+                    options::OPT_fno_address_sanitizer, false))
+    return;
+  // LibAsan is "../lib/clang/linux/ArchName/libclang_rt.asan.a
+  llvm::SmallString<128> LibAsan =
+      llvm::sys::path::parent_path(StringRef(TC.getDriver().Dir));
+  llvm::sys::path::append(LibAsan, "lib", "clang", "linux", TC.getArchName());
+  llvm::sys::path::append(LibAsan, "libclang_rt.asan.a");
+  CmdArgs.push_back(Args.MakeArgString(LibAsan));
+  CmdArgs.push_back("-lpthread");
+  CmdArgs.push_back("-ldl");
+  CmdArgs.push_back("-export-dynamic");
+  TC.AddCXXStdlibLibArgs(Args, CmdArgs);
+}
+
 void Clang::ConstructJob(Compilation &C, const JobAction &JA,
                          const InputInfo &Output,
                          const InputInfoList &Inputs,
@@ -4443,6 +4464,9 @@ void linuxtools::Link::ConstructJob(Compilation &C, const JobAction &JA,
     CmdArgs.push_back("-lm");
   }
 
+  // Call this before we add the C run-time.
+  addAsanRTLinux(getToolChain(), Args, CmdArgs);
+
   if (!Args.hasArg(options::OPT_nostdlib)) {
     if (Args.hasArg(options::OPT_static))
       CmdArgs.push_back("--start-group");