diff options
-rw-r--r-- | lib/Driver/ToolChains.cpp | 25 | ||||
-rw-r--r-- | test/Driver/linux-ld.c | 14 |
2 files changed, 33 insertions, 6 deletions
diff --git a/lib/Driver/ToolChains.cpp b/lib/Driver/ToolChains.cpp index 1eb7c9cdf2..67697569c4 100644 --- a/lib/Driver/ToolChains.cpp +++ b/lib/Driver/ToolChains.cpp @@ -2059,10 +2059,20 @@ Linux::Linux(const Driver &D, const llvm::Triple &Triple, const ArgList &Args) addPathIfExists((GCCInstallation.getInstallPath() + GCCInstallation.getMultiarchSuffix()), Paths); - addPathIfExists(LibPath + "/../" + GCCTriple.str() + "/lib/../" + Multilib, - Paths); - addPathIfExists(LibPath + "/" + MultiarchTriple, Paths); - addPathIfExists(LibPath + "/../" + Multilib, Paths); + + // If the GCC installation we found is inside of the sysroot, we want to + // prefer libraries installed in the parent prefix of the GCC installation. + // It is important to *not* use these paths when the GCC installation is + // outside of the system root as that can pick up un-intented libraries. + // This usually happens when there is an external cross compiler on the + // host system, and a more minimal sysroot available that is the target of + // the cross. + if (StringRef(LibPath).startswith(SysRoot)) { + addPathIfExists(LibPath + "/../" + GCCTriple.str() + "/lib/../" + Multilib, + Paths); + addPathIfExists(LibPath + "/" + MultiarchTriple, Paths); + addPathIfExists(LibPath + "/../" + Multilib, Paths); + } } addPathIfExists(SysRoot + "/lib/" + MultiarchTriple, Paths); addPathIfExists(SysRoot + "/lib/../" + Multilib, Paths); @@ -2081,8 +2091,11 @@ Linux::Linux(const Driver &D, const llvm::Triple &Triple, const ArgList &Args) const llvm::Triple &GCCTriple = GCCInstallation.getTriple(); if (!GCCInstallation.getMultiarchSuffix().empty()) addPathIfExists(GCCInstallation.getInstallPath(), Paths); - addPathIfExists(LibPath + "/../" + GCCTriple.str() + "/lib", Paths); - addPathIfExists(LibPath, Paths); + + if (StringRef(LibPath).startswith(SysRoot)) { + addPathIfExists(LibPath + "/../" + GCCTriple.str() + "/lib", Paths); + addPathIfExists(LibPath, Paths); + } } addPathIfExists(SysRoot + "/lib", Paths); addPathIfExists(SysRoot + "/usr/lib", Paths); diff --git a/test/Driver/linux-ld.c b/test/Driver/linux-ld.c index 7c6cac71b9..1ade7f1d05 100644 --- a/test/Driver/linux-ld.c +++ b/test/Driver/linux-ld.c @@ -92,6 +92,20 @@ // CHECK-64-TO-32: "-L[[SYSROOT]]/usr/lib" // // RUN: %clang -no-canonical-prefixes %s -### -o %t.o 2>&1 \ +// RUN: -target x86_64-unknown-linux -m32 \ +// RUN: -gcc-toolchain %S/Inputs/multilib_64bit_linux_tree/usr \ +// RUN: --sysroot=%S/Inputs/multilib_32bit_linux_tree \ +// RUN: | FileCheck --check-prefix=CHECK-64-TO-32-SYSROOT %s +// CHECK-64-TO-32-SYSROOT: "{{.*}}ld{{(.exe)?}}" "--sysroot=[[SYSROOT:[^"]+]]" +// CHECK-64-TO-32-SYSROOT: "{{.*}}/usr/lib/gcc/x86_64-unknown-linux/4.6.0/32/crtbegin.o" +// CHECK-64-TO-32-SYSROOT: "-L{{[^"]*}}/Inputs/multilib_64bit_linux_tree/usr/lib/gcc/x86_64-unknown-linux/4.6.0/32" +// CHECK-64-TO-32-SYSROOT: "-L[[SYSROOT]]/lib/../lib32" +// CHECK-64-TO-32-SYSROOT: "-L[[SYSROOT]]/usr/lib/../lib32" +// CHECK-64-TO-32-SYSROOT: "-L{{[^"]*}}/Inputs/multilib_64bit_linux_tree/usr/lib/gcc/x86_64-unknown-linux/4.6.0" +// CHECK-64-TO-32-SYSROOT: "-L[[SYSROOT]]/lib" +// CHECK-64-TO-32-SYSROOT: "-L[[SYSROOT]]/usr/lib" +// +// RUN: %clang -no-canonical-prefixes %s -### -o %t.o 2>&1 \ // RUN: -target i386-unknown-linux -m32 \ // RUN: -ccc-install-dir %S/Inputs/fake_install_tree/bin \ // RUN: --sysroot=%S/Inputs/basic_linux_tree \ |