Index: test/Driver/B-opt.c
===================================================================
--- test/Driver/B-opt.c	(revision 0)
+++ test/Driver/B-opt.c	(revision 0)
@@ -0,0 +1,27 @@
+// REQUIRES: shell
+//
+// RUN: rm -rf %t.dir
+// RUN: mkdir -p %t.dir/dir1 %t.dir/dir2 %t.dir/dir3
+// RUN: touch %t.dir/dir1/i386-unknown-linux-ld
+// RUN: chmod u+x %t.dir/dir1/i386-unknown-linux-ld
+// RUN: touch %t.dir/dir1/ld
+// RUN: chmod u+x %t.dir/dir1/ld
+// RUN: touch %t.dir/dir2/ld
+// RUN: chmod u+x %t.dir/dir2/ld
+// RUN: touch %t.dir/dir3/prefix-ld
+// RUN: chmod u+x %t.dir/dir3/prefix-ld
+//
+// RUN: %clang %s -### -o %t.o -target i386-unknown-linux \
+// RUN:     -B %t.dir/dir1 2>&1 \
+// RUN:   | FileCheck --check-prefix=CHECK-B-OPT-TRIPLE %s
+// CHECK-B-OPT-TRIPLE: "{{.*}}.dir/dir1/i386-unknown-linux-ld"
+//
+// RUN: %clang %s -### -o %t.o -target i386-unknown-linux \
+// RUN:     -B %t.dir/dir2 2>&1 \
+// RUN:   | FileCheck --check-prefix=CHECK-B-OPT-DIR %s
+// CHECK-B-OPT-DIR: "{{.*}}.dir/dir2/ld"
+//
+// RUN: %clang %s -### -o %t.o -target i386-unknown-linux \
+// RUN:     -B %t.dir/dir3/prefix- 2>&1 \
+// RUN:   | FileCheck --check-prefix=CHECK-B-OPT-PREFIX %s
+// CHECK-B-OPT-PREFIX: "{{.*}}.dir/dir3/prefix-ld"
Index: lib/Driver/Driver.cpp
===================================================================
--- lib/Driver/Driver.cpp	(revision 166388)
+++ lib/Driver/Driver.cpp	(working copy)
@@ -1597,12 +1597,19 @@
   // attempting to use this prefix when looking for program paths.
   for (Driver::prefix_list::const_iterator it = PrefixDirs.begin(),
        ie = PrefixDirs.end(); it != ie; ++it) {
-    llvm::sys::Path P(*it);
-    P.appendComponent(TargetSpecificExecutable);
-    if (P.canExecute()) return P.str();
-    P.eraseComponent();
-    P.appendComponent(Name);
-    if (P.canExecute()) return P.str();
+    bool IsDirectory;
+    if (!llvm::sys::fs::is_directory(*it, IsDirectory) && IsDirectory) {
+      llvm::sys::Path P(*it);
+      P.appendComponent(TargetSpecificExecutable);
+      if (P.canExecute()) return P.str();
+      P.eraseComponent();
+      P.appendComponent(Name);
+      if (P.canExecute()) return P.str();
+    }
+    else {
+      llvm::sys::Path P(*it + Name);
+      if (P.canExecute()) return P.str();
+    }
   }
 
   const ToolChain::path_list &List = TC.getProgramPaths();
