[PATCH 1/2] objtool/klp: Disambiguate global symbols colliding with local symbols

From: Wilson Felipe Pereira

Date: Mon Nov 16 2026 - 19:08:49 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() when the exported target is defined in the patched
object.

Signed-off-by: Wilson Felipe Pereira <wfelipe@xxxxxxxxxx>
---
tools/objtool/klp-diff.c | 11 +++++++++++
tools/objtool/klp-symid.c | 2 +-
tools/objtool/klp-sympos.c | 9 ++++++---
3 files changed, 18 insertions(+), 4 deletions(-)

diff --git a/tools/objtool/klp-diff.c b/tools/objtool/klp-diff.c
index 34da55e45609..37ad315d00af 100644
--- a/tools/objtool/klp-diff.c
+++ b/tools/objtool/klp-diff.c
@@ -1404,9 +1404,20 @@ static int clone_reloc_klp(struct elfs *e, struct reloc *patched_reloc,
*/

if (export) {
+ const char *modname = find_modname(e);
+
+ if (!modname)
+ return -1;
+
sym_modname = export->mod;
sym_orig_name = export->sym;
sympos = 0;
+ if (!strcmp(export->mod, modname) &&
+ patched_sym->twin && !is_undef_sym(patched_sym->twin)) {
+ sympos = klp_find_sympos(e->orig, patched_sym->twin);
+ if (sympos == ULONG_MAX)
+ return -1;
+ }
} else {
sym_modname = find_modname(e);
if (!sym_modname)
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.703.gf848ed807c-goog