Skip to content

Commit c79c4ef

Browse files
tejlmandkartben
authored andcommitted
linker: move last section id constant to c-code
Move creation of last section id from ld linker script LONG() usage to C code with last section attribute. The use of `LONG()` works correctly with ld but lld emits a warning because .last_section section is not allocated as there are no matching input sections and discards the `LONG()` call, meaning the last section identifier will not be present in the flash. > ld.lld: warning: ignoring memory region assignment for > non-allocatable section '.last_section' Placing the last section id in `.last_section` in C code makes lld allocate the memory for the id and thereby create the output section with the correct output. Signed-off-by: Torsten Rasmussen <Torsten.Rasmussen@nordicsemi.no>
1 parent 8f2560c commit c79c4ef

File tree

8 files changed

+30
-30
lines changed

8 files changed

+30
-30
lines changed

include/zephyr/arch/arm/cortex_a_r/scripts/linker.ld

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -366,11 +366,9 @@ GROUP_END(OCM)
366366
/* Must be last in romable region */
367367
SECTION_PROLOGUE(.last_section,,)
368368
{
369-
#ifdef CONFIG_LINKER_LAST_SECTION_ID
370-
/* Fill last section with a word to ensure location counter and actual rom
371-
* region data usage match. */
372-
LONG(CONFIG_LINKER_LAST_SECTION_ID_PATTERN)
373-
#endif
369+
/* .last_section contains a fixed word to ensure location counter and actual
370+
* rom region data usage match when CONFIG_LINKER_LAST_SECTION_ID=y. */
371+
KEEP(*(.last_section))
374372
} GROUP_LINK_IN(ROMABLE_REGION)
375373

376374
/* To provide the image size as a const expression,

include/zephyr/arch/arm/cortex_m/scripts/linker.ld

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -412,11 +412,9 @@ GROUP_END(DTCM)
412412
/* Must be last in romable region */
413413
SECTION_PROLOGUE(.last_section,,)
414414
{
415-
#ifdef CONFIG_LINKER_LAST_SECTION_ID
416-
/* Fill last section with a word to ensure location counter and actual rom
417-
* region data usage match. */
418-
LONG(CONFIG_LINKER_LAST_SECTION_ID_PATTERN)
419-
#endif
415+
/* .last_section contains a fixed word to ensure location counter and actual
416+
* rom region data usage match when CONFIG_LINKER_LAST_SECTION_ID=y. */
417+
KEEP(*(.last_section))
420418
} GROUP_LINK_IN(ROMABLE_REGION)
421419

422420
/* To provide the image size as a const expression,

include/zephyr/arch/arm64/scripts/linker.ld

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -351,11 +351,9 @@ SECTIONS
351351
/* Must be last in romable region */
352352
SECTION_PROLOGUE(.last_section,,)
353353
{
354-
#ifdef CONFIG_LINKER_LAST_SECTION_ID
355-
/* Fill last section with a word to ensure location counter and actual rom
356-
* region data usage match. */
357-
LONG(CONFIG_LINKER_LAST_SECTION_ID_PATTERN)
358-
#endif
354+
/* .last_section contains a fixed word to ensure location counter and actual
355+
* rom region data usage match when CONFIG_LINKER_LAST_SECTION_ID=y. */
356+
KEEP(*(.last_section))
359357
} GROUP_ROM_LINK_IN(RAMABLE_REGION, ROMABLE_REGION)
360358

361359
/* To provide the image size as a const expression,

include/zephyr/arch/riscv/common/linker.ld

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -454,15 +454,13 @@ GROUP_END(DTCM)
454454
/* Must be last in romable region */
455455
SECTION_PROLOGUE(.last_section,,)
456456
{
457-
#ifdef CONFIG_LINKER_LAST_SECTION_ID
458-
/* Fill last section with a word to ensure location counter and actual rom
459-
* region data usage match. */
460-
LONG(CONFIG_LINKER_LAST_SECTION_ID_PATTERN)
457+
/* .last_section contains a fixed word to ensure location counter and actual
458+
* rom region data usage match when CONFIG_LINKER_LAST_SECTION_ID=y. */
459+
KEEP(*(.last_section))
461460
/* __rom_region_size is used when configuring the PMP entry of the ROM region.
462461
* Addresses (pmpaddr) in PMP registers need to be aligned to 4. Align
463462
* __rom_region_size to 4 to meet that requirement. */
464463
MPU_MIN_SIZE_ALIGN
465-
#endif
466464
} GROUP_LINK_IN(ROMABLE_REGION)
467465

468466
/* To provide the image size as a const expression,

lib/utils/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,5 @@ zephyr_library_include_directories(
2828
${ZEPHYR_BASE}/kernel/include
2929
${ZEPHYR_BASE}/arch/${ARCH}/include
3030
)
31+
32+
zephyr_sources_ifdef(CONFIG_LINKER_LAST_SECTION_ID last_section_id.c)

lib/utils/last_section_id.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/*
2+
* Copyright (C) 2025, Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
*/
7+
#include <zephyr/types.h>
8+
9+
static uint32_t last_id __attribute__((section(".last_section"))) __attribute__((__used__)) =
10+
CONFIG_LINKER_LAST_SECTION_ID_PATTERN;

soc/andestech/ae350/linker.ld

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -421,11 +421,9 @@ GROUP_END(DTCM)
421421
/* Must be last in romable region */
422422
SECTION_PROLOGUE(.last_section,,)
423423
{
424-
#ifdef CONFIG_LINKER_LAST_SECTION_ID
425-
/* Fill last section with a word to ensure location counter and actual rom
426-
* region data usage match. */
427-
LONG(CONFIG_LINKER_LAST_SECTION_ID_PATTERN)
428-
#endif
424+
/* .last_section contains a fixed word to ensure location counter and actual
425+
* rom region data usage match when CONFIG_LINKER_LAST_SECTION_ID=y. */
426+
KEEP(*(.last_section))
429427
} GROUP_LINK_IN(ROMABLE_REGION)
430428

431429
/* Because ROMABLE_REGION != RAMABLE_REGION in XIP-system, it is valid

soc/infineon/cat1b/cyw20829/linker.ld

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -466,11 +466,9 @@ GROUP_END(DTCM)
466466
/* Must be last in romable region */
467467
SECTION_PROLOGUE(.last_section,,)
468468
{
469-
#ifdef CONFIG_LINKER_LAST_SECTION_ID
470-
/* Fill last section with a word to ensure location counter and actual rom
471-
* region data usage match. */
472-
LONG(CONFIG_LINKER_LAST_SECTION_ID_PATTERN)
473-
#endif
469+
/* .last_section contains a fixed word to ensure location counter and actual
470+
* rom region data usage match when CONFIG_LINKER_LAST_SECTION_ID=y. */
471+
KEEP(*(.last_section))
474472
} GROUP_LINK_IN(ROMABLE_REGION)
475473

476474
/* To provide the image size as a const expression,

0 commit comments

Comments
 (0)