When copying dynamic libraries to binary, copy them into the runfiles as well.

RELNOTES: None.
PiperOrigin-RevId: 256184273
diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcBinary.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcBinary.java
index ae723c9..330740c 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcBinary.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcBinary.java
@@ -583,6 +583,13 @@
     // logical since all symlinked libraries will be linked anyway and would
     // not require manual loading but if we do, then we would need to collect
     // their names and use a different constructor below.
+    NestedSetBuilder<Artifact> transitiveArtifacts =
+        NestedSetBuilder.<Artifact>stableOrder()
+            .addTransitive(filesToBuild)
+            .addTransitive(extraLinkTimeRuntimeLibraries.build());
+    if (featureConfiguration.isEnabled(CppRuleClasses.COPY_DYNAMIC_LIBRARIES_TO_BINARY)) {
+      transitiveArtifacts.addTransitive(copiedRuntimeDynamicLibraries);
+    }
     Runfiles runfiles =
         collectRunfiles(
             ruleContext,
@@ -593,10 +600,7 @@
             ccLinkingOutputs,
             ccCompilationContext,
             linkingMode,
-            NestedSetBuilder.<Artifact>stableOrder()
-                .addTransitive(filesToBuild)
-                .addTransitive(extraLinkTimeRuntimeLibraries.build())
-                .build(),
+            transitiveArtifacts.build(),
             fakeLinkerInputs,
             fake,
             compilationHelper.getCompilationUnitSources(),
@@ -656,11 +660,6 @@
     }
 
     if (copiedRuntimeDynamicLibraries != null) {
-      // When COPY_DYNAMIC_LIBRARIES_TO_BINARY is enabled, runtime dynamic libraries should be
-      // copied to the binary's directory. Therefore, we add them to HIDDEN_TOP_LEVEL output group
-      // to make sure they are built for the target and runtime_dynamic_libraries output group
-      // to make them easier to access.
-      ruleBuilder.addOutputGroup(OutputGroupInfo.HIDDEN_TOP_LEVEL, copiedRuntimeDynamicLibraries);
       ruleBuilder.addOutputGroup("runtime_dynamic_libraries", copiedRuntimeDynamicLibraries);
     }