From dff466c0791465f929a1d8cd551d4c84b360ba32 Mon Sep 17 00:00:00 2001 From: Kostya Serebryany Date: Wed, 30 Nov 2011 01:39:16 +0000 Subject: [PATCH] when -faddress-sanitizer is present, add required flags to the linker command (linux-only) git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@145467 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Driver/Tools.cpp | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/lib/Driver/Tools.cpp b/lib/Driver/Tools.cpp index 04c77c00be..e17bcb79f2 100644 --- a/lib/Driver/Tools.cpp +++ b/lib/Driver/Tools.cpp @@ -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"); -- 2.40.0