From: Puranjay Mohan <puranjay@kernel.org>
Init code and data are freed once boot finishes, so a klp relocation
against them can never resolve.
Signed-off-by: Puranjay Mohan <puranjay@kernel.org>
Assisted-by: Claude:claude-opus-5
Signed-off-by: Song Liu <song@kernel.org>
---
.../tests/generic/fixtures/init_reference.c | 16 ++++++++++++++++
.../objtool/tests/generic/test-init-reference.sh | 16 ++++++++++++++++
2 files changed, 32 insertions(+)
create mode 100644 tools/objtool/tests/generic/fixtures/init_reference.c
create mode 100755 tools/objtool/tests/generic/test-init-reference.sh
diff --git a/tools/objtool/tests/generic/fixtures/init_reference.c b/tools/objtool/tests/generic/fixtures/init_reference.c
new file mode 100644
index 000000000000..51b2a57dde14
--- /dev/null
+++ b/tools/objtool/tests/generic/fixtures/init_reference.c
@@ -0,0 +1,16 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Patched function referencing data in an .init section. */
+
+static const char __modinfo[]
+ __attribute__((section(".modinfo"), used, aligned(1))) = "\0name=vmlinux";
+
+static int init_only __attribute__((section(".init.data"), used)) = 5;
+
+int target(int x)
+{
+#ifdef PATCHED
+ return x + init_only + 1;
+#else
+ return x + init_only;
+#endif
+}diff --git a/tools/objtool/tests/generic/test-init-reference.sh b/tools/objtool/tests/generic/test-init-reference.sh
new file mode 100755
index 000000000000..8bf4b98bf458
--- /dev/null
+++ b/tools/objtool/tests/generic/test-init-reference.sh
@@ -0,0 +1,16 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0
+#
+# Init code and data are freed after boot, so a klp relocation against them can
+# never resolve. Such a patch must be rejected.
+
+. "$(dirname "$0")/../lib.sh"
+
+setup
+build_pair init_reference.c
+run_diff 255
+
+diff_log | grep -q "can't patch or reference init code/data" ||
+ fail "expected rejection, got: $(diff_log | tail -1)"
+
+pass "reference to init data rejected"
--
2.53.0-Meta