objtool/klp: Fix sympos for global/local symbol collisions
Subsystems (2)
v2 cover objtool/klp: Fix sympos for global/local symbol collisions
When a defined STB_GLOBAL symbol in one translation unit shares 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), runtime kallsyms lookup counts both symbols together. Emitting sympos 0 for the global symbol causes module load to fail in klp_find_object_symbol() with "unresolvable ambiguity for symbol".
Patch 1 disambiguates defined non-local symbols across .klp.symid generation, klp_find_sympos(), and clone_reloc_klp() (while preserving sympos 0 when unique). Patch 2 adds tests covering both module (.symtab order) and vmlinux (.klp.symid address order) paths, including EXPORT_SYMBOL_FOR_MODULES() exports.
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/[email protected]/
Wilson Felipe Pereira (2):
objtool/klp: Disambiguate global symbols colliding with local symbols objtool/klp: Add test for global/local symbol name collisions
tools/objtool/klp-diff.c | 5 +- tools/objtool/klp-symid.c | 2 +- tools/objtool/klp-sympos.c | 9 +- .../tests/generic/fixtures/sympos_vmlinux.c | 6 ++ .../tests/generic/test-sympos-global-dup.sh | 99 +++++++++++++++++++ 5 files changed, 116 insertions(+), 5 deletions(-) create mode 100755 tools/objtool/tests/generic/test-sympos-global-dup.sh
base-commit: b6a9fa34ce4187cb69d3c588a4bbffc31ce5db8f
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
PATCH v2 1/2 objtool/klp: Disambiguate global symbols colliding with local symbols
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.
---
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/[email protected]/
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(-)
tools/objtool/klp-diff.c +4 -1
@@ -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 ;
tools/objtool/klp-symid.c +1 -1
@@ -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))
tools/objtool/klp-sympos.c +6 -3
@@ -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)
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
PATCH v2 2/2 objtool/klp: Add test for global/local symbol name collisions
In reply to Wilson Felipe PereiraAdd 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.
---
v1 -> v2:
- Rebase onto v7.4-rc4 (b6a9fa34ce41); no functional changes.
v1: https://lore.kernel.org/all/[email protected]/
.../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
tools/objtool/tests/generic/fixtures/sympos_vmlinux.c +6 -0
@@ -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) {
tools/objtool/tests/generic/test-sympos-global-dup.sh new file +99 -0
99 lines not on this page · raw message
Signature · 1 line
2.56.1.750.gf70dc682cf-goog