Skip to contentSkip to conversation

perf: Fix missing CET property notes on x86 with -fcf-protection

PATCH In review Trevor Allison 3 participants 7 messages Jul 8 – Jul 13 perf events

lore
Share & tools
lore
#0 lore raw

cover perf: Fix missing CET property notes on x86 with -fcf-protection

When perf is built with -fcf-protection, the x86 binary can lack CET (IBT/SHSTK) in .note.gnu.property because perf links several static archives whole-archive: if any input object lacks matching property notes, the linker drops CET from the final binary.

Fix libbpf EXTRA_CFLAGS handling, C++ flag propagation, and missing .note.gnu.property sections in x86 assembly (like .note.GNU-stack). All three are needed for CET to survive the link.

Trevor Allison (3):
perf build: Preserve EXTRA_CFLAGS when building in-tree libbpf perf build: Apply EXTRA_CXXFLAGS to C++ builds
perf x86: Add .note.gnu.property to assembly linked into perf

 tools/perf/Makefile.config               |  1 +
 tools/perf/Makefile.perf                 |  2 +-
 tools/perf/arch/x86/tests/regs_load.S    | 24 ++++++++++++++++++++++++
 tools/perf/bench/mem-memcpy-x86-64-asm.S | 24 ++++++++++++++++++++++++
 tools/perf/bench/mem-memset-x86-64-asm.S | 24 ++++++++++++++++++++++++
 5 files changed, 74 insertions(+), 1 deletion(-)
Signature · 1 line
2.50.1 (Apple Git-155)
#1 lore raw

PATCH 1/3 perf build: Preserve EXTRA_CFLAGS when building in-tree libbpf

The libbpf sub-make passes EXTRA_CFLAGS="-fPIC", replacing the parent EXTRA_CFLAGS. Append -fPIC instead, since libbpf.a is linked whole-archive into perf.

---
 tools/perf/Makefile.perf | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/perf/Makefile.perf +1 -1
index 2b53d9e0c73b..a59ed1d70ecc 100644
@@ -706,7 +706,7 @@ $(LIBAPI)-clean:
 $(LIBBPF): FORCE | $(LIBBPF_OUTPUT)
 	$(Q)$(MAKE) -C $(LIBBPF_DIR) FEATURES_DUMP=$(FEATURE_DUMP_EXPORT) \
 		O= OUTPUT=$(LIBBPF_OUTPUT)/ DESTDIR=$(LIBBPF_DESTDIR) prefix= subdir= \
-		EXTRA_CFLAGS="-fPIC" $@ install_headers
+		EXTRA_CFLAGS="$(EXTRA_CFLAGS) -fPIC" $@ install_headers
 
 $(LIBBPF)-clean:
 	$(call QUIET_CLEAN, libbpf)
Signature · 1 line
2.50.1 (Apple Git-155)
#2 lore raw

PATCH 2/3 perf build: Apply EXTRA_CXXFLAGS to C++ builds

In reply to Trevor Allison

C++ sources did not receive EXTRA_CXXFLAGS or EXTRA_CFLAGS, unlike C sources. Initialize CXXFLAGS from both so C++ objects such as demangle-cxx.o get the same builder flags as the rest of perf.

---
 tools/perf/Makefile.config | 1 +
 1 file changed, 1 insertion(+)
tools/perf/Makefile.config +1 -0
index 0ba307e78fe1..052a6cc7942f 100644
@@ -17,6 +17,7 @@ detected     = $(shell echo "$(1)=y"       >> $(OUTPUT).config-detected)
 detected_var = $(shell echo "$(1)=$($(1))" >> $(OUTPUT).config-detected)
 
 CFLAGS := $(EXTRA_CFLAGS) $(filter-out -Wnested-externs,$(EXTRA_WARNINGS))
+CXXFLAGS := $(EXTRA_CXXFLAGS) $(EXTRA_CFLAGS)
 HOSTCFLAGS := $(filter-out -Wnested-externs,$(EXTRA_WARNINGS))
 
 # This is required because the kernel is built with this and some of the code
Signature · 1 line
2.50.1 (Apple Git-155)
#3 lore raw

Re: [PATCH 2/3] perf build: Apply EXTRA_CXXFLAGS to C++ builds

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Medium] C++ feature detection tests do not receive `EXTRA_CFLAGS`, causing a structural mismatch between…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] C++ feature detection tests do not receive EXTRA_CFLAGS, causing a structural mismatch between feature detection and actual build compilation flags.
- [Low] Injecting EXTRA_CFLAGS into CXXFLAGS breaks C++ builds when C-specific flags are present.
--

--- Patch [2]: [PATCH 2/3] perf build: Apply EXTRA_CXXFLAGS to C++ builds --- commit 4b2b9e4dd8644e0e1fa95ec876a875596e1e9178
Author: Trevor Allison <[email protected]>

perf build: Apply EXTRA_CXXFLAGS to C++ builds

The commit initializes CXXFLAGS in tools/perf/Makefile.config with both EXTRA_CXXFLAGS and EXTRA_CFLAGS so that C++ objects receive the same builder flags as the rest of perf.

tools/perf/Makefile.config detected = $(shell echo "$(1)=y" >> $(OUTPUT).config-detected) · line 20

Quoted 9 lines
tools/perf/Makefile.config +1 -0
index 0ba307e78fe1f..052a6cc7942f9 100644
@@ -17,6 +17,7 @@ detected     = $(shell echo "$(1)=y"       >> $(OUTPUT).config-detected)
 detected_var = $(shell echo "$(1)=$($(1))" >> $(OUTPUT).config-detected)
 
 CFLAGS := $(EXTRA_CFLAGS) $(filter-out -Wnested-externs,$(EXTRA_WARNINGS))
+CXXFLAGS := $(EXTRA_CXXFLAGS) $(EXTRA_CFLAGS)

[Severity: Low]
Could this cause a build regression if EXTRA_CFLAGS contains C-only flags?

If EXTRA_CFLAGS contains flags that are only valid for C/ObjC (such as -Wstrict-prototypes), applying them to C++ files causes compiler warnings. With -Werror enabled, this results in a build failure.

[Severity: Medium]
Does this create a mismatch between feature detection and the main build?

While CXXFLAGS here receives EXTRA_CFLAGS, the C++ feature detection tests in tools/build/Makefile.feature still only use:

CXXFLAGS="$(EXTRA_CXXFLAGS) $(FEATURE_CHECK_CXXFLAGS-$(1))"

If EXTRA_CFLAGS contains mandatory compiler flags like --sysroot or -m32, the C++ feature detection tests will fail without EXTRA_CFLAGS. This causes false negatives where the perf binary silently lacks supported features, despite the main build succeeding.

Quoted 1 line
tools/perf/Makefile.config +0 -0
 HOSTCFLAGS := $(filter-out -Wnested-externs,$(EXTRA_WARNINGS))
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
#4 lore raw

PATCH 3/3 perf x86: Add .note.gnu.property to assembly linked into perf

In reply to Trevor Allison

Assembly sources do not get .note.gnu.property from the compiler. Add the section to the x86 files that are linked into perf, using the same approach as the existing .note.GNU-stack notes.

---
 tools/perf/arch/x86/tests/regs_load.S    | 24 ++++++++++++++++++++++++
 tools/perf/bench/mem-memcpy-x86-64-asm.S | 24 ++++++++++++++++++++++++
 tools/perf/bench/mem-memset-x86-64-asm.S | 24 ++++++++++++++++++++++++
 3 files changed, 72 insertions(+)
tools/perf/arch/x86/tests/regs_load.S +24 -0
index 80f14f52e3f6..1947f95cceda 100644
@@ -97,3 +97,27 @@ SYM_FUNC_END(perf_regs_load)
  * the ELF stack should not be restricted at all and set it RWX.
  */
 .section .note.GNU-stack,"",@progbits
+
+/*
+ * We need to provide .note.gnu.property section with IBT and SHSTK,
+ * saying that this object supports the same CET properties as C code
+ * built with -fcf-protection. Otherwise static linking drops CET from
+ * the final perf binary when other objects are CET-enabled.
+ */
+	.pushsection .note.gnu.property, "a"
+	.p2align 3
+	.long 1f - 0f
+	.long 4f - 1f
+	.long 5
+0:
+	.asciz "GNU"
+1:
+	.p2align 3
+	.long 0xc0000002
+	.long 3f - 2f
+2:
+	.long 3
+3:
+	.p2align 3
+4:
+	.popsection
tools/perf/bench/mem-memcpy-x86-64-asm.S +24 -0
index 1b9fef7efcdc..e7b84aa43ab4 100644
@@ -18,3 +18,27 @@
  * the ELF stack should not be restricted at all and set it RWX.
  */
 .section .note.GNU-stack,"",@progbits
+
+/*
+ * We need to provide .note.gnu.property section with IBT and SHSTK,
+ * saying that this object supports the same CET properties as C code
+ * built with -fcf-protection. Otherwise static linking drops CET from
+ * the final perf binary when other objects are CET-enabled.
+ */
+	.pushsection .note.gnu.property, "a"
+	.p2align 3
+	.long 1f - 0f
+	.long 4f - 1f
+	.long 5
+0:
+	.asciz "GNU"
+1:
+	.p2align 3
+	.long 0xc0000002
+	.long 3f - 2f
+2:
+	.long 3
+3:
+	.p2align 3
+4:
+	.popsection
tools/perf/bench/mem-memset-x86-64-asm.S +24 -0
index abd26c95f1aa..f8002da91c3c 100644
@@ -13,3 +13,27 @@
  * the ELF stack should not be restricted at all and set it RWX.
  */
 .section .note.GNU-stack,"",@progbits
+
+/*
+ * We need to provide .note.gnu.property section with IBT and SHSTK,
+ * saying that this object supports the same CET properties as C code
+ * built with -fcf-protection. Otherwise static linking drops CET from
+ * the final perf binary when other objects are CET-enabled.
+ */
+	.pushsection .note.gnu.property, "a"
+	.p2align 3
+	.long 1f - 0f
+	.long 4f - 1f
+	.long 5
+0:
+	.asciz "GNU"
+1:
+	.p2align 3
+	.long 0xc0000002
+	.long 3f - 2f
+2:
+	.long 3
+3:
+	.p2align 3
+4:
+	.popsection
Signature · 1 line
2.50.1 (Apple Git-155)
#5 lore raw

Re: [PATCH 3/3] perf x86: Add .note.gnu.property to assembly linked into perf

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Medium] Adding .note.gnu.property asserts IBT support without providing ENDBR64 instructions, causing a…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] Adding .note.gnu.property asserts IBT support without providing ENDBR64 instructions, causing a #CP crash in perf memory benchmarks.
- [Medium] The .note.gnu.property section hardcodes 8-byte alignment, violating the 32-bit x86 ELF specification.
--

--- Patch [3]: [PATCH 3/3] perf x86: Add .note.gnu.property to assembly linked into perf --- commit b48d7edb7fb18bc3dace2af272de108eb561d69b
Author: Trevor Allison <[email protected]>

perf x86: Add .note.gnu.property to assembly linked into perf

This commit adds .note.gnu.property to x86 assembly files linked into perf to indicate CET properties support. This is intended to prevent static linking from dropping CET from the final binary.

Quoted 4 lines
tools/perf/arch/x86/tests/regs_load.S +0 -0
index 80f14f52e3f60..1947f95cceda5 100644

[ ... ]

tools/perf/arch/x86/tests/regs_load.S SYM_FUNC_END(perf_regs_load) · line 108

Quoted 13 lines
tools/perf/arch/x86/tests/regs_load.S +9 -0
@@ -97,3 +97,27 @@ SYM_FUNC_END(perf_regs_load)
  * the ELF stack should not be restricted at all and set it RWX.
  */
 .section .note.GNU-stack,"",@progbits
+
+/*
+ * We need to provide .note.gnu.property section with IBT and SHSTK,
+ * saying that this object supports the same CET properties as C code
+ * built with -fcf-protection. Otherwise static linking drops CET from
+ * the final perf binary when other objects are CET-enabled.
+ */
+	.pushsection .note.gnu.property, "a"
+	.p2align 3

[Severity: Medium]
Does this unconditionally hardcode 8-byte alignment for 32-bit x86 builds?

The assembly file regs_load.S can be compiled for 32-bit x86, which requires 4-byte alignment (.p2align 2) for ELF notes. When the linker merges these, the forced 8-byte alignment can create zero-byte padding gaps between notes, corrupting the contiguous ELF note structure.

Could this cause the dynamic loader or tools like readelf to fail to parse the ELF notes properly?

[ ... ]

Quoted 4 lines
tools/perf/bench/mem-memcpy-x86-64-asm.S +0 -0
index 1b9fef7efcdcc..e7b84aa43ab44 100644

[ ... ]

tools/perf/bench/mem-memcpy-x86-64-asm.S line 29

Quoted 13 lines
tools/perf/bench/mem-memcpy-x86-64-asm.S +9 -0
@@ -18,3 +18,27 @@
  * the ELF stack should not be restricted at all and set it RWX.
  */
 .section .note.GNU-stack,"",@progbits
+
+/*
+ * We need to provide .note.gnu.property section with IBT and SHSTK,
+ * saying that this object supports the same CET properties as C code
+ * built with -fcf-protection. Otherwise static linking drops CET from
+ * the final perf binary when other objects are CET-enabled.
+ */
+	.pushsection .note.gnu.property, "a"
+	.p2align 3

[Severity: Medium]
Will this cause a Control Protection Exception (#CP) crash on IBT-capable hardware when running perf bench mem memcpy?

Adding this section artificially claims IBT support and forces the linker to enable CET on the final perf binary. However, perf's local linkage headers do not automatically emit endbr64 instructions for SYM_FUNC_START.

When do_memcpy() in tools/perf/bench/mem-functions.c indirectly calls these assembly entry points like memcpy_orig:

	fn(dst, src, size);

the hardware may detect the missing endbr64 on the indirect branch target and throw a #CP fault.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
maintainer
#6 lore raw

Re: [PATCH 3/3] perf x86: Add .note.gnu.property to assembly linked into perf

Hello,

Quoted 4 lines · On Wed, Jul 08, 2026 at 05:08:39AM +0000, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] Adding .note.gnu.property asserts IBT support without providing ENDBR64 instructions, causing a #CP crash in perf memory benchmarks.
- [Medium] The .note.gnu.property section hardcodes 8-byte alignment, violating the 32-bit x86 ELF specification.
--

Can you please take a look and address the both comments?

Thanks,
Namhyung

Quoted 79 lines

--- Patch [3]: [PATCH 3/3] perf x86: Add .note.gnu.property to assembly linked into perf --- commit b48d7edb7fb18bc3dace2af272de108eb561d69b
Author: Trevor Allison <[email protected]>

perf x86: Add .note.gnu.property to assembly linked into perf

This commit adds .note.gnu.property to x86 assembly files linked into perf to indicate CET properties support. This is intended to prevent static linking from dropping CET from the final binary.

Quoted 4 lines
tools/perf/arch/x86/tests/regs_load.S +0 -0
index 80f14f52e3f60..1947f95cceda5 100644

[ ... ]

tools/perf/arch/x86/tests/regs_load.S SYM_FUNC_END(perf_regs_load) · line 108

Quoted 13 lines
tools/perf/arch/x86/tests/regs_load.S +9 -0
@@ -97,3 +97,27 @@ SYM_FUNC_END(perf_regs_load)
  * the ELF stack should not be restricted at all and set it RWX.
  */
 .section .note.GNU-stack,"",@progbits
+
+/*
+ * We need to provide .note.gnu.property section with IBT and SHSTK,
+ * saying that this object supports the same CET properties as C code
+ * built with -fcf-protection. Otherwise static linking drops CET from
+ * the final perf binary when other objects are CET-enabled.
+ */
+	.pushsection .note.gnu.property, "a"
+	.p2align 3

[Severity: Medium]
Does this unconditionally hardcode 8-byte alignment for 32-bit x86 builds?

The assembly file regs_load.S can be compiled for 32-bit x86, which requires 4-byte alignment (.p2align 2) for ELF notes. When the linker merges these, the forced 8-byte alignment can create zero-byte padding gaps between notes, corrupting the contiguous ELF note structure.

Could this cause the dynamic loader or tools like readelf to fail to parse the ELF notes properly?

[ ... ]

Quoted 4 lines
tools/perf/bench/mem-memcpy-x86-64-asm.S +0 -0
index 1b9fef7efcdcc..e7b84aa43ab44 100644

[ ... ]

tools/perf/bench/mem-memcpy-x86-64-asm.S line 29

Quoted 13 lines
tools/perf/bench/mem-memcpy-x86-64-asm.S +9 -0
@@ -18,3 +18,27 @@
  * the ELF stack should not be restricted at all and set it RWX.
  */
 .section .note.GNU-stack,"",@progbits
+
+/*
+ * We need to provide .note.gnu.property section with IBT and SHSTK,
+ * saying that this object supports the same CET properties as C code
+ * built with -fcf-protection. Otherwise static linking drops CET from
+ * the final perf binary when other objects are CET-enabled.
+ */
+	.pushsection .note.gnu.property, "a"
+	.p2align 3

[Severity: Medium]
Will this cause a Control Protection Exception (#CP) crash on IBT-capable hardware when running perf bench mem memcpy?

Adding this section artificially claims IBT support and forces the linker to enable CET on the final perf binary. However, perf's local linkage headers do not automatically emit endbr64 instructions for SYM_FUNC_START.

When do_memcpy() in tools/perf/bench/mem-functions.c indirectly calls these assembly entry points like memcpy_orig:

	fn(dst, src, size);

the hardware may detect the missing endbr64 on the indirect branch target and throw a #CP fault.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3