diff --git a/CMakeLists.txt b/CMakeLists.txt index 5bd9348cb57..3bd9bf49e08 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1714,13 +1714,8 @@ if(CONFIG_BUILD_OUTPUT_ADJUST_LMA) ) endif() -if(NOT CONFIG_CPP_EXCEPTIONS) - set(eh_frame_section ".eh_frame") -else() - set(eh_frame_section "") -endif() set(remove_sections_argument_list "") -foreach(section .comment COMMON ${eh_frame_section}) +foreach(section .comment COMMON) list(APPEND remove_sections_argument_list $${section}) endforeach() diff --git a/include/zephyr/arch/arm/cortex_a_r/scripts/linker.ld b/include/zephyr/arch/arm/cortex_a_r/scripts/linker.ld index 1d2744e253b..477072cac36 100644 --- a/include/zephyr/arch/arm/cortex_a_r/scripts/linker.ld +++ b/include/zephyr/arch/arm/cortex_a_r/scripts/linker.ld @@ -402,11 +402,9 @@ GROUP_END(OCM) /* Must be last in romable region */ SECTION_PROLOGUE(.last_section,,) { -#ifdef CONFIG_LINKER_LAST_SECTION_ID - /* Fill last section with a word to ensure location counter and actual rom - * region data usage match. */ - LONG(CONFIG_LINKER_LAST_SECTION_ID_PATTERN) -#endif + /* .last_section contains a fixed word to ensure location counter and actual + * rom region data usage match when CONFIG_LINKER_LAST_SECTION_ID=y. */ + KEEP(*(.last_section)) } GROUP_LINK_IN(ROMABLE_REGION) /* To provide the image size as a const expression, diff --git a/include/zephyr/arch/arm/cortex_m/scripts/linker.ld b/include/zephyr/arch/arm/cortex_m/scripts/linker.ld index 6a1e91b66ed..8e0a18186f7 100644 --- a/include/zephyr/arch/arm/cortex_m/scripts/linker.ld +++ b/include/zephyr/arch/arm/cortex_m/scripts/linker.ld @@ -488,11 +488,9 @@ GROUP_END(DTCM) /* Must be last in romable region */ SECTION_PROLOGUE(.last_section,,) { -#ifdef CONFIG_LINKER_LAST_SECTION_ID - /* Fill last section with a word to ensure location counter and actual rom - * region data usage match. */ - LONG(CONFIG_LINKER_LAST_SECTION_ID_PATTERN) -#endif + /* .last_section contains a fixed word to ensure location counter and actual + * rom region data usage match when CONFIG_LINKER_LAST_SECTION_ID=y. */ + KEEP(*(.last_section)) } GROUP_LINK_IN(ROMABLE_REGION) /* To provide the image size as a const expression, diff --git a/include/zephyr/arch/arm64/scripts/linker.ld b/include/zephyr/arch/arm64/scripts/linker.ld index d5a4e25abaa..ee29e43b77b 100644 --- a/include/zephyr/arch/arm64/scripts/linker.ld +++ b/include/zephyr/arch/arm64/scripts/linker.ld @@ -351,11 +351,9 @@ SECTIONS /* Must be last in romable region */ SECTION_PROLOGUE(.last_section,,) { -#ifdef CONFIG_LINKER_LAST_SECTION_ID - /* Fill last section with a word to ensure location counter and actual rom - * region data usage match. */ - LONG(CONFIG_LINKER_LAST_SECTION_ID_PATTERN) -#endif + /* .last_section contains a fixed word to ensure location counter and actual + * rom region data usage match when CONFIG_LINKER_LAST_SECTION_ID=y. */ + KEEP(*(.last_section)) } GROUP_ROM_LINK_IN(RAMABLE_REGION, ROMABLE_REGION) /* To provide the image size as a const expression, diff --git a/include/zephyr/arch/riscv/common/linker.ld b/include/zephyr/arch/riscv/common/linker.ld index 87aa1590199..a63bc034d64 100644 --- a/include/zephyr/arch/riscv/common/linker.ld +++ b/include/zephyr/arch/riscv/common/linker.ld @@ -454,15 +454,13 @@ GROUP_END(DTCM) /* Must be last in romable region */ SECTION_PROLOGUE(.last_section,,) { -#ifdef CONFIG_LINKER_LAST_SECTION_ID - /* Fill last section with a word to ensure location counter and actual rom - * region data usage match. */ - LONG(CONFIG_LINKER_LAST_SECTION_ID_PATTERN) + /* .last_section contains a fixed word to ensure location counter and actual + * rom region data usage match when CONFIG_LINKER_LAST_SECTION_ID=y. */ + KEEP(*(.last_section)) /* __rom_region_size is used when configuring the PMP entry of the ROM region. * Addresses (pmpaddr) in PMP registers need to be aligned to 4. Align * __rom_region_size to 4 to meet that requirement. */ MPU_MIN_SIZE_ALIGN -#endif } GROUP_LINK_IN(ROMABLE_REGION) /* To provide the image size as a const expression, diff --git a/include/zephyr/arch/x86/ia32/linker.ld b/include/zephyr/arch/x86/ia32/linker.ld index 02360d98202..298e8e4f56e 100644 --- a/include/zephyr/arch/x86/ia32/linker.ld +++ b/include/zephyr/arch/x86/ia32/linker.ld @@ -557,12 +557,6 @@ SECTIONS #include /DISCARD/ : { *(.note.GNU-stack) } -/* - * eh_frame section won't be removed even with "--gc-sections" by LLVM lld. - */ -#if !defined(CONFIG_CPP_EXCEPTIONS) - /DISCARD/ : { *(.eh_frame) } -#endif /* * The sections below are still treated as warnings diff --git a/include/zephyr/arch/x86/intel64/linker.ld b/include/zephyr/arch/x86/intel64/linker.ld index 9506b301486..d220d8ccac4 100644 --- a/include/zephyr/arch/x86/intel64/linker.ld +++ b/include/zephyr/arch/x86/intel64/linker.ld @@ -223,12 +223,6 @@ SECTIONS *(.rel.*) *(.rela.*) } -/* - * eh_frame section won't be removed even with "--gc-sections" by LLVM lld. - */ -#if !defined(CONFIG_CPP_EXCEPTIONS) - /DISCARD/ : { *(.eh_frame) } -#endif /* * The sections below are still treated as warnings diff --git a/include/zephyr/linker/cplusplus-rom.ld b/include/zephyr/linker/cplusplus-rom.ld index 5981e2f7b98..728511647a2 100644 --- a/include/zephyr/linker/cplusplus-rom.ld +++ b/include/zephyr/linker/cplusplus-rom.ld @@ -24,3 +24,7 @@ #endif /* CONFIG_CPP_EXCEPTIONS */ #endif /* CONFIG_CPP */ + +#if !defined(CONFIG_CPP_EXCEPTIONS) + /DISCARD/ : { *(.eh_frame) } +#endif diff --git a/lib/utils/CMakeLists.txt b/lib/utils/CMakeLists.txt index e0e1673f445..1d6cca5b4fa 100644 --- a/lib/utils/CMakeLists.txt +++ b/lib/utils/CMakeLists.txt @@ -25,3 +25,5 @@ zephyr_library_include_directories( ${ZEPHYR_BASE}/kernel/include ${ZEPHYR_BASE}/arch/${ARCH}/include ) + +zephyr_sources_ifdef(CONFIG_LINKER_LAST_SECTION_ID last_section_id.c) diff --git a/lib/utils/last_section_id.c b/lib/utils/last_section_id.c new file mode 100644 index 00000000000..02a20654ce1 --- /dev/null +++ b/lib/utils/last_section_id.c @@ -0,0 +1,10 @@ +/* + * Copyright (C) 2025, Nordic Semiconductor ASA + * + * SPDX-License-Identifier: Apache-2.0 + * + */ +#include + +static uint32_t last_id __attribute__((section(".last_section"))) __attribute__((__used__)) = + CONFIG_LINKER_LAST_SECTION_ID_PATTERN; diff --git a/soc/andestech/ae350/linker.ld b/soc/andestech/ae350/linker.ld index 4c94c705212..e9a05f219b5 100644 --- a/soc/andestech/ae350/linker.ld +++ b/soc/andestech/ae350/linker.ld @@ -412,11 +412,9 @@ GROUP_END(DTCM) /* Must be last in romable region */ SECTION_PROLOGUE(.last_section,,) { -#ifdef CONFIG_LINKER_LAST_SECTION_ID - /* Fill last section with a word to ensure location counter and actual rom - * region data usage match. */ - LONG(CONFIG_LINKER_LAST_SECTION_ID_PATTERN) -#endif + /* .last_section contains a fixed word to ensure location counter and actual + * rom region data usage match when CONFIG_LINKER_LAST_SECTION_ID=y. */ + KEEP(*(.last_section)) } GROUP_LINK_IN(ROMABLE_REGION) /* Because ROMABLE_REGION != RAMABLE_REGION in XIP-system, it is valid diff --git a/soc/infineon/cat1b/cyw20829/linker.ld b/soc/infineon/cat1b/cyw20829/linker.ld index 2395740c8a5..5f6b02e25bd 100644 --- a/soc/infineon/cat1b/cyw20829/linker.ld +++ b/soc/infineon/cat1b/cyw20829/linker.ld @@ -466,11 +466,9 @@ GROUP_END(DTCM) /* Must be last in romable region */ SECTION_PROLOGUE(.last_section,,) { -#ifdef CONFIG_LINKER_LAST_SECTION_ID - /* Fill last section with a word to ensure location counter and actual rom - * region data usage match. */ - LONG(CONFIG_LINKER_LAST_SECTION_ID_PATTERN) -#endif + /* .last_section contains a fixed word to ensure location counter and actual + * rom region data usage match when CONFIG_LINKER_LAST_SECTION_ID=y. */ + KEEP(*(.last_section)) } GROUP_LINK_IN(ROMABLE_REGION) /* To provide the image size as a const expression,