From: George Guo <dongtai.guo@linux.dev>
To: chenhuacai@kernel.org, jpoimboe@kernel.org, peterz@infradead.org,
jikos@kernel.org, mbenes@suse.cz, pmladek@suse.com
Cc: kernel@xen0n.name, joe.lawrence@redhat.com, rostedt@goodmis.org,
ardb@kernel.org, nathan@kernel.org,
nick.desaulniers+lkml@gmail.com, yangtiezhu@loongson.cn,
jiaxun.yang@flygoat.com, wangrui@loongson.cn,
liukexin@kylinos.cn, guodongtai@kylinos.cn, xry111@xry111.site,
wangyuli@aosc.io, loongarch@lists.linux.dev,
live-patching@vger.kernel.org, llvm@lists.linux.dev,
linux-kernel@vger.kernel.org
Subject: [PATCH v4 09/14] objtool/klp: Convert local label references
Date: Fri, 24 Jul 2026 19:41:22 +0800 [thread overview]
Message-ID: <20260724114128.31451-10-dongtai.guo@linux.dev> (raw)
In-Reply-To: <20260724114128.31451-1-dongtai.guo@linux.dev>
From: George Guo <guodongtai@kylinos.cn>
Some toolchains reference an object through a local assembler label (.L*)
rather than a section symbol plus offset. The assembler keeps the label
because a "section + constant offset" reference would go stale under
linker relaxation, while a symbol reference can be recomputed afterwards.
Such labels are assembler-local, absent from kallsyms, and are never
cloned into the livepatch object.
Two toolchains hit this:
- GCC/GAS on LoongArch references special section entries (__ex_table,
__bug_table, __jump_table, .altinstructions) through local text
labels instead of a section symbol.
- Clang emits a switch jump table through a local label (.LJTI*) in a
SHF_MERGE|SHF_STRINGS .rodata section, where the table shares the
section with the livepatch's klp_func name strings.
convert_reloc_secsym_to_sym() only handled the section symbol form and
returned early for a label reference, so the label was never converted
and the reference resolved incorrectly, with no error at build or load
time:
- GCC: should_keep_special_sym() cannot correlate the entry with an
included function and silently drops it, so the livepatch module is
missing the patched function's __ex_table / __bug_table /
__jump_table entries.
- Clang: the jump table is not cloned, and its base label resolves into
the name strings that occupy the same offset; the switch's indirect
jump then lands on a wild address and the patched function oopses at
runtime.
Redirect a local label reference off the label, mirroring the section
symbol case:
- In a text section, to the containing function symbol. A label with
no containing function symbol (e.g. hand-written asm in a plain .text
section) can't be correlated; skip it rather than failing, since such
entries belong to unchanged code and are dropped anyway.
- In a non-text section (e.g. the .rodata jump table), to the section
symbol plus the full offset.
Reported-by: Joe Lawrence <joe.lawrence@redhat.com>
Suggested-by: Joe Lawrence <joe.lawrence@redhat.com>
Co-developed-by: Kexin Liu <liukexin@kylinos.cn>
Signed-off-by: Kexin Liu <liukexin@kylinos.cn>
Signed-off-by: George Guo <guodongtai@kylinos.cn>
---
tools/objtool/klp-diff.c | 49 +++++++++++++++++++++++++++++++++++++++-
1 file changed, 48 insertions(+), 1 deletion(-)
diff --git a/tools/objtool/klp-diff.c b/tools/objtool/klp-diff.c
index b9624bd9439b..3e573b73fe6b 100644
--- a/tools/objtool/klp-diff.c
+++ b/tools/objtool/klp-diff.c
@@ -1423,8 +1423,55 @@ static int convert_reloc_secsym_to_sym(struct elf *elf, struct reloc *reloc)
if (!strcmp(reloc->sec->name, ".rela__patchable_function_entries"))
return convert_pfe_reloc(elf, reloc);
- if (!is_sec_sym(sym))
+ if (!is_sec_sym(sym)) {
+ /*
+ * Most toolchains reference special-section entries via the
+ * section symbol plus an offset. GCC/GAS on LoongArch instead
+ * references a local text label (.L*): LoongArch linker
+ * relaxation is the reason GAS keeps the label rather than
+ * reducing it to a section symbol reference. Such a label is
+ * never cloned into the livepatch object, so the entry would be
+ * silently dropped. Redirect the relocation to the containing
+ * function, mirroring the section-symbol case below.
+ */
+ if (is_local_label(sym)) {
+ unsigned long offset = sym->offset + reloc_addend(reloc);
+
+ if (is_text_sec(sec)) {
+ sym = find_symbol_containing_inclusive(sec, offset);
+ if (!sym) {
+ /*
+ * A local label with no containing function
+ * symbol (e.g. hand-written asm in a plain .text
+ * section). It can't be correlated to a function,
+ * so skip it rather than failing the build; such
+ * entries belong to unchanged code and are dropped
+ * anyway.
+ */
+ return 1;
+ }
+
+ reloc->sym = sym;
+ set_reloc_sym(elf, reloc, sym->idx);
+ set_reloc_addend(elf, reloc, offset - sym->offset);
+ } else {
+ /*
+ * A local label in a non-text section, e.g. Clang's
+ * .LJTI* switch jump table in .rodata. It isn't
+ * cloned into the livepatch either, so redirect the
+ * reloc to the section symbol plus the full offset,
+ * mirroring the section-symbol case below.
+ */
+ if (!sec->sym && !elf_create_section_symbol(elf, sec))
+ return -1;
+ reloc->sym = sec->sym;
+ set_reloc_sym(elf, reloc, sec->sym->idx);
+ set_reloc_addend(elf, reloc, offset);
+ }
+ }
+
return 0;
+ }
sym = find_symbol_containing_inclusive(sec, arch_adjusted_addend(reloc));
if (!sym) {
--
2.53.0
next prev parent reply other threads:[~2026-07-24 11:44 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-07-24 11:41 [PATCH v4 00/14] LoongArch: Add livepatch build (KLP) support George Guo
2026-07-24 11:41 ` [PATCH v4 01/14] objtool/LoongArch: Add arch_adjusted_addend() for KLP support George Guo
2026-07-24 11:41 ` [PATCH v4 02/14] LoongArch: Mark special sections " George Guo
2026-07-24 11:41 ` [PATCH v4 03/14] livepatch/klp-build: use -fPIC and drop direct-extern-access on LoongArch George Guo
2026-07-24 11:41 ` [PATCH v4 04/14] LoongArch: Fix EFI linking with -fdata-sections George Guo
2026-07-24 11:41 ` [PATCH v4 05/14] objtool/klp: Add LoongArch jump opcode bytes support George Guo
2026-07-24 11:41 ` [PATCH v4 06/14] klp-build: Add LoongArch syscall patching macro George Guo
2026-07-24 11:41 ` [PATCH v4 07/14] LoongArch: Add livepatch build (KLP) support George Guo
2026-07-24 11:41 ` [PATCH v4 08/14] LoongArch: Select FTRACE_MCOUNT_USE_PATCHABLE_FUNCTION_ENTRY George Guo
2026-07-24 11:41 ` George Guo [this message]
2026-07-24 11:41 ` [PATCH v4 10/14] objtool/klp: Fix ANNOTATE_DATA_SPECIAL parsing for local label references George Guo
2026-07-24 11:41 ` [PATCH v4 11/14] objtool/klp: Fold LoongArch paired ADD/SUB relocations into PCREL George Guo
2026-07-24 11:41 ` [PATCH v4 12/14] objtool/LoongArch: Enable inline alternative cloning for KLP George Guo
2026-07-24 11:41 ` [PATCH v4 13/14] objtool/klp: Keep LoongArch tablejump annotation table entries George Guo
2026-07-24 11:41 ` [PATCH v4 14/14] objtool/klp: Rewrite PC-relative data references to GOT on LoongArch George Guo
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260724114128.31451-10-dongtai.guo@linux.dev \
--to=dongtai.guo@linux.dev \
--cc=ardb@kernel.org \
--cc=chenhuacai@kernel.org \
--cc=guodongtai@kylinos.cn \
--cc=jiaxun.yang@flygoat.com \
--cc=jikos@kernel.org \
--cc=joe.lawrence@redhat.com \
--cc=jpoimboe@kernel.org \
--cc=kernel@xen0n.name \
--cc=linux-kernel@vger.kernel.org \
--cc=liukexin@kylinos.cn \
--cc=live-patching@vger.kernel.org \
--cc=llvm@lists.linux.dev \
--cc=loongarch@lists.linux.dev \
--cc=mbenes@suse.cz \
--cc=nathan@kernel.org \
--cc=nick.desaulniers+lkml@gmail.com \
--cc=peterz@infradead.org \
--cc=pmladek@suse.com \
--cc=rostedt@goodmis.org \
--cc=wangrui@loongson.cn \
--cc=wangyuli@aosc.io \
--cc=xry111@xry111.site \
--cc=yangtiezhu@loongson.cn \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox