mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v3 00/11] objtool/powerpc: Fix up feature section alternatives at build time
@ 2026-12-02  7:40 Shivang Upadhyay
  2026-12-02  7:40 ` [PATCH v3 01/11] objtool: Make symbol offsets section relative in linked objects Shivang Upadhyay
                   ` (10 more replies)
  0 siblings, 11 replies; 12+ messages in thread
From: Shivang Upadhyay @ 2026-12-02  7:40 UTC (permalink / raw)
  To: linuxppc-dev, linux-kernel
  Cc: maddy, mpe, npiggin, chleroy, ritesh.list, sshegde, jpoimboe,
	peterz, shivangu, ardb, will, sv, joe.lawrence, song, mingo,
	mbenes

The replacement code of a powerpc feature section (BEGIN_FTR_SECTION /
FTR_SECTION_ELSE / ALT_FTR_SECTION_END macros) is emitted into a __ftr_alt_*
section.  At boot, apply_feature_fixups() copies the replacement code over it.
Since the copy lands at a different address than the one it was linked for, the
PC-relative branches in it have to be translated, which is currently done at
runtime by patch_feature_section().

This has two drawbacks:

 - __ftr_alt_* sections are allocatable but not executable, so the
   linker cannot insert branch stubs for them.  They therefore have to
   be placed close to .text, and every branch in them must reach its
   target directly.  This can break the build as the kernel grows.

 - The branches are translated on every boot, although everything
   needed to do it is known at link time.

This series moves the translation to build time.  After vmlinux is
linked, objtool is run on it with the new --alt-fixup option.  It parses
the __ftr_fixup, __mmu_ftr_fixup and __fw_ftr_fixup tables, and for each
PC-relative relocation in the replacement code, asks the arch to rewrite
the instruction so that it is valid at the place it will be copied to.
With that, the __ftr_alt_* sections are marked executable and moved out
of .text into their own output section, so the linker can insert stubs
where needed.

Objtool does not process modules or the VDSO, so the runtime translation
is kept for those and only skipped for the core kernel.

Previously, Nicholas Piggin proposed a build-time solution using a custom
powerpc tool [1].  The approach here builds on that idea, but uses
objtool's existing ELF parsing and instruction patching infrastructure
instead of a separate tool.

Patch overview:

  1-3   Teach objtool about linked objects (sections with a non-zero
        address): make symbol and relocation offsets section relative,
        and keep the file layout when writing it back.
  4-6   Add --alt-fixup, built on top of the existing alternatives
        handling, and describe the powerpc feature fixup tables to it.
  7-8   Share the powerpc branch helpers between the kernel and
        objtool, and implement arch_support_alt_relocation() for
        powerpc.
  9-11  Make __ftr_alt_* executable, always build objtool for powerpc,
        run it post-link, and stop translating branches at boot for the
        core kernel.

This patchset applies atop upstream master branch.

Testing:
  verfied build and boot on ppc64le, ppc64, ppc32be.

[1] Original powerpc tool approach:
    http://patchwork.ozlabs.org/project/linuxppc-dev/patch/20170521010130.13552-1-npiggin@gmail.com/

---
===== Changelog ======

Changes since v2: https://lore.kernel.org/all/20260929055138.43489-1-shivangu@linux.ibm.com/
- Reworked after Peter Zijlstra's review: objtool now understands linked
  objects (sections with a non-zero address) instead of special casing
  --ftr-fixup.  Symbol and relocation offsets are made section relative
  (1-2/11), which replaces the find_insn() offset hack.
- ELF_F_LAYOUT is now set for every non-ET_REL file instead of only with
  --ftr-fixup (3/11).
- --ftr-fixup and its powerpc specific parsing are replaced
  by --alt-fixup, which reuses the generic alternatives code and
  arch_support_alt_relocation() (4-6/11, 8/11).  The hacks that skipped
  add_jump_destinations() and silenced intra-function call warnings are
  gone, objtool just stops after processing the alternatives.
- The powerpc branch helpers are shared between the kernel and objtool
  through code-patching-branch.c rather than duplicated (7/11).
- Dropped HAVE_OBJTOOL_FTR_FIXUP and the top level kbuild changes;
  ARCH_VMLINUX_NEEDS_RELOCS and a vmlinux.unstripped rule in
  Makefile.postlink are used instead (10/11).  The runtime branch
  translation is kept for modules and the VDSO (11/11).


v1: https://lore.kernel.org/all/20260505084628.17940-1-sv@linux.ibm.com/

=======================

Shivang Upadhyay (11):
  objtool: Make symbol offsets section relative in linked objects
  objtool: Make relocation offsets section relative in linked objects
  objtool: Keep the layout of linked objects when writing them back
  objtool: Pass the objtool_file to arch_support_alt_relocation()
  objtool: Add --alt-fixup
  objtool: Describe special_entry parsing for --alt-fixup
  powerpc/lib: Extract branch patching helpers to code-patching-branch.c
  objtool/powerpc: Implement arch_support_alt_relocation()
  powerpc: Mark feature section alternatives as executable
  powerpc: Always build objtool for powerpc
  powerpc: Remove runtime checking of jumps in core kernel feature-fixup

 arch/powerpc/Kconfig                          |   1 +
 arch/powerpc/Makefile                         |   2 +-
 arch/powerpc/Makefile.postlink                |   4 +
 arch/powerpc/include/asm/feature-fixups.h     |   2 +-
 arch/powerpc/include/asm/text-patching.h      |   3 +
 arch/powerpc/kernel/vmlinux.lds.S             |   7 +-
 arch/powerpc/lib/code-patching-branch.c       | 128 ++++++++++++++++++
 arch/powerpc/lib/code-patching.c              | 110 +--------------
 arch/powerpc/lib/feature-fixups.c             |  46 ++++---
 tools/arch/powerpc/include/asm/inst.h         |  41 ++++++
 .../arch/powerpc/include/asm/text-patching.h  |  67 +++++++++
 tools/objtool/arch/loongarch/special.c        |   3 +-
 tools/objtool/arch/powerpc/decode.c           |   6 +-
 .../arch/powerpc/include/arch/special.h       |  16 +++
 tools/objtool/arch/powerpc/special.c          |  42 +++++-
 tools/objtool/arch/x86/special.c              |   3 +-
 tools/objtool/builtin-check.c                 |   4 +-
 tools/objtool/check.c                         |   7 +-
 tools/objtool/elf.c                           |  13 ++
 tools/objtool/include/objtool/builtin.h       |   1 +
 tools/objtool/include/objtool/elf.h           |  14 +-
 tools/objtool/include/objtool/special.h       |   3 +-
 tools/objtool/special.c                       |  80 ++++++++++-
 23 files changed, 456 insertions(+), 147 deletions(-)
 create mode 100644 arch/powerpc/lib/code-patching-branch.c
 create mode 100644 tools/arch/powerpc/include/asm/inst.h
 create mode 100644 tools/arch/powerpc/include/asm/text-patching.h

-- 
2.56.1


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

* [PATCH v3 01/11] objtool: Make symbol offsets section relative in linked objects
  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 ` Shivang Upadhyay
  2026-12-02  7:40 ` [PATCH v3 02/11] objtool: Make relocation " Shivang Upadhyay
                   ` (9 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: Shivang Upadhyay @ 2026-12-02  7:40 UTC (permalink / raw)
  To: linuxppc-dev, linux-kernel
  Cc: maddy, mpe, npiggin, chleroy, ritesh.list, sshegde, jpoimboe,
	peterz, shivangu, ardb, will, sv, joe.lawrence, song, mingo,
	mbenes

objtool expects symbol addresses to be offsets from the start of their
section, but in linked object files they are absolute. Subtract the
section's address when it is non-zero to get the section-relative
offset.

Signed-off-by: Shivang Upadhyay <shivangu@linux.ibm.com>
---
 tools/objtool/elf.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/tools/objtool/elf.c b/tools/objtool/elf.c
index a791f4ea6ec1..493cb9f21dde 100644
--- a/tools/objtool/elf.c
+++ b/tools/objtool/elf.c
@@ -522,6 +522,12 @@ static int elf_add_symbol(struct elf *elf, struct symbol *sym)
 		elf->num_files++;
 
 	sym->offset = sym->sym.st_value;
+
+	// If section address is given (ex. linked objects), Remove it from
+	// symbol address.
+	if (sym->sec->sh.sh_addr)
+		sym->offset -= sym->sec->sh.sh_addr;
+
 	sym->len = sym->sym.st_size;
 
 	__sym_for_each(iter, &sym->sec->symbol_tree, sym->offset, sym->offset) {
-- 
2.56.1


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

* [PATCH v3 02/11] objtool: Make relocation offsets section relative in linked objects
  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 ` Shivang Upadhyay
  2026-12-02  7:40 ` [PATCH v3 03/11] objtool: Keep the layout of linked objects when writing them back Shivang Upadhyay
                   ` (8 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: Shivang Upadhyay @ 2026-12-02  7:40 UTC (permalink / raw)
  To: linuxppc-dev, linux-kernel
  Cc: maddy, mpe, npiggin, chleroy, ritesh.list, sshegde, jpoimboe,
	peterz, shivangu, ardb, will, sv, joe.lawrence, song, mingo,
	mbenes

In a relocatable object, a relocation's r_offset is an offset into
the section being relocated. In a linked object whose relocations
were preserved by the linker (--emit-relocs), it is a virtual address
instead.

Make reloc_offset() and set_reloc_offset() convert between the two,
using the base section's sh_addr. For a relocatable object sh_addr is 0,
so their behavior doesn't change.

Signed-off-by: Shivang Upadhyay <shivangu@linux.ibm.com>
---
 tools/objtool/include/objtool/elf.h | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)

diff --git a/tools/objtool/include/objtool/elf.h b/tools/objtool/include/objtool/elf.h
index a82517a76a0f..e12fca2cc33b 100644
--- a/tools/objtool/include/objtool/elf.h
+++ b/tools/objtool/include/objtool/elf.h
@@ -396,14 +396,24 @@ static inline unsigned long sec_size(struct section *sec)
 		((Elf64_Rela *)reloc_rel(reloc))->field = val;		\
 })
 
+/*
+ * Relocation offsets in linked objects are absolute addresses; present them
+ * as offsets into the relocated section like in relocatable objects (where
+ * sh_addr is 0).
+ */
+static inline u64 reloc_base_addr(struct reloc *reloc)
+{
+	return reloc->sec->base ? reloc->sec->base->sh.sh_addr : 0;
+}
+
 static inline u64 reloc_offset(struct reloc *reloc)
 {
-	return __get_reloc_field(reloc, r_offset);
+	return __get_reloc_field(reloc, r_offset) - reloc_base_addr(reloc);
 }
 
 static inline void set_reloc_offset(struct elf *elf, struct reloc *reloc, u64 offset)
 {
-	__set_reloc_field(reloc, r_offset, offset);
+	__set_reloc_field(reloc, r_offset, offset + reloc_base_addr(reloc));
 	mark_sec_changed(elf, reloc->sec, true);
 }
 
-- 
2.56.1


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

* [PATCH v3 03/11] objtool: Keep the layout of linked objects when writing them back
  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 ` Shivang Upadhyay
  2026-12-02  7:40 ` [PATCH v3 04/11] objtool: Pass the objtool_file to arch_support_alt_relocation() Shivang Upadhyay
                   ` (7 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: Shivang Upadhyay @ 2026-12-02  7:40 UTC (permalink / raw)
  To: linuxppc-dev, linux-kernel
  Cc: maddy, mpe, npiggin, chleroy, ritesh.list, sshegde, jpoimboe,
	peterz, shivangu, ardb, will, sv, joe.lawrence, song, mingo,
	mbenes

When objtool modifies a file, libelf recomputes section offsets and
rewrites it. That is fine for a relocatable object, but a linked object
has a layout already fixed by the linker: program headers refer to file
offsets, and section contents are what gets loaded. Letting libelf
rearrange that layout corrupts the file.

Set ELF_F_LAYOUT for anything that is not ET_REL so libelf preserves the
existing layout. Objtool only patches bytes in place in that case.

Signed-off-by: Shivang Upadhyay <shivangu@linux.ibm.com>
---
 tools/objtool/elf.c | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/tools/objtool/elf.c b/tools/objtool/elf.c
index 493cb9f21dde..cb5e9f75a16e 100644
--- a/tools/objtool/elf.c
+++ b/tools/objtool/elf.c
@@ -1250,6 +1250,13 @@ struct elf *elf_open_read(const char *name, int flags)
 		goto err;
 	}
 
+	/*
+	 * A linked object has a layout fixed by the linker (segments etc.);
+	 * don't let libelf rearrange it when writing back.
+	 */
+	if (elf->ehdr.e_type != ET_REL)
+		elf_flagelf(elf->elf, ELF_C_SET, ELF_F_LAYOUT);
+
 	if (read_sections(elf))
 		goto err;
 
-- 
2.56.1


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

* [PATCH v3 04/11] objtool: Pass the objtool_file to arch_support_alt_relocation()
  2026-12-02  7:40 [PATCH v3 00/11] objtool/powerpc: Fix up feature section alternatives at build time Shivang Upadhyay
                   ` (2 preceding siblings ...)
  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 ` Shivang Upadhyay
  2026-12-02  7:40 ` [PATCH v3 05/11] objtool: Add --alt-fixup Shivang Upadhyay
                   ` (6 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: Shivang Upadhyay @ 2026-12-02  7:40 UTC (permalink / raw)
  To: linuxppc-dev, linux-kernel
  Cc: maddy, mpe, npiggin, chleroy, ritesh.list, sshegde, jpoimboe,
	peterz, shivangu, ardb, will, sv, joe.lawrence, song, mingo,
	mbenes

arch_support_alt_relocation() is called for each PC-relative relocation
in the replacement code of an alternative.  It only gets the special_alt,
the instruction and the relocation, which is enough to tell whether an
arch can cope with it, but not to act on it: an arch that wants to fix
the instruction up has no way to reach the ELF file.

Pass the objtool_file along.  No functional change. Will be utilized in
next patches.

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

diff --git a/tools/objtool/arch/loongarch/special.c b/tools/objtool/arch/loongarch/special.c
index aba774109437..585ba327fe86 100644
--- a/tools/objtool/arch/loongarch/special.c
+++ b/tools/objtool/arch/loongarch/special.c
@@ -3,7 +3,8 @@
 #include <objtool/special.h>
 #include <objtool/warn.h>
 
-bool arch_support_alt_relocation(struct special_alt *special_alt,
+bool arch_support_alt_relocation(struct objtool_file *file,
+				 struct special_alt *special_alt,
 				 struct instruction *insn,
 				 struct reloc *reloc)
 {
diff --git a/tools/objtool/arch/powerpc/special.c b/tools/objtool/arch/powerpc/special.c
index 8f9bf61ca089..2db320796fee 100644
--- a/tools/objtool/arch/powerpc/special.c
+++ b/tools/objtool/arch/powerpc/special.c
@@ -5,7 +5,8 @@
 #include <objtool/builtin.h>
 
 
-bool arch_support_alt_relocation(struct special_alt *special_alt,
+bool arch_support_alt_relocation(struct objtool_file *file,
+				 struct special_alt *special_alt,
 				 struct instruction *insn,
 				 struct reloc *reloc)
 {
diff --git a/tools/objtool/arch/x86/special.c b/tools/objtool/arch/x86/special.c
index 1e84c81bfcd8..839997915711 100644
--- a/tools/objtool/arch/x86/special.c
+++ b/tools/objtool/arch/x86/special.c
@@ -62,7 +62,8 @@ void arch_handle_alternative(struct special_alt *alt)
 	prev = alt;
 }
 
-bool arch_support_alt_relocation(struct special_alt *special_alt,
+bool arch_support_alt_relocation(struct objtool_file *file,
+				 struct special_alt *special_alt,
 				 struct instruction *insn,
 				 struct reloc *reloc)
 {
diff --git a/tools/objtool/check.c b/tools/objtool/check.c
index 4ef5bf97d333..357676135680 100644
--- a/tools/objtool/check.c
+++ b/tools/objtool/check.c
@@ -1833,7 +1833,7 @@ static int handle_group_alt(struct objtool_file *file,
 		 */
 		alt_reloc = insn_reloc(file, insn);
 		if (alt_reloc && arch_pc_relative_reloc(alt_reloc) &&
-		    !arch_support_alt_relocation(special_alt, insn, alt_reloc)) {
+		    !arch_support_alt_relocation(file, special_alt, insn, alt_reloc)) {
 
 			ERROR_INSN(insn, "unsupported relocation in alternatives section");
 			return -1;
diff --git a/tools/objtool/include/objtool/special.h b/tools/objtool/include/objtool/special.h
index 620dbf6cb0e5..eeaed4e62b86 100644
--- a/tools/objtool/include/objtool/special.h
+++ b/tools/objtool/include/objtool/special.h
@@ -39,7 +39,8 @@ void arch_handle_alternative(struct special_alt *alt);
  */
 bool arch_alt_ignore_new_reloc(struct section *sec, unsigned long offset);
 
-bool arch_support_alt_relocation(struct special_alt *special_alt,
+bool arch_support_alt_relocation(struct objtool_file *file,
+				 struct special_alt *special_alt,
 				 struct instruction *insn,
 				 struct reloc *reloc);
 struct reloc *arch_find_switch_table(struct objtool_file *file,
-- 
2.56.1


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

* [PATCH v3 05/11] objtool: Add --alt-fixup
  2026-12-02  7:40 [PATCH v3 00/11] objtool/powerpc: Fix up feature section alternatives at build time Shivang Upadhyay
                   ` (3 preceding siblings ...)
  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 ` Shivang Upadhyay
  2026-12-02  7:40 ` [PATCH v3 06/11] objtool: Describe special_entry parsing for --alt-fixup Shivang Upadhyay
                   ` (5 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: Shivang Upadhyay @ 2026-12-02  7:40 UTC (permalink / raw)
  To: linuxppc-dev, linux-kernel
  Cc: maddy, mpe, npiggin, chleroy, ritesh.list, sshegde, jpoimboe,
	peterz, shivangu, ardb, will, sv, joe.lawrence, song, mingo,
	mbenes

Add --alt-fixup, an action that reads the alternatives of a linked object
and lets the arch rewrite the replacement code through
arch_support_alt_relocation(), which handle_group_alt() already calls
for each pc-relative relocation in it.  Nothing else is validated, so
return from decode_file() once the alternatives have been processed.

Signed-off-by: Shivang Upadhyay <shivangu@linux.ibm.com>
---
 tools/objtool/builtin-check.c           | 4 +++-
 tools/objtool/check.c                   | 5 +++++
 tools/objtool/include/objtool/builtin.h | 1 +
 3 files changed, 9 insertions(+), 1 deletion(-)

diff --git a/tools/objtool/builtin-check.c b/tools/objtool/builtin-check.c
index 75b11dc85010..916ce58bc8b5 100644
--- a/tools/objtool/builtin-check.c
+++ b/tools/objtool/builtin-check.c
@@ -73,6 +73,7 @@ static int parse_hacks(const struct option *opt, const char *str, int unset)
 
 static const struct option check_options[] = {
 	OPT_GROUP("Actions:"),
+	OPT_BOOLEAN(0,		 "alt-fixup", &opts.alt_fixup, "fix up relocations in alternative replacement code"),
 	OPT_STRING_OPTARG('d',	 "disas", &opts.disas, "function-pattern", "disassemble functions", "*"),
 	OPT_CALLBACK_OPTARG('h', "hacks", NULL, NULL, "jump_label,noinstr,skylake", "patch toolchain bugs/limitations", parse_hacks),
 	OPT_BOOLEAN('i',	 "ibt", &opts.ibt, "validate and annotate IBT"),
@@ -180,7 +181,8 @@ static bool opts_valid(void)
 		return false;
 	}
 
-	if (opts.disas			||
+	if (opts.alt_fixup		||
+	    opts.disas			||
 	    opts.hack_jump_label	||
 	    opts.hack_noinstr		||
 	    opts.ibt			||
diff --git a/tools/objtool/check.c b/tools/objtool/check.c
index 357676135680..6c1bf0aeb3b1 100644
--- a/tools/objtool/check.c
+++ b/tools/objtool/check.c
@@ -2603,6 +2603,7 @@ static bool validate_branch_enabled(void)
 static bool alts_needed(void)
 {
 	return validate_branch_enabled()	||
+	       opts.alt_fixup			||
 	       opts.noinstr			||
 	       opts.hack_jump_label		||
 	       opts.disas			||
@@ -2657,6 +2658,10 @@ int decode_file(struct objtool_file *file)
 			return -1;
 	}
 
+	/* Fixing up the alternatives is all there is to do */
+	if (opts.alt_fixup)
+		return 0;
+
 	if (add_jump_destinations(file))
 		return -1;
 
diff --git a/tools/objtool/include/objtool/builtin.h b/tools/objtool/include/objtool/builtin.h
index 349690bb1c50..35f94151732e 100644
--- a/tools/objtool/include/objtool/builtin.h
+++ b/tools/objtool/include/objtool/builtin.h
@@ -9,6 +9,7 @@
 
 struct opts {
 	/* actions: */
+	bool alt_fixup;
 	bool checksum;
 	const char *disas;
 	bool dump_orc;
-- 
2.56.1


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

* [PATCH v3 06/11] objtool: Describe special_entry parsing for --alt-fixup
  2026-12-02  7:40 [PATCH v3 00/11] objtool/powerpc: Fix up feature section alternatives at build time Shivang Upadhyay
                   ` (4 preceding siblings ...)
  2026-12-02  7:40 ` [PATCH v3 05/11] objtool: Add --alt-fixup Shivang Upadhyay
@ 2026-12-02  7:40 ` Shivang Upadhyay
  2026-12-02  7:40 ` [PATCH v3 07/11] powerpc/lib: Extract branch patching helpers to code-patching-branch.c Shivang Upadhyay
                   ` (4 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: Shivang Upadhyay @ 2026-12-02  7:40 UTC (permalink / raw)
  To: linuxppc-dev, linux-kernel
  Cc: maddy, mpe, npiggin, chleroy, ritesh.list, sshegde, jpoimboe,
	peterz, shivangu, ardb, will, sv, joe.lawrence, song, mingo,
	mbenes

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


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

* [PATCH v3 07/11] powerpc/lib: Extract branch patching helpers to code-patching-branch.c
  2026-12-02  7:40 [PATCH v3 00/11] objtool/powerpc: Fix up feature section alternatives at build time Shivang Upadhyay
                   ` (5 preceding siblings ...)
  2026-12-02  7:40 ` [PATCH v3 06/11] objtool: Describe special_entry parsing for --alt-fixup Shivang Upadhyay
@ 2026-12-02  7:40 ` Shivang Upadhyay
  2026-12-02  7:40 ` [PATCH v3 08/11] objtool/powerpc: Implement arch_support_alt_relocation() Shivang Upadhyay
                   ` (3 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: Shivang Upadhyay @ 2026-12-02  7:40 UTC (permalink / raw)
  To: linuxppc-dev, linux-kernel
  Cc: maddy, mpe, npiggin, chleroy, ritesh.list, sshegde, jpoimboe,
	peterz, shivangu, ardb, will, sv, joe.lawrence, song, mingo,
	mbenes

Extract portable branch manipulation and decoding functions
(is_conditional_branch, create_cond_branch, instr_is_relative_branch,
instr_is_relative_link_branch, branch_target_at, translate_branch_at)
into arch/powerpc/lib/code-patching-branch.c.

Include code-patching-branch.c directly in arch/powerpc/lib/code-patching.c,
which avoids duplicating branch patching logic between the kernel runtime
and userspace host tools (like objtool).

Also, Add minimal versions of <asm/inst.h> and <asm/text-patching.h> under
tools/arch/powerpc/include/asm/ to provide instruction definitions
(ppc_inst_t) and inline branch creation helpers (create_branch, etc.)
for objtool and other user space tools.

Signed-off-by: Shivang Upadhyay <shivangu@linux.ibm.com>
---
 arch/powerpc/include/asm/text-patching.h      |   3 +
 arch/powerpc/lib/code-patching-branch.c       | 128 ++++++++++++++++++
 arch/powerpc/lib/code-patching.c              | 110 +--------------
 tools/arch/powerpc/include/asm/inst.h         |  41 ++++++
 .../arch/powerpc/include/asm/text-patching.h  |  67 +++++++++
 5 files changed, 240 insertions(+), 109 deletions(-)
 create mode 100644 arch/powerpc/lib/code-patching-branch.c
 create mode 100644 tools/arch/powerpc/include/asm/inst.h
 create mode 100644 tools/arch/powerpc/include/asm/text-patching.h

diff --git a/arch/powerpc/include/asm/text-patching.h b/arch/powerpc/include/asm/text-patching.h
index e7f14720f630..c3612636a381 100644
--- a/arch/powerpc/include/asm/text-patching.h
+++ b/arch/powerpc/include/asm/text-patching.h
@@ -157,7 +157,10 @@ static inline int instr_is_branch_bform(ppc_inst_t instr)
 int instr_is_relative_branch(ppc_inst_t instr);
 int instr_is_relative_link_branch(ppc_inst_t instr);
 unsigned long branch_target(const u32 *instr);
+unsigned long branch_target_at(const u32 *instr, unsigned long pc);
 int translate_branch(ppc_inst_t *instr, const u32 *dest, const u32 *src);
+int translate_branch_at(ppc_inst_t *instr, unsigned long dest,
+			const u32 *src, unsigned long src_addr);
 bool is_conditional_branch(ppc_inst_t instr);
 
 #define OP_RT_RA_MASK	0xffff0000UL
diff --git a/arch/powerpc/lib/code-patching-branch.c b/arch/powerpc/lib/code-patching-branch.c
new file mode 100644
index 000000000000..11be0b626748
--- /dev/null
+++ b/arch/powerpc/lib/code-patching-branch.c
@@ -0,0 +1,128 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ *  Copyright 2008 Michael Ellerman, IBM Corporation.
+ */
+
+#include <asm/text-patching.h>
+#include <asm/inst.h>
+
+/*
+ * Helper to check if a given instruction is a conditional branch
+ * Derived from the conditional checks in analyse_instr()
+ */
+bool is_conditional_branch(ppc_inst_t instr)
+{
+	unsigned int opcode = ppc_inst_primary_opcode(instr);
+
+	if (opcode == 16)       /* bc, bca, bcl, bcla */
+		return true;
+	if (opcode == 19) {
+		switch ((ppc_inst_val(instr) >> 1) & 0x3ff) {
+		case 16:        /* bclr, bclrl */
+		case 528:       /* bcctr, bcctrl */
+		case 560:       /* bctar, bctarl */
+			return true;
+		}
+	}
+	return false;
+}
+
+int create_cond_branch(ppc_inst_t *instr, const u32 *addr,
+		       unsigned long target, int flags)
+{
+	long offset;
+
+	offset = target;
+	if (! (flags & BRANCH_ABSOLUTE))
+		offset = offset - (unsigned long)addr;
+
+	/* Check we can represent the target in the instruction format */
+	if (!is_offset_in_cond_branch_range(offset))
+		return 1;
+
+	/* Mask out the flags and target, so they don't step on each other. */
+	*instr = ppc_inst(0x40000000 | (flags & 0x3FF0003) | (offset & 0xFFFC));
+
+	return 0;
+}
+
+int instr_is_relative_branch(ppc_inst_t instr)
+{
+	if (ppc_inst_val(instr) & BRANCH_ABSOLUTE)
+		return 0;
+
+	return instr_is_branch_iform(instr) || instr_is_branch_bform(instr);
+}
+
+int instr_is_relative_link_branch(ppc_inst_t instr)
+{
+	return instr_is_relative_branch(instr) && (ppc_inst_val(instr) & BRANCH_SET_LINK);
+}
+
+static unsigned long branch_iform_target(const u32 *instr, unsigned long pc)
+{
+	signed long imm;
+
+	imm = ppc_inst_val(ppc_inst_read(instr)) & 0x3FFFFFC;
+
+	/* If the top bit of the immediate value is set this is negative */
+	if (imm & 0x2000000)
+		imm -= 0x4000000;
+
+	if ((ppc_inst_val(ppc_inst_read(instr)) & BRANCH_ABSOLUTE) == 0)
+		imm += pc;
+
+	return (unsigned long)imm;
+}
+
+static unsigned long branch_bform_target(const u32 *instr, unsigned long pc)
+{
+	signed long imm;
+
+	imm = ppc_inst_val(ppc_inst_read(instr)) & 0xFFFC;
+
+	/* If the top bit of the immediate value is set this is negative */
+	if (imm & 0x8000)
+		imm -= 0x10000;
+
+	if ((ppc_inst_val(ppc_inst_read(instr)) & BRANCH_ABSOLUTE) == 0)
+		imm += pc;
+
+	return (unsigned long)imm;
+}
+
+unsigned long branch_target_at(const u32 *instr, unsigned long pc)
+{
+	if (instr_is_branch_iform(ppc_inst_read(instr)))
+		return branch_iform_target(instr, pc);
+	else if (instr_is_branch_bform(ppc_inst_read(instr)))
+		return branch_bform_target(instr, pc);
+
+	return 0;
+}
+
+unsigned long branch_target(const u32 *instr)
+{
+	return branch_target_at(instr, (unsigned long)instr);
+}
+
+int translate_branch_at(ppc_inst_t *instr, unsigned long dest,
+			const u32 *src, unsigned long src_addr)
+{
+	unsigned long target;
+	target = branch_target_at(src, src_addr);
+
+	if (instr_is_branch_iform(ppc_inst_read(src)))
+		return create_branch(instr, (const u32 *)dest, target,
+				     ppc_inst_val(ppc_inst_read(src)));
+	else if (instr_is_branch_bform(ppc_inst_read(src)))
+		return create_cond_branch(instr, (const u32 *)dest, target,
+					  ppc_inst_val(ppc_inst_read(src)));
+
+	return 1;
+}
+
+int translate_branch(ppc_inst_t *instr, const u32 *dest, const u32 *src)
+{
+	return translate_branch_at(instr, (unsigned long)dest, src, (unsigned long)src);
+}
diff --git a/arch/powerpc/lib/code-patching.c b/arch/powerpc/lib/code-patching.c
index 44ff9f684bef..bfe4d93dcb6f 100644
--- a/arch/powerpc/lib/code-patching.c
+++ b/arch/powerpc/lib/code-patching.c
@@ -537,113 +537,5 @@ int patch_branch(u32 *addr, unsigned long target, int flags)
 	return patch_instruction(addr, instr);
 }
 
-/*
- * Helper to check if a given instruction is a conditional branch
- * Derived from the conditional checks in analyse_instr()
- */
-bool is_conditional_branch(ppc_inst_t instr)
-{
-	unsigned int opcode = ppc_inst_primary_opcode(instr);
-
-	if (opcode == 16)       /* bc, bca, bcl, bcla */
-		return true;
-	if (opcode == 19) {
-		switch ((ppc_inst_val(instr) >> 1) & 0x3ff) {
-		case 16:        /* bclr, bclrl */
-		case 528:       /* bcctr, bcctrl */
-		case 560:       /* bctar, bctarl */
-			return true;
-		}
-	}
-	return false;
-}
+#include "code-patching-branch.c"
 NOKPROBE_SYMBOL(is_conditional_branch);
-
-int create_cond_branch(ppc_inst_t *instr, const u32 *addr,
-		       unsigned long target, int flags)
-{
-	long offset;
-
-	offset = target;
-	if (! (flags & BRANCH_ABSOLUTE))
-		offset = offset - (unsigned long)addr;
-
-	/* Check we can represent the target in the instruction format */
-	if (!is_offset_in_cond_branch_range(offset))
-		return 1;
-
-	/* Mask out the flags and target, so they don't step on each other. */
-	*instr = ppc_inst(0x40000000 | (flags & 0x3FF0003) | (offset & 0xFFFC));
-
-	return 0;
-}
-
-int instr_is_relative_branch(ppc_inst_t instr)
-{
-	if (ppc_inst_val(instr) & BRANCH_ABSOLUTE)
-		return 0;
-
-	return instr_is_branch_iform(instr) || instr_is_branch_bform(instr);
-}
-
-int instr_is_relative_link_branch(ppc_inst_t instr)
-{
-	return instr_is_relative_branch(instr) && (ppc_inst_val(instr) & BRANCH_SET_LINK);
-}
-
-static unsigned long branch_iform_target(const u32 *instr)
-{
-	signed long imm;
-
-	imm = ppc_inst_val(ppc_inst_read(instr)) & 0x3FFFFFC;
-
-	/* If the top bit of the immediate value is set this is negative */
-	if (imm & 0x2000000)
-		imm -= 0x4000000;
-
-	if ((ppc_inst_val(ppc_inst_read(instr)) & BRANCH_ABSOLUTE) == 0)
-		imm += (unsigned long)instr;
-
-	return (unsigned long)imm;
-}
-
-static unsigned long branch_bform_target(const u32 *instr)
-{
-	signed long imm;
-
-	imm = ppc_inst_val(ppc_inst_read(instr)) & 0xFFFC;
-
-	/* If the top bit of the immediate value is set this is negative */
-	if (imm & 0x8000)
-		imm -= 0x10000;
-
-	if ((ppc_inst_val(ppc_inst_read(instr)) & BRANCH_ABSOLUTE) == 0)
-		imm += (unsigned long)instr;
-
-	return (unsigned long)imm;
-}
-
-unsigned long branch_target(const u32 *instr)
-{
-	if (instr_is_branch_iform(ppc_inst_read(instr)))
-		return branch_iform_target(instr);
-	else if (instr_is_branch_bform(ppc_inst_read(instr)))
-		return branch_bform_target(instr);
-
-	return 0;
-}
-
-int translate_branch(ppc_inst_t *instr, const u32 *dest, const u32 *src)
-{
-	unsigned long target;
-	target = branch_target(src);
-
-	if (instr_is_branch_iform(ppc_inst_read(src)))
-		return create_branch(instr, dest, target,
-				     ppc_inst_val(ppc_inst_read(src)));
-	else if (instr_is_branch_bform(ppc_inst_read(src)))
-		return create_cond_branch(instr, dest, target,
-					  ppc_inst_val(ppc_inst_read(src)));
-
-	return 1;
-}
diff --git a/tools/arch/powerpc/include/asm/inst.h b/tools/arch/powerpc/include/asm/inst.h
new file mode 100644
index 000000000000..7a2edca7a486
--- /dev/null
+++ b/tools/arch/powerpc/include/asm/inst.h
@@ -0,0 +1,41 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+#ifndef _TOOLS_POWERPC_ASM_INST_H
+#define _TOOLS_POWERPC_ASM_INST_H
+
+#include <linux/types.h>
+
+#define OP_PREFIX 1
+
+typedef struct {
+	u32 val;
+	u32 suffix;
+} ppc_inst_t;
+
+#define ppc_inst(x) ((ppc_inst_t){ .val = (x) })
+
+static inline u32 ppc_inst_val(ppc_inst_t x)
+{
+	return x.val;
+}
+
+static inline int ppc_inst_primary_opcode(ppc_inst_t x)
+{
+	return ppc_inst_val(x) >> 26;
+}
+
+static inline ppc_inst_t ppc_inst_read(const u32 *ptr)
+{
+	return ppc_inst(*ptr);
+}
+
+static inline bool ppc_inst_prefixed(ppc_inst_t x)
+{
+	return ppc_inst_primary_opcode(x) == OP_PREFIX;
+}
+
+static inline int ppc_inst_len(ppc_inst_t x)
+{
+	return ppc_inst_prefixed(x) ? 8 : 4;
+}
+
+#endif /* _TOOLS_POWERPC_ASM_INST_H */
diff --git a/tools/arch/powerpc/include/asm/text-patching.h b/tools/arch/powerpc/include/asm/text-patching.h
new file mode 100644
index 000000000000..f953ab290ea7
--- /dev/null
+++ b/tools/arch/powerpc/include/asm/text-patching.h
@@ -0,0 +1,67 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+#ifndef _TOOLS_POWERPC_ASM_TEXT_PATCHING_H
+#define _TOOLS_POWERPC_ASM_TEXT_PATCHING_H
+
+#include <linux/types.h>
+#include <asm/inst.h>
+
+#define BRANCH_SET_LINK	0x1
+#define BRANCH_ABSOLUTE	0x2
+
+static inline bool is_offset_in_branch_range(long offset)
+{
+	return (offset >= -0x2000000 && offset <= 0x1fffffc && !(offset & 0x3));
+}
+
+static inline bool is_offset_in_cond_branch_range(long offset)
+{
+	return offset >= -0x8000 && offset <= 0x7fff && !(offset & 0x3);
+}
+
+static inline int create_branch(ppc_inst_t *instr, const u32 *addr,
+				unsigned long target, int flags)
+{
+	long offset;
+
+	*instr = ppc_inst(0);
+	offset = target;
+	if (! (flags & BRANCH_ABSOLUTE))
+		offset = offset - (unsigned long)addr;
+
+	/* Check we can represent the target in the instruction format */
+	if (!is_offset_in_branch_range(offset))
+		return 1;
+
+	/* Mask out the flags and target, so they don't step on each other. */
+	*instr = ppc_inst(0x48000000 | (flags & 0x3) | (offset & 0x03FFFFFC));
+
+	return 0;
+}
+
+static inline unsigned int branch_opcode(ppc_inst_t instr)
+{
+	return ppc_inst_primary_opcode(instr) & 0x3F;
+}
+
+static inline int instr_is_branch_iform(ppc_inst_t instr)
+{
+	return branch_opcode(instr) == 18;
+}
+
+static inline int instr_is_branch_bform(ppc_inst_t instr)
+{
+	return branch_opcode(instr) == 16;
+}
+
+int create_cond_branch(ppc_inst_t *instr, const u32 *addr,
+		       unsigned long target, int flags);
+int instr_is_relative_branch(ppc_inst_t instr);
+int instr_is_relative_link_branch(ppc_inst_t instr);
+unsigned long branch_target(const u32 *instr);
+unsigned long branch_target_at(const u32 *instr, unsigned long pc);
+int translate_branch(ppc_inst_t *instr, const u32 *dest, const u32 *src);
+int translate_branch_at(ppc_inst_t *instr, unsigned long dest,
+			const u32 *src, unsigned long src_addr);
+bool is_conditional_branch(ppc_inst_t instr);
+
+#endif /* _TOOLS_POWERPC_ASM_TEXT_PATCHING_H */
-- 
2.56.1


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

* [PATCH v3 08/11] objtool/powerpc: Implement arch_support_alt_relocation()
  2026-12-02  7:40 [PATCH v3 00/11] objtool/powerpc: Fix up feature section alternatives at build time Shivang Upadhyay
                   ` (6 preceding siblings ...)
  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
  2026-12-02  7:40 ` [PATCH v3 09/11] powerpc: Mark feature section alternatives as executable Shivang Upadhyay
                   ` (2 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: Shivang Upadhyay @ 2026-12-02  7:40 UTC (permalink / raw)
  To: linuxppc-dev, linux-kernel
  Cc: maddy, mpe, npiggin, chleroy, ritesh.list, sshegde, jpoimboe,
	peterz, shivangu, ardb, will, sv, joe.lawrence, song, mingo,
	mbenes

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


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

* [PATCH v3 09/11] powerpc: Mark feature section alternatives as executable
  2026-12-02  7:40 [PATCH v3 00/11] objtool/powerpc: Fix up feature section alternatives at build time Shivang Upadhyay
                   ` (7 preceding siblings ...)
  2026-12-02  7:40 ` [PATCH v3 08/11] objtool/powerpc: Implement arch_support_alt_relocation() Shivang Upadhyay
@ 2026-12-02  7:40 ` 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
  10 siblings, 0 replies; 12+ messages in thread
From: Shivang Upadhyay @ 2026-12-02  7:40 UTC (permalink / raw)
  To: linuxppc-dev, linux-kernel
  Cc: maddy, mpe, npiggin, chleroy, ritesh.list, sshegde, jpoimboe,
	peterz, shivangu, ardb, will, sv, joe.lawrence, song, mingo,
	mbenes

The replacement code of a feature section is put in __ftr_alt_<label>,
which is an allocatable section but not an executable one, even though it
holds instructions. Mark it with 'x' flag.

The replacement code of a feature section (__ftr_alt_*) is linked inside
.text.  Collect it in .__ftr_alternates.text instead, placed after the
init text. It is never run from where it is linked: it is only copied
over the original code at boot.

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>
---
 arch/powerpc/include/asm/feature-fixups.h | 2 +-
 arch/powerpc/kernel/vmlinux.lds.S         | 7 +++++--
 2 files changed, 6 insertions(+), 3 deletions(-)

diff --git a/arch/powerpc/include/asm/feature-fixups.h b/arch/powerpc/include/asm/feature-fixups.h
index 756a6c694018..2c1d033cbb85 100644
--- a/arch/powerpc/include/asm/feature-fixups.h
+++ b/arch/powerpc/include/asm/feature-fixups.h
@@ -32,7 +32,7 @@
 
 #define FTR_SECTION_ELSE_NESTED(label)			\
 label##2:						\
-	.pushsection __ftr_alt_##label,"a";		\
+	.pushsection __ftr_alt_##label,"ax";		\
 	.align 2;					\
 label##3:
 
diff --git a/arch/powerpc/kernel/vmlinux.lds.S b/arch/powerpc/kernel/vmlinux.lds.S
index 8fc11d6565bf..07f3d6b35a7e 100644
--- a/arch/powerpc/kernel/vmlinux.lds.S
+++ b/arch/powerpc/kernel/vmlinux.lds.S
@@ -99,8 +99,7 @@ SECTIONS
 	.text : AT(ADDR(.text) - LOAD_OFFSET) {
 		ALIGN_FUNCTION();
 #endif
-		/* careful! __ftr_alt_* sections need to be close to .text */
-		*(.text.hot .text.hot.* TEXT_MAIN .text.fixup .text.unlikely .text.unlikely.* .fixup __ftr_alt_* .ref.text);
+		*(.text.hot .text.hot.* TEXT_MAIN .text.fixup .text.unlikely .text.unlikely.* .fixup .ref.text);
 		*(.tramp.ftrace.text);
 		NOINSTR_TEXT
 		SCHED_TEXT
@@ -267,6 +266,10 @@ SECTIONS
 		_einittext = .;
 	} :text
 
+	.__ftr_alternates.text : AT(ADDR(.__ftr_alternates.text) - LOAD_OFFSET) {
+		*(__ftr_alt_*);
+	}
+
 	/* .exit.text is discarded at runtime, not link time,
 	 * to deal with references from __bug_table
 	 */
-- 
2.56.1


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

* [PATCH v3 10/11] powerpc: Always build objtool for powerpc
  2026-12-02  7:40 [PATCH v3 00/11] objtool/powerpc: Fix up feature section alternatives at build time Shivang Upadhyay
                   ` (8 preceding siblings ...)
  2026-12-02  7:40 ` [PATCH v3 09/11] powerpc: Mark feature section alternatives as executable Shivang Upadhyay
@ 2026-12-02  7:40 ` Shivang Upadhyay
  2026-12-02  7:40 ` [PATCH v3 11/11] powerpc: Remove runtime checking of jumps in core kernel feature-fixup Shivang Upadhyay
  10 siblings, 0 replies; 12+ messages in thread
From: Shivang Upadhyay @ 2026-12-02  7:40 UTC (permalink / raw)
  To: linuxppc-dev, linux-kernel
  Cc: maddy, mpe, npiggin, chleroy, ritesh.list, sshegde, jpoimboe,
	peterz, shivangu, ardb, will, sv, joe.lawrence, song, mingo,
	mbenes

- Unconditionally build objtool on powerpc
- Select ARCH_VMLINUX_NEEDS_RELOCS, so that relocations are kept in
  vmlinux.unstriped file
- Add vmlinux.unstripped rule in Makefile.postlink that runs
  objtool with --alt-fixup --link option on 64-bit Book3S.

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>
---
 arch/powerpc/Kconfig           | 1 +
 arch/powerpc/Makefile          | 2 +-
 arch/powerpc/Makefile.postlink | 4 ++++
 3 files changed, 6 insertions(+), 1 deletion(-)

diff --git a/arch/powerpc/Kconfig b/arch/powerpc/Kconfig
index c3b6cd655606..a759392ea855 100644
--- a/arch/powerpc/Kconfig
+++ b/arch/powerpc/Kconfig
@@ -273,6 +273,7 @@ config PPC
 	select HAVE_NMI				if PERF_EVENTS || (PPC64 && PPC_BOOK3S)
 	select HAVE_OPTPROBES
 	select HAVE_OBJTOOL			if ARCH_USING_PATCHABLE_FUNCTION_ENTRY || MPROFILE_KERNEL || PPC32
+	select ARCH_VMLINUX_NEEDS_RELOCS
 	select HAVE_OBJTOOL_MCOUNT		if HAVE_OBJTOOL
 	select HAVE_PERF_EVENTS
 	select HAVE_PERF_EVENTS_NMI		if PPC64
diff --git a/arch/powerpc/Makefile b/arch/powerpc/Makefile
index 9385db478c59..b246e5ab8ba4 100644
--- a/arch/powerpc/Makefile
+++ b/arch/powerpc/Makefile
@@ -398,7 +398,7 @@ vdso_prepare: prepare0
 		$(build)=arch/powerpc/kernel/vdso include/generated/vdso64-offsets.h)
 endif
 
-archprepare: checkbin
+archprepare: tools/objtool checkbin
 
 archheaders:
 	$(Q)$(MAKE) $(build)=arch/powerpc/kernel/syscalls all
diff --git a/arch/powerpc/Makefile.postlink b/arch/powerpc/Makefile.postlink
index bb601be36173..5a1f6493581c 100644
--- a/arch/powerpc/Makefile.postlink
+++ b/arch/powerpc/Makefile.postlink
@@ -43,6 +43,10 @@ ifndef CONFIG_PPC64_ELF_ABI_V1
 endif
 endif
 
+# Fix branches in __ftr_fixup segment, with help of objtool
+vmlinux.unstripped: FORCE
+	$(objtree)/tools/objtool/objtool --alt-fixup --link "$@"
+
 clean:
 	rm -f .tmp_symbols.txt
 
-- 
2.56.1


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

* [PATCH v3 11/11] powerpc: Remove runtime checking of jumps in core kernel feature-fixup
  2026-12-02  7:40 [PATCH v3 00/11] objtool/powerpc: Fix up feature section alternatives at build time Shivang Upadhyay
                   ` (9 preceding siblings ...)
  2026-12-02  7:40 ` [PATCH v3 10/11] powerpc: Always build objtool for powerpc Shivang Upadhyay
@ 2026-12-02  7:40 ` Shivang Upadhyay
  10 siblings, 0 replies; 12+ messages in thread
From: Shivang Upadhyay @ 2026-12-02  7:40 UTC (permalink / raw)
  To: linuxppc-dev, linux-kernel
  Cc: maddy, mpe, npiggin, chleroy, ritesh.list, sshegde, jpoimboe,
	peterz, shivangu, ardb, will, sv, joe.lawrence, song, mingo,
	mbenes

Relative branches in the core kernel's alternative code are now fixed up
at build time by objtool (--alt-fixup), so skip the runtime translation
for them.

objtool does not process the VDSO or modules, so keep translating
branches for those. Add a 'translate' argument to the patching path:
do_feature_fixups() (used by modules and the VDSO) and the selftests
pass true, while the core kernel fixups pass false.

Now the fixup of alterate section is handled at boot time, We can remove
the runtime checking of jumps.

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>
---
 arch/powerpc/lib/feature-fixups.c | 46 +++++++++++++++++++------------
 1 file changed, 29 insertions(+), 17 deletions(-)

diff --git a/arch/powerpc/lib/feature-fixups.c b/arch/powerpc/lib/feature-fixups.c
index 587c8cf1230f..f127f45d9331 100644
--- a/arch/powerpc/lib/feature-fixups.c
+++ b/arch/powerpc/lib/feature-fixups.c
@@ -51,14 +51,22 @@ static u32 *calc_addr(struct fixup_entry *fcur, long offset)
 	return (u32 *)((unsigned long)fcur + offset);
 }
 
-static int patch_alt_instruction(u32 *src, u32 *dest, u32 *alt_start, u32 *alt_end)
+/*
+ * Relative branches in the alternative code of the core kernel have been
+ * fixed up for where they get copied to at build time, by objtool
+ * (--alt-fixup), so @translate is false for the core kernel. The VDSO and
+ * modules are not processed by objtool, so their relative branches still
+ * need to be translated at patch time.
+ */
+static int patch_alt_instruction(u32 *src, u32 *dest, u32 *alt_start,
+				 u32 *alt_end, bool translate)
 {
 	int err;
 	ppc_inst_t instr;
 
 	instr = ppc_inst_read(src);
 
-	if (instr_is_relative_branch(ppc_inst_read(src))) {
+	if (translate && instr_is_relative_branch(ppc_inst_read(src))) {
 		u32 *target = (u32 *)branch_target(src);
 
 		/* Branch within the section doesn't need translating */
@@ -75,7 +83,7 @@ static int patch_alt_instruction(u32 *src, u32 *dest, u32 *alt_start, u32 *alt_e
 }
 
 static int patch_feature_section_mask(unsigned long value, unsigned long mask,
-				      struct fixup_entry *fcur)
+				      struct fixup_entry *fcur, bool translate)
 {
 	u32 *start, *end, *alt_start, *alt_end, *src, *dest;
 
@@ -95,7 +103,8 @@ static int patch_feature_section_mask(unsigned long value, unsigned long mask,
 
 	for (; src < alt_end; src = ppc_inst_next(src, src),
 			      dest = ppc_inst_next(dest, dest)) {
-		if (patch_alt_instruction(src, dest, alt_start, alt_end))
+		if (patch_alt_instruction(src, dest, alt_start, alt_end,
+					  translate))
 			return 1;
 	}
 
@@ -106,7 +115,8 @@ static int patch_feature_section_mask(unsigned long value, unsigned long mask,
 }
 
 static void do_feature_fixups_mask(unsigned long value, unsigned long mask,
-				   void *fixup_start, void *fixup_end)
+				   void *fixup_start, void *fixup_end,
+				   bool translate)
 {
 	struct fixup_entry *fcur, *fend;
 
@@ -114,7 +124,7 @@ static void do_feature_fixups_mask(unsigned long value, unsigned long mask,
 	fend = fixup_end;
 
 	for (; fcur < fend; fcur++) {
-		if (patch_feature_section_mask(value, mask, fcur)) {
+		if (patch_feature_section_mask(value, mask, fcur, translate)) {
 			WARN_ON(1);
 			printk("Unable to patch feature section at %p - %p" \
 				" with %p - %p\n",
@@ -126,9 +136,10 @@ static void do_feature_fixups_mask(unsigned long value, unsigned long mask,
 	}
 }
 
+/* Used for modules and the VDSO, whose branches must be translated. */
 void do_feature_fixups(unsigned long value, void *fixup_start, void *fixup_end)
 {
-	do_feature_fixups_mask(value, ~0, fixup_start, fixup_end);
+	do_feature_fixups_mask(value, ~0, fixup_start, fixup_end, true);
 }
 
 #ifdef CONFIG_PPC_BARRIER_NOSPEC
@@ -645,13 +656,13 @@ void __init apply_feature_fixups(void)
 	 * Apply the CPU-specific and firmware specific fixups to kernel text
 	 * (nop out sections not relevant to this CPU or this firmware).
 	 */
-	do_feature_fixups(spec->cpu_features,
-			  PTRRELOC(&__start___ftr_fixup),
-			  PTRRELOC(&__stop___ftr_fixup));
+	do_feature_fixups_mask(spec->cpu_features, ~0,
+			       PTRRELOC(&__start___ftr_fixup),
+			       PTRRELOC(&__stop___ftr_fixup), false);
 
-	do_feature_fixups(spec->mmu_features,
-			  PTRRELOC(&__start___mmu_ftr_fixup),
-			  PTRRELOC(&__stop___mmu_ftr_fixup));
+	do_feature_fixups_mask(spec->mmu_features, ~0,
+			       PTRRELOC(&__start___mmu_ftr_fixup),
+			       PTRRELOC(&__stop___mmu_ftr_fixup), false);
 
 	do_lwsync_fixups(spec->cpu_features,
 			 PTRRELOC(&__start___lwsync_fixup),
@@ -659,8 +670,9 @@ void __init apply_feature_fixups(void)
 
 #ifdef CONFIG_PPC64
 	saved_firmware_features = powerpc_firmware_features;
-	do_feature_fixups(powerpc_firmware_features,
-			  &__start___fw_ftr_fixup, &__stop___fw_ftr_fixup);
+	do_feature_fixups_mask(powerpc_firmware_features, ~0,
+			       &__start___fw_ftr_fixup, &__stop___fw_ftr_fixup,
+			       false);
 #endif
 	do_final_fixups();
 }
@@ -672,7 +684,7 @@ void __init update_mmu_feature_fixups(unsigned long mask)
 
 	do_feature_fixups_mask(cur_cpu_spec->mmu_features, mask,
 			       PTRRELOC(&__start___mmu_ftr_fixup),
-			       PTRRELOC(&__stop___mmu_ftr_fixup));
+			       PTRRELOC(&__stop___mmu_ftr_fixup), false);
 	mmu_feature_keys_init();
 }
 
@@ -711,7 +723,7 @@ late_initcall(check_features);
 
 static int patch_feature_section(unsigned long value, struct fixup_entry *fcur)
 {
-	return patch_feature_section_mask(value, ~0, fcur);
+	return patch_feature_section_mask(value, ~0, fcur, true);
 }
 
 /* This must be after the text it fixes up, vmlinux.lds.S enforces that atm */
-- 
2.56.1


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

end of thread, other threads:[~2026-12-02  7:43 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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 ` [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

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®