[PATCH v2] module.lds,codetag: force 0 sh_addr for sections

Joe Lawrence posted 1 patch 1 month ago
include/asm-generic/codetag.lds.h |  2 +-
scripts/module.lds.S              | 12 ++++++------
2 files changed, 7 insertions(+), 7 deletions(-)
[PATCH v2] module.lds,codetag: force 0 sh_addr for sections
Posted by Joe Lawrence 1 month ago
Commit 1ba9f8979426 ("vmlinux.lds: Unify TEXT_MAIN, DATA_MAIN, and
related macros") added .text and made .data, .bss, and .rodata sections
unconditional in the module linker script, but without an explicit
address like the other sections in the same file.

When linking modules with ld.bfd -r, sections defined without an address
inherit the location counter, resulting in non-zero sh_addr values in
the .ko.  Relocatable objects are expected to have sh_addr=0 for these
sections and these non-zero addresses confuse elfutils and have been
reported to cause segmentation faults in SystemTap [1].

Add the 0 address specifier to all sections in module.lds, including the
.codetag.* sections via MOD_SEPARATE_CODETAG_SECTIONS macro.

Link: https://sourceware.org/bugzilla/show_bug.cgi?id=33958
Fixes: 1ba9f8979426 ("vmlinux.lds: Unify TEXT_MAIN, DATA_MAIN, and related macros")
Signed-off-by: Joe Lawrence <joe.lawrence@redhat.com>
---
 include/asm-generic/codetag.lds.h |  2 +-
 scripts/module.lds.S              | 12 ++++++------
 2 files changed, 7 insertions(+), 7 deletions(-)

v2:
- Update the MOD_SEPARATE_CODETAG_SECTION for .codetag.* as well [Petr]

v1: https://lore.kernel.org/lkml/20260304160611.143862-1-joe.lawrence@redhat.com

diff --git a/include/asm-generic/codetag.lds.h b/include/asm-generic/codetag.lds.h
index a14f4bdafdda..4948e5d4e9d9 100644
--- a/include/asm-generic/codetag.lds.h
+++ b/include/asm-generic/codetag.lds.h
@@ -18,7 +18,7 @@
 	IF_MEM_ALLOC_PROFILING(SECTION_WITH_BOUNDARIES(alloc_tags))
 
 #define MOD_SEPARATE_CODETAG_SECTION(_name)	\
-	.codetag.##_name : {			\
+	.codetag.##_name 0 : {			\
 		SECTION_WITH_BOUNDARIES(_name)	\
 	}
 
diff --git a/scripts/module.lds.S b/scripts/module.lds.S
index 054ef99e8288..e1cab3cee3f7 100644
--- a/scripts/module.lds.S
+++ b/scripts/module.lds.S
@@ -32,30 +32,30 @@ SECTIONS {
 	__jump_table		0 : ALIGN(8) { KEEP(*(__jump_table)) }
 	__ex_table		0 : ALIGN(4) { KEEP(*(__ex_table)) }
 
-	__patchable_function_entries : { *(__patchable_function_entries) }
+	__patchable_function_entries 0 : { *(__patchable_function_entries) }
 
 	.init.klp_funcs		0 : ALIGN(8) { KEEP(*(.init.klp_funcs)) }
 	.init.klp_objects	0 : ALIGN(8) { KEEP(*(.init.klp_objects)) }
 
 #ifdef CONFIG_ARCH_USES_CFI_TRAPS
-	__kcfi_traps		: { KEEP(*(.kcfi_traps)) }
+	__kcfi_traps		0 : { KEEP(*(.kcfi_traps)) }
 #endif
 
-	.text : {
+	.text			0 : {
 		*(.text .text.[0-9a-zA-Z_]*)
 	}
 
-	.bss : {
+	.bss			0 : {
 		*(.bss .bss.[0-9a-zA-Z_]*)
 		*(.bss..L*)
 	}
 
-	.data : {
+	.data			0 : {
 		*(.data .data.[0-9a-zA-Z_]*)
 		*(.data..L*)
 	}
 
-	.rodata : {
+	.rodata			0 : {
 		*(.rodata .rodata.[0-9a-zA-Z_]*)
 		*(.rodata..L*)
 	}
-- 
2.53.0
Re: [PATCH v2] module.lds,codetag: force 0 sh_addr for sections
Posted by Sami Tolvanen 2 weeks, 6 days ago
On Wed, 04 Mar 2026 20:52:37 -0500, Joe Lawrence wrote:
> Commit 1ba9f8979426 ("vmlinux.lds: Unify TEXT_MAIN, DATA_MAIN, and
> related macros") added .text and made .data, .bss, and .rodata sections
> unconditional in the module linker script, but without an explicit
> address like the other sections in the same file.
> 
> When linking modules with ld.bfd -r, sections defined without an address
> inherit the location counter, resulting in non-zero sh_addr values in
> the .ko.  Relocatable objects are expected to have sh_addr=0 for these
> sections and these non-zero addresses confuse elfutils and have been
> reported to cause segmentation faults in SystemTap [1].
> 
> [...]

Applied to modules-next, thanks!

[1/1] module.lds,codetag: force 0 sh_addr for sections
      commit: 4afc71bba8b7d7841681e7647ae02f5079aaf28f

Best regards,

	Sami
Re: [PATCH v2] module.lds,codetag: force 0 sh_addr for sections
Posted by Sami Tolvanen 4 weeks, 1 day ago
On Wed, Mar 04, 2026 at 08:52:37PM -0500, Joe Lawrence wrote:
> Commit 1ba9f8979426 ("vmlinux.lds: Unify TEXT_MAIN, DATA_MAIN, and
> related macros") added .text and made .data, .bss, and .rodata sections
> unconditional in the module linker script, but without an explicit
> address like the other sections in the same file.
> 
> When linking modules with ld.bfd -r, sections defined without an address
> inherit the location counter, resulting in non-zero sh_addr values in
> the .ko.  Relocatable objects are expected to have sh_addr=0 for these
> sections and these non-zero addresses confuse elfutils and have been
> reported to cause segmentation faults in SystemTap [1].
> 
> Add the 0 address specifier to all sections in module.lds, including the
> .codetag.* sections via MOD_SEPARATE_CODETAG_SECTIONS macro.
> 
> Link: https://sourceware.org/bugzilla/show_bug.cgi?id=33958
> Fixes: 1ba9f8979426 ("vmlinux.lds: Unify TEXT_MAIN, DATA_MAIN, and related macros")
> Signed-off-by: Joe Lawrence <joe.lawrence@redhat.com>
> ---
>  include/asm-generic/codetag.lds.h |  2 +-
>  scripts/module.lds.S              | 12 ++++++------
>  2 files changed, 7 insertions(+), 7 deletions(-)
> 
> v2:
> - Update the MOD_SEPARATE_CODETAG_SECTION for .codetag.* as well [Petr]

Do we also need similar changes in any of the architecture-specific module
linker scripts (arch/*/include/asm/module.lds.h)?

Sami
Re: [PATCH v2] module.lds,codetag: force 0 sh_addr for sections
Posted by Petr Pavlu 3 weeks, 3 days ago
On 3/11/26 10:12 PM, Sami Tolvanen wrote:
> On Wed, Mar 04, 2026 at 08:52:37PM -0500, Joe Lawrence wrote:
>> Commit 1ba9f8979426 ("vmlinux.lds: Unify TEXT_MAIN, DATA_MAIN, and
>> related macros") added .text and made .data, .bss, and .rodata sections
>> unconditional in the module linker script, but without an explicit
>> address like the other sections in the same file.
>>
>> When linking modules with ld.bfd -r, sections defined without an address
>> inherit the location counter, resulting in non-zero sh_addr values in
>> the .ko.  Relocatable objects are expected to have sh_addr=0 for these
>> sections and these non-zero addresses confuse elfutils and have been
>> reported to cause segmentation faults in SystemTap [1].
>>
>> Add the 0 address specifier to all sections in module.lds, including the
>> .codetag.* sections via MOD_SEPARATE_CODETAG_SECTIONS macro.
>>
>> Link: https://sourceware.org/bugzilla/show_bug.cgi?id=33958
>> Fixes: 1ba9f8979426 ("vmlinux.lds: Unify TEXT_MAIN, DATA_MAIN, and related macros")
>> Signed-off-by: Joe Lawrence <joe.lawrence@redhat.com>
>> ---
>>  include/asm-generic/codetag.lds.h |  2 +-
>>  scripts/module.lds.S              | 12 ++++++------
>>  2 files changed, 7 insertions(+), 7 deletions(-)
>>
>> v2:
>> - Update the MOD_SEPARATE_CODETAG_SECTION for .codetag.* as well [Petr]
> 
> Do we also need similar changes in any of the architecture-specific module
> linker scripts (arch/*/include/asm/module.lds.h)?

I overlooked these architecture-specific files. I believe we should do
the same for them. For instance, riscv explicitly defines the .plt, .got
and .got.plt sections, and they have misleading addresses:

$ readelf -t fs/xfs/xfs.ko
[...]
Section Headers:
  [Nr] Name
       Type              Address          Offset            Link
       Size              EntSize          Info              Align
       Flags
[...]
  [48] __versions
       PROGBITS         0000000000000000  0000000000194e90  0
       0000000000007900 0000000000000000  0                 8
       [0000000000000002]: ALLOC
  [49] .plt
       PROGBITS         0000000000007900  000000000019c790  0
       0000000000000001 0000000000000000  0                 1
       [0000000000000006]: ALLOC, EXEC
  [50] .got
       PROGBITS         0000000000007901  000000000019c791  0
       0000000000000001 0000000000000000  0                 1
       [0000000000000003]: WRITE, ALLOC
  [51] .got.plt
       PROGBITS         0000000000007902  000000000019c792  0
       0000000000000001 0000000000000000  0                 1
       [0000000000000002]: ALLOC
[...]

Nonetheless, this can be done separately. I think fixes for these files
should better go through architecture-specific trees anyway.

I can check the individual architectures and prepare the necessary
patches, unless someone else is already looking into this or wants to
take a look.

-- 
Thanks,
Petr
Re: [PATCH v2] module.lds,codetag: force 0 sh_addr for sections
Posted by Josh Poimboeuf 3 weeks, 1 day ago
On Mon, Mar 16, 2026 at 03:23:20PM +0100, Petr Pavlu wrote:
> > Do we also need similar changes in any of the architecture-specific module
> > linker scripts (arch/*/include/asm/module.lds.h)?
> 
> I overlooked these architecture-specific files. I believe we should do
> the same for them. For instance, riscv explicitly defines the .plt, .got
> and .got.plt sections, and they have misleading addresses:
> 
> $ readelf -t fs/xfs/xfs.ko
> [...]
> Section Headers:
>   [Nr] Name
>        Type              Address          Offset            Link
>        Size              EntSize          Info              Align
>        Flags
> [...]
>   [48] __versions
>        PROGBITS         0000000000000000  0000000000194e90  0
>        0000000000007900 0000000000000000  0                 8
>        [0000000000000002]: ALLOC
>   [49] .plt
>        PROGBITS         0000000000007900  000000000019c790  0
>        0000000000000001 0000000000000000  0                 1
>        [0000000000000006]: ALLOC, EXEC
>   [50] .got
>        PROGBITS         0000000000007901  000000000019c791  0
>        0000000000000001 0000000000000000  0                 1
>        [0000000000000003]: WRITE, ALLOC
>   [51] .got.plt
>        PROGBITS         0000000000007902  000000000019c792  0
>        0000000000000001 0000000000000000  0                 1
>        [0000000000000002]: ALLOC
> [...]
> 
> Nonetheless, this can be done separately. I think fixes for these files
> should better go through architecture-specific trees anyway.
> 
> I can check the individual architectures and prepare the necessary
> patches, unless someone else is already looking into this or wants to
> take a look.

I agree those can be done separately.  In the meantime do you plan to
take this patch in the module tree?

-- 
Josh
Re: [PATCH v2] module.lds,codetag: force 0 sh_addr for sections
Posted by Sami Tolvanen 3 weeks, 1 day ago
On Tue, Mar 17, 2026 at 03:55:57PM -0700, Josh Poimboeuf wrote:
> On Mon, Mar 16, 2026 at 03:23:20PM +0100, Petr Pavlu wrote:
> > Nonetheless, this can be done separately. I think fixes for these files
> > should better go through architecture-specific trees anyway.
> > 
> > I can check the individual architectures and prepare the necessary
> > patches, unless someone else is already looking into this or wants to
> > take a look.
> 
> I agree those can be done separately.  In the meantime do you plan to
> take this patch in the module tree?

Yes, that's the plan.

Sami
Re: [PATCH v2] module.lds,codetag: force 0 sh_addr for sections
Posted by Joe Lawrence 4 weeks ago
On Wed, Mar 11, 2026 at 09:12:07PM +0000, Sami Tolvanen wrote:
> On Wed, Mar 04, 2026 at 08:52:37PM -0500, Joe Lawrence wrote:
> > Commit 1ba9f8979426 ("vmlinux.lds: Unify TEXT_MAIN, DATA_MAIN, and
> > related macros") added .text and made .data, .bss, and .rodata sections
> > unconditional in the module linker script, but without an explicit
> > address like the other sections in the same file.
> > 
> > When linking modules with ld.bfd -r, sections defined without an address
> > inherit the location counter, resulting in non-zero sh_addr values in
> > the .ko.  Relocatable objects are expected to have sh_addr=0 for these
> > sections and these non-zero addresses confuse elfutils and have been
> > reported to cause segmentation faults in SystemTap [1].
> > 
> > Add the 0 address specifier to all sections in module.lds, including the
> > .codetag.* sections via MOD_SEPARATE_CODETAG_SECTIONS macro.
> > 
> > Link: https://sourceware.org/bugzilla/show_bug.cgi?id=33958
> > Fixes: 1ba9f8979426 ("vmlinux.lds: Unify TEXT_MAIN, DATA_MAIN, and related macros")
> > Signed-off-by: Joe Lawrence <joe.lawrence@redhat.com>
> > ---
> >  include/asm-generic/codetag.lds.h |  2 +-
> >  scripts/module.lds.S              | 12 ++++++------
> >  2 files changed, 7 insertions(+), 7 deletions(-)
> > 
> > v2:
> > - Update the MOD_SEPARATE_CODETAG_SECTION for .codetag.* as well [Petr]
> 
> Do we also need similar changes in any of the architecture-specific module
> linker scripts (arch/*/include/asm/module.lds.h)?
> 

Hi Sami,

That is a good question that is unfortunately beyond my limited linker script
knowledge.  Some of those arch-specific module.lds.h do not specify
address for several sections and have been that way for years ... so if
I were to guess, I don't think 1ba9f8979426 changed their behavior one
way or another.

--
Joe
Re: [PATCH v2] module.lds,codetag: force 0 sh_addr for sections
Posted by Petr Pavlu 1 month ago
On 3/5/26 2:52 AM, Joe Lawrence wrote:
> Commit 1ba9f8979426 ("vmlinux.lds: Unify TEXT_MAIN, DATA_MAIN, and
> related macros") added .text and made .data, .bss, and .rodata sections
> unconditional in the module linker script, but without an explicit
> address like the other sections in the same file.
> 
> When linking modules with ld.bfd -r, sections defined without an address
> inherit the location counter, resulting in non-zero sh_addr values in
> the .ko.  Relocatable objects are expected to have sh_addr=0 for these
> sections and these non-zero addresses confuse elfutils and have been
> reported to cause segmentation faults in SystemTap [1].
> 
> Add the 0 address specifier to all sections in module.lds, including the
> .codetag.* sections via MOD_SEPARATE_CODETAG_SECTIONS macro.
> 
> Link: https://sourceware.org/bugzilla/show_bug.cgi?id=33958
> Fixes: 1ba9f8979426 ("vmlinux.lds: Unify TEXT_MAIN, DATA_MAIN, and related macros")
> Signed-off-by: Joe Lawrence <joe.lawrence@redhat.com>

Reviewed-by: Petr Pavlu <petr.pavlu@suse.com>

-- 
Thanks,
Petr
Re: [PATCH v2] module.lds,codetag: force 0 sh_addr for sections
Posted by Josh Poimboeuf 1 month ago
On Wed, Mar 04, 2026 at 08:52:37PM -0500, Joe Lawrence wrote:
> Commit 1ba9f8979426 ("vmlinux.lds: Unify TEXT_MAIN, DATA_MAIN, and
> related macros") added .text and made .data, .bss, and .rodata sections
> unconditional in the module linker script, but without an explicit
> address like the other sections in the same file.
> 
> When linking modules with ld.bfd -r, sections defined without an address
> inherit the location counter, resulting in non-zero sh_addr values in
> the .ko.  Relocatable objects are expected to have sh_addr=0 for these
> sections and these non-zero addresses confuse elfutils and have been
> reported to cause segmentation faults in SystemTap [1].
> 
> Add the 0 address specifier to all sections in module.lds, including the
> .codetag.* sections via MOD_SEPARATE_CODETAG_SECTIONS macro.
> 
> Link: https://sourceware.org/bugzilla/show_bug.cgi?id=33958
> Fixes: 1ba9f8979426 ("vmlinux.lds: Unify TEXT_MAIN, DATA_MAIN, and related macros")
> Signed-off-by: Joe Lawrence <joe.lawrence@redhat.com>
> ---
>  include/asm-generic/codetag.lds.h |  2 +-
>  scripts/module.lds.S              | 12 ++++++------
>  2 files changed, 7 insertions(+), 7 deletions(-)
> 
> v2:
> - Update the MOD_SEPARATE_CODETAG_SECTION for .codetag.* as well [Petr]
> 
> v1: https://lore.kernel.org/lkml/20260304160611.143862-1-joe.lawrence@redhat.com

Thanks for the fix!

Acked-by: Josh Poimboeuf <jpoimboe@kernel.org>

-- 
Josh