mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Shivang Upadhyay <shivangu@linux.ibm.com>
To: linuxppc-dev@lists.ozlabs.org, linux-kernel@vger.kernel.org
Cc: maddy@linux.ibm.com, mpe@ellerman.id.au, npiggin@gmail.com,
	chleroy@kernel.org, ritesh.list@gmail.com, sshegde@linux.ibm.com,
	jpoimboe@kernel.org, peterz@infradead.org,
	shivangu@linux.ibm.com, ardb@kernel.org, will@kernel.org,
	sv@linux.ibm.com, joe.lawrence@redhat.com, song@kernel.org,
	mingo@kernel.org, mbenes@suse.cz
Subject: [PATCH v3 06/11] objtool: Describe special_entry parsing for --alt-fixup
Date: Wed,  2 Dec 2026 13:10:53 +0530	[thread overview]
Message-ID: <20261202074059.899491-7-shivangu@linux.ibm.com> (raw)
In-Reply-To: <20261202074059.899491-1-shivangu@linux.ibm.com>

struct special_entry describes a special section entry with a length of
the original and of the replacement code, in bytes. Powerpc tables store
where the code ends instead, as a relocation against the end label.

For these entries, we compute orig_len and new_len, with help of
find_reloc_by_dest and reloc_to_sec_off function. Parse these entries
when --alt-fixup is provided.

BEGIN_FTR_SECTION/END_FTR_SECTION and friends record, for each pair of
original and replacement code, an entry in __ftr_fixup, __mmu_ftr_fixup
or __fw_ftr_fixup (see MAKE_FTR_SECTION_ENTRY()):

	word mask, value, start, end, alt_start, alt_end;

Add these sections to special_entries, so they can be parsed.

Signed-off-by: Shivang Upadhyay <shivangu@linux.ibm.com>
---
 .../arch/powerpc/include/arch/special.h       | 16 ++++
 tools/objtool/special.c                       | 80 ++++++++++++++++++-
 2 files changed, 93 insertions(+), 3 deletions(-)

diff --git a/tools/objtool/arch/powerpc/include/arch/special.h b/tools/objtool/arch/powerpc/include/arch/special.h
index ffef9ada7133..5667a476fd16 100644
--- a/tools/objtool/arch/powerpc/include/arch/special.h
+++ b/tools/objtool/arch/powerpc/include/arch/special.h
@@ -18,4 +18,20 @@
 #define ALT_ORIG_LEN_OFFSET 10
 #define ALT_NEW_LEN_OFFSET 11
 
+/*
+ * Feature fixup entries (__ftr_fixup, __mmu_ftr_fixup, __fw_ftr_fixup), see
+ * MAKE_FTR_SECTION_ENTRY() in asm/feature-fixups.h:
+ *
+ *	word mask, value;
+ *	word start, end, alt_start, alt_end;
+ *
+ * The sizes below are in words, objtool multiplies them by the ELF word size.
+ */
+
+#define FTR_ENTRY_SIZE		6
+#define FTR_ORIG_OFFSET		2
+#define FTR_ORIG_END_OFFSET	3
+#define FTR_NEW_OFFSET		4
+#define FTR_NEW_END_OFFSET	5
+
 #endif /* _PPC_ARCH_SPECIAL_H */
diff --git a/tools/objtool/special.c b/tools/objtool/special.c
index 2a533afbc69a..31420134d77e 100644
--- a/tools/objtool/special.c
+++ b/tools/objtool/special.c
@@ -23,9 +23,26 @@ struct special_entry {
 	unsigned char orig_len, new_len; /* group only */
 	unsigned char feature; /* ALTERNATIVE macro CPU feature */
 	unsigned char key; /* jump_label key */
+
+	// --alt-fixup entries, read by nothing else.
+	bool fixup;
+	int orig_end, new_end;
 };
 
-static const struct special_entry entries[] = {
+/* powerpc feature fixups, see arch/powerpc/include/asm/feature-fixups.h */
+#define FTR_FIXUP_ENTRY(name)			\
+	{					\
+		.sec = name,			\
+		.group = true,			\
+		.size = FTR_ENTRY_SIZE,		\
+		.orig = FTR_ORIG_OFFSET,	\
+		.new = FTR_NEW_OFFSET,		\
+		.fixup = true,			\
+		.orig_end = FTR_ORIG_END_OFFSET,\
+		.new_end = FTR_NEW_END_OFFSET,	\
+	}
+
+static struct special_entry entries[] = {
 	{
 		.sec = ".altinstructions",
 		.group = true,
@@ -50,6 +67,11 @@ static const struct special_entry entries[] = {
 		.orig = EX_ORIG_OFFSET,
 		.new = EX_NEW_OFFSET,
 	},
+#ifdef FTR_ENTRY_SIZE
+	FTR_FIXUP_ENTRY("__ftr_fixup"),
+	FTR_FIXUP_ENTRY("__mmu_ftr_fixup"),
+	FTR_FIXUP_ENTRY("__fw_ftr_fixup"),
+#endif
 	{},
 };
 
@@ -93,9 +115,25 @@ static int get_alt_entry(struct elf *elf, const struct special_entry *entry,
 
 	reloc_to_sec_off(orig_reloc, &alt->orig_sec, &alt->orig_off);
 
+	if (entry->fixup) {
+			struct section *end_sec;
+			unsigned long end_off;
+
+			alt->orig_off -= entry->orig;
+			struct reloc *end_reloc = find_reloc_by_dest(elf, sec, idx * entry->size + entry->orig_end);
+			if (!end_reloc) {
+				ERROR_FUNC(sec,  idx * entry->size + entry->orig_end, "can't find reloc");
+				return -1;
+			}
+			reloc_to_sec_off(end_reloc, &end_sec, &end_off);
+			end_off -= entry->orig_end;
+
+			alt->orig_len = end_off - alt->orig_off;
+	}
+
 	arch_handle_alternative(alt);
 
-	if (!entry->group || alt->new_len) {
+	if (!entry->group || alt->new_len || entry->fixup) {
 		new_reloc = find_reloc_by_dest(elf, sec, offset + entry->new);
 		if (!new_reloc) {
 			ERROR_FUNC(sec, offset + entry->new, "can't find new reloc");
@@ -104,6 +142,22 @@ static int get_alt_entry(struct elf *elf, const struct special_entry *entry,
 
 		reloc_to_sec_off(new_reloc, &alt->new_sec, &alt->new_off);
 
+		if (entry->fixup) {
+			struct section *end_sec;
+			unsigned long end_off;
+
+			alt->new_off -= entry->new;
+			struct reloc *end_reloc = find_reloc_by_dest(elf, sec, idx * entry->size + entry->new_end);
+			if (!end_reloc) {
+				ERROR_FUNC(sec,  idx * entry->size + entry->new_end, "can't find reloc");
+				return -1;
+			}
+			reloc_to_sec_off(end_reloc, &end_sec, &end_off);
+			end_off -= entry->new_end;
+
+			alt->new_len = end_off - alt->new_off;
+		}
+
 		/* _ASM_EXTABLE_EX hack */
 		if (alt->new_off >= 0x7ffffff0)
 			alt->new_off -= 0x7ffffff0;
@@ -130,7 +184,7 @@ static int get_alt_entry(struct elf *elf, const struct special_entry *entry,
  */
 int special_get_alts(struct elf *elf, struct list_head *alts)
 {
-	const struct special_entry *entry;
+	struct special_entry *entry;
 	struct section *sec;
 	unsigned int nr_entries;
 	struct special_alt *alt;
@@ -139,6 +193,17 @@ int special_get_alts(struct elf *elf, struct list_head *alts)
 	INIT_LIST_HEAD(alts);
 
 	for (entry = entries; entry->sec; entry++) {
+		/* The fixup layout is given in words, scale it to the ELF word size */
+		if (entry->fixup) {
+			size_t word = elf_addr_size(elf);
+
+			entry->size *= word;
+			entry->orig *= word;
+			entry->new *= word;
+			entry->orig_end *= word;
+			entry->new_end *= word;
+		}
+
 		sec = find_section_by_name(elf, entry->sec);
 		if (!sec)
 			continue;
@@ -161,6 +226,15 @@ int special_get_alts(struct elf *elf, struct list_head *alts)
 			if (get_alt_entry(elf, entry, sec, idx, alt))
 				return -1;
 
+			/*
+			 * Nothing to copy and fix up if the original is just
+			 * nopped. Such an entry can overlap other ones.
+			 */
+			if (entry->fixup && !alt->new_len) {
+				free(alt);
+				continue;
+			}
+
 			list_add_tail(&alt->list, alts);
 		}
 	}
-- 
2.56.1


  parent reply	other threads:[~2026-12-02  7:42 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-02  7:40 [PATCH v3 00/11] objtool/powerpc: Fix up feature section alternatives at build time Shivang Upadhyay
2026-12-02  7:40 ` [PATCH v3 01/11] objtool: Make symbol offsets section relative in linked objects Shivang Upadhyay
2026-12-02  7:40 ` [PATCH v3 02/11] objtool: Make relocation " Shivang Upadhyay
2026-12-02  7:40 ` [PATCH v3 03/11] objtool: Keep the layout of linked objects when writing them back Shivang Upadhyay
2026-12-02  7:40 ` [PATCH v3 04/11] objtool: Pass the objtool_file to arch_support_alt_relocation() Shivang Upadhyay
2026-12-02  7:40 ` [PATCH v3 05/11] objtool: Add --alt-fixup Shivang Upadhyay
2026-12-02  7:40 ` Shivang Upadhyay [this message]
2026-12-02  7:40 ` [PATCH v3 07/11] powerpc/lib: Extract branch patching helpers to code-patching-branch.c Shivang Upadhyay
2026-12-02  7:40 ` [PATCH v3 08/11] objtool/powerpc: Implement arch_support_alt_relocation() Shivang Upadhyay
2026-12-02  7:40 ` [PATCH v3 09/11] powerpc: Mark feature section alternatives as executable Shivang Upadhyay
2026-12-02  7:40 ` [PATCH v3 10/11] powerpc: Always build objtool for powerpc Shivang Upadhyay
2026-12-02  7:40 ` [PATCH v3 11/11] powerpc: Remove runtime checking of jumps in core kernel feature-fixup Shivang Upadhyay

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=20261202074059.899491-7-shivangu@linux.ibm.com \
    --to=shivangu@linux.ibm.com \
    --cc=ardb@kernel.org \
    --cc=chleroy@kernel.org \
    --cc=joe.lawrence@redhat.com \
    --cc=jpoimboe@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linuxppc-dev@lists.ozlabs.org \
    --cc=maddy@linux.ibm.com \
    --cc=mbenes@suse.cz \
    --cc=mingo@kernel.org \
    --cc=mpe@ellerman.id.au \
    --cc=npiggin@gmail.com \
    --cc=peterz@infradead.org \
    --cc=ritesh.list@gmail.com \
    --cc=song@kernel.org \
    --cc=sshegde@linux.ibm.com \
    --cc=sv@linux.ibm.com \
    --cc=will@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

all inboxes | Powered by JetHome®