Fix autodetection of linker flags

Flags passed through clang to linker get -Wl, stripped in the error message
(e.g. -Wl,-no-as-needed will be reported as "ld: unknwon option:
-no-as-needed"). This cl fixes the autodetection to expect the stripped variant.

Fixes #5468.

RELNOTES: None.
PiperOrigin-RevId: 203341563
diff --git a/tools/cpp/unix_cc_configure.bzl b/tools/cpp/unix_cc_configure.bzl
index 3141b0b..d1ef438 100644
--- a/tools/cpp/unix_cc_configure.bzl
+++ b/tools/cpp/unix_cc_configure.bzl
@@ -162,8 +162,8 @@
     ])
     return result.stderr.find(option) == -1
 
-def _is_linker_option_supported(repository_ctx, cc, option):
-    """Checks that `option` is supported by the C compiler. Doesn't %-escape the option."""
+def _is_linker_option_supported(repository_ctx, cc, option, pattern):
+    """Checks that `option` is supported by the C linker. Doesn't %-escape the option."""
     result = repository_ctx.execute([
         cc,
         option,
@@ -171,7 +171,7 @@
         "/dev/null",
         str(repository_ctx.path("tools/cpp/empty.cc")),
     ])
-    return result.stderr.find(option) == -1
+    return result.stderr.find(pattern) == -1
 
 def _is_gold_supported(repository_ctx, cc):
     """Checks that `gold` is supported by the C compiler."""
@@ -193,9 +193,9 @@
     """Returns `[option]` if supported, `[]` otherwise. Doesn't %-escape the option."""
     return [option] if _is_compiler_option_supported(repository_ctx, cc, option) else []
 
-def _add_linker_option_if_supported(repository_ctx, cc, option):
+def _add_linker_option_if_supported(repository_ctx, cc, option, pattern):
     """Returns `[option]` if supported, `[]` otherwise. Doesn't %-escape the option."""
-    return [option] if _is_linker_option_supported(repository_ctx, cc, option) else []
+    return [option] if _is_linker_option_supported(repository_ctx, cc, option, pattern) else []
 
 def _get_no_canonical_prefixes_opt(repository_ctx, cc):
     # If the compiler sometimes rewrites paths in the .d files without symlinks
@@ -270,10 +270,12 @@
             repository_ctx,
             cc,
             "-Wl,-no-as-needed",
+            "-no-as-needed",
         ) + _add_linker_option_if_supported(
             repository_ctx,
             cc,
             "-Wl,-z,relro,-z,now",
+            "-z,relro,-z,now",
         ) + (
             [
                 "-undefined",
@@ -360,7 +362,12 @@
             "-fdata-sections",
         ],
         "linker_flag": (
-            [] if darwin else _add_linker_option_if_supported(repository_ctx, cc, "-Wl,--gc-sections")
+            [] if darwin else _add_linker_option_if_supported(
+                repository_ctx,
+                cc,
+                "-Wl,--gc-sections",
+                "-gc-sections",
+            )
         ),
     }