From: Puranjay Mohan <puranjay@kernel.org>
Two functions contribute entries to one special section but only one is
patched. Cloning the neighbouring entry drags in whatever it points at,
which is how a livepatch ends up holding relocations against unrelated
code.
Signed-off-by: Puranjay Mohan <puranjay@kernel.org>
Assisted-by: Claude:claude-opus-5
Signed-off-by: Song Liu <song@kernel.org>
---
.../generic/fixtures/special_section_shared.c | 31 +++++++++++++++++++
.../generic/test-special-section-shared.sh | 26 ++++++++++++++++
2 files changed, 57 insertions(+)
create mode 100644 tools/objtool/tests/generic/fixtures/special_section_shared.c
create mode 100755 tools/objtool/tests/generic/test-special-section-shared.sh
diff --git a/tools/objtool/tests/generic/fixtures/special_section_shared.c b/tools/objtool/tests/generic/fixtures/special_section_shared.c
new file mode 100644
index 000000000000..f54e24f862c6
--- /dev/null
+++ b/tools/objtool/tests/generic/fixtures/special_section_shared.c
@@ -0,0 +1,31 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Two functions contribute to one special section; only one is patched. */
+
+static const char __modinfo[]
+ __attribute__((section(".modinfo"), used, aligned(1))) = "\0name=vmlinux";
+
+int other(int x)
+{
+ asm volatile(
+ "2:\n\t"
+ ".pushsection .kcfi_traps, \"a\"\n\t"
+ ".balign 4\n\t"
+ ".long 2b - .\n\t"
+ ".popsection\n\t");
+ return x * 5;
+}
+
+int target(int x)
+{
+ asm volatile(
+ "1:\n\t"
+ ".pushsection .kcfi_traps, \"a\"\n\t"
+ ".balign 4\n\t"
+ ".long 1b - .\n\t"
+ ".popsection\n\t");
+#ifdef PATCHED
+ return x + 2;
+#else
+ return x + 1;
+#endif
+}diff --git a/tools/objtool/tests/generic/test-special-section-shared.sh b/tools/objtool/tests/generic/test-special-section-shared.sh
new file mode 100755
index 000000000000..05c6110b6aef
--- /dev/null
+++ b/tools/objtool/tests/generic/test-special-section-shared.sh
@@ -0,0 +1,26 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0
+#
+# Only the patched function's special section entry may be extracted.
+
+. "$(dirname "$0")/../lib.sh"
+
+setup
+build_pair special_section_shared.c
+
+has_input_section orig.o .kcfi_traps ||
+ probe_skip "fixture produced no .kcfi_traps on this arch"
+
+run_diff
+
+assert_patched target
+assert_not_patched other
+assert_section ".kcfi_traps"
+
+entries="$(out_relocs | awk '/rela\.kcfi_traps/,/^$/' | grep -c 'target')"
+[ "$entries" = 1 ] || fail "expected one .kcfi_traps entry, found $entries"
+
+out_relocs | awk '/rela\.kcfi_traps/,/^$/' | grep -q 'other' &&
+ fail "the untouched function's entry was dragged in"
+
+pass "only the patched function's entry extracted"
--
2.53.0-Meta