diff mbox series

[PATCH-next,v3] arm32: enable HAVE_LD_DEAD_CODE_DATA_ELIMINATION

Message ID 20240315063154.696633-1-liuyuntao12@huawei.com (mailing list archive)
State New, archived
Headers show
Series [PATCH-next,v3] arm32: enable HAVE_LD_DEAD_CODE_DATA_ELIMINATION | expand

Commit Message

liuyuntao (F) March 15, 2024, 6:31 a.m. UTC
The current arm32 architecture does not yet support the
HAVE_LD_DEAD_CODE_DATA_ELIMINATION feature. arm32 is widely used in
embedded scenarios, and enabling this feature would be beneficial for
reducing the size of the kernel image.

In order to make this work, we keep the necessary tables by annotating
them with KEEP, also it requires further changes to linker script to KEEP
some tables and wildcard compiler generated sections into the right place.
When using ld.lld for linking, the KEEP keyword is not recognized within
the OVERLAY command, Ard proposed a concise method to solve this problem.

It boots normally with defconfig, vexpress_defconfig and tinyconfig.
The size comparison of zImage is as follows:
defconfig       vexpress_defconfig      tinyconfig
5137712         5138024                 424192          no dce
5032560         4997824                 298384          dce
2.0%            2.7%                    29.7%           shrink

When using smaller config file, there is a significant reduction in the
size of the zImage.

We also tested this patch on a commercially available single-board
computer, and the comparison is as follows:
a15eb_config
2161384         no dce
2092240         dce
3.2%            shrink

The zImage size has been reduced by approximately 3.2%, which is 70KB on
2.1M.

Signed-off-by: Yuntao Liu <liuyuntao12@huawei.com>
Tested-by: Arnd Bergmann <arnd@arndb.de>
Reviewed-by: Arnd Bergmann <arnd@arndb.de>
---
v3:
   - A better way to KEEP .vectors section for ld.lld linking.

v2:
   - Support config XIP_KERNEL.
   - Support LLVM compilation.
   - https://lore.kernel.org/all/20240307151231.654025-1-liuyuntao12@huawei.com/

v1: https://lore.kernel.org/all/20240220081527.23408-1-liuyuntao12@huawei.com/
---
 arch/arm/Kconfig                       | 1 +
 arch/arm/boot/compressed/vmlinux.lds.S | 6 +++++-
 arch/arm/include/asm/vmlinux.lds.h     | 2 +-
 arch/arm/kernel/entry-armv.S           | 3 +++
 arch/arm/kernel/vmlinux-xip.lds.S      | 4 ++--
 arch/arm/kernel/vmlinux.lds.S          | 6 +++---
 6 files changed, 15 insertions(+), 7 deletions(-)

Comments

Ard Biesheuvel March 15, 2024, 8:06 a.m. UTC | #1
On Fri, 15 Mar 2024 at 07:37, Yuntao Liu <liuyuntao12@huawei.com> wrote:
>
> The current arm32 architecture does not yet support the
> HAVE_LD_DEAD_CODE_DATA_ELIMINATION feature. arm32 is widely used in
> embedded scenarios, and enabling this feature would be beneficial for
> reducing the size of the kernel image.
>
> In order to make this work, we keep the necessary tables by annotating
> them with KEEP, also it requires further changes to linker script to KEEP
> some tables and wildcard compiler generated sections into the right place.
> When using ld.lld for linking, the KEEP keyword is not recognized within
> the OVERLAY command, Ard proposed a concise method to solve this problem.
>
> It boots normally with defconfig, vexpress_defconfig and tinyconfig.
> The size comparison of zImage is as follows:
> defconfig       vexpress_defconfig      tinyconfig
> 5137712         5138024                 424192          no dce
> 5032560         4997824                 298384          dce
> 2.0%            2.7%                    29.7%           shrink
>
> When using smaller config file, there is a significant reduction in the
> size of the zImage.
>
> We also tested this patch on a commercially available single-board
> computer, and the comparison is as follows:
> a15eb_config
> 2161384         no dce
> 2092240         dce
> 3.2%            shrink
>
> The zImage size has been reduced by approximately 3.2%, which is 70KB on
> 2.1M.
>
> Signed-off-by: Yuntao Liu <liuyuntao12@huawei.com>
> Tested-by: Arnd Bergmann <arnd@arndb.de>
> Reviewed-by: Arnd Bergmann <arnd@arndb.de>
> ---
> v3:
>    - A better way to KEEP .vectors section for ld.lld linking.
>
> v2:
>    - Support config XIP_KERNEL.
>    - Support LLVM compilation.
>    - https://lore.kernel.org/all/20240307151231.654025-1-liuyuntao12@huawei.com/
>
> v1: https://lore.kernel.org/all/20240220081527.23408-1-liuyuntao12@huawei.com/
> ---
>  arch/arm/Kconfig                       | 1 +
>  arch/arm/boot/compressed/vmlinux.lds.S | 6 +++++-
>  arch/arm/include/asm/vmlinux.lds.h     | 2 +-
>  arch/arm/kernel/entry-armv.S           | 3 +++
>  arch/arm/kernel/vmlinux-xip.lds.S      | 4 ++--
>  arch/arm/kernel/vmlinux.lds.S          | 6 +++---
>  6 files changed, 15 insertions(+), 7 deletions(-)
>
> diff --git a/arch/arm/Kconfig b/arch/arm/Kconfig
> index b14aed3a17ab..45f25f6e7a62 100644
> --- a/arch/arm/Kconfig
> +++ b/arch/arm/Kconfig
> @@ -114,6 +114,7 @@ config ARM
>         select HAVE_KERNEL_XZ
>         select HAVE_KPROBES if !XIP_KERNEL && !CPU_ENDIAN_BE32 && !CPU_V7M
>         select HAVE_KRETPROBES if HAVE_KPROBES
> +       select HAVE_LD_DEAD_CODE_DATA_ELIMINATION
>         select HAVE_MOD_ARCH_SPECIFIC
>         select HAVE_NMI
>         select HAVE_OPTPROBES if !THUMB2_KERNEL
> diff --git a/arch/arm/boot/compressed/vmlinux.lds.S b/arch/arm/boot/compressed/vmlinux.lds.S
> index 3fcb3e62dc56..affd30714f01 100644
> --- a/arch/arm/boot/compressed/vmlinux.lds.S
> +++ b/arch/arm/boot/compressed/vmlinux.lds.S
> @@ -89,7 +89,11 @@ SECTIONS
>       * The EFI stub always executes from RAM, and runs strictly before the
>       * decompressor, so we can make an exception for its r/w data, and keep it
>       */
> +#ifdef CONFIG_LD_DEAD_CODE_DATA_ELIMINATION
> +    *(.data.* .bss.*)
> +#else
>      *(.data.efistub .bss.efistub)
> +#endif
>      __pecoff_data_end = .;
>

This is still not right.

Can you just add -fno-data-sections to cflags-$(CONFIG_ARM) in
drivers/firmware/efi/libstub/Makefile?


>      /*
> @@ -125,7 +129,7 @@ SECTIONS
>
>    . = BSS_START;
>    __bss_start = .;
> -  .bss                 : { *(.bss) }
> +  .bss                 : { *(.bss .bss.*) }
>    _end = .;
>
>    . = ALIGN(8);                /* the stack must be 64-bit aligned */
> diff --git a/arch/arm/include/asm/vmlinux.lds.h b/arch/arm/include/asm/vmlinux.lds.h
> index 4c8632d5c432..d60f6e83a9f7 100644
> --- a/arch/arm/include/asm/vmlinux.lds.h
> +++ b/arch/arm/include/asm/vmlinux.lds.h
> @@ -42,7 +42,7 @@
>  #define PROC_INFO                                                      \
>                 . = ALIGN(4);                                           \
>                 __proc_info_begin = .;                                  \
> -               *(.proc.info.init)                                      \
> +               KEEP(*(.proc.info.init))                                \
>                 __proc_info_end = .;
>
>  #define IDMAP_TEXT                                                     \
> diff --git a/arch/arm/kernel/entry-armv.S b/arch/arm/kernel/entry-armv.S
> index 6150a716828c..f01d23a220e6 100644
> --- a/arch/arm/kernel/entry-armv.S
> +++ b/arch/arm/kernel/entry-armv.S
> @@ -1065,6 +1065,7 @@ vector_addrexcptn:
>         .globl  vector_fiq
>
>         .section .vectors, "ax", %progbits
> +       .reloc  .text, R_ARM_NONE, .
>         W(b)    vector_rst
>         W(b)    vector_und
>  ARM(   .reloc  ., R_ARM_LDR_PC_G0, .L__vector_swi              )
> @@ -1078,6 +1079,7 @@ THUMB(    .reloc  ., R_ARM_THM_PC12, .L__vector_swi               )
>
>  #ifdef CONFIG_HARDEN_BRANCH_HISTORY
>         .section .vectors.bhb.loop8, "ax", %progbits
> +       .reloc  .text, R_ARM_NONE, .
>         W(b)    vector_rst
>         W(b)    vector_bhb_loop8_und
>  ARM(   .reloc  ., R_ARM_LDR_PC_G0, .L__vector_bhb_loop8_swi    )
> @@ -1090,6 +1092,7 @@ THUMB(    .reloc  ., R_ARM_THM_PC12, .L__vector_bhb_loop8_swi     )
>         W(b)    vector_bhb_loop8_fiq
>
>         .section .vectors.bhb.bpiall, "ax", %progbits
> +       .reloc  .text, R_ARM_NONE, .
>         W(b)    vector_rst
>         W(b)    vector_bhb_bpiall_und
>  ARM(   .reloc  ., R_ARM_LDR_PC_G0, .L__vector_bhb_bpiall_swi   )
> diff --git a/arch/arm/kernel/vmlinux-xip.lds.S b/arch/arm/kernel/vmlinux-xip.lds.S
> index c16d196b5aad..5eddb75a7174 100644
> --- a/arch/arm/kernel/vmlinux-xip.lds.S
> +++ b/arch/arm/kernel/vmlinux-xip.lds.S
> @@ -63,7 +63,7 @@ SECTIONS
>         . = ALIGN(4);
>         __ex_table : AT(ADDR(__ex_table) - LOAD_OFFSET) {
>                 __start___ex_table = .;
> -               ARM_MMU_KEEP(*(__ex_table))
> +               ARM_MMU_KEEP(KEEP(*(__ex_table)))
>                 __stop___ex_table = .;
>         }
>
> @@ -83,7 +83,7 @@ SECTIONS
>         }
>         .init.arch.info : {
>                 __arch_info_begin = .;
> -               *(.arch.info.init)
> +               KEEP(*(.arch.info.init))
>                 __arch_info_end = .;
>         }
>         .init.tagtable : {
> diff --git a/arch/arm/kernel/vmlinux.lds.S b/arch/arm/kernel/vmlinux.lds.S
> index bd9127c4b451..de373c6c2ae8 100644
> --- a/arch/arm/kernel/vmlinux.lds.S
> +++ b/arch/arm/kernel/vmlinux.lds.S
> @@ -74,7 +74,7 @@ SECTIONS
>         . = ALIGN(4);
>         __ex_table : AT(ADDR(__ex_table) - LOAD_OFFSET) {
>                 __start___ex_table = .;
> -               ARM_MMU_KEEP(*(__ex_table))
> +               ARM_MMU_KEEP(KEEP(*(__ex_table)))
>                 __stop___ex_table = .;
>         }
>
> @@ -99,7 +99,7 @@ SECTIONS
>         }
>         .init.arch.info : {
>                 __arch_info_begin = .;
> -               *(.arch.info.init)
> +               KEEP(*(.arch.info.init))
>                 __arch_info_end = .;
>         }
>         .init.tagtable : {
> @@ -116,7 +116,7 @@ SECTIONS
>  #endif
>         .init.pv_table : {
>                 __pv_table_begin = .;
> -               *(.pv_table)
> +               KEEP(*(.pv_table))
>                 __pv_table_end = .;
>         }
>
> --
> 2.34.1
>
liuyuntao (F) March 15, 2024, 10:23 a.m. UTC | #2
On 2024/3/15 16:06, Ard Biesheuvel wrote:
> On Fri, 15 Mar 2024 at 07:37, Yuntao Liu <liuyuntao12@huawei.com> wrote:
>>
>> The current arm32 architecture does not yet support the
>> HAVE_LD_DEAD_CODE_DATA_ELIMINATION feature. arm32 is widely used in
>> embedded scenarios, and enabling this feature would be beneficial for
>> reducing the size of the kernel image.
>>
>> In order to make this work, we keep the necessary tables by annotating
>> them with KEEP, also it requires further changes to linker script to KEEP
>> some tables and wildcard compiler generated sections into the right place.
>> When using ld.lld for linking, the KEEP keyword is not recognized within
>> the OVERLAY command, Ard proposed a concise method to solve this problem.
>>
>> It boots normally with defconfig, vexpress_defconfig and tinyconfig.
>> The size comparison of zImage is as follows:
>> defconfig       vexpress_defconfig      tinyconfig
>> 5137712         5138024                 424192          no dce
>> 5032560         4997824                 298384          dce
>> 2.0%            2.7%                    29.7%           shrink
>>
>> When using smaller config file, there is a significant reduction in the
>> size of the zImage.
>>
>> We also tested this patch on a commercially available single-board
>> computer, and the comparison is as follows:
>> a15eb_config
>> 2161384         no dce
>> 2092240         dce
>> 3.2%            shrink
>>
>> The zImage size has been reduced by approximately 3.2%, which is 70KB on
>> 2.1M.
>>
>> Signed-off-by: Yuntao Liu <liuyuntao12@huawei.com>
>> Tested-by: Arnd Bergmann <arnd@arndb.de>
>> Reviewed-by: Arnd Bergmann <arnd@arndb.de>
>> ---
>> v3:
>>     - A better way to KEEP .vectors section for ld.lld linking.
>>
>> v2:
>>     - Support config XIP_KERNEL.
>>     - Support LLVM compilation.
>>     - https://lore.kernel.org/all/20240307151231.654025-1-liuyuntao12@huawei.com/
>>
>> v1: https://lore.kernel.org/all/20240220081527.23408-1-liuyuntao12@huawei.com/
>> ---
>>   arch/arm/Kconfig                       | 1 +
>>   arch/arm/boot/compressed/vmlinux.lds.S | 6 +++++-
>>   arch/arm/include/asm/vmlinux.lds.h     | 2 +-
>>   arch/arm/kernel/entry-armv.S           | 3 +++
>>   arch/arm/kernel/vmlinux-xip.lds.S      | 4 ++--
>>   arch/arm/kernel/vmlinux.lds.S          | 6 +++---
>>   6 files changed, 15 insertions(+), 7 deletions(-)
>>
>> diff --git a/arch/arm/Kconfig b/arch/arm/Kconfig
>> index b14aed3a17ab..45f25f6e7a62 100644
>> --- a/arch/arm/Kconfig
>> +++ b/arch/arm/Kconfig
>> @@ -114,6 +114,7 @@ config ARM
>>          select HAVE_KERNEL_XZ
>>          select HAVE_KPROBES if !XIP_KERNEL && !CPU_ENDIAN_BE32 && !CPU_V7M
>>          select HAVE_KRETPROBES if HAVE_KPROBES
>> +       select HAVE_LD_DEAD_CODE_DATA_ELIMINATION
>>          select HAVE_MOD_ARCH_SPECIFIC
>>          select HAVE_NMI
>>          select HAVE_OPTPROBES if !THUMB2_KERNEL
>> diff --git a/arch/arm/boot/compressed/vmlinux.lds.S b/arch/arm/boot/compressed/vmlinux.lds.S
>> index 3fcb3e62dc56..affd30714f01 100644
>> --- a/arch/arm/boot/compressed/vmlinux.lds.S
>> +++ b/arch/arm/boot/compressed/vmlinux.lds.S
>> @@ -89,7 +89,11 @@ SECTIONS
>>        * The EFI stub always executes from RAM, and runs strictly before the
>>        * decompressor, so we can make an exception for its r/w data, and keep it
>>        */
>> +#ifdef CONFIG_LD_DEAD_CODE_DATA_ELIMINATION
>> +    *(.data.* .bss.*)
>> +#else
>>       *(.data.efistub .bss.efistub)
>> +#endif
>>       __pecoff_data_end = .;
>>
> 
> This is still not right.
> 
> Can you just add -fno-data-sections to cflags-$(CONFIG_ARM) in
> drivers/firmware/efi/libstub/Makefile?
> 

I rebuild kernel in such way:
--- a/drivers/firmware/efi/libstub/Makefile
+++ b/drivers/firmware/efi/libstub/Makefile
@@ -28,6 +28,7 @@ cflags-$(CONFIG_ARM)          += -DEFI_HAVE_STRLEN 
-DEFI_HAVE_STRNLEN \
                                    -DEFI_HAVE_MEMCHR -DEFI_HAVE_STRRCHR \
                                    -DEFI_HAVE_STRCMP -fno-builtin -fpic \
                                    $(call cc-option,-mno-single-pic-base)
+cflags-$(CONFIG_ARM)           += -fno-data-sections
  cflags-$(CONFIG_RISCV)         += -fpic -DNO_ALTERNATIVE -mno-relax
  cflags-$(CONFIG_LOONGARCH)     += -fpi
but I am still encountering this error:
arm-linux-gnueabi-ld: warning: orphan section `.data.efi_loglevel' from 
`drivers/firmware/efi/libstub/printk.stub.o' being placed in section 
`.data.efi_loglevel'
arm-linux-gnueabi-ld: warning: orphan section `.data.screen_info_guid' 
from `drivers/firmware/efi/libstub/screen_info.stub.o' being placed in 
section `.data.screen_info_guid'
arm-linux-gnueabi-ld: warning: orphan section `.data.cpu_state_guid' 
from `drivers/firmware/efi/libstub/arm32-stub.stub.o' being placed in 
section `.data.cpu_state_guid'
arm-linux-gnueabi-ld: warning: orphan section `.data.efi_nokaslr' from 
`drivers/firmware/efi/libstub/efi-stub-helper.stub.o' being placed in 
section `.data.efi_nokaslr'
arm-linux-gnueabi-ld: error: zImage file size is incorrect

I am puzzled because I could not find any option named 
-fno-data-sections for GCC.
Ard Biesheuvel March 15, 2024, 10:46 a.m. UTC | #3
On Fri, 15 Mar 2024 at 11:23, liuyuntao (F) <liuyuntao12@huawei.com> wrote:
>
>
>
> On 2024/3/15 16:06, Ard Biesheuvel wrote:
> > On Fri, 15 Mar 2024 at 07:37, Yuntao Liu <liuyuntao12@huawei.com> wrote:
> >>
> >> The current arm32 architecture does not yet support the
> >> HAVE_LD_DEAD_CODE_DATA_ELIMINATION feature. arm32 is widely used in
> >> embedded scenarios, and enabling this feature would be beneficial for
> >> reducing the size of the kernel image.
> >>
> >> In order to make this work, we keep the necessary tables by annotating
> >> them with KEEP, also it requires further changes to linker script to KEEP
> >> some tables and wildcard compiler generated sections into the right place.
> >> When using ld.lld for linking, the KEEP keyword is not recognized within
> >> the OVERLAY command, Ard proposed a concise method to solve this problem.
> >>
> >> It boots normally with defconfig, vexpress_defconfig and tinyconfig.
> >> The size comparison of zImage is as follows:
> >> defconfig       vexpress_defconfig      tinyconfig
> >> 5137712         5138024                 424192          no dce
> >> 5032560         4997824                 298384          dce
> >> 2.0%            2.7%                    29.7%           shrink
> >>
> >> When using smaller config file, there is a significant reduction in the
> >> size of the zImage.
> >>
> >> We also tested this patch on a commercially available single-board
> >> computer, and the comparison is as follows:
> >> a15eb_config
> >> 2161384         no dce
> >> 2092240         dce
> >> 3.2%            shrink
> >>
> >> The zImage size has been reduced by approximately 3.2%, which is 70KB on
> >> 2.1M.
> >>
> >> Signed-off-by: Yuntao Liu <liuyuntao12@huawei.com>
> >> Tested-by: Arnd Bergmann <arnd@arndb.de>
> >> Reviewed-by: Arnd Bergmann <arnd@arndb.de>
> >> ---
> >> v3:
> >>     - A better way to KEEP .vectors section for ld.lld linking.
> >>
> >> v2:
> >>     - Support config XIP_KERNEL.
> >>     - Support LLVM compilation.
> >>     - https://lore.kernel.org/all/20240307151231.654025-1-liuyuntao12@huawei.com/
> >>
> >> v1: https://lore.kernel.org/all/20240220081527.23408-1-liuyuntao12@huawei.com/
> >> ---
> >>   arch/arm/Kconfig                       | 1 +
> >>   arch/arm/boot/compressed/vmlinux.lds.S | 6 +++++-
> >>   arch/arm/include/asm/vmlinux.lds.h     | 2 +-
> >>   arch/arm/kernel/entry-armv.S           | 3 +++
> >>   arch/arm/kernel/vmlinux-xip.lds.S      | 4 ++--
> >>   arch/arm/kernel/vmlinux.lds.S          | 6 +++---
> >>   6 files changed, 15 insertions(+), 7 deletions(-)
> >>
> >> diff --git a/arch/arm/Kconfig b/arch/arm/Kconfig
> >> index b14aed3a17ab..45f25f6e7a62 100644
> >> --- a/arch/arm/Kconfig
> >> +++ b/arch/arm/Kconfig
> >> @@ -114,6 +114,7 @@ config ARM
> >>          select HAVE_KERNEL_XZ
> >>          select HAVE_KPROBES if !XIP_KERNEL && !CPU_ENDIAN_BE32 && !CPU_V7M
> >>          select HAVE_KRETPROBES if HAVE_KPROBES
> >> +       select HAVE_LD_DEAD_CODE_DATA_ELIMINATION
> >>          select HAVE_MOD_ARCH_SPECIFIC
> >>          select HAVE_NMI
> >>          select HAVE_OPTPROBES if !THUMB2_KERNEL
> >> diff --git a/arch/arm/boot/compressed/vmlinux.lds.S b/arch/arm/boot/compressed/vmlinux.lds.S
> >> index 3fcb3e62dc56..affd30714f01 100644
> >> --- a/arch/arm/boot/compressed/vmlinux.lds.S
> >> +++ b/arch/arm/boot/compressed/vmlinux.lds.S
> >> @@ -89,7 +89,11 @@ SECTIONS
> >>        * The EFI stub always executes from RAM, and runs strictly before the
> >>        * decompressor, so we can make an exception for its r/w data, and keep it
> >>        */
> >> +#ifdef CONFIG_LD_DEAD_CODE_DATA_ELIMINATION
> >> +    *(.data.* .bss.*)
> >> +#else
> >>       *(.data.efistub .bss.efistub)
> >> +#endif
> >>       __pecoff_data_end = .;
> >>
> >
> > This is still not right.
> >
> > Can you just add -fno-data-sections to cflags-$(CONFIG_ARM) in
> > drivers/firmware/efi/libstub/Makefile?
> >
>
> I rebuild kernel in such way:
> --- a/drivers/firmware/efi/libstub/Makefile
> +++ b/drivers/firmware/efi/libstub/Makefile
> @@ -28,6 +28,7 @@ cflags-$(CONFIG_ARM)          += -DEFI_HAVE_STRLEN
> -DEFI_HAVE_STRNLEN \
>                                     -DEFI_HAVE_MEMCHR -DEFI_HAVE_STRRCHR \
>                                     -DEFI_HAVE_STRCMP -fno-builtin -fpic \
>                                     $(call cc-option,-mno-single-pic-base)
> +cflags-$(CONFIG_ARM)           += -fno-data-sections
>   cflags-$(CONFIG_RISCV)         += -fpic -DNO_ALTERNATIVE -mno-relax
>   cflags-$(CONFIG_LOONGARCH)     += -fpi
> but I am still encountering this error:
> arm-linux-gnueabi-ld: warning: orphan section `.data.efi_loglevel' from
> `drivers/firmware/efi/libstub/printk.stub.o' being placed in section
> `.data.efi_loglevel'
> arm-linux-gnueabi-ld: warning: orphan section `.data.screen_info_guid'
> from `drivers/firmware/efi/libstub/screen_info.stub.o' being placed in
> section `.data.screen_info_guid'
> arm-linux-gnueabi-ld: warning: orphan section `.data.cpu_state_guid'
> from `drivers/firmware/efi/libstub/arm32-stub.stub.o' being placed in
> section `.data.cpu_state_guid'
> arm-linux-gnueabi-ld: warning: orphan section `.data.efi_nokaslr' from
> `drivers/firmware/efi/libstub/efi-stub-helper.stub.o' being placed in
> section `.data.efi_nokaslr'
> arm-linux-gnueabi-ld: error: zImage file size is incorrect
>
> I am puzzled because I could not find any option named
> -fno-data-sections for GCC.
>

How about

--- a/drivers/firmware/efi/libstub/Makefile
+++ b/drivers/firmware/efi/libstub/Makefile
@@ -28,6 +28,7 @@
                                   -DEFI_HAVE_MEMCHR -DEFI_HAVE_STRRCHR \
                                   -DEFI_HAVE_STRCMP -fno-builtin -fpic \
                                   $(call cc-option,-mno-single-pic-base)
+cflags-$(CONFIG_ARM)           := $(filter-out -fdata-sections, $(CFLAGS-y))
 cflags-$(CONFIG_RISCV)         += -fpic -DNO_ALTERNATIVE -mno-relax
 cflags-$(CONFIG_LOONGARCH)     += -fpie
liuyuntao (F) March 15, 2024, 11:48 a.m. UTC | #4
On 2024/3/15 18:46, Ard Biesheuvel wrote:
> On Fri, 15 Mar 2024 at 11:23, liuyuntao (F) <liuyuntao12@huawei.com> wrote:
>>
>>
>>
>> On 2024/3/15 16:06, Ard Biesheuvel wrote:
>>> On Fri, 15 Mar 2024 at 07:37, Yuntao Liu <liuyuntao12@huawei.com> wrote:
>>>>
>>>> The current arm32 architecture does not yet support the
>>>> HAVE_LD_DEAD_CODE_DATA_ELIMINATION feature. arm32 is widely used in
>>>> embedded scenarios, and enabling this feature would be beneficial for
>>>> reducing the size of the kernel image.
>>>>
>>>> In order to make this work, we keep the necessary tables by annotating
>>>> them with KEEP, also it requires further changes to linker script to KEEP
>>>> some tables and wildcard compiler generated sections into the right place.
>>>> When using ld.lld for linking, the KEEP keyword is not recognized within
>>>> the OVERLAY command, Ard proposed a concise method to solve this problem.
>>>>
>>>> It boots normally with defconfig, vexpress_defconfig and tinyconfig.
>>>> The size comparison of zImage is as follows:
>>>> defconfig       vexpress_defconfig      tinyconfig
>>>> 5137712         5138024                 424192          no dce
>>>> 5032560         4997824                 298384          dce
>>>> 2.0%            2.7%                    29.7%           shrink
>>>>
>>>> When using smaller config file, there is a significant reduction in the
>>>> size of the zImage.
>>>>
>>>> We also tested this patch on a commercially available single-board
>>>> computer, and the comparison is as follows:
>>>> a15eb_config
>>>> 2161384         no dce
>>>> 2092240         dce
>>>> 3.2%            shrink
>>>>
>>>> The zImage size has been reduced by approximately 3.2%, which is 70KB on
>>>> 2.1M.
>>>>
>>>> Signed-off-by: Yuntao Liu <liuyuntao12@huawei.com>
>>>> Tested-by: Arnd Bergmann <arnd@arndb.de>
>>>> Reviewed-by: Arnd Bergmann <arnd@arndb.de>
>>>> ---
>>>> v3:
>>>>      - A better way to KEEP .vectors section for ld.lld linking.
>>>>
>>>> v2:
>>>>      - Support config XIP_KERNEL.
>>>>      - Support LLVM compilation.
>>>>      - https://lore.kernel.org/all/20240307151231.654025-1-liuyuntao12@huawei.com/
>>>>
>>>> v1: https://lore.kernel.org/all/20240220081527.23408-1-liuyuntao12@huawei.com/
>>>> ---
>>>>    arch/arm/Kconfig                       | 1 +
>>>>    arch/arm/boot/compressed/vmlinux.lds.S | 6 +++++-
>>>>    arch/arm/include/asm/vmlinux.lds.h     | 2 +-
>>>>    arch/arm/kernel/entry-armv.S           | 3 +++
>>>>    arch/arm/kernel/vmlinux-xip.lds.S      | 4 ++--
>>>>    arch/arm/kernel/vmlinux.lds.S          | 6 +++---
>>>>    6 files changed, 15 insertions(+), 7 deletions(-)
>>>>
>>>> diff --git a/arch/arm/Kconfig b/arch/arm/Kconfig
>>>> index b14aed3a17ab..45f25f6e7a62 100644
>>>> --- a/arch/arm/Kconfig
>>>> +++ b/arch/arm/Kconfig
>>>> @@ -114,6 +114,7 @@ config ARM
>>>>           select HAVE_KERNEL_XZ
>>>>           select HAVE_KPROBES if !XIP_KERNEL && !CPU_ENDIAN_BE32 && !CPU_V7M
>>>>           select HAVE_KRETPROBES if HAVE_KPROBES
>>>> +       select HAVE_LD_DEAD_CODE_DATA_ELIMINATION
>>>>           select HAVE_MOD_ARCH_SPECIFIC
>>>>           select HAVE_NMI
>>>>           select HAVE_OPTPROBES if !THUMB2_KERNEL
>>>> diff --git a/arch/arm/boot/compressed/vmlinux.lds.S b/arch/arm/boot/compressed/vmlinux.lds.S
>>>> index 3fcb3e62dc56..affd30714f01 100644
>>>> --- a/arch/arm/boot/compressed/vmlinux.lds.S
>>>> +++ b/arch/arm/boot/compressed/vmlinux.lds.S
>>>> @@ -89,7 +89,11 @@ SECTIONS
>>>>         * The EFI stub always executes from RAM, and runs strictly before the
>>>>         * decompressor, so we can make an exception for its r/w data, and keep it
>>>>         */
>>>> +#ifdef CONFIG_LD_DEAD_CODE_DATA_ELIMINATION
>>>> +    *(.data.* .bss.*)
>>>> +#else
>>>>        *(.data.efistub .bss.efistub)
>>>> +#endif
>>>>        __pecoff_data_end = .;
>>>>
>>>
>>> This is still not right.
>>>
>>> Can you just add -fno-data-sections to cflags-$(CONFIG_ARM) in
>>> drivers/firmware/efi/libstub/Makefile?
>>>
>>
>> I rebuild kernel in such way:
>> --- a/drivers/firmware/efi/libstub/Makefile
>> +++ b/drivers/firmware/efi/libstub/Makefile
>> @@ -28,6 +28,7 @@ cflags-$(CONFIG_ARM)          += -DEFI_HAVE_STRLEN
>> -DEFI_HAVE_STRNLEN \
>>                                      -DEFI_HAVE_MEMCHR -DEFI_HAVE_STRRCHR \
>>                                      -DEFI_HAVE_STRCMP -fno-builtin -fpic \
>>                                      $(call cc-option,-mno-single-pic-base)
>> +cflags-$(CONFIG_ARM)           += -fno-data-sections
>>    cflags-$(CONFIG_RISCV)         += -fpic -DNO_ALTERNATIVE -mno-relax
>>    cflags-$(CONFIG_LOONGARCH)     += -fpi
>> but I am still encountering this error:
>> arm-linux-gnueabi-ld: warning: orphan section `.data.efi_loglevel' from
>> `drivers/firmware/efi/libstub/printk.stub.o' being placed in section
>> `.data.efi_loglevel'
>> arm-linux-gnueabi-ld: warning: orphan section `.data.screen_info_guid'
>> from `drivers/firmware/efi/libstub/screen_info.stub.o' being placed in
>> section `.data.screen_info_guid'
>> arm-linux-gnueabi-ld: warning: orphan section `.data.cpu_state_guid'
>> from `drivers/firmware/efi/libstub/arm32-stub.stub.o' being placed in
>> section `.data.cpu_state_guid'
>> arm-linux-gnueabi-ld: warning: orphan section `.data.efi_nokaslr' from
>> `drivers/firmware/efi/libstub/efi-stub-helper.stub.o' being placed in
>> section `.data.efi_nokaslr'
>> arm-linux-gnueabi-ld: error: zImage file size is incorrect
>>
>> I am puzzled because I could not find any option named
>> -fno-data-sections for GCC.
>>
> 
> How about
> 
> --- a/drivers/firmware/efi/libstub/Makefile
> +++ b/drivers/firmware/efi/libstub/Makefile
> @@ -28,6 +28,7 @@
>                                     -DEFI_HAVE_MEMCHR -DEFI_HAVE_STRRCHR \
>                                     -DEFI_HAVE_STRCMP -fno-builtin -fpic \
>                                     $(call cc-option,-mno-single-pic-base)
> +cflags-$(CONFIG_ARM)           := $(filter-out -fdata-sections, $(CFLAGS-y))
>   cflags-$(CONFIG_RISCV)         += -fpic -DNO_ALTERNATIVE -mno-relax
>   cflags-$(CONFIG_LOONGARCH)     += -fpie

Another error:
cannot initialize array of ‘short unsigned int’ from a string literal 
with type array of ‘unsigned int’
  17 | static const efi_char16_t shim_MokSBState_name[] = L"MokSBStateRT";

I checked the cflags-y variable, and the -fshort-wchar option does 
exist.cflags-y = -std=gnu11 -fshort-wchar -funsigned-char -fno-common 
   -fno-PIE -fno-strict-aliasing ...
Arnd Bergmann March 15, 2024, 11:51 a.m. UTC | #5
On Fri, Mar 15, 2024, at 12:48, liuyuntao (F) wrote:
> On 2024/3/15 18:46, Ard Biesheuvel wrote:
>> @@ -28,6 +28,7 @@
>>                                     -DEFI_HAVE_MEMCHR -DEFI_HAVE_STRRCHR \
>>                                     -DEFI_HAVE_STRCMP -fno-builtin -fpic \
>>                                     $(call cc-option,-mno-single-pic-base)
>> +cflags-$(CONFIG_ARM)           := $(filter-out -fdata-sections, $(CFLAGS-y))
>>   cflags-$(CONFIG_RISCV)         += -fpic -DNO_ALTERNATIVE -mno-relax
>>   cflags-$(CONFIG_LOONGARCH)     += -fpie
>
> Another error:
> cannot initialize array of ‘short unsigned int’ from a string literal 
> with type array of ‘unsigned int’
>   17 | static const efi_char16_t shim_MokSBState_name[] = L"MokSBStateRT";

The line sets cflags-y to an empty string.

It probably should have been

cflags-$(CONFIG_ARM)           := $(filter-out -fdata-sections, $(cflags-y))

(small cflags instead of CFLAGS).

     Arnd
liuyuntao (F) March 15, 2024, 12:10 p.m. UTC | #6
On 2024/3/15 19:51, Arnd Bergmann wrote:
> On Fri, Mar 15, 2024, at 12:48, liuyuntao (F) wrote:
>> On 2024/3/15 18:46, Ard Biesheuvel wrote:
>>> @@ -28,6 +28,7 @@
>>>                                      -DEFI_HAVE_MEMCHR -DEFI_HAVE_STRRCHR \
>>>                                      -DEFI_HAVE_STRCMP -fno-builtin -fpic \
>>>                                      $(call cc-option,-mno-single-pic-base)
>>> +cflags-$(CONFIG_ARM)           := $(filter-out -fdata-sections, $(CFLAGS-y))
>>>    cflags-$(CONFIG_RISCV)         += -fpic -DNO_ALTERNATIVE -mno-relax
>>>    cflags-$(CONFIG_LOONGARCH)     += -fpie
>>
>> Another error:
>> cannot initialize array of ‘short unsigned int’ from a string literal
>> with type array of ‘unsigned int’
>>    17 | static const efi_char16_t shim_MokSBState_name[] = L"MokSBStateRT";
> 
> The line sets cflags-y to an empty string.
> 
> It probably should have been
> 
> cflags-$(CONFIG_ARM)           := $(filter-out -fdata-sections, $(cflags-y))
> 
> (small cflags instead of CFLAGS).
> 
>       Arnd

Oh, my mistake, after fixed this bug, still this error:
> arm-linux-gnueabi-ld: warning: orphan section `.data.efi_loglevel' from `drivers/firmware/efi/libstub/printk.stub.o' being placed in section `.data.efi_loglevel'
> arm-linux-gnueabi-ld: warning: orphan section `.data.screen_info_guid' from `drivers/firmware/efi/libstub/screen_info.stub.o' being placed in section `.data.screen_info_guid'
> arm-linux-gnueabi-ld: warning: orphan section `.data.cpu_state_guid' from `drivers/firmware/efi/libstub/arm32-stub.stub.o' being placed in section `.data.cpu_state_guid'
> arm-linux-gnueabi-ld: warning: orphan section `.data.efi_nokaslr' from `drivers/firmware/efi/libstub/efi-stub-helper.stub.o' being placed in section `.data.efi_nokaslr'

The -fdata-sections option is present in the KBUILD_CFLAGS_KERNEL 
variable; altering cflags-y only affect the KBUILD_CFLAGS variable.
It seems that the -fdata-sections option needs to be removed from the 
KBUILD_CFLAGS_KERNEL variable.
> ifdef CONFIG_LD_DEAD_CODE_DATA_ELIMINATION
> KBUILD_CFLAGS_KERNEL += -ffunction-sections -fdata-sections
> KBUILD_RUSTFLAGS_KERNEL += -Zfunction-sections=y
> LDFLAGS_vmlinux += --gc-sections
> endif
Ard Biesheuvel March 15, 2024, 12:13 p.m. UTC | #7
On Fri, 15 Mar 2024 at 13:10, liuyuntao (F) <liuyuntao12@huawei.com> wrote:
>
>
>
> On 2024/3/15 19:51, Arnd Bergmann wrote:
> > On Fri, Mar 15, 2024, at 12:48, liuyuntao (F) wrote:
> >> On 2024/3/15 18:46, Ard Biesheuvel wrote:
> >>> @@ -28,6 +28,7 @@
> >>>                                      -DEFI_HAVE_MEMCHR -DEFI_HAVE_STRRCHR \
> >>>                                      -DEFI_HAVE_STRCMP -fno-builtin -fpic \
> >>>                                      $(call cc-option,-mno-single-pic-base)
> >>> +cflags-$(CONFIG_ARM)           := $(filter-out -fdata-sections, $(CFLAGS-y))
> >>>    cflags-$(CONFIG_RISCV)         += -fpic -DNO_ALTERNATIVE -mno-relax
> >>>    cflags-$(CONFIG_LOONGARCH)     += -fpie
> >>
> >> Another error:
> >> cannot initialize array of ‘short unsigned int’ from a string literal
> >> with type array of ‘unsigned int’
> >>    17 | static const efi_char16_t shim_MokSBState_name[] = L"MokSBStateRT";
> >
> > The line sets cflags-y to an empty string.
> >
> > It probably should have been
> >
> > cflags-$(CONFIG_ARM)           := $(filter-out -fdata-sections, $(cflags-y))
> >
> > (small cflags instead of CFLAGS).
> >

Oops indeed.

> Oh, my mistake, after fixed this bug, still this error:
> > arm-linux-gnueabi-ld: warning: orphan section `.data.efi_loglevel' from `drivers/firmware/efi/libstub/printk.stub.o' being placed in section `.data.efi_loglevel'
> > arm-linux-gnueabi-ld: warning: orphan section `.data.screen_info_guid' from `drivers/firmware/efi/libstub/screen_info.stub.o' being placed in section `.data.screen_info_guid'
> > arm-linux-gnueabi-ld: warning: orphan section `.data.cpu_state_guid' from `drivers/firmware/efi/libstub/arm32-stub.stub.o' being placed in section `.data.cpu_state_guid'
> > arm-linux-gnueabi-ld: warning: orphan section `.data.efi_nokaslr' from `drivers/firmware/efi/libstub/efi-stub-helper.stub.o' being placed in section `.data.efi_nokaslr'
>
> The -fdata-sections option is present in the KBUILD_CFLAGS_KERNEL
> variable; altering cflags-y only affect the KBUILD_CFLAGS variable.
> It seems that the -fdata-sections option needs to be removed from the
> KBUILD_CFLAGS_KERNEL variable.

Indeed.

So

KBUILD_CFLAGS_KERNEL := $(filter-out -fdata-sections,$(KBUILD_CFLAGS_KERNEL))

(assuming I got it right this time without build testing)
liuyuntao (F) March 15, 2024, 12:21 p.m. UTC | #8
On 2024/3/15 20:13, Ard Biesheuvel wrote:
> On Fri, 15 Mar 2024 at 13:10, liuyuntao (F) <liuyuntao12@huawei.com> wrote:
>>
>>
>>
>> On 2024/3/15 19:51, Arnd Bergmann wrote:
>>> On Fri, Mar 15, 2024, at 12:48, liuyuntao (F) wrote:
>>>> On 2024/3/15 18:46, Ard Biesheuvel wrote:
>>>>> @@ -28,6 +28,7 @@
>>>>>                                       -DEFI_HAVE_MEMCHR -DEFI_HAVE_STRRCHR \
>>>>>                                       -DEFI_HAVE_STRCMP -fno-builtin -fpic \
>>>>>                                       $(call cc-option,-mno-single-pic-base)
>>>>> +cflags-$(CONFIG_ARM)           := $(filter-out -fdata-sections, $(CFLAGS-y))
>>>>>     cflags-$(CONFIG_RISCV)         += -fpic -DNO_ALTERNATIVE -mno-relax
>>>>>     cflags-$(CONFIG_LOONGARCH)     += -fpie
>>>>
>>>> Another error:
>>>> cannot initialize array of ‘short unsigned int’ from a string literal
>>>> with type array of ‘unsigned int’
>>>>     17 | static const efi_char16_t shim_MokSBState_name[] = L"MokSBStateRT";
>>>
>>> The line sets cflags-y to an empty string.
>>>
>>> It probably should have been
>>>
>>> cflags-$(CONFIG_ARM)           := $(filter-out -fdata-sections, $(cflags-y))
>>>
>>> (small cflags instead of CFLAGS).
>>>
> 
> Oops indeed.
> 
>> Oh, my mistake, after fixed this bug, still this error:
>>> arm-linux-gnueabi-ld: warning: orphan section `.data.efi_loglevel' from `drivers/firmware/efi/libstub/printk.stub.o' being placed in section `.data.efi_loglevel'
>>> arm-linux-gnueabi-ld: warning: orphan section `.data.screen_info_guid' from `drivers/firmware/efi/libstub/screen_info.stub.o' being placed in section `.data.screen_info_guid'
>>> arm-linux-gnueabi-ld: warning: orphan section `.data.cpu_state_guid' from `drivers/firmware/efi/libstub/arm32-stub.stub.o' being placed in section `.data.cpu_state_guid'
>>> arm-linux-gnueabi-ld: warning: orphan section `.data.efi_nokaslr' from `drivers/firmware/efi/libstub/efi-stub-helper.stub.o' being placed in section `.data.efi_nokaslr'
>>
>> The -fdata-sections option is present in the KBUILD_CFLAGS_KERNEL
>> variable; altering cflags-y only affect the KBUILD_CFLAGS variable.
>> It seems that the -fdata-sections option needs to be removed from the
>> KBUILD_CFLAGS_KERNEL variable.
> 
> Indeed.
> 
> So
> 
> KBUILD_CFLAGS_KERNEL := $(filter-out -fdata-sections,$(KBUILD_CFLAGS_KERNEL))
> 
> (assuming I got it right this time without build testing)

Um, it not worked,too, same error.
> --- a/drivers/firmware/efi/libstub/Makefile
> +++ b/drivers/firmware/efi/libstub/Makefile
> @@ -28,6 +28,7 @@ cflags-$(CONFIG_ARM)          += -DEFI_HAVE_STRLEN -DEFI_HAVE_STRNLEN \
>                                    -DEFI_HAVE_MEMCHR -DEFI_HAVE_STRRCHR \
>                                    -DEFI_HAVE_STRCMP -fno-builtin -fpic \
>                                    $(call cc-option,-mno-single-pic-base)
> +KBUILD_CFLAGS_KERNE            := $(filter-out -fdata-sections, $(KBUILD_CFLAGS_KERNE))
>  cflags-$(CONFIG_RISCV)         += -fpic -DNO_ALTERNATIVE -mno-relax
>  cflags-$(CONFIG_LOONGARCH)     += -fpie
Ard Biesheuvel March 15, 2024, 12:46 p.m. UTC | #9
On Fri, 15 Mar 2024 at 13:22, liuyuntao (F) <liuyuntao12@huawei.com> wrote:
>
>
>
> On 2024/3/15 20:13, Ard Biesheuvel wrote:
> > On Fri, 15 Mar 2024 at 13:10, liuyuntao (F) <liuyuntao12@huawei.com> wrote:
> >>
> >>
> >>
> >> On 2024/3/15 19:51, Arnd Bergmann wrote:
> >>> On Fri, Mar 15, 2024, at 12:48, liuyuntao (F) wrote:
> >>>> On 2024/3/15 18:46, Ard Biesheuvel wrote:
> >>>>> @@ -28,6 +28,7 @@
> >>>>>                                       -DEFI_HAVE_MEMCHR -DEFI_HAVE_STRRCHR \
> >>>>>                                       -DEFI_HAVE_STRCMP -fno-builtin -fpic \
> >>>>>                                       $(call cc-option,-mno-single-pic-base)
> >>>>> +cflags-$(CONFIG_ARM)           := $(filter-out -fdata-sections, $(CFLAGS-y))
> >>>>>     cflags-$(CONFIG_RISCV)         += -fpic -DNO_ALTERNATIVE -mno-relax
> >>>>>     cflags-$(CONFIG_LOONGARCH)     += -fpie
> >>>>
> >>>> Another error:
> >>>> cannot initialize array of ‘short unsigned int’ from a string literal
> >>>> with type array of ‘unsigned int’
> >>>>     17 | static const efi_char16_t shim_MokSBState_name[] = L"MokSBStateRT";
> >>>
> >>> The line sets cflags-y to an empty string.
> >>>
> >>> It probably should have been
> >>>
> >>> cflags-$(CONFIG_ARM)           := $(filter-out -fdata-sections, $(cflags-y))
> >>>
> >>> (small cflags instead of CFLAGS).
> >>>
> >
> > Oops indeed.
> >
> >> Oh, my mistake, after fixed this bug, still this error:
> >>> arm-linux-gnueabi-ld: warning: orphan section `.data.efi_loglevel' from `drivers/firmware/efi/libstub/printk.stub.o' being placed in section `.data.efi_loglevel'
> >>> arm-linux-gnueabi-ld: warning: orphan section `.data.screen_info_guid' from `drivers/firmware/efi/libstub/screen_info.stub.o' being placed in section `.data.screen_info_guid'
> >>> arm-linux-gnueabi-ld: warning: orphan section `.data.cpu_state_guid' from `drivers/firmware/efi/libstub/arm32-stub.stub.o' being placed in section `.data.cpu_state_guid'
> >>> arm-linux-gnueabi-ld: warning: orphan section `.data.efi_nokaslr' from `drivers/firmware/efi/libstub/efi-stub-helper.stub.o' being placed in section `.data.efi_nokaslr'
> >>
> >> The -fdata-sections option is present in the KBUILD_CFLAGS_KERNEL
> >> variable; altering cflags-y only affect the KBUILD_CFLAGS variable.
> >> It seems that the -fdata-sections option needs to be removed from the
> >> KBUILD_CFLAGS_KERNEL variable.
> >
> > Indeed.
> >
> > So
> >
> > KBUILD_CFLAGS_KERNEL := $(filter-out -fdata-sections,$(KBUILD_CFLAGS_KERNEL))
> >
> > (assuming I got it right this time without build testing)
>
> Um, it not worked,too, same error.

Aren't you missing a 'L' ?

> > --- a/drivers/firmware/efi/libstub/Makefile
> > +++ b/drivers/firmware/efi/libstub/Makefile
> > @@ -28,6 +28,7 @@ cflags-$(CONFIG_ARM)          += -DEFI_HAVE_STRLEN -DEFI_HAVE_STRNLEN \
> >                                    -DEFI_HAVE_MEMCHR -DEFI_HAVE_STRRCHR \
> >

> > ifdef CONFIG_LD_DEAD_CODE_DATA_ELIMINATION
> > KBUILD_CFLAGS_KERNEL += -ffunction-sections -fdata-sections
> > KBUILD_RUSTFLAGS_KERNEL += -Zfunction-sections=y
> > LDFLAGS_vmlinux += --gc-sections
> > endif
>
>                                   -DEFI_HAVE_STRCMP -fno-builtin -fpic \
> >                                    $(call cc-option,-mno-single-pic-base)
> > +KBUILD_CFLAGS_KERNE            := $(filter-out -fdata-sections, $(KBUILD_CFLAGS_KERNE))
> >  cflags-$(CONFIG_RISCV)         += -fpic -DNO_ALTERNATIVE -mno-relax
> >  cflags-$(CONFIG_LOONGARCH)     += -fpie
>
>
liuyuntao (F) March 15, 2024, 12:51 p.m. UTC | #10
On 2024/3/15 20:46, Ard Biesheuvel wrote:
> On Fri, 15 Mar 2024 at 13:22, liuyuntao (F) <liuyuntao12@huawei.com> wrote:
>>
>>
>>
>> On 2024/3/15 20:13, Ard Biesheuvel wrote:
>>> On Fri, 15 Mar 2024 at 13:10, liuyuntao (F) <liuyuntao12@huawei.com> wrote:
>>>>
>>>>
>>>>
>>>> On 2024/3/15 19:51, Arnd Bergmann wrote:
>>>>> On Fri, Mar 15, 2024, at 12:48, liuyuntao (F) wrote:
>>>>>> On 2024/3/15 18:46, Ard Biesheuvel wrote:
>>>>>>> @@ -28,6 +28,7 @@
>>>>>>>                                        -DEFI_HAVE_MEMCHR -DEFI_HAVE_STRRCHR \
>>>>>>>                                        -DEFI_HAVE_STRCMP -fno-builtin -fpic \
>>>>>>>                                        $(call cc-option,-mno-single-pic-base)
>>>>>>> +cflags-$(CONFIG_ARM)           := $(filter-out -fdata-sections, $(CFLAGS-y))
>>>>>>>      cflags-$(CONFIG_RISCV)         += -fpic -DNO_ALTERNATIVE -mno-relax
>>>>>>>      cflags-$(CONFIG_LOONGARCH)     += -fpie
>>>>>>
>>>>>> Another error:
>>>>>> cannot initialize array of ‘short unsigned int’ from a string literal
>>>>>> with type array of ‘unsigned int’
>>>>>>      17 | static const efi_char16_t shim_MokSBState_name[] = L"MokSBStateRT";
>>>>>
>>>>> The line sets cflags-y to an empty string.
>>>>>
>>>>> It probably should have been
>>>>>
>>>>> cflags-$(CONFIG_ARM)           := $(filter-out -fdata-sections, $(cflags-y))
>>>>>
>>>>> (small cflags instead of CFLAGS).
>>>>>
>>>
>>> Oops indeed.
>>>
>>>> Oh, my mistake, after fixed this bug, still this error:
>>>>> arm-linux-gnueabi-ld: warning: orphan section `.data.efi_loglevel' from `drivers/firmware/efi/libstub/printk.stub.o' being placed in section `.data.efi_loglevel'
>>>>> arm-linux-gnueabi-ld: warning: orphan section `.data.screen_info_guid' from `drivers/firmware/efi/libstub/screen_info.stub.o' being placed in section `.data.screen_info_guid'
>>>>> arm-linux-gnueabi-ld: warning: orphan section `.data.cpu_state_guid' from `drivers/firmware/efi/libstub/arm32-stub.stub.o' being placed in section `.data.cpu_state_guid'
>>>>> arm-linux-gnueabi-ld: warning: orphan section `.data.efi_nokaslr' from `drivers/firmware/efi/libstub/efi-stub-helper.stub.o' being placed in section `.data.efi_nokaslr'
>>>>
>>>> The -fdata-sections option is present in the KBUILD_CFLAGS_KERNEL
>>>> variable; altering cflags-y only affect the KBUILD_CFLAGS variable.
>>>> It seems that the -fdata-sections option needs to be removed from the
>>>> KBUILD_CFLAGS_KERNEL variable.
>>>
>>> Indeed.
>>>
>>> So
>>>
>>> KBUILD_CFLAGS_KERNEL := $(filter-out -fdata-sections,$(KBUILD_CFLAGS_KERNEL))
>>>
>>> (assuming I got it right this time without build testing)
>>
>> Um, it not worked,too, same error.
> 
> Aren't you missing a 'L' ?
> 
>>> --- a/drivers/firmware/efi/libstub/Makefile
>>> +++ b/drivers/firmware/efi/libstub/Makefile
>>> @@ -28,6 +28,7 @@ cflags-$(CONFIG_ARM)          += -DEFI_HAVE_STRLEN -DEFI_HAVE_STRNLEN \
>>>                                     -DEFI_HAVE_MEMCHR -DEFI_HAVE_STRRCHR \
>>>
> 
>>> ifdef CONFIG_LD_DEAD_CODE_DATA_ELIMINATION
>>> KBUILD_CFLAGS_KERNEL += -ffunction-sections -fdata-sections
>>> KBUILD_RUSTFLAGS_KERNEL += -Zfunction-sections=y
>>> LDFLAGS_vmlinux += --gc-sections
>>> endif
>>
>>                                    -DEFI_HAVE_STRCMP -fno-builtin -fpic \
>>>                                     $(call cc-option,-mno-single-pic-base)
>>> +KBUILD_CFLAGS_KERNE            := $(filter-out -fdata-sections, $(KBUILD_CFLAGS_KERNE))
>>>   cflags-$(CONFIG_RISCV)         += -fpic -DNO_ALTERNATIVE -mno-relax
>>>   cflags-$(CONFIG_LOONGARCH)     += -fpie
>>
>>
Sorry, I was too careless.
How about reset KBUILD_CFLAGS_KERNEL?

> +# KBUILD_CFLAGS_KERNEL is is set by CONFIG_LD_DEAD_CODE_DATA_ELIMINATION,
> +# not need here, just reset it.
> +KBUILD_CFLAGS_KERNEL :=
> +
>  # non-x86 reuses KBUILD_CFLAGS, x86 does not
>  cflags-y                       := $(KBUILD_CFLAGS)
Ard Biesheuvel March 15, 2024, 12:58 p.m. UTC | #11
On Fri, 15 Mar 2024 at 13:52, liuyuntao (F) <liuyuntao12@huawei.com> wrote:
>
>
>
> On 2024/3/15 20:46, Ard Biesheuvel wrote:
> > On Fri, 15 Mar 2024 at 13:22, liuyuntao (F) <liuyuntao12@huawei.com> wrote:
> >>
> >>
> >>
> >> On 2024/3/15 20:13, Ard Biesheuvel wrote:
> >>> On Fri, 15 Mar 2024 at 13:10, liuyuntao (F) <liuyuntao12@huawei.com> wrote:
> >>>>
> >>>>
> >>>>
> >>>> On 2024/3/15 19:51, Arnd Bergmann wrote:
> >>>>> On Fri, Mar 15, 2024, at 12:48, liuyuntao (F) wrote:
> >>>>>> On 2024/3/15 18:46, Ard Biesheuvel wrote:
> >>>>>>> @@ -28,6 +28,7 @@
> >>>>>>>                                        -DEFI_HAVE_MEMCHR -DEFI_HAVE_STRRCHR \
> >>>>>>>                                        -DEFI_HAVE_STRCMP -fno-builtin -fpic \
> >>>>>>>                                        $(call cc-option,-mno-single-pic-base)
> >>>>>>> +cflags-$(CONFIG_ARM)           := $(filter-out -fdata-sections, $(CFLAGS-y))
> >>>>>>>      cflags-$(CONFIG_RISCV)         += -fpic -DNO_ALTERNATIVE -mno-relax
> >>>>>>>      cflags-$(CONFIG_LOONGARCH)     += -fpie
> >>>>>>
> >>>>>> Another error:
> >>>>>> cannot initialize array of ‘short unsigned int’ from a string literal
> >>>>>> with type array of ‘unsigned int’
> >>>>>>      17 | static const efi_char16_t shim_MokSBState_name[] = L"MokSBStateRT";
> >>>>>
> >>>>> The line sets cflags-y to an empty string.
> >>>>>
> >>>>> It probably should have been
> >>>>>
> >>>>> cflags-$(CONFIG_ARM)           := $(filter-out -fdata-sections, $(cflags-y))
> >>>>>
> >>>>> (small cflags instead of CFLAGS).
> >>>>>
> >>>
> >>> Oops indeed.
> >>>
> >>>> Oh, my mistake, after fixed this bug, still this error:
> >>>>> arm-linux-gnueabi-ld: warning: orphan section `.data.efi_loglevel' from `drivers/firmware/efi/libstub/printk.stub.o' being placed in section `.data.efi_loglevel'
> >>>>> arm-linux-gnueabi-ld: warning: orphan section `.data.screen_info_guid' from `drivers/firmware/efi/libstub/screen_info.stub.o' being placed in section `.data.screen_info_guid'
> >>>>> arm-linux-gnueabi-ld: warning: orphan section `.data.cpu_state_guid' from `drivers/firmware/efi/libstub/arm32-stub.stub.o' being placed in section `.data.cpu_state_guid'
> >>>>> arm-linux-gnueabi-ld: warning: orphan section `.data.efi_nokaslr' from `drivers/firmware/efi/libstub/efi-stub-helper.stub.o' being placed in section `.data.efi_nokaslr'
> >>>>
> >>>> The -fdata-sections option is present in the KBUILD_CFLAGS_KERNEL
> >>>> variable; altering cflags-y only affect the KBUILD_CFLAGS variable.
> >>>> It seems that the -fdata-sections option needs to be removed from the
> >>>> KBUILD_CFLAGS_KERNEL variable.
> >>>
> >>> Indeed.
> >>>
> >>> So
> >>>
> >>> KBUILD_CFLAGS_KERNEL := $(filter-out -fdata-sections,$(KBUILD_CFLAGS_KERNEL))
> >>>
> >>> (assuming I got it right this time without build testing)
> >>
> >> Um, it not worked,too, same error.
> >
> > Aren't you missing a 'L' ?
> >
> >>> --- a/drivers/firmware/efi/libstub/Makefile
> >>> +++ b/drivers/firmware/efi/libstub/Makefile
> >>> @@ -28,6 +28,7 @@ cflags-$(CONFIG_ARM)          += -DEFI_HAVE_STRLEN -DEFI_HAVE_STRNLEN \
> >>>                                     -DEFI_HAVE_MEMCHR -DEFI_HAVE_STRRCHR \
> >>>
> >
> >>> ifdef CONFIG_LD_DEAD_CODE_DATA_ELIMINATION
> >>> KBUILD_CFLAGS_KERNEL += -ffunction-sections -fdata-sections
> >>> KBUILD_RUSTFLAGS_KERNEL += -Zfunction-sections=y
> >>> LDFLAGS_vmlinux += --gc-sections
> >>> endif
> >>
> >>                                    -DEFI_HAVE_STRCMP -fno-builtin -fpic \
> >>>                                     $(call cc-option,-mno-single-pic-base)
> >>> +KBUILD_CFLAGS_KERNE            := $(filter-out -fdata-sections, $(KBUILD_CFLAGS_KERNE))
> >>>   cflags-$(CONFIG_RISCV)         += -fpic -DNO_ALTERNATIVE -mno-relax
> >>>   cflags-$(CONFIG_LOONGARCH)     += -fpie
> >>
> >>
> Sorry, I was too careless.
> How about reset KBUILD_CFLAGS_KERNEL?
>

No, that is not the solution.

Either avoid setting -fdata-sections altogether, or filter it out.


> > +# KBUILD_CFLAGS_KERNEL is is set by CONFIG_LD_DEAD_CODE_DATA_ELIMINATION,
> > +# not need here, just reset it.
> > +KBUILD_CFLAGS_KERNEL :=
> > +
> >  # non-x86 reuses KBUILD_CFLAGS, x86 does not
> >  cflags-y                       := $(KBUILD_CFLAGS)
>
diff mbox series

Patch

diff --git a/arch/arm/Kconfig b/arch/arm/Kconfig
index b14aed3a17ab..45f25f6e7a62 100644
--- a/arch/arm/Kconfig
+++ b/arch/arm/Kconfig
@@ -114,6 +114,7 @@  config ARM
 	select HAVE_KERNEL_XZ
 	select HAVE_KPROBES if !XIP_KERNEL && !CPU_ENDIAN_BE32 && !CPU_V7M
 	select HAVE_KRETPROBES if HAVE_KPROBES
+	select HAVE_LD_DEAD_CODE_DATA_ELIMINATION
 	select HAVE_MOD_ARCH_SPECIFIC
 	select HAVE_NMI
 	select HAVE_OPTPROBES if !THUMB2_KERNEL
diff --git a/arch/arm/boot/compressed/vmlinux.lds.S b/arch/arm/boot/compressed/vmlinux.lds.S
index 3fcb3e62dc56..affd30714f01 100644
--- a/arch/arm/boot/compressed/vmlinux.lds.S
+++ b/arch/arm/boot/compressed/vmlinux.lds.S
@@ -89,7 +89,11 @@  SECTIONS
      * The EFI stub always executes from RAM, and runs strictly before the
      * decompressor, so we can make an exception for its r/w data, and keep it
      */
+#ifdef CONFIG_LD_DEAD_CODE_DATA_ELIMINATION
+    *(.data.* .bss.*)
+#else
     *(.data.efistub .bss.efistub)
+#endif
     __pecoff_data_end = .;
 
     /*
@@ -125,7 +129,7 @@  SECTIONS
 
   . = BSS_START;
   __bss_start = .;
-  .bss			: { *(.bss) }
+  .bss			: { *(.bss .bss.*) }
   _end = .;
 
   . = ALIGN(8);		/* the stack must be 64-bit aligned */
diff --git a/arch/arm/include/asm/vmlinux.lds.h b/arch/arm/include/asm/vmlinux.lds.h
index 4c8632d5c432..d60f6e83a9f7 100644
--- a/arch/arm/include/asm/vmlinux.lds.h
+++ b/arch/arm/include/asm/vmlinux.lds.h
@@ -42,7 +42,7 @@ 
 #define PROC_INFO							\
 		. = ALIGN(4);						\
 		__proc_info_begin = .;					\
-		*(.proc.info.init)					\
+		KEEP(*(.proc.info.init))				\
 		__proc_info_end = .;
 
 #define IDMAP_TEXT							\
diff --git a/arch/arm/kernel/entry-armv.S b/arch/arm/kernel/entry-armv.S
index 6150a716828c..f01d23a220e6 100644
--- a/arch/arm/kernel/entry-armv.S
+++ b/arch/arm/kernel/entry-armv.S
@@ -1065,6 +1065,7 @@  vector_addrexcptn:
 	.globl	vector_fiq
 
 	.section .vectors, "ax", %progbits
+	.reloc  .text, R_ARM_NONE, .
 	W(b)	vector_rst
 	W(b)	vector_und
 ARM(	.reloc	., R_ARM_LDR_PC_G0, .L__vector_swi		)
@@ -1078,6 +1079,7 @@  THUMB(	.reloc	., R_ARM_THM_PC12, .L__vector_swi		)
 
 #ifdef CONFIG_HARDEN_BRANCH_HISTORY
 	.section .vectors.bhb.loop8, "ax", %progbits
+	.reloc  .text, R_ARM_NONE, .
 	W(b)	vector_rst
 	W(b)	vector_bhb_loop8_und
 ARM(	.reloc	., R_ARM_LDR_PC_G0, .L__vector_bhb_loop8_swi	)
@@ -1090,6 +1092,7 @@  THUMB(	.reloc	., R_ARM_THM_PC12, .L__vector_bhb_loop8_swi	)
 	W(b)	vector_bhb_loop8_fiq
 
 	.section .vectors.bhb.bpiall, "ax", %progbits
+	.reloc  .text, R_ARM_NONE, .
 	W(b)	vector_rst
 	W(b)	vector_bhb_bpiall_und
 ARM(	.reloc	., R_ARM_LDR_PC_G0, .L__vector_bhb_bpiall_swi	)
diff --git a/arch/arm/kernel/vmlinux-xip.lds.S b/arch/arm/kernel/vmlinux-xip.lds.S
index c16d196b5aad..5eddb75a7174 100644
--- a/arch/arm/kernel/vmlinux-xip.lds.S
+++ b/arch/arm/kernel/vmlinux-xip.lds.S
@@ -63,7 +63,7 @@  SECTIONS
 	. = ALIGN(4);
 	__ex_table : AT(ADDR(__ex_table) - LOAD_OFFSET) {
 		__start___ex_table = .;
-		ARM_MMU_KEEP(*(__ex_table))
+		ARM_MMU_KEEP(KEEP(*(__ex_table)))
 		__stop___ex_table = .;
 	}
 
@@ -83,7 +83,7 @@  SECTIONS
 	}
 	.init.arch.info : {
 		__arch_info_begin = .;
-		*(.arch.info.init)
+		KEEP(*(.arch.info.init))
 		__arch_info_end = .;
 	}
 	.init.tagtable : {
diff --git a/arch/arm/kernel/vmlinux.lds.S b/arch/arm/kernel/vmlinux.lds.S
index bd9127c4b451..de373c6c2ae8 100644
--- a/arch/arm/kernel/vmlinux.lds.S
+++ b/arch/arm/kernel/vmlinux.lds.S
@@ -74,7 +74,7 @@  SECTIONS
 	. = ALIGN(4);
 	__ex_table : AT(ADDR(__ex_table) - LOAD_OFFSET) {
 		__start___ex_table = .;
-		ARM_MMU_KEEP(*(__ex_table))
+		ARM_MMU_KEEP(KEEP(*(__ex_table)))
 		__stop___ex_table = .;
 	}
 
@@ -99,7 +99,7 @@  SECTIONS
 	}
 	.init.arch.info : {
 		__arch_info_begin = .;
-		*(.arch.info.init)
+		KEEP(*(.arch.info.init))
 		__arch_info_end = .;
 	}
 	.init.tagtable : {
@@ -116,7 +116,7 @@  SECTIONS
 #endif
 	.init.pv_table : {
 		__pv_table_begin = .;
-		*(.pv_table)
+		KEEP(*(.pv_table))
 		__pv_table_end = .;
 	}