* [Devel] [PATCH hci-8.2 v2 01/28] ms/objtool/klp: Fix detection of corrupt static branch/call entries
2026-11-26 5:06 [Devel] [PATCH hci-8.2 v2 00/28] ms/objtool: fix silently wrong livepatches by klp-build Eva Kurchatova
@ 2026-11-26 5:07 ` Eva Kurchatova
2026-11-26 5:07 ` [Devel] [PATCH hci-8.2 v2 02/28] ms/objtool/klp: Disable unsupported pr_debug() usage Eva Kurchatova
` (28 subsequent siblings)
29 siblings, 0 replies; 31+ messages in thread
From: Eva Kurchatova @ 2026-11-26 5:07 UTC (permalink / raw)
To: khorenko, vladimir.riabchun; +Cc: devel
From: Josh Poimboeuf <jpoimboe@kernel.org>
Patching a function which references a static key living in a kernel
module is unsupported due to ordering issues inherent to late module
patching:
1) Load a livepatch module which has a __jump_table entry which needs
a klp reloc to reference static key K which lives in module M.
2) The __jump_table klp reloc does *not* get resolved because module M
is not yet loaded.
3) jump_label_add_module() corrupts memory (or causes a panic) when
dereferencing the uninitialized pointer to key K.
validate_special_section_klp_reloc() intends to prevent that from ever
happening by catching it at build time. However, it incorrectly assumes
the special section entry's reloc symbol references have already been
converted from section symbols to object symbols, causing the validation
to miss corruption in extracted static branch/call table entries.
Make sure the references have been properly converted before doing the
validation.
Fixes: dd590d4d57eb ("objtool/klp: Introduce klp diff subcommand for diffing object files")
Reported-by: Song Liu <song@kernel.org>
Reviewed-and-tested-by: Song Liu <song@kernel.org>
Link: https://patch.msgid.link/124ad747b751df0df1725eff89de8332e3fb26d6.1770759954.git.jpoimboe@kernel.org
Signed-off-by: Josh Poimboeuf <jpoimboe@kernel.org>
(cherry picked from commit f9fb44b0ecefc1f218db56661ed66d4e8d67317d)
https://virtuozzo.atlassian.net/browse/VSTOR-128510
Feature: klp-build support
Signed-off-by: Eva Kurchatova <eva.kurchatova@virtuozzo.com>
Reviewed-by: Sashiko <no-reply@sashiko.virtuozzo.com>
---
tools/objtool/klp-diff.c | 3 +++
1 file changed, 3 insertions(+)
diff --git a/tools/objtool/klp-diff.c b/tools/objtool/klp-diff.c
index 4d1f9e9977eb..2db34b86e655 100644
--- a/tools/objtool/klp-diff.c
+++ b/tools/objtool/klp-diff.c
@@ -1335,6 +1335,9 @@ static int validate_special_section_klp_reloc(struct elfs *e, struct symbol *sym
const char *sym_modname;
struct export *export;
+ if (convert_reloc_sym(e->patched, reloc))
+ continue;
+
/* Static branch/call keys are always STT_OBJECT */
if (reloc->sym->type != STT_OBJECT) {
--
2.56.1
_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel
^ permalink raw reply [flat|nested] 31+ messages in thread* [Devel] [PATCH hci-8.2 v2 02/28] ms/objtool/klp: Disable unsupported pr_debug() usage
2026-11-26 5:06 [Devel] [PATCH hci-8.2 v2 00/28] ms/objtool: fix silently wrong livepatches by klp-build Eva Kurchatova
2026-11-26 5:07 ` [Devel] [PATCH hci-8.2 v2 01/28] ms/objtool/klp: Fix detection of corrupt static branch/call entries Eva Kurchatova
@ 2026-11-26 5:07 ` Eva Kurchatova
2026-11-26 5:07 ` [Devel] [PATCH hci-8.2 v2 03/28] ms/objtool: Fix data alignment in elf_add_data() Eva Kurchatova
` (27 subsequent siblings)
29 siblings, 0 replies; 31+ messages in thread
From: Eva Kurchatova @ 2026-11-26 5:07 UTC (permalink / raw)
To: khorenko, vladimir.riabchun; +Cc: devel
From: Josh Poimboeuf <jpoimboe@kernel.org>
Instead of erroring out on unsupported pr_debug() (e.g., when patching a
module), issue a warning and make it inert, similar to how unsupported
tracepoints are currently handled.
Reviewed-and-tested-by: Song Liu <song@kernel.org>
Link: https://patch.msgid.link/3a7db3a5b7d4abf9b2534803a74e2e7231322738.1770759954.git.jpoimboe@kernel.org
Signed-off-by: Josh Poimboeuf <jpoimboe@kernel.org>
(cherry picked from commit e476bb277cf91b7ac3ea803ec78a4f0791bddec3)
https://virtuozzo.atlassian.net/browse/VSTOR-128510
Feature: klp-build support
Signed-off-by: Eva Kurchatova <eva.kurchatova@virtuozzo.com>
Reviewed-by: Sashiko <no-reply@sashiko.virtuozzo.com>
---
tools/objtool/klp-diff.c | 23 +++++++++++++++--------
1 file changed, 15 insertions(+), 8 deletions(-)
diff --git a/tools/objtool/klp-diff.c b/tools/objtool/klp-diff.c
index 2db34b86e655..13319ee9d56e 100644
--- a/tools/objtool/klp-diff.c
+++ b/tools/objtool/klp-diff.c
@@ -1305,18 +1305,18 @@ static bool should_keep_special_sym(struct elf *elf, struct symbol *sym)
* be applied after static branch/call init, resulting in code corruption.
*
* Validate a special section entry to avoid that. Note that an inert
- * tracepoint is harmless enough, in that case just skip the entry and print a
- * warning. Otherwise, return an error.
+ * tracepoint or pr_debug() is harmless enough, in that case just skip the
+ * entry and print a warning. Otherwise, return an error.
*
- * This is only a temporary limitation which will be fixed when livepatch adds
- * support for submodules: fully self-contained modules which are embedded in
- * the top-level livepatch module's data and which can be loaded on demand when
- * their corresponding to-be-patched module gets loaded. Then klp relocs can
- * be retired.
+ * TODO: This is only a temporary limitation which will be fixed when livepatch
+ * adds support for submodules: fully self-contained modules which are embedded
+ * in the top-level livepatch module's data and which can be loaded on demand
+ * when their corresponding to-be-patched module gets loaded. Then klp relocs
+ * can be retired.
*
* Return:
* -1: error: validation failed
- * 1: warning: tracepoint skipped
+ * 1: warning: disabled tracepoint or pr_debug()
* 0: success
*/
static int validate_special_section_klp_reloc(struct elfs *e, struct symbol *sym)
@@ -1374,6 +1374,13 @@ static int validate_special_section_klp_reloc(struct elfs *e, struct symbol *sym
continue;
}
+ if (strstr(reloc->sym->name, "__UNIQUE_ID_ddebug_")) {
+ WARN("%s: disabling unsupported pr_debug()",
+ code_sym->name);
+ ret = 1;
+ continue;
+ }
+
ERROR("%s+0x%lx: unsupported static branch key %s. Use static_key_enabled() instead",
code_sym->name, code_offset, reloc->sym->name);
return -1;
--
2.56.1
_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel
^ permalink raw reply [flat|nested] 31+ messages in thread* [Devel] [PATCH hci-8.2 v2 03/28] ms/objtool: Fix data alignment in elf_add_data()
2026-11-26 5:06 [Devel] [PATCH hci-8.2 v2 00/28] ms/objtool: fix silently wrong livepatches by klp-build Eva Kurchatova
2026-11-26 5:07 ` [Devel] [PATCH hci-8.2 v2 01/28] ms/objtool/klp: Fix detection of corrupt static branch/call entries Eva Kurchatova
2026-11-26 5:07 ` [Devel] [PATCH hci-8.2 v2 02/28] ms/objtool/klp: Disable unsupported pr_debug() usage Eva Kurchatova
@ 2026-11-26 5:07 ` Eva Kurchatova
2026-11-26 5:07 ` [Devel] [PATCH hci-8.2 v2 04/28] ms/objtool/klp: fix data alignment in __clone_symbol() Eva Kurchatova
` (26 subsequent siblings)
29 siblings, 0 replies; 31+ messages in thread
From: Eva Kurchatova @ 2026-11-26 5:07 UTC (permalink / raw)
To: khorenko, vladimir.riabchun; +Cc: devel
From: Josh Poimboeuf <jpoimboe@kernel.org>
Any data added to a section needs to be aligned in accordance with the
section's sh_addralign value. Particularly strings added to a .str1.8
section. Otherwise you may get some funky strings.
Fixes: dd590d4d57eb ("objtool/klp: Introduce klp diff subcommand for diffing object files")
Link: https://patch.msgid.link/d962fc0ca24fa0825cca8dad71932dccdd9312a9.1772681234.git.jpoimboe@kernel.org
Signed-off-by: Josh Poimboeuf <jpoimboe@kernel.org>
(cherry picked from commit 356e4b2f5b80f757965f3f4d0219c81fca91b6f2)
https://virtuozzo.atlassian.net/browse/VSTOR-128510
Feature: klp-build support
Signed-off-by: Eva Kurchatova <eva.kurchatova@virtuozzo.com>
Reviewed-by: Sashiko <no-reply@sashiko.virtuozzo.com>
---
tools/objtool/elf.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/tools/objtool/elf.c b/tools/objtool/elf.c
index bc1ca8e439c9..da20cec49374 100644
--- a/tools/objtool/elf.c
+++ b/tools/objtool/elf.c
@@ -1351,7 +1351,7 @@ void *elf_add_data(struct elf *elf, struct section *sec, const void *data, size_
memcpy(sec->data->d_buf, data, size);
sec->data->d_size = size;
- sec->data->d_align = 1;
+ sec->data->d_align = sec->sh.sh_addralign;
offset = ALIGN_UP(sec->sh.sh_size, sec->sh.sh_addralign);
sec->sh.sh_size = offset + size;
--
2.56.1
_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel
^ permalink raw reply [flat|nested] 31+ messages in thread* [Devel] [PATCH hci-8.2 v2 04/28] ms/objtool/klp: fix data alignment in __clone_symbol()
2026-11-26 5:06 [Devel] [PATCH hci-8.2 v2 00/28] ms/objtool: fix silently wrong livepatches by klp-build Eva Kurchatova
` (2 preceding siblings ...)
2026-11-26 5:07 ` [Devel] [PATCH hci-8.2 v2 03/28] ms/objtool: Fix data alignment in elf_add_data() Eva Kurchatova
@ 2026-11-26 5:07 ` Eva Kurchatova
2026-11-26 5:07 ` [Devel] [PATCH hci-8.2 v2 05/28] ms/objtool/klp: Fix .data..once static local non-correlation Eva Kurchatova
` (25 subsequent siblings)
29 siblings, 0 replies; 31+ messages in thread
From: Eva Kurchatova @ 2026-11-26 5:07 UTC (permalink / raw)
To: khorenko, vladimir.riabchun; +Cc: devel
From: Joe Lawrence <joe.lawrence@redhat.com>
Commit 356e4b2f5b80 ("objtool: Fix data alignment in elf_add_data()")
corrected the alignment of data within a section (honoring the section's
sh_addralign). Apply the same alignment when klp-diff mode clones a
symbol, adjusting the new symbol's offset for the output section's
sh_addralign.
Fixes: dd590d4d57eb ("objtool/klp: Introduce klp diff subcommand for diffing object files")
Signed-off-by: Joe Lawrence <joe.lawrence@redhat.com>
Link: https://patch.msgid.link/20260310203751.1479229-2-joe.lawrence@redhat.com
Signed-off-by: Josh Poimboeuf <jpoimboe@kernel.org>
(cherry picked from commit 2f2600decb3004938762a3f2d0eba3ea9e01045b)
https://virtuozzo.atlassian.net/browse/VSTOR-128510
Feature: klp-build support
Signed-off-by: Eva Kurchatova <eva.kurchatova@virtuozzo.com>
Reviewed-by: Sashiko <no-reply@sashiko.virtuozzo.com>
---
tools/objtool/klp-diff.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/tools/objtool/klp-diff.c b/tools/objtool/klp-diff.c
index 13319ee9d56e..e0c64690c1e0 100644
--- a/tools/objtool/klp-diff.c
+++ b/tools/objtool/klp-diff.c
@@ -14,6 +14,7 @@
#include <objtool/util.h>
#include <arch/special.h>
+#include <linux/align.h>
#include <linux/objtool_types.h>
#include <linux/livepatch_external.h>
#include <linux/stringify.h>
@@ -531,7 +532,7 @@ static struct symbol *__clone_symbol(struct elf *elf, struct symbol *patched_sym
}
if (!is_sec_sym(patched_sym))
- offset = sec_size(out_sec);
+ offset = ALIGN(sec_size(out_sec), out_sec->sh.sh_addralign);
if (patched_sym->len || is_sec_sym(patched_sym)) {
void *data = NULL;
--
2.56.1
_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel
^ permalink raw reply [flat|nested] 31+ messages in thread* [Devel] [PATCH hci-8.2 v2 05/28] ms/objtool/klp: Fix .data..once static local non-correlation
2026-11-26 5:06 [Devel] [PATCH hci-8.2 v2 00/28] ms/objtool: fix silently wrong livepatches by klp-build Eva Kurchatova
` (3 preceding siblings ...)
2026-11-26 5:07 ` [Devel] [PATCH hci-8.2 v2 04/28] ms/objtool/klp: fix data alignment in __clone_symbol() Eva Kurchatova
@ 2026-11-26 5:07 ` Eva Kurchatova
2026-11-26 5:07 ` [Devel] [PATCH hci-8.2 v2 06/28] ms/objtool/klp: Fix create_fake_symbols() skipping entsize-based sections Eva Kurchatova
` (24 subsequent siblings)
29 siblings, 0 replies; 31+ messages in thread
From: Eva Kurchatova @ 2026-11-26 5:07 UTC (permalink / raw)
To: khorenko, vladimir.riabchun; +Cc: devel
From: Josh Poimboeuf <jpoimboe@kernel.org>
While there was once a section named .data.once, it has since been
renamed to .data..once with commit dbefa1f31a91 ("Rename .data.once to
.data..once to fix resetting WARN*_ONCE"). Fix it.
Fixes: dd590d4d57eb ("objtool/klp: Introduce klp diff subcommand for diffing object files")
Acked-by: Song Liu <song@kernel.org>
Reviewed-by: Miroslav Benes <mbenes@suse.cz>
Signed-off-by: Josh Poimboeuf <jpoimboe@kernel.org>
(cherry picked from commit ff529864e738f447ff4c019956319930fc274a23)
https://virtuozzo.atlassian.net/browse/VSTOR-128510
Feature: klp-build support
Signed-off-by: Eva Kurchatova <eva.kurchatova@virtuozzo.com>
Reviewed-by: Sashiko <no-reply@sashiko.virtuozzo.com>
---
tools/objtool/klp-diff.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/tools/objtool/klp-diff.c b/tools/objtool/klp-diff.c
index e0c64690c1e0..0f9ce6541e93 100644
--- a/tools/objtool/klp-diff.c
+++ b/tools/objtool/klp-diff.c
@@ -256,7 +256,8 @@ static bool is_uncorrelated_static_local(struct symbol *sym)
if (!is_object_sym(sym) || !is_local_sym(sym))
return false;
- if (!strcmp(sym->sec->name, ".data.once"))
+ /* WARN_ONCE, etc */
+ if (!strcmp(sym->sec->name, ".data..once"))
return true;
for (int i = 0; i < ARRAY_SIZE(vars); i++) {
--
2.56.1
_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel
^ permalink raw reply [flat|nested] 31+ messages in thread* [Devel] [PATCH hci-8.2 v2 06/28] ms/objtool/klp: Fix create_fake_symbols() skipping entsize-based sections
2026-11-26 5:06 [Devel] [PATCH hci-8.2 v2 00/28] ms/objtool: fix silently wrong livepatches by klp-build Eva Kurchatova
` (4 preceding siblings ...)
2026-11-26 5:07 ` [Devel] [PATCH hci-8.2 v2 05/28] ms/objtool/klp: Fix .data..once static local non-correlation Eva Kurchatova
@ 2026-11-26 5:07 ` Eva Kurchatova
2026-11-26 5:07 ` [Devel] [PATCH hci-8.2 v2 07/28] ms/objtool/klp: Fix handling of zero-length .altinstr_replacement sections Eva Kurchatova
` (23 subsequent siblings)
29 siblings, 0 replies; 31+ messages in thread
From: Eva Kurchatova @ 2026-11-26 5:07 UTC (permalink / raw)
To: khorenko, vladimir.riabchun; +Cc: devel
From: Joe Lawrence <joe.lawrence@redhat.com>
create_fake_symbols() has two phases: creating symbols from
ANNOTATE_DATA_SPECIAL entries, and a fallback that uses sh_entsize for
special sections like .static_call_sites.
When .discard.annotate_data is absent, the function returns early,
skipping the entsize fallback and silently allowing unsupported
module-local static call keys through.
Fix it by jumping to the entsize phase instead of returning early.
Fixes: dd590d4d57eb ("objtool/klp: Introduce klp diff subcommand for diffing object files")
Assisted-by: Claude:claude-4-opus
Signed-off-by: Joe Lawrence <joe.lawrence@redhat.com>
Acked-by: Song Liu <song@kernel.org>
Reviewed-by: Miroslav Benes <mbenes@suse.cz>
Signed-off-by: Josh Poimboeuf <jpoimboe@kernel.org>
(cherry picked from commit 3de711fba73ad93b8b3fbe09cf681cefed5d573d)
https://virtuozzo.atlassian.net/browse/VSTOR-128510
Feature: klp-build support
Signed-off-by: Eva Kurchatova <eva.kurchatova@virtuozzo.com>
Reviewed-by: Sashiko <no-reply@sashiko.virtuozzo.com>
---
tools/objtool/klp-diff.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/tools/objtool/klp-diff.c b/tools/objtool/klp-diff.c
index 0f9ce6541e93..9dd2224f45aa 100644
--- a/tools/objtool/klp-diff.c
+++ b/tools/objtool/klp-diff.c
@@ -1219,7 +1219,7 @@ static int create_fake_symbols(struct elf *elf)
sec = find_section_by_name(elf, ".discard.annotate_data");
if (!sec || !sec->rsec)
- return 0;
+ goto entsize;
for_each_reloc(sec->rsec, reloc) {
unsigned long offset, size;
@@ -1251,7 +1251,7 @@ static int create_fake_symbols(struct elf *elf)
/*
* 2) Make symbols for sh_entsize, and simple arrays of pointers:
*/
-
+entsize:
for_each_sec(elf, sec) {
unsigned int entry_size;
unsigned long offset;
--
2.56.1
_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel
^ permalink raw reply [flat|nested] 31+ messages in thread* [Devel] [PATCH hci-8.2 v2 07/28] ms/objtool/klp: Fix handling of zero-length .altinstr_replacement sections
2026-11-26 5:06 [Devel] [PATCH hci-8.2 v2 00/28] ms/objtool: fix silently wrong livepatches by klp-build Eva Kurchatova
` (5 preceding siblings ...)
2026-11-26 5:07 ` [Devel] [PATCH hci-8.2 v2 06/28] ms/objtool/klp: Fix create_fake_symbols() skipping entsize-based sections Eva Kurchatova
@ 2026-11-26 5:07 ` Eva Kurchatova
2026-11-26 5:07 ` [Devel] [PATCH hci-8.2 v2 08/28] ms/objtool/klp: Fix cloning of zero-length section symbols Eva Kurchatova
` (22 subsequent siblings)
29 siblings, 0 replies; 31+ messages in thread
From: Eva Kurchatova @ 2026-11-26 5:07 UTC (permalink / raw)
To: khorenko, vladimir.riabchun; +Cc: devel
From: Josh Poimboeuf <jpoimboe@kernel.org>
When a section is empty (e.g. only zero-length alternative
replacements), there are no symbols to convert a section symbol
reference to. Skip the reloc instead of erroring out.
Fixes: dd590d4d57eb ("objtool/klp: Introduce klp diff subcommand for diffing object files")
Acked-by: Song Liu <song@kernel.org>
Reviewed-by: Miroslav Benes <mbenes@suse.cz>
Signed-off-by: Josh Poimboeuf <jpoimboe@kernel.org>
(cherry picked from commit c4c02d4450b5b7e2fbde578252f71a5697180112)
https://virtuozzo.atlassian.net/browse/VSTOR-128510
Feature: klp-build support
Signed-off-by: Eva Kurchatova <eva.kurchatova@virtuozzo.com>
Reviewed-by: Sashiko <no-reply@sashiko.virtuozzo.com>
---
tools/objtool/klp-diff.c | 13 ++++++++++++-
1 file changed, 12 insertions(+), 1 deletion(-)
diff --git a/tools/objtool/klp-diff.c b/tools/objtool/klp-diff.c
index 9dd2224f45aa..5e39839759bb 100644
--- a/tools/objtool/klp-diff.c
+++ b/tools/objtool/klp-diff.c
@@ -864,6 +864,13 @@ static int convert_reloc_secsym_to_sym(struct elf *elf, struct reloc *reloc)
/* No dedicated section; find the symbol manually */
sym = find_symbol_containing(sec, arch_adjusted_addend(reloc));
if (!sym) {
+ /*
+ * This is presumably an .altinstr_replacement section which is
+ * empty due to it only having zero-length replacement(s).
+ */
+ if (!sec_size(sec))
+ return 1;
+
/*
* This can happen for special section references to weak code
* whose symbol has been stripped by the linker.
@@ -1124,6 +1131,7 @@ static int clone_sym_relocs(struct elfs *e, struct symbol *patched_sym)
for_each_reloc(patched_rsec, patched_reloc) {
unsigned long offset;
+ int ret;
if (reloc_offset(patched_reloc) < start ||
reloc_offset(patched_reloc) >= end)
@@ -1137,12 +1145,15 @@ static int clone_sym_relocs(struct elfs *e, struct symbol *patched_sym)
!strcmp(patched_reloc->sym->sec->name, ".altinstr_aux"))
continue;
- if (convert_reloc_sym(e->patched, patched_reloc)) {
+ ret = convert_reloc_sym(e->patched, patched_reloc);
+ if (ret < 0) {
ERROR_FUNC(patched_rsec->base, reloc_offset(patched_reloc),
"failed to convert reloc sym '%s' to its proper format",
patched_reloc->sym->name);
return -1;
}
+ if (ret > 0)
+ continue;
offset = out_sym->offset + (reloc_offset(patched_reloc) - patched_sym->offset);
--
2.56.1
_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel
^ permalink raw reply [flat|nested] 31+ messages in thread* [Devel] [PATCH hci-8.2 v2 08/28] ms/objtool/klp: Fix cloning of zero-length section symbols
2026-11-26 5:06 [Devel] [PATCH hci-8.2 v2 00/28] ms/objtool: fix silently wrong livepatches by klp-build Eva Kurchatova
` (6 preceding siblings ...)
2026-11-26 5:07 ` [Devel] [PATCH hci-8.2 v2 07/28] ms/objtool/klp: Fix handling of zero-length .altinstr_replacement sections Eva Kurchatova
@ 2026-11-26 5:07 ` Eva Kurchatova
2026-11-26 5:07 ` [Devel] [PATCH hci-8.2 v2 09/28] ms/objtool/klp: Fix relocation conversion failures for R_X86_64_NONE Eva Kurchatova
` (21 subsequent siblings)
29 siblings, 0 replies; 31+ messages in thread
From: Eva Kurchatova @ 2026-11-26 5:07 UTC (permalink / raw)
To: khorenko, vladimir.riabchun; +Cc: devel
From: Josh Poimboeuf <jpoimboe@kernel.org>
Fix NULL dereference when cloning a symbol from an empty section.
sec->data is only populated for sections with non-zero size.
Fixes: dd590d4d57eb ("objtool/klp: Introduce klp diff subcommand for diffing object files")
Acked-by: Song Liu <song@kernel.org>
Reviewed-by: Miroslav Benes <mbenes@suse.cz>
Signed-off-by: Josh Poimboeuf <jpoimboe@kernel.org>
(cherry picked from commit 98377f3ba7c02d6eb34e203c9f9823bc62b0d231)
https://virtuozzo.atlassian.net/browse/VSTOR-128510
Feature: klp-build support
Signed-off-by: Eva Kurchatova <eva.kurchatova@virtuozzo.com>
Reviewed-by: Sashiko <no-reply@sashiko.virtuozzo.com>
---
tools/objtool/klp-diff.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/tools/objtool/klp-diff.c b/tools/objtool/klp-diff.c
index 5e39839759bb..817db8540c8b 100644
--- a/tools/objtool/klp-diff.c
+++ b/tools/objtool/klp-diff.c
@@ -540,7 +540,7 @@ static struct symbol *__clone_symbol(struct elf *elf, struct symbol *patched_sym
size_t size;
/* bss doesn't have data */
- if (patched_sym->sec->data->d_buf)
+ if (patched_sym->sec->data && patched_sym->sec->data->d_buf)
data = patched_sym->sec->data->d_buf + patched_sym->offset;
if (is_sec_sym(patched_sym))
--
2.56.1
_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel
^ permalink raw reply [flat|nested] 31+ messages in thread* [Devel] [PATCH hci-8.2 v2 09/28] ms/objtool/klp: Fix relocation conversion failures for R_X86_64_NONE
2026-11-26 5:06 [Devel] [PATCH hci-8.2 v2 00/28] ms/objtool: fix silently wrong livepatches by klp-build Eva Kurchatova
` (7 preceding siblings ...)
2026-11-26 5:07 ` [Devel] [PATCH hci-8.2 v2 08/28] ms/objtool/klp: Fix cloning of zero-length section symbols Eva Kurchatova
@ 2026-11-26 5:07 ` Eva Kurchatova
2026-11-26 5:07 ` [Devel] [PATCH hci-8.2 v2 10/28] ms/objtool/klp: Fix reloc corruption in convert_reloc_sym_to_secsym() Eva Kurchatova
` (20 subsequent siblings)
29 siblings, 0 replies; 31+ messages in thread
From: Eva Kurchatova @ 2026-11-26 5:07 UTC (permalink / raw)
To: khorenko, vladimir.riabchun; +Cc: devel
From: Josh Poimboeuf <jpoimboe@kernel.org>
Objtool has some hacks which NOP out certain calls/jumps and replace
their relocations with R_X86_64_NONE. The klp-diff relocation
extraction code will error out when trying to copy these relocations due
to their negative addend, which would only makes sense for a PC-relative
branch instruction. Just ignore them.
Fixes: dd590d4d57eb ("objtool/klp: Introduce klp diff subcommand for diffing object files")
Acked-by: Song Liu <song@kernel.org>
Reviewed-by: Miroslav Benes <mbenes@suse.cz>
Signed-off-by: Josh Poimboeuf <jpoimboe@kernel.org>
(cherry picked from commit 3787e82a4e3a0a04aeb5543580ee90bed3a36e55)
https://virtuozzo.atlassian.net/browse/VSTOR-128510
Feature: klp-build support
Signed-off-by: Eva Kurchatova <eva.kurchatova@virtuozzo.com>
Reviewed-by: Sashiko <no-reply@sashiko.virtuozzo.com>
---
tools/objtool/klp-diff.c | 3 +++
1 file changed, 3 insertions(+)
diff --git a/tools/objtool/klp-diff.c b/tools/objtool/klp-diff.c
index 817db8540c8b..941be5829ce7 100644
--- a/tools/objtool/klp-diff.c
+++ b/tools/objtool/klp-diff.c
@@ -891,6 +891,9 @@ static int convert_reloc_secsym_to_sym(struct elf *elf, struct reloc *reloc)
*/
static int convert_reloc_sym(struct elf *elf, struct reloc *reloc)
{
+ if (reloc_type(reloc) == R_NONE)
+ return 1;
+
if (is_reloc_allowed(reloc))
return 0;
--
2.56.1
_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel
^ permalink raw reply [flat|nested] 31+ messages in thread* [Devel] [PATCH hci-8.2 v2 10/28] ms/objtool/klp: Fix reloc corruption in convert_reloc_sym_to_secsym()
2026-11-26 5:06 [Devel] [PATCH hci-8.2 v2 00/28] ms/objtool: fix silently wrong livepatches by klp-build Eva Kurchatova
` (8 preceding siblings ...)
2026-11-26 5:07 ` [Devel] [PATCH hci-8.2 v2 09/28] ms/objtool/klp: Fix relocation conversion failures for R_X86_64_NONE Eva Kurchatova
@ 2026-11-26 5:07 ` Eva Kurchatova
2026-11-26 5:07 ` [Devel] [PATCH hci-8.2 v2 11/28] ms/objtool/klp: Simplify reloc symbol conversion Eva Kurchatova
` (19 subsequent siblings)
29 siblings, 0 replies; 31+ messages in thread
From: Eva Kurchatova @ 2026-11-26 5:07 UTC (permalink / raw)
To: khorenko, vladimir.riabchun; +Cc: devel
From: Josh Poimboeuf <jpoimboe@kernel.org>
Use the section symbol's index instead of the old symbol's index when
updating the ELF relocation entry in convert_reloc_sym_to_secsym().
Found by Sashiko review.
Acked-by: Song Liu <song@kernel.org>
Signed-off-by: Josh Poimboeuf <jpoimboe@kernel.org>
(cherry picked from commit 5f49ec82b9f65507c262a6e3f1b53663f79eedce)
https://virtuozzo.atlassian.net/browse/VSTOR-128510
Feature: klp-build support
Signed-off-by: Eva Kurchatova <eva.kurchatova@virtuozzo.com>
Reviewed-by: Sashiko <no-reply@sashiko.virtuozzo.com>
---
tools/objtool/klp-diff.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/tools/objtool/klp-diff.c b/tools/objtool/klp-diff.c
index 941be5829ce7..fa7a5c857b73 100644
--- a/tools/objtool/klp-diff.c
+++ b/tools/objtool/klp-diff.c
@@ -846,7 +846,7 @@ static int convert_reloc_sym_to_secsym(struct elf *elf, struct reloc *reloc)
return -1;
reloc->sym = sec->sym;
- set_reloc_sym(elf, reloc, sym->idx);
+ set_reloc_sym(elf, reloc, sec->sym->idx);
set_reloc_addend(elf, reloc, sym->offset + reloc_addend(reloc));
return 0;
}
--
2.56.1
_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel
^ permalink raw reply [flat|nested] 31+ messages in thread* [Devel] [PATCH hci-8.2 v2 11/28] ms/objtool/klp: Simplify reloc symbol conversion
2026-11-26 5:06 [Devel] [PATCH hci-8.2 v2 00/28] ms/objtool: fix silently wrong livepatches by klp-build Eva Kurchatova
` (9 preceding siblings ...)
2026-11-26 5:07 ` [Devel] [PATCH hci-8.2 v2 10/28] ms/objtool/klp: Fix reloc corruption in convert_reloc_sym_to_secsym() Eva Kurchatova
@ 2026-11-26 5:07 ` Eva Kurchatova
2026-11-26 5:07 ` [Devel] [PATCH hci-8.2 v2 12/28] ms/objtool: Move mark_rodata() to elf.c Eva Kurchatova
` (18 subsequent siblings)
29 siblings, 0 replies; 31+ messages in thread
From: Eva Kurchatova @ 2026-11-26 5:07 UTC (permalink / raw)
To: khorenko, vladimir.riabchun; +Cc: devel
From: Josh Poimboeuf <jpoimboe@kernel.org>
Inline section_reference_needed() and is_reloc_allowed() into
convert_reloc_sym() and remove the redundant is_reloc_allowed() check in
clone_reloc().
Move the is_sec_sym() checks into the convert callees so they become
no-ops when the reloc is already in the right format. This allows
convert_reloc_sym() to unconditionally dispatch to the right converter
based on section type.
Acked-by: Song Liu <song@kernel.org>
Reviewed-by: Miroslav Benes <mbenes@suse.cz>
Signed-off-by: Josh Poimboeuf <jpoimboe@kernel.org>
(cherry picked from commit 8fdc3585b3b09f2c55d8573edc6a1dbfeb728f03)
https://virtuozzo.atlassian.net/browse/VSTOR-128510
Feature: klp-build support
Signed-off-by: Eva Kurchatova <eva.kurchatova@virtuozzo.com>
Reviewed-by: Sashiko <no-reply@sashiko.virtuozzo.com>
---
tools/objtool/klp-diff.c | 76 +++++++++++++++-------------------------
1 file changed, 28 insertions(+), 48 deletions(-)
diff --git a/tools/objtool/klp-diff.c b/tools/objtool/klp-diff.c
index fa7a5c857b73..55208c6edfc6 100644
--- a/tools/objtool/klp-diff.c
+++ b/tools/objtool/klp-diff.c
@@ -696,39 +696,6 @@ static int clone_included_functions(struct elfs *e)
return 0;
}
-/*
- * Determine whether a relocation should reference the section rather than the
- * underlying symbol.
- */
-static bool section_reference_needed(struct section *sec)
-{
- /*
- * String symbols are zero-length and uncorrelated. It's easier to
- * deal with them as section symbols.
- */
- if (is_string_sec(sec))
- return true;
-
- /*
- * .rodata has mostly anonymous data so there's no way to determine the
- * length of a needed reference. just copy the whole section if needed.
- */
- if (strstarts(sec->name, ".rodata"))
- return true;
-
- /* UBSAN anonymous data */
- if (strstarts(sec->name, ".data..Lubsan") || /* GCC */
- strstarts(sec->name, ".data..L__unnamed_")) /* Clang */
- return true;
-
- return false;
-}
-
-static bool is_reloc_allowed(struct reloc *reloc)
-{
- return section_reference_needed(reloc->sym->sec) == is_sec_sym(reloc->sym);
-}
-
static struct export *find_export(struct symbol *sym)
{
struct export *export;
@@ -837,11 +804,15 @@ static bool klp_reloc_needed(struct reloc *patched_reloc)
return true;
}
+/* Return -1 error, 0 success, 1 skip */
static int convert_reloc_sym_to_secsym(struct elf *elf, struct reloc *reloc)
{
struct symbol *sym = reloc->sym;
struct section *sec = sym->sec;
+ if (is_sec_sym(sym))
+ return 0;
+
if (!sec->sym && !elf_create_section_symbol(elf, sec))
return -1;
@@ -851,11 +822,15 @@ static int convert_reloc_sym_to_secsym(struct elf *elf, struct reloc *reloc)
return 0;
}
+/* Return -1 error, 0 success, 1 skip */
static int convert_reloc_secsym_to_sym(struct elf *elf, struct reloc *reloc)
{
struct symbol *sym = reloc->sym;
struct section *sec = sym->sec;
+ if (!is_sec_sym(sym))
+ return 0;
+
/* If the symbol has a dedicated section, it's easy to find */
sym = find_symbol_by_offset(sec, 0);
if (sym && sym->len == sec_size(sec))
@@ -885,22 +860,34 @@ static int convert_reloc_secsym_to_sym(struct elf *elf, struct reloc *reloc)
return 0;
}
+/*
+ * Sections with anonymous or uncorrelated data (strings, UBSAN data)
+ * need section symbol references.
+ */
+static bool is_uncorrelated_section(struct section *sec)
+{
+ return is_string_sec(sec) ||
+ strstarts(sec->name, ".rodata") ||
+ strstarts(sec->name, ".data..Lubsan") || /* GCC */
+ strstarts(sec->name, ".data..L__unnamed_"); /* Clang */
+}
+
/*
* Convert a relocation symbol reference to the needed format: either a section
- * symbol or the underlying symbol itself.
+ * symbol or the underlying symbol itself. Return -1 error, 0 success, 1 skip.
*/
static int convert_reloc_sym(struct elf *elf, struct reloc *reloc)
{
+ struct section *sec = reloc->sym->sec;
+
if (reloc_type(reloc) == R_NONE)
return 1;
- if (is_reloc_allowed(reloc))
- return 0;
-
- if (section_reference_needed(reloc->sym->sec))
+ if (is_uncorrelated_section(sec))
return convert_reloc_sym_to_secsym(elf, reloc);
- else
- return convert_reloc_secsym_to_sym(elf, reloc);
+
+ /* Everything else: references should use named symbols. */
+ return convert_reloc_secsym_to_sym(elf, reloc);
}
/*
@@ -1045,13 +1032,6 @@ static int clone_reloc(struct elfs *e, struct reloc *patched_reloc,
struct symbol *out_sym;
bool klp;
- if (!is_reloc_allowed(patched_reloc)) {
- ERROR_FUNC(patched_reloc->sec->base, reloc_offset(patched_reloc),
- "missing symbol for reference to %s+%ld",
- patched_sym->name, addend);
- return -1;
- }
-
klp = klp_reloc_needed(patched_reloc);
dbg_clone_reloc(sec, offset, patched_sym, addend, export, klp);
@@ -1081,7 +1061,7 @@ static int clone_reloc(struct elfs *e, struct reloc *patched_reloc,
/*
* For strings, all references use section symbols, thanks to
- * section_reference_needed(). clone_symbol() has cloned an empty
+ * convert_reloc_sym(). clone_symbol() has cloned an empty
* version of the string section. Now copy the string itself.
*/
if (is_string_sec(patched_sym->sec)) {
--
2.56.1
_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel
^ permalink raw reply [flat|nested] 31+ messages in thread* [Devel] [PATCH hci-8.2 v2 12/28] ms/objtool: Move mark_rodata() to elf.c
2026-11-26 5:06 [Devel] [PATCH hci-8.2 v2 00/28] ms/objtool: fix silently wrong livepatches by klp-build Eva Kurchatova
` (10 preceding siblings ...)
2026-11-26 5:07 ` [Devel] [PATCH hci-8.2 v2 11/28] ms/objtool/klp: Simplify reloc symbol conversion Eva Kurchatova
@ 2026-11-26 5:07 ` Eva Kurchatova
2026-11-26 5:07 ` [Devel] [PATCH hci-8.2 v2 13/28] ms/objtool/klp: Fix pointer comparisons for rodata objects Eva Kurchatova
` (17 subsequent siblings)
29 siblings, 0 replies; 31+ messages in thread
From: Eva Kurchatova @ 2026-11-26 5:07 UTC (permalink / raw)
To: khorenko, vladimir.riabchun; +Cc: devel
From: Josh Poimboeuf <jpoimboe@kernel.org>
Move the sec->rodata marking from check.c to elf.c so it's set during
ELF reading rather than during the check pipeline. This makes the
rodata flag available to all objtool users, including klp-diff which
reads ELF files directly without running check().
Add an is_rodata_sec() helper to elf.h for consistency with
is_text_sec() and is_string_sec().
Acked-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Acked-by: Song Liu <song@kernel.org>
Reviewed-by: Miroslav Benes <mbenes@suse.cz>
Signed-off-by: Josh Poimboeuf <jpoimboe@kernel.org>
(cherry picked from commit 3e01ab44af207a2d8c197653e7278083b039a274)
https://virtuozzo.atlassian.net/browse/VSTOR-128510
Feature: klp-build support
Signed-off-by: Eva Kurchatova <eva.kurchatova@virtuozzo.com>
Reviewed-by: Sashiko <no-reply@sashiko.virtuozzo.com>
---
tools/objtool/check.c | 11 +++--------
tools/objtool/elf.c | 13 +++++++++++++
tools/objtool/include/objtool/elf.h | 5 +++++
3 files changed, 21 insertions(+), 8 deletions(-)
diff --git a/tools/objtool/check.c b/tools/objtool/check.c
index dc24ceed3c7d..f9539dae294f 100644
--- a/tools/objtool/check.c
+++ b/tools/objtool/check.c
@@ -2524,7 +2524,6 @@ static int classify_symbols(struct objtool_file *file)
static void mark_rodata(struct objtool_file *file)
{
struct section *sec;
- bool found = false;
/*
* Search for the following rodata sections, each of which can
@@ -2537,15 +2536,11 @@ static void mark_rodata(struct objtool_file *file)
* .rodata.str1.* sections are ignored; they don't contain jump tables.
*/
for_each_sec(file->elf, sec) {
- if ((!strncmp(sec->name, ".rodata", 7) &&
- !strstr(sec->name, ".str1.")) ||
- !strncmp(sec->name, ".data.rel.ro", 12)) {
- sec->rodata = true;
- found = true;
+ if (is_rodata_sec(sec)) {
+ file->rodata = true;
+ return;
}
}
-
- file->rodata = found;
}
static void mark_holes(struct objtool_file *file)
diff --git a/tools/objtool/elf.c b/tools/objtool/elf.c
index da20cec49374..a1cbaba7e1c1 100644
--- a/tools/objtool/elf.c
+++ b/tools/objtool/elf.c
@@ -1096,6 +1096,17 @@ static int read_relocs(struct elf *elf)
return 0;
}
+static void mark_rodata(struct elf *elf)
+{
+ struct section *sec;
+
+ for_each_sec(elf, sec) {
+ if ((strstarts(sec->name, ".rodata") && !strstr(sec->name, ".str1.")) ||
+ strstarts(sec->name, ".data.rel.ro"))
+ sec->rodata = true;
+ }
+}
+
struct elf *elf_open_read(const char *name, int flags)
{
struct elf *elf;
@@ -1146,6 +1157,8 @@ struct elf *elf_open_read(const char *name, int flags)
if (read_sections(elf))
goto err;
+ mark_rodata(elf);
+
if (read_symbols(elf))
goto err;
diff --git a/tools/objtool/include/objtool/elf.h b/tools/objtool/include/objtool/elf.h
index a2b3fb97aed9..fd2afbbee903 100644
--- a/tools/objtool/include/objtool/elf.h
+++ b/tools/objtool/include/objtool/elf.h
@@ -292,6 +292,11 @@ static inline bool is_text_sec(struct section *sec)
return sec->sh.sh_flags & SHF_EXECINSTR;
}
+static inline bool is_rodata_sec(struct section *sec)
+{
+ return sec->rodata;
+}
+
static inline bool sec_changed(struct section *sec)
{
return sec->_changed;
--
2.56.1
_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel
^ permalink raw reply [flat|nested] 31+ messages in thread* [Devel] [PATCH hci-8.2 v2 13/28] ms/objtool/klp: Fix pointer comparisons for rodata objects
2026-11-26 5:06 [Devel] [PATCH hci-8.2 v2 00/28] ms/objtool: fix silently wrong livepatches by klp-build Eva Kurchatova
` (11 preceding siblings ...)
2026-11-26 5:07 ` [Devel] [PATCH hci-8.2 v2 12/28] ms/objtool: Move mark_rodata() to elf.c Eva Kurchatova
@ 2026-11-26 5:07 ` Eva Kurchatova
2026-11-26 5:07 ` [Devel] [PATCH hci-8.2 v2 14/28] objtool/klp: use the section symbol for .rodata labels Eva Kurchatova
` (16 subsequent siblings)
29 siblings, 0 replies; 31+ messages in thread
From: Eva Kurchatova @ 2026-11-26 5:07 UTC (permalink / raw)
To: khorenko, vladimir.riabchun; +Cc: devel
From: Josh Poimboeuf <jpoimboe@kernel.org>
klp-diff treats all rodata as uncorrelated, so any reference to it uses
a duplicated copy rather than using a KLP reloc.
For the contents of the data itself, a duplicated copy is fine.
However, pointer comparisons (e.g., f->f_op == &foo_ops) are broken.
Fix it by correlating non-anonymous rodata objects.
Also, use a new find_symbol_containing_inclusive() helper for matching
the end of a symbol so bounds calculations don't get broken, for the
case where an array or other symbol's ending address is used as part of
a bounds calculation.
While these are really two distinct changes, they need to be done in the
same patch so as to avoid introducing bisection regressions.
Acked-by: Song Liu <song@kernel.org>
Signed-off-by: Josh Poimboeuf <jpoimboe@kernel.org>
(cherry picked from commit d5b0f025281f287759820f57b7ba1bac7827b338)
https://virtuozzo.atlassian.net/browse/VSTOR-128510
Feature: klp-build support
Signed-off-by: Eva Kurchatova <eva.kurchatova@virtuozzo.com>
---
tools/objtool/elf.c | 14 ++++++++++++++
tools/objtool/include/objtool/elf.h | 1 +
tools/objtool/klp-diff.c | 15 +++++++++++++--
3 files changed, 28 insertions(+), 2 deletions(-)
diff --git a/tools/objtool/elf.c b/tools/objtool/elf.c
index a1cbaba7e1c1..9f1ebb643686 100644
--- a/tools/objtool/elf.c
+++ b/tools/objtool/elf.c
@@ -223,6 +223,20 @@ struct symbol *find_symbol_containing(const struct section *sec, unsigned long o
return sym;
}
+/*
+ * Also match the symbol end address which can be used for a bounds comparison.
+ */
+struct symbol *find_symbol_containing_inclusive(const struct section *sec,
+ unsigned long offset)
+{
+ struct symbol *sym = find_symbol_containing(sec, offset);
+
+ if (!sym && offset)
+ sym = find_symbol_containing(sec, offset - 1);
+
+ return sym;
+}
+
/*
* Returns size of hole starting at @offset.
*/
diff --git a/tools/objtool/include/objtool/elf.h b/tools/objtool/include/objtool/elf.h
index fd2afbbee903..bc1f604fc67a 100644
--- a/tools/objtool/include/objtool/elf.h
+++ b/tools/objtool/include/objtool/elf.h
@@ -186,6 +186,7 @@ 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);
struct symbol *find_symbol_containing(const struct section *sec, unsigned long offset);
+struct symbol *find_symbol_containing_inclusive(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);
struct reloc *find_reloc_by_dest_range(const struct elf *elf, struct section *sec,
diff --git a/tools/objtool/klp-diff.c b/tools/objtool/klp-diff.c
index 55208c6edfc6..2f1cedf9632b 100644
--- a/tools/objtool/klp-diff.c
+++ b/tools/objtool/klp-diff.c
@@ -352,6 +352,7 @@ static bool dont_correlate(struct symbol *sym)
is_uncorrelated_static_local(sym) ||
is_clang_tmp_label(sym) ||
is_string_sec(sym->sec) ||
+ (is_rodata_sec(sym->sec) && !is_object_sym(sym)) ||
is_special_section(sym->sec) ||
is_special_section_aux(sym->sec) ||
strstarts(sym->name, "__initcall__");
@@ -837,7 +838,7 @@ static int convert_reloc_secsym_to_sym(struct elf *elf, struct reloc *reloc)
goto found_sym;
/* No dedicated section; find the symbol manually */
- sym = find_symbol_containing(sec, arch_adjusted_addend(reloc));
+ sym = find_symbol_containing_inclusive(sec, arch_adjusted_addend(reloc));
if (!sym) {
/*
* This is presumably an .altinstr_replacement section which is
@@ -846,6 +847,17 @@ static int convert_reloc_secsym_to_sym(struct elf *elf, struct reloc *reloc)
if (!sec_size(sec))
return 1;
+ /*
+ * .rodata is a mixed bag of named objects and anonymous data.
+ *
+ * Convert section symbol references to named object symbols
+ * when possible, to preserve pointer identity for const
+ * structs like file_operations. Otherwise a section symbol is
+ * fine.
+ */
+ if (is_rodata_sec(sec))
+ return 0;
+
/*
* This can happen for special section references to weak code
* whose symbol has been stripped by the linker.
@@ -867,7 +879,6 @@ static int convert_reloc_secsym_to_sym(struct elf *elf, struct reloc *reloc)
static bool is_uncorrelated_section(struct section *sec)
{
return is_string_sec(sec) ||
- strstarts(sec->name, ".rodata") ||
strstarts(sec->name, ".data..Lubsan") || /* GCC */
strstarts(sec->name, ".data..L__unnamed_"); /* Clang */
}
--
2.56.1
_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel
^ permalink raw reply [flat|nested] 31+ messages in thread* [Devel] [PATCH hci-8.2 v2 14/28] objtool/klp: use the section symbol for .rodata labels
2026-11-26 5:06 [Devel] [PATCH hci-8.2 v2 00/28] ms/objtool: fix silently wrong livepatches by klp-build Eva Kurchatova
` (12 preceding siblings ...)
2026-11-26 5:07 ` [Devel] [PATCH hci-8.2 v2 13/28] ms/objtool/klp: Fix pointer comparisons for rodata objects Eva Kurchatova
@ 2026-11-26 5:07 ` Eva Kurchatova
2026-11-26 5:07 ` [Devel] [PATCH hci-8.2 v2 15/28] objtool/klp: reject changed .rodata objects Eva Kurchatova
` (15 subsequent siblings)
29 siblings, 0 replies; 31+ messages in thread
From: Eva Kurchatova @ 2026-11-26 5:07 UTC (permalink / raw)
To: khorenko, vladimir.riabchun; +Cc: devel
Since "objtool/klp: Fix pointer comparisons for rodata objects", a
reference to .rodata keeps its symbol instead of the section symbol.
Asm puts global labels without a size there, such as the
uretprobe_trampoline_entry and uretprobe_trampoline_end pair. They are
not correlated, so the livepatch gets a copy of each of length 0, and
both land at one address: a patched arch_uprobe_trampoline() then works
on garbage.
Reference a non-object symbol in .rodata through the section symbol
again, as before that commit, so that the whole section is cloned.
A section copy which follows one of its objects in the output does not
start at the section symbol, and references to it, or to anonymous
.rodata data, then point into that object. Fail klp diff instead; with
the opposite reference order the object only gets a second copy.
https://virtuozzo.atlassian.net/browse/VSTOR-128510
Feature: klp-build support
Signed-off-by: Eva Kurchatova <eva.kurchatova@virtuozzo.com>
---
tools/objtool/klp-diff.c | 9 +++++++++
1 file changed, 9 insertions(+)
diff --git a/tools/objtool/klp-diff.c b/tools/objtool/klp-diff.c
index 2f1cedf9632b..5122a3b51196 100644
--- a/tools/objtool/klp-diff.c
+++ b/tools/objtool/klp-diff.c
@@ -535,6 +535,11 @@ static struct symbol *__clone_symbol(struct elf *elf, struct symbol *patched_sym
if (!is_sec_sym(patched_sym))
offset = ALIGN(sec_size(out_sec), out_sec->sh.sh_addralign);
+ else if (sec_size(patched_sec) && sec_size(out_sec)) {
+ /* The copy would not start at the section symbol */
+ ERROR("can't clone section %s after one of its symbols", patched_sec->name);
+ return NULL;
+ }
if (patched_sym->len || is_sec_sym(patched_sym)) {
void *data = NULL;
@@ -897,6 +902,10 @@ static int convert_reloc_sym(struct elf *elf, struct reloc *reloc)
if (is_uncorrelated_section(sec))
return convert_reloc_sym_to_secsym(elf, reloc);
+ /* Non-object .rodata symbols, like asm labels, may have no size: clone the section */
+ if (is_rodata_sec(sec) && !is_sec_sym(reloc->sym) && !is_object_sym(reloc->sym))
+ return convert_reloc_sym_to_secsym(elf, reloc);
+
/* Everything else: references should use named symbols. */
return convert_reloc_secsym_to_sym(elf, reloc);
}
--
2.56.1
_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel
^ permalink raw reply [flat|nested] 31+ messages in thread* [Devel] [PATCH hci-8.2 v2 15/28] objtool/klp: reject changed .rodata objects
2026-11-26 5:06 [Devel] [PATCH hci-8.2 v2 00/28] ms/objtool: fix silently wrong livepatches by klp-build Eva Kurchatova
` (13 preceding siblings ...)
2026-11-26 5:07 ` [Devel] [PATCH hci-8.2 v2 14/28] objtool/klp: use the section symbol for .rodata labels Eva Kurchatova
@ 2026-11-26 5:07 ` Eva Kurchatova
2026-11-26 5:07 ` [Devel] [PATCH hci-8.2 v2 16/28] ms/objtool/klp: Fix module name normalization for paths with dots Eva Kurchatova
` (14 subsequent siblings)
29 siblings, 0 replies; 31+ messages in thread
From: Eva Kurchatova @ 2026-11-26 5:07 UTC (permalink / raw)
To: khorenko, vladimir.riabchun; +Cc: devel
Since "objtool/klp: Fix pointer comparisons for rodata objects", a named
.rodata object is correlated, and the patched code references the
original through a klp reloc. A change to the object, such as a new row
of a static const table, is then lost without notice; so is a reference
to another function's same-named local table, which correlation by name
in order binds when the patch adds one earlier in the file. The same
goes for a writable object which the patch makes const and changes.
Fail klp diff when a correlated object in .rodata on either side differs
in length, bytes or relocations, or stops being const, as mainline does
since 6016dd33a10a ("objtool/klp: Rewrite symbol correlation
algorithm"), which is too large to take here. Relocations are compared
by target: strings by contents, correlated symbols by twin and offset,
uncorrelated ones such as __func__ by name and bytes. Targets no symbol
contains are compared by section only, as their offsets move with any
constant added before them, so a change to such data is not caught.
https://virtuozzo.atlassian.net/browse/VSTOR-128510
Feature: klp-build support
Signed-off-by: Eva Kurchatova <eva.kurchatova@virtuozzo.com>
---
tools/objtool/klp-diff.c | 134 +++++++++++++++++++++++++++++++++++++++
1 file changed, 134 insertions(+)
diff --git a/tools/objtool/klp-diff.c b/tools/objtool/klp-diff.c
index 5122a3b51196..fa9089d6c3c1 100644
--- a/tools/objtool/klp-diff.c
+++ b/tools/objtool/klp-diff.c
@@ -443,6 +443,137 @@ static int correlate_symbols(struct elfs *e)
return 0;
}
+/* The symbol a reloc points into and the offset in it, NULL if anonymous */
+static struct symbol *reloc_target(struct reloc *reloc, s64 *offset)
+{
+ struct symbol *sym = reloc->sym;
+
+ *offset = arch_adjusted_addend(reloc);
+ if (!is_sec_sym(sym))
+ return sym;
+
+ sym = find_symbol_containing_inclusive(sym->sec, *offset);
+ if (sym)
+ *offset -= sym->offset;
+ return sym;
+}
+
+static const void *sym_data(struct symbol *sym)
+{
+ if (!sym->sec->data || !sym->sec->data->d_buf)
+ return NULL;
+
+ return sym->sec->data->d_buf + sym->offset;
+}
+
+/* A symbol without data is in .bss, which reads as zeroes */
+static bool data_changed(struct symbol *orig, struct symbol *patched)
+{
+ const unsigned char *orig_data = sym_data(orig);
+ const unsigned char *patched_data = sym_data(patched);
+ unsigned long i;
+
+ if (orig->len != patched->len)
+ return true;
+
+ if (orig_data && patched_data)
+ return memcmp(orig_data, patched_data, orig->len);
+
+ for (i = 0; i < orig->len; i++)
+ if ((orig_data && orig_data[i]) || (patched_data && patched_data[i]))
+ return true;
+
+ return false;
+}
+
+static const char *reloc_string(struct reloc *reloc)
+{
+ struct symbol *sym = reloc->sym;
+
+ return sym->sec->data->d_buf + sym->offset + arch_adjusted_addend(reloc);
+}
+
+static bool reloc_changed(struct reloc *orig, struct reloc *patched)
+{
+ struct symbol *orig_sym, *patched_sym;
+ s64 orig_off, patched_off;
+
+ if (reloc_type(orig) != reloc_type(patched))
+ return true;
+
+ /* Strings move inside their merged section: compare the contents */
+ if (is_string_sec(orig->sym->sec) || is_string_sec(patched->sym->sec))
+ return !is_string_sec(orig->sym->sec) ||
+ !is_string_sec(patched->sym->sec) ||
+ strcmp(reloc_string(orig), reloc_string(patched));
+
+ orig_sym = reloc_target(orig, &orig_off);
+ patched_sym = reloc_target(patched, &patched_off);
+
+ if (!orig_sym || !patched_sym)
+ return orig_sym != patched_sym ||
+ strcmp(orig->sym->sec->name, patched->sym->sec->name);
+
+ if (orig_off != patched_off)
+ return true;
+
+ if (orig_sym->twin || patched_sym->twin)
+ return orig_sym->twin != patched_sym;
+
+ /* Uncorrelated, e.g. __func__: the original stays referenced too */
+ return strcmp(orig_sym->demangled_name, patched_sym->demangled_name) ||
+ data_changed(orig_sym, patched_sym);
+}
+
+static bool object_changed(struct elfs *e, struct symbol *orig, struct symbol *patched)
+{
+ struct reloc *orig_reloc, *patched_reloc;
+
+ /* A twin outside .rodata (no longer const) loses the change too */
+ if (!is_rodata_sec(patched->sec) || data_changed(orig, patched))
+ return true;
+
+ patched_reloc = find_reloc_by_dest_range(e->patched, patched->sec,
+ patched->offset, patched->len);
+ sym_for_each_reloc(e->orig, orig, orig_reloc) {
+ if (!patched_reloc ||
+ reloc_offset(patched_reloc) >= patched->offset + patched->len ||
+ reloc_offset(orig_reloc) - orig->offset !=
+ reloc_offset(patched_reloc) - patched->offset ||
+ reloc_changed(orig_reloc, patched_reloc))
+ return true;
+
+ patched_reloc = rsec_next_reloc(patched->sec->rsec, patched_reloc);
+ }
+
+ return patched_reloc &&
+ reloc_offset(patched_reloc) < patched->offset + patched->len;
+}
+
+/*
+ * The patched code references a correlated object through a klp reloc to
+ * the original, so a change to an object that is or becomes const would
+ * be lost.
+ */
+static int check_changed_rodata(struct elfs *e)
+{
+ struct symbol *orig;
+ int ret = 0;
+
+ for_each_sym(e->orig, orig) {
+ if (!is_object_sym(orig) || !orig->twin ||
+ (!is_rodata_sec(orig->sec) && !is_rodata_sec(orig->twin->sec)))
+ continue;
+
+ if (object_changed(e, orig, orig->twin)) {
+ ERROR("changed data: %s", orig->name);
+ ret = -1;
+ }
+ }
+
+ return ret;
+}
+
/* "sympos" is used by livepatch to disambiguate duplicate symbol names */
static unsigned long find_sympos(struct elf *elf, struct symbol *sym)
{
@@ -1723,6 +1854,9 @@ int cmd_klp_diff(int argc, const char **argv)
if (correlate_symbols(&e))
return -1;
+ if (check_changed_rodata(&e))
+ return -1;
+
if (mark_changed_functions(&e))
return 0;
--
2.56.1
_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel
^ permalink raw reply [flat|nested] 31+ messages in thread* [Devel] [PATCH hci-8.2 v2 16/28] ms/objtool/klp: Fix module name normalization for paths with dots
2026-11-26 5:06 [Devel] [PATCH hci-8.2 v2 00/28] ms/objtool: fix silently wrong livepatches by klp-build Eva Kurchatova
` (14 preceding siblings ...)
2026-11-26 5:07 ` [Devel] [PATCH hci-8.2 v2 15/28] objtool/klp: reject changed .rodata objects Eva Kurchatova
@ 2026-11-26 5:07 ` Eva Kurchatova
2026-11-26 5:07 ` [Devel] [PATCH hci-8.2 v2 17/28] ms/objtool/klp: Normalize Module.symvers paths to module names Eva Kurchatova
` (13 subsequent siblings)
29 siblings, 0 replies; 31+ messages in thread
From: Eva Kurchatova @ 2026-11-26 5:07 UTC (permalink / raw)
To: khorenko, vladimir.riabchun; +Cc: devel
From: Josh Poimboeuf <jpoimboe@kernel.org>
When .modinfo has no "name=" tag, __find_modname() falls back to
converting the object's build-tree path to a runtime module name by
stripping directory components, converting '-' to '_' and truncating the
file extension.
It does all that in a single pass over the entire path, so the first dot
anywhere in the path ends the name. For an object built in a directory
whose name contains a dot, e.g. "drivers/foo-1.0/bar.o", the result is a
bogus module name.
Strip the directory components up front so only the basename is scanned
for the extension separator.
Fixes: dd590d4d57eb ("objtool/klp: Introduce klp diff subcommand for diffing object files")
Reported-by: Sashiko <sashiko-bot@kernel.org>
Signed-off-by: Josh Poimboeuf <jpoimboe@kernel.org>
Signed-off-by: Ingo Molnar <mingo@kernel.org>
Cc: live-patching@vger.kernel.org
Link: https://patch.msgid.link/9017b4609553bed16674e8f924d34691cbc2b2c1.1785727106.git.jpoimboe@kernel.org
(cherry picked from commit 165affd6f095323d953b0ed5823ec4d0db3b7d0d)
https://virtuozzo.atlassian.net/browse/VSTOR-128510
Feature: klp-build support
Signed-off-by: Eva Kurchatova <eva.kurchatova@virtuozzo.com>
---
tools/objtool/klp-diff.c | 10 ++++++----
1 file changed, 6 insertions(+), 4 deletions(-)
diff --git a/tools/objtool/klp-diff.c b/tools/objtool/klp-diff.c
index fa9089d6c3c1..d12439a48274 100644
--- a/tools/objtool/klp-diff.c
+++ b/tools/objtool/klp-diff.c
@@ -848,7 +848,7 @@ static struct export *find_export(struct symbol *sym)
static const char *__find_modname(struct elfs *e)
{
struct section *sec;
- char *name;
+ char *name, *slash;
sec = find_section_by_name(e->orig, ".modinfo");
if (!sec) {
@@ -866,10 +866,12 @@ static const char *__find_modname(struct elfs *e)
return NULL;
}
+ slash = strrchr(name, '/');
+ if (slash)
+ name = slash + 1;
+
for (char *c = name; *c; c++) {
- if (*c == '/')
- name = c + 1;
- else if (*c == '-')
+ if (*c == '-')
*c = '_';
else if (*c == '.') {
*c = '\0';
--
2.56.1
_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel
^ permalink raw reply [flat|nested] 31+ messages in thread* [Devel] [PATCH hci-8.2 v2 17/28] ms/objtool/klp: Normalize Module.symvers paths to module names
2026-11-26 5:06 [Devel] [PATCH hci-8.2 v2 00/28] ms/objtool: fix silently wrong livepatches by klp-build Eva Kurchatova
` (15 preceding siblings ...)
2026-11-26 5:07 ` [Devel] [PATCH hci-8.2 v2 16/28] ms/objtool/klp: Fix module name normalization for paths with dots Eva Kurchatova
@ 2026-11-26 5:07 ` Eva Kurchatova
2026-11-26 5:07 ` [Devel] [PATCH hci-8.2 v2 18/28] ms/objtool/klp: Fix cross-module klp relocation section naming Eva Kurchatova
` (12 subsequent siblings)
29 siblings, 0 replies; 31+ messages in thread
From: Eva Kurchatova @ 2026-11-26 5:07 UTC (permalink / raw)
To: khorenko, vladimir.riabchun; +Cc: devel
From: Joe Lawrence <joe.lawrence@redhat.com>
Module.symvers contains build-tree object paths as module identifiers
(e.g., "arch/x86/kvm/kvm") rather than runtime module names ("kvm").
Objtool's clone_reloc_klp() uses this field directly for exported
symbols, while unexported symbols correctly go through __find_modname().
This means that exported symbol relocations may land in a .klp.rela
section named with the build path rather than the module name. That is
a crash waiting to happen: the kernel's livepatch loader silently skips
this relocation because it doesn't match the expected klp_object name.
The unresolved relocation sits in the newly activated code, crashing
when executed.
Normalize export->mod at Module.symvers read time using the same logic
as __find_modname() (refactored into a shared normalize_modname()
helper).
Fixes: dd590d4d57eb ("objtool/klp: Introduce klp diff subcommand for diffing object files")
Reported-by: Ben Procknow <bprockno@redhat.com>
Signed-off-by: Joe Lawrence <joe.lawrence@redhat.com>
Signed-off-by: Josh Poimboeuf <jpoimboe@kernel.org>
Signed-off-by: Ingo Molnar <mingo@kernel.org>
Reviewed-by: Miroslav Benes <mbenes@suse.cz>
Cc: live-patching@vger.kernel.org
Link: https://patch.msgid.link/dbe1b72931bd3c31b751fd0729613d9f2226fff6.1785727106.git.jpoimboe@kernel.org
(cherry picked from commit 8668bf91e0508abeb8e98b4edd89032be02228a1)
https://virtuozzo.atlassian.net/browse/VSTOR-128510
Feature: klp-build support
Signed-off-by: Eva Kurchatova <eva.kurchatova@virtuozzo.com>
---
tools/objtool/klp-diff.c | 49 ++++++++++++++++++++++++++++------------
1 file changed, 34 insertions(+), 15 deletions(-)
diff --git a/tools/objtool/klp-diff.c b/tools/objtool/klp-diff.c
index d12439a48274..5b14005f8eee 100644
--- a/tools/objtool/klp-diff.c
+++ b/tools/objtool/klp-diff.c
@@ -83,6 +83,35 @@ static char *escape_str(const char *orig)
return new;
}
+/*
+ * Convert a build-tree object path to a runtime module name: strip
+ * directory components, replace '-' with '_', and remove file
+ * extensions. Examples:
+ *
+ * "arch/x86/kvm/kvm" -> "kvm"
+ * "arch/x86/kvm/kvm-intel" -> "kvm_intel".
+ *
+ * Used by read_exports() to normalize Module.symvers entries and by
+ * __find_modname() as a fallback when .modinfo lacks a "name=" tag.
+ */
+static char *normalize_modname(char *name)
+{
+ char *slash = strrchr(name, '/');
+
+ if (slash)
+ name = slash + 1;
+
+ for (char *c = name; *c; c++) {
+ if (*c == '-')
+ *c = '_';
+ else if (*c == '.') {
+ *c = '\0';
+ break;
+ }
+ }
+ return name;
+}
+
static int read_exports(void)
{
const char *symvers = "Module.symvers";
@@ -150,6 +179,9 @@ static int read_exports(void)
return -1;
}
+ if (strcmp(export->mod, "vmlinux"))
+ export->mod = normalize_modname(export->mod);
+
export->sym = strdup(sym);
if (!export->sym) {
ERROR_GLIBC("strdup");
@@ -848,7 +880,7 @@ static struct export *find_export(struct symbol *sym)
static const char *__find_modname(struct elfs *e)
{
struct section *sec;
- char *name, *slash;
+ char *name;
sec = find_section_by_name(e->orig, ".modinfo");
if (!sec) {
@@ -866,20 +898,7 @@ static const char *__find_modname(struct elfs *e)
return NULL;
}
- slash = strrchr(name, '/');
- if (slash)
- name = slash + 1;
-
- for (char *c = name; *c; c++) {
- if (*c == '-')
- *c = '_';
- else if (*c == '.') {
- *c = '\0';
- break;
- }
- }
-
- return name;
+ return normalize_modname(name);
}
/* Get the object's module name as defined by the kernel (and klp_object) */
--
2.56.1
_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel
^ permalink raw reply [flat|nested] 31+ messages in thread* [Devel] [PATCH hci-8.2 v2 18/28] ms/objtool/klp: Fix cross-module klp relocation section naming
2026-11-26 5:06 [Devel] [PATCH hci-8.2 v2 00/28] ms/objtool: fix silently wrong livepatches by klp-build Eva Kurchatova
` (16 preceding siblings ...)
2026-11-26 5:07 ` [Devel] [PATCH hci-8.2 v2 17/28] ms/objtool/klp: Normalize Module.symvers paths to module names Eva Kurchatova
@ 2026-11-26 5:07 ` Eva Kurchatova
2026-11-26 5:07 ` [Devel] [PATCH hci-8.2 v2 19/28] ms/objtool/klp: Don't match local symbols against exports Eva Kurchatova
` (11 subsequent siblings)
29 siblings, 0 replies; 31+ messages in thread
From: Eva Kurchatova @ 2026-11-26 5:07 UTC (permalink / raw)
To: khorenko, vladimir.riabchun; +Cc: devel
From: Josh Poimboeuf <jpoimboe@kernel.org>
A klp relocation section is .klp.rela.<objname>.<secname>, where objname
is the object being patched.
klp-build wrongly derives objname from where the referenced symbol
lives, not where it's referenced. For a cross-module reference like
patched can_isotp code calling can.ko's can_rx_unregister(), that gives
.klp.rela.can..text rather than .klp.rela.can_isotp..text. Unless the
patch happens to patch can.ko as well, the relocation never gets applied
and the call goes off into the weeds.
Name the intermediate section __klp_relocs.<objname> so post-link can
read the patched object's name from there.
Fixes: dd590d4d57eb ("objtool/klp: Introduce klp diff subcommand for diffing object files")
Reported-by: Joe Lawrence <joe.lawrence@redhat.com>
Link: https://lore.kernel.org/20260720145658.1103243-2-joe.lawrence@redhat.com
Acked-by: Song Liu <song@kernel.org>
Acked-by: Joe Lawrence <joe.lawrence@redhat.com>
Link: https://patch.msgid.link/ee93a08f3e55e76ffa67d04e283917ddaa893f09.1786138493.git.jpoimboe@kernel.org
Signed-off-by: Josh Poimboeuf <jpoimboe@kernel.org>
(cherry picked from commit 07f14d6af9d7791fdc2b44a3dd0693e42c71d2b8)
https://virtuozzo.atlassian.net/browse/VSTOR-128510
Feature: klp-build support
Signed-off-by: Eva Kurchatova <eva.kurchatova@virtuozzo.com>
---
tools/objtool/include/objtool/klp.h | 10 ++++--
tools/objtool/klp-diff.c | 17 +++++++--
tools/objtool/klp-post-link.c | 53 +++++++++++++++++------------
3 files changed, 52 insertions(+), 28 deletions(-)
diff --git a/tools/objtool/include/objtool/klp.h b/tools/objtool/include/objtool/klp.h
index ad830a7ce55b..d9cb594f4924 100644
--- a/tools/objtool/include/objtool/klp.h
+++ b/tools/objtool/include/objtool/klp.h
@@ -14,11 +14,15 @@
#define KLP_FUNCS_SEC "__klp_funcs"
/*
- * __klp_relocs is an intermediate section which are created by klp diff and
- * converted into KLP symbols/relas by "objtool klp post-link". This is needed
- * to work around the linker, which doesn't preserve SHN_LIVEPATCH or
+ * __klp_relocs.<objname> are intermediate sections which are created by klp
+ * diff and converted into KLP symbols/relas by "objtool klp post-link". This
+ * is needed to work around the linker, which doesn't preserve SHN_LIVEPATCH or
* SHF_RELA_LIVEPATCH, nor does it support having two RELA sections for a
* single PROGBITS section.
+ *
+ * "objname" is the name of the object being patched ("vmlinux" or a module
+ * name). post-link uses it to name the resulting
+ * .klp.rela.objname.section_name sections.
*/
#define KLP_RELOCS_SEC "__klp_relocs"
#define KLP_STRINGS_SEC ".rodata.klp.str1.1"
diff --git a/tools/objtool/klp-diff.c b/tools/objtool/klp-diff.c
index 5b14005f8eee..a2483b404b66 100644
--- a/tools/objtool/klp-diff.c
+++ b/tools/objtool/klp-diff.c
@@ -1143,8 +1143,8 @@ static int clone_reloc_klp(struct elfs *e, struct reloc *patched_reloc,
}
/*
- * Create the __klp_relocs entry. This will be converted to an actual
- * KLP rela by "objtool klp post-link".
+ * Create the __klp_relocs.<objname> entry. This will be converted to
+ * an actual KLP rela by "objtool klp post-link".
*
* This intermediate step is necessary to prevent corruption by the
* linker, which doesn't know how to properly handle two rela sections
@@ -1152,7 +1152,18 @@ static int clone_reloc_klp(struct elfs *e, struct reloc *patched_reloc,
*/
if (!klp_relocs) {
- klp_relocs = elf_create_section(e->out, KLP_RELOCS_SEC, 0,
+ const char *objname = find_modname(e);
+ char sec_name[SEC_NAME_LEN];
+
+ if (!objname)
+ return -1;
+
+ /* section format: __klp_relocs.objname */
+ if (snprintf_check(sec_name, SEC_NAME_LEN,
+ KLP_RELOCS_SEC ".%s", objname))
+ return -1;
+
+ klp_relocs = elf_create_section(e->out, sec_name, 0,
0, SHT_PROGBITS, 8, SHF_ALLOC);
if (!klp_relocs)
return -1;
diff --git a/tools/objtool/klp-post-link.c b/tools/objtool/klp-post-link.c
index c013e39957b1..350d20495897 100644
--- a/tools/objtool/klp-post-link.c
+++ b/tools/objtool/klp-post-link.c
@@ -19,19 +19,11 @@
#include <objtool/util.h>
#include <linux/livepatch_external.h>
-static int fix_klp_relocs(struct elf *elf)
+static int fix_klp_reloc_sec(struct elf *elf, struct section *symtab,
+ struct section *klp_relocs)
{
- struct section *symtab, *klp_relocs;
-
- klp_relocs = find_section_by_name(elf, KLP_RELOCS_SEC);
- if (!klp_relocs)
- return 0;
-
- symtab = find_section_by_name(elf, ".symtab");
- if (!symtab) {
- ERROR("missing .symtab");
- return -1;
- }
+ /* section format: __klp_relocs.sec_objname */
+ const char *sec_objname = klp_relocs->name + strlen(KLP_RELOCS_SEC ".");
for (int i = 0; i < sec_size(klp_relocs) / sizeof(struct klp_reloc); i++) {
struct klp_reloc *klp_reloc;
@@ -39,7 +31,6 @@ static int fix_klp_relocs(struct elf *elf)
struct section *sec, *tmp, *klp_rsec;
unsigned long offset;
struct reloc *reloc;
- char sym_modname[64];
char rsec_name[SEC_NAME_LEN];
u64 addend;
struct symbol *sym, *klp_sym;
@@ -55,7 +46,7 @@ static int fix_klp_relocs(struct elf *elf)
reloc = find_reloc_by_dest(elf, klp_relocs,
klp_reloc_off + offsetof(struct klp_reloc, offset));
if (!reloc) {
- ERROR("malformed " KLP_RELOCS_SEC " section");
+ ERROR("malformed %s section", klp_relocs->name);
return -1;
}
@@ -66,17 +57,13 @@ static int fix_klp_relocs(struct elf *elf)
reloc = find_reloc_by_dest(elf, klp_relocs,
klp_reloc_off + offsetof(struct klp_reloc, sym));
if (!reloc) {
- ERROR("malformed " KLP_RELOCS_SEC " section");
+ ERROR("malformed %s section", klp_relocs->name);
return -1;
}
klp_sym = reloc->sym;
addend = reloc_addend(reloc);
- /* symbol format: .klp.sym.modname.sym_name,sympos */
- if (sscanf(klp_sym->name + strlen(KLP_SYM_PREFIX), "%55[^.]", sym_modname) != 1)
- ERROR("can't find modname in klp symbol '%s'", klp_sym->name);
-
/*
* Create the KLP rela:
*/
@@ -84,7 +71,7 @@ static int fix_klp_relocs(struct elf *elf)
/* section format: .klp.rela.sec_objname.section_name */
if (snprintf_check(rsec_name, SEC_NAME_LEN,
KLP_RELOC_SEC_PREFIX "%s.%s",
- sym_modname, sec->name))
+ sec_objname, sec->name))
return -1;
klp_rsec = find_section_by_name(elf, rsec_name);
@@ -134,10 +121,32 @@ static int fix_klp_relocs(struct elf *elf)
return 0;
}
+static int fix_klp_relocs(struct elf *elf)
+{
+ struct section *symtab, *sec;
+
+ symtab = find_section_by_name(elf, ".symtab");
+ if (!symtab) {
+ ERROR("missing .symtab");
+ return -1;
+ }
+
+ for_each_sec(elf, sec) {
+ if (strncmp(sec->name, KLP_RELOCS_SEC ".",
+ strlen(KLP_RELOCS_SEC ".")))
+ continue;
+
+ if (fix_klp_reloc_sec(elf, symtab, sec))
+ return -1;
+ }
+
+ return 0;
+}
+
/*
* This runs on the livepatch module after all other linking has been done. It
- * converts the intermediate __klp_relocs section into proper KLP relocs to be
- * processed by livepatch. This needs to run last to avoid linker wreckage.
+ * converts the intermediate __klp_relocs.* sections into proper KLP relocs to
+ * be processed by livepatch. This needs to run last to avoid linker wreckage.
* Linkers don't tend to handle the "two rela sections for a single base
* section" case very well, nor do they appreciate SHN_LIVEPATCH.
*/
--
2.56.1
_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel
^ permalink raw reply [flat|nested] 31+ messages in thread* [Devel] [PATCH hci-8.2 v2 19/28] ms/objtool/klp: Don't match local symbols against exports
2026-11-26 5:06 [Devel] [PATCH hci-8.2 v2 00/28] ms/objtool: fix silently wrong livepatches by klp-build Eva Kurchatova
` (17 preceding siblings ...)
2026-11-26 5:07 ` [Devel] [PATCH hci-8.2 v2 18/28] ms/objtool/klp: Fix cross-module klp relocation section naming Eva Kurchatova
@ 2026-11-26 5:07 ` Eva Kurchatova
2026-11-26 5:07 ` [Devel] [PATCH hci-8.2 v2 20/28] ms/objtool: Fix .cold function detection for duplicate symbols Eva Kurchatova
` (10 subsequent siblings)
29 siblings, 0 replies; 31+ messages in thread
From: Eva Kurchatova @ 2026-11-26 5:07 UTC (permalink / raw)
To: khorenko, vladimir.riabchun; +Cc: devel
From: Josh Poimboeuf <jpoimboe@kernel.org>
While cloning a reloc, klp diff calls find_export() to determine whether
the referenced symbol is exported. That decides whether the reference
needs a klp reloc, which object the klp symbol belongs to, and whether
the symbol's data needs to be copied into the patch module.
But find_export() matches purely on symbol name, so a static function or
variable which happens to share its name with an export is mistaken for
a reference to that export:
- klp_reloc_needed() creates a klp reloc pointing at the exporting
module's symbol rather than the local one. For a vmlinux export it
skips the klp reloc altogether, leaving a normal reloc which the
module loader resolves to the vmlinux symbol.
- clone_reloc() treats the symbol as external and clones it without
its data, leaving a dangling reference.
- validate_special_section_klp_reloc() attributes a static branch or
call key to the wrong module, and for a vmlinux export skips the
unsupported-key check entirely.
Exports are always global, so ignore local symbols in find_export().
Fixes: dd590d4d57eb ("objtool/klp: Introduce klp diff subcommand for diffing object files")
Acked-by: Song Liu <song@kernel.org>
Acked-by: Joe Lawrence <joe.lawrence@redhat.com>
Link: https://patch.msgid.link/dafc7c017d1f62e2190a20dc84a52fbfc4c70210.1786138493.git.jpoimboe@kernel.org
Signed-off-by: Josh Poimboeuf <jpoimboe@kernel.org>
(cherry picked from commit 86a697572c6271083694025eefa8de518eeef623)
https://virtuozzo.atlassian.net/browse/VSTOR-128510
Feature: klp-build support
Signed-off-by: Eva Kurchatova <eva.kurchatova@virtuozzo.com>
---
tools/objtool/klp-diff.c | 3 +++
1 file changed, 3 insertions(+)
diff --git a/tools/objtool/klp-diff.c b/tools/objtool/klp-diff.c
index a2483b404b66..90c2b6d17568 100644
--- a/tools/objtool/klp-diff.c
+++ b/tools/objtool/klp-diff.c
@@ -869,6 +869,9 @@ static struct export *find_export(struct symbol *sym)
{
struct export *export;
+ if (is_local_sym(sym))
+ return NULL;
+
hash_for_each_possible(exports, export, hash, str_hash(sym->name)) {
if (!strcmp(export->sym, sym->name))
return export;
--
2.56.1
_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel
^ permalink raw reply [flat|nested] 31+ messages in thread* [Devel] [PATCH hci-8.2 v2 20/28] ms/objtool: Fix .cold function detection for duplicate symbols
2026-11-26 5:06 [Devel] [PATCH hci-8.2 v2 00/28] ms/objtool: fix silently wrong livepatches by klp-build Eva Kurchatova
` (18 preceding siblings ...)
2026-11-26 5:07 ` [Devel] [PATCH hci-8.2 v2 19/28] ms/objtool/klp: Don't match local symbols against exports Eva Kurchatova
@ 2026-11-26 5:07 ` Eva Kurchatova
2026-11-26 5:07 ` [Devel] [PATCH hci-8.2 v2 21/28] ms/objtool/klp: Avoid NULL pointer dereference when printing code symbol name Eva Kurchatova
` (9 subsequent siblings)
29 siblings, 0 replies; 31+ messages in thread
From: Eva Kurchatova @ 2026-11-26 5:07 UTC (permalink / raw)
To: khorenko, vladimir.riabchun; +Cc: devel
From: Josh Poimboeuf <jpoimboe@kernel.org>
The objtool .cold child/parent correlation is done in two phases: first
in elf_add_symbol() and later in add_jump_destinations().
The first phase is rather crude and can pick the wrong parent if there
are duplicates with the same name.
The second phase usually fixes that, but only if the parent has a direct
jump to the child. It does *not* work if the only branch from the
parent to the child is an alternative or jump table entry.
Make the first phase more robust by looking for the parent in the same
STT_FILE as the child.
Fixes the following objtool warnings in an AutoFDO build with a large
CLANG_AUTOFDO_PROFILE profile:
vmlinux.o: warning: objtool: rdev_add_key() falls through to next function rdev_add_key.cold()
vmlinux.o: warning: objtool: rdev_set_default_key() falls through to next function rdev_set_default_key.cold()
Fixes: 13810435b9a7 ("objtool: Support GCC 8's cold subfunctions")
Signed-off-by: Josh Poimboeuf <jpoimboe@kernel.org>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Link: https://patch.msgid.link/82c7b52e40efa75dd10e1c550cc75c1ce10ac2c9.1763671318.git.jpoimboe@kernel.org
(cherry picked from commit 2c2acca2eabf53a954ed5aacef987bbf909b9f12)
https://virtuozzo.atlassian.net/browse/VSTOR-156924
Feature: klp-build support
Signed-off-by: Eva Kurchatova <eva.kurchatova@virtuozzo.com>
---
tools/objtool/elf.c | 28 ++++++++++++++++++++++++++--
tools/objtool/include/objtool/elf.h | 2 +-
2 files changed, 27 insertions(+), 3 deletions(-)
diff --git a/tools/objtool/elf.c b/tools/objtool/elf.c
index 9f1ebb643686..167a57cf71ab 100644
--- a/tools/objtool/elf.c
+++ b/tools/objtool/elf.c
@@ -302,6 +302,23 @@ struct symbol *find_symbol_by_name(const struct elf *elf, const char *name)
return NULL;
}
+/* Find local symbol with matching STT_FILE */
+static struct symbol *find_local_symbol_by_file_and_name(const struct elf *elf,
+ struct symbol *file,
+ const char *name)
+{
+ struct symbol *sym;
+
+ elf_hash_for_each_possible(symbol_name, sym, name_hash, str_hash(name)) {
+ if (sym->bind == STB_LOCAL && sym->file == file &&
+ !strcmp(sym->name, name)) {
+ return sym;
+ }
+ }
+
+ return NULL;
+}
+
struct symbol *find_global_symbol_by_name(const struct elf *elf, const char *name)
{
struct symbol *sym;
@@ -538,7 +555,7 @@ static int elf_add_symbol(struct elf *elf, struct symbol *sym)
static int read_symbols(struct elf *elf)
{
struct section *symtab, *symtab_shndx, *sec;
- struct symbol *sym, *pfunc;
+ struct symbol *sym, *pfunc, *file = NULL;
int symbols_nr, i;
char *coldstr;
Elf_Data *shndx_data = NULL;
@@ -611,6 +628,11 @@ static int read_symbols(struct elf *elf)
if (elf_add_symbol(elf, sym))
return -1;
+
+ if (sym->type == STT_FILE)
+ file = sym;
+ else if (sym->bind == STB_LOCAL)
+ sym->file = file;
}
if (opts.stats) {
@@ -640,7 +662,9 @@ static int read_symbols(struct elf *elf)
return -1;
}
- pfunc = find_symbol_by_name(elf, pname);
+ pfunc = find_local_symbol_by_file_and_name(elf, sym->file, pname);
+ if (!pfunc)
+ pfunc = find_global_symbol_by_name(elf, pname);
free(pname);
if (!pfunc) {
diff --git a/tools/objtool/include/objtool/elf.h b/tools/objtool/include/objtool/elf.h
index bc1f604fc67a..411f7ada7e47 100644
--- a/tools/objtool/include/objtool/elf.h
+++ b/tools/objtool/include/objtool/elf.h
@@ -69,7 +69,7 @@ struct symbol {
unsigned int idx, len;
unsigned long offset;
unsigned long __subtree_last;
- struct symbol *pfunc, *cfunc, *alias;
+ struct symbol *pfunc, *cfunc, *alias, *file;
unsigned char bind, type;
u8 uaccess_safe : 1;
u8 static_call_tramp : 1;
--
2.56.1
_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel
^ permalink raw reply [flat|nested] 31+ messages in thread* [Devel] [PATCH hci-8.2 v2 21/28] ms/objtool/klp: Avoid NULL pointer dereference when printing code symbol name
2026-11-26 5:06 [Devel] [PATCH hci-8.2 v2 00/28] ms/objtool: fix silently wrong livepatches by klp-build Eva Kurchatova
` (19 preceding siblings ...)
2026-11-26 5:07 ` [Devel] [PATCH hci-8.2 v2 20/28] ms/objtool: Fix .cold function detection for duplicate symbols Eva Kurchatova
@ 2026-11-26 5:07 ` Eva Kurchatova
2026-11-26 5:07 ` [Devel] [PATCH hci-8.2 v2 22/28] ms/objtool/klp: Add .klp.symid for sympos disambiguation Eva Kurchatova
` (8 subsequent siblings)
29 siblings, 0 replies; 31+ messages in thread
From: Eva Kurchatova @ 2026-11-26 5:07 UTC (permalink / raw)
To: khorenko, vladimir.riabchun; +Cc: devel
From: Josh Poimboeuf <jpoimboe@kernel.org>
Fix a hypothetical NULL pointer defereference of the 'code_sym'
variable. In theory this should never happen.
Reviewed-and-tested-by: Song Liu <song@kernel.org>
Link: https://patch.msgid.link/64116517bc93851a98fe366ea0a4d807f4c70aab.1770759954.git.jpoimboe@kernel.org
Signed-off-by: Josh Poimboeuf <jpoimboe@kernel.org>
(cherry picked from commit 11c2adcd1fa2a9380a507db1e57c8542bfc81827)
https://virtuozzo.atlassian.net/browse/VSTOR-156924
Feature: klp-build support
Signed-off-by: Eva Kurchatova <eva.kurchatova@virtuozzo.com>
---
tools/objtool/klp-diff.c | 15 +++++++++------
1 file changed, 9 insertions(+), 6 deletions(-)
diff --git a/tools/objtool/klp-diff.c b/tools/objtool/klp-diff.c
index 90c2b6d17568..1428ac71dff8 100644
--- a/tools/objtool/klp-diff.c
+++ b/tools/objtool/klp-diff.c
@@ -1505,7 +1505,7 @@ static int validate_special_section_klp_reloc(struct elfs *e, struct symbol *sym
{
bool static_branch = !strcmp(sym->sec->name, "__jump_table");
bool static_call = !strcmp(sym->sec->name, ".static_call_sites");
- struct symbol *code_sym = NULL;
+ const char *code_sym = NULL;
unsigned long code_offset = 0;
struct reloc *reloc;
int ret = 0;
@@ -1525,7 +1525,7 @@ static int validate_special_section_klp_reloc(struct elfs *e, struct symbol *sym
/* Save code location which can be printed below */
if (reloc->sym->type == STT_FUNC && !code_sym) {
- code_sym = reloc->sym;
+ code_sym = reloc->sym->name;
code_offset = reloc_addend(reloc);
}
@@ -1548,23 +1548,26 @@ static int validate_special_section_klp_reloc(struct elfs *e, struct symbol *sym
if (!strcmp(sym_modname, "vmlinux"))
continue;
+ if (!code_sym)
+ code_sym = "<unknown>";
+
if (static_branch) {
if (strstarts(reloc->sym->name, "__tracepoint_")) {
WARN("%s: disabling unsupported tracepoint %s",
- code_sym->name, reloc->sym->name + 13);
+ code_sym, reloc->sym->name + 13);
ret = 1;
continue;
}
if (strstr(reloc->sym->name, "__UNIQUE_ID_ddebug_")) {
WARN("%s: disabling unsupported pr_debug()",
- code_sym->name);
+ code_sym);
ret = 1;
continue;
}
ERROR("%s+0x%lx: unsupported static branch key %s. Use static_key_enabled() instead",
- code_sym->name, code_offset, reloc->sym->name);
+ code_sym, code_offset, reloc->sym->name);
return -1;
}
@@ -1575,7 +1578,7 @@ static int validate_special_section_klp_reloc(struct elfs *e, struct symbol *sym
}
ERROR("%s()+0x%lx: unsupported static call key %s. Use KLP_STATIC_CALL() instead",
- code_sym->name, code_offset, reloc->sym->name);
+ code_sym, code_offset, reloc->sym->name);
return -1;
}
--
2.56.1
_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel
^ permalink raw reply [flat|nested] 31+ messages in thread* [Devel] [PATCH hci-8.2 v2 22/28] ms/objtool/klp: Add .klp.symid for sympos disambiguation
2026-11-26 5:06 [Devel] [PATCH hci-8.2 v2 00/28] ms/objtool: fix silently wrong livepatches by klp-build Eva Kurchatova
` (20 preceding siblings ...)
2026-11-26 5:07 ` [Devel] [PATCH hci-8.2 v2 21/28] ms/objtool/klp: Avoid NULL pointer dereference when printing code symbol name Eva Kurchatova
@ 2026-11-26 5:07 ` Eva Kurchatova
2026-11-26 5:07 ` [Devel] [PATCH hci-8.2 v2 23/28] ms/objtool/klp: Fix symbol resolution for duplicate data symbols Eva Kurchatova
` (7 subsequent siblings)
29 siblings, 0 replies; 31+ messages in thread
From: Eva Kurchatova @ 2026-11-26 5:07 UTC (permalink / raw)
To: khorenko, vladimir.riabchun; +Cc: devel
From: Josh Poimboeuf <jpoimboe@kernel.org>
Livepatch identifies a duplicate-named symbol by its position (sympos)
among same-named kallsyms entries, which for vmlinux are counted in
ascending address order in the final linked kernel. That order can't be
reliably derived from vmlinux.o: the final link reorders sub-sections
(.text.unlikely*, .data..*, etc).
Bridge the gap with a new .klp.symid section which can be used to
correlate symbols between vmlinux.o and vmlinux so that klp-diff can
reliably determine the sympos.
The table can't survive --gc-sections: keeping it alive would keep every
duplicate-named symbol's section alive, so the reference kernel would
stop matching the one which ships. klp-build rejects
CONFIG_LD_DEAD_CODE_DATA_ELIMINATION instead. Nothing is lost today:
x86_64 is the only HAVE_KLP_BUILD arch and doesn't select
HAVE_LD_DEAD_CODE_DATA_ELIMINATION, arm64 and s390 have never selected
it either, and on powerpc, it's still EXPERIMENTAL and disabled by every
distro kernel.
This is the build-time half of reliable vmlinux sympos computation;
"objtool klp diff" will consume the table in a subsequent commit.
Signed-off-by: Josh Poimboeuf <jpoimboe@kernel.org>
Signed-off-by: Ingo Molnar <mingo@kernel.org>
Cc: live-patching@vger.kernel.org
Link: https://patch.msgid.link/64d50f077b569f47883c015cdb7079edb068efe8.1785727106.git.jpoimboe@kernel.org
(cherry picked from commit 029223d301620bc4e1086696047b0d5d6eba5edd)
Rebase note: for_each_sym_by_name() is added here, on the symbol name
hash of this tree, and str_hash() moves to elf.h from elf.c and
klp-diff.c, as mainline has them since 0333b7399587 ("objtool: Replace
iterator callback with for_each_sym_by_mangled_name()") and
def5b60dcd22 ("objtool/klp: Fix --debug-checksum for duplicate symbol
names"). Context conflicts only elsewhere.
https://virtuozzo.atlassian.net/browse/VSTOR-156924
Feature: klp-build support
Signed-off-by: Eva Kurchatova <eva.kurchatova@virtuozzo.com>
---
include/asm-generic/vmlinux.lds.h | 10 +-
scripts/Makefile.vmlinux_o | 3 +
scripts/livepatch/klp-build | 5 +
scripts/mod/modpost.c | 1 +
tools/objtool/Build | 1 +
tools/objtool/builtin-check.c | 7 ++
tools/objtool/check.c | 7 ++
tools/objtool/elf.c | 12 ++-
tools/objtool/include/objtool/builtin.h | 1 +
tools/objtool/include/objtool/elf.h | 17 ++++
tools/objtool/include/objtool/klp.h | 15 +++
tools/objtool/klp-diff.c | 5 -
tools/objtool/klp-symid.c | 117 ++++++++++++++++++++++++
13 files changed, 190 insertions(+), 11 deletions(-)
create mode 100644 tools/objtool/klp-symid.c
diff --git a/include/asm-generic/vmlinux.lds.h b/include/asm-generic/vmlinux.lds.h
index 366ac310ad09..28c9ac174599 100644
--- a/include/asm-generic/vmlinux.lds.h
+++ b/include/asm-generic/vmlinux.lds.h
@@ -841,12 +841,20 @@
.stab.index 0 : { *(.stab.index) } \
.stab.indexstr 0 : { *(.stab.indexstr) }
+#ifdef CONFIG_KLP_BUILD
+#define KLP_SYMID \
+ .klp.symid 0 : { *(.klp.symid) }
+#else
+#define KLP_SYMID
+#endif
+
/* Required sections not related to debugging. */
#define ELF_DETAILS \
.comment 0 : { *(.comment) } \
.symtab 0 : { *(.symtab) } \
.strtab 0 : { *(.strtab) } \
- .shstrtab 0 : { *(.shstrtab) }
+ .shstrtab 0 : { *(.shstrtab) } \
+ KLP_SYMID
#ifdef CONFIG_GENERIC_BUG
#define BUG_TABLE \
diff --git a/scripts/Makefile.vmlinux_o b/scripts/Makefile.vmlinux_o
index cd6b21d89f44..632f882e2791 100644
--- a/scripts/Makefile.vmlinux_o
+++ b/scripts/Makefile.vmlinux_o
@@ -47,6 +47,9 @@ endif
vmlinux-objtool-args-$(CONFIG_NOINSTR_VALIDATION) += --noinstr \
$(if $(or $(CONFIG_MITIGATION_UNRET_ENTRY),$(CONFIG_MITIGATION_SRSO)), --unret)
+# Only used for builds initiated by klp-build
+vmlinux-objtool-args-$(if $(KLP_SYMIDS),y) += --klp-symids
+
objtool-args = $(vmlinux-objtool-args-y) --link
# Link of vmlinux.o used for section mismatch analysis
diff --git a/scripts/livepatch/klp-build b/scripts/livepatch/klp-build
index 882272120c9e..3bd134fe2283 100755
--- a/scripts/livepatch/klp-build
+++ b/scripts/livepatch/klp-build
@@ -249,6 +249,9 @@ validate_config() {
[[ -v CONFIG_GCC_PLUGIN_RANDSTRUCT ]] && \
die "kernel option 'CONFIG_GCC_PLUGIN_RANDSTRUCT' not supported"
+ [[ -v CONFIG_LD_DEAD_CODE_DATA_ELIMINATION ]] && \
+ die "kernel option 'CONFIG_LD_DEAD_CODE_DATA_ELIMINATION' not supported"
+
return 0
}
@@ -513,6 +516,8 @@ build_kernel() {
#
cmd+=("KBUILD_MODPOST_WARN=1")
+ cmd+=("KLP_SYMIDS=1")
+
cmd+=("$VERBOSE")
cmd+=("-j$JOBS")
cmd+=("KCFLAGS=-ffunction-sections -fdata-sections")
diff --git a/scripts/mod/modpost.c b/scripts/mod/modpost.c
index 6e5ae3a05b36..746a51cf9821 100644
--- a/scripts/mod/modpost.c
+++ b/scripts/mod/modpost.c
@@ -749,6 +749,7 @@ static const char *const section_white_list[] =
".gnu.lto*",
".discard.*",
".llvm.call-graph-profile", /* call graph */
+ ".klp.symid", /* objtool --klp-symids */
NULL
};
diff --git a/tools/objtool/Build b/tools/objtool/Build
index 8cd71b9a5eef..6b01eaa4e554 100644
--- a/tools/objtool/Build
+++ b/tools/objtool/Build
@@ -6,6 +6,7 @@ objtool-y += check.o
objtool-y += special.o
objtool-y += builtin-check.o
objtool-y += elf.o
+objtool-y += klp-symid.o
objtool-y += objtool.o
objtool-$(BUILD_ORC) += orc_gen.o orc_dump.o
diff --git a/tools/objtool/builtin-check.c b/tools/objtool/builtin-check.c
index e550fb9a504e..45163ba80b09 100644
--- a/tools/objtool/builtin-check.c
+++ b/tools/objtool/builtin-check.c
@@ -77,6 +77,7 @@ static const struct option check_options[] = {
OPT_BOOLEAN(0, "cfi", &opts.cfi, "annotate kernel control flow integrity (kCFI) function preambles"),
OPT_CALLBACK_OPTARG('h', "hacks", NULL, NULL, "jump_label,noinstr,skylake", "patch toolchain bugs/limitations", parse_hacks),
OPT_BOOLEAN('i', "ibt", &opts.ibt, "validate and annotate IBT"),
+ OPT_BOOLEAN(0, "klp-symids", &opts.klp_symids, "generate .klp.symids for duplicate symbol disambiguation"),
OPT_BOOLEAN('m', "mcount", &opts.mcount, "annotate mcount/fentry calls for ftrace"),
OPT_BOOLEAN(0, "noabs", &opts.noabs, "reject absolute references in allocatable sections"),
OPT_BOOLEAN('n', "noinstr", &opts.noinstr, "validate noinstr rules"),
@@ -174,10 +175,16 @@ static bool opts_valid(void)
return false;
}
+ if (opts.klp_symids && !opts.link) {
+ ERROR("--klp-symids requires --link");
+ return false;
+ }
+
if (opts.checksum ||
opts.hack_jump_label ||
opts.hack_noinstr ||
opts.ibt ||
+ opts.klp_symids ||
opts.mcount ||
opts.noabs ||
opts.noinstr ||
diff --git a/tools/objtool/check.c b/tools/objtool/check.c
index f9539dae294f..403da0d3739a 100644
--- a/tools/objtool/check.c
+++ b/tools/objtool/check.c
@@ -13,6 +13,7 @@
#include <objtool/cfi.h>
#include <objtool/arch.h>
#include <objtool/check.h>
+#include <objtool/klp.h>
#include <objtool/special.h>
#include <objtool/warn.h>
#include <objtool/checksum.h>
@@ -4972,6 +4973,12 @@ int check(struct objtool_file *file)
goto out;
}
+ if (opts.klp_symids) {
+ ret = klp_create_symid_sections(file);
+ if (ret)
+ goto out;
+ }
+
if (opts.noabs)
warnings += check_abs_references(file);
diff --git a/tools/objtool/elf.c b/tools/objtool/elf.c
index 167a57cf71ab..1c6d071f81e2 100644
--- a/tools/objtool/elf.c
+++ b/tools/objtool/elf.c
@@ -27,11 +27,6 @@
#define ALIGN_UP_POW2(x) (1U << ((8 * sizeof(x)) - __builtin_clz((x) - 1U)))
#define MAX(a, b) ((a) > (b) ? (a) : (b))
-static inline u32 str_hash(const char *str)
-{
- return jhash(str, strlen(str), 0);
-}
-
#define __elf_table(name) (elf->name##_hash)
#define __elf_bits(name) (elf->name##_bits)
@@ -290,6 +285,13 @@ struct symbol *find_func_containing(struct section *sec, unsigned long offset)
return NULL;
}
+/* The first symbol of the hash chain of @name, see for_each_sym_by_name() */
+struct symbol *elf_sym_name_chain(const struct elf *elf, const char *name)
+{
+ return elf_list_entry(__elf_table_entry(symbol_name, str_hash(name)),
+ struct symbol, name_hash);
+}
+
struct symbol *find_symbol_by_name(const struct elf *elf, const char *name)
{
struct symbol *sym;
diff --git a/tools/objtool/include/objtool/builtin.h b/tools/objtool/include/objtool/builtin.h
index bb0b25eb08ba..b944cb30540e 100644
--- a/tools/objtool/include/objtool/builtin.h
+++ b/tools/objtool/include/objtool/builtin.h
@@ -16,6 +16,7 @@ struct opts {
bool hack_noinstr;
bool hack_skylake;
bool ibt;
+ bool klp_symids;
bool mcount;
bool noabs;
bool noinstr;
diff --git a/tools/objtool/include/objtool/elf.h b/tools/objtool/include/objtool/elf.h
index 411f7ada7e47..757f7c78acd3 100644
--- a/tools/objtool/include/objtool/elf.h
+++ b/tools/objtool/include/objtool/elf.h
@@ -21,6 +21,11 @@
#define SEC_NAME_LEN 1024
#define SYM_NAME_LEN 512
+static inline u32 str_hash(const char *str)
+{
+ return jhash(str, strlen(str), 0);
+}
+
#define bswap_if_needed(elf, val) __bswap_if_needed(&elf->ehdr, val)
#ifdef LIBELF_USE_DEPRECATED
@@ -184,6 +189,7 @@ struct section *find_section_by_name(const struct elf *elf, const char *name);
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 *elf_sym_name_chain(const struct elf *elf, const char *name);
struct symbol *find_global_symbol_by_name(const struct elf *elf, const char *name);
struct symbol *find_symbol_containing(const struct section *sec, unsigned long offset);
struct symbol *find_symbol_containing_inclusive(const struct section *sec, unsigned long offset);
@@ -470,6 +476,17 @@ static inline void set_sym_next_reloc(struct reloc *reloc, struct reloc *next)
#define for_each_sym_continue(elf, sym) \
list_for_each_entry_continue(sym, &elf->symbols, global_list)
+static inline struct symbol *elf_sym_name_next(struct symbol *sym)
+{
+ return sym->name_hash.next ?
+ container_of(sym->name_hash.next, struct symbol, name_hash) : NULL;
+}
+
+#define for_each_sym_by_name(elf, _name, sym) \
+ for (sym = elf_sym_name_chain(elf, _name); sym; \
+ sym = elf_sym_name_next(sym)) \
+ if (strcmp(sym->name, _name)) {} else
+
#define rsec_next_reloc(rsec, reloc) \
reloc_idx(reloc) < sec_num_entries(rsec) - 1 ? reloc + 1 : NULL
diff --git a/tools/objtool/include/objtool/klp.h b/tools/objtool/include/objtool/klp.h
index d9cb594f4924..b23ddde8b47d 100644
--- a/tools/objtool/include/objtool/klp.h
+++ b/tools/objtool/include/objtool/klp.h
@@ -33,6 +33,21 @@ struct klp_reloc {
u32 type;
};
+/*
+ * .klp.symid is used to correlate symbols between vmlinux.o and vmlinux, for
+ * calculating sympos to disambiguate duplicately-named symbols.
+ */
+#define KLP_SYMID_SEC ".klp.symid"
+
+struct klp_symid {
+ u64 id;
+ u64 addr;
+};
+
+struct objtool_file;
+
+int klp_create_symid_sections(struct objtool_file *file);
+
int cmd_klp_diff(int argc, const char **argv);
int cmd_klp_post_link(int argc, const char **argv);
diff --git a/tools/objtool/klp-diff.c b/tools/objtool/klp-diff.c
index 1428ac71dff8..d9c91539f690 100644
--- a/tools/objtool/klp-diff.c
+++ b/tools/objtool/klp-diff.c
@@ -46,11 +46,6 @@ static const struct option klp_diff_options[] = {
static DEFINE_HASHTABLE(exports, 15);
-static inline u32 str_hash(const char *str)
-{
- return jhash(str, strlen(str), 0);
-}
-
static char *escape_str(const char *orig)
{
size_t len = 0;
diff --git a/tools/objtool/klp-symid.c b/tools/objtool/klp-symid.c
new file mode 100644
index 000000000000..cf188cdfa607
--- /dev/null
+++ b/tools/objtool/klp-symid.c
@@ -0,0 +1,117 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Emit the .klp.symid table which allows "objtool klp diff" to reliably
+ * disambiguate duplicate-named local symbols in vmlinux.
+ *
+ * Livepatch identifies a duplicate-named symbol by its position (sympos)
+ * among the same-named kallsyms entries, counted in ascending address order
+ * in the final linked vmlinux. That order can't be derived from vmlinux.o
+ * alone: the final link reorders sub-sections (.text.unlikely*, .data..*,
+ * etc).
+ *
+ * Bridge the gap with a table which survives the final link: a single
+ * non-alloc section containing an array of { id, addr } entries, where
+ * 'id' is a unique counter identifier and 'addr' has a relocation to the
+ * symbol. The linker copies 'id' verbatim and resolves 'addr' to the symbol's
+ * final address.
+ *
+ * The table is only emitted for vmlinux.o, and only when klp-build asks for it
+ * with KLP_SYMIDS=1, which adds --klp-symids to the vmlinux.o objtool run.
+ *
+ * It can't survive --gc-sections, which sweeps the whole section; klp-build
+ * rejects CONFIG_LD_DEAD_CODE_DATA_ELIMINATION.
+ */
+#include <linux/string.h>
+
+#include <objtool/objtool.h>
+#include <objtool/warn.h>
+#include <objtool/endianness.h>
+#include <objtool/klp.h>
+
+static const char * const discarded_secs[] = {
+ ".discard",
+ ".modinfo",
+ "__tracepoint_check",
+};
+
+static bool discarded_sec(struct section *sec)
+{
+ if (!(sec->sh.sh_flags & SHF_ALLOC))
+ return true;
+
+ for (int i = 0; i < ARRAY_SIZE(discarded_secs); i++)
+ if (strstarts(sec->name, discarded_secs[i]))
+ return true;
+
+ return false;
+}
+
+static bool symid_needed(struct elf *elf, struct symbol *sym)
+{
+ struct symbol *s;
+
+ if (!is_local_sym(sym) || is_undef_sym(sym))
+ return false;
+
+ if (!is_func_sym(sym) && !is_object_sym(sym))
+ return false;
+
+ if (is_prefix_func(sym))
+ return false;
+
+ if (discarded_sec(sym->sec))
+ return false;
+
+ for_each_sym_by_name(elf, sym->name, s) {
+ if (s == sym || is_sec_sym(s) || is_file_sym(s) || is_undef_sym(s))
+ continue;
+ return true;
+ }
+
+ return false;
+}
+
+int klp_create_symid_sections(struct objtool_file *file)
+{
+ struct elf *elf = file->elf;
+ struct klp_symid *symids;
+ struct section *sec;
+ struct symbol *sym;
+ u64 nr = 0, i = 0;
+
+ if (!str_ends_with(objname, "vmlinux.o"))
+ return 0;
+
+ for_each_sym(elf, sym)
+ if (symid_needed(elf, sym))
+ nr++;
+
+ if (!nr)
+ return 0;
+
+ sec = elf_create_section(elf, KLP_SYMID_SEC, 0, sizeof(struct klp_symid),
+ SHT_PROGBITS, 8, 0);
+ if (!sec)
+ return -1;
+
+ symids = elf_add_data(elf, sec, NULL, nr * sizeof(struct klp_symid));
+ if (!symids)
+ return -1;
+
+ for_each_sym(elf, sym) {
+ if (!symid_needed(elf, sym))
+ continue;
+
+ symids[i].id = bswap_if_needed(elf, i);
+
+ if (!elf_create_reloc(elf, sec,
+ i * sizeof(struct klp_symid) +
+ offsetof(struct klp_symid, addr),
+ sym, 0, R_ABS64))
+ return -1;
+
+ i++;
+ }
+
+ return 0;
+}
--
2.56.1
_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel
^ permalink raw reply [flat|nested] 31+ messages in thread* [Devel] [PATCH hci-8.2 v2 23/28] ms/objtool/klp: Fix symbol resolution for duplicate data symbols
2026-11-26 5:06 [Devel] [PATCH hci-8.2 v2 00/28] ms/objtool: fix silently wrong livepatches by klp-build Eva Kurchatova
` (21 preceding siblings ...)
2026-11-26 5:07 ` [Devel] [PATCH hci-8.2 v2 22/28] ms/objtool/klp: Add .klp.symid for sympos disambiguation Eva Kurchatova
@ 2026-11-26 5:07 ` Eva Kurchatova
2026-11-26 5:07 ` [Devel] [PATCH hci-8.2 v2 24/28] ms/objtool/klp: Fix size of empty special section entries Eva Kurchatova
` (6 subsequent siblings)
29 siblings, 0 replies; 31+ messages in thread
From: Eva Kurchatova @ 2026-11-26 5:07 UTC (permalink / raw)
To: khorenko, vladimir.riabchun; +Cc: devel
From: Josh Poimboeuf <jpoimboe@kernel.org>
find_sympos() calculates a sympos used by livepatch to disambiguate
duplicately-named symbols. For function symbols, there's a hack which
counts .text.unlikely symbols before other .text symbols, matching the
linker script's section ordering.
Not only is the hack fragile, data symbols can have the same problem.
So for example, adding a reference to pwq_cache in
ep_unregister_pollwait() can trigger a corrupt sympos and a relocation
to the wrong pwq_cache symbol in the livepatch module, resulting in a
crash or undefined behavior.
Remove the existing hack in favor of a fully deterministic solution,
using the new .klp.symid table to derive the symbol-to-id mapping from
the original vmlinux.o and the id-to-address mapping from the
corresponding vmlinux, which can then be used to determine the exact
sympos associated with the original vmlinux.
Modules don't need any special treatment: the .ko has the same
section/symbol ordering as the original whole-archive symbol table.
Fixes: dd590d4d57eb ("objtool/klp: Introduce klp diff subcommand for diffing object files")
Reported-by: Ben Procknow <bprockno@redhat.com>
Reported-by: Joe Lawrence <joe.lawrence@redhat.com>
Signed-off-by: Josh Poimboeuf <jpoimboe@kernel.org>
Signed-off-by: Ingo Molnar <mingo@kernel.org>
Cc: live-patching@vger.kernel.org
Link: https://lore.kernel.org/20260710153042.3156788-1-joe.lawrence@redhat.com
Link: https://lore.kernel.org/20260724221730.3126529-1-joe.lawrence@redhat.com
Link: https://patch.msgid.link/919785e3bf2245db02ff6391e735d9cb139170b1.1785727106.git.jpoimboe@kernel.org
(cherry picked from commit 15fa203ef91e8a303c322eaaa8ca01a6ddaf94dc)
Rebase note: klp diff runs in the orig directory here, where the vmlinux
copied by copy_orig_objects() is found; mainline also copies it for its
"objtool klp checksum" step, which this tree does not have.
https://virtuozzo.atlassian.net/browse/VSTOR-156924
Feature: klp-build support
Signed-off-by: Eva Kurchatova <eva.kurchatova@virtuozzo.com>
---
scripts/livepatch/klp-build | 2 +
tools/objtool/Build | 3 +-
tools/objtool/include/objtool/klp.h | 4 +
tools/objtool/klp-diff.c | 66 +----
tools/objtool/klp-sympos.c | 411 ++++++++++++++++++++++++++++
5 files changed, 424 insertions(+), 62 deletions(-)
create mode 100644 tools/objtool/klp-sympos.c
diff --git a/scripts/livepatch/klp-build b/scripts/livepatch/klp-build
index 3bd134fe2283..5ed6f6d76970 100755
--- a/scripts/livepatch/klp-build
+++ b/scripts/livepatch/klp-build
@@ -570,6 +570,8 @@ copy_orig_objects() {
done
xtrace_restore
+ cp -f "$PWD/vmlinux" "$ORIG_DIR" || die "missing vmlinux"
+
mv -f "$TMP_DIR/build.log" "$ORIG_DIR"
touch "$TIMESTAMP"
}
diff --git a/tools/objtool/Build b/tools/objtool/Build
index 6b01eaa4e554..4d4705f534a2 100644
--- a/tools/objtool/Build
+++ b/tools/objtool/Build
@@ -10,7 +10,8 @@ objtool-y += klp-symid.o
objtool-y += objtool.o
objtool-$(BUILD_ORC) += orc_gen.o orc_dump.o
-objtool-$(BUILD_KLP) += builtin-klp.o klp-diff.o klp-post-link.o
+objtool-$(BUILD_KLP) += builtin-klp.o klp-diff.o klp-post-link.o \
+ klp-sympos.o
objtool-y += libstring.o
objtool-y += libctype.o
diff --git a/tools/objtool/include/objtool/klp.h b/tools/objtool/include/objtool/klp.h
index b23ddde8b47d..67e62290f4f4 100644
--- a/tools/objtool/include/objtool/klp.h
+++ b/tools/objtool/include/objtool/klp.h
@@ -45,9 +45,13 @@ struct klp_symid {
};
struct objtool_file;
+struct elf;
+struct symbol;
int klp_create_symid_sections(struct objtool_file *file);
+int klp_sympos_init(struct elf *orig);
+unsigned long klp_find_sympos(struct elf *elf, struct symbol *sym);
int cmd_klp_diff(int argc, const char **argv);
int cmd_klp_post_link(int argc, const char **argv);
diff --git a/tools/objtool/klp-diff.c b/tools/objtool/klp-diff.c
index d9c91539f690..5268bb212737 100644
--- a/tools/objtool/klp-diff.c
+++ b/tools/objtool/klp-diff.c
@@ -601,65 +601,6 @@ static int check_changed_rodata(struct elfs *e)
return ret;
}
-/* "sympos" is used by livepatch to disambiguate duplicate symbol names */
-static unsigned long find_sympos(struct elf *elf, struct symbol *sym)
-{
- bool vmlinux = str_ends_with(objname, "vmlinux.o");
- unsigned long sympos = 0, nr_matches = 0;
- bool has_dup = false;
- struct symbol *s;
-
- if (sym->bind != STB_LOCAL)
- return 0;
-
- if (vmlinux && sym->type == STT_FUNC) {
- /*
- * HACK: Unfortunately, symbol ordering can differ between
- * vmlinux.o and vmlinux due to the linker script emitting
- * .text.unlikely* before .text*. Count .text.unlikely* first.
- *
- * TODO: Disambiguate symbols more reliably (checksums?)
- */
- for_each_sym(elf, s) {
- if (strstarts(s->sec->name, ".text.unlikely") &&
- !strcmp(s->name, sym->name)) {
- nr_matches++;
- if (s == sym)
- sympos = nr_matches;
- else
- has_dup = true;
- }
- }
- for_each_sym(elf, s) {
- if (!strstarts(s->sec->name, ".text.unlikely") &&
- !strcmp(s->name, sym->name)) {
- nr_matches++;
- if (s == sym)
- sympos = nr_matches;
- else
- has_dup = true;
- }
- }
- } else {
- for_each_sym(elf, s) {
- if (!strcmp(s->name, sym->name)) {
- nr_matches++;
- if (s == sym)
- sympos = nr_matches;
- else
- has_dup = true;
- }
- }
- }
-
- if (!sympos) {
- ERROR("can't find sympos for %s", sym->name);
- return ULONG_MAX;
- }
-
- return has_dup ? sympos : 0;
-}
-
static int clone_sym_relocs(struct elfs *e, struct symbol *patched_sym);
static struct symbol *__clone_symbol(struct elf *elf, struct symbol *patched_sym,
@@ -1119,7 +1060,7 @@ static int clone_reloc_klp(struct elfs *e, struct reloc *patched_reloc,
return -1;
sym_orig_name = patched_sym->twin->name;
- sympos = find_sympos(e->orig, patched_sym->twin);
+ sympos = klp_find_sympos(e->orig, patched_sym->twin);
if (sympos == ULONG_MAX)
return -1;
}
@@ -1727,7 +1668,7 @@ static int create_klp_sections(struct elfs *e)
/* klp_func_ext.sympos */
BUILD_BUG_ON(sizeof(sympos) != sizeof_field(struct klp_func_ext, sympos));
- sympos = find_sympos(e->orig, sym->clone->twin);
+ sympos = klp_find_sympos(e->orig, sym->clone->twin);
if (sympos == ULONG_MAX)
return -1;
memcpy(func_data + offsetof(struct klp_func_ext, sympos), &sympos,
@@ -1875,6 +1816,9 @@ int cmd_klp_diff(int argc, const char **argv)
if (!e.orig || !e.patched)
return -1;
+ if (klp_sympos_init(e.orig))
+ return -1;
+
if (read_exports())
return -1;
diff --git a/tools/objtool/klp-sympos.c b/tools/objtool/klp-sympos.c
new file mode 100644
index 000000000000..bbfae516d339
--- /dev/null
+++ b/tools/objtool/klp-sympos.c
@@ -0,0 +1,411 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Compute "sympos", the position used by livepatch to disambiguate
+ * duplicate symbol names in the patched object.
+ */
+#include <stdlib.h>
+#include <string.h>
+#include <fcntl.h>
+
+#include <objtool/objtool.h>
+#include <objtool/warn.h>
+#include <objtool/endianness.h>
+#include <objtool/klp.h>
+
+#include <linux/string.h>
+
+struct vmlinux_sym {
+ struct hlist_node hash;
+ const char *name;
+ u64 addr;
+};
+
+struct vmlinux_symid {
+ struct hlist_node hash;
+ u64 id;
+ u64 addr;
+};
+
+struct vmlinux_o_symid {
+ struct hlist_node hash;
+ u64 id;
+ unsigned int sym_idx;
+};
+
+static DEFINE_HASHTABLE(vmlinux_o_symids, 16);
+
+/*
+ * The original linked kernel, found next to the orig vmlinux.o. Read with raw
+ * libelf rather than elf_open_read(): only the symbol table and the resolved
+ * .klp.symid table are needed, not the (huge) instruction/reloc machinery.
+ *
+ * Both tables are built once by read_orig_vmlinux(). The Elf handle stays
+ * open because the hashed names point into its mmapped string table.
+ */
+static struct {
+ Elf *elf;
+ DECLARE_HASHTABLE(syms, 16); /* name -> address */
+ DECLARE_HASHTABLE(symids, 16); /* .klp.symid id -> address */
+} vmlinux;
+
+/*
+ * Would the symbol be visible to the runtime's kallsyms-based symbol lookup?
+ */
+static bool vmlinux_sym_in_kallsyms(Elf *elf, GElf_Sym *sym)
+{
+ unsigned int type = GELF_ST_TYPE(sym->st_info);
+ GElf_Shdr shdr;
+ Elf_Scn *scn;
+
+ if (sym->st_shndx == SHN_UNDEF || sym->st_shndx >= SHN_LORESERVE)
+ return false;
+
+ if (type == STT_SECTION || type == STT_FILE)
+ return false;
+
+ scn = elf_getscn(elf, sym->st_shndx);
+ if (!scn || !gelf_getshdr(scn, &shdr))
+ return false;
+
+ return shdr.sh_flags & SHF_ALLOC;
+}
+
+static int read_orig_vmlinux(const char *filename)
+{
+ size_t shstrndx, nr_syms = 0, nr_symids = 0, strtab_idx = 0;
+ Elf_Data *symtab_data = NULL, *symid_data = NULL;
+ struct klp_symid *symids;
+ Elf_Scn *scn = NULL;
+ GElf_Ehdr ehdr;
+ int fd;
+
+ fd = open(filename, O_RDONLY);
+ if (fd == -1) {
+ ERROR_GLIBC("can't open '%s'", filename);
+ return -1;
+ }
+
+ if (elf_version(EV_CURRENT) == EV_NONE) {
+ ERROR_ELF("elf_version");
+ return -1;
+ }
+
+ vmlinux.elf = elf_begin(fd, ELF_C_READ_MMAP, NULL);
+ if (!vmlinux.elf) {
+ ERROR_ELF("elf_begin");
+ return -1;
+ }
+
+ if (!gelf_getehdr(vmlinux.elf, &ehdr)) {
+ ERROR_ELF("gelf_getehdr");
+ return -1;
+ }
+
+ if (elf_getshdrstrndx(vmlinux.elf, &shstrndx)) {
+ ERROR_ELF("elf_getshdrstrndx");
+ return -1;
+ }
+
+ while ((scn = elf_nextscn(vmlinux.elf, scn))) {
+ const char *name;
+ GElf_Shdr shdr;
+
+ if (!gelf_getshdr(scn, &shdr)) {
+ ERROR_ELF("gelf_getshdr");
+ return -1;
+ }
+
+ if (shdr.sh_type == SHT_SYMTAB) {
+ symtab_data = elf_getdata(scn, NULL);
+ if (!symtab_data) {
+ ERROR_ELF("elf_getdata");
+ return -1;
+ }
+ nr_syms = shdr.sh_size / shdr.sh_entsize;
+ strtab_idx = shdr.sh_link;
+ continue;
+ }
+
+ name = elf_strptr(vmlinux.elf, shstrndx, shdr.sh_name);
+ if (name && !strcmp(name, KLP_SYMID_SEC)) {
+ if (shdr.sh_size % sizeof(struct klp_symid)) {
+ ERROR("%s: %s: struct klp_symid size mismatch",
+ filename, KLP_SYMID_SEC);
+ return -1;
+ }
+ symid_data = elf_getdata(scn, NULL);
+ if (!symid_data) {
+ ERROR_ELF("elf_getdata");
+ return -1;
+ }
+ nr_symids = shdr.sh_size / sizeof(struct klp_symid);
+ }
+ }
+
+ if (!symtab_data) {
+ ERROR("%s: missing symbol table", filename);
+ return -1;
+ }
+
+ if (!symid_data) {
+ ERROR("%s: missing %s section, kernel not built with CONFIG_KLP_BUILD?",
+ filename, KLP_SYMID_SEC);
+ return -1;
+ }
+
+ for (size_t i = 0; i < nr_syms; i++) {
+ struct vmlinux_sym *vsym;
+ const char *name;
+ GElf_Sym s;
+
+ if (!gelf_getsym(symtab_data, i, &s)) {
+ ERROR_ELF("gelf_getsym");
+ return -1;
+ }
+
+ if (!vmlinux_sym_in_kallsyms(vmlinux.elf, &s))
+ continue;
+
+ name = elf_strptr(vmlinux.elf, strtab_idx, s.st_name);
+ if (!name)
+ continue;
+
+ vsym = calloc(1, sizeof(*vsym));
+ if (!vsym) {
+ ERROR_GLIBC("calloc");
+ return -1;
+ }
+
+ vsym->name = name;
+ vsym->addr = s.st_value;
+ hash_add(vmlinux.syms, &vsym->hash, str_hash(name));
+ }
+
+ symids = symid_data->d_buf;
+
+ for (size_t i = 0; i < nr_symids; i++) {
+ struct vmlinux_symid *vsymid;
+
+ vsymid = calloc(1, sizeof(*vsymid));
+ if (!vsymid) {
+ ERROR_GLIBC("calloc");
+ return -1;
+ }
+
+ vsymid->id = __bswap_if_needed(&ehdr, symids[i].id);
+ vsymid->addr = __bswap_if_needed(&ehdr, symids[i].addr);
+ hash_add(vmlinux.symids, &vsymid->hash, vsymid->id);
+ }
+
+ /* the fd and Elf handle stay open, the hashed names live in the mmap */
+ return 0;
+}
+
+/*
+ * Read the orig vmlinux.o's .klp.symid table, an array of entries whose 'addr'
+ * fields have relocs to the symbols they describe.
+ */
+static int read_vmlinux_o_symids(struct elf *vmlinux_o)
+{
+ struct section *sec;
+
+ for_each_sec(vmlinux_o, sec) {
+ unsigned long nr;
+
+ if (strcmp(sec->name, KLP_SYMID_SEC))
+ continue;
+
+ if (sec_size(sec) % sizeof(struct klp_symid)) {
+ ERROR("%s: %s: struct klp_symid size mismatch",
+ vmlinux_o->name, KLP_SYMID_SEC);
+ return -1;
+ }
+
+ nr = sec_size(sec) / sizeof(struct klp_symid);
+
+ for (unsigned long i = 0; i < nr; i++) {
+ unsigned long offset = i * sizeof(struct klp_symid);
+ struct vmlinux_o_symid *entry;
+ struct klp_symid *symid;
+ struct reloc *reloc;
+
+ entry = calloc(1, sizeof(*entry));
+ if (!entry) {
+ ERROR_GLIBC("calloc");
+ return -1;
+ }
+
+ symid = sec->data->d_buf + offset;
+ entry->id = bswap_if_needed(vmlinux_o, symid->id);
+
+ reloc = find_reloc_by_dest(vmlinux_o, sec,
+ offset + offsetof(struct klp_symid, addr));
+ if (!reloc) {
+ ERROR("%s: missing reloc for %s entry",
+ vmlinux_o->name, KLP_SYMID_SEC);
+ return -1;
+ }
+ entry->sym_idx = reloc->sym->idx;
+
+ hash_add(vmlinux_o_symids, &entry->hash, entry->sym_idx);
+ }
+ }
+
+ return 0;
+}
+
+int klp_sympos_init(struct elf *orig)
+{
+ char *filename;
+ int ret;
+
+ if (!str_ends_with(objname, "vmlinux.o"))
+ return 0;
+
+ if (read_vmlinux_o_symids(orig))
+ return -1;
+
+ filename = strndup(objname, strlen(objname) - 2);
+ if (!filename) {
+ ERROR_GLIBC("strndup");
+ return -1;
+ }
+
+ ret = read_orig_vmlinux(filename);
+ free(filename);
+
+ return ret;
+}
+
+/* Find the symbol's id in the orig vmlinux.o's .klp.symid table */
+static int find_vmlinux_o_symid(struct symbol *sym, u64 *id)
+{
+ struct vmlinux_o_symid *entry;
+
+ hash_for_each_possible(vmlinux_o_symids, entry, hash, sym->idx) {
+ if (entry->sym_idx == sym->idx) {
+ *id = entry->id;
+ return 0;
+ }
+ }
+
+ ERROR("no %s entry for symbol %s in orig vmlinux.o", KLP_SYMID_SEC,
+ sym->name);
+ return -1;
+}
+
+/* Find the symbol's final address in the orig vmlinux's .klp.symid table */
+static int find_vmlinux_symid_addr(u64 id, u64 *addr)
+{
+ struct vmlinux_symid *symid;
+
+ hash_for_each_possible(vmlinux.symids, symid, hash, id) {
+ if (symid->id == id) {
+ *addr = symid->addr;
+ return 0;
+ }
+ }
+
+ return -1;
+}
+
+/*
+ * Find the sympos of a vmlinux-local symbol by ranking its final address
+ * among the duplicately named symbols in the linked orig vmlinux, replicating
+ * the order in which kallsyms_on_each_match_symbol() counts them.
+ */
+static unsigned long find_vmlinux_sympos(struct symbol *sym)
+{
+ unsigned long nr_matches = 0, sympos = 1;
+ u32 key = str_hash(sym->name);
+ struct vmlinux_sym *vsym;
+ bool found = false;
+ u64 id, addr;
+
+ hash_for_each_possible(vmlinux.syms, vsym, hash, key)
+ if (!strcmp(vsym->name, sym->name))
+ nr_matches++;
+
+ if (!nr_matches) {
+ ERROR("can't find symbol %s in orig vmlinux", sym->name);
+ return ULONG_MAX;
+ }
+
+ /*
+ * Unique symbols don't need disambiguating. They also have no
+ * .klp.symid entry, which is only emitted for names duplicated in
+ * vmlinux.o, so the lookups below would fail.
+ */
+ if (nr_matches == 1)
+ return 0;
+
+ if (find_vmlinux_o_symid(sym, &id))
+ return ULONG_MAX;
+
+ if (find_vmlinux_symid_addr(id, &addr)) {
+ ERROR("no %s entry for symbol %s in orig vmlinux", KLP_SYMID_SEC,
+ sym->name);
+ return ULONG_MAX;
+ }
+
+ hash_for_each_possible(vmlinux.syms, vsym, hash, key) {
+ if (strcmp(vsym->name, sym->name))
+ continue;
+
+ if (vsym->addr < addr)
+ sympos++;
+ else if (vsym->addr == addr)
+ found = true;
+ }
+
+ if (!found) {
+ ERROR("%s address mismatch for symbol %s, stale orig vmlinux?",
+ KLP_SYMID_SEC, sym->name);
+ return ULONG_MAX;
+ }
+
+ return sympos;
+}
+
+/*
+ * "sympos" is used by livepatch to disambiguate duplicate symbol names.
+ */
+unsigned long klp_find_sympos(struct elf *elf, struct symbol *sym)
+{
+ unsigned long sympos = 0, nr_matches = 0;
+ bool has_dup = false;
+ struct symbol *s;
+
+ 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
+ * the .klp.symid table.
+ */
+ if (vmlinux.elf)
+ return find_vmlinux_sympos(sym);
+
+ /*
+ * modules: the final .ko preserves symbol table order, so a
+ * symtab-order count here matches the runtime count done by
+ * module_kallsyms_on_each_symbol().
+ */
+ for_each_sym(elf, s) {
+ if (!strcmp(s->name, sym->name)) {
+ nr_matches++;
+ if (s == sym)
+ sympos = nr_matches;
+ else
+ has_dup = true;
+ }
+ }
+
+ if (!sympos) {
+ ERROR("can't find sympos for %s", sym->name);
+ return ULONG_MAX;
+ }
+
+ return has_dup ? sympos : 0;
+}
--
2.56.1
_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel
^ permalink raw reply [flat|nested] 31+ messages in thread* [Devel] [PATCH hci-8.2 v2 24/28] ms/objtool/klp: Fix size of empty special section entries
2026-11-26 5:06 [Devel] [PATCH hci-8.2 v2 00/28] ms/objtool: fix silently wrong livepatches by klp-build Eva Kurchatova
` (22 preceding siblings ...)
2026-11-26 5:07 ` [Devel] [PATCH hci-8.2 v2 23/28] ms/objtool/klp: Fix symbol resolution for duplicate data symbols Eva Kurchatova
@ 2026-11-26 5:07 ` Eva Kurchatova
2026-11-26 5:07 ` [Devel] [PATCH hci-8.2 v2 25/28] ms/objtool/klp: Fix vmlinux .klp.symid link error for .no_trim_symbol symbols Eva Kurchatova
` (5 subsequent siblings)
29 siblings, 0 replies; 31+ messages in thread
From: Eva Kurchatova @ 2026-11-26 5:07 UTC (permalink / raw)
To: khorenko, vladimir.riabchun; +Cc: devel
From: Josh Poimboeuf <jpoimboe@kernel.org>
create_fake_symbols() sizes each ANNOTATE_DATA_SPECIAL entry from the
offset of the next annotation, falling back to the end of the section
for the last entry. But the last entry is detected by a zero size,
which also happens for an *empty* entry: ALTERNATIVE(oldinstr, "", ft)
still annotates its zero-length replacement, at the same offset as the
next entry's annotation.
So every empty replacement gets a fake symbol spanning the entire rest
of .altinstr_replacement. That's harmless today only because
find_symbol_containing() picks the smaller of two overlapping symbols.
Track whether a next annotation was found rather than inferring it from
the size. A zero-length fake symbol is fine: find_symbol_containing()
skips those, so the properly sized symbol at the same offset still wins.
Fixes: dd590d4d57eb ("objtool/klp: Introduce klp diff subcommand for diffing object files")
Acked-by: Song Liu <song@kernel.org>
Acked-by: Joe Lawrence <joe.lawrence@redhat.com>
Link: https://patch.msgid.link/913e691c5009397df832c7c9a18cd5cf71b42737.1786138493.git.jpoimboe@kernel.org
Signed-off-by: Josh Poimboeuf <jpoimboe@kernel.org>
(cherry picked from commit 1ea786097cd79522b76cbd41beeb8f84ef3a76f4)
https://virtuozzo.atlassian.net/browse/VSTOR-156924
Feature: klp-build support
Signed-off-by: Eva Kurchatova <eva.kurchatova@virtuozzo.com>
---
tools/objtool/klp-diff.c | 13 +++++++++++--
1 file changed, 11 insertions(+), 2 deletions(-)
diff --git a/tools/objtool/klp-diff.c b/tools/objtool/klp-diff.c
index 5268bb212737..694790b8639c 100644
--- a/tools/objtool/klp-diff.c
+++ b/tools/objtool/klp-diff.c
@@ -1340,13 +1340,17 @@ static int create_fake_symbols(struct elf *elf)
for_each_reloc(sec->rsec, reloc) {
unsigned long offset, size;
struct reloc *next_reloc;
+ bool last = true;
if (annotype(elf, sec, reloc) != ANNOTYPE_DATA_SPECIAL)
continue;
offset = reloc_addend(reloc);
- size = 0;
+ /*
+ * Find the start of the next entry so the fake symbol size can
+ * be calculated.
+ */
next_reloc = reloc;
for_each_reloc_continue(sec->rsec, next_reloc) {
if (annotype(elf, sec, next_reloc) != ANNOTYPE_DATA_SPECIAL ||
@@ -1354,10 +1358,15 @@ static int create_fake_symbols(struct elf *elf)
continue;
size = reloc_addend(next_reloc) - offset;
+ last = false;
break;
}
- if (!size)
+ /*
+ * If no next entry found, this is the last entry, so its size
+ * is from the current offset to the end of the section.
+ */
+ if (last)
size = sec_size(reloc->sym->sec) - offset;
if (create_fake_symbol(elf, reloc->sym->sec, offset, size))
--
2.56.1
_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel
^ permalink raw reply [flat|nested] 31+ messages in thread* [Devel] [PATCH hci-8.2 v2 25/28] ms/objtool/klp: Fix vmlinux .klp.symid link error for .no_trim_symbol symbols
2026-11-26 5:06 [Devel] [PATCH hci-8.2 v2 00/28] ms/objtool: fix silently wrong livepatches by klp-build Eva Kurchatova
` (23 preceding siblings ...)
2026-11-26 5:07 ` [Devel] [PATCH hci-8.2 v2 24/28] ms/objtool/klp: Fix size of empty special section entries Eva Kurchatova
@ 2026-11-26 5:07 ` Eva Kurchatova
2026-11-26 5:07 ` [Devel] [PATCH hci-8.2 v2 26/28] ms/objtool/klp: Ignore replacement offset of empty x86 alternatives Eva Kurchatova
` (4 subsequent siblings)
29 siblings, 0 replies; 31+ messages in thread
From: Eva Kurchatova @ 2026-11-26 5:07 UTC (permalink / raw)
To: khorenko, vladimir.riabchun; +Cc: devel
From: Josh Poimboeuf <jpoimboe@kernel.org>
Testing klp-build with arm64 produced the following linker error during
the original kernel build:
`__notrim.1' referenced in section `.klp.symid' of vmlinux.o: defined in discarded section `.no_trim_symbol' of vmlinux.o
symbol_get() puts a static __notrim[] in .no_trim_symbol, which GCC
names __notrim.1, __notrim.2, etc. Two or more built-in translation
units calling symbol_get() thus produce duplicate names, resulting in
corresponding .klp.symid references which trigger the above error.
Add .no_trim_symbol to the discarded section list so its symbols don't
get symids.
Note this issue is not specific to arm64: it just needs two built-in
symbol_get() callers. arm64 trips over it easily because it has KVM
always compiled in vmlinux, whereas on x86 it's typically a module.
Fixes: 029223d30162 ("objtool/klp: Add .klp.symid for sympos disambiguation")
Acked-by: Song Liu <song@kernel.org>
Acked-by: Joe Lawrence <joe.lawrence@redhat.com>
Link: https://patch.msgid.link/5a3cc4cded743167dd0878220201d80f7e48e5d7.1786138493.git.jpoimboe@kernel.org
Signed-off-by: Josh Poimboeuf <jpoimboe@kernel.org>
(cherry picked from commit 3a83d9cd3d0e66c0ef99ce84f972ed26f50c990f)
https://virtuozzo.atlassian.net/browse/VSTOR-156924
Feature: klp-build support
Signed-off-by: Eva Kurchatova <eva.kurchatova@virtuozzo.com>
---
tools/objtool/klp-symid.c | 1 +
1 file changed, 1 insertion(+)
diff --git a/tools/objtool/klp-symid.c b/tools/objtool/klp-symid.c
index cf188cdfa607..21d8708013ab 100644
--- a/tools/objtool/klp-symid.c
+++ b/tools/objtool/klp-symid.c
@@ -31,6 +31,7 @@
static const char * const discarded_secs[] = {
".discard",
".modinfo",
+ ".no_trim_symbol",
"__tracepoint_check",
};
--
2.56.1
_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel
^ permalink raw reply [flat|nested] 31+ messages in thread* [Devel] [PATCH hci-8.2 v2 26/28] ms/objtool/klp: Ignore replacement offset of empty x86 alternatives
2026-11-26 5:06 [Devel] [PATCH hci-8.2 v2 00/28] ms/objtool: fix silently wrong livepatches by klp-build Eva Kurchatova
` (24 preceding siblings ...)
2026-11-26 5:07 ` [Devel] [PATCH hci-8.2 v2 25/28] ms/objtool/klp: Fix vmlinux .klp.symid link error for .no_trim_symbol symbols Eva Kurchatova
@ 2026-11-26 5:07 ` Eva Kurchatova
2026-11-26 5:07 ` [Devel] [PATCH hci-8.2 v2 27/28] ms/objtool/klp: Allow new references to module exports Eva Kurchatova
` (3 subsequent siblings)
29 siblings, 0 replies; 31+ messages in thread
From: Eva Kurchatova @ 2026-11-26 5:07 UTC (permalink / raw)
To: khorenko, vladimir.riabchun; +Cc: devel
From: Josh Poimboeuf <jpoimboe@kernel.org>
An x86 alternative with an empty replacement, e.g. the second entry of
ALTERNATIVE_2("orig", "repl", ft1, "", ft2)
has a replacementlen of zero. Its replacement offset still gets a
relocation, but the label it points at is the end of the previous
replacement, which is also the beginning of the *next* alternative's
replacement. The value is meaningless; get_alt_entry() already ignores
it for that reason.
klp diff doesn't ignore it. When such an alternative belongs to a
changed function, cloning its relocations drags in the unrelated
neighboring replacement, along with everything that replacement
references. On an x86 clang/lto build an empty alternative in
meminfo_proc_show() pulled in the replacement of an alternative in
proc_kcore_init(), silently emitting a klp relocation against init text
which has long since been freed by the time the patch is applied.
Add arch_alt_ignore_new_reloc() and skip such relocations when cloning.
This has to be arch specific: on arm64 a zero-length replacement instead
identifies an alternative callback, whose replacement offset points at
the callback function and must be preserved.
Fixes: dd590d4d57eb ("objtool/klp: Introduce klp diff subcommand for diffing object files")
Acked-by: Song Liu <song@kernel.org>
Acked-by: Joe Lawrence <joe.lawrence@redhat.com>
Link: https://patch.msgid.link/7a885b70974795c3417f3358869e62aafd4ef783.1786138493.git.jpoimboe@kernel.org
Signed-off-by: Josh Poimboeuf <jpoimboe@kernel.org>
(cherry picked from commit 636f230ce21e2730072c58280f1fa0bed4a00eed)
https://virtuozzo.atlassian.net/browse/VSTOR-156924
Feature: klp-build support
Signed-off-by: Eva Kurchatova <eva.kurchatova@virtuozzo.com>
---
tools/objtool/arch/x86/special.c | 27 +++++++++++++++++++++++++
tools/objtool/include/objtool/special.h | 7 +++++++
tools/objtool/klp-diff.c | 6 +++++-
3 files changed, 39 insertions(+), 1 deletion(-)
diff --git a/tools/objtool/arch/x86/special.c b/tools/objtool/arch/x86/special.c
index 09300761f108..161650ac703d 100644
--- a/tools/objtool/arch/x86/special.c
+++ b/tools/objtool/arch/x86/special.c
@@ -1,10 +1,37 @@
// SPDX-License-Identifier: GPL-2.0-or-later
#include <string.h>
+#include <arch/special.h>
#include <objtool/special.h>
#include <objtool/builtin.h>
#include <objtool/warn.h>
+/*
+ * An alternative with an empty replacement, e.g. the second entry of
+ *
+ * ALTERNATIVE_2("orig", "repl", ft1, "", ft2)
+ *
+ * still gets a relocation for its replacement offset. But the label it points
+ * at is the end of the previous entry's replacement, which is also the
+ * beginning of the *next* entry's replacement. The value is meaningless: it's
+ * only ever used with a length of zero.
+ */
+bool arch_alt_ignore_new_reloc(struct section *sec, unsigned long offset)
+{
+ unsigned long entry_off;
+
+ if (strcmp(sec->name, ".altinstructions"))
+ return false;
+
+ entry_off = offset - (offset % ALT_ENTRY_SIZE);
+
+ if (offset - entry_off != ALT_NEW_OFFSET)
+ return false;
+
+ return !*(unsigned char *)(sec->data->d_buf + entry_off +
+ ALT_NEW_LEN_OFFSET);
+}
+
void arch_handle_alternative(struct special_alt *alt)
{
static struct special_alt *group, *prev;
diff --git a/tools/objtool/include/objtool/special.h b/tools/objtool/include/objtool/special.h
index 72d09c0adf1a..39ce3fe557ba 100644
--- a/tools/objtool/include/objtool/special.h
+++ b/tools/objtool/include/objtool/special.h
@@ -32,6 +32,13 @@ int special_get_alts(struct elf *elf, struct list_head *alts);
void arch_handle_alternative(struct special_alt *alt);
+/*
+ * Should the reloc at @offset -- the "new" (replacement) field of a special
+ * section group entry -- be ignored? The meaning of a zero-length replacement
+ * is arch specific, so the arch decides.
+ */
+bool arch_alt_ignore_new_reloc(struct section *sec, unsigned long offset);
+
bool arch_support_alt_relocation(struct special_alt *special_alt,
struct instruction *insn,
struct reloc *reloc);
diff --git a/tools/objtool/klp-diff.c b/tools/objtool/klp-diff.c
index 694790b8639c..7eac2ef0f73d 100644
--- a/tools/objtool/klp-diff.c
+++ b/tools/objtool/klp-diff.c
@@ -12,7 +12,7 @@
#include <objtool/arch.h>
#include <objtool/klp.h>
#include <objtool/util.h>
-#include <arch/special.h>
+#include <objtool/special.h>
#include <linux/align.h>
#include <linux/objtool_types.h>
@@ -1250,6 +1250,10 @@ static int clone_sym_relocs(struct elfs *e, struct symbol *patched_sym)
!strcmp(patched_reloc->sym->sec->name, ".altinstr_aux"))
continue;
+ if (arch_alt_ignore_new_reloc(patched_sym->sec,
+ reloc_offset(patched_reloc)))
+ continue;
+
ret = convert_reloc_sym(e->patched, patched_reloc);
if (ret < 0) {
ERROR_FUNC(patched_rsec->base, reloc_offset(patched_reloc),
--
2.56.1
_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel
^ permalink raw reply [flat|nested] 31+ messages in thread* [Devel] [PATCH hci-8.2 v2 27/28] ms/objtool/klp: Allow new references to module exports
2026-11-26 5:06 [Devel] [PATCH hci-8.2 v2 00/28] ms/objtool: fix silently wrong livepatches by klp-build Eva Kurchatova
` (25 preceding siblings ...)
2026-11-26 5:07 ` [Devel] [PATCH hci-8.2 v2 26/28] ms/objtool/klp: Ignore replacement offset of empty x86 alternatives Eva Kurchatova
@ 2026-11-26 5:07 ` Eva Kurchatova
2026-11-26 5:07 ` [Devel] [PATCH hci-8.2 v2 28/28] ms/objtool/klp: Fix vmlinux .klp.symid link error for .exitcall.exit symbols Eva Kurchatova
` (2 subsequent siblings)
29 siblings, 0 replies; 31+ messages in thread
From: Eva Kurchatova @ 2026-11-26 5:07 UTC (permalink / raw)
To: khorenko, vladimir.riabchun; +Cc: devel
From: Joe Lawrence <joe.lawrence@redhat.com>
klp_reloc_needed() returns true for module exports to support
late-module patching. However, clone_reloc_klp() unconditionally
rejects symbols without a twin (i.e., new references added by the
patch), even when the symbol is a known export from Module.symvers.
Relax the check: allow new references to exported symbols by only
erroring on !twin when there is no export. The export metadata from
Module.symvers provides sufficient context to emit the klp-relocation
without a twin.
For a module export that isn't sufficient on its own though, as the
resulting klp relocation will only be resolved at patch-enable time if
the exporting module is loaded.
If the original (unpatched) module already depends on the exporting
module, the dependency is safe: the module loader ensures the dependency
is satisfied before the patched module can be loaded, so the
klp relocation target will exist.
However, if the patch introduces a reference to a module that the
original doesn't depend on, there is no such guarantee. The exporting
module could be absent or could be unloaded at any time, leading to a
relocation failure or use-after-free.
So also add a build-time check: when a new symbol reference (no twin)
targets a module export, verify that the original module already has at
least one UNDEF symbol resolving to that same exporting module. If not,
error out with a diagnostic message.
Signed-off-by: Joe Lawrence <joe.lawrence@redhat.com>
Acked-by: Song Liu <song@kernel.org>
Acked-by: Joe Lawrence <joe.lawrence@redhat.com>
Link: https://patch.msgid.link/e0d725acb4774747f0e271308b4ca33daae2d5db.1786138493.git.jpoimboe@kernel.org
Signed-off-by: Josh Poimboeuf <jpoimboe@kernel.org>
(cherry picked from commit 72d76d0c18ebd40f6a32df875ff40528564849c1)
https://virtuozzo.atlassian.net/browse/VSTOR-156924
Feature: klp-build support
Signed-off-by: Eva Kurchatova <eva.kurchatova@virtuozzo.com>
---
tools/objtool/klp-diff.c | 35 +++++++++++++++++++++++++++++++++--
1 file changed, 33 insertions(+), 2 deletions(-)
diff --git a/tools/objtool/klp-diff.c b/tools/objtool/klp-diff.c
index 7eac2ef0f73d..066dbe69c709 100644
--- a/tools/objtool/klp-diff.c
+++ b/tools/objtool/klp-diff.c
@@ -1001,6 +1001,28 @@ static int convert_reloc_sym(struct elf *elf, struct reloc *reloc)
return convert_reloc_secsym_to_sym(elf, reloc);
}
+/*
+ * Check if the original module already has a dependency on dep_mod, i.e. it
+ * already references at least one export from that module.
+ */
+static bool has_module_dep(struct elfs *e, const char *dep_mod)
+{
+ struct symbol *sym;
+
+ for_each_sym(e->orig, sym) {
+ struct export *exp;
+
+ if (!is_undef_sym(sym) || is_weak_sym(sym))
+ continue;
+
+ exp = find_export(sym);
+ if (exp && !strcmp(exp->mod, dep_mod))
+ return true;
+ }
+
+ return false;
+}
+
/*
* Convert a regular relocation to a klp relocation (sort of).
*/
@@ -1019,8 +1041,17 @@ static int clone_reloc_klp(struct elfs *e, struct reloc *patched_reloc,
unsigned long sympos;
if (!patched_sym->twin) {
- ERROR("unexpected klp reloc for new symbol %s", patched_sym->name);
- return -1;
+ if (!export) {
+ ERROR("unexpected klp reloc for new symbol %s", patched_sym->name);
+ return -1;
+ }
+
+ if (strcmp(export->mod, "vmlinux") &&
+ !has_module_dep(e, export->mod)) {
+ ERROR("%s: new reference to %s (exported by %s) would create an undeclared module dependency",
+ patched_sym->name, export->sym, export->mod);
+ return -1;
+ }
}
/*
--
2.56.1
_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel
^ permalink raw reply [flat|nested] 31+ messages in thread* [Devel] [PATCH hci-8.2 v2 28/28] ms/objtool/klp: Fix vmlinux .klp.symid link error for .exitcall.exit symbols
2026-11-26 5:06 [Devel] [PATCH hci-8.2 v2 00/28] ms/objtool: fix silently wrong livepatches by klp-build Eva Kurchatova
` (26 preceding siblings ...)
2026-11-26 5:07 ` [Devel] [PATCH hci-8.2 v2 27/28] ms/objtool/klp: Allow new references to module exports Eva Kurchatova
@ 2026-11-26 5:07 ` Eva Kurchatova
2026-11-26 6:15 ` [Devel] [PATCH hci-8.2 v2 00/28] ms/objtool: fix silently wrong livepatches by klp-build Eva Kurchatova (Virtuozzo)
2026-11-26 16:34 ` [Devel] [PATCH hci-8.2 v3 " Konstantin Khorenko
29 siblings, 0 replies; 31+ messages in thread
From: Eva Kurchatova @ 2026-11-26 5:07 UTC (permalink / raw)
To: khorenko, vladimir.riabchun; +Cc: devel
From: Puranjay Mohan <puranjay@kernel.org>
Building a kernel via klp-build fails to link:
`__exitcall_aes_mod_exit' referenced in section `.klp.symid' of vmlinux.o: defined in discarded section `.exitcall.exit' of vmlinux.o
`__exitcall_dax_exit' referenced in section `.klp.symid' of vmlinux.o: defined in discarded section `.exitcall.exit' of vmlinux.o
`__exitcall_hid_exit' referenced in section `.klp.symid' of vmlinux.o: defined in discarded section `.exitcall.exit' of vmlinux.o
`__exitcall_usb_serial_module_exit' referenced in section `.klp.symid' of vmlinux.o: defined in discarded section `.exitcall.exit' of vmlinux.o
module_exit() on a built-in emits a static __exitcall_$fn pointer into
.exitcall.exit, which vmlinux.lds.h discards unconditionally via
EXIT_CALL. When two built-in translation units define a module_exit()
function of the same name, the resulting local symbols collide,
symid_needed() sees a duplicate and emits a .klp.symid entry for each,
referencing symbols the linker then throws away.
Same-named module_exit() functions are not rare:
dax_exit drivers/dax/device.c, drivers/dax/fsdev.c
hid_exit drivers/hid/hid-core.c, drivers/hid/usbhid/hid-core.c
aes_mod_exit arch/arm64/crypto/aes-ce-ccm-glue.c, lib/crypto/aes.c
usb_serial_module_exit module_usb_serial_driver() expands to this fixed
name in each of its ~49 users
The last one makes the collision structural rather than accidental: any
kernel with two built-in USB serial drivers has it. This is not arch
specific either; it only requires the objects to be built in rather than
modular, which is why a monolithic config trips it while a typical
distro config does not.
Add .exitcall.exit to the discarded section list so its symbols don't get
symids.
This is the same failure mode as "objtool/klp: Fix vmlinux .klp.symid
link error for .no_trim_symbol symbols", for another unconditionally
discarded allocated section.
Fixes: 029223d30162 ("objtool/klp: Add .klp.symid for sympos disambiguation")
Signed-off-by: Puranjay Mohan <puranjay@kernel.org>
Link: https://patch.msgid.link/m24igzlbxf.fsf@kernel.org
Signed-off-by: Josh Poimboeuf <jpoimboe@kernel.org>
(cherry picked from commit 6656cf1e975aa152c082012b820efffaebd49979)
https://virtuozzo.atlassian.net/browse/VSTOR-156924
Feature: klp-build support
Signed-off-by: Eva Kurchatova <eva.kurchatova@virtuozzo.com>
---
tools/objtool/klp-symid.c | 1 +
1 file changed, 1 insertion(+)
diff --git a/tools/objtool/klp-symid.c b/tools/objtool/klp-symid.c
index 21d8708013ab..b19f76dff13e 100644
--- a/tools/objtool/klp-symid.c
+++ b/tools/objtool/klp-symid.c
@@ -30,6 +30,7 @@
static const char * const discarded_secs[] = {
".discard",
+ ".exitcall.exit",
".modinfo",
".no_trim_symbol",
"__tracepoint_check",
--
2.56.1
_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel
^ permalink raw reply [flat|nested] 31+ messages in thread* Re: [Devel] [PATCH hci-8.2 v2 00/28] ms/objtool: fix silently wrong livepatches by klp-build
2026-11-26 5:06 [Devel] [PATCH hci-8.2 v2 00/28] ms/objtool: fix silently wrong livepatches by klp-build Eva Kurchatova
` (27 preceding siblings ...)
2026-11-26 5:07 ` [Devel] [PATCH hci-8.2 v2 28/28] ms/objtool/klp: Fix vmlinux .klp.symid link error for .exitcall.exit symbols Eva Kurchatova
@ 2026-11-26 6:15 ` Eva Kurchatova (Virtuozzo)
2026-11-26 16:34 ` [Devel] [PATCH hci-8.2 v3 " Konstantin Khorenko
29 siblings, 0 replies; 31+ messages in thread
From: Eva Kurchatova (Virtuozzo) @ 2026-11-26 6:15 UTC (permalink / raw)
To: khorenko, vladimir.riabchun; +Cc: devel
On 11/26/26 07:06, Eva Kurchatova wrote:
> Patchset for tasks:
> https://virtuozzo.atlassian.net/browse/VSTOR-128510
> https://virtuozzo.atlassian.net/browse/VSTOR-156924
> https://virtuozzo.atlassian.net/browse/VSTOR-157091
>
> klp-build in 26.x is the RHEL 10.2 import. Mainline has fixed many bugs
> in it since, and the objtool in it fails 31 of 48 mainline tests.
>
> This series takes only the mainline fixes for bugs that a vz10 build
> (x86_64, gcc) can hit and that make klp-build produce a livepatch which
> builds but is broken, or refuse a common patch. Patches 1-19 are for
> VSTOR-128510, patches 20-28 for VSTOR-156924 (1-19 are prerequisites):
>
> - pointers to const data compared against a private copy (13)
> - klp relocations to exports of another module put in a section the
> livepatch loader never applies, e.g. jump into garbage (16-18)
> - static symbols bound to an export of the same name (19)
> - strings and data cloned at the wrong offset (3, 4)
> - module static keys not rejected at build time (1, 6)
> - WARN_ONCE() flags correlated between unrelated call sites (5)
> - functions using pr_debug() in modules can't be patched (2); objtool
> crashes on empty sections (8) and corrupts a relocation (10)
> - symbol positions of the same-named static symbols counted in same
> order as vmlinux.o, which the final link reorders (22, 23, 25, 28)
> - match the .cold part of a function by name (20)
> - fix NULL dereference when printing a code symbol (21)
> - fix x86 alternative replacement (24, 26)
> - fix new references to an export of another module (27)
>
> Patches 7, 9, 11 and 12 are mainline prerequisites of 13. The picks
> match mainline line for line, except 22 and 23, which have a rebase
> note.
>
> Since patch 13, a named const object is correlated, and the livepatch
> references the original. Patches 14 and 15 are original. Patch 15 makes
> klp diff fail when a correlated const object changes, replacing a much
> larger upstream correlation rewrite, 6016dd33a10a, too large to take.
> Patch 14 fixes an existing mainline problem, and fails klp diff if the
> section would be cloned due to one of its objects, or size-less one.
> See VSTOR-157091 for details, patch 14 to be sent mainline too.
>
> Patch 22 makes klp-build builds emit a .klp.symid table, a section that
> is not allocated; kernels built without klp-build do not change.
>
> Tested with klp-build on rh10-6.12.0-211.75.1.26.4.vz10 in QEMU.
>
> Changes in v3:
> - Merged the VSTOR-156924 proposal as patches 20-28, not posted before.
> - New patch 14: Check .rodata section labels and prevent object cloning.
> - Patch 15: Check an object which the patch makes const, compare only
> the section of a target that no symbol contains, lowercase subject.
> - Cover letter: Renumber patches, update covered tasks.
>
> Link to v2: http://lore.virtuozzo.com/kernel/179560292324.21954.16900360366849940769.b4-review@b4/T/#t
>
> Changes in v2:
> - Patch 14: Fail klp diff when a correlated .rodata object changed.
> - Cover letter: Reflect the .rodata object handling change.
> - Reviewed-by: Sashiko on patches 1-12.
>
> Link to v1: http://lore.virtuozzo.com/kernel/20261124050655.1853940-1-eva.kurchatova@virtuozzo.com/T/#t
>
>
> Eva Kurchatova (2):
> objtool/klp: use the section symbol for .rodata labels
> objtool/klp: reject changed .rodata objects
>
> Joe Lawrence (4):
> ms/objtool/klp: fix data alignment in __clone_symbol()
> ms/objtool/klp: Fix create_fake_symbols() skipping entsize-based
> sections
> ms/objtool/klp: Normalize Module.symvers paths to module names
> ms/objtool/klp: Allow new references to module exports
>
> Josh Poimboeuf (21):
> ms/objtool/klp: Fix detection of corrupt static branch/call entries
> ms/objtool/klp: Disable unsupported pr_debug() usage
> ms/objtool: Fix data alignment in elf_add_data()
> ms/objtool/klp: Fix .data..once static local non-correlation
> ms/objtool/klp: Fix handling of zero-length .altinstr_replacement
> sections
> ms/objtool/klp: Fix cloning of zero-length section symbols
> ms/objtool/klp: Fix relocation conversion failures for R_X86_64_NONE
> ms/objtool/klp: Fix reloc corruption in convert_reloc_sym_to_secsym()
> ms/objtool/klp: Simplify reloc symbol conversion
> ms/objtool: Move mark_rodata() to elf.c
> ms/objtool/klp: Fix pointer comparisons for rodata objects
> ms/objtool/klp: Fix module name normalization for paths with dots
> ms/objtool/klp: Fix cross-module klp relocation section naming
> ms/objtool/klp: Don't match local symbols against exports
> ms/objtool: Fix .cold function detection for duplicate symbols
> ms/objtool/klp: Avoid NULL pointer dereference when printing code
> symbol name
> ms/objtool/klp: Add .klp.symid for sympos disambiguation
> ms/objtool/klp: Fix symbol resolution for duplicate data symbols
> ms/objtool/klp: Fix size of empty special section entries
> ms/objtool/klp: Fix vmlinux .klp.symid link error for .no_trim_symbol
> symbols
> ms/objtool/klp: Ignore replacement offset of empty x86 alternatives
>
> Puranjay Mohan (1):
> ms/objtool/klp: Fix vmlinux .klp.symid link error for .exitcall.exit
> symbols
>
> include/asm-generic/vmlinux.lds.h | 10 +-
> scripts/Makefile.vmlinux_o | 3 +
> scripts/livepatch/klp-build | 7 +
> scripts/mod/modpost.c | 1 +
> tools/objtool/Build | 4 +-
> tools/objtool/arch/x86/special.c | 27 ++
> tools/objtool/builtin-check.c | 7 +
> tools/objtool/check.c | 18 +-
> tools/objtool/elf.c | 69 +++-
> tools/objtool/include/objtool/builtin.h | 1 +
> tools/objtool/include/objtool/elf.h | 25 +-
> tools/objtool/include/objtool/klp.h | 29 +-
> tools/objtool/include/objtool/special.h | 7 +
> tools/objtool/klp-diff.c | 471 ++++++++++++++++--------
> tools/objtool/klp-post-link.c | 53 +--
> tools/objtool/klp-symid.c | 119 ++++++
> tools/objtool/klp-sympos.c | 411 +++++++++++++++++++++
> 17 files changed, 1073 insertions(+), 189 deletions(-)
> create mode 100644 tools/objtool/klp-symid.c
> create mode 100644 tools/objtool/klp-sympos.c
Sorry, wrong series version. This is a v3, as mentioned throughout the
rest of the cover letter. I missed part of the git invocation shell
auto-fill and didn't fix it up in editor. I can re-send if needed.
_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel
^ permalink raw reply [flat|nested] 31+ messages in thread* Re: [Devel] [PATCH hci-8.2 v3 00/28] ms/objtool: fix silently wrong livepatches by klp-build
2026-11-26 5:06 [Devel] [PATCH hci-8.2 v2 00/28] ms/objtool: fix silently wrong livepatches by klp-build Eva Kurchatova
` (28 preceding siblings ...)
2026-11-26 6:15 ` [Devel] [PATCH hci-8.2 v2 00/28] ms/objtool: fix silently wrong livepatches by klp-build Eva Kurchatova (Virtuozzo)
@ 2026-11-26 16:34 ` Konstantin Khorenko
29 siblings, 0 replies; 31+ messages in thread
From: Konstantin Khorenko @ 2026-11-26 16:34 UTC (permalink / raw)
To: vladimir.riabchun, Eva Kurchatova; +Cc: devel
On Thu, 26 Nov 2026 07:06:59 +0200, Eva Kurchatova wrote:
> ms/objtool: fix silently wrong livepatches by klp-build
>
> Patchset for tasks:
> https://virtuozzo.atlassian.net/browse/VSTOR-128510
> https://virtuozzo.atlassian.net/browse/VSTOR-156924
> https://virtuozzo.atlassian.net/browse/VSTOR-157091
>
> [...]
Applied to branch-rh10-6.12.0-211.75.1.26.x.vz10-ovz, will be released in rh10-6.12.0-211.75.1.26.5.vz10, thanks!
[01/28] ms/objtool/klp: Fix detection of corrupt static branch/call entries
https://bitbucket.org/virtuozzocore/vzkernel.vzs/commits/05d2badde0ba
[02/28] ms/objtool/klp: Disable unsupported pr_debug() usage
https://bitbucket.org/virtuozzocore/vzkernel.vzs/commits/670ef3c414a2
[03/28] ms/objtool: Fix data alignment in elf_add_data()
https://bitbucket.org/virtuozzocore/vzkernel.vzs/commits/17c24bee69b4
[04/28] ms/objtool/klp: fix data alignment in __clone_symbol()
https://bitbucket.org/virtuozzocore/vzkernel.vzs/commits/b5dc229faf97
[05/28] ms/objtool/klp: Fix .data..once static local non-correlation
https://bitbucket.org/virtuozzocore/vzkernel.vzs/commits/f4f7288fd250
[06/28] ms/objtool/klp: Fix create_fake_symbols() skipping entsize-based sections
https://bitbucket.org/virtuozzocore/vzkernel.vzs/commits/083a259bf99b
[07/28] ms/objtool/klp: Fix handling of zero-length .altinstr_replacement sections
https://bitbucket.org/virtuozzocore/vzkernel.vzs/commits/2a89d8850ab6
[08/28] ms/objtool/klp: Fix cloning of zero-length section symbols
https://bitbucket.org/virtuozzocore/vzkernel.vzs/commits/44316e4143d5
[09/28] ms/objtool/klp: Fix relocation conversion failures for R_X86_64_NONE
https://bitbucket.org/virtuozzocore/vzkernel.vzs/commits/439c30a5ecf1
[10/28] ms/objtool/klp: Fix reloc corruption in convert_reloc_sym_to_secsym()
https://bitbucket.org/virtuozzocore/vzkernel.vzs/commits/1adcde441559
[11/28] ms/objtool/klp: Simplify reloc symbol conversion
https://bitbucket.org/virtuozzocore/vzkernel.vzs/commits/a09b61e9a7b5
[12/28] ms/objtool: Move mark_rodata() to elf.c
https://bitbucket.org/virtuozzocore/vzkernel.vzs/commits/6014c28cfbe1
[13/28] ms/objtool/klp: Fix pointer comparisons for rodata objects
https://bitbucket.org/virtuozzocore/vzkernel.vzs/commits/28dfab03d4f7
[14/28] objtool/klp: use the section symbol for .rodata labels
https://bitbucket.org/virtuozzocore/vzkernel.vzs/commits/a38535c776b4
[15/28] objtool/klp: reject changed .rodata objects
https://bitbucket.org/virtuozzocore/vzkernel.vzs/commits/0dcfab4e5d9b
[16/28] ms/objtool/klp: Fix module name normalization for paths with dots
https://bitbucket.org/virtuozzocore/vzkernel.vzs/commits/b5fc4447a390
[17/28] ms/objtool/klp: Normalize Module.symvers paths to module names
https://bitbucket.org/virtuozzocore/vzkernel.vzs/commits/904b5341873a
[18/28] ms/objtool/klp: Fix cross-module klp relocation section naming
https://bitbucket.org/virtuozzocore/vzkernel.vzs/commits/355bb8eab620
[19/28] ms/objtool/klp: Don't match local symbols against exports
https://bitbucket.org/virtuozzocore/vzkernel.vzs/commits/cf5c11b700d7
[20/28] ms/objtool: Fix .cold function detection for duplicate symbols
https://bitbucket.org/virtuozzocore/vzkernel.vzs/commits/67b513ded66a
[21/28] ms/objtool/klp: Avoid NULL pointer dereference when printing code symbol name
https://bitbucket.org/virtuozzocore/vzkernel.vzs/commits/5cef0fb6518c
[22/28] ms/objtool/klp: Add .klp.symid for sympos disambiguation
https://bitbucket.org/virtuozzocore/vzkernel.vzs/commits/4e7ac86164b7
[23/28] ms/objtool/klp: Fix symbol resolution for duplicate data symbols
https://bitbucket.org/virtuozzocore/vzkernel.vzs/commits/05e1b8502c80
[24/28] ms/objtool/klp: Fix size of empty special section entries
https://bitbucket.org/virtuozzocore/vzkernel.vzs/commits/f82840e5849d
[25/28] ms/objtool/klp: Fix vmlinux .klp.symid link error for .no_trim_symbol symbols
https://bitbucket.org/virtuozzocore/vzkernel.vzs/commits/b471a1e59016
[26/28] ms/objtool/klp: Ignore replacement offset of empty x86 alternatives
https://bitbucket.org/virtuozzocore/vzkernel.vzs/commits/f2d31880578c
[27/28] ms/objtool/klp: Allow new references to module exports
https://bitbucket.org/virtuozzocore/vzkernel.vzs/commits/da7b7112af70
[28/28] ms/objtool/klp: Fix vmlinux .klp.symid link error for .exitcall.exit symbols
https://bitbucket.org/virtuozzocore/vzkernel.vzs/commits/9d3240d27f8b
--
Konstantin Khorenko,
Virtuozzo Linux Kernel Team
_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel
^ permalink raw reply [flat|nested] 31+ messages in thread