Live Patching
 help / color / mirror / Atom feed
From: Longjun Luo <luolongjuna@gmail.com>
To: jpoimboe@kernel.org, peterz@infradead.org
Cc: mbenes@suse.cz, pmladek@suse.com, joe.lawrence@redhat.com,
	song@kernel.org, puranjay@kernel.org, nathan@kernel.org,
	ndesaulniers@google.com, morbo@google.com,
	justinstitt@google.com, live-patching@vger.kernel.org,
	llvm@lists.linux.dev, linux-kernel@vger.kernel.org,
	Longjun Luo <luolongjuna@gmail.com>
Subject: [RFC PATCH v2 03/11] objtool/klp: Preserve identifier digits when stripping suffixes
Date: Wed,  4 Nov 2026 21:31:49 +0800	[thread overview]
Message-ID: <20261104133158.3187525-4-luolongjuna@gmail.com> (raw)
In-Reply-To: <20261104133158.3187525-1-luolongjuna@gmail.com>

The name tx.crc32 loses its trailing digits, while tx.crc32.llvm.<hash>
keeps them because of an early return. ThinLTO promotion can therefore
break correlation and copy existing static state into the patch.

Strip LLVM hashes and complete .<number> suffixes on the same path.
Keep the counter-stripping rule for __UNIQUE_ID names, including
Clang's function-prefixed names.

Add a ThinLTO test which checks that the existing crc32 state is reused.
The available objtool tests pass with GCC 16.2.1 and Clang 22.1.8.

Fixes: 0d83da43b1e1 ("objtool/klp: Add --checksum option to generate per-function checksums")
Fixes: 020b71dcafee ("objtool/klp: Remove .llvm suffix in demangle_name()")
Assisted-by: LLM
Signed-off-by: Longjun Luo <luolongjuna@gmail.com>
---
 tools/objtool/elf.c                           | 33 +++++++++++--------
 .../fixtures/identity/promotion_crc32.c       |  8 +++++
 .../fixtures/identity/promotion_crc32_user.c  | 15 +++++++++
 .../generic/test-identity-thinlto-crc32.sh    | 23 +++++++++++++
 4 files changed, 66 insertions(+), 13 deletions(-)
 create mode 100644 tools/objtool/tests/generic/fixtures/identity/promotion_crc32.c
 create mode 100644 tools/objtool/tests/generic/fixtures/identity/promotion_crc32_user.c
 create mode 100755 tools/objtool/tests/generic/test-identity-thinlto-crc32.sh

diff --git a/tools/objtool/elf.c b/tools/objtool/elf.c
index 71f1a272e468..3ddbf5143260 100644
--- a/tools/objtool/elf.c
+++ b/tools/objtool/elf.c
@@ -448,32 +448,39 @@ static int read_sections(struct elf *elf)
  */
 static ssize_t demangled_name_len(const char *name)
 {
-	ssize_t idx;
+	ssize_t idx, len;
 	const char *p;
 
 	p = strstr(name, ".llvm.");
-	if (p)
-		return p - name;
+	len = p ? p - name : strlen(name);
 
-	if (!strstarts(name, "__UNIQUE_ID_") && !strchr(name, '.'))
-		return strlen(name);
+	if (strstarts(name, "__UNIQUE_ID_") || strstr(name, ".__UNIQUE_ID_")) {
+		for (idx = len - 1; idx >= 0; idx--) {
+			char c = name[idx];
 
-	for (idx = strlen(name) - 1; idx >= 0; idx--) {
-		char c = name[idx];
+			if (!isdigit(c) && c != '.' && c != '_')
+				break;
+		}
+		return idx + 1;
+	}
+
+	while (len && isdigit(name[len - 1])) {
+		p = name + len - 1;
+		while (p > name && isdigit(*p))
+			p--;
 
-		if (!isdigit(c) && c != '.' && c != '_')
+		if (p == name || *p != '.')
 			break;
+		len = p - name;
 	}
-	if (idx <= 0)
-		return strlen(name);
-	return idx + 1;
+	return len;
 }
 
 /*
  * Remove number suffix of a symbol.
  *
- * Specifically, remove trailing numbers for "__UNIQUE_ID_" symbols and
- * symbols with '.'.
+ * Remove complete .<number> suffixes and trailing numbers from
+ * "__UNIQUE_ID_" symbols.
  *
  * With CONFIG_LTO_CLANG_THIN, it is possible to have nested __UNIQUE_ID_,
  * such as
diff --git a/tools/objtool/tests/generic/fixtures/identity/promotion_crc32.c b/tools/objtool/tests/generic/fixtures/identity/promotion_crc32.c
new file mode 100644
index 000000000000..d95914eacdc0
--- /dev/null
+++ b/tools/objtool/tests/generic/fixtures/identity/promotion_crc32.c
@@ -0,0 +1,8 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (C) 2026 Longjun Luo <luolongjuna@gmail.com> */
+unsigned int tx(unsigned int value)
+{
+	static volatile unsigned int crc32;
+
+	return crc32 += value;
+}
diff --git a/tools/objtool/tests/generic/fixtures/identity/promotion_crc32_user.c b/tools/objtool/tests/generic/fixtures/identity/promotion_crc32_user.c
new file mode 100644
index 000000000000..b44fa1d4a75e
--- /dev/null
+++ b/tools/objtool/tests/generic/fixtures/identity/promotion_crc32_user.c
@@ -0,0 +1,15 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (C) 2026 Longjun Luo <luolongjuna@gmail.com> */
+#include "common.h"
+MODINFO;
+
+unsigned int tx(unsigned int value);
+
+unsigned int target(unsigned int value)
+{
+#ifdef PATCHED
+	return tx(value);
+#else
+	return value;
+#endif
+}
diff --git a/tools/objtool/tests/generic/test-identity-thinlto-crc32.sh b/tools/objtool/tests/generic/test-identity-thinlto-crc32.sh
new file mode 100755
index 000000000000..66988d0b7692
--- /dev/null
+++ b/tools/objtool/tests/generic/test-identity-thinlto-crc32.sh
@@ -0,0 +1,23 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0
+# Copyright (C) 2026 Longjun Luo <luolongjuna@gmail.com>
+# ThinLTO promotion must retain the digits in a static variable's name.
+. "$(dirname "$0")/../identity-lib.sh"
+
+setup
+build_identity_pair thin "" identity/promotion_crc32.c identity/promotion_crc32_user.c
+in_symbols orig.o | awk '$4 == "OBJECT" && $5 == "LOCAL" && $8 == "tx.crc32"' |
+	grep -q . || probe_skip "CRC state was not local before importing"
+in_symbols patched.o | awk '$4 == "OBJECT" && $5 == "GLOBAL" && $8 ~ /^tx\.crc32\.llvm\.[0-9]+$/' |
+	grep -q . || probe_skip "ThinLTO did not promote the CRC state"
+
+assert_checksum_matches tx
+run_diff
+assert_patched target
+assert_not_patched tx
+out_symbols | awk '$4 == "OBJECT" && $7 != "UND" && $8 ~ /^tx\.crc32(\.|$)/' |
+	grep -q . && fail "promoted CRC state was allocated again"
+run_post_link
+assert_original_reference target tx.crc32
+
+pass "digits in static names survive ThinLTO promotion"
-- 
2.55.0


  parent reply	other threads:[~2026-11-04 13:32 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-04 13:31 [RFC PATCH v2 00/11] objtool/klp: DWARF-based cross-build symbol correlation Longjun Luo
2026-11-04 13:31 ` [RFC PATCH v2 01/11] objtool/klp: Fix checksums and copying of wide strings Longjun Luo
2026-11-04 14:03   ` sashiko-bot
2026-11-04 13:31 ` [RFC PATCH v2 02/11] objtool/klp: Hash compound literals by content Longjun Luo
2026-11-04 13:55   ` sashiko-bot
2026-11-04 13:31 ` Longjun Luo [this message]
2026-11-04 13:31 ` [RFC PATCH v2 04/11] objtool/klp: Read symbol identities from DWARF Longjun Luo
2026-11-04 13:51   ` sashiko-bot
2026-11-04 13:31 ` [RFC PATCH v2 05/11] objtool/klp: Support reduced and split DWARF Longjun Luo
2026-11-04 13:31 ` [RFC PATCH v2 06/11] klp-build: Require CONFIG_DEBUG_INFO Longjun Luo
2026-11-04 13:31 ` [RFC PATCH v2 07/11] objtool/klp: Correlate symbols using DWARF identities Longjun Luo
2026-11-04 14:09   ` sashiko-bot
2026-11-04 13:31 ` [RFC PATCH v2 08/11] objtool/klp: Include symbol identities in reference checksums Longjun Luo
2026-11-04 14:06   ` sashiko-bot
2026-11-04 13:31 ` [RFC PATCH v2 09/11] objtool/klp: Extend twins through unchanged function references Longjun Luo
2026-11-04 14:05   ` sashiko-bot
2026-11-04 13:31 ` [RFC PATCH v2 10/11] objtool/klp: Check unmatched data references Longjun Luo
2026-11-04 13:31 ` [RFC PATCH v2 11/11] objtool/klp: Test symbol identity and state preservation Longjun Luo
2026-11-04 13:50   ` sashiko-bot

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=20261104133158.3187525-4-luolongjuna@gmail.com \
    --to=luolongjuna@gmail.com \
    --cc=joe.lawrence@redhat.com \
    --cc=jpoimboe@kernel.org \
    --cc=justinstitt@google.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=live-patching@vger.kernel.org \
    --cc=llvm@lists.linux.dev \
    --cc=mbenes@suse.cz \
    --cc=morbo@google.com \
    --cc=nathan@kernel.org \
    --cc=ndesaulniers@google.com \
    --cc=peterz@infradead.org \
    --cc=pmladek@suse.com \
    --cc=puranjay@kernel.org \
    --cc=song@kernel.org \
    /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