Skip to content

Commit 0fec26e

Browse files
committed
Add pico_check_linker_script function to check for compatibility of custom linker scripts
1 parent b8f5b4f commit 0fec26e

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

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 Pico SDK >2.1.1 - setting PICO_CRT0_ALLOCATE_SPACERS=0 to maintain compatibility")
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)