diff --git a/cpython-unix/build-cpython.sh b/cpython-unix/build-cpython.sh index 6df845881..d112f069a 100755 --- a/cpython-unix/build-cpython.sh +++ b/cpython-unix/build-cpython.sh @@ -85,6 +85,19 @@ if [ -n "${CROSS_COMPILING}" ]; then fi fi +# Inject -Bsymbolic* on shared library to force direct symbol resolution +# and potentially unlock more compiler+linker optimizations. +# +# Patch is safe on all arches but runs into context conflict on macOS. +# It isn't needed for macOS. So workaround by not applying there. +if [[ "${PYBUILD_PLATFORM}" != macos* ]]; then + if [ -n "${PYTHON_MEETS_MINIMUM_VERSION_3_11}" ]; then + patch -p1 -i "${ROOT}/patch-configure-linker-symbolic.patch" + else + patch -p1 -i "${ROOT}/patch-configure-linker-symbolic-3.10.patch" + fi +fi + # LIBTOOL_CRUFT is unused and breaks cross-compiling on macOS. Nuke it. # Submitted upstream at https://github.com/python/cpython/pull/101048. if [ -n "${PYTHON_MEETS_MAXIMUM_VERSION_3_11}" ]; then diff --git a/cpython-unix/patch-configure-linker-symbolic-3.10.patch b/cpython-unix/patch-configure-linker-symbolic-3.10.patch new file mode 100644 index 000000000..045d1c4e3 --- /dev/null +++ b/cpython-unix/patch-configure-linker-symbolic-3.10.patch @@ -0,0 +1,23 @@ +diff --git a/configure.ac b/configure.ac +index ac3be3850a9..88a2027f517 100644 +--- a/configure.ac ++++ b/configure.ac +@@ -2713,8 +2713,16 @@ then + fi + ;; + Linux*|GNU*|QNX*|VxWorks*) +- LDSHARED='$(CC) -shared' +- LDCXXSHARED='$(CXX) -shared';; ++ # See https://maskray.me/blog/2021-05-16-elf-interposition-and-bsymbolic. ++ # This is related to the -fno-semantic-interposition optimization that ++ # --enable-optimizations engages automatically. It changes the linkage ++ # behavior so the runtime loader searches the current DSO first before ++ # falling back to the default search mechanism of the executable plus ++ # all its loaded DSOs. It has much the same effect as ++ # -fno-semantic-interposition but can also enable optimizations across ++ # translation units. ++ LDSHARED='$(CC) -shared -Wl,-Bsymbolic-functions' ++ LDCXXSHARED='$(CXX) -shared -Wl,-Bsymbolic-functions';; + FreeBSD*) + if [[ "`$CC -dM -E -