Skip to content

Commit 3e0ce76

Browse files
tejlmandcarlescufi
authored andcommitted
[nrf fromlist] 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. Upstream PR #: 88970 Signed-off-by: Torsten Rasmussen <[email protected]>
1 parent 11c542a commit 3e0ce76

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

+3-5
Original file line numberDiff line numberDiff line change
@@ -402,11 +402,9 @@ GROUP_END(OCM)
402402
/* Must be last in romable region */
403403
SECTION_PROLOGUE(.last_section,,)
404404
{
405-
#ifdef CONFIG_LINKER_LAST_SECTION_ID
406-
/* Fill last section with a word to ensure location counter and actual rom
407-
* region data usage match. */
408-
LONG(CONFIG_LINKER_LAST_SECTION_ID_PATTERN)
409-
#endif
405+
/* .last_section contains a fixed word to ensure location counter and actual
406+
* rom region data usage match when CONFIG_LINKER_LAST_SECTION_ID=y. */
407+
KEEP(*(.last_section))
410408
} GROUP_LINK_IN(ROMABLE_REGION)
411409

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

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

+3-5
Original file line numberDiff line numberDiff line change
@@ -488,11 +488,9 @@ GROUP_END(DTCM)
488488
/* Must be last in romable region */
489489
SECTION_PROLOGUE(.last_section,,)
490490
{
491-
#ifdef CONFIG_LINKER_LAST_SECTION_ID
492-
/* Fill last section with a word to ensure location counter and actual rom
493-
* region data usage match. */
494-
LONG(CONFIG_LINKER_LAST_SECTION_ID_PATTERN)
495-
#endif
491+
/* .last_section contains a fixed word to ensure location counter and actual
492+
* rom region data usage match when CONFIG_LINKER_LAST_SECTION_ID=y. */
493+
KEEP(*(.last_section))
496494
} GROUP_LINK_IN(ROMABLE_REGION)
497495

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

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

+3-5
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

+3-5
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

+2
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,5 @@ zephyr_library_include_directories(
2525
${ZEPHYR_BASE}/kernel/include
2626
${ZEPHYR_BASE}/arch/${ARCH}/include
2727
)
28+
29+
zephyr_sources_ifdef(CONFIG_LINKER_LAST_SECTION_ID last_section_id.c)

lib/utils/last_section_id.c

+10
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

+3-5
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
/* Because ROMABLE_REGION != RAMABLE_REGION in XIP-system, it is valid

soc/infineon/cat1b/cyw20829/linker.ld

+3-5
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)