Skip to content

Commit 5a23cb8

Browse files
committed
Use picolibc instead of newlib-nano
1. Support picolibc stdio. There was already picolibc support code present in the library for the ARM LLVM toolchain. The conditionals which selected it have been changed to use __PICOLIBC__ instead of __clang_major__. 2. Add _exit stub. Code using assert or abort end up calling _exit through the picolibc signal handling code. 3. Switch to picolibc.specs. This is needed for toolchains which don't use picolibc by default, and can also be used without trouble in toolchains where picolibc is the default. Signed-off-by: Keith Packard <[email protected]>
1 parent a53b2eb commit 5a23cb8

File tree

6 files changed

+66
-9
lines changed

6 files changed

+66
-9
lines changed

platform/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ target_sources(platform_s
9090
target_sources(tfm_s
9191
PRIVATE
9292
ext/common/faults.c
93+
ext/common/picolibc.c
9394
)
9495

9596
target_link_libraries(platform_s

platform/ext/common/picolibc.c

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright © 2025, Keith Packard <[email protected]>
3+
*
4+
* SPDX-License-Identifier: BSD-3-Clause
5+
*
6+
*/
7+
8+
#include <stdint.h>
9+
#include <string.h>
10+
#include "config_impl.h"
11+
12+
/*
13+
* Picolibc's startup code only initializes a single data and bss
14+
* segment. Augment that by initializing the remaining segments using
15+
* the lists provided by the linker script in a constructor which will
16+
* be called from _start
17+
*/
18+
19+
#if defined(__PICOLIBC__) && TFM_ISOLATION_LEVEL > 0
20+
21+
static void __attribute__((constructor))
22+
picolibc_startup(void)
23+
{
24+
typedef struct __copy_table {
25+
uint32_t const* src;
26+
uint32_t* dest;
27+
uint32_t wlen;
28+
} __copy_table_t;
29+
30+
typedef struct __zero_table {
31+
uint32_t* dest;
32+
uint32_t wlen;
33+
} __zero_table_t;
34+
35+
extern const __copy_table_t __copy_table_start__;
36+
extern const __copy_table_t __copy_table_end__;
37+
extern const __zero_table_t __zero_table_start__;
38+
extern const __zero_table_t __zero_table_end__;
39+
40+
for (__copy_table_t const* pTable = &__copy_table_start__; pTable < &__copy_table_end__; ++pTable) {
41+
memcpy(pTable->dest, pTable->src, pTable->wlen << 2);
42+
}
43+
44+
for (__zero_table_t const* pTable = &__zero_table_start__; pTable < &__zero_table_end__; ++pTable) {
45+
memset(pTable->dest, 0, pTable->wlen << 2);
46+
}
47+
}
48+
#endif

platform/ext/common/syscalls_stub.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@
1414
#include <stddef.h>
1515
#include <stdint.h>
1616

17+
#ifdef __PICOLIBC__
18+
__attribute__((weak))
19+
void _exit(int status)
20+
{
21+
(void) status;
22+
for(;;);
23+
}
24+
#else
1725
__attribute__((weak))
1826
void _close(void)
1927
{
@@ -53,3 +61,5 @@ __attribute__((weak))
5361
void _write(void)
5462
{
5563
}
64+
65+
#endif /* !__PICOLIBC__ */

platform/ext/common/uart_stdout.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ int fputc(int ch, FILE *f)
8585
/* Redirect sdtio for PicoLib in LLVM toolchain
8686
as per https://github.com/picolibc/picolibc/blob/main/doc/os.md
8787
'fputch()' named intentionally different from 'fputc()' from picolib */
88-
#elif defined(__clang_major__)
88+
#elif defined(__PICOLIBC__)
8989

9090
int fputch(char ch, struct __file *f)
9191
{

platform/ns/toolchain_ns_GNUARM.cmake

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,7 @@ string(APPEND CMAKE_ASM_LINK_FLAGS " " ${LINKER_CP_OPTION})
177177
add_compile_definitions($<$<STREQUAL:${TFM_SYSTEM_ARCHITECTURE},armv8.1-m.main>:__ARM_ARCH_8_1M_MAIN__=1>)
178178

179179
add_compile_options(
180-
-specs=nano.specs
181-
-specs=nosys.specs
180+
-specs=picolibc.specs
182181
-Wall
183182
-Wno-format
184183
-Wno-array-parameter
@@ -200,8 +199,8 @@ add_compile_options(
200199

201200
add_link_options(
202201
--entry=Reset_Handler
203-
-specs=nano.specs
204-
-specs=nosys.specs
202+
-specs=picolibc.specs
203+
-mthumb
205204
LINKER:-check-sections
206205
LINKER:-fatal-warnings
207206
LINKER:--gc-sections

toolchain_GNUARM.cmake

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,7 @@ if(GCC_VERSION VERSION_GREATER_EQUAL "8.0.0")
107107
endif()
108108

109109
add_compile_options(
110-
-specs=nano.specs
111-
-specs=nosys.specs
110+
-specs=picolibc.specs
112111
-Wall
113112
-Wno-format
114113
-Wno-array-parameter
@@ -147,8 +146,8 @@ endif()
147146

148147
add_link_options(
149148
--entry=Reset_Handler
150-
-specs=nano.specs
151-
-specs=nosys.specs
149+
-specs=picolibc.specs
150+
-mthumb
152151
LINKER:-check-sections
153152
LINKER:-fatal-warnings
154153
LINKER:--gc-sections

0 commit comments

Comments
 (0)