Skip to content

Commit f5e0371

Browse files
Make spacer sections allocatable (#2515)
* Default to allocating spacer sections (stack & heap), with PICO_CRT0_ALLOCATE_SPACERS config option to disable the behaviour * Add pico_check_linker_script function to check for compatibility of custom linker scripts * tweak error message --------- Co-authored-by: Graham Sanderson <[email protected]>
1 parent 9682896 commit f5e0371

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

src/rp2_common/pico_crt0/crt0.S

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -582,10 +582,16 @@ runtime_init:
582582
//
583583
// Strictly the most correct thing to do (as .stack and .heap are unreferenced) is to mark them as "a", and also KEEP, which
584584
// works correctly for both GCC and Clang, however doing so may break anyone who already has custom linker scripts without
585-
// the KEEP. Therefore we will only add the "a" on Clang, but will also use KEEP to our own linker scripts.
585+
// the KEEP. Therefore we add a define of PICO_CRT0_ALLOCATE_SPACERS to switch between the old and new behaviour, so anyone
586+
// with custom linker scripts without the KEEP can set it to 0 (or update their linker script).
587+
588+
// 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
589+
#ifndef PICO_CRT0_ALLOCATE_SPACERS
590+
#define PICO_CRT0_ALLOCATE_SPACERS 1
591+
#endif
586592

587593
.macro spacer_section name
588-
#if PICO_ASSEMBLER_IS_CLANG
594+
#if PICO_CRT0_ALLOCATE_SPACERS
589595
.section \name, "a"
590596
#else
591597
.section \name

src/rp2_common/pico_standard_link/CMakeLists.txt

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,48 @@ if (NOT TARGET pico_standard_link)
2525
set_target_properties(${TARGET} PROPERTIES ${PROP} "${_LINK_DEPENDS}")
2626
endfunction()
2727

28+
# pico_check_linker_script(LDSCRIPT)
29+
# \brief_nodesc\ Check the linker script for compatibility
30+
#
31+
# Checks the linker script for compatibility with the current SDK version,
32+
# and if not, raises warnings and enables workarounds to maintain
33+
# compatibility where possible.
34+
#
35+
# \param\ LDSCRIPT Full path to the linker script to check
36+
function(pico_check_linker_script TARGET LDSCRIPT)
37+
if (EXISTS ${LDSCRIPT})
38+
file(READ ${LDSCRIPT} LDSCRIPT_CONTENTS)
39+
else()
40+
return()
41+
endif()
42+
43+
# Check if the linker script uses KEEP to keep the .stack and .heap sections
44+
# and if not, set PICO_CRT0_ALLOCATE_SPACERS to 0 to maintain compatibility
45+
string(FIND "${LDSCRIPT_CONTENTS}" "KEEP(*(.stack*))" KEEP_STACK_FOUND)
46+
string(FIND "${LDSCRIPT_CONTENTS}" "KEEP(*(.heap*))" KEEP_HEAP_FOUND)
47+
string(FIND "${LDSCRIPT_CONTENTS}" "*(.stack*)" STACK_FOUND)
48+
string(FIND "${LDSCRIPT_CONTENTS}" "*(.heap*)" HEAP_FOUND)
49+
set(PICO_CRT0_ALLOCATE_SPACERS TRUE)
50+
if ((${STACK_FOUND} GREATER -1) AND NOT (${KEEP_STACK_FOUND} GREATER -1))
51+
message(WARNING "Linker script ${LDSCRIPT} does not KEEP the .stack section - replace `*(.stack*)` with `KEEP(*(.stack*))`")
52+
set(PICO_CRT0_ALLOCATE_SPACERS FALSE)
53+
endif()
54+
if ((${HEAP_FOUND} GREATER -1) AND NOT (${KEEP_HEAP_FOUND} GREATER -1))
55+
message(WARNING "Linker script ${LDSCRIPT} does not KEEP the .heap section - replace `*(.heap*)` with `KEEP(*(.heap*))`")
56+
set(PICO_CRT0_ALLOCATE_SPACERS FALSE)
57+
endif()
58+
if (NOT ${PICO_CRT0_ALLOCATE_SPACERS})
59+
message(WARNING "Linker script ${LDSCRIPT} is incompatible with certain Pico SDK >2.1.1 features; setting PICO_CRT0_ALLOCATE_SPACERS=0 as a workaround")
60+
target_compile_definitions(${TARGET} PRIVATE PICO_CRT0_ALLOCATE_SPACERS=0)
61+
endif()
62+
endfunction()
63+
2864
# pico_set_linker_script(TARGET LDSCRIPT)
2965
# \brief\ Set the linker script for the target
3066
#
3167
# \param\ LDSCRIPT Full path to the linker script to set
3268
function(pico_set_linker_script TARGET LDSCRIPT)
69+
pico_check_linker_script(${TARGET} ${LDSCRIPT})
3370
set_target_properties(${TARGET} PROPERTIES PICO_TARGET_LINKER_SCRIPT ${LDSCRIPT})
3471
endfunction()
3572

0 commit comments

Comments
 (0)