vmlinux.lds: Unify TEXT_MAIN, DATA_MAIN, and related macros

TEXT_MAIN, DATA_MAIN and friends are defined differently depending on
whether certain config options enable -ffunction-sections and/or
-fdata-sections.

There's no technical reason for that beyond voodoo coding.  Keeping the
separate implementations adds unnecessary complexity, fragments the
logic, and increases the risk of subtle bugs.

Unify the macros by using the same input section patterns across all
configs.

This is a prerequisite for the upcoming livepatch klp-build tooling
which will manually enable -ffunction-sections and -fdata-sections via
KCFLAGS.

Cc: Heiko Carstens <hca@linux.ibm.com>
Cc: Vasily Gorbik <gor@linux.ibm.com>
Cc: Alexander Gordeev <agordeev@linux.ibm.com>
Acked-by: Petr Mladek <pmladek@suse.com>
Tested-by: Joe Lawrence <joe.lawrence@redhat.com>
Signed-off-by: Josh Poimboeuf <jpoimboe@kernel.org>
This commit is contained in:
Josh Poimboeuf 2025-09-17 09:03:10 -07:00
parent 68e71067ec
commit 1ba9f89794
2 changed files with 17 additions and 35 deletions

View File

@ -87,39 +87,24 @@
#define ALIGN_FUNCTION() . = ALIGN(CONFIG_FUNCTION_ALIGNMENT) #define ALIGN_FUNCTION() . = ALIGN(CONFIG_FUNCTION_ALIGNMENT)
/* /*
* LD_DEAD_CODE_DATA_ELIMINATION option enables -fdata-sections, which * Support -ffunction-sections by matching .text and .text.*,
* generates .data.identifier sections, which need to be pulled in with * but exclude '.text..*'.
* .data. We don't want to pull in .data..other sections, which Linux
* has defined. Same for text and bss.
* *
* With LTO_CLANG, the linker also splits sections by default, so we need * Special .text.* sections that are typically grouped separately, such as
* these macros to combine the sections during the final link. * .text.unlikely or .text.hot, must be matched explicitly before using
* * TEXT_MAIN.
* With AUTOFDO_CLANG and PROPELLER_CLANG, by default, the linker splits
* text sections and regroups functions into subsections.
*
* RODATA_MAIN is not used because existing code already defines .rodata.x
* sections to be brought in with rodata.
*/ */
#if defined(CONFIG_LD_DEAD_CODE_DATA_ELIMINATION) || defined(CONFIG_LTO_CLANG) || \
defined(CONFIG_AUTOFDO_CLANG) || defined(CONFIG_PROPELLER_CLANG)
#define TEXT_MAIN .text .text.[0-9a-zA-Z_]* #define TEXT_MAIN .text .text.[0-9a-zA-Z_]*
#else
#define TEXT_MAIN .text /*
#endif * Support -fdata-sections by matching .data, .data.*, and others,
#if defined(CONFIG_LD_DEAD_CODE_DATA_ELIMINATION) || defined(CONFIG_LTO_CLANG) * but exclude '.data..*'.
*/
#define DATA_MAIN .data .data.[0-9a-zA-Z_]* .data.rel.* .data..L* .data..compoundliteral* .data.$__unnamed_* .data.$L* #define DATA_MAIN .data .data.[0-9a-zA-Z_]* .data.rel.* .data..L* .data..compoundliteral* .data.$__unnamed_* .data.$L*
#define SDATA_MAIN .sdata .sdata.[0-9a-zA-Z_]* #define SDATA_MAIN .sdata .sdata.[0-9a-zA-Z_]*
#define RODATA_MAIN .rodata .rodata.[0-9a-zA-Z_]* .rodata..L* #define RODATA_MAIN .rodata .rodata.[0-9a-zA-Z_]* .rodata..L*
#define BSS_MAIN .bss .bss.[0-9a-zA-Z_]* .bss..L* .bss..compoundliteral* #define BSS_MAIN .bss .bss.[0-9a-zA-Z_]* .bss..L* .bss..compoundliteral*
#define SBSS_MAIN .sbss .sbss.[0-9a-zA-Z_]* #define SBSS_MAIN .sbss .sbss.[0-9a-zA-Z_]*
#else
#define DATA_MAIN .data .data.rel .data.rel.local
#define SDATA_MAIN .sdata
#define RODATA_MAIN .rodata
#define BSS_MAIN .bss
#define SBSS_MAIN .sbss
#endif
/* /*
* GCC 4.5 and later have a 32 bytes section alignment for structures. * GCC 4.5 and later have a 32 bytes section alignment for structures.
@ -581,9 +566,8 @@ defined(CONFIG_AUTOFDO_CLANG) || defined(CONFIG_PROPELLER_CLANG)
* during second ld run in second ld pass when generating System.map * during second ld run in second ld pass when generating System.map
* *
* TEXT_MAIN here will match symbols with a fixed pattern (for example, * TEXT_MAIN here will match symbols with a fixed pattern (for example,
* .text.hot or .text.unlikely) if dead code elimination or * .text.hot or .text.unlikely). Match those before TEXT_MAIN to ensure
* function-section is enabled. Match these symbols first before * they get grouped together.
* TEXT_MAIN to ensure they are grouped together.
* *
* Also placing .text.hot section at the beginning of a page, this * Also placing .text.hot section at the beginning of a page, this
* would help the TLB performance. * would help the TLB performance.

View File

@ -38,12 +38,10 @@ SECTIONS {
__kcfi_traps : { KEEP(*(.kcfi_traps)) } __kcfi_traps : { KEEP(*(.kcfi_traps)) }
#endif #endif
#ifdef CONFIG_LTO_CLANG .text : {
/* *(.text .text.[0-9a-zA-Z_]*)
* With CONFIG_LTO_CLANG, LLD always enables -fdata-sections and }
* -ffunction-sections, which increases the size of the final module.
* Merge the split sections in the final binary.
*/
.bss : { .bss : {
*(.bss .bss.[0-9a-zA-Z_]*) *(.bss .bss.[0-9a-zA-Z_]*)
*(.bss..L*) *(.bss..L*)
@ -58,7 +56,7 @@ SECTIONS {
*(.rodata .rodata.[0-9a-zA-Z_]*) *(.rodata .rodata.[0-9a-zA-Z_]*)
*(.rodata..L*) *(.rodata..L*)
} }
#endif
MOD_SEPARATE_CODETAG_SECTIONS() MOD_SEPARATE_CODETAG_SECTIONS()
} }