mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH 1/2] objtool/klp: Fix the offset and length used to read SHF_STRINGS data
@ 2026-11-26 10:53 Longjun Luo
  2026-11-26 10:53 ` [PATCH 2/2] objtool/klp: Fix string sentinels referenced through .L labels Longjun Luo
  0 siblings, 1 reply; 2+ messages in thread
From: Longjun Luo @ 2026-11-26 10:53 UTC (permalink / raw)
  To: jpoimboe, peterz
  Cc: mbenes, pmladek, joe.lawrence, song, puranjay, nathan,
	ndesaulniers, morbo, justinstitt, live-patching, llvm,
	linux-kernel, Longjun Luo

For offset, handle PC-relative references, like the one in
proc_ns_dir_lookup() with Clang ThinLTO. Use arch_adjusted_addend()
to read the string, and generate symbol-based relocations when needed.

For length, add support for wide strings. The u16 psr_setup_time_us[]
table is placed in a wide-string section by Clang. The table
occupies 14 bytes, while strlen() gives objtool a length of only 5 bytes.

With GCC and Clang, the test catches both wide string truncation and a
missing PC-relative adjustment.

Fixes: 0d83da43b1e1 ("objtool/klp: Add --checksum option to generate per-function checksums")
Fixes: dd590d4d57eb ("objtool/klp: Introduce klp diff subcommand for diffing object files")
Fixes: 873a2208ea31 ("objtool/klp: Calculate object checksums")
Signed-off-by: Longjun Luo <luolongjuna@gmail.com>
---
Ran the test suite on x86-64 (tip/objtool/core, b99a5337da3c).
Same results with patch 1 alone and with both patches.

GCC 12.2.0 (-fno-pie)  49 passed, 4 skipped
Clang 17.0.6           51 passed, 2 skipped

With this GCC's default PIE, the same 35 tests fail with or without
the patches.

Codex helped write the new tests.

 tools/objtool/elf.c                           | 27 +++++++++++++-
 tools/objtool/include/objtool/elf.h           |  1 +
 tools/objtool/klp-checksum.c                  |  6 ++-
 tools/objtool/klp-diff.c                      | 37 ++++++++++++++++---
 .../tests/generic/fixtures/checksum_insn.c    | 11 ++++++
 .../tests/generic/test-checksum-insn.sh       | 18 +++++++++
 6 files changed, 91 insertions(+), 9 deletions(-)

diff --git a/tools/objtool/elf.c b/tools/objtool/elf.c
index a791f4ea6ec1..4a8698114666 100644
--- a/tools/objtool/elf.c
+++ b/tools/objtool/elf.c
@@ -1382,9 +1382,32 @@ struct elf *elf_create_file(GElf_Ehdr *ehdr, const char *name)
 	return elf;
 }
 
+static size_t __elf_strlen(const char *str, size_t width, size_t size)
+{
+	size_t len;
+
+	for (len = 0; size - len >= width; len += width) {
+		if (!memchr_inv(str + len, 0, width))
+			return len;
+	}
+	return 0;
+}
+
+/*
+ * Same as strlen, add support for wide strings like:
+ *   u16 psr_setup_time_us[] from the DRM DisplayPort helpers.
+ * Clang puts this data into a wide string section with sh_entsize = 2.
+ */
+size_t elf_strlen(struct section *sec, unsigned long offset)
+{
+	return __elf_strlen(sec->data->d_buf + offset, sec->sh.sh_entsize ?: 1,
+			    sec_size(sec) - offset);
+}
+
 unsigned int elf_add_string(struct elf *elf, struct section *strtab, const char *str)
 {
 	unsigned int offset;
+	size_t width, len;
 
 	if (!strtab)
 		strtab = find_section_by_name(elf, ".strtab");
@@ -1399,8 +1422,10 @@ unsigned int elf_add_string(struct elf *elf, struct section *strtab, const char
 	}
 
 	offset = ALIGN(sec_size(strtab), strtab->sh.sh_addralign);
+	width = strtab->sh.sh_entsize ?: 1;
+	len = __elf_strlen(str, width, SIZE_MAX);
 
-	if (!elf_add_data(elf, strtab, str, strlen(str) + 1))
+	if (!elf_add_data(elf, strtab, str, len + width))
 		return -1;
 
 	return offset;
diff --git a/tools/objtool/include/objtool/elf.h b/tools/objtool/include/objtool/elf.h
index f7e6a652ee78..50d5b17907c8 100644
--- a/tools/objtool/include/objtool/elf.h
+++ b/tools/objtool/include/objtool/elf.h
@@ -180,6 +180,7 @@ struct symbol *elf_create_section_symbol(struct elf *elf, struct section *sec);
 void *elf_add_data(struct elf *elf, struct section *sec, const void *data,
 		   size_t size);
 
+size_t elf_strlen(struct section *sec, unsigned long offset);
 unsigned int elf_add_string(struct elf *elf, struct section *strtab, const char *str);
 
 struct reloc *elf_create_reloc(struct elf *elf, struct section *sec,
diff --git a/tools/objtool/klp-checksum.c b/tools/objtool/klp-checksum.c
index ebe25f9c5260..2547e142794b 100644
--- a/tools/objtool/klp-checksum.c
+++ b/tools/objtool/klp-checksum.c
@@ -138,7 +138,8 @@ static void checksum_update_insn(struct objtool_file *file, struct symbol *func,
 		char *str;
 
 		str = sym->sec->data->d_buf + sym->offset + offset;
-		__checksum_update_insn(func, insn, str, strlen(str));
+		__checksum_update_insn(func, insn, str,
+				       elf_strlen(sym->sec, sym->offset + offset));
 		goto alts;
 	}
 
@@ -215,7 +216,8 @@ static void checksum_update_object(struct objtool_file *file, struct symbol *sym
 
 			str = target->sec->data->d_buf + target->offset + offset;
 			__checksum_update_object(sym, sym_offset,
-						 "reloc string", str, strlen(str));
+						 "reloc string", str,
+						 elf_strlen(target->sec, target->offset + offset));
 			continue;
 		}
 
diff --git a/tools/objtool/klp-diff.c b/tools/objtool/klp-diff.c
index 95d0d86666d7..dd5c4d604be3 100644
--- a/tools/objtool/klp-diff.c
+++ b/tools/objtool/klp-diff.c
@@ -1584,18 +1584,43 @@ static int clone_reloc(struct elfs *e, struct reloc *patched_reloc,
 		return -1;
 
 	/*
-	 * For strings, all references use section symbols, thanks to
-	 * convert_reloc_sym().  clone_symbol() has cloned an empty
-	 * version of the string section.  Now copy the string itself.
+	 * For string relocations, in some cases, compilers generate
+	 * PC-relative code like:
+	 *
+	 *   xorw 0(%rip), %dx    # "mnt", R_X86_64_PC32, addend -4
+	 *   (from proc_ns_dir_lookup(), Clang ThinLTO).
+	 *
+	 * In that case, str_off is not equal to the addend.
+	 * To read the string, use arch_adjusted_addend instead.
+	 *
+	 * To write the string, we could use out_off + (addend - str_off)
+	 * as the section-based addend.
+	 * The addend may become negative, and make lld report
+	 * "offset is outside the section".
+	 * So for this case, create temp symbol to use symbol-based relocation.
 	 */
 	if (is_string_sec(patched_sym->sec)) {
-		const char *str = patched_sym->sec->data->d_buf + addend;
+		long str_off = arch_adjusted_addend(patched_reloc);
+		const char *str = patched_sym->sec->data->d_buf + str_off;
+		struct section *out_sec = out_sym->sec;
+		unsigned int out_off;
 
 		__dbg_clone("\"%s\"", escape_str(str));
 
-		addend = elf_add_string(e->out, out_sym->sec, str);
-		if (addend == -1)
+		out_off = elf_add_string(e->out, out_sec, str);
+		if (out_off == -1)
 			return -1;
+
+		if (addend != str_off) {
+			out_sym = elf_create_symbol(e->out, ".L.str", out_sec,
+						    STB_LOCAL, STT_NOTYPE, out_off, 0);
+			if (!out_sym)
+				return -1;
+
+			addend -= str_off;
+		} else {
+			addend = out_off;
+		}
 	}
 
 	if (!elf_create_reloc(e->out, sec, offset, out_sym, addend,
diff --git a/tools/objtool/tests/generic/fixtures/checksum_insn.c b/tools/objtool/tests/generic/fixtures/checksum_insn.c
index 10f70a74a976..70c2e9106f8a 100644
--- a/tools/objtool/tests/generic/fixtures/checksum_insn.c
+++ b/tools/objtool/tests/generic/fixtures/checksum_insn.c
@@ -76,3 +76,14 @@ int target(int x)
 
 	return r;
 }
+
+#ifdef WIDE_STRING
+unsigned short wide_string(void)
+{
+#ifdef PATCHED
+	return *(const volatile unsigned short *)u"\u0200";
+#else
+	return *(const volatile unsigned short *)u"\u0100";
+#endif
+}
+#endif
diff --git a/tools/objtool/tests/generic/test-checksum-insn.sh b/tools/objtool/tests/generic/test-checksum-insn.sh
index e1c04a1f518a..692ab1b174b2 100755
--- a/tools/objtool/tests/generic/test-checksum-insn.sh
+++ b/tools/objtool/tests/generic/test-checksum-insn.sh
@@ -46,4 +46,22 @@ build_pair checksum_insn.c
 run_checksum
 assert_checksum_matches target
 
+# The wide character changes while its low byte stays zero.
+build_pair checksum_insn.c -DWIDE_STRING -fno-pic
+assert_checksum_differs wide_string
+run_diff
+for obj in patched out; do
+	$OBJCOPY -O binary --only-section='*.str2.*' "$workdir/$obj.o" \
+		"$workdir/$obj.bin" || fail "cannot extract wide string from $obj.o"
+done
+[ -s "$workdir/patched.bin" ] || fail "fixture produced no two-byte string section"
+cmp -s "$workdir/patched.bin" "$workdir/out.bin" ||
+	fail "wide string was copied incorrectly"
+
+# The x86-64 RIP-relative load needs a string label with addend -4.
+if $READELF -h "$workdir/patched.o" | grep -q 'Machine:.*X86-64'; then
+	section_relocs .text.wide_string | grep -Eq 'R_X86_64_PC32.* \.L[^ ]* - 4$' ||
+		fail "wide_string() lost its PC-relative string reference"
+fi
+
 pass "instruction checksums cover reloc targets, addends and string contents"
-- 
2.53.0


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-26 10:53 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-26 10:53 [PATCH 1/2] objtool/klp: Fix the offset and length used to read SHF_STRINGS data Longjun Luo
2026-11-26 10:53 ` [PATCH 2/2] objtool/klp: Fix string sentinels referenced through .L labels Longjun Luo

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®