From 87f68b4dfed14eb08132fe9798fb209f1bed64f2 Mon Sep 17 00:00:00 2001 From: Nicolas Pitre Date: Tue, 30 Jul 2024 01:28:27 -0400 Subject: [PATCH] arm64: linker: move data section between rodata and bss Having .data after .bss creates a big empty gap in zephyr.bin to provide proper offset for the data section. Reversing them allows for dropping .bss from zephyr.bin's output as it is typically done, saving precious flash memory space. For demonstration purpose, let's consider those changes: |--- a/samples/hello_world/src/main.c |+++ b/samples/hello_world/src/main.c |@@ -6,9 +6,12 @@ | | #include | |+int dummybuf[1024 * 1024]; |+ | int main(void) | { | printf("Hello World! %s\n", CONFIG_BOARD_TARGET); |+ dummybuf[0] = 42; | | return 0; | } Before this commit: $ ls -l build/zephyr/zephyr.bin -rwxr-xr-x 1 nico nico 4561908 Aug 2 15:13 build/zephyr/zephyr.bin* After this commit: $ ls -l build/zephyr/zephyr.bin -rwxr-xr-x 1 nico nico 37028 Aug 2 15:17 build/zephyr/zephyr.bin* While at it, remove some more repetitive comments with no value. Also defaults CONFIG_LINKER_LAST_SECTION_ID to n as it otherwise screws everything up for the same reason as above. Signed-off-by: Nicolas Pitre --- Kconfig.zephyr | 2 +- include/zephyr/arch/arm64/scripts/linker.ld | 85 ++++++++------------- 2 files changed, 33 insertions(+), 54 deletions(-) diff --git a/Kconfig.zephyr b/Kconfig.zephyr index 0e5c665b21d..89cdefdc87f 100644 --- a/Kconfig.zephyr +++ b/Kconfig.zephyr @@ -275,7 +275,7 @@ config LINKER_GENERIC_SECTIONS_PRESENT_AT_BOOT config LINKER_LAST_SECTION_ID bool "Last section identifier" - default y + default y if !ARM64 depends on ARM || ARM64 || RISCV help If enabled, the last section will contain an identifier. diff --git a/include/zephyr/arch/arm64/scripts/linker.ld b/include/zephyr/arch/arm64/scripts/linker.ld index a8fd0937b2e..d8d38df47e4 100644 --- a/include/zephyr/arch/arm64/scripts/linker.ld +++ b/include/zephyr/arch/arm64/scripts/linker.ld @@ -161,12 +161,10 @@ SECTIONS __exidx_end = .; } GROUP_ROM_LINK_IN(RAMABLE_REGION, ROMABLE_REGION) - __rodata_region_start = .; + MMU_ALIGN; + __rodata_region_start = .; #include -/* Located in generated directory. This file is populated by calling - * zephyr_linker_sources(ROM_SECTIONS ...). Useful for grouping iterable RO structs. - */ #include #include @@ -184,9 +182,6 @@ SECTIONS *(.got) *(.got.plt) -/* Located in generated directory. This file is populated by the - * zephyr_linker_sources() Cmake function. - */ #include #include @@ -194,8 +189,8 @@ SECTIONS } GROUP_ROM_LINK_IN(RAMABLE_REGION, ROMABLE_REGION) #include - MMU_ALIGN; + MMU_ALIGN; __rodata_region_end = .; __rodata_region_size = __rodata_region_end - __rodata_region_start; __rom_region_end = .; @@ -214,19 +209,32 @@ SECTIONS GROUP_START(RAMABLE_REGION) +#ifdef CONFIG_XIP . = RAM_ADDR; - /* Align the start of image RAM with the - * minimum granularity required by MMU. - */ - . = ALIGN(_region_min_align); +#endif + MMU_ALIGN; _image_ram_start = .; #ifdef CONFIG_XIP z_mapped_start = .; #endif -/* Located in generated directory. This file is populated by the - * zephyr_linker_sources() Cmake function. - */ + __data_region_start = .; + + SECTION_DATA_PROLOGUE(_DATA_SECTION_NAME,,) + { + MMU_ALIGN; + __data_start = .; + *(.data) + *(".data.*") + *(".kernel.*") + +#include + + __data_end = .; + } GROUP_DATA_LINK_IN(RAMABLE_REGION, ROMABLE_REGION) + __data_size = __data_end - __data_start; + __data_load_start = LOADADDR(_DATA_SECTION_NAME); + #include #if defined(CONFIG_USERSPACE) @@ -249,6 +257,15 @@ SECTIONS _app_smem_rom_start = LOADADDR(_APP_SMEM_SECTION_NAME); #endif /* CONFIG_USERSPACE */ +#include +#include +#include + +#include + + __data_region_end = .; + __data_region_load_start = LOADADDR(_DATA_SECTION_NAME); + SECTION_DATA_PROLOGUE(_BSS_SECTION_NAME,(NOLOAD), BSS_ALIGN) { . = ALIGN(8); @@ -265,48 +282,10 @@ SECTIONS #include - SECTION_DATA_PROLOGUE(_DATA_SECTION_NAME,,) - { - __data_region_start = .; - __data_start = .; - *(.data) - *(".data.*") - *(".kernel.*") - -/* Located in generated directory. This file is populated by the - * zephyr_linker_sources() Cmake function. - */ -#include - - __data_end = .; - - } GROUP_DATA_LINK_IN(RAMABLE_REGION, ROMABLE_REGION) - __data_size = __data_end - __data_start; - __data_load_start = LOADADDR(_DATA_SECTION_NAME); - - __data_region_load_start = LOADADDR(_DATA_SECTION_NAME); - -#include -#include -#include - -/* Located in generated directory. This file is populated by the - * zephyr_linker_sources() Cmake function. - */ -#include - - __data_region_end = .; - - /* Define linker symbols */ - __kernel_ram_end = RAM_ADDR + RAM_SIZE; __kernel_ram_size = __kernel_ram_end - __kernel_ram_start; - -/* Located in generated directory. This file is populated by the - * zephyr_linker_sources() Cmake function. - */ #include #define LAST_RAM_ALIGN MMU_ALIGN;