From: Josh Poimboeuf <jpoimboe@kernel.org>
To: x86@kernel.org
Cc: linux-kernel@vger.kernel.org, live-patching@vger.kernel.org,
Peter Zijlstra <peterz@infradead.org>,
Joe Lawrence <joe.lawrence@redhat.com>,
Song Liu <song@kernel.org>,
Catalin Marinas <catalin.marinas@arm.com>,
Will Deacon <will@kernel.org>,
linux-arm-kernel@lists.infradead.org,
Mark Rutland <mark.rutland@arm.com>,
Nathan Chancellor <nathan@kernel.org>,
Nicolas Schier <nsc@kernel.org>,
Herbert Xu <herbert@gondor.apana.org.au>,
Miroslav Benes <mbenes@suse.cz>, Petr Mladek <pmladek@suse.com>
Subject: [PATCH v4 14/22] objtool: Reuse string references
Date: Sat, 8 Aug 2026 16:17:18 -0700 [thread overview]
Message-ID: <e0856be4208a6bed2b22dffe2bf8e010470f49cf.1786230311.git.jpoimboe@kernel.org> (raw)
In-Reply-To: <cover.1786230311.git.jpoimboe@kernel.org>
For duplicate strings, elf_add_string() just blindly adds duplicates.
That can be a problem for arm64 which often uses two consecutive
instructions (and corresponding relocations) to put an address into a
register, like:
d8: 90000001 adrp x1, 0 <meminfo_proc_show> d8: R_AARCH64_ADR_PREL_PG_HI21 .rodata.meminfo_proc_show.str1.8
dc: 91000021 add x1, x1, #0x0 dc: R_AARCH64_ADD_ABS_LO12_NC .rodata.meminfo_proc_show.str1.8
Referencing two different addresses in the ADRP+ADD pair would corrupt
the memory access. Avoid that by detecting and reusing duplicates when
cloning string relocs.
Signed-off-by: Josh Poimboeuf <jpoimboe@kernel.org>
---
tools/objtool/elf.c | 29 +++++++++++++++++++++++------
tools/objtool/include/objtool/elf.h | 3 ++-
tools/objtool/klp-diff.c | 4 +++-
3 files changed, 28 insertions(+), 8 deletions(-)
diff --git a/tools/objtool/elf.c b/tools/objtool/elf.c
index 8bf225d70d918..3bb04d6155a8b 100644
--- a/tools/objtool/elf.c
+++ b/tools/objtool/elf.c
@@ -1379,9 +1379,27 @@ struct elf *elf_create_file(GElf_Ehdr *ehdr, const char *name)
return elf;
}
-unsigned int elf_add_string(struct elf *elf, struct section *strtab, const char *str)
+int elf_find_string(struct elf *elf, struct section *strtab, const char *str)
{
- unsigned int offset;
+ char *d_buf;
+ int i;
+
+ if (!strtab->data)
+ return -1;
+
+ d_buf = strtab->data->d_buf;
+
+ for (i = 0; i < strtab->data->d_size; i += strlen(d_buf + i) + 1) {
+ if (!strcmp(d_buf + i, str))
+ return i;
+ }
+
+ return -1;
+}
+
+int elf_add_string(struct elf *elf, struct section *strtab, const char *str)
+{
+ void *data;
if (!strtab)
strtab = find_section_by_name(elf, ".strtab");
@@ -1395,12 +1413,11 @@ unsigned int elf_add_string(struct elf *elf, struct section *strtab, const char
return -1;
}
- offset = ALIGN(sec_size(strtab), strtab->sh.sh_addralign);
-
- if (!elf_add_data(elf, strtab, str, strlen(str) + 1))
+ data = elf_add_data(elf, strtab, str, strlen(str) + 1);
+ if (!data)
return -1;
- return offset;
+ return data - strtab->data->d_buf;
}
void *elf_add_data(struct elf *elf, struct section *sec, const void *data, size_t size)
diff --git a/tools/objtool/include/objtool/elf.h b/tools/objtool/include/objtool/elf.h
index 527dd10859de4..5cba96c6392cb 100644
--- a/tools/objtool/include/objtool/elf.h
+++ b/tools/objtool/include/objtool/elf.h
@@ -188,7 +188,8 @@ 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);
-unsigned int elf_add_string(struct elf *elf, struct section *strtab, const char *str);
+int elf_find_string(struct elf *elf, struct section *strtab, const char *str);
+int elf_add_string(struct elf *elf, struct section *strtab, const char *str);
struct reloc *elf_create_reloc(struct elf *elf, struct section *sec,
unsigned long offset, struct symbol *sym,
diff --git a/tools/objtool/klp-diff.c b/tools/objtool/klp-diff.c
index 19dcf930a562b..446ea6f9864a1 100644
--- a/tools/objtool/klp-diff.c
+++ b/tools/objtool/klp-diff.c
@@ -1543,7 +1543,9 @@ static int clone_reloc(struct elfs *e, struct reloc *patched_reloc,
__dbg_clone("\"%s\"", escape_str(str));
- addend = elf_add_string(e->out, out_sym->sec, str);
+ addend = elf_find_string(e->out, out_sym->sec, str);
+ if (addend == -1)
+ addend = elf_add_string(e->out, out_sym->sec, str);
if (addend == -1)
return -1;
}
--
2.54.0
next prev parent reply other threads:[~2026-08-08 23:19 UTC|newest]
Thread overview: 23+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-08-08 23:17 [PATCH v4 00/22] objtool/arm64: Port klp-build to arm64 Josh Poimboeuf
2026-08-08 23:17 ` [PATCH v4 01/22] objtool/klp: Fix .kcfi_traps special section extraction Josh Poimboeuf
2026-08-08 23:17 ` [PATCH v4 02/22] klp-build: Reject patches to init/*.c Josh Poimboeuf
2026-08-08 23:17 ` [PATCH v4 03/22] arm64: Annotate intra-function calls Josh Poimboeuf
2026-08-08 23:17 ` [PATCH v4 04/22] arm64: Fix EFI linking with -fdata-sections Josh Poimboeuf
2026-08-08 23:17 ` [PATCH v4 05/22] arm64: Rename TRAMP_VALIAS -> TRAMP_VALIAS_ASM in asm-offsets Josh Poimboeuf
2026-08-08 23:17 ` [PATCH v4 06/22] arm64: vdso: Discard .discard.* sections Josh Poimboeuf
2026-08-08 23:17 ` [PATCH v4 07/22] arm64: Annotate special section entries Josh Poimboeuf
2026-08-08 23:17 ` [PATCH v4 08/22] arm64: Remove unnecessary empty alternatives Josh Poimboeuf
2026-08-08 23:17 ` [PATCH v4 09/22] crypto: arm64: Move data to .rodata Josh Poimboeuf
2026-08-08 23:17 ` [PATCH v4 10/22] objtool: Allow setting --mnop without --mcount Josh Poimboeuf
2026-08-08 23:17 ` [PATCH v4 11/22] kbuild: Only run objtool if there is at least one command Josh Poimboeuf
2026-08-08 23:17 ` [PATCH v4 12/22] objtool: Ignore jumps to the end of the function for checksum runs Josh Poimboeuf
2026-08-08 23:17 ` [PATCH v4 13/22] objtool: Refactor elf_add_data() to use a growable data buffer Josh Poimboeuf
2026-08-08 23:17 ` Josh Poimboeuf [this message]
2026-08-08 23:17 ` [PATCH v4 15/22] objtool: Prevent kCFI hashes from being decoded as instructions Josh Poimboeuf
2026-08-08 23:17 ` [PATCH v4 16/22] objtool/klp: Add arm64 support for prefix/PFE detection Josh Poimboeuf
2026-08-08 23:17 ` [PATCH v4 17/22] objtool/klp: Filter arm64 mapping symbols in find_symbol_by_offset() Josh Poimboeuf
2026-08-08 23:17 ` [PATCH v4 18/22] objtool/klp: Don't correlate arm64 mapping symbols Josh Poimboeuf
2026-08-08 23:17 ` [PATCH v4 19/22] objtool/klp: Clone inline alternative replacements Josh Poimboeuf
2026-08-08 23:17 ` [PATCH v4 20/22] objtool/klp: Introduce objtool for arm64 Josh Poimboeuf
2026-08-08 23:17 ` [PATCH v4 21/22] klp-build: Support cross-compilation Josh Poimboeuf
2026-08-08 23:17 ` [PATCH v4 22/22] klp-build: Add arm64 syscall patching macro Josh Poimboeuf
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=e0856be4208a6bed2b22dffe2bf8e010470f49cf.1786230311.git.jpoimboe@kernel.org \
--to=jpoimboe@kernel.org \
--cc=catalin.marinas@arm.com \
--cc=herbert@gondor.apana.org.au \
--cc=joe.lawrence@redhat.com \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=live-patching@vger.kernel.org \
--cc=mark.rutland@arm.com \
--cc=mbenes@suse.cz \
--cc=nathan@kernel.org \
--cc=nsc@kernel.org \
--cc=peterz@infradead.org \
--cc=pmladek@suse.com \
--cc=song@kernel.org \
--cc=will@kernel.org \
--cc=x86@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