From b8f5b4f5585f3527595d78707e3c363170d1c061 Mon Sep 17 00:00:00 2001 From: William Vinnicombe Date: Wed, 11 Jun 2025 10:30:20 +0100 Subject: [PATCH 1/3] Default to allocating spacer sections (stack & heap), with PICO_CRT0_ALLOCATE_SPACERS config option to disable the behaviour --- src/rp2_common/pico_crt0/crt0.S | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/rp2_common/pico_crt0/crt0.S b/src/rp2_common/pico_crt0/crt0.S index 43ce5ed34..ea3b99a5a 100644 --- a/src/rp2_common/pico_crt0/crt0.S +++ b/src/rp2_common/pico_crt0/crt0.S @@ -582,10 +582,16 @@ runtime_init: // // Strictly the most correct thing to do (as .stack and .heap are unreferenced) is to mark them as "a", and also KEEP, which // works correctly for both GCC and Clang, however doing so may break anyone who already has custom linker scripts without -// the KEEP. Therefore we will only add the "a" on Clang, but will also use KEEP to our own linker scripts. +// the KEEP. Therefore we add a define of PICO_CRT0_ALLOCATE_SPACERS to switch between the old and new behaviour, so anyone +// with custom linker scripts without the KEEP can set it to 0 (or update their linker script). + +// PICO_CONFIG: PICO_CRT0_ALLOCATE_SPACERS, Set spacer sections as allocatable. This makes them appear in print-memory-usage but is incompatible with linker scripts that do not KEEP the sections, type=bool, default=1, advanced=true, group=pico_crt0 +#ifndef PICO_CRT0_ALLOCATE_SPACERS +#define PICO_CRT0_ALLOCATE_SPACERS 1 +#endif .macro spacer_section name -#if PICO_ASSEMBLER_IS_CLANG +#if PICO_CRT0_ALLOCATE_SPACERS .section \name, "a" #else .section \name From 0fec26e67e2e03e8de275a28c9c9e9af9d9c299c Mon Sep 17 00:00:00 2001 From: William Vinnicombe Date: Wed, 11 Jun 2025 11:20:45 +0100 Subject: [PATCH 2/3] Add pico_check_linker_script function to check for compatibility of custom linker scripts --- .../pico_standard_link/CMakeLists.txt | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/src/rp2_common/pico_standard_link/CMakeLists.txt b/src/rp2_common/pico_standard_link/CMakeLists.txt index 484abc892..c755c0967 100644 --- a/src/rp2_common/pico_standard_link/CMakeLists.txt +++ b/src/rp2_common/pico_standard_link/CMakeLists.txt @@ -25,11 +25,48 @@ if (NOT TARGET pico_standard_link) set_target_properties(${TARGET} PROPERTIES ${PROP} "${_LINK_DEPENDS}") endfunction() + # pico_check_linker_script(LDSCRIPT) + # \brief_nodesc\ Check the linker script for compatibility + # + # Checks the linker script for compatibility with the current SDK version, + # and if not, raises warnings and enables workarounds to maintain + # compatibility where possible. + # + # \param\ LDSCRIPT Full path to the linker script to check + function(pico_check_linker_script TARGET LDSCRIPT) + if (EXISTS ${LDSCRIPT}) + file(READ ${LDSCRIPT} LDSCRIPT_CONTENTS) + else() + return() + endif() + + # Check if the linker script uses KEEP to keep the .stack and .heap sections + # and if not, set PICO_CRT0_ALLOCATE_SPACERS to 0 to maintain compatibility + string(FIND "${LDSCRIPT_CONTENTS}" "KEEP(*(.stack*))" KEEP_STACK_FOUND) + string(FIND "${LDSCRIPT_CONTENTS}" "KEEP(*(.heap*))" KEEP_HEAP_FOUND) + string(FIND "${LDSCRIPT_CONTENTS}" "*(.stack*)" STACK_FOUND) + string(FIND "${LDSCRIPT_CONTENTS}" "*(.heap*)" HEAP_FOUND) + set(PICO_CRT0_ALLOCATE_SPACERS TRUE) + if ((${STACK_FOUND} GREATER -1) AND NOT (${KEEP_STACK_FOUND} GREATER -1)) + message(WARNING "Linker script ${LDSCRIPT} does not KEEP the .stack section - replace `*(.stack*)` with `KEEP(*(.stack*))`") + set(PICO_CRT0_ALLOCATE_SPACERS FALSE) + endif() + if ((${HEAP_FOUND} GREATER -1) AND NOT (${KEEP_HEAP_FOUND} GREATER -1)) + message(WARNING "Linker script ${LDSCRIPT} does not KEEP the .heap section - replace `*(.heap*)` with `KEEP(*(.heap*))`") + set(PICO_CRT0_ALLOCATE_SPACERS FALSE) + endif() + if (NOT ${PICO_CRT0_ALLOCATE_SPACERS}) + message(WARNING "Linker script ${LDSCRIPT} is incompatible with Pico SDK >2.1.1 - setting PICO_CRT0_ALLOCATE_SPACERS=0 to maintain compatibility") + target_compile_definitions(${TARGET} PRIVATE PICO_CRT0_ALLOCATE_SPACERS=0) + endif() + endfunction() + # pico_set_linker_script(TARGET LDSCRIPT) # \brief\ Set the linker script for the target # # \param\ LDSCRIPT Full path to the linker script to set function(pico_set_linker_script TARGET LDSCRIPT) + pico_check_linker_script(${TARGET} ${LDSCRIPT}) set_target_properties(${TARGET} PROPERTIES PICO_TARGET_LINKER_SCRIPT ${LDSCRIPT}) endfunction() From eb1752dd4c0c7840958486965e9bdaa3d6884841 Mon Sep 17 00:00:00 2001 From: Graham Sanderson Date: Wed, 16 Jul 2025 17:15:14 -0500 Subject: [PATCH 3/3] tweak error message --- src/rp2_common/pico_standard_link/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/rp2_common/pico_standard_link/CMakeLists.txt b/src/rp2_common/pico_standard_link/CMakeLists.txt index c755c0967..c16968bba 100644 --- a/src/rp2_common/pico_standard_link/CMakeLists.txt +++ b/src/rp2_common/pico_standard_link/CMakeLists.txt @@ -56,7 +56,7 @@ if (NOT TARGET pico_standard_link) set(PICO_CRT0_ALLOCATE_SPACERS FALSE) endif() if (NOT ${PICO_CRT0_ALLOCATE_SPACERS}) - message(WARNING "Linker script ${LDSCRIPT} is incompatible with Pico SDK >2.1.1 - setting PICO_CRT0_ALLOCATE_SPACERS=0 to maintain compatibility") + message(WARNING "Linker script ${LDSCRIPT} is incompatible with certain Pico SDK >2.1.1 features; setting PICO_CRT0_ALLOCATE_SPACERS=0 as a workaround") target_compile_definitions(${TARGET} PRIVATE PICO_CRT0_ALLOCATE_SPACERS=0) endif() endfunction()