From: Song Liu <song@kernel.org>
To: live-patching@vger.kernel.org
Cc: jpoimboe@kernel.org, jikos@kernel.org, mbenes@suse.cz,
pmladek@suse.com, joe.lawrence@redhat.com, kernel-team@meta.com,
Song Liu <song@kernel.org>
Subject: [PATCH v3 6/8] objtool/klp: Match symbols based on demangled_name for global variables
Date: Wed, 25 Feb 2026 16:54:34 -0800 [thread overview]
Message-ID: <20260226005436.379303-7-song@kernel.org> (raw)
In-Reply-To: <20260226005436.379303-1-song@kernel.org>
correlate_symbols() will always try to match full name first. If there is
no match, try match only demangled_name.
In very rare cases, it is possible to have multiple foo.llvm.<hash> in
the same kernel. Whenever there is ambiguity like this, fail the klp diff.
Signed-off-by: Song Liu <song@kernel.org>
---
tools/objtool/elf.c | 13 +++++++
tools/objtool/include/objtool/elf.h | 3 ++
tools/objtool/klp-diff.c | 57 +++++++++++++++++++++++++++++
3 files changed, 73 insertions(+)
diff --git a/tools/objtool/elf.c b/tools/objtool/elf.c
index efb13ec0a89d..5ddbfa8f8701 100644
--- a/tools/objtool/elf.c
+++ b/tools/objtool/elf.c
@@ -323,6 +323,19 @@ struct symbol *find_global_symbol_by_name(const struct elf *elf, const char *nam
return NULL;
}
+void iterate_global_symbol_by_demangled_name(const struct elf *elf,
+ const char *demangled_name,
+ void (*process)(struct symbol *sym, void *data),
+ void *data)
+{
+ struct symbol *sym;
+
+ elf_hash_for_each_possible(symbol_name, sym, name_hash, str_hash(demangled_name)) {
+ if (!strcmp(sym->demangled_name, demangled_name) && !is_local_sym(sym))
+ process(sym, data);
+ }
+}
+
struct reloc *find_reloc_by_dest_range(const struct elf *elf, struct section *sec,
unsigned long offset, unsigned int len)
{
diff --git a/tools/objtool/include/objtool/elf.h b/tools/objtool/include/objtool/elf.h
index e12c516bd320..25573e5af76e 100644
--- a/tools/objtool/include/objtool/elf.h
+++ b/tools/objtool/include/objtool/elf.h
@@ -186,6 +186,9 @@ struct symbol *find_func_by_offset(struct section *sec, unsigned long offset);
struct symbol *find_symbol_by_offset(struct section *sec, unsigned long offset);
struct symbol *find_symbol_by_name(const struct elf *elf, const char *name);
struct symbol *find_global_symbol_by_name(const struct elf *elf, const char *name);
+void iterate_global_symbol_by_demangled_name(const struct elf *elf, const char *demangled_name,
+ void (*process)(struct symbol *sym, void *data),
+ void *data);
struct symbol *find_symbol_containing(const struct section *sec, unsigned long offset);
int find_symbol_hole_containing(const struct section *sec, unsigned long offset);
struct reloc *find_reloc_by_dest(const struct elf *elf, struct section *sec, unsigned long offset);
diff --git a/tools/objtool/klp-diff.c b/tools/objtool/klp-diff.c
index 57606bc3390a..92043da0ed0b 100644
--- a/tools/objtool/klp-diff.c
+++ b/tools/objtool/klp-diff.c
@@ -355,6 +355,46 @@ static bool dont_correlate(struct symbol *sym)
strstarts(sym->name, "__initcall__");
}
+struct process_demangled_name_data {
+ struct symbol *ret;
+ int count;
+};
+
+static void process_demangled_name(struct symbol *sym, void *d)
+{
+ struct process_demangled_name_data *data = d;
+
+ if (sym->twin)
+ return;
+
+ data->count++;
+ data->ret = sym;
+}
+
+/*
+ * When there is no full name match, try match demangled_name. This would
+ * match original foo.llvm.123 to patched foo.llvm.456.
+ *
+ * Note that, in very rare cases, it is possible to have multiple
+ * foo.llvm.<hash> in the same kernel. When this happens, report error and
+ * fail the diff.
+ */
+static int find_global_symbol_by_demangled_name(struct elf *elf, struct symbol *sym,
+ struct symbol **out_sym)
+{
+ struct process_demangled_name_data data = {};
+
+ iterate_global_symbol_by_demangled_name(elf, sym->demangled_name,
+ process_demangled_name,
+ &data);
+ if (data.count > 1) {
+ ERROR("Multiple (%d) correlation candidates for %s", data.count, sym->name);
+ return -1;
+ }
+ *out_sym = data.ret;
+ return 0;
+}
+
/*
* For each symbol in the original kernel, find its corresponding "twin" in the
* patched kernel.
@@ -453,6 +493,23 @@ static int correlate_symbols(struct elfs *e)
continue;
sym2 = find_global_symbol_by_name(e->patched, sym1->name);
+ if (sym2 && !sym2->twin) {
+ sym1->twin = sym2;
+ sym2->twin = sym1;
+ }
+ }
+
+ /*
+ * Correlate globals with demangled_name.
+ * A separate loop is needed because we want to finish all the
+ * full name correlations first.
+ */
+ for_each_sym(e->orig, sym1) {
+ if (sym1->bind == STB_LOCAL || sym1->twin)
+ continue;
+
+ if (find_global_symbol_by_demangled_name(e->patched, sym1, &sym2))
+ return -1;
if (sym2 && !sym2->twin) {
sym1->twin = sym2;
--
2.47.3
next prev parent reply other threads:[~2026-02-26 0:55 UTC|newest]
Thread overview: 26+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-02-26 0:54 [PATCH v3 0/8] objtool/klp: klp-build LTO support and tests Song Liu
2026-02-26 0:54 ` [PATCH v3 1/8] objtool/klp: Remove redundent strcmp in correlate_symbols Song Liu
2026-03-05 19:38 ` Josh Poimboeuf
2026-02-26 0:54 ` [PATCH v3 2/8] objtool/klp: Remove trailing '_' in demangle_name() Song Liu
2026-02-26 0:54 ` [PATCH v3 3/8] objtool/klp: Use sym->demangled_name for symbol_name hash Song Liu
2026-03-05 19:43 ` Josh Poimboeuf
2026-02-26 0:54 ` [PATCH v3 4/8] objtool/klp: Also demangle global objects Song Liu
2026-02-26 0:54 ` [PATCH v3 5/8] objtool/klp: Remove .llvm suffix in demangle_name() Song Liu
2026-02-26 0:54 ` Song Liu [this message]
2026-02-26 0:54 ` [PATCH v3 7/8] objtool/klp: Correlate locals to globals Song Liu
2026-03-05 19:51 ` Josh Poimboeuf
2026-03-05 23:10 ` Song Liu
2026-02-26 0:54 ` [PATCH v3 8/8] livepatch: Add tests for klp-build toolchain Song Liu
2026-02-27 10:04 ` Miroslav Benes
2026-02-27 17:26 ` Song Liu
2026-03-02 8:38 ` Miroslav Benes
2026-03-04 19:33 ` Joe Lawrence
2026-03-04 23:12 ` Song Liu
2026-03-05 1:39 ` Joe Lawrence
2026-03-05 5:03 ` Song Liu
2026-03-05 14:08 ` Petr Mladek
2026-03-05 15:18 ` Joe Lawrence
2026-03-05 15:20 ` Petr Mladek
2026-03-05 18:50 ` Josh Poimboeuf
2026-03-06 9:13 ` Petr Mladek
2026-03-05 19:33 ` Josh Poimboeuf
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=20260226005436.379303-7-song@kernel.org \
--to=song@kernel.org \
--cc=jikos@kernel.org \
--cc=joe.lawrence@redhat.com \
--cc=jpoimboe@kernel.org \
--cc=kernel-team@meta.com \
--cc=live-patching@vger.kernel.org \
--cc=mbenes@suse.cz \
--cc=pmladek@suse.com \
/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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.