Skip to content

Make spacer sections allocatable #2515

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/rp2_common/pico_crt0/crt0.S
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
37 changes: 37 additions & 0 deletions src/rp2_common/pico_standard_link/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
Loading