From 1ae7e0b9c8cf75e6faa11bd97cc20142dba149d0 Mon Sep 17 00:00:00 2001 From: William Vinnicombe Date: Mon, 1 Dec 2025 20:40:45 +0000 Subject: [PATCH] Add customisable heap location, with pico_set_linker_script_var function --- .../pico_crt0/rp2040/sections_copy_to_ram.ld | 4 ++-- src/rp2_common/pico_crt0/rp2040/sections_default.ld | 4 ++-- src/rp2_common/pico_crt0/rp2040/sections_no_flash.ld | 4 ++-- .../pico_crt0/rp2350/sections_copy_to_ram.ld | 4 ++-- src/rp2_common/pico_crt0/rp2350/sections_default.ld | 4 ++-- src/rp2_common/pico_crt0/rp2350/sections_no_flash.ld | 4 ++-- src/rp2_common/pico_standard_link/CMakeLists.txt | 12 ++++++++++++ 7 files changed, 24 insertions(+), 12 deletions(-) diff --git a/src/rp2_common/pico_crt0/rp2040/sections_copy_to_ram.ld b/src/rp2_common/pico_crt0/rp2040/sections_copy_to_ram.ld index 5557ee1c..1777f660 100644 --- a/src/rp2_common/pico_crt0/rp2040/sections_copy_to_ram.ld +++ b/src/rp2_common/pico_crt0/rp2040/sections_copy_to_ram.ld @@ -199,7 +199,7 @@ SECTIONS __bss_end__ = .; } > RAM - .heap (NOLOAD): + .heap DEFINED(HEAP_LOC) ? HEAP_LOC : . (NOLOAD): { __end__ = .; end = __end__; @@ -207,7 +207,7 @@ SECTIONS } > RAM /* historically on GCC sbrk was growing past __HeapLimit to __StackLimit, however to be more compatible, we now set __HeapLimit explicitly to where the end of the heap is */ - __HeapLimit = ORIGIN(RAM) + LENGTH(RAM); + __HeapLimit = DEFINED(HEAP_LIMIT) ? HEAP_LIMIT : ORIGIN(RAM) + LENGTH(RAM); /* Start and end symbols must be word-aligned */ .scratch_x : { diff --git a/src/rp2_common/pico_crt0/rp2040/sections_default.ld b/src/rp2_common/pico_crt0/rp2040/sections_default.ld index 57984e46..2e72b83b 100644 --- a/src/rp2_common/pico_crt0/rp2040/sections_default.ld +++ b/src/rp2_common/pico_crt0/rp2040/sections_default.ld @@ -198,7 +198,7 @@ SECTIONS __bss_end__ = .; } > RAM - .heap (NOLOAD): + .heap DEFINED(HEAP_LOC) ? HEAP_LOC : . (NOLOAD): { __end__ = .; end = __end__; @@ -206,7 +206,7 @@ SECTIONS } > RAM /* historically on GCC sbrk was growing past __HeapLimit to __StackLimit, however to be more compatible, we now set __HeapLimit explicitly to where the end of the heap is */ - __HeapLimit = ORIGIN(RAM) + LENGTH(RAM); + __HeapLimit = DEFINED(HEAP_LIMIT) ? HEAP_LIMIT : ORIGIN(RAM) + LENGTH(RAM); /* Start and end symbols must be word-aligned */ .scratch_x : { diff --git a/src/rp2_common/pico_crt0/rp2040/sections_no_flash.ld b/src/rp2_common/pico_crt0/rp2040/sections_no_flash.ld index 9e082d53..c71ee3b0 100644 --- a/src/rp2_common/pico_crt0/rp2040/sections_no_flash.ld +++ b/src/rp2_common/pico_crt0/rp2040/sections_no_flash.ld @@ -167,7 +167,7 @@ SECTIONS __bss_end__ = .; } > RAM - .heap (NOLOAD): + .heap DEFINED(HEAP_LOC) ? HEAP_LOC : . (NOLOAD): { __end__ = .; end = __end__; @@ -175,7 +175,7 @@ SECTIONS } > RAM /* historically on GCC sbrk was growing past __HeapLimit to __StackLimit, however to be more compatible, we now set __HeapLimit explicitly to where the end of the heap is */ - __HeapLimit = ORIGIN(RAM) + LENGTH(RAM); + __HeapLimit = DEFINED(HEAP_LIMIT) ? HEAP_LIMIT : ORIGIN(RAM) + LENGTH(RAM); /* Start and end symbols must be word-aligned */ .scratch_x : { diff --git a/src/rp2_common/pico_crt0/rp2350/sections_copy_to_ram.ld b/src/rp2_common/pico_crt0/rp2350/sections_copy_to_ram.ld index 296eb46d..3c372f1a 100644 --- a/src/rp2_common/pico_crt0/rp2350/sections_copy_to_ram.ld +++ b/src/rp2_common/pico_crt0/rp2350/sections_copy_to_ram.ld @@ -226,7 +226,7 @@ SECTIONS } PROVIDE(__persistent_data_end__ = .); - .heap (NOLOAD): + .heap DEFINED(HEAP_LOC) ? HEAP_LOC : . (NOLOAD): { __end__ = .; end = __end__; @@ -234,7 +234,7 @@ SECTIONS } > RAM /* historically on GCC sbrk was growing past __HeapLimit to __StackLimit, however to be more compatible, we now set __HeapLimit explicitly to where the end of the heap is */ - __HeapLimit = ORIGIN(RAM) + LENGTH(RAM); + __HeapLimit = DEFINED(HEAP_LIMIT) ? HEAP_LIMIT : ORIGIN(RAM) + LENGTH(RAM); /* Start and end symbols must be word-aligned */ .scratch_x : { diff --git a/src/rp2_common/pico_crt0/rp2350/sections_default.ld b/src/rp2_common/pico_crt0/rp2350/sections_default.ld index 42cebd90..71379640 100644 --- a/src/rp2_common/pico_crt0/rp2350/sections_default.ld +++ b/src/rp2_common/pico_crt0/rp2350/sections_default.ld @@ -219,7 +219,7 @@ SECTIONS } PROVIDE(__persistent_data_end__ = .); - .heap (NOLOAD): + .heap DEFINED(HEAP_LOC) ? HEAP_LOC : . (NOLOAD): { __end__ = .; end = __end__; @@ -227,7 +227,7 @@ SECTIONS } > RAM /* historically on GCC sbrk was growing past __HeapLimit to __StackLimit, however to be more compatible, we now set __HeapLimit explicitly to where the end of the heap is */ - __HeapLimit = ORIGIN(RAM) + LENGTH(RAM); + __HeapLimit = DEFINED(HEAP_LIMIT) ? HEAP_LIMIT : ORIGIN(RAM) + LENGTH(RAM); /* Start and end symbols must be word-aligned */ .scratch_x : { diff --git a/src/rp2_common/pico_crt0/rp2350/sections_no_flash.ld b/src/rp2_common/pico_crt0/rp2350/sections_no_flash.ld index 5149282c..84340896 100644 --- a/src/rp2_common/pico_crt0/rp2350/sections_no_flash.ld +++ b/src/rp2_common/pico_crt0/rp2350/sections_no_flash.ld @@ -179,7 +179,7 @@ SECTIONS } PROVIDE(__persistent_data_end__ = .); - .heap (NOLOAD): + .heap DEFINED(HEAP_LOC) ? HEAP_LOC : . (NOLOAD): { __end__ = .; end = __end__; @@ -187,7 +187,7 @@ SECTIONS } > RAM /* historically on GCC sbrk was growing past __HeapLimit to __StackLimit, however to be more compatible, we now set __HeapLimit explicitly to where the end of the heap is */ - __HeapLimit = ORIGIN(RAM) + LENGTH(RAM); + __HeapLimit = DEFINED(HEAP_LIMIT) ? HEAP_LIMIT : ORIGIN(RAM) + LENGTH(RAM); /* Start and end symbols must be word-aligned */ .scratch_x : { diff --git a/src/rp2_common/pico_standard_link/CMakeLists.txt b/src/rp2_common/pico_standard_link/CMakeLists.txt index 61e93a8c..d5292a5c 100644 --- a/src/rp2_common/pico_standard_link/CMakeLists.txt +++ b/src/rp2_common/pico_standard_link/CMakeLists.txt @@ -105,6 +105,15 @@ if (NOT TARGET pico_standard_link) set_target_properties(${TARGET} PROPERTIES PICO_TARGET_LINKER_SCRIPT ${LDSCRIPT}) endfunction() + # pico_set_linker_script_var(TARGET NAME VALUE) + # \brief\ Set the linker script for the target + # + # \param\ NAME Name of varAible to set + # \param\ VALUE Value of variable to set + function(pico_set_linker_script_var TARGET NAME VALUE) + set_property(TARGET ${TARGET} APPEND PROPERTY PICO_TARGET_LINKER_SCRIPT_VARS "--defsym=${NAME}=${VALUE}") + endfunction() + # pico_set_binary_type(TARGET TYPE) # \brief\ Set the binary type for the target # @@ -165,6 +174,9 @@ if (NOT TARGET pico_standard_link) # add include path for main linker script sections target_link_options(pico_standard_link INTERFACE "LINKER:-L${PICO_LINKER_SCRIPT_PATH}") + # add variables set by pico_set_linker_script_var function + target_link_options(pico_standard_link INTERFACE "LINKER:$,,>") + # LINKER script will be PICO_TARGET_LINKER_SCRIPT if set on target, or ${CMAKE_CURRENT_LIST_DIR}/memmap_foo.ld # if PICO_TARGET_BINARY_TYPE is set to foo on the target, otherwise ${CMAKE_CURRENT_LIST_DIR}/memmap_${PICO_DEFAULT_BINARY_TYPE).ld set(_LINKER_SCRIPT_EXPRESSION "$>,$,${PICO_LINKER_SCRIPT_PATH}/memmap_$,>,${PICO_DEFAULT_BINARY_TYPE},$>.ld>")