mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Wilson Felipe Pereira <wfelipe@google.com>
To: Josh Poimboeuf <jpoimboe@kernel.org>,
	Peter Zijlstra <peterz@infradead.org>,
	 Miroslav Benes <mbenes@suse.cz>, Petr Mladek <pmladek@suse.com>,
	 Joe Lawrence <joe.lawrence@redhat.com>,
	Song Liu <song@kernel.org>
Cc: live-patching@vger.kernel.org, linux-kernel@vger.kernel.org,
	 Dylan Hatch <dylanbhatch@google.com>, Yi Zhu <chuyee@google.com>,
	 Wilson Felipe Pereira <wfelipe@google.com>
Subject: [PATCH 2/2] objtool/klp: Add test for global/local symbol name collisions
Date: Tue, 17 Nov 2026 00:08:32 +0000	[thread overview]
Message-ID: <20261117000835.1992893-3-wfelipe@google.com> (raw)
In-Reply-To: <20261117000835.1992893-1-wfelipe@google.com>

Add a test covering sympos disambiguation when a defined STB_GLOBAL symbol
shares its name with a static STB_LOCAL symbol in another translation
unit:
- A unique non-exported global continues to emit sympos 0.
- On the module path, a static in the first TU and a non-exported global
  in the second TU emit sympos 1 and 2 in .symtab order.
- On the vmlinux (.klp.symid) path, placing the global at a lower address
  than the static emits sympos 1 in linked address order, both for an
  unexported global and for an EXPORT_SYMBOL_FOR_MODULES() export.

Verified by reverting each hunk in klp-sympos.c, klp-symid.c, and
klp-diff.c individually and confirming the corresponding subtest fails.

Signed-off-by: Wilson Felipe Pereira <wfelipe@google.com>
---
 .../tests/generic/fixtures/sympos_vmlinux.c   |  6 ++
 .../tests/generic/test-sympos-global-dup.sh   | 99 +++++++++++++++++++
 2 files changed, 105 insertions(+)
 create mode 100755 tools/objtool/tests/generic/test-sympos-global-dup.sh

diff --git a/tools/objtool/tests/generic/fixtures/sympos_vmlinux.c b/tools/objtool/tests/generic/fixtures/sympos_vmlinux.c
index d5e70994c582..68d6fca414e4 100644
--- a/tools/objtool/tests/generic/fixtures/sympos_vmlinux.c
+++ b/tools/objtool/tests/generic/fixtures/sympos_vmlinux.c
@@ -26,8 +26,14 @@ static const char __modinfo[]
 	__attribute__((section(".modinfo"), used, aligned(1))) = "\0name=vmlinux";
 #endif
 
+#ifdef GLOBAL_DUP
+int dup_counter = 2;
+#elif defined(EXTERN_DUP)
+extern int dup_counter;
+#else
 /* volatile so it survives as an STT_OBJECT rather than being folded away */
 static volatile int dup_counter __attribute__((section(VARSEC))) = 1;
+#endif
 
 int FUNC_NAME(int x)
 {
diff --git a/tools/objtool/tests/generic/test-sympos-global-dup.sh b/tools/objtool/tests/generic/test-sympos-global-dup.sh
new file mode 100755
index 000000000000..b309b1f3f7f8
--- /dev/null
+++ b/tools/objtool/tests/generic/test-sympos-global-dup.sh
@@ -0,0 +1,99 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0
+#
+# A defined STB_GLOBAL symbol in one translation unit can share its name with a
+# static (STB_LOCAL) symbol in another translation unit (for example,
+# cgroup_storage_lookup in kernel/bpf/local_storage.c vs
+# kernel/bpf/bpf_cgrp_storage.c).
+#
+# At runtime, kallsyms_on_each_match_symbol() and
+# module_kallsyms_on_each_symbol() count both local and global symbols of that
+# name, so emitting sympos 0 for the global reference fails module load with
+# "unresolvable ambiguity for symbol". Both the module path (symtab order) and
+# the vmlinux path (.klp.symid address order) must disambiguate the global
+# symbol with its 1-based position while still emitting 0 when the global name
+# is unique.
+
+. "$(dirname "$0")/../lib.sh"
+
+# 1. Unique non-exported global: sympos must remain 0.
+setup
+build_one sympos_vmlinux.c def_g.o     -DFUNC_NAME=def_g -DGLOBAL_DUP
+build_one sympos_vmlinux.c use_orig.o  -DFUNC_NAME=caller -DEXTERN_DUP -DNO_MODINFO
+build_one sympos_vmlinux.c use_patch.o -DFUNC_NAME=caller -DEXTERN_DUP -DNO_MODINFO \
+	-DPATCHED
+
+partial_link "$workdir/orig.o"    "$workdir/def_g.o" "$workdir/use_orig.o" ||
+	probe_skip "partial link unavailable"
+partial_link "$workdir/patched.o" "$workdir/def_g.o" "$workdir/use_patch.o" ||
+	probe_skip "partial link unavailable"
+
+run_diff
+assert_klp_sympos dup_counter 0
+cleanup
+
+# 2. Module path: STB_LOCAL in first TU + non-exported STB_GLOBAL in second TU.
+#    In ELF .symtab, STB_LOCAL symbols precede STB_GLOBAL symbols, so the local
+#    is position 1 and the global is position 2.
+setup
+for p in "" "-DPATCHED"; do
+	# shellcheck disable=SC2086
+	build_one sympos_vmlinux.c "loc$p.o" -DFUNC_NAME=use_local $p
+	# shellcheck disable=SC2086
+	build_one sympos_vmlinux.c "glob$p.o" -DFUNC_NAME=use_global \
+		-DGLOBAL_DUP -DNO_MODINFO $p
+done
+
+partial_link "$workdir/orig.o"    "$workdir/loc.o"         "$workdir/glob.o" ||
+	probe_skip "partial link unavailable"
+partial_link "$workdir/patched.o" "$workdir/loc-DPATCHED.o" "$workdir/glob-DPATCHED.o" ||
+	probe_skip "partial link unavailable"
+
+[ "$(count_input_symbols orig.o dup_counter)" = 2 ] ||
+	fail "fixture did not produce two dup_counter symbols"
+
+run_diff
+assert_klp_sympos dup_counter 1
+assert_klp_sympos dup_counter 2
+out_symbols | grep -qE '\.klp\.sym\.[^.]+\.dup_counter,0([[:space:]]|$)' &&
+	fail "sympos 0 emitted for global symbol colliding with local symbol"
+cleanup
+
+# 3. vmlinux path (.klp.symid): with -fdata-sections the STB_GLOBAL lands in
+#    .data.dup_counter (lower address) while the STB_LOCAL lands in .data.zzz
+#    (higher address), opposite to ELF .symtab order where locals come first.
+#    Patching only the global caller must emit sympos 1 (address order).
+setup
+build_one sympos_vmlinux.c loc.o -DFUNC_NAME=use_local -DVARSEC='".data.zzz"'
+build_one sympos_vmlinux.c glob_orig.o -DFUNC_NAME=use_global -DGLOBAL_DUP \
+	-DNO_MODINFO
+build_one sympos_vmlinux.c glob_patch.o -DFUNC_NAME=use_global -DGLOBAL_DUP \
+	-DNO_MODINFO -DPATCHED
+
+make_vmlinux_pair "$workdir/loc.o" "$workdir/glob_orig.o" \
+	       -- "$workdir/loc.o" "$workdir/glob_patch.o"
+
+[ "$(count_input_symbols vmlinux.o dup_counter)" = 2 ] ||
+	fail "vmlinux fixture did not produce two dup_counter symbols"
+
+# Premise: in ELF .symtab the local comes first (higher address .data.zzz),
+# while in address order the global comes first (.data.aaa).
+first_addr="$(in_symbols vmlinux | awk '$8 == "dup_counter" { print $2; exit }')"
+low_addr="$(in_symbols vmlinux | awk '$8 == "dup_counter" { print $2 }' | sort | head -1)"
+[ "$first_addr" != "$low_addr" ] ||
+	probe_skip "linker did not place global before local in address order"
+
+run_diff
+assert_klp_sympos dup_counter 1
+out_symbols | grep -qE '\.klp\.sym\.[^.]+\.dup_counter,(0|2)([[:space:]]|$)' &&
+	fail "wrong sympos emitted for vmlinux global colliding with higher-addr local"
+
+# 4. Same vmlinux collision when the global uses EXPORT_SYMBOL_FOR_MODULES(),
+#    which takes the export branch in clone_reloc_klp().
+add_exports_ns vmlinux "module:other" dup_counter
+run_diff
+assert_klp_sympos dup_counter 1
+out_symbols | grep -qE '\.klp\.sym\.[^.]+\.dup_counter,(0|2)([[:space:]]|$)' &&
+	fail "wrong sympos emitted for module-restricted export colliding with local"
+
+pass "global symbol colliding with static local gets disambiguated sympos"
-- 
2.56.1.703.gf848ed807c-goog


      parent reply	other threads:[~2026-11-17  0:08 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-17  0:08 [PATCH 0/2] objtool/klp: Fix sympos for global/local symbol collisions Wilson Felipe Pereira
2026-11-17  0:08 ` [PATCH 1/2] objtool/klp: Disambiguate global symbols colliding with local symbols Wilson Felipe Pereira
2026-11-17  0:08 ` Wilson Felipe Pereira [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261117000835.1992893-3-wfelipe@google.com \
    --to=wfelipe@google.com \
    --cc=chuyee@google.com \
    --cc=dylanbhatch@google.com \
    --cc=joe.lawrence@redhat.com \
    --cc=jpoimboe@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=live-patching@vger.kernel.org \
    --cc=mbenes@suse.cz \
    --cc=peterz@infradead.org \
    --cc=pmladek@suse.com \
    --cc=song@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®