All of lore.kernel.org
 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 08/11] objtool/powerpc: Implement arch_support_alt_relocation()
Date: Wed,  2 Dec 2026 13:10:55 +0530	[thread overview]
Message-ID: <20261202074059.899491-9-shivangu@linux.ibm.com> (raw)
In-Reply-To: <20261202074059.899491-1-shivangu@linux.ibm.com>

Feature section replacement code is copied over the original code at
boot, but its PC-relative branches are resolved for the address it is
linked at, so they point to the wrong place after the copy.  The kernel
currently corrects them at boot in patch_feature_section().

Have objtool correct them at build time instead, by rewriting each such
branch in the replacement code so it is valid at its final location.
Report an error if a branch cannot be encoded there, for example because
it is out of range.

Also make arch_pc_relative_reloc() recognise the powerpc branch
relocations so that they reach arch_support_alt_relocation().

Co-developed-by: Nicholas Piggin <npiggin@gmail.com>
Signed-off-by: Nicholas Piggin <npiggin@gmail.com>
Co-developed-by: Sathvika Vasireddy <sv@linux.ibm.com>
Signed-off-by: Sathvika Vasireddy <sv@linux.ibm.com>
Signed-off-by: Shivang Upadhyay <shivangu@linux.ibm.com>
---
 tools/objtool/arch/powerpc/decode.c  |  6 +----
 tools/objtool/arch/powerpc/special.c | 39 +++++++++++++++++++++++++++-
 2 files changed, 39 insertions(+), 6 deletions(-)

diff --git a/tools/objtool/arch/powerpc/decode.c b/tools/objtool/arch/powerpc/decode.c
index e534ac1123b3..2273cab73087 100644
--- a/tools/objtool/arch/powerpc/decode.c
+++ b/tools/objtool/arch/powerpc/decode.c
@@ -102,11 +102,7 @@ unsigned long arch_jump_destination(struct instruction *insn)
 
 bool arch_pc_relative_reloc(struct reloc *reloc)
 {
-	/*
-	 * The powerpc build only allows certain relocation types, see
-	 * relocs_check.sh, and none of those accepted are PC relative.
-	 */
-	return false;
+	return reloc_type(reloc) == R_PPC_REL24 || reloc_type(reloc) == R_PPC_REL14;
 }
 
 void arch_initial_func_cfi_state(struct cfi_init_state *state)
diff --git a/tools/objtool/arch/powerpc/special.c b/tools/objtool/arch/powerpc/special.c
index 2db320796fee..1a34b08d6118 100644
--- a/tools/objtool/arch/powerpc/special.c
+++ b/tools/objtool/arch/powerpc/special.c
@@ -3,14 +3,51 @@
 #include <stdlib.h>
 #include <objtool/special.h>
 #include <objtool/builtin.h>
+#include <objtool/warn.h>
 
+#include <asm/text-patching.h>
+#include <asm/inst.h>
+
+#include "../../../arch/powerpc/lib/code-patching-branch.c"
 
 bool arch_support_alt_relocation(struct objtool_file *file,
 				 struct special_alt *special_alt,
 				 struct instruction *insn,
 				 struct reloc *reloc)
 {
-	exit(-1);
+	struct elf *elf = file->elf;
+	u64 alt = special_alt->new_sec->sh.sh_addr + special_alt->new_off;
+	u64 orig = special_alt->orig_sec->sh.sh_addr + special_alt->orig_off;
+	u64 insn_addr = insn->sec->sh.sh_addr + insn->offset;
+	struct symbol *sym = reloc->sym;
+	u64 target = sym->sym.st_value + reloc_addend(reloc);
+	u64 dest;
+	u32 *word = insn->sec->data->d_buf + insn->offset;
+	u32 val = bswap_if_needed(elf, *word);
+	ppc_inst_t instr = ppc_inst(val);
+	int err;
+
+	/* The linker points direct calls to the local entry point */
+	if (is_func_sym(sym))
+		target += PPC64_LOCAL_ENTRY_OFFSET(sym->sym.st_other);
+
+	dest = insn_addr + orig - alt;
+
+	if (instr_is_branch_iform(instr))
+		err = create_branch(&instr, (const u32 *)dest, target, ppc_inst_val(instr));
+	else if (instr_is_branch_bform(instr))
+		err = create_cond_branch(&instr, (const u32 *)dest, target, ppc_inst_val(instr));
+	else
+		err = 1;
+
+	if (err) {
+		ERROR_INSN(insn, "alternative branch out of range");
+		return false;
+	}
+
+	val = bswap_if_needed(elf, ppc_inst_val(instr));
+	return !elf_write_insn(elf, insn->sec, insn->offset, sizeof(val),
+			       (const char *)&val);
 }
 
 struct reloc *arch_find_switch_table(struct objtool_file *file,
-- 
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 ` [PATCH v3 06/11] objtool: Describe special_entry parsing for --alt-fixup Shivang Upadhyay
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 ` Shivang Upadhyay [this message]
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-9-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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.