[PATCH v2 43/62] x86/alternative: Define ELF section entry size for alternatives

From: Josh Poimboeuf
Date: Fri May 09 2025 - 16:25:57 EST


In preparation for the objtool klp diff subcommand, define the entry
size for the .altinstructions section in its ELF header. This will
allow tooling to extract individual entries.

Signed-off-by: Josh Poimboeuf <jpoimboe@xxxxxxxxxx>
---
arch/x86/include/asm/alternative.h | 7 +++++--
arch/x86/kernel/alternative.c | 2 ++
2 files changed, 7 insertions(+), 2 deletions(-)

diff --git a/arch/x86/include/asm/alternative.h b/arch/x86/include/asm/alternative.h
index e18cdaa1573c..212761eec886 100644
--- a/arch/x86/include/asm/alternative.h
+++ b/arch/x86/include/asm/alternative.h
@@ -15,6 +15,8 @@
#define ALT_DIRECT_CALL(feature) ((ALT_FLAG_DIRECT_CALL << ALT_FLAGS_SHIFT) | (feature))
#define ALT_CALL_ALWAYS ALT_DIRECT_CALL(X86_FEATURE_ALWAYS)

+#define ALTINSTR_SIZE 14
+
#ifndef __ASSEMBLER__

#include <linux/stddef.h>
@@ -165,7 +167,8 @@ static inline int alternatives_text_reserved(void *start, void *end)
"773:\n"

#define ALTINSTR_ENTRY(ft_flags) \
- ".pushsection .altinstructions,\"a\"\n" \
+ ".pushsection .altinstructions, \"aM\", @progbits, " \
+ __stringify(ALTINSTR_SIZE) "\n" \
" .long 771b - .\n" /* label */ \
" .long 774f - .\n" /* new instruction */ \
" .4byte " __stringify(ft_flags) "\n" /* feature + flags */ \
@@ -328,7 +331,7 @@ void nop_func(void);
741: \
.skip -(((744f-743f)-(741b-740b)) > 0) * ((744f-743f)-(741b-740b)),0x90 ;\
742: \
- .pushsection .altinstructions,"a" ; \
+ .pushsection .altinstructions, "aM", @progbits, ALTINSTR_SIZE ; \
altinstr_entry 740b,743f,flag,742b-740b,744f-743f ; \
.popsection ; \
.pushsection .altinstr_replacement,"ax" ; \
diff --git a/arch/x86/kernel/alternative.c b/arch/x86/kernel/alternative.c
index ec220e53cb52..d6064dd87dde 100644
--- a/arch/x86/kernel/alternative.c
+++ b/arch/x86/kernel/alternative.c
@@ -425,6 +425,8 @@ void __init_or_module noinline apply_alternatives(struct alt_instr *start,
u8 *instr, *replacement;
struct alt_instr *a, *b;

+ BUILD_BUG_ON(ALTINSTR_SIZE != sizeof(struct alt_instr));
+
DPRINTK(ALT, "alt table %px, -> %px", start, end);

/*
--
2.49.0