[tip: objtool/core] objtool/klp: Add test for symids in discarded sections

From: tip-bot2 for Puranjay Mohan

Date: Mon Sep 21 2026 - 05:50:02 EST


The following commit has been merged into the objtool/core branch of tip:

Commit-ID: e018004ab8fa4c68155da976125ed8645c075383
Gitweb: https://git.kernel.org/tip/e018004ab8fa4c68155da976125ed8645c075383
Author: Puranjay Mohan <puranjay@xxxxxxxxxx>
AuthorDate: Wed, 16 Sep 2026 11:43:15 -07:00
Committer: Ingo Molnar <mingo@xxxxxxxxxx>
CommitterDate: Mon, 21 Sep 2026 11:05:13 +02:00

objtool/klp: Add test for symids in discarded sections

.klp.symid records duplicate-named locals so klp diff can work out their
sympos. Symbols in sections the vmlinux link throws away have to be left
out, or the table references symbols which no longer exist and the link
fails:

`__exitcall_foo' referenced in section `.klp.symid' of vmlinux.o:
defined in discarded section `.exitcall.exit' of vmlinux.o

Two translation units are compiled from one fixture and partially linked so
the result has duplicate locals, which symid_needed() requires. One
duplicate is in a live section and one in .exitcall.exit.

Checking the live duplicate as well keeps the test honest: it would
otherwise pass just as happily if symid generation stopped working
entirely.

Signed-off-by: Puranjay Mohan <puranjay@xxxxxxxxxx>
Signed-off-by: Song Liu <song@xxxxxxxxxx>
Signed-off-by: Josh Poimboeuf <jpoimboe@xxxxxxxxxx>
Signed-off-by: Ingo Molnar <mingo@xxxxxxxxxx>
Assisted-by: Claude:claude-opus-5
Link: https://patch.msgid.link/20260916184351.2720310-23-song@xxxxxxxxxx
---
tools/objtool/tests/generic/fixtures/symid_discarded.c | 25 +++++-
tools/objtool/tests/generic/test-symid-discarded.sh | 44 +++++++++-
2 files changed, 69 insertions(+)
create mode 100644 tools/objtool/tests/generic/fixtures/symid_discarded.c
create mode 100755 tools/objtool/tests/generic/test-symid-discarded.sh

diff --git a/tools/objtool/tests/generic/fixtures/symid_discarded.c b/tools/objtool/tests/generic/fixtures/symid_discarded.c
new file mode 100644
index 0000000..573cc2d
--- /dev/null
+++ b/tools/objtool/tests/generic/fixtures/symid_discarded.c
@@ -0,0 +1,25 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Compiled twice and partially linked so the result has duplicate locals,
+ * which is what symid_needed() requires. dup_normal is in a live section,
+ * dup_discarded in one the vmlinux link throws away. DISCARDED_SEC selects
+ * which discarded section, since there is more than one and each was its own
+ * bug.
+ */
+
+#ifndef DISCARDED_SEC
+#define DISCARDED_SEC ".exitcall.exit"
+#endif
+
+static const char __modinfo[]
+ __attribute__((section(".modinfo"), used, aligned(1))) = "\0name=vmlinux";
+
+static int dup_normal = 1;
+
+static void *dup_discarded
+ __attribute__((section(DISCARDED_SEC), used)) = &dup_normal;
+
+int FUNC_NAME(void)
+{
+ return dup_normal + (dup_discarded != (void *)0);
+}
diff --git a/tools/objtool/tests/generic/test-symid-discarded.sh b/tools/objtool/tests/generic/test-symid-discarded.sh
new file mode 100755
index 0000000..388a249
--- /dev/null
+++ b/tools/objtool/tests/generic/test-symid-discarded.sh
@@ -0,0 +1,44 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0
+#
+# .klp.symid must not reference symbols in sections the vmlinux link discards.
+# Each such section has been its own bug, found only when someone built a
+# config where a duplicate happened to land there, so cover the whole list
+# rather than whichever one was reported last.
+
+. "$(dirname "$0")/../lib.sh"
+
+setup
+
+# Allocated sections which vmlinux.lds.h discards unconditionally. A symid
+# referencing one of these fails the vmlinux link outright:
+#
+# `__exitcall_hid_exit' referenced in section `.klp.symid' of vmlinux.o:
+# defined in discarded section `.exitcall.exit' of vmlinux.o
+for sec in .exitcall.exit .no_trim_symbol; do
+ build_one symid_discarded.c a.o \
+ -DFUNC_NAME=use_a -DDISCARDED_SEC="\"$sec\""
+ build_one symid_discarded.c b.o \
+ -DFUNC_NAME=use_b -DDISCARDED_SEC="\"$sec\""
+
+ # --klp-symids only runs on a file named vmlinux.o
+ rm -f "$workdir/vmlinux.o"
+ partial_link "$workdir/vmlinux.o" "$workdir/a.o" "$workdir/b.o" ||
+ probe_skip "partial link unavailable"
+
+ "$OBJTOOL" --klp-symids --link "$workdir/vmlinux.o" ||
+ fail "objtool --klp-symids failed"
+
+ symids="$(in_relocs vmlinux.o |
+ awk '/rela.klp.symid/,/^$/')"
+
+ # Without this the test would also pass if symid generation stopped
+ # entirely.
+ echo "$symids" | grep -q 'dup_normal' ||
+ fail "$sec: no symid for the duplicate in a live section"
+
+ echo "$symids" | grep -q 'dup_discarded' &&
+ fail "symid emitted for a symbol in discarded section $sec"
+done
+
+pass "no symids for symbols in discarded sections"