[PATCH v2 45/62] x86/extable: Define ELF section entry size for exception tables

Josh Poimboeuf posted 62 patches 7 months, 1 week ago
There is a newer version of this series
[PATCH v2 45/62] x86/extable: Define ELF section entry size for exception tables
Posted by Josh Poimboeuf 7 months, 1 week ago
In preparation for the objtool klp diff subcommand, define the entry
size for the __ex_table section in its ELF header.  This will allow
tooling to extract individual entries.

Signed-off-by: Josh Poimboeuf <jpoimboe@kernel.org>
---
 arch/x86/include/asm/asm.h | 20 ++++++++++++--------
 kernel/extable.c           |  2 ++
 2 files changed, 14 insertions(+), 8 deletions(-)

diff --git a/arch/x86/include/asm/asm.h b/arch/x86/include/asm/asm.h
index f963848024a5..62dff336f206 100644
--- a/arch/x86/include/asm/asm.h
+++ b/arch/x86/include/asm/asm.h
@@ -138,15 +138,17 @@ static __always_inline __pure void *rip_rel_ptr(void *p)
 
 # include <asm/extable_fixup_types.h>
 
+#define EXTABLE_SIZE 12
+
 /* Exception table entry */
 #ifdef __ASSEMBLER__
 
-# define _ASM_EXTABLE_TYPE(from, to, type)			\
-	.pushsection "__ex_table","a" ;				\
-	.balign 4 ;						\
-	.long (from) - . ;					\
-	.long (to) - . ;					\
-	.long type ;						\
+# define _ASM_EXTABLE_TYPE(from, to, type)				\
+	.pushsection "__ex_table", "aM", @progbits, EXTABLE_SIZE;	\
+	.balign 4 ;							\
+	.long (from) - . ;						\
+	.long (to) - . ;						\
+	.long type ;							\
 	.popsection
 
 # ifdef CONFIG_KPROBES
@@ -189,7 +191,8 @@ static __always_inline __pure void *rip_rel_ptr(void *p)
 	".purgem extable_type_reg\n"
 
 # define _ASM_EXTABLE_TYPE(from, to, type)			\
-	" .pushsection \"__ex_table\",\"a\"\n"			\
+	" .pushsection __ex_table, \"aM\", @progbits, "		\
+		       __stringify(EXTABLE_SIZE) "\n"		\
 	" .balign 4\n"						\
 	" .long (" #from ") - .\n"				\
 	" .long (" #to ") - .\n"				\
@@ -197,7 +200,8 @@ static __always_inline __pure void *rip_rel_ptr(void *p)
 	" .popsection\n"
 
 # define _ASM_EXTABLE_TYPE_REG(from, to, type, reg)				\
-	" .pushsection \"__ex_table\",\"a\"\n"					\
+	" .pushsection __ex_table, \"aM\", @progbits, "				\
+		       __stringify(EXTABLE_SIZE) "\n"				\
 	" .balign 4\n"								\
 	" .long (" #from ") - .\n"						\
 	" .long (" #to ") - .\n"						\
diff --git a/kernel/extable.c b/kernel/extable.c
index 71f482581cab..0ae3ee2ef266 100644
--- a/kernel/extable.c
+++ b/kernel/extable.c
@@ -55,6 +55,8 @@ const struct exception_table_entry *search_exception_tables(unsigned long addr)
 {
 	const struct exception_table_entry *e;
 
+	BUILD_BUG_ON(EXTABLE_SIZE != sizeof(struct exception_table_entry));
+
 	e = search_kernel_exception_table(addr);
 	if (!e)
 		e = search_module_extables(addr);
-- 
2.49.0
Re: [PATCH v2 45/62] x86/extable: Define ELF section entry size for exception tables
Posted by Brian Gerst 6 months, 1 week ago
On Fri, May 9, 2025 at 4:51 PM Josh Poimboeuf <jpoimboe@kernel.org> wrote:
>
> In preparation for the objtool klp diff subcommand, define the entry
> size for the __ex_table section in its ELF header.  This will allow
> tooling to extract individual entries.
>
> Signed-off-by: Josh Poimboeuf <jpoimboe@kernel.org>
> ---
>  arch/x86/include/asm/asm.h | 20 ++++++++++++--------
>  kernel/extable.c           |  2 ++
>  2 files changed, 14 insertions(+), 8 deletions(-)
>
> diff --git a/arch/x86/include/asm/asm.h b/arch/x86/include/asm/asm.h
> index f963848024a5..62dff336f206 100644
> --- a/arch/x86/include/asm/asm.h
> +++ b/arch/x86/include/asm/asm.h
> @@ -138,15 +138,17 @@ static __always_inline __pure void *rip_rel_ptr(void *p)
>
>  # include <asm/extable_fixup_types.h>
>
> +#define EXTABLE_SIZE 12

Put this in asm-offsets.c instead.  That removes the need for the
BUILD_BUG_ON().


Brian Gerst
Re: [PATCH v2 45/62] x86/extable: Define ELF section entry size for exception tables
Posted by Josh Poimboeuf 6 months, 1 week ago
On Thu, Jun 05, 2025 at 11:58:23PM -0400, Brian Gerst wrote:
> On Fri, May 9, 2025 at 4:51 PM Josh Poimboeuf <jpoimboe@kernel.org> wrote:
> >
> > In preparation for the objtool klp diff subcommand, define the entry
> > size for the __ex_table section in its ELF header.  This will allow
> > tooling to extract individual entries.
> >
> > Signed-off-by: Josh Poimboeuf <jpoimboe@kernel.org>
> > ---
> >  arch/x86/include/asm/asm.h | 20 ++++++++++++--------
> >  kernel/extable.c           |  2 ++
> >  2 files changed, 14 insertions(+), 8 deletions(-)
> >
> > diff --git a/arch/x86/include/asm/asm.h b/arch/x86/include/asm/asm.h
> > index f963848024a5..62dff336f206 100644
> > --- a/arch/x86/include/asm/asm.h
> > +++ b/arch/x86/include/asm/asm.h
> > @@ -138,15 +138,17 @@ static __always_inline __pure void *rip_rel_ptr(void *p)
> >
> >  # include <asm/extable_fixup_types.h>
> >
> > +#define EXTABLE_SIZE 12
> 
> Put this in asm-offsets.c instead.

But that's only for .S code right?  This is also needed for inline asm.

-- 
Josh
Re: [PATCH v2 45/62] x86/extable: Define ELF section entry size for exception tables
Posted by Brian Gerst 6 months, 1 week ago
On Fri, Jun 6, 2025 at 3:48 AM Josh Poimboeuf <jpoimboe@kernel.org> wrote:
>
> On Thu, Jun 05, 2025 at 11:58:23PM -0400, Brian Gerst wrote:
> > On Fri, May 9, 2025 at 4:51 PM Josh Poimboeuf <jpoimboe@kernel.org> wrote:
> > >
> > > In preparation for the objtool klp diff subcommand, define the entry
> > > size for the __ex_table section in its ELF header.  This will allow
> > > tooling to extract individual entries.
> > >
> > > Signed-off-by: Josh Poimboeuf <jpoimboe@kernel.org>
> > > ---
> > >  arch/x86/include/asm/asm.h | 20 ++++++++++++--------
> > >  kernel/extable.c           |  2 ++
> > >  2 files changed, 14 insertions(+), 8 deletions(-)
> > >
> > > diff --git a/arch/x86/include/asm/asm.h b/arch/x86/include/asm/asm.h
> > > index f963848024a5..62dff336f206 100644
> > > --- a/arch/x86/include/asm/asm.h
> > > +++ b/arch/x86/include/asm/asm.h
> > > @@ -138,15 +138,17 @@ static __always_inline __pure void *rip_rel_ptr(void *p)
> > >
> > >  # include <asm/extable_fixup_types.h>
> > >
> > > +#define EXTABLE_SIZE 12
> >
> > Put this in asm-offsets.c instead.
>
> But that's only for .S code right?  This is also needed for inline asm.

<asm/asm-offsets.h> can be used in C code too.  Normally it wouldn't
be needed but the inline asm case is a valid use.


Brian Gerst
Re: [PATCH v2 45/62] x86/extable: Define ELF section entry size for exception tables
Posted by Josh Poimboeuf 6 months, 1 week ago
On Fri, Jun 06, 2025 at 09:26:30PM -0400, Brian Gerst wrote:
> On Fri, Jun 6, 2025 at 3:48 AM Josh Poimboeuf <jpoimboe@kernel.org> wrote:
> >
> > On Thu, Jun 05, 2025 at 11:58:23PM -0400, Brian Gerst wrote:
> > > On Fri, May 9, 2025 at 4:51 PM Josh Poimboeuf <jpoimboe@kernel.org> wrote:
> > > >
> > > > In preparation for the objtool klp diff subcommand, define the entry
> > > > size for the __ex_table section in its ELF header.  This will allow
> > > > tooling to extract individual entries.
> > > >
> > > > Signed-off-by: Josh Poimboeuf <jpoimboe@kernel.org>
> > > > ---
> > > >  arch/x86/include/asm/asm.h | 20 ++++++++++++--------
> > > >  kernel/extable.c           |  2 ++
> > > >  2 files changed, 14 insertions(+), 8 deletions(-)
> > > >
> > > > diff --git a/arch/x86/include/asm/asm.h b/arch/x86/include/asm/asm.h
> > > > index f963848024a5..62dff336f206 100644
> > > > --- a/arch/x86/include/asm/asm.h
> > > > +++ b/arch/x86/include/asm/asm.h
> > > > @@ -138,15 +138,17 @@ static __always_inline __pure void *rip_rel_ptr(void *p)
> > > >
> > > >  # include <asm/extable_fixup_types.h>
> > > >
> > > > +#define EXTABLE_SIZE 12
> > >
> > > Put this in asm-offsets.c instead.
> >
> > But that's only for .S code right?  This is also needed for inline asm.
> 
> <asm/asm-offsets.h> can be used in C code too.  Normally it wouldn't
> be needed but the inline asm case is a valid use.

Ah, nice.  This is much better.  Thanks!

diff --git a/arch/x86/include/asm/alternative.h b/arch/x86/include/asm/alternative.h
index e206e0f96568..eb24d9ba30d7 100644
--- a/arch/x86/include/asm/alternative.h
+++ b/arch/x86/include/asm/alternative.h
@@ -16,8 +16,6 @@
 #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>
@@ -200,7 +198,7 @@ static inline int alternatives_text_reserved(void *start, void *end)
 
 #define ALTINSTR_ENTRY(ft_flags)					      \
 	".pushsection .altinstructions, \"aM\", @progbits, "		      \
-		      __stringify(ALTINSTR_SIZE) "\n"			      \
+		      __stringify(ALT_INSTR_SIZE) "\n"			      \
 	" .long 771b - .\n"				/* label           */ \
 	" .long 774f - .\n"				/* new instruction */ \
 	" .4byte " __stringify(ft_flags) "\n"		/* feature + flags */ \
@@ -363,7 +361,7 @@ void nop_func(void);
 741:									\
 	.skip -(((744f-743f)-(741b-740b)) > 0) * ((744f-743f)-(741b-740b)),0x90	;\
 742:									\
-	.pushsection .altinstructions, "aM", @progbits, ALTINSTR_SIZE ;	\
+	.pushsection .altinstructions, "aM", @progbits, ALT_INSTR_SIZE ;\
 	altinstr_entry 740b,743f,flag,742b-740b,744f-743f ;		\
 	.popsection ;							\
 	.pushsection .altinstr_replacement,"ax"	;			\
diff --git a/arch/x86/include/asm/asm.h b/arch/x86/include/asm/asm.h
index 62dff336f206..eb0b33f02be3 100644
--- a/arch/x86/include/asm/asm.h
+++ b/arch/x86/include/asm/asm.h
@@ -138,7 +138,9 @@ static __always_inline __pure void *rip_rel_ptr(void *p)
 
 # include <asm/extable_fixup_types.h>
 
-#define EXTABLE_SIZE 12
+#ifndef COMPILE_OFFSETS
+#include <asm/asm-offsets.h>
+#endif
 
 /* Exception table entry */
 #ifdef __ASSEMBLER__
diff --git a/arch/x86/kernel/alternative.c b/arch/x86/kernel/alternative.c
index d01ab5fa631f..4888e1c8be6a 100644
--- a/arch/x86/kernel/alternative.c
+++ b/arch/x86/kernel/alternative.c
@@ -593,8 +593,6 @@ 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);
 
 	/*
diff --git a/arch/x86/kernel/asm-offsets.c b/arch/x86/kernel/asm-offsets.c
index 6259b474073b..805da27854ee 100644
--- a/arch/x86/kernel/asm-offsets.c
+++ b/arch/x86/kernel/asm-offsets.c
@@ -123,4 +123,8 @@ static void __used common(void)
 	OFFSET(ARIA_CTX_rounds, aria_ctx, rounds);
 #endif
 
+	BLANK();
+	DEFINE(EXTABLE_SIZE,	 sizeof(struct exception_table_entry));
+	DEFINE(UNWIND_HINT_SIZE, sizeof(struct unwind_hint));
+	DEFINE(ALT_INSTR_SIZE,	 sizeof(struct alt_instr));
 }
diff --git a/arch/x86/kernel/unwind_orc.c b/arch/x86/kernel/unwind_orc.c
index 4624d6d916a2..977ee75e047c 100644
--- a/arch/x86/kernel/unwind_orc.c
+++ b/arch/x86/kernel/unwind_orc.c
@@ -199,8 +199,6 @@ static struct orc_entry *orc_find(unsigned long ip)
 {
 	static struct orc_entry *orc;
 
-	BUILD_BUG_ON(UNWIND_HINT_SIZE != sizeof(struct unwind_hint));
-
 	if (ip == 0)
 		return &null_orc_entry;
 
diff --git a/arch/x86/mm/extable.c b/arch/x86/mm/extable.c
index d4aae98b3739..bf8dab18be97 100644
--- a/arch/x86/mm/extable.c
+++ b/arch/x86/mm/extable.c
@@ -303,8 +303,6 @@ int fixup_exception(struct pt_regs *regs, int trapnr, unsigned long error_code,
 	const struct exception_table_entry *e;
 	int type, reg, imm;
 
-	BUILD_BUG_ON(EXTABLE_SIZE != sizeof(struct exception_table_entry));
-
 #ifdef CONFIG_PNPBIOS
 	if (unlikely(SEGMENT_IS_PNP_CODE(regs->cs))) {
 		extern u32 pnp_bios_fault_eip, pnp_bios_fault_esp;
diff --git a/include/linux/objtool.h b/include/linux/objtool.h
index d4137a46ee70..e93f0c28b54b 100644
--- a/include/linux/objtool.h
+++ b/include/linux/objtool.h
@@ -8,8 +8,6 @@
 
 #include <asm/asm.h>
 
-#define UNWIND_HINT_SIZE 12
-
 #ifndef __ASSEMBLY__
 
 #define UNWIND_HINT(type, sp_reg, sp_offset, signal)		\
diff --git a/kernel/bounds.c b/kernel/bounds.c
index 29b2cd00df2c..02b619eb6106 100644
--- a/kernel/bounds.c
+++ b/kernel/bounds.c
@@ -6,6 +6,7 @@
  */
 
 #define __GENERATING_BOUNDS_H
+#define COMPILE_OFFSETS
 /* Include headers that define the enum constants of interest */
 #include <linux/page-flags.h>
 #include <linux/mmzone.h>
diff --git a/scripts/mod/devicetable-offsets.c b/scripts/mod/devicetable-offsets.c
index d3d00e85edf7..ef2ffb68f69d 100644
--- a/scripts/mod/devicetable-offsets.c
+++ b/scripts/mod/devicetable-offsets.c
@@ -1,4 +1,5 @@
 // SPDX-License-Identifier: GPL-2.0
+#define COMPILE_OFFSETS
 #include <linux/kbuild.h>
 #include <linux/mod_devicetable.h>
 

Re: [PATCH v2 45/62] x86/extable: Define ELF section entry size for exception tables
Posted by Joe Lawrence 6 months, 3 weeks ago
On 5/9/25 4:17 PM, Josh Poimboeuf wrote:
> In preparation for the objtool klp diff subcommand, define the entry
> size for the __ex_table section in its ELF header.  This will allow
> tooling to extract individual entries.
> 
> Signed-off-by: Josh Poimboeuf <jpoimboe@kernel.org>
> ---
>  arch/x86/include/asm/asm.h | 20 ++++++++++++--------
>  kernel/extable.c           |  2 ++
>  2 files changed, 14 insertions(+), 8 deletions(-)
> 
> diff --git a/arch/x86/include/asm/asm.h b/arch/x86/include/asm/asm.h
> index f963848024a5..62dff336f206 100644
> --- a/arch/x86/include/asm/asm.h
> +++ b/arch/x86/include/asm/asm.h
> @@ -138,15 +138,17 @@ static __always_inline __pure void *rip_rel_ptr(void *p)
>  
>  # include <asm/extable_fixup_types.h>
>  
> +#define EXTABLE_SIZE 12
>
> + > [ ... snip ... ]
>

EXTABLE_SIZE defined in arch/x86/ ...

> diff --git a/kernel/extable.c b/kernel/extable.c
> index 71f482581cab..0ae3ee2ef266 100644
> --- a/kernel/extable.c
> +++ b/kernel/extable.c
> @@ -55,6 +55,8 @@ const struct exception_table_entry *search_exception_tables(unsigned long addr)
>  {
>  	const struct exception_table_entry *e;
>  
> +	BUILD_BUG_ON(EXTABLE_SIZE != sizeof(struct exception_table_entry));
> +

but referenced in kernel/ where a non-x86 build like ppc64le build won't
know what EXTABLE_SIZE is :(

-- 
Joe
Re: [PATCH v2 45/62] x86/extable: Define ELF section entry size for exception tables
Posted by Josh Poimboeuf 6 months, 1 week ago
On Wed, May 28, 2025 at 10:40:55AM -0400, Joe Lawrence wrote:
> On 5/9/25 4:17 PM, Josh Poimboeuf wrote:
> > In preparation for the objtool klp diff subcommand, define the entry
> > size for the __ex_table section in its ELF header.  This will allow
> > tooling to extract individual entries.
> > 
> > Signed-off-by: Josh Poimboeuf <jpoimboe@kernel.org>
> > ---
> >  arch/x86/include/asm/asm.h | 20 ++++++++++++--------
> >  kernel/extable.c           |  2 ++
> >  2 files changed, 14 insertions(+), 8 deletions(-)
> > 
> > diff --git a/arch/x86/include/asm/asm.h b/arch/x86/include/asm/asm.h
> > index f963848024a5..62dff336f206 100644
> > --- a/arch/x86/include/asm/asm.h
> > +++ b/arch/x86/include/asm/asm.h
> > @@ -138,15 +138,17 @@ static __always_inline __pure void *rip_rel_ptr(void *p)
> >  
> >  # include <asm/extable_fixup_types.h>
> >  
> > +#define EXTABLE_SIZE 12
> >
> > + > [ ... snip ... ]
> >
> 
> EXTABLE_SIZE defined in arch/x86/ ...
> 
> > diff --git a/kernel/extable.c b/kernel/extable.c
> > index 71f482581cab..0ae3ee2ef266 100644
> > --- a/kernel/extable.c
> > +++ b/kernel/extable.c
> > @@ -55,6 +55,8 @@ const struct exception_table_entry *search_exception_tables(unsigned long addr)
> >  {
> >  	const struct exception_table_entry *e;
> >  
> > +	BUILD_BUG_ON(EXTABLE_SIZE != sizeof(struct exception_table_entry));
> > +
> 
> but referenced in kernel/ where a non-x86 build like ppc64le build won't
> know what EXTABLE_SIZE is :(

Thanks, I'll move the BUILD_BUG_ON() to the x86 extable code:

diff --git a/arch/x86/mm/extable.c b/arch/x86/mm/extable.c
index bf8dab18be97..d4aae98b3739 100644
--- a/arch/x86/mm/extable.c
+++ b/arch/x86/mm/extable.c
@@ -303,6 +303,8 @@ int fixup_exception(struct pt_regs *regs, int trapnr, unsigned long error_code,
 	const struct exception_table_entry *e;
 	int type, reg, imm;
 
+	BUILD_BUG_ON(EXTABLE_SIZE != sizeof(struct exception_table_entry));
+
 #ifdef CONFIG_PNPBIOS
 	if (unlikely(SEGMENT_IS_PNP_CODE(regs->cs))) {
 		extern u32 pnp_bios_fault_eip, pnp_bios_fault_esp;
diff --git a/kernel/extable.c b/kernel/extable.c
index 0ae3ee2ef266..71f482581cab 100644
--- a/kernel/extable.c
+++ b/kernel/extable.c
@@ -55,8 +55,6 @@ const struct exception_table_entry *search_exception_tables(unsigned long addr)
 {
 	const struct exception_table_entry *e;
 
-	BUILD_BUG_ON(EXTABLE_SIZE != sizeof(struct exception_table_entry));
-
 	e = search_kernel_exception_table(addr);
 	if (!e)
 		e = search_module_extables(addr);