[PATCH v2 1/2] objtool/klp: Disambiguate global symbols colliding with local symbols
From: Wilson Felipe Pereira
Date: Mon Nov 23 2026 - 18:04:25 EST
klp_find_sympos() returns 0 for any non-STB_LOCAL symbol, symid_needed()
skips non-local symbols when emitting .klp.symid entries for vmlinux.o,
and clone_reloc_klp() hardcodes sympos = 0 when a relocation targets a
restricted or module export.
However, a 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, both
kallsyms_on_each_match_symbol() and module_kallsyms_on_each_symbol() count
local and global symbols together, so emitting sympos 0 for the global
symbol fails module load in klp_find_object_symbol() with 'unresolvable
ambiguity for symbol'.
Allow defined non-local symbols to receive .klp.symid entries when
duplicate names exist in vmlinux.o, compute sympos for defined non-local
symbols in klp_find_sympos() (returning 0 when unique and skipping
undefined and non-alloc symbols), and resolve sympos via klp_find_sympos()
in clone_reloc_klp() whenever patched_sym->twin exists.
Signed-off-by: Wilson Felipe Pereira <wfelipe@xxxxxxxxxx>
---
v1 -> v2:
- Simplify clone_reloc_klp() by unifying klp_find_sympos() below the
export/non-export branch (Josh Poimboeuf).
- Rebase onto v7.4-rc4 (b6a9fa34ce41).
v1: https://lore.kernel.org/all/20261117000835.1992893-2-wfelipe@xxxxxxxxxx/
tools/objtool/klp-diff.c | 5 ++++-
tools/objtool/klp-symid.c | 2 +-
tools/objtool/klp-sympos.c | 9 ++++++---
3 files changed, 11 insertions(+), 5 deletions(-)
diff --git a/tools/objtool/klp-diff.c b/tools/objtool/klp-diff.c
index 34da55e45609..fad4cfea9c9b 100644
--- a/tools/objtool/klp-diff.c
+++ b/tools/objtool/klp-diff.c
@@ -1406,13 +1406,16 @@ static int clone_reloc_klp(struct elfs *e, struct reloc *patched_reloc,
if (export) {
sym_modname = export->mod;
sym_orig_name = export->sym;
- sympos = 0;
} else {
sym_modname = find_modname(e);
if (!sym_modname)
return -1;
sym_orig_name = patched_sym->twin->name;
+ }
+
+ sympos = 0;
+ if (patched_sym->twin) {
sympos = klp_find_sympos(e->orig, patched_sym->twin);
if (sympos == ULONG_MAX)
return -1;
diff --git a/tools/objtool/klp-symid.c b/tools/objtool/klp-symid.c
index b19f76dff13e..d01c13b8ffa8 100644
--- a/tools/objtool/klp-symid.c
+++ b/tools/objtool/klp-symid.c
@@ -52,7 +52,7 @@ static bool symid_needed(struct elf *elf, struct symbol *sym)
{
struct symbol *s;
- if (!is_local_sym(sym) || is_undef_sym(sym))
+ if (is_undef_sym(sym))
return false;
if (!is_func_sym(sym) && !is_object_sym(sym))
diff --git a/tools/objtool/klp-sympos.c b/tools/objtool/klp-sympos.c
index dfca9dd74681..087892ef3593 100644
--- a/tools/objtool/klp-sympos.c
+++ b/tools/objtool/klp-sympos.c
@@ -381,14 +381,14 @@ unsigned long klp_find_sympos(struct elf *elf, struct symbol *sym)
bool has_dup = false;
struct symbol *s;
+ if (is_undef_sym(sym))
+ return 0;
+
if (is_init_sym(sym)) {
ERROR("%s: can't patch or reference init code/data", sym->name);
return ULONG_MAX;
}
- if (sym->bind != STB_LOCAL)
- return 0;
-
/*
* vmlinux: the final link reorders symbols relative to vmlinux.o,
* so the position needs to be derived from the linked orig vmlinux via
@@ -403,6 +403,9 @@ unsigned long klp_find_sympos(struct elf *elf, struct symbol *sym)
* module_kallsyms_on_each_symbol().
*/
for_each_sym(elf, s) {
+ if (is_sec_sym(s) || is_file_sym(s) || is_undef_sym(s) ||
+ !(s->sec->sh.sh_flags & SHF_ALLOC))
+ continue;
if (!strcmp(s->name, sym->name)) {
nr_matches++;
if (s == sym)
--
2.56.1.750.gf70dc682cf-goog