perf build: Fix Python extension build with GCC 16 hardening
PATCH 1/1 perf build: Fix Python extension build with GCC 16 hardening
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.
--- tools/perf/Makefile.perf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tools/perf/Makefile.perf +1 -1
@@ -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; \
Signature · 1 line
2.51.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] The patch explicitly allows text relocations in the Python extension shared object (`perf.so`)…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] The patch explicitly allows text relocations in the Python extension shared object (perf.so) by adding -Wl,-z,notext, masking the root cause of missing -fPIC flags and breaking W^X (Write XOR Execute) security protections. --
commit f3832fb0ad0d1f9c2e69039b2a5110f77d220d5e
Author: Jan Polensky <[email protected]>
perf build: Fix Python extension build with GCC 16 hardening
This commit addresses a build failure in the Python extension caused by GCC 16 stricter security policies regarding dynamic relocations in read-only segments. It adds the -Wl,-z,notext flag to the linker to explicitly allow text relocations in the Python extension shared object.
tools/perf/Makefile.perf all: shell_compatibility_test $(ALL_PROGRAMS) $(LANG_BINDINGS) $(OTHER_PROGRAMS) · line 752
Quoted 10 lines
tools/perf/Makefile.perf +1
-1@@ -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" \
[Severity: Medium]
Does adding -Wl,-z,notext mask the root cause of the GCC 16 linker error?
The error regarding dynamic relocations in a read-only segment usually indicates that non-position-independent code from the static libraries is being linked into the shared library.
Using -Wl,-z,notext suppresses this error by allowing text relocations, which forces the dynamic linker to map the executable text segment as writable at load time. This breaks W^X security protections and weakens the security of any Python process loading the extension.
Should the required object files be compiled with -fPIC instead to properly resolve the linker error without compromising security?
Quoted 3 lines
tools/perf/Makefile.perf +0
-0CFLAGS='$(CFLAGS)' LDFLAGS='$(LDFLAGS) $(LIBS_PY)' \ $(PYTHON_WORD) util/setup.py \ --quiet build_ext; \
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Arnaldo Carvalho de Melo
maintainerQuoted 32 lines · On Wed, May 20, 2026 at 01:09:05PM +0000, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] The patch explicitly allows text relocations in the Python extension shared object (perf.so) by adding-Wl,-z,notext, masking the root cause of missing-fPICflags and breaking W^X (Write XOR Execute) security protections. --commit
f3832fb0ad0d1f9c2e69039b2a5110f77d220d5e
Author: Jan Polensky <[email protected]>perf build: Fix Python extension build with GCC 16 hardening
This commit addresses a build failure in the Python extension caused by GCC 16 stricter security policies regarding dynamic relocations in read-only segments. It adds the -Wl,-z,notext flag to the linker to explicitly allow text relocations in the Python extension shared object.
tools/perf/Makefile.perf all: shell_compatibility_test $(ALL_PROGRAMS) $(LANG_BINDINGS) $(OTHER_PROGRAMS) · line 752
Quoted 10 lines
tools/perf/Makefile.perf +1
-1@@ -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" \[Severity: Medium]
Does adding -Wl,-z,notext mask the root cause of the GCC 16 linker error?The error regarding dynamic relocations in a read-only segment usually indicates that non-position-independent code from the static libraries is being linked into the shared library.
Quoted 7 lines
Using -Wl,-z,notext suppresses this error by allowing text relocations, which forces the dynamic linker to map the executable text segment as writable at load time. This breaks W^X security protections and weakens the security of any Python process loading the extension.
Should the required object files be compiled with -fPIC instead to properly resolve the linker error without compromising security?
So I asked Claude about this and he suggests doing like other parts of perf and using -fPIC, can you try to do that?
---------------------------------------
● Sashiko is right. -Wl,-z,notext is a band-aid that weakens security —
it makes the text segment writable at load time, defeating W^X.
The root cause is that PERFLIBS_PY (the static libraries linked into the Python .so) are compiled without -fPIC. When you link non-PIC .a archives into a shared library, the linker needs text relocations. GCC 16 correctly rejects this by default.
The proper fix is to build the static libraries that go into the Python extension with -fPIC. The perf build system already has precedent for this — look for how libperf and libsubcmd handle PIC builds. The Python extension's dependencies need a parallel -fPIC build, or the existing objects need to be compiled with -fPIC when the Python binding is enabled.
The -Wl,-z,notext approach would also cause issues on distros that enforce RELRO + BIND_NOW hardening, and some package builders would reject the resulting .so outright.
- Arnaldo
Quoted 61 lines · Arnaldo Carvalho de Melo
Quoted 32 lines · On Wed, May 20, 2026 at 01:09:05PM +0000, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] The patch explicitly allows text relocations in the Python extension shared object (perf.so) by adding-Wl,-z,notext, masking the root cause of missing-fPICflags and breaking W^X (Write XOR Execute) security protections. --commit
f3832fb0ad0d1f9c2e69039b2a5110f77d220d5e
Author: Jan Polensky <[email protected]>perf build: Fix Python extension build with GCC 16 hardening
This commit addresses a build failure in the Python extension caused by GCC 16 stricter security policies regarding dynamic relocations in read-only segments. It adds the -Wl,-z,notext flag to the linker to explicitly allow text relocations in the Python extension shared object.
tools/perf/Makefile.perf all: shell_compatibility_test $(ALL_PROGRAMS) $(LANG_BINDINGS) $(OTHER_PROGRAMS) · line 752
Quoted 10 lines
tools/perf/Makefile.perf +1
-1@@ -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" \[Severity: Medium]
Does adding -Wl,-z,notext mask the root cause of the GCC 16 linker error?The error regarding dynamic relocations in a read-only segment usually indicates that non-position-independent code from the static libraries is being linked into the shared library.
Quoted 7 lines
Using -Wl,-z,notext suppresses this error by allowing text relocations, which forces the dynamic linker to map the executable text segment as writable at load time. This breaks W^X security protections and weakens the security of any Python process loading the extension.
Should the required object files be compiled with -fPIC instead to properly resolve the linker error without compromising security?
So I asked Claude about this and he suggests doing like other parts of perf and using -fPIC, can you try to do that?
---------------------------------------
● Sashiko is right. -Wl,-z,notext is a band-aid that weakens security —
it makes the text segment writable at load time, defeating W^X.The root cause is that
PERFLIBS_PY(the static libraries linked into the Python .so) are compiled without -fPIC. When you link non-PIC .a archives into a shared library, the linker needs text relocations. GCC 16 correctly rejects this by default.The proper fix is to build the static libraries that go into the Python extension with -fPIC. The perf build system already has precedent for this — look for how libperf and libsubcmd handle PIC builds. The Python extension's dependencies need a parallel -fPIC build, or the existing objects need to be compiled with -fPIC when the Python binding is enabled.
Quoted 5 lines
The -Wl,-z,notext approach would also cause issues on distros that enforce RELRO +
BIND_NOWhardening, and some package builders would reject the resulting .so outright.- Arnaldo
Hi Arnaldo,
thanks for the pointer. I double-checked this on s390x and did some reduced-link testing for the ET_DYN / Python-extension case.
The failure is not triggered by the perf Python binding object alone.
A reduced reproducer succeeds when linking:
python.o
+ libapi.a
+ libperf.a
+ libsubcmd.a
+ libsymbol.a
+ libbpf.a
+ libperf-ui.a
The link starts to fail with:
/usr/bin/ld.bfd: error: read-only segment has dynamic relocations
as soon as libperf-util.a is added.
In my tree, libperf-util.a currently contains only perf-util-in.o, and replacing the archive with that single object still reproduces the same failure.
Reduced reproducer:
gcc -pthread -shared \
-Wl,-z,noexecstack \
-o /tmp/try-perf-util-only.so \
/root/linux/tools/perf/python_ext_build/tmp/root/linux/tools/perf/util/python.o \
-Wl,--whole-archive \
/root/linux/tools/perf/libapi/libapi.a \
/root/linux/tools/perf/libperf/libperf.a \
/root/linux/tools/perf/libsubcmd/libsubcmd.a \
/root/linux/tools/perf/libsymbol/libsymbol.a \
/root/linux/tools/perf/libbpf/libbpf.a \
/root/linux/tools/perf/libperf-ui.a \
-Wl,--no-whole-archive \
/root/perf-util-one/perf-util-in.o \
-lpython3.14 -lpthread -ldl -lm -lutilInspection of perf-util-in.o on s390x shows many direct R_390_PC32DBL relocations from .text against .data.rel.ro, .data.rel.ro.local, .data.rel.local, .bss, and .rodata.
Representative examples include:
arm64__associate_instruction_ops:
larl %r6 -> R_390_PC32DBL .data.rel.ro+0x2
loongarch__associate_ins_ops:
larl %r9 -> R_390_PC32DBL .data.rel.ro+0x22
larl %r9 -> R_390_PC32DBL .data.rel.ro+0x42 check_ppc_insn:
larl %r4 -> R_390_PC32DBL .data.rel.local+0x4c2
larl %r2 -> R_390_PC32DBL .data.rel.ro.local+0x22
larl %r1 -> R_390_PC32DBL .data.rel.local+0x2
larl %r2 -> R_390_PC32DBL .data.rel.ro.local+0x2 s390__associate_ins_ops:
larl %r10 -> R_390_PC32DBL .data.rel.ro+0x732
larl %r10 -> R_390_PC32DBL .data.rel.ro+0x752So from my reproducer, I don't think we can describe this simply as "all Python-extension dependency archives are missing -fPIC".
The issue narrows down much more specifically to perf-util-in.o in the ET_DYN link of the Python extension, and the direct text-to-data section relocations above look like the most relevant candidates.
So I agree that -Wl,-z,notext is only a workaround. I'd prefer to understand and fix the underlying relocation pattern in perf-util-in.o rather than merge that as-is.
If useful, I can send the relevant readelf/objdump snippets in a follow-up.
Best regards,
Jan
Ian Rogers
reviewerQuoted 140 lines · Jan Polensky
Quoted 61 lines · Arnaldo Carvalho de Melo
Quoted 32 lines · On Wed, May 20, 2026 at 01:09:05PM +0000, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] The patch explicitly allows text relocations in the Python extension shared object (perf.so) by adding-Wl,-z,notext, masking the root cause of missing-fPICflags and breaking W^X (Write XOR Execute) security protections. --commit
f3832fb0ad0d1f9c2e69039b2a5110f77d220d5e
Author: Jan Polensky <[email protected]>perf build: Fix Python extension build with GCC 16 hardening
This commit addresses a build failure in the Python extension caused by GCC 16 stricter security policies regarding dynamic relocations in read-only segments. It adds the -Wl,-z,notext flag to the linker to explicitly allow text relocations in the Python extension shared object.
tools/perf/Makefile.perf all: shell_compatibility_test $(ALL_PROGRAMS) $(LANG_BINDINGS) $(OTHER_PROGRAMS) · line 752
Quoted 10 lines
tools/perf/Makefile.perf +1
-1@@ -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" \[Severity: Medium]
Does adding -Wl,-z,notext mask the root cause of the GCC 16 linker error?The error regarding dynamic relocations in a read-only segment usually indicates that non-position-independent code from the static libraries is being linked into the shared library.
Quoted 7 lines
Using -Wl,-z,notext suppresses this error by allowing text relocations, which forces the dynamic linker to map the executable text segment as writable at load time. This breaks W^X security protections and weakens the security of any Python process loading the extension.
Should the required object files be compiled with -fPIC instead to properly resolve the linker error without compromising security?
So I asked Claude about this and he suggests doing like other parts of perf and using -fPIC, can you try to do that?
---------------------------------------
● Sashiko is right. -Wl,-z,notext is a band-aid that weakens security —
it makes the text segment writable at load time, defeating W^X.The root cause is that
PERFLIBS_PY(the static libraries linked into the Python .so) are compiled without -fPIC. When you link non-PIC .a archives into a shared library, the linker needs text relocations. GCC 16 correctly rejects this by default.The proper fix is to build the static libraries that go into the Python extension with -fPIC. The perf build system already has precedent for this — look for how libperf and libsubcmd handle PIC builds. The Python extension's dependencies need a parallel -fPIC build, or the existing objects need to be compiled with -fPIC when the Python binding is enabled.
Quoted 5 lines
The -Wl,-z,notext approach would also cause issues on distros that enforce RELRO +
BIND_NOWhardening, and some package builders would reject the resulting .so outright.- Arnaldo
Hi Arnaldo,
thanks for the pointer. I double-checked this on s390x and did some reduced-link testing for the
ET_DYN/ Python-extension case.The failure is not triggered by the perf Python binding object alone.
A reduced reproducer succeeds when linking:
python.o
+ libapi.a
+ libperf.a
+ libsubcmd.a
+ libsymbol.a
+ libbpf.a
+ libperf-ui.aThe link starts to fail with:
/usr/bin/ld.bfd: error: read-only segment has dynamic relocationsas soon as libperf-util.a is added.
In my tree, libperf-util.a currently contains only perf-util-in.o, and replacing the archive with that single object still reproduces the same failure.
Reduced reproducer:
gcc -pthread -shared \ -Wl,-z,noexecstack \ -o /tmp/try-perf-util-only.so \ /root/linux/tools/perf/python_ext_build/tmp/root/linux/tools/perf/util/python.o \ -Wl,--whole-archive \ /root/linux/tools/perf/libapi/libapi.a \ /root/linux/tools/perf/libperf/libperf.a \ /root/linux/tools/perf/libsubcmd/libsubcmd.a \ /root/linux/tools/perf/libsymbol/libsymbol.a \ /root/linux/tools/perf/libbpf/libbpf.a \ /root/linux/tools/perf/libperf-ui.a \ -Wl,--no-whole-archive \ /root/perf-util-one/perf-util-in.o \ -lpython3.14 -lpthread -ldl -lm -lutilInspection of perf-util-in.o on s390x shows many direct
R_390_PC32DBLrelocations from .text against .data.rel.ro, .data.rel.ro.local, .data.rel.local, .bss, and .rodata.Representative examples include:
arm64__associate_instruction_ops:larl %r6 -> R_390_PC32DBL .data.rel.ro+0x2
loongarch__associate_ins_ops:larl %r9 -> R_390_PC32DBL .data.rel.ro+0x22 larl %r9 -> R_390_PC32DBL .data.rel.ro+0x42
check_ppc_insn:larl %r4 -> R_390_PC32DBL .data.rel.local+0x4c2 larl %r2 -> R_390_PC32DBL .data.rel.ro.local+0x22 larl %r1 -> R_390_PC32DBL .data.rel.local+0x2 larl %r2 -> R_390_PC32DBL .data.rel.ro.local+0x2
s390__associate_ins_ops:larl %r10 -> R_390_PC32DBL .data.rel.ro+0x732 larl %r10 -> R_390_PC32DBL .data.rel.ro+0x752So from my reproducer, I don't think we can describe this simply as "all Python-extension dependency archives are missing -fPIC".
The issue narrows down much more specifically to perf-util-in.o in the
ET_DYNlink of the Python extension, and the direct text-to-data section relocations above look like the most relevant candidates.
Quoted 5 lines
So I agree that -Wl,-z,notext is only a workaround. I'd prefer to understand and fix the underlying relocation pattern in perf-util-in.o rather than merge that as-is.
If useful, I can send the relevant readelf/objdump snippets in a follow-up.
Hi Jan,
I wonder about two tests:
1) adding V=1 JOBS=1 to your make command to switch on single threading and build verbosity. This may make it possible to identify the gcc command that is missing -fPIC. For example, I see -fPIC on x86:
gcc -Wp,-MD,/tmp/perf4/util/annotate-arch/.annotate-arm64.o.d
-Wp,-MT,/tmp/perf4/util/annotate-arch/annotate -arm64.o -Wbad-function-cast -Wdeclaration-after-statement -Wformat-security -Wformat-y2k -Winit-self -Wmissin g-declarations -Wmissing-prototypes -Wno-system-headers -Wold-style-definition -Wpacked -Wredundant-decls -Wst rict-prototypes -Wswitch-default -Wswitch-enum -Wundef -Wwrite-strings -Wformat -Wno-type-limits -Wstrict-alia sing=3 -Wshadow -fno-strict-aliasing -I/tmp/perf4/arch/x86/include/generated -DHAVE_ARCH_X86_64_SUPPORT -Werro r -g -fno-omit-frame-pointer -Wall -Wextra -std=gnu11 -funsigned-char -fstack-protector-all -D_LARGEFILE64_SOU RCE -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -I~/linux/tools/perf/util/include -I~/linux/tools/perf/arch/x86/include -I/usr/local/google/home/irogers/re pos/linux4/tools/include/ -I~/linux/tools/arch/x86/include/uapi -I/usr/loc al/google/home/irogers/repos/linux4/tools/include/uapi -I~/linux/tools/arc h/x86/include/ -I~/linux/tools/arch/x86/ -I/tmp/perf4//util -I/tmp/perf4/ -I~/linux/tools/perf/util -I~/linux/to ols/perf -DHAVE_PTHREAD_ATTR_SETAFFINITY_NP -DHAVE_PTHREAD_BARRIER -DHAVE_EVENTFD_SUPPORT -DHAVE_GETTID -DHAVE _FILE_HANDLE -DHAVE_AIO_SUPPORT -DHAVE_SCANDIRAT_SUPPORT -DHAVE_SCHED_GETCPU_SUPPORT -DHAVE_SETNS_SUPPORT -DHA VE_ZLIB_SUPPORT -DHAVE_LIBELF_SUPPORT -DHAVE_ELF_GETPHDRNUM_SUPPORT -DHAVE_GELF_GETNOTE_SUPPORT -DHAVE_ELF_GET SHDRSTRNDX_SUPPORT -DHAVE_DEBUGINFOD_SUPPORT -DHAVE_LIBDW_SUPPORT -DHAVE_LIBBPF_SUPPORT -DHAVE_SDT_EVENT -DHAV E_JITDUMP -DHAVE_LIBOPENSSL_SUPPORT -DHAVE_BPF_SKEL -DHAVE_DWARF_UNWIND_SUPPORT -DHAVE_SLANG_SUPPORT -DHAVE_TI MERFD_SUPPORT -fPIC -DHAVE_LIBLLVM_SUPPORT -I/usr/lib/llvm-19/include -D_GNU_SOURCE -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -DHAVE_CXA_DEMANGLE_SUPPORT -DHAVE_LZMA_SUPPORT -DHAVE_ZSTD_SUPPORT -DHAVE_BACKTRACE_SUPPORT -DHAVE_LIBNUMA_SUPPORT -DHAVE_PERF_READ_VDSO32 -DHAVE_LIBBABELTRACE_SUPPORT -DHAVE_ LIBCAPSTONE_SUPPORT -DHAVE_JVMTI_CMLR -DHAVE_LIBPFM -DHAVE_LIBTRACEEVENT -I/usr/include/traceevent -DLIBTRACEE VENT_VERSION=67072 -DHAVE_RUST_SUPPORT -I/tmp/perf4/libapi/include -I/tmp/perf4/libbpf/include -I/tmp/perf4/li bsubcmd/include -I/tmp/perf4/libsymbol/include -I/tmp/perf4/libperf/include -I/tmp/perf4/ -D"BUILD_STR(s)=#s" -c -o /tmp/perf4/util/annotate-arch/annotate-arm64.o util/annotate-arch/annotate-arm64.c
2) adding EXTRA_CFLAGS="-fPIC" to your make command. This should force this option wherever it might be missing.
Thanks,
Ian
Quoted 3 lines
Best regards,
Jan
Quoted 196 lines · Ian Rogers
Quoted 140 lines · Jan Polensky
Quoted 61 lines · Arnaldo Carvalho de Melo
Quoted 32 lines · On Wed, May 20, 2026 at 01:09:05PM +0000, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] The patch explicitly allows text relocations in the Python extension shared object (perf.so) by adding-Wl,-z,notext, masking the root cause of missing-fPICflags and breaking W^X (Write XOR Execute) security protections. --commit
f3832fb0ad0d1f9c2e69039b2a5110f77d220d5e
Author: Jan Polensky <[email protected]>perf build: Fix Python extension build with GCC 16 hardening
This commit addresses a build failure in the Python extension caused by GCC 16 stricter security policies regarding dynamic relocations in read-only segments. It adds the -Wl,-z,notext flag to the linker to explicitly allow text relocations in the Python extension shared object.
tools/perf/Makefile.perf all: shell_compatibility_test $(ALL_PROGRAMS) $(LANG_BINDINGS) $(OTHER_PROGRAMS) · line 752
Quoted 10 lines
tools/perf/Makefile.perf +1
-1@@ -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" \[Severity: Medium]
Does adding -Wl,-z,notext mask the root cause of the GCC 16 linker error?The error regarding dynamic relocations in a read-only segment usually indicates that non-position-independent code from the static libraries is being linked into the shared library.
Quoted 7 lines
Using -Wl,-z,notext suppresses this error by allowing text relocations, which forces the dynamic linker to map the executable text segment as writable at load time. This breaks W^X security protections and weakens the security of any Python process loading the extension.
Should the required object files be compiled with -fPIC instead to properly resolve the linker error without compromising security?
So I asked Claude about this and he suggests doing like other parts of perf and using -fPIC, can you try to do that?
---------------------------------------
● Sashiko is right. -Wl,-z,notext is a band-aid that weakens security —
it makes the text segment writable at load time, defeating W^X.The root cause is that
PERFLIBS_PY(the static libraries linked into the Python .so) are compiled without -fPIC. When you link non-PIC .a archives into a shared library, the linker needs text relocations. GCC 16 correctly rejects this by default.The proper fix is to build the static libraries that go into the Python extension with -fPIC. The perf build system already has precedent for this — look for how libperf and libsubcmd handle PIC builds. The Python extension's dependencies need a parallel -fPIC build, or the existing objects need to be compiled with -fPIC when the Python binding is enabled.
Quoted 5 lines
The -Wl,-z,notext approach would also cause issues on distros that enforce RELRO +
BIND_NOWhardening, and some package builders would reject the resulting .so outright.- Arnaldo
Hi Arnaldo,
thanks for the pointer. I double-checked this on s390x and did some reduced-link testing for the
ET_DYN/ Python-extension case.The failure is not triggered by the perf Python binding object alone.
A reduced reproducer succeeds when linking:
python.o
+ libapi.a
+ libperf.a
+ libsubcmd.a
+ libsymbol.a
+ libbpf.a
+ libperf-ui.aThe link starts to fail with:
/usr/bin/ld.bfd: error: read-only segment has dynamic relocationsas soon as libperf-util.a is added.
In my tree, libperf-util.a currently contains only perf-util-in.o, and replacing the archive with that single object still reproduces the same failure.
Reduced reproducer:
gcc -pthread -shared \ -Wl,-z,noexecstack \ -o /tmp/try-perf-util-only.so \ /root/linux/tools/perf/python_ext_build/tmp/root/linux/tools/perf/util/python.o \ -Wl,--whole-archive \ /root/linux/tools/perf/libapi/libapi.a \ /root/linux/tools/perf/libperf/libperf.a \ /root/linux/tools/perf/libsubcmd/libsubcmd.a \ /root/linux/tools/perf/libsymbol/libsymbol.a \ /root/linux/tools/perf/libbpf/libbpf.a \ /root/linux/tools/perf/libperf-ui.a \ -Wl,--no-whole-archive \ /root/perf-util-one/perf-util-in.o \ -lpython3.14 -lpthread -ldl -lm -lutilInspection of perf-util-in.o on s390x shows many direct
R_390_PC32DBLrelocations from .text against .data.rel.ro, .data.rel.ro.local, .data.rel.local, .bss, and .rodata.Representative examples include:
arm64__associate_instruction_ops:larl %r6 -> R_390_PC32DBL .data.rel.ro+0x2
loongarch__associate_ins_ops:larl %r9 -> R_390_PC32DBL .data.rel.ro+0x22 larl %r9 -> R_390_PC32DBL .data.rel.ro+0x42
check_ppc_insn:larl %r4 -> R_390_PC32DBL .data.rel.local+0x4c2 larl %r2 -> R_390_PC32DBL .data.rel.ro.local+0x22 larl %r1 -> R_390_PC32DBL .data.rel.local+0x2 larl %r2 -> R_390_PC32DBL .data.rel.ro.local+0x2
s390__associate_ins_ops:larl %r10 -> R_390_PC32DBL .data.rel.ro+0x732 larl %r10 -> R_390_PC32DBL .data.rel.ro+0x752So from my reproducer, I don't think we can describe this simply as "all Python-extension dependency archives are missing -fPIC".
The issue narrows down much more specifically to perf-util-in.o in the
ET_DYNlink of the Python extension, and the direct text-to-data section relocations above look like the most relevant candidates.Quoted 5 lines
So I agree that -Wl,-z,notext is only a workaround. I'd prefer to understand and fix the underlying relocation pattern in perf-util-in.o rather than merge that as-is.
If useful, I can send the relevant readelf/objdump snippets in a follow-up.
Hi Jan,
I wonder about two tests:
1) adding V=1 JOBS=1 to your make command to switch on single threading and build verbosity. This may make it possible to identify the gcc command that is missing -fPIC. For example, I see -fPIC on x86:
gcc -Wp,-MD,/tmp/perf4/util/annotate-arch/.annotate-arm64.o.d(quoted diff) +0
-14-Wp,-MT,/tmp/perf4/util/annotate-arch/annotate -arm64.o -Wbad-function-cast -Wdeclaration-after-statement -Wformat-security -Wformat-y2k -Winit-self -Wmissin g-declarations -Wmissing-prototypes -Wno-system-headers -Wold-style-definition -Wpacked -Wredundant-decls -Wst rict-prototypes -Wswitch-default -Wswitch-enum -Wundef -Wwrite-strings -Wformat -Wno-type-limits -Wstrict-alia sing=3 -Wshadow -fno-strict-aliasing -I/tmp/perf4/arch/x86/include/generated -DHAVE_ARCH_X86_64_SUPPORT -Werro r -g -fno-omit-frame-pointer -Wall -Wextra -std=gnu11 -funsigned-char -fstack-protector-all -D_LARGEFILE64_SOU RCE -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -I~/linux/tools/perf/util/include -I~/linux/tools/perf/arch/x86/include -I/usr/local/google/home/irogers/re pos/linux4/tools/include/ -I~/linux/tools/arch/x86/include/uapi -I/usr/local/google/home/irogers/repos/linux4/tools/include/uapi
(quoted diff) +0
-4-I~/linux/tools/arc h/x86/include/ -I~/linux/tools/arch/x86/ -I/tmp/perf4//util -I/tmp/perf4/ -I~/linux/tools/perf/util -I~/linux/to ols/perf -DHAVE_PTHREAD_ATTR_SETAFFINITY_NP -DHAVE_PTHREAD_BARRIER -DHAVE_EVENTFD_SUPPORT -DHAVE_GETTID -DHAVE
_FILE_HANDLE-DHAVE_AIO_SUPPORT -DHAVE_SCANDIRAT_SUPPORT(quoted diff) +0
-12-DHAVE_SCHED_GETCPU_SUPPORT -DHAVE_SETNS_SUPPORT -DHA VE_ZLIB_SUPPORT -DHAVE_LIBELF_SUPPORT -DHAVE_ELF_GETPHDRNUM_SUPPORT -DHAVE_GELF_GETNOTE_SUPPORT -DHAVE_ELF_GET SHDRSTRNDX_SUPPORT -DHAVE_DEBUGINFOD_SUPPORT -DHAVE_LIBDW_SUPPORT -DHAVE_LIBBPF_SUPPORT -DHAVE_SDT_EVENT -DHAV E_JITDUMP -DHAVE_LIBOPENSSL_SUPPORT -DHAVE_BPF_SKEL -DHAVE_DWARF_UNWIND_SUPPORT -DHAVE_SLANG_SUPPORT -DHAVE_TI MERFD_SUPPORT -fPIC -DHAVE_LIBLLVM_SUPPORT -I/usr/lib/llvm-19/include -D_GNU_SOURCE -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -DHAVE_CXA_DEMANGLE_SUPPORT -DHAVE_LZMA_SUPPORT -DHAVE_ZSTD_SUPPORT -DHAVE_BACKTRACE_SUPPORT -DHAVE_LIBNUMA_SUPPORT -DHAVE_PERF_READ_VDSO32 -DHAVE_LIBBABELTRACE_SUPPORT -DHAVE_
LIBCAPSTONE_SUPPORT-DHAVE_JVMTI_CMLR -DHAVE_LIBPFM -DHAVE_LIBTRACEEVENT -I/usr/include/traceevent -DLIBTRACEE VENT_VERSION=67072 -DHAVE_RUST_SUPPORT -I/tmp/perf4/libapi/include(quoted diff) +0
-3-I/tmp/perf4/libbpf/include -I/tmp/perf4/li bsubcmd/include -I/tmp/perf4/libsymbol/include -I/tmp/perf4/libperf/include -I/tmp/perf4/ -D"BUILD_STR(s)=#s" -c -o
/tmp/perf4/util/annotate-arch/annotate-arm64.outil/annotate-arch/annotate-arm64.c
Quoted 9 lines
2) adding EXTRA_CFLAGS="-fPIC" to your make command. This should force this option wherever it might be missing.
Thanks,
IanQuoted 3 lines
Best regards,
Jan
Hi Ian,
I tested both suggestions the second in the beginning of my research:
EXTRA_CFLAGS="-fPIC" - Does NOT work. Same error:
/usr/bin/ld.bfd: error: read-only segment has dynamic relocations
collect2: error: ld returned 1 exit status
The issue is s390x-specific R_390_PC32DBL relocations in perf-util-in.o (from larl instructions) that persist even with -fPIC.
Current options:
1. Use -Wl,-z,notext (security issue)
2. Refactor code to avoid these relocations
3. Disable Python extension on s390x (not an option)
Thoughts?
Best regards,
Jan
Ian Rogers
reviewerQuoted 219 lines · Jan Polensky
Quoted 196 lines · Ian Rogers
Quoted 140 lines · Jan Polensky
Quoted 61 lines · Arnaldo Carvalho de Melo
Quoted 32 lines · On Wed, May 20, 2026 at 01:09:05PM +0000, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] The patch explicitly allows text relocations in the Python extension shared object (perf.so) by adding-Wl,-z,notext, masking the root cause of missing-fPICflags and breaking W^X (Write XOR Execute) security protections. --commit
f3832fb0ad0d1f9c2e69039b2a5110f77d220d5e
Author: Jan Polensky <[email protected]>perf build: Fix Python extension build with GCC 16 hardening
This commit addresses a build failure in the Python extension caused by GCC 16 stricter security policies regarding dynamic relocations in read-only segments. It adds the -Wl,-z,notext flag to the linker to explicitly allow text relocations in the Python extension shared object.
tools/perf/Makefile.perf all: shell_compatibility_test $(ALL_PROGRAMS) $(LANG_BINDINGS) $(OTHER_PROGRAMS) · line 752
Quoted 10 lines
tools/perf/Makefile.perf +1
-1@@ -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" \[Severity: Medium]
Does adding -Wl,-z,notext mask the root cause of the GCC 16 linker error?The error regarding dynamic relocations in a read-only segment usually indicates that non-position-independent code from the static libraries is being linked into the shared library.
Quoted 7 lines
Using -Wl,-z,notext suppresses this error by allowing text relocations, which forces the dynamic linker to map the executable text segment as writable at load time. This breaks W^X security protections and weakens the security of any Python process loading the extension.
Should the required object files be compiled with -fPIC instead to properly resolve the linker error without compromising security?
So I asked Claude about this and he suggests doing like other parts of perf and using -fPIC, can you try to do that?
---------------------------------------
● Sashiko is right. -Wl,-z,notext is a band-aid that weakens security —
it makes the text segment writable at load time, defeating W^X.The root cause is that
PERFLIBS_PY(the static libraries linked into the Python .so) are compiled without -fPIC. When you link non-PIC .a archives into a shared library, the linker needs text relocations. GCC 16 correctly rejects this by default.The proper fix is to build the static libraries that go into the Python extension with -fPIC. The perf build system already has precedent for this — look for how libperf and libsubcmd handle PIC builds. The Python extension's dependencies need a parallel -fPIC build, or the existing objects need to be compiled with -fPIC when the Python binding is enabled.
Quoted 5 lines
The -Wl,-z,notext approach would also cause issues on distros that enforce RELRO +
BIND_NOWhardening, and some package builders would reject the resulting .so outright.- Arnaldo
Hi Arnaldo,
thanks for the pointer. I double-checked this on s390x and did some reduced-link testing for the
ET_DYN/ Python-extension case.The failure is not triggered by the perf Python binding object alone.
A reduced reproducer succeeds when linking:
python.o
+ libapi.a
+ libperf.a
+ libsubcmd.a
+ libsymbol.a
+ libbpf.a
+ libperf-ui.aThe link starts to fail with:
/usr/bin/ld.bfd: error: read-only segment has dynamic relocationsas soon as libperf-util.a is added.
In my tree, libperf-util.a currently contains only perf-util-in.o, and replacing the archive with that single object still reproduces the same failure.
Reduced reproducer:
gcc -pthread -shared \ -Wl,-z,noexecstack \ -o /tmp/try-perf-util-only.so \ /root/linux/tools/perf/python_ext_build/tmp/root/linux/tools/perf/util/python.o \ -Wl,--whole-archive \ /root/linux/tools/perf/libapi/libapi.a \ /root/linux/tools/perf/libperf/libperf.a \ /root/linux/tools/perf/libsubcmd/libsubcmd.a \ /root/linux/tools/perf/libsymbol/libsymbol.a \ /root/linux/tools/perf/libbpf/libbpf.a \ /root/linux/tools/perf/libperf-ui.a \ -Wl,--no-whole-archive \ /root/perf-util-one/perf-util-in.o \ -lpython3.14 -lpthread -ldl -lm -lutilInspection of perf-util-in.o on s390x shows many direct
R_390_PC32DBLrelocations from .text against .data.rel.ro, .data.rel.ro.local, .data.rel.local, .bss, and .rodata.Representative examples include:
arm64__associate_instruction_ops:larl %r6 -> R_390_PC32DBL .data.rel.ro+0x2
loongarch__associate_ins_ops:larl %r9 -> R_390_PC32DBL .data.rel.ro+0x22 larl %r9 -> R_390_PC32DBL .data.rel.ro+0x42
check_ppc_insn:larl %r4 -> R_390_PC32DBL .data.rel.local+0x4c2 larl %r2 -> R_390_PC32DBL .data.rel.ro.local+0x22 larl %r1 -> R_390_PC32DBL .data.rel.local+0x2 larl %r2 -> R_390_PC32DBL .data.rel.ro.local+0x2
s390__associate_ins_ops:larl %r10 -> R_390_PC32DBL .data.rel.ro+0x732 larl %r10 -> R_390_PC32DBL .data.rel.ro+0x752So from my reproducer, I don't think we can describe this simply as "all Python-extension dependency archives are missing -fPIC".
The issue narrows down much more specifically to perf-util-in.o in the
ET_DYNlink of the Python extension, and the direct text-to-data section relocations above look like the most relevant candidates.Quoted 5 lines
So I agree that -Wl,-z,notext is only a workaround. I'd prefer to understand and fix the underlying relocation pattern in perf-util-in.o rather than merge that as-is.
If useful, I can send the relevant readelf/objdump snippets in a follow-up.
Hi Jan,
I wonder about two tests:
1) adding V=1 JOBS=1 to your make command to switch on single threading and build verbosity. This may make it possible to identify the gcc command that is missing -fPIC. For example, I see -fPIC on x86:
gcc -Wp,-MD,/tmp/perf4/util/annotate-arch/.annotate-arm64.o.d(quoted diff) +0
-14-Wp,-MT,/tmp/perf4/util/annotate-arch/annotate -arm64.o -Wbad-function-cast -Wdeclaration-after-statement -Wformat-security -Wformat-y2k -Winit-self -Wmissin g-declarations -Wmissing-prototypes -Wno-system-headers -Wold-style-definition -Wpacked -Wredundant-decls -Wst rict-prototypes -Wswitch-default -Wswitch-enum -Wundef -Wwrite-strings -Wformat -Wno-type-limits -Wstrict-alia sing=3 -Wshadow -fno-strict-aliasing -I/tmp/perf4/arch/x86/include/generated -DHAVE_ARCH_X86_64_SUPPORT -Werro r -g -fno-omit-frame-pointer -Wall -Wextra -std=gnu11 -funsigned-char -fstack-protector-all -D_LARGEFILE64_SOU RCE -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -I~/linux/tools/perf/util/include -I~/linux/tools/perf/arch/x86/include -I/usr/local/google/home/irogers/re pos/linux4/tools/include/ -I~/linux/tools/arch/x86/include/uapi -I/usr/local/google/home/irogers/repos/linux4/tools/include/uapi
(quoted diff) +0
-4-I~/linux/tools/arc h/x86/include/ -I~/linux/tools/arch/x86/ -I/tmp/perf4//util -I/tmp/perf4/ -I~/linux/tools/perf/util -I~/linux/to ols/perf -DHAVE_PTHREAD_ATTR_SETAFFINITY_NP -DHAVE_PTHREAD_BARRIER -DHAVE_EVENTFD_SUPPORT -DHAVE_GETTID -DHAVE
_FILE_HANDLE-DHAVE_AIO_SUPPORT -DHAVE_SCANDIRAT_SUPPORT(quoted diff) +0
-12-DHAVE_SCHED_GETCPU_SUPPORT -DHAVE_SETNS_SUPPORT -DHA VE_ZLIB_SUPPORT -DHAVE_LIBELF_SUPPORT -DHAVE_ELF_GETPHDRNUM_SUPPORT -DHAVE_GELF_GETNOTE_SUPPORT -DHAVE_ELF_GET SHDRSTRNDX_SUPPORT -DHAVE_DEBUGINFOD_SUPPORT -DHAVE_LIBDW_SUPPORT -DHAVE_LIBBPF_SUPPORT -DHAVE_SDT_EVENT -DHAV E_JITDUMP -DHAVE_LIBOPENSSL_SUPPORT -DHAVE_BPF_SKEL -DHAVE_DWARF_UNWIND_SUPPORT -DHAVE_SLANG_SUPPORT -DHAVE_TI MERFD_SUPPORT -fPIC -DHAVE_LIBLLVM_SUPPORT -I/usr/lib/llvm-19/include -D_GNU_SOURCE -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -DHAVE_CXA_DEMANGLE_SUPPORT -DHAVE_LZMA_SUPPORT -DHAVE_ZSTD_SUPPORT -DHAVE_BACKTRACE_SUPPORT -DHAVE_LIBNUMA_SUPPORT -DHAVE_PERF_READ_VDSO32 -DHAVE_LIBBABELTRACE_SUPPORT -DHAVE_
LIBCAPSTONE_SUPPORT-DHAVE_JVMTI_CMLR -DHAVE_LIBPFM -DHAVE_LIBTRACEEVENT -I/usr/include/traceevent -DLIBTRACEE VENT_VERSION=67072 -DHAVE_RUST_SUPPORT -I/tmp/perf4/libapi/include(quoted diff) +0
-3-I/tmp/perf4/libbpf/include -I/tmp/perf4/li bsubcmd/include -I/tmp/perf4/libsymbol/include -I/tmp/perf4/libperf/include -I/tmp/perf4/ -D"BUILD_STR(s)=#s" -c -o
/tmp/perf4/util/annotate-arch/annotate-arm64.outil/annotate-arch/annotate-arm64.cQuoted 9 lines
2) adding EXTRA_CFLAGS="-fPIC" to your make command. This should force this option wherever it might be missing.
Thanks,
IanQuoted 3 lines
Best regards,
JanHi Ian,
I tested both suggestions the second in the beginning of my research:
EXTRA_CFLAGS="-fPIC" - Does NOT work. Same error:
/usr/bin/ld.bfd: error: read-only segment has dynamic relocations
collect2: error: ld returned 1 exit statusThe issue is s390x-specific
R_390_PC32DBLrelocations in perf-util-in.o (fromlarlinstructions) that persist even with -fPIC.
Quoted 6 lines
Current options:
1. Use -Wl,-z,notext (security issue)
2. Refactor code to avoid these relocations
3. Disable Python extension on s390x (not an option)Thoughts?
So it appears that part of the problem is declaring arrays and structs to be const. Making things non-const I think is also not an option. Would EXTRA_CFLAGS="-fvisibility=hidden" help?
It would we interesting to build with -Wl,-z,notext and then look for the symbol the linker is fighting with:
eu-findtextrel perf.cpython....so
or
readelf -d perf.so | grep TEXTREL
readelf -r perf.so
Thanks,
Ian
Quoted 2 lines
Best regards,
Jan
Quoted 8 lines · Ian Rogers
Quoted 7 lines · Jan Polensky
Quoted 5 lines · Ian Rogers
Quoted 4 lines · Jan Polensky
Quoted 2 lines · Arnaldo Carvalho de Melo
Quoted 1 line · On Wed, May 20, 2026 at 01:09:05PM +0000, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
[snip]
Quoted 12 lines
So it appears that part of the problem is declaring arrays and structs to be const. Making things non-const I think is also not an option. Would EXTRA_CFLAGS="-fvisibility=hidden" help?
It would we interesting to build with -Wl,-z,notext and then look for the symbol the linker is fighting with:
eu-findtextrel perf.cpython....so
or
readelf -d perf.so | grep TEXTREL
readelf -r perf.so
Quoted 3 lines
Thanks,
Ian
Hi,
building the Python perf extension on s390x shows GCC version-dependent behavior.
Build command:
make NO_LIBTRACEEVENT=1 python-perf
Verified build results:
GCC 15.2.0 (Ubuntu 15.2.0-4ubuntu4)
GNU ld (GNU Binutils for Ubuntu) 2.45
Build succeeds and creates python/perf.cpython-313-s390x-linux-gnu.so Verification:
readelf -d python/perf.cpython-313-s390x-linux-gnu.so | grep TEXTREL Output:0x0000000000000016 (TEXTREL) 0x0
GCC 16.1.0 (cross-compiler)
GNU ld (GNU Binutils) 2.46
Build succeeds and creates python/perf.cpython-313-s390x-linux-gnu.so Verification:
readelf -d python/perf.cpython-313-s390x-linux-gnu.so | grep TEXTREL No output
GCC 16.1.1 20260515 (Red Hat 16.1.1-2)
GNU ld version 2.46-3.fc44
Build fails with:
/usr/bin/ld.bfd: error: read-only segment has dynamic relocations
Analysis:
Base libraries libapi, libperf, libsubcmd, and libsymbol compiled with -g/-ggdb3 create DWARF debug sections with absolute relocations (R_390_64). When statically linked into the Python shared library, these become TEXTREL entries.
Observed behavior:
GCC 15.2.0 generates TEXTREL.
GCC 16.1.0 does not generate TEXTREL.
GCC 16.1.1 generates TEXTREL.
Binutils 2.45 accepts shared libraries with TEXTREL. Binutils 2.46 rejects them.
Question to clarify:
What changed between GCC 16.1.0 and 16.1.1 on s390x that reintroduced TEXTREL generation here?
Best regards,
Jan
Namhyung Kim
maintainerQuoted 231 lines · Ian Rogers
Quoted 219 lines · Jan Polensky
Quoted 196 lines · Ian Rogers
Quoted 140 lines · Jan Polensky
Quoted 61 lines · Arnaldo Carvalho de Melo
Quoted 32 lines · On Wed, May 20, 2026 at 01:09:05PM +0000, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] The patch explicitly allows text relocations in the Python extension shared object (perf.so) by adding-Wl,-z,notext, masking the root cause of missing-fPICflags and breaking W^X (Write XOR Execute) security protections. --commit
f3832fb0ad0d1f9c2e69039b2a5110f77d220d5e
Author: Jan Polensky <[email protected]>perf build: Fix Python extension build with GCC 16 hardening
This commit addresses a build failure in the Python extension caused by GCC 16 stricter security policies regarding dynamic relocations in read-only segments. It adds the -Wl,-z,notext flag to the linker to explicitly allow text relocations in the Python extension shared object.
tools/perf/Makefile.perf all: shell_compatibility_test $(ALL_PROGRAMS) $(LANG_BINDINGS) $(OTHER_PROGRAMS) · line 752
Quoted 10 lines
tools/perf/Makefile.perf +1
-1@@ -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" \[Severity: Medium]
Does adding -Wl,-z,notext mask the root cause of the GCC 16 linker error?The error regarding dynamic relocations in a read-only segment usually indicates that non-position-independent code from the static libraries is being linked into the shared library.
Quoted 7 lines
Using -Wl,-z,notext suppresses this error by allowing text relocations, which forces the dynamic linker to map the executable text segment as writable at load time. This breaks W^X security protections and weakens the security of any Python process loading the extension.
Should the required object files be compiled with -fPIC instead to properly resolve the linker error without compromising security?
So I asked Claude about this and he suggests doing like other parts of perf and using -fPIC, can you try to do that?
---------------------------------------
● Sashiko is right. -Wl,-z,notext is a band-aid that weakens security —
it makes the text segment writable at load time, defeating W^X.The root cause is that
PERFLIBS_PY(the static libraries linked into the Python .so) are compiled without -fPIC. When you link non-PIC .a archives into a shared library, the linker needs text relocations. GCC 16 correctly rejects this by default.The proper fix is to build the static libraries that go into the Python extension with -fPIC. The perf build system already has precedent for this — look for how libperf and libsubcmd handle PIC builds. The Python extension's dependencies need a parallel -fPIC build, or the existing objects need to be compiled with -fPIC when the Python binding is enabled.
Quoted 5 lines
The -Wl,-z,notext approach would also cause issues on distros that enforce RELRO +
BIND_NOWhardening, and some package builders would reject the resulting .so outright.- Arnaldo
Hi Arnaldo,
thanks for the pointer. I double-checked this on s390x and did some reduced-link testing for the
ET_DYN/ Python-extension case.The failure is not triggered by the perf Python binding object alone.
A reduced reproducer succeeds when linking:
python.o
+ libapi.a
+ libperf.a
+ libsubcmd.a
+ libsymbol.a
+ libbpf.a
+ libperf-ui.aThe link starts to fail with:
/usr/bin/ld.bfd: error: read-only segment has dynamic relocationsas soon as libperf-util.a is added.
In my tree, libperf-util.a currently contains only perf-util-in.o, and replacing the archive with that single object still reproduces the same failure.
Reduced reproducer:
gcc -pthread -shared \ -Wl,-z,noexecstack \ -o /tmp/try-perf-util-only.so \ /root/linux/tools/perf/python_ext_build/tmp/root/linux/tools/perf/util/python.o \ -Wl,--whole-archive \ /root/linux/tools/perf/libapi/libapi.a \ /root/linux/tools/perf/libperf/libperf.a \ /root/linux/tools/perf/libsubcmd/libsubcmd.a \ /root/linux/tools/perf/libsymbol/libsymbol.a \ /root/linux/tools/perf/libbpf/libbpf.a \ /root/linux/tools/perf/libperf-ui.a \ -Wl,--no-whole-archive \ /root/perf-util-one/perf-util-in.o \ -lpython3.14 -lpthread -ldl -lm -lutilInspection of perf-util-in.o on s390x shows many direct
R_390_PC32DBLrelocations from .text against .data.rel.ro, .data.rel.ro.local, .data.rel.local, .bss, and .rodata.Representative examples include:
arm64__associate_instruction_ops:larl %r6 -> R_390_PC32DBL .data.rel.ro+0x2
loongarch__associate_ins_ops:larl %r9 -> R_390_PC32DBL .data.rel.ro+0x22 larl %r9 -> R_390_PC32DBL .data.rel.ro+0x42
check_ppc_insn:larl %r4 -> R_390_PC32DBL .data.rel.local+0x4c2 larl %r2 -> R_390_PC32DBL .data.rel.ro.local+0x22 larl %r1 -> R_390_PC32DBL .data.rel.local+0x2 larl %r2 -> R_390_PC32DBL .data.rel.ro.local+0x2
s390__associate_ins_ops:larl %r10 -> R_390_PC32DBL .data.rel.ro+0x732 larl %r10 -> R_390_PC32DBL .data.rel.ro+0x752So from my reproducer, I don't think we can describe this simply as "all Python-extension dependency archives are missing -fPIC".
The issue narrows down much more specifically to perf-util-in.o in the
ET_DYNlink of the Python extension, and the direct text-to-data section relocations above look like the most relevant candidates.Quoted 5 lines
So I agree that -Wl,-z,notext is only a workaround. I'd prefer to understand and fix the underlying relocation pattern in perf-util-in.o rather than merge that as-is.
If useful, I can send the relevant readelf/objdump snippets in a follow-up.
Hi Jan,
I wonder about two tests:
1) adding V=1 JOBS=1 to your make command to switch on single threading and build verbosity. This may make it possible to identify the gcc command that is missing -fPIC. For example, I see -fPIC on x86:
gcc -Wp,-MD,/tmp/perf4/util/annotate-arch/.annotate-arm64.o.d(quoted diff) +0
-14-Wp,-MT,/tmp/perf4/util/annotate-arch/annotate -arm64.o -Wbad-function-cast -Wdeclaration-after-statement -Wformat-security -Wformat-y2k -Winit-self -Wmissin g-declarations -Wmissing-prototypes -Wno-system-headers -Wold-style-definition -Wpacked -Wredundant-decls -Wst rict-prototypes -Wswitch-default -Wswitch-enum -Wundef -Wwrite-strings -Wformat -Wno-type-limits -Wstrict-alia sing=3 -Wshadow -fno-strict-aliasing -I/tmp/perf4/arch/x86/include/generated -DHAVE_ARCH_X86_64_SUPPORT -Werro r -g -fno-omit-frame-pointer -Wall -Wextra -std=gnu11 -funsigned-char -fstack-protector-all -D_LARGEFILE64_SOU RCE -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -I~/linux/tools/perf/util/include -I~/linux/tools/perf/arch/x86/include -I/usr/local/google/home/irogers/re pos/linux4/tools/include/ -I~/linux/tools/arch/x86/include/uapi -I/usr/local/google/home/irogers/repos/linux4/tools/include/uapi
(quoted diff) +0
-4-I~/linux/tools/arc h/x86/include/ -I~/linux/tools/arch/x86/ -I/tmp/perf4//util -I/tmp/perf4/ -I~/linux/tools/perf/util -I~/linux/to ols/perf -DHAVE_PTHREAD_ATTR_SETAFFINITY_NP -DHAVE_PTHREAD_BARRIER -DHAVE_EVENTFD_SUPPORT -DHAVE_GETTID -DHAVE
_FILE_HANDLE-DHAVE_AIO_SUPPORT -DHAVE_SCANDIRAT_SUPPORT(quoted diff) +0
-12-DHAVE_SCHED_GETCPU_SUPPORT -DHAVE_SETNS_SUPPORT -DHA VE_ZLIB_SUPPORT -DHAVE_LIBELF_SUPPORT -DHAVE_ELF_GETPHDRNUM_SUPPORT -DHAVE_GELF_GETNOTE_SUPPORT -DHAVE_ELF_GET SHDRSTRNDX_SUPPORT -DHAVE_DEBUGINFOD_SUPPORT -DHAVE_LIBDW_SUPPORT -DHAVE_LIBBPF_SUPPORT -DHAVE_SDT_EVENT -DHAV E_JITDUMP -DHAVE_LIBOPENSSL_SUPPORT -DHAVE_BPF_SKEL -DHAVE_DWARF_UNWIND_SUPPORT -DHAVE_SLANG_SUPPORT -DHAVE_TI MERFD_SUPPORT -fPIC -DHAVE_LIBLLVM_SUPPORT -I/usr/lib/llvm-19/include -D_GNU_SOURCE -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -DHAVE_CXA_DEMANGLE_SUPPORT -DHAVE_LZMA_SUPPORT -DHAVE_ZSTD_SUPPORT -DHAVE_BACKTRACE_SUPPORT -DHAVE_LIBNUMA_SUPPORT -DHAVE_PERF_READ_VDSO32 -DHAVE_LIBBABELTRACE_SUPPORT -DHAVE_
LIBCAPSTONE_SUPPORT-DHAVE_JVMTI_CMLR -DHAVE_LIBPFM -DHAVE_LIBTRACEEVENT -I/usr/include/traceevent -DLIBTRACEE VENT_VERSION=67072 -DHAVE_RUST_SUPPORT -I/tmp/perf4/libapi/include(quoted diff) +0
-3-I/tmp/perf4/libbpf/include -I/tmp/perf4/li bsubcmd/include -I/tmp/perf4/libsymbol/include -I/tmp/perf4/libperf/include -I/tmp/perf4/ -D"BUILD_STR(s)=#s" -c -o
/tmp/perf4/util/annotate-arch/annotate-arm64.outil/annotate-arch/annotate-arm64.cQuoted 9 lines
2) adding EXTRA_CFLAGS="-fPIC" to your make command. This should force this option wherever it might be missing.
Thanks,
IanQuoted 3 lines
Best regards,
JanHi Ian,
I tested both suggestions the second in the beginning of my research:
EXTRA_CFLAGS="-fPIC" - Does NOT work. Same error:
/usr/bin/ld.bfd: error: read-only segment has dynamic relocations
collect2: error: ld returned 1 exit statusThe issue is s390x-specific
R_390_PC32DBLrelocations in perf-util-in.o (fromlarlinstructions) that persist even with -fPIC.Quoted 6 lines
Current options:
1. Use -Wl,-z,notext (security issue)
2. Refactor code to avoid these relocations
3. Disable Python extension on s390x (not an option)Thoughts?
So it appears that part of the problem is declaring arrays and structs to be const. Making things non-const I think is also not an option. Would EXTRA_CFLAGS="-fvisibility=hidden" help?
Quoted 6 lines
It would we interesting to build with -Wl,-z,notext and then look for the symbol the linker is fighting with:
eu-findtextrel perf.cpython....so
or
readelf -d perf.so | grep TEXTREL
readelf -r perf.so
I'm curious if it's s390 specific. I don't know what code patterns make it relocatable. Is taking an address of struct ins_ops variable a problem? Does it matter being a static variable?
Thanks,
Namhyung
Ian Rogers
reviewerQuoted 240 lines · Namhyung Kim
Quoted 231 lines · Ian Rogers
Quoted 219 lines · Jan Polensky
Quoted 196 lines · Ian Rogers
Quoted 140 lines · Jan Polensky
Quoted 61 lines · Arnaldo Carvalho de Melo
Quoted 32 lines · On Wed, May 20, 2026 at 01:09:05PM +0000, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] The patch explicitly allows text relocations in the Python extension shared object (perf.so) by adding-Wl,-z,notext, masking the root cause of missing-fPICflags and breaking W^X (Write XOR Execute) security protections. --commit
f3832fb0ad0d1f9c2e69039b2a5110f77d220d5e
Author: Jan Polensky <[email protected]>perf build: Fix Python extension build with GCC 16 hardening
This commit addresses a build failure in the Python extension caused by GCC 16 stricter security policies regarding dynamic relocations in read-only segments. It adds the -Wl,-z,notext flag to the linker to explicitly allow text relocations in the Python extension shared object.
tools/perf/Makefile.perf all: shell_compatibility_test $(ALL_PROGRAMS) $(LANG_BINDINGS) $(OTHER_PROGRAMS) · line 752
Quoted 10 lines
tools/perf/Makefile.perf +1
-1@@ -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" \[Severity: Medium]
Does adding -Wl,-z,notext mask the root cause of the GCC 16 linker error?The error regarding dynamic relocations in a read-only segment usually indicates that non-position-independent code from the static libraries is being linked into the shared library.
Quoted 7 lines
Using -Wl,-z,notext suppresses this error by allowing text relocations, which forces the dynamic linker to map the executable text segment as writable at load time. This breaks W^X security protections and weakens the security of any Python process loading the extension.
Should the required object files be compiled with -fPIC instead to properly resolve the linker error without compromising security?
So I asked Claude about this and he suggests doing like other parts of perf and using -fPIC, can you try to do that?
---------------------------------------
● Sashiko is right. -Wl,-z,notext is a band-aid that weakens security —
it makes the text segment writable at load time, defeating W^X.The root cause is that
PERFLIBS_PY(the static libraries linked into the Python .so) are compiled without -fPIC. When you link non-PIC .a archives into a shared library, the linker needs text relocations. GCC 16 correctly rejects this by default.The proper fix is to build the static libraries that go into the Python extension with -fPIC. The perf build system already has precedent for this — look for how libperf and libsubcmd handle PIC builds. The Python extension's dependencies need a parallel -fPIC build, or the existing objects need to be compiled with -fPIC when the Python binding is enabled.
Quoted 5 lines
The -Wl,-z,notext approach would also cause issues on distros that enforce RELRO +
BIND_NOWhardening, and some package builders would reject the resulting .so outright.- Arnaldo
Hi Arnaldo,
thanks for the pointer. I double-checked this on s390x and did some reduced-link testing for the
ET_DYN/ Python-extension case.The failure is not triggered by the perf Python binding object alone.
A reduced reproducer succeeds when linking:
python.o
+ libapi.a
+ libperf.a
+ libsubcmd.a
+ libsymbol.a
+ libbpf.a
+ libperf-ui.aThe link starts to fail with:
/usr/bin/ld.bfd: error: read-only segment has dynamic relocationsas soon as libperf-util.a is added.
In my tree, libperf-util.a currently contains only perf-util-in.o, and replacing the archive with that single object still reproduces the same failure.
Reduced reproducer:
gcc -pthread -shared \ -Wl,-z,noexecstack \ -o /tmp/try-perf-util-only.so \ /root/linux/tools/perf/python_ext_build/tmp/root/linux/tools/perf/util/python.o \ -Wl,--whole-archive \ /root/linux/tools/perf/libapi/libapi.a \ /root/linux/tools/perf/libperf/libperf.a \ /root/linux/tools/perf/libsubcmd/libsubcmd.a \ /root/linux/tools/perf/libsymbol/libsymbol.a \ /root/linux/tools/perf/libbpf/libbpf.a \ /root/linux/tools/perf/libperf-ui.a \ -Wl,--no-whole-archive \ /root/perf-util-one/perf-util-in.o \ -lpython3.14 -lpthread -ldl -lm -lutilInspection of perf-util-in.o on s390x shows many direct
R_390_PC32DBLrelocations from .text against .data.rel.ro, .data.rel.ro.local, .data.rel.local, .bss, and .rodata.Representative examples include:
arm64__associate_instruction_ops:larl %r6 -> R_390_PC32DBL .data.rel.ro+0x2
loongarch__associate_ins_ops:larl %r9 -> R_390_PC32DBL .data.rel.ro+0x22 larl %r9 -> R_390_PC32DBL .data.rel.ro+0x42
check_ppc_insn:larl %r4 -> R_390_PC32DBL .data.rel.local+0x4c2 larl %r2 -> R_390_PC32DBL .data.rel.ro.local+0x22 larl %r1 -> R_390_PC32DBL .data.rel.local+0x2 larl %r2 -> R_390_PC32DBL .data.rel.ro.local+0x2
s390__associate_ins_ops:larl %r10 -> R_390_PC32DBL .data.rel.ro+0x732 larl %r10 -> R_390_PC32DBL .data.rel.ro+0x752So from my reproducer, I don't think we can describe this simply as "all Python-extension dependency archives are missing -fPIC".
The issue narrows down much more specifically to perf-util-in.o in the
ET_DYNlink of the Python extension, and the direct text-to-data section relocations above look like the most relevant candidates.Quoted 5 lines
So I agree that -Wl,-z,notext is only a workaround. I'd prefer to understand and fix the underlying relocation pattern in perf-util-in.o rather than merge that as-is.
If useful, I can send the relevant readelf/objdump snippets in a follow-up.
Hi Jan,
I wonder about two tests:
1) adding V=1 JOBS=1 to your make command to switch on single threading and build verbosity. This may make it possible to identify the gcc command that is missing -fPIC. For example, I see -fPIC on x86:
gcc -Wp,-MD,/tmp/perf4/util/annotate-arch/.annotate-arm64.o.d(quoted diff) +0
-14-Wp,-MT,/tmp/perf4/util/annotate-arch/annotate -arm64.o -Wbad-function-cast -Wdeclaration-after-statement -Wformat-security -Wformat-y2k -Winit-self -Wmissin g-declarations -Wmissing-prototypes -Wno-system-headers -Wold-style-definition -Wpacked -Wredundant-decls -Wst rict-prototypes -Wswitch-default -Wswitch-enum -Wundef -Wwrite-strings -Wformat -Wno-type-limits -Wstrict-alia sing=3 -Wshadow -fno-strict-aliasing -I/tmp/perf4/arch/x86/include/generated -DHAVE_ARCH_X86_64_SUPPORT -Werro r -g -fno-omit-frame-pointer -Wall -Wextra -std=gnu11 -funsigned-char -fstack-protector-all -D_LARGEFILE64_SOU RCE -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -I~/linux/tools/perf/util/include -I~/linux/tools/perf/arch/x86/include -I/usr/local/google/home/irogers/re pos/linux4/tools/include/ -I~/linux/tools/arch/x86/include/uapi -I/usr/local/google/home/irogers/repos/linux4/tools/include/uapi
(quoted diff) +0
-4-I~/linux/tools/arc h/x86/include/ -I~/linux/tools/arch/x86/ -I/tmp/perf4//util -I/tmp/perf4/ -I~/linux/tools/perf/util -I~/linux/to ols/perf -DHAVE_PTHREAD_ATTR_SETAFFINITY_NP -DHAVE_PTHREAD_BARRIER -DHAVE_EVENTFD_SUPPORT -DHAVE_GETTID -DHAVE
_FILE_HANDLE-DHAVE_AIO_SUPPORT -DHAVE_SCANDIRAT_SUPPORT(quoted diff) +0
-12-DHAVE_SCHED_GETCPU_SUPPORT -DHAVE_SETNS_SUPPORT -DHA VE_ZLIB_SUPPORT -DHAVE_LIBELF_SUPPORT -DHAVE_ELF_GETPHDRNUM_SUPPORT -DHAVE_GELF_GETNOTE_SUPPORT -DHAVE_ELF_GET SHDRSTRNDX_SUPPORT -DHAVE_DEBUGINFOD_SUPPORT -DHAVE_LIBDW_SUPPORT -DHAVE_LIBBPF_SUPPORT -DHAVE_SDT_EVENT -DHAV E_JITDUMP -DHAVE_LIBOPENSSL_SUPPORT -DHAVE_BPF_SKEL -DHAVE_DWARF_UNWIND_SUPPORT -DHAVE_SLANG_SUPPORT -DHAVE_TI MERFD_SUPPORT -fPIC -DHAVE_LIBLLVM_SUPPORT -I/usr/lib/llvm-19/include -D_GNU_SOURCE -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -DHAVE_CXA_DEMANGLE_SUPPORT -DHAVE_LZMA_SUPPORT -DHAVE_ZSTD_SUPPORT -DHAVE_BACKTRACE_SUPPORT -DHAVE_LIBNUMA_SUPPORT -DHAVE_PERF_READ_VDSO32 -DHAVE_LIBBABELTRACE_SUPPORT -DHAVE_
LIBCAPSTONE_SUPPORT-DHAVE_JVMTI_CMLR -DHAVE_LIBPFM -DHAVE_LIBTRACEEVENT -I/usr/include/traceevent -DLIBTRACEE VENT_VERSION=67072 -DHAVE_RUST_SUPPORT -I/tmp/perf4/libapi/include(quoted diff) +0
-3-I/tmp/perf4/libbpf/include -I/tmp/perf4/li bsubcmd/include -I/tmp/perf4/libsymbol/include -I/tmp/perf4/libperf/include -I/tmp/perf4/ -D"BUILD_STR(s)=#s" -c -o
/tmp/perf4/util/annotate-arch/annotate-arm64.outil/annotate-arch/annotate-arm64.cQuoted 9 lines
2) adding EXTRA_CFLAGS="-fPIC" to your make command. This should force this option wherever it might be missing.
Thanks,
IanQuoted 3 lines
Best regards,
JanHi Ian,
I tested both suggestions the second in the beginning of my research:
EXTRA_CFLAGS="-fPIC" - Does NOT work. Same error:
/usr/bin/ld.bfd: error: read-only segment has dynamic relocations
collect2: error: ld returned 1 exit statusThe issue is s390x-specific
R_390_PC32DBLrelocations in perf-util-in.o (fromlarlinstructions) that persist even with -fPIC.Quoted 6 lines
Current options:
1. Use -Wl,-z,notext (security issue)
2. Refactor code to avoid these relocations
3. Disable Python extension on s390x (not an option)Thoughts?
So it appears that part of the problem is declaring arrays and structs to be const. Making things non-const I think is also not an option. Would EXTRA_CFLAGS="-fvisibility=hidden" help?
Quoted 6 lines
It would we interesting to build with -Wl,-z,notext and then look for the symbol the linker is fighting with:
eu-findtextrel perf.cpython....so
or
readelf -d perf.so | grep TEXTREL
readelf -r perf.so
Quoted 3 lines
I'm curious if it's s390 specific. I don't know what code patterns make it relocatable. Is taking an address of struct
ins_opsvariable a problem? Does it matter being a static variable?
So what I think the problem is that:
arm64__associate_instruction_ops:
larl %r6 -> R_390_PC32DBL .data.rel.ro+0x2
points at the arm64__associate_instruction_ops code:
https://web.git.kernel.org/pub/scm/linux/kernel/git/perf/perf-tools-next.git/tree/tools/perf/util/annotate-arch/annotate-arm64.c?h=perf-tools-next#n79
```c
static const struct ins_ops *arm64__associate_instruction_ops(struct
arch *arch, const char *name)
{
struct arch_arm64 *arm = container_of(arch, struct arch_arm64, arch);
const struct ins_ops *ops;
regmatch_t match[2];
if (!regexec(&arm->jump_insn, name, 2, match, 0))
ops = &jump_ops;
```jump_ops is declared extern in disasm.h:
https://web.git.kernel.org/pub/scm/linux/kernel/git/perf/perf-tools-next.git/tree/tools/perf/util/disasm.h?h=perf-tools-next#n117
```c tools/perf/util/disasm.h:extern const struct ins_ops jump_ops; ```
Since jump_ops is declared extern and would be patchable in a shared DSO (i.e., the Python module), GCC is creating relocations within .text that fails W^X checking in the linker. To prevent jump_ops from being patchable in this situation we can change its declaration to:
```c
__attribute__((visibility("hidden"))) extern const struct ins_ops jump_ops;
```but it is probably easier and more generic to add -fvisibility=hidden to the CFLAGS in Makefile.config. I asked about this behavior as an EXTRA_CFLAGS option, but I'm not sure if Jan has tested it yet?
Thanks,
Ian
Quoted 3 lines
Thanks,
Namhyung
Ian Rogers
reviewerQuoted 30 lines · Ian Rogers
So what I think the problem is that:
arm64__associate_instruction_ops:larl %r6 -> R_390_PC32DBL .data.rel.ro+0x2points at the
arm64__associate_instruction_opscode:
https://web.git.kernel.org/pub/scm/linux/kernel/git/perf/perf-tools-next.git/tree/tools/perf/util/annotate-arch/annotate-arm64.c?h=perf-tools-next#n79```c static const struct ins_ops *arm64__associate_instruction_ops(struct arch *arch, const char *name) { struct arch_arm64 *arm = container_of(arch, struct arch_arm64, arch); const struct ins_ops *ops; regmatch_t match[2]; if (!regexec(&arm->jump_insn, name, 2, match, 0)) ops = &jump_ops; ```
jump_opsis declared extern indisasm.h:
https://web.git.kernel.org/pub/scm/linux/kernel/git/perf/perf-tools-next.git/tree/tools/perf/util/disasm.h?h=perf-tools-next#n117```c tools/perf/util/disasm.h:extern const struct ins_ops jump_ops; ```Since
jump_opsis declaredexternand would be patchable in a shared DSO (i.e., the Python module), GCC is creating relocations within .text that fails W^X checking in the linker. To preventjump_opsfrom being patchable in this situation we can change its declaration to:
Quoted 7 lines
```c __attribute__((visibility("hidden"))) extern const struct ins_ops jump_ops; ```but it is probably easier and more generic to add -fvisibility=hidden to the CFLAGS in Makefile.config. I asked about this behavior as an
EXTRA_CFLAGSoption, but I'm not sure if Jan has tested it yet?
Hi Jan,
Did you get a chance to see if the hidden visibility would resolve this issue? I can propose a patch (or someone else can) to add -fvisibility=hidden to the CFLAGS in tools/perf/Makefile.config.
Thanks,
Ian
Quoted 113 lines · Namhyung Kim
Quoted 112 lines · Ian Rogers
Quoted 111 lines · Jan Polensky
Quoted 110 lines · Ian Rogers
Quoted 109 lines · Jan Polensky
Quoted 61 lines · Arnaldo Carvalho de Melo
Quoted 32 lines · On Wed, May 20, 2026 at 01:09:05PM +0000, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] The patch explicitly allows text relocations in the Python extension shared object (perf.so) by adding-Wl,-z,notext, masking the root cause of missing-fPICflags and breaking W^X (Write XOR Execute) security protections. --commit
f3832fb0ad0d1f9c2e69039b2a5110f77d220d5e
Author: Jan Polensky <[email protected]>perf build: Fix Python extension build with GCC 16 hardening
This commit addresses a build failure in the Python extension caused by GCC 16 stricter security policies regarding dynamic relocations in read-only segments. It adds the -Wl,-z,notext flag to the linker to explicitly allow text relocations in the Python extension shared object.
tools/perf/Makefile.perf all: shell_compatibility_test $(ALL_PROGRAMS) $(LANG_BINDINGS) $(OTHER_PROGRAMS) · line 752
Quoted 10 lines
tools/perf/Makefile.perf +1
-1@@ -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" \[Severity: Medium]
Does adding -Wl,-z,notext mask the root cause of the GCC 16 linker error?The error regarding dynamic relocations in a read-only segment usually indicates that non-position-independent code from the static libraries is being linked into the shared library.
Quoted 7 lines
Using -Wl,-z,notext suppresses this error by allowing text relocations, which forces the dynamic linker to map the executable text segment as writable at load time. This breaks W^X security protections and weakens the security of any Python process loading the extension.
Should the required object files be compiled with -fPIC instead to properly resolve the linker error without compromising security?
So I asked Claude about this and he suggests doing like other parts of perf and using -fPIC, can you try to do that?
---------------------------------------
● Sashiko is right. -Wl,-z,notext is a band-aid that weakens security —
it makes the text segment writable at load time, defeating W^X.The root cause is that
PERFLIBS_PY(the static libraries linked into the Python .so) are compiled without -fPIC. When you link non-PIC .a archives into a shared library, the linker needs text relocations. GCC 16 correctly rejects this by default.The proper fix is to build the static libraries that go into the Python extension with -fPIC. The perf build system already has precedent for this — look for how libperf and libsubcmd handle PIC builds. The Python extension's dependencies need a parallel -fPIC build, or the existing objects need to be compiled with -fPIC when the Python binding is enabled.
Quoted 5 lines
The -Wl,-z,notext approach would also cause issues on distros that enforce RELRO +
BIND_NOWhardening, and some package builders would reject the resulting .so outright.- Arnaldo
Hi Arnaldo,
thanks for the pointer. I double-checked this on s390x and did some reduced-link testing for the
ET_DYN/ Python-extension case.The failure is not triggered by the perf Python binding object alone.
A reduced reproducer succeeds when linking:
python.o
+ libapi.a
+ libperf.a
+ libsubcmd.a
+ libsymbol.a
+ libbpf.a
+ libperf-ui.aThe link starts to fail with:
/usr/bin/ld.bfd: error: read-only segment has dynamic relocationsas soon as libperf-util.a is added.
In my tree, libperf-util.a currently contains only perf-util-in.o, and replacing the archive with that single object still reproduces the same failure.
Reduced reproducer:
gcc -pthread -shared \ -Wl,-z,noexecstack \ -o /tmp/try-perf-util-only.so \ /root/linux/tools/perf/python_ext_build/tmp/root/linux/tools/perf/util/python.o \ -Wl,--whole-archive \ /root/linux/tools/perf/libapi/libapi.a \ /root/linux/tools/perf/libperf/libperf.a \ /root/linux/tools/perf/libsubcmd/libsubcmd.a \ /root/linux/tools/perf/libsymbol/libsymbol.a \ /root/linux/tools/perf/libbpf/libbpf.a \ /root/linux/tools/perf/libperf-ui.a \ -Wl,--no-whole-archive \ /root/perf-util-one/perf-util-in.o \ -lpython3.14 -lpthread -ldl -lm -lutil
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Inspection of perf-util-in.o on s390x shows many direct
R_390_PC32DBLrelocations from .text against .data.rel.ro, .data.rel.ro.local, .data.rel.local, .bss, and .rodata.
R_390_PC32DBL are PC-relative relocations that usually get resolved by the linker during final link of the shared library.
Quoted 91 lines
Quoted 91 lines
Quoted 91 lines
Quoted 91 lines
Quoted 27 lines
Representative examples include:
arm64__associate_instruction_ops:larl %r6 -> R_390_PC32DBL .data.rel.ro+0x2
loongarch__associate_ins_ops:larl %r9 -> R_390_PC32DBL .data.rel.ro+0x22 larl %r9 -> R_390_PC32DBL .data.rel.ro+0x42
check_ppc_insn:larl %r4 -> R_390_PC32DBL .data.rel.local+0x4c2 larl %r2 -> R_390_PC32DBL .data.rel.ro.local+0x22 larl %r1 -> R_390_PC32DBL .data.rel.local+0x2 larl %r2 -> R_390_PC32DBL .data.rel.ro.local+0x2
s390__associate_ins_ops:larl %r10 -> R_390_PC32DBL .data.rel.ro+0x732 larl %r10 -> R_390_PC32DBL .data.rel.ro+0x752So from my reproducer, I don't think we can describe this simply as "all Python-extension dependency archives are missing -fPIC".
The issue narrows down much more specifically to perf-util-in.o in the
ET_DYNlink of the Python extension, and the direct text-to-data section relocations above look like the most relevant candidates.Quoted 5 lines
So I agree that -Wl,-z,notext is only a workaround. I'd prefer to understand and fix the underlying relocation pattern in perf-util-in.o rather than merge that as-is.
If useful, I can send the relevant readelf/objdump snippets in a follow-up.
Hi Jan,
I wonder about two tests:
1) adding V=1 JOBS=1 to your make command to switch on single threading and build verbosity. This may make it possible to identify the gcc command that is missing -fPIC. For example, I see -fPIC on x86:
gcc -Wp,-MD,/tmp/perf4/util/annotate-arch/.annotate-arm64.o.d(quoted diff) +0
-14-Wp,-MT,/tmp/perf4/util/annotate-arch/annotate -arm64.o -Wbad-function-cast -Wdeclaration-after-statement -Wformat-security -Wformat-y2k -Winit-self -Wmissin g-declarations -Wmissing-prototypes -Wno-system-headers -Wold-style-definition -Wpacked -Wredundant-decls -Wst rict-prototypes -Wswitch-default -Wswitch-enum -Wundef -Wwrite-strings -Wformat -Wno-type-limits -Wstrict-alia sing=3 -Wshadow -fno-strict-aliasing -I/tmp/perf4/arch/x86/include/generated -DHAVE_ARCH_X86_64_SUPPORT -Werro r -g -fno-omit-frame-pointer -Wall -Wextra -std=gnu11 -funsigned-char -fstack-protector-all -D_LARGEFILE64_SOU RCE -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -I~/linux/tools/perf/util/include -I~/linux/tools/perf/arch/x86/include -I/usr/local/google/home/irogers/re pos/linux4/tools/include/ -I~/linux/tools/arch/x86/include/uapi -I/usr/local/google/home/irogers/repos/linux4/tools/include/uapi
(quoted diff) +0
-4-I~/linux/tools/arc h/x86/include/ -I~/linux/tools/arch/x86/ -I/tmp/perf4//util -I/tmp/perf4/ -I~/linux/tools/perf/util -I~/linux/to ols/perf -DHAVE_PTHREAD_ATTR_SETAFFINITY_NP -DHAVE_PTHREAD_BARRIER -DHAVE_EVENTFD_SUPPORT -DHAVE_GETTID -DHAVE
_FILE_HANDLE-DHAVE_AIO_SUPPORT -DHAVE_SCANDIRAT_SUPPORT(quoted diff) +0
-12-DHAVE_SCHED_GETCPU_SUPPORT -DHAVE_SETNS_SUPPORT -DHA VE_ZLIB_SUPPORT -DHAVE_LIBELF_SUPPORT -DHAVE_ELF_GETPHDRNUM_SUPPORT -DHAVE_GELF_GETNOTE_SUPPORT -DHAVE_ELF_GET SHDRSTRNDX_SUPPORT -DHAVE_DEBUGINFOD_SUPPORT -DHAVE_LIBDW_SUPPORT -DHAVE_LIBBPF_SUPPORT -DHAVE_SDT_EVENT -DHAV E_JITDUMP -DHAVE_LIBOPENSSL_SUPPORT -DHAVE_BPF_SKEL -DHAVE_DWARF_UNWIND_SUPPORT -DHAVE_SLANG_SUPPORT -DHAVE_TI MERFD_SUPPORT -fPIC -DHAVE_LIBLLVM_SUPPORT -I/usr/lib/llvm-19/include -D_GNU_SOURCE -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -DHAVE_CXA_DEMANGLE_SUPPORT -DHAVE_LZMA_SUPPORT -DHAVE_ZSTD_SUPPORT -DHAVE_BACKTRACE_SUPPORT -DHAVE_LIBNUMA_SUPPORT -DHAVE_PERF_READ_VDSO32 -DHAVE_LIBBABELTRACE_SUPPORT -DHAVE_
LIBCAPSTONE_SUPPORT-DHAVE_JVMTI_CMLR -DHAVE_LIBPFM -DHAVE_LIBTRACEEVENT -I/usr/include/traceevent -DLIBTRACEE VENT_VERSION=67072 -DHAVE_RUST_SUPPORT -I/tmp/perf4/libapi/include(quoted diff) +0
-3-I/tmp/perf4/libbpf/include -I/tmp/perf4/li bsubcmd/include -I/tmp/perf4/libsymbol/include -I/tmp/perf4/libperf/include -I/tmp/perf4/ -D"BUILD_STR(s)=#s" -c -o
/tmp/perf4/util/annotate-arch/annotate-arm64.outil/annotate-arch/annotate-arm64.c2) adding EXTRA_CFLAGS="-fPIC" to your make command. This should force this option wherever it might be missing.
Thanks,
IanQuoted 3 lines
Best regards,
Jan
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Hi Ian,
I tested both suggestions the second in the beginning of my research:
EXTRA_CFLAGS="-fPIC" - Does NOT work. Same error:
/usr/bin/ld.bfd: error: read-only segment has dynamic relocations
collect2: error: ld returned 1 exit status
This did not work because EXTRA_CLFAGS do not get appended to CXXFLAGS (see below).
Quoted 11 lines
Quoted 11 lines
Quoted 10 lines
The issue is s390x-specific
R_390_PC32DBLrelocations in perf-util-in.o (fromlarlinstructions) that persist even with -fPIC.Current options:
1. Use -Wl,-z,notext (security issue)
2. Refactor code to avoid these relocations
3. Disable Python extension on s390x (not an option)Thoughts?
Quoted 7 lines
Quoted 7 lines
So it appears that part of the problem is declaring arrays and structs to be const. Making things non-const I think is also not an option. Would EXTRA_CFLAGS="-fvisibility=hidden" help?
It would we interesting to build with -Wl,-z,notext and then look for the symbol the linker is fighting with:
eu-findtextrel perf.cpython....so
Thanks for the hint!
$ eu-findtextrel python_ext_build/lib/perf.cpython-314-s390x-linux-gnu.so either the file containing the function '_ZN4llvm7hashing6detailL2k3E' or the file containing the function '_ZN4llvm7numbersL7inv_pisE' is not compiled with -fpic/-fPIC either the file containing the function '_ZN4llvm7numbersL3pisE' or the file containing the function '_ZN4llvmL18BitReverseTable256E' is not compiled with -fpic/-fPIC
Inspecting the objdump -wDzR output shows that the issue is caused by a few R_390_RELATIVE relocations in .rodata:
$ objdump -wDzR tools/perf/python/perf.cpython-314-s390x-linux-gnu.so ... Disassembly of section .rodata: ... 000000000034efc8 <_ZN4llvm7hashing6detailL2k3E>: 34efc8: c9 49 d7 c7 .long 0xc949d7c7 34efcc: 50 9e 65 57 st %r9,1367(%r14,%r6) 34efd0: 33 2e lcer %f2,%f14 ... 34eff4: 38 34 ler %f3,%f4 34eff6: 00 00 .long 0x00000000 34eff8: R_390_RELATIVE *ABS*+0x34efd0
000000000034eff8 <_ZN4llvm7numbersL3pisE>:
34eff8: 00 00 00 00 .long 0x00000000 34eff8: R_390_RELATIVE *ABS*+0x34efd0 34effc: 00 34 ef d0 .long 0x0034efd0 34f000: 30 2e lper %f2,%f14 ... 34f024: 32 34 lter %f3,%f4 34f026: 00 00 .long 0x00000000 34f028: R_390_RELATIVE *ABS*+0x34f000
000000000034f028 <_ZN4llvm7numbersL7inv_pisE>:
34f028: 00 00 00 00 .long 0x00000000 34f028: R_390_RELATIVE *ABS*+0x34f000 34f02c: 00 34 f0 00 .long 0x0034f000
000000000034f030 <_ZN4llvmL18BitReverseTable256E>:
34f030: 00 80 40 c0 .long 0x008040c0
...
They originate from util/llvm-c-helpers.o, which gets linked into util/perf-util-in.o, which gets linked into perf-util-in.o, which gets copied into archive libperf-util.a, which finally gets linked into the shared library:
$ find . -type f -name "*.o" -print0 | xargs -0 nm -A | grep "_ZN4llvm7numbersL7inv_pisE" nm: ./tools/perf/arch/perf-test-in.o: no symbols ./tools/perf/perf-util-in.o:000000000006e240 r _ZN4llvm7numbersL7inv_pisE ./tools/perf/util/llvm-c-helpers.o:0000000000000080 r _ZN4llvm7numbersL7inv_pisE ./tools/perf/util/perf-util-in.o:000000000006e240 r _ZN4llvm7numbersL7inv_pisE
util/llvm-c-helpers.o is built from util/llvm-c-helpers.cc. The cause is that CXXFLAGS for s390 is missing -fPIC. Commit a9a3f1d18a6c ("perf
s390: Always build with -fPIC") added compiler option -fPIC to CFLAGS for s390. I have sent a new patch to resolve this issue:
[PATCH] perf s390: Fix TEXTREL in Python extension by compiling as PIC
https://lore.kernel.org/all/[email protected]/
Quoted 7 lines
Quoted 3 lines
or
readelf -d perf.so | grep TEXTREL
readelf -r perf.soI'm curious if it's s390 specific. I don't know what code patterns make it relocatable. Is taking an address of struct
ins_opsvariable a problem? Does it matter being a static variable?
I did not investigate why other architectures do not (need to) add -fPIC to CFLAGS/CXXFLAGS in tools/perf/Makefile.config although a shared library is built.
Regards,
Jens
Signature · 6 lines
Jens Remus Linux on Z Development (D3303) [email protected] / [email protected] IBM Deutschland Research & Development GmbH; Vorsitzender des Aufsichtsrats: Wolfgang Wendt; Geschäftsführung: David Faller; Sitz der Gesellschaft: Ehningen; Registergericht: Amtsgericht Stuttgart, HRB 243294 IBM Data Privacy Statement: https://www.ibm.com/privacy/
Quoted 32 lines · Jan Polensky
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 statusThis 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 <[email protected]>Signed-off-by Jan Polensky <[email protected]>--- tools/perf/Makefile.perf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)tools/perf/Makefile.perf +0
-0
tools/perf/Makefile.perf all: shell_compatibility_test $(ALL_PROGRAMS) $(LANG_BINDINGS) $(OTHER_PROGRAMS) · line 755
Quoted 8 lines
tools/perf/Makefile.perf +1
-1@@ -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; \
Thanks for fixing this.
Signature · 9 lines
Thomas Richter, Dept 3303, IBM s390 Linux Development, Boeblingen, Germany -- IBM Deutschland Research & Development GmbH Vorsitzender des Aufsichtsrats: Wolfgang Wendt Geschäftsführung: David Faller Sitz der Gesellschaft: Böblingen / Registergericht: Amtsgericht Stuttgart, HRB 243294