boards/arm/stm32: Fix CMake build for protected mode on stm32f4discovery

- Added support for multiple linker scripts preprocessing in CMake
- Changed kernel/CMakeLists.txt to use target_sources for nuttx_user
- Added KEEP() directive in user-space.ld for .userspace section
- Removed hardcoded -funwind-tables to fix binary size bloat
- Updated memory regions: ksram/usram 4KB→16KB, xsram 104KB→80KB
- Enabled CONFIG_ARCH_FPU, CONFIG_STM32_CCMEXCLUDE, adjusted heap size

Fixes protected mode CMake build for stm32f4discovery:kostest configuration.

Signed-off-by: Bartosz <bartol2205@gmail.com>
This commit is contained in:
Bartosz Wawrzynek
2026-01-30 09:30:08 +01:00
committed by Donny(董九柱)
parent a7567677a8
commit fe184f79f3
7 changed files with 116 additions and 44 deletions

View File

@@ -628,18 +628,22 @@ process_all_directory_romfs()
# Get linker script to use
get_property(ldscript GLOBAL PROPERTY LD_SCRIPT)
# Pre-compile linker script
# Pre-compile linker script(s)
if(NOT CONFIG_ARCH_SIM)
get_filename_component(LD_SCRIPT_NAME ${ldscript} NAME)
set(LD_SCRIPT_TMP "${CMAKE_BINARY_DIR}/${LD_SCRIPT_NAME}.tmp")
set(ldscript_tmp_list)
foreach(ld ${ldscript})
get_filename_component(LD_SCRIPT_NAME ${ld} NAME)
set(LD_SCRIPT_TMP "${CMAKE_BINARY_DIR}/${LD_SCRIPT_NAME}.tmp")
nuttx_generate_preprocess_target(SOURCE_FILE ${ldscript} TARGET_FILE
${LD_SCRIPT_TMP})
nuttx_generate_preprocess_target(SOURCE_FILE ${ld} TARGET_FILE
${LD_SCRIPT_TMP})
add_custom_target(ldscript_tmp DEPENDS ${LD_SCRIPT_TMP})
add_dependencies(nuttx ldscript_tmp)
add_custom_target(ldscript_tmp_${LD_SCRIPT_NAME} DEPENDS ${LD_SCRIPT_TMP})
add_dependencies(nuttx ldscript_tmp_${LD_SCRIPT_NAME})
set(ldscript ${LD_SCRIPT_TMP})
list(APPEND ldscript_tmp_list -T ${LD_SCRIPT_TMP})
endforeach()
set(ldscript ${ldscript_tmp_list})
endif()
# Perform link
@@ -679,7 +683,6 @@ if(NOT CONFIG_ARCH_SIM)
target_link_libraries(
nuttx
PRIVATE ${NUTTX_EXTRA_FLAGS}
-T
${ldscript}
$<$<NOT:$<BOOL:${DISABLE_LINK_GROUP}>>:-Wl,--start-group>
${nuttx_libs}
@@ -811,14 +814,19 @@ if(CONFIG_BUILD_PROTECTED)
get_property(user_ldscript GLOBAL PROPERTY LD_SCRIPT_USER)
# Pre-compile linker script
get_filename_component(LD_SCRIPT_USER_NAME ${user_ldscript} NAME)
set(LD_SCRIPT_USER_TMP "${CMAKE_BINARY_DIR}/${LD_SCRIPT_USER_NAME}.tmp")
nuttx_generate_preprocess_target(SOURCE_FILE ${user_ldscript} TARGET_FILE
${LD_SCRIPT_USER_TMP})
add_custom_target(user_ldscript_tmp DEPENDS ${LD_SCRIPT_USER_TMP})
add_dependencies(nuttx_user user_ldscript_tmp)
set(user_ldscript ${LD_SCRIPT_USER_TMP})
# Pre-compile linker script(s)
set(user_ldscript_tmp_list)
foreach(ldscript ${user_ldscript})
get_filename_component(LD_SCRIPT_USER_NAME ${ldscript} NAME)
set(LD_SCRIPT_USER_TMP "${CMAKE_BINARY_DIR}/${LD_SCRIPT_USER_NAME}.tmp")
nuttx_generate_preprocess_target(SOURCE_FILE ${ldscript} TARGET_FILE
${LD_SCRIPT_USER_TMP})
add_custom_target(user_ldscript_tmp_${LD_SCRIPT_USER_NAME}
DEPENDS ${LD_SCRIPT_USER_TMP})
add_dependencies(nuttx_user user_ldscript_tmp_${LD_SCRIPT_USER_NAME})
list(APPEND user_ldscript_tmp_list -T ${LD_SCRIPT_USER_TMP})
endforeach()
set(user_ldscript ${user_ldscript_tmp_list})
# reset link options that don't fit userspace
get_target_property(nuttx_user_LINK_OPTIONS nuttx_user LINK_OPTIONS)
@@ -848,8 +856,7 @@ if(CONFIG_BUILD_PROTECTED)
target_link_libraries(
nuttx_user
PRIVATE -T
${user_ldscript}
PRIVATE ${user_ldscript}
$<$<NOT:$<BOOL:${DISABLE_LINK_GROUP}>>:-Wl,--start-group>
${nuttx_system_libs}
${nuttx_apps_libs}

View File

@@ -0,0 +1,42 @@
############################################################################
# boards/arm/stm32/stm32f4discovery/configs/kostest/Make.defs
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership. The
# ASF licenses this file to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance with the
# License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
#
############################################################################
include $(TOPDIR)/.config
include $(TOPDIR)/tools/Config.mk
include $(TOPDIR)/arch/arm/src/armv7-m/Toolchain.defs
LDSCRIPT1 = memory.ld
LDSCRIPT2 = kernel-space.ld
ARCHSCRIPT += $(BOARD_DIR)$(DELIM)scripts$(DELIM)$(LDSCRIPT1)
ARCHSCRIPT += $(BOARD_DIR)$(DELIM)scripts$(DELIM)$(LDSCRIPT2)
ARCHPICFLAGS = -fpic -msingle-pic-base -mpic-register=r10
CFLAGS := $(ARCHCFLAGS) $(ARCHOPTIMIZATION) $(ARCHCPUFLAGS) $(ARCHINCLUDES) $(ARCHDEFINES) $(EXTRAFLAGS)
CPICFLAGS = $(ARCHPICFLAGS) $(CFLAGS)
CXXFLAGS := $(ARCHCXXFLAGS) $(ARCHOPTIMIZATION) $(ARCHCPUFLAGS) $(ARCHXXINCLUDES) $(ARCHDEFINES) $(EXTRAFLAGS)
CXXPICFLAGS = $(ARCHPICFLAGS) $(CXXFLAGS)
CPPFLAGS := $(ARCHINCLUDES) $(ARCHDEFINES) $(EXTRAFLAGS)
AFLAGS := $(CFLAGS) -D__ASSEMBLY__
NXFLATLDFLAGS1 = -r -d -warn-common
NXFLATLDFLAGS2 = $(NXFLATLDFLAGS1) -T$(TOPDIR)/binfmt/libnxflat/gnu-nxflat-pcrel.ld -no-check-sections
LDNXFLATFLAGS = -e main -s 2048

View File

@@ -5,7 +5,6 @@
# You can then do "make savedefconfig" to generate a new defconfig file that includes your
# modifications.
#
# CONFIG_ARCH_FPU is not set
CONFIG_ARCH="arm"
CONFIG_ARCH_BOARD="stm32f4discovery"
CONFIG_ARCH_BOARD_STM32F4_DISCOVERY=y
@@ -23,8 +22,10 @@ CONFIG_DEBUG_HARDFAULT_ALERT=y
CONFIG_DEBUG_SYMBOLS=y
CONFIG_DISABLE_ENVIRON=y
CONFIG_DISABLE_MOUNTPOINT=y
CONFIG_IDLETHREAD_STACKSIZE=2048
CONFIG_INIT_ENTRYPOINT="ostest_main"
CONFIG_INTELHEX_BINARY=y
CONFIG_MM_KERNEL_HEAPSIZE=16384
CONFIG_MM_REGIONS=2
CONFIG_NUTTX_USERSPACE=0x08020000
CONFIG_PASS1_BUILDIR="boards/arm/stm32/stm32f4discovery/kernel"
@@ -38,6 +39,7 @@ CONFIG_STACK_COLORATION=y
CONFIG_START_DAY=22
CONFIG_START_MONTH=3
CONFIG_START_YEAR=2013
CONFIG_STM32_CCMEXCLUDE=y
CONFIG_STM32_JTAG_SW_ENABLE=y
CONFIG_STM32_USART2=y
CONFIG_SYMTAB_ORDEREDBYNAME=y

View File

@@ -20,4 +20,4 @@
#
# ##############################################################################
nuttx_add_aux_library(userspace stm32_userspace.c)
target_sources(nuttx_user PRIVATE stm32_userspace.c)

View File

@@ -57,18 +57,19 @@
*
* A detailed memory map for the 112KB SRAM region is as follows:
*
* 0x20000 0000: Kernel .data region. Typical size: 0.1KB
* ------- ---- Kernel .bss region. Typical size: 1.8KB
* 0x20000 0800: Kernel IDLE thread stack (approximate). Size is
* determined by CONFIG_IDLETHREAD_STACKSIZE and
* adjustments for alignment. Typical is 1KB.
* ------- ---- Padded to 4KB
* 0x20000 1000: User .data region. Size is variable.
* ------- ---- User .bss region Size is variable.
* 0x20000 2000: Beginning of kernel heap. Size determined by
* CONFIG_MM_KERNEL_HEAPSIZE.
* ------- ---- Beginning of user heap. Can vary with other settings.
* 0x20001 c000: End+1 of CPU RAM
* 0x2000 0000: Kernel .data region. Typical size: 0.1KB
* ------ ---- Kernel .bss region. Typical size: 1.8KB
* 0x2000 0800: Kernel IDLE thread stack (approximate). Size is
* determined by CONFIG_IDLETHREAD_STACKSIZE and
* adjustments for alignment. Typical is 1KB.
* ------ ---- Padded to 8KB
* 0x2000 2000: User .data region. Size is variable.
* ------ ---- User .bss region Size is variable.
* 0x2000 4000: Beginning of kernel heap. Size determined by
* CONFIG_MM_KERNEL_HEAPSIZE which must be set to 16Kb.
* 0x2000 8000: Beginning of 32Kb user heap.
* 0x2001 0000: The remainder of SRAM is, unfortunately, wasted.
* 0x2001 c000: End+1 of CPU RAM
*/
MEMORY
@@ -81,7 +82,7 @@ MEMORY
/* 112Kb of contiguous SRAM */
ksram (rwx) : ORIGIN = 0x20000000, LENGTH = 4K
usram (rwx) : ORIGIN = 0x20001000, LENGTH = 4K
xsram (rwx) : ORIGIN = 0x20002000, LENGTH = 104K
ksram (rwx) : ORIGIN = 0x20000000, LENGTH = 16K
usram (rwx) : ORIGIN = 0x20004000, LENGTH = 16K
xsram (rwx) : ORIGIN = 0x20008000, LENGTH = 80K
}

View File

@@ -24,11 +24,24 @@
* this script.
*/
/* Make sure that the critical memory management functions are in user-space.
* the user heap memory manager will reside in user-space but be usable both
* by kernel- and user-space code
*/
EXTERN(umm_initialize)
EXTERN(umm_addregion)
EXTERN(malloc)
EXTERN(realloc)
EXTERN(zalloc)
EXTERN(free)
OUTPUT_ARCH(arm)
SECTIONS
{
.userspace : {
*(.userspace)
KEEP(*(.userspace))
} > uflash
.text : {

View File

@@ -177,8 +177,13 @@ endif()
target_sources(board PRIVATE ${SRCS})
# TODO: make this the default and then allow boards to redefine
set_property(GLOBAL PROPERTY LD_SCRIPT "${NUTTX_BOARD_DIR}/scripts/ld.script")
# Set linker script based on build type
if(CONFIG_BUILD_PROTECTED)
set_property(GLOBAL PROPERTY LD_SCRIPT "${NUTTX_BOARD_DIR}/scripts/memory.ld"
"${NUTTX_BOARD_DIR}/scripts/kernel-space.ld")
else()
set_property(GLOBAL PROPERTY LD_SCRIPT "${NUTTX_BOARD_DIR}/scripts/ld.script")
endif()
# TODO:move this to appropriate arch/toolchain level
set_property(
@@ -195,11 +200,13 @@ set_property(
# -funwind-tables ARCHCXXFLAGS += -fno-rtti -funwind-tables ifneq
# ($(CONFIG_DEBUG_NOOPT),y) ARCHOPTIMIZATION += -fno-strength-reduce endif endif
set_property(
TARGET nuttx
APPEND
PROPERTY NUTTX_COMPILE_OPTIONS -funwind-tables)
set_property(GLOBAL APPEND PROPERTY COMPILE_OPTIONS -fno-strength-reduce)
if(CONFIG_UNWINDER_ARM)
set_property(
TARGET nuttx
APPEND
PROPERTY NUTTX_COMPILE_OPTIONS -funwind-tables)
set_property(GLOBAL APPEND PROPERTY COMPILE_OPTIONS -fno-strength-reduce)
endif()
# TODO: nxflat NXFLATLDFLAGS1 = -r -d -warn-common NXFLATLDFLAGS2 =
# $(NXFLATLDFLAGS1) -T$(TOPDIR)/binfmt/libnxflat/gnu-nxflat-pcrel.ld