Stop the build if the linker produces an error code. Otherwise, the numbers in
the linker's error message can be misinterpreted as a linker version number, the
build completes successfully, and the resulting compiler doesn't work because
the supposed linker version number is invalid.

https://github.com/llvm/llvm-project/issues/186194
--- a/autoconf/m4/link_options.m4
+++ b/autoconf/m4/link_options.m4
@@ -6,13 +6,15 @@
 AC_DEFUN([AC_LINK_GET_VERSION],
   [AC_CACHE_CHECK([for linker version],[llvm_cv_link_version],
   [
-   version_string="$(${LD:-ld} -v 2>&1 | head -1)"
+   AS_IF([version_string=$(${LD:-ld} -v 2>&1)], [],
+         [ld_status=$?
+          AC_MSG_ERROR([${LD:-ld} failed with status $ld_status])])
 
    # Check for ld64.
-   if (echo "$version_string" | grep -q "ld64"); then
-     llvm_cv_link_version=$(echo "$version_string" | sed -e "s#.*ld64-\([^ ]*\)\( (.*)\)\{0,1\}#\1#")
+   if (echo "$version_string" | head -1 | grep -q "ld64"); then
+     llvm_cv_link_version=$(echo "$version_string" | sed -ne "1s#.*ld64-\([^ ]*\)\( (.*)\)\{0,1\}#\1#p")
    else
-     llvm_cv_link_version=$(echo "$version_string" | sed -e "s#[^0-9]*\([0-9.]*\).*#\1#")
+     llvm_cv_link_version=$(echo "$version_string" | sed -ne "1s#[^0-9]*\([0-9.]*\).*#\1#p")
    fi
   ])
   AC_DEFINE_UNQUOTED([HOST_LINK_VERSION],"$llvm_cv_link_version",
--- a/configure
+++ b/configure
@@ -7337,13 +7337,21 @@
   echo $ECHO_N "(cached) $ECHO_C" >&6
 else
 
-   version_string="$(${LD:-ld} -v 2>&1 | head -1)"
+   if version_string=$(${LD:-ld} -v 2>&1); then
+  :
+else
+  ld_status=$?
+          { { echo "$as_me:$LINENO: error: ${LD:-ld} failed with status $ld_status" >&5
+echo "$as_me: error: ${LD:-ld} failed with status $ld_status" >&2;}
+   { (exit 1); exit 1; }; }
+fi
+
 
    # Check for ld64.
-   if (echo "$version_string" | grep -q "ld64"); then
-     llvm_cv_link_version=$(echo "$version_string" | sed -e "s#.*ld64-\([^ ]*\)\( (.*)\)\{0,1\}#\1#")
+   if (echo "$version_string" | head -1 | grep -q "ld64"); then
+     llvm_cv_link_version=$(echo "$version_string" | sed -ne "1s#.*ld64-\([^ ]*\)\( (.*)\)\{0,1\}#\1#p")
    else
-     llvm_cv_link_version=$(echo "$version_string" | sed -e "s#[^0-9]*\([0-9.]*\).*#\1#")
+     llvm_cv_link_version=$(echo "$version_string" | sed -ne "1s#[^0-9]*\([0-9.]*\).*#\1#p")
    fi
 
 fi
