Index: cfe/trunk/lib/Driver/ToolChains/CommonArgs.cpp =================================================================== --- cfe/trunk/lib/Driver/ToolChains/CommonArgs.cpp +++ cfe/trunk/lib/Driver/ToolChains/CommonArgs.cpp @@ -597,19 +597,6 @@ StaticRuntimes.push_back("esan"); } -static void addLibFuzzerRuntime(const ToolChain &TC, - const ArgList &Args, - ArgStringList &CmdArgs) { - StringRef ParentDir = - llvm::sys::path::parent_path(TC.getDriver().InstalledDir); - SmallString<128> P(ParentDir); - llvm::sys::path::append(P, "lib", "libLLVMFuzzer.a"); - CmdArgs.push_back(Args.MakeArgString(P)); - if (!Args.hasArg(clang::driver::options::OPT_nostdlibxx)) - TC.AddCXXStdlibLibArgs(Args, CmdArgs); -} - - // Should be called before we add system libraries (C++ ABI, libstdc++/libc++, // C runtime, etc). Returns true if sanitizer system deps need to be linked in. bool tools::addSanitizerRuntimes(const ToolChain &TC, const ArgList &Args, @@ -619,10 +606,14 @@ collectSanitizerRuntimes(TC, Args, SharedRuntimes, StaticRuntimes, NonWholeStaticRuntimes, HelperStaticRuntimes, RequiredSymbols); + // Inject libfuzzer dependencies. if (TC.getSanitizerArgs().needsFuzzer() && !Args.hasArg(options::OPT_shared)) { - addLibFuzzerRuntime(TC, Args, CmdArgs); + + addSanitizerRuntime(TC, Args, CmdArgs, "fuzzer", false, true); + if (!Args.hasArg(clang::driver::options::OPT_nostdlibxx)) + TC.AddCXXStdlibLibArgs(Args, CmdArgs); } for (auto RT : SharedRuntimes) Index: cfe/trunk/lib/Driver/ToolChains/Darwin.h =================================================================== --- cfe/trunk/lib/Driver/ToolChains/Darwin.h +++ cfe/trunk/lib/Driver/ToolChains/Darwin.h @@ -154,8 +154,6 @@ /// Add the linker arguments to link the compiler runtime library. virtual void AddLinkRuntimeLibArgs(const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs) const; - virtual void AddFuzzerLinkArgs(const llvm::opt::ArgList &Args, - llvm::opt::ArgStringList &CmdArgs) const; virtual void addStartObjectFileArgs(const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs) const { @@ -489,7 +487,8 @@ private: void AddLinkSanitizerLibArgs(const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs, - StringRef Sanitizer) const; + StringRef Sanitizer, + bool shared = true) const; }; } // end namespace toolchains Index: cfe/trunk/lib/Driver/ToolChains/Darwin.cpp =================================================================== --- cfe/trunk/lib/Driver/ToolChains/Darwin.cpp +++ cfe/trunk/lib/Driver/ToolChains/Darwin.cpp @@ -930,18 +930,6 @@ } } -void MachO::AddFuzzerLinkArgs(const ArgList &Args, ArgStringList &CmdArgs) const { - - // Go up one directory from Clang to find the libfuzzer archive file. - StringRef ParentDir = llvm::sys::path::parent_path(getDriver().InstalledDir); - SmallString<128> P(ParentDir); - llvm::sys::path::append(P, "lib", "libLLVMFuzzer.a"); - CmdArgs.push_back(Args.MakeArgString(P)); - - // Libfuzzer is written in C++ and requires libcxx. - AddCXXStdlibLibArgs(Args, CmdArgs); -} - StringRef Darwin::getPlatformFamily() const { switch (TargetPlatform) { case DarwinPlatformKind::MacOS: @@ -1003,11 +991,12 @@ void DarwinClang::AddLinkSanitizerLibArgs(const ArgList &Args, ArgStringList &CmdArgs, - StringRef Sanitizer) const { + StringRef Sanitizer, + bool Shared) const { AddLinkRuntimeLib( Args, CmdArgs, (Twine("libclang_rt.") + Sanitizer + "_" + - getOSLibraryNameSuffix() + "_dynamic.dylib").str(), + getOSLibraryNameSuffix() + (Shared ? "_dynamic.dylib" : ".a")).str(), /*AlwaysLink*/ true, /*IsEmbedded*/ false, /*AddRPath*/ true); } @@ -1053,8 +1042,12 @@ AddLinkSanitizerLibArgs(Args, CmdArgs, "ubsan"); if (Sanitize.needsTsanRt()) AddLinkSanitizerLibArgs(Args, CmdArgs, "tsan"); - if (Sanitize.needsFuzzer() && !Args.hasArg(options::OPT_dynamiclib)) - AddFuzzerLinkArgs(Args, CmdArgs); + if (Sanitize.needsFuzzer() && !Args.hasArg(options::OPT_dynamiclib)) { + AddLinkSanitizerLibArgs(Args, CmdArgs, "fuzzer", /*shared=*/false); + + // Libfuzzer is written in C++ and requires libcxx. + AddCXXStdlibLibArgs(Args, CmdArgs); + } if (Sanitize.needsStatsRt()) { StringRef OS = isTargetMacOS() ? "osx" : "iossim"; AddLinkRuntimeLib(Args, CmdArgs, Index: cfe/trunk/test/Driver/fuzzer.c =================================================================== --- cfe/trunk/test/Driver/fuzzer.c +++ cfe/trunk/test/Driver/fuzzer.c @@ -2,7 +2,7 @@ // RUN: %clang -fsanitize=fuzzer %s -target x86_64-apple-darwin14 -### 2>&1 | FileCheck --check-prefixes=CHECK-FUZZER-LIB,CHECK-COVERAGE-FLAGS %s // -// CHECK-FUZZER-LIB: libLLVMFuzzer.a +// CHECK-FUZZER-LIB: libclang_rt.libfuzzer // CHECK-COVERAGE: -fsanitize-coverage-trace-pc-guard // CHECK-COVERAGE-SAME: -fsanitize-coverage-indirect-calls // CHECK-COVERAGE-SAME: -fsanitize-coverage-trace-cmp @@ -19,11 +19,11 @@ // Check that we don't link in libFuzzer.a when producing a shared object. // RUN: %clang -fsanitize=fuzzer %s -shared -o %t.so -### 2>&1 | FileCheck --check-prefixes=CHECK-NOLIB-SO %s -// CHECK-NOLIB-SO-NOT: libLLVMFuzzer.a +// CHECK-NOLIB-SO-NOT: libclang_rt.libfuzzer // Check that we don't link in libFuzzer when compiling with -fsanitize=fuzzer-no-link. // RUN: %clang -fsanitize=fuzzer-no-link %s -target x86_64-apple-darwin14 -### 2>&1 | FileCheck --check-prefixes=CHECK-NOLIB,CHECK-COV %s -// CHECK-NOLIB-NOT: libLLVMFuzzer.a +// CHECK-NOLIB-NOT: libclang_rt.libfuzzer // CHECK-COV: -fsanitize-coverage-trace-pc-guard // RUN: %clang -fsanitize=fuzzer -fsanitize-coverage=trace-pc %s -### 2>&1 | FileCheck --check-prefixes=CHECK-MSG %s