diff --git a/meta/lib/oeqa/selftest/cases/gcc.py b/meta/lib/oeqa/selftest/cases/gcc.py
index efae5bedf4..d651db5272 100644
--- a/meta/lib/oeqa/selftest/cases/gcc.py
+++ b/meta/lib/oeqa/selftest/cases/gcc.py
@@ -3,7 +3,6 @@
 #
 # SPDX-License-Identifier: MIT
 #
-import hashlib
 import subprocess
 import os
 import time
@@ -96,7 +95,7 @@ class GccSelfTestBase(OESelftestTestCase, OEPTestResultTestCase):
 
             # With ControlPersist=yes, never times out on its own, so close it
             # explicitly once all test shards have finished.
-            controlpath = "/tmp/oe-gcc-ssh-" + hashlib.sha1(get_bb_var("B", "gcc-runtime").encode()).hexdigest()[:12]
+            controlpath = get_bb_var("TOOLCHAIN_TEST_SSH_CONTROLPATH", "gcc-runtime")
             try:
                 return self.run_check(*args, ssh=qemu.ip, **kwargs)
             finally:
diff --git a/meta/recipes-devtools/gcc/gcc-testsuite.inc b/meta/recipes-devtools/gcc/gcc-testsuite.inc
index db08a01f3d..19dffb9ea5 100644
--- a/meta/recipes-devtools/gcc/gcc-testsuite.inc
+++ b/meta/recipes-devtools/gcc/gcc-testsuite.inc
@@ -5,6 +5,8 @@ TOOLCHAIN_TEST_HOST ??= "localhost"
 TOOLCHAIN_TEST_HOST_USER ??= "root"
 TOOLCHAIN_TEST_HOST_PORT ??= "2222"
 
+TOOLCHAIN_TEST_SSH_CONTROLPATH ??= "/tmp/oe-gcc-ssh-${@__import__('hashlib').sha1(d.getVar('B').encode()).hexdigest()[:12]}"
+
 MAKE_CHECK_BOARDFLAGS ??= ""
 MAKE_CHECK_BOARDARGS ??= "--target_board=${TOOLCHAIN_TEST_TARGET}${MAKE_CHECK_BOARDFLAGS}"
 
@@ -80,7 +82,7 @@ python check_prepare() {
         return "\n".join(content)
 
     def generate_remote_ssh_linux_config(d):
-        import hashlib, os
+        import os
         content = []
         content.append('load_generic_config "unix"')
         content.append('process_multilib_options ""')
@@ -92,7 +94,7 @@ python check_prepare() {
         # Use SSH multiplexing to share one master connection across GCC tests.
         # Keep the master alive until the selftest explicitly closes it.
         # Use a per-build ControlPath to avoid conflicts between builds.
-        controlpath = "/tmp/oe-gcc-ssh-" + hashlib.sha1(d.getVar("B").encode()).hexdigest()[:12]
+        controlpath = d.getVar("TOOLCHAIN_TEST_SSH_CONTROLPATH")
         # remove stale socket left by a previous stopped run of this build
         if os.path.exists(controlpath):
             os.remove(controlpath)
