Index: projects/compiler-rt/lib/asan/CMakeLists.txt =================================================================== --- projects/compiler-rt/lib/asan/CMakeLists.txt +++ projects/compiler-rt/lib/asan/CMakeLists.txt @@ -1,12 +1,5 @@ # Build for the AddressSanitizer runtime support library. -if(APPLE) -# Don't set rpath for the ASan libraries. Developers are encouraged to ship -# their binaries together with the corresponding ASan runtime libraries, -# so they'll anyway need to fix the rpath and the install name. -set(CMAKE_BUILD_WITH_INSTALL_RPATH OFF) -endif() - set(ASAN_SOURCES asan_allocator2.cc asan_activation.cc Index: projects/compiler-rt/test/asan/TestCases/Darwin/interface_symbols_darwin.c =================================================================== --- projects/compiler-rt/test/asan/TestCases/Darwin/interface_symbols_darwin.c +++ projects/compiler-rt/test/asan/TestCases/Darwin/interface_symbols_darwin.c @@ -5,9 +5,7 @@ // RUN: %clang_asan -dead_strip -O2 %s -o %t.exe // RUN: rm -f %t.symbols %t.interface -// RUN: nm -g `otool -L %t.exe | grep "asan_osx_dynamic.dylib" | \ -// RUN: tr -d '\011' | \ -// RUN: sed "s/.dylib.*/.dylib/"` \ +// RUN: nm -g `%clang_asan %s -fsanitize=address -### 2>&1 | grep "libclang_rt.asan_osx_dynamic.dylib" | sed -e 's/.*"\(.*libclang_rt.asan_osx_dynamic.dylib\)".*/\1/'` \ // RUN: | grep " T " | sed "s/.* T //" \ // RUN: | grep "__asan_" | sed "s/___asan_/__asan_/" \ // RUN: | sed -E "s/__asan_init_v[0-9]+/__asan_init/" \ Index: tools/clang/lib/Driver/ToolChains.h =================================================================== --- tools/clang/lib/Driver/ToolChains.h +++ tools/clang/lib/Driver/ToolChains.h @@ -236,7 +236,8 @@ llvm::opt::ArgStringList &CmdArgs, StringRef DarwinStaticLib, bool AlwaysLink = false, - bool IsEmbedded = false) const; + bool IsEmbedded = false, + bool AddRPath = false) const; /// } /// @name ToolChain Implementation Index: tools/clang/lib/Driver/ToolChains.cpp =================================================================== --- tools/clang/lib/Driver/ToolChains.cpp +++ tools/clang/lib/Driver/ToolChains.cpp @@ -292,16 +292,36 @@ void MachO::AddLinkRuntimeLib(const ArgList &Args, ArgStringList &CmdArgs, StringRef DarwinStaticLib, bool AlwaysLink, - bool IsEmbedded) const { - SmallString<128> P(getDriver().ResourceDir); - llvm::sys::path::append(P, "lib", IsEmbedded ? "macho_embedded" : "darwin", - DarwinStaticLib); + bool IsEmbedded, bool AddRPath) const { + SmallString<128> Dir(getDriver().ResourceDir); + llvm::sys::path::append(Dir, "lib", IsEmbedded ? "macho_embedded" : "darwin"); + SmallString<128> P(Dir); + llvm::sys::path::append(P, DarwinStaticLib); + // For now, allow missing resource libraries to support developers who may // not have compiler-rt checked out or integrated into their build (unless // we explicitly force linking with this library). if (AlwaysLink || llvm::sys::fs::exists(P.str())) CmdArgs.push_back(Args.MakeArgString(P.str())); + + // Adding the rpaths might negatively interact when other rpaths are involved, + // so we should make sure we add the rpaths last, after all user-specified + // rpaths. This is currently true from this place, but we need to be + // careful if this function is ever called before user's rpaths are emitted. + if (AddRPath) { + assert(DarwinStaticLib.endswith(".dylib") && "must be a dynamic library"); + + // Add @executable_path to rpath to support having the dylib copied with + // the executable. + CmdArgs.push_back("-rpath"); + CmdArgs.push_back("@executable_path"); + + // Add the path to the resource dir to rpath to support using the dylib + // from the default location without copying. + CmdArgs.push_back("-rpath"); + CmdArgs.push_back(Args.MakeArgString(Dir.str())); + } } void DarwinClang::AddLinkRuntimeLibArgs(const ArgList &Args, @@ -379,12 +399,12 @@ if (isTargetMacOS()) { AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.asan_osx_dynamic.dylib", - true); + true, false, true); } else { if (isTargetIOSSimulator()) { AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.asan_iossim_dynamic.dylib", - true); + true, false, true); } } } Index: tools/clang/test/Driver/darwin-asan-rpath.c =================================================================== --- tools/clang/test/Driver/darwin-asan-rpath.c +++ tools/clang/test/Driver/darwin-asan-rpath.c @@ -0,0 +1,7 @@ +// RUN: %clang -x c -target x86_64-apple-darwin -fsanitize=address %s -### 2>&1 | FileCheck --check-prefix=CHECK-OSX %s +// CHECK-OSX: "{{.*}}/lib/darwin/libclang_rt.asan_osx_dynamic.dylib" +// CHECK-OSX: "-rpath" "{{.*}}/lib/darwin" + +// RUN: %clang -x c -target x86_64-apple-darwin -mios-simulator-version-min=7.0 -fsanitize=address %s -### 2>&1 | FileCheck --check-prefix=CHECK-IOSSIM %s +// CHECK-IOSSIM: "{{.*}}/lib/darwin/libclang_rt.asan_iossim_dynamic.dylib" +// CHECK-IOSSIM: "-rpath" "{{.*}}/lib/darwin"