[PATCH 1/1] perf build: Fix Python extension build with GCC 16 hardening

From: Jan Polensky

Date: Wed May 20 2026 - 09:03:31 EST


The Python extension build fails with modern GCC versions (16+) due to
the new hardening feature that prevents dynamic relocations in read-only
segments:

/usr/bin/ld.bfd: error: read-only segment has dynamic relocations
collect2: error: ld returned 1 exit status

This occurs when building the perf Python bindings because the linker
enforces stricter security policies by default.

Add -Wl,-z,notext to LDSHARED to explicitly allow text relocations in
the Python extension shared object. This is necessary for the Python
C extension to link successfully while maintaining compatibility with
the Python build system.

The flag only affects the Python extension build and does not impact
the security of the main perf binary or other components.

Tested on Fedora 44 with GCC 16.1.1 20260501 (Red Hat 16.1.1-1) and
Python 3.14.4 on s390x architecture.

Reported-by: Thomas Richter <tmricht@xxxxxxxxxxxxx>
Signed-off-by: Jan Polensky <japo@xxxxxxxxxxxxx>
---
tools/perf/Makefile.perf | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tools/perf/Makefile.perf b/tools/perf/Makefile.perf
index bfb37f527241..12af424dd5c1 100644
--- a/tools/perf/Makefile.perf
+++ b/tools/perf/Makefile.perf
@@ -749,7 +749,7 @@ all: shell_compatibility_test $(ALL_PROGRAMS) $(LANG_BINDINGS) $(OTHER_PROGRAMS)
$(shell [ -d '$(OUTPUT)python' ] || mkdir -p '$(OUTPUT)python')

$(OUTPUT)python/perf$(PYTHON_EXTENSION_SUFFIX): util/python.c util/setup.py $(PERFLIBS_PY)
- $(QUIET_GEN)LDSHARED="$(CC) -pthread -shared" \
+ $(QUIET_GEN)LDSHARED="$(CC) -pthread -shared -Wl,-z,notext" \
CFLAGS='$(CFLAGS)' LDFLAGS='$(LDFLAGS) $(LIBS_PY)' \
$(PYTHON_WORD) util/setup.py \
--quiet build_ext; \
--
2.51.0