Skip to content

Commit 033397b

Browse files
committed
fix(esp_tee): Add standard newlib function stubs to resolve build warnings
- Disable C++ exceptions for TEE build to reduce flash footprint
1 parent 3bb3f93 commit 033397b

File tree

5 files changed

+178
-39
lines changed

5 files changed

+178
-39
lines changed

components/cxx/CMakeLists.txt

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,23 @@
11
idf_build_get_property(target IDF_TARGET)
2+
idf_build_get_property(esp_tee_build ESP_TEE_BUILD)
23

34
if(${target} STREQUAL "linux")
45
return() # This component is not necessary on the POSIX/Linux simulator
56
endif()
67

7-
idf_component_register(SRCS "cxx_exception_stubs.cpp"
8-
"cxx_guards.cpp"
9-
"cxx_init.cpp"
10-
# Make sure that pthread is in component list
11-
PRIV_REQUIRES pthread esp_system)
8+
set(srcs "cxx_exception_stubs.cpp")
9+
set(priv_requires esp_system)
1210

13-
if(NOT CONFIG_CXX_EXCEPTIONS)
11+
if(NOT esp_tee_build)
12+
list(APPEND srcs "cxx_guards.cpp" "cxx_init.cpp")
13+
# Make sure that pthread is in component list
14+
list(APPEND priv_requires pthread)
15+
endif()
16+
17+
idf_component_register(SRCS ${srcs}
18+
PRIV_REQUIRES ${priv_requires})
19+
20+
if(esp_tee_build OR NOT CONFIG_CXX_EXCEPTIONS)
1421
set(WRAP_FUNCTIONS
1522
__register_frame_info_bases
1623
__register_frame_info
@@ -55,25 +62,31 @@ if(CMAKE_C_COMPILER_ID MATCHES "Clang")
5562
else()
5663
target_link_libraries(${COMPONENT_LIB} PUBLIC stdc++ gcc)
5764
endif()
58-
target_link_libraries(${COMPONENT_LIB} INTERFACE "-u __cxa_guard_dummy")
59-
target_link_libraries(${COMPONENT_LIB} INTERFACE "-u __cxx_init_dummy")
6065

61-
# Force libpthread to appear later than libstdc++ in link line since libstdc++ depends on libpthread.
62-
# Furthermore, force libcxx to appear later than libgcc because some libgcc unwind code is wrapped, if C++
63-
# exceptions are disabled. libcxx (this component) provides the unwind code wrappers.
64-
# This is to prevent linking of libgcc's unwind code which considerably increases the binary size.
65-
# Also force libnewlib to appear later than libstdc++ in link line since libstdc++ depends on
66-
# some functions in libnewlib, e.g. getentropy().
67-
idf_component_get_property(pthread pthread COMPONENT_LIB)
68-
idf_component_get_property(newlib newlib COMPONENT_LIB)
69-
idf_component_get_property(cxx cxx COMPONENT_LIB)
7066
add_library(stdcpp_deps INTERFACE)
71-
if(CMAKE_C_COMPILER_ID MATCHES "Clang")
72-
target_link_libraries(stdcpp_deps INTERFACE stdc++ c $<TARGET_FILE:${pthread}> $<TARGET_FILE:${newlib}>)
73-
else()
74-
target_link_libraries(stdcpp_deps INTERFACE stdc++ $<TARGET_FILE:${pthread}> $<TARGET_FILE:${newlib}>)
75-
endif()
7667
target_link_libraries(${COMPONENT_LIB} PUBLIC stdcpp_deps)
68+
69+
if(NOT esp_tee_build)
70+
target_link_libraries(${COMPONENT_LIB} INTERFACE "-u __cxa_guard_dummy")
71+
target_link_libraries(${COMPONENT_LIB} INTERFACE "-u __cxx_init_dummy")
72+
73+
# Force libpthread to appear later than libstdc++ in link line since libstdc++ depends on libpthread.
74+
# Furthermore, force libcxx to appear later than libgcc because some libgcc unwind code is wrapped, if C++
75+
# exceptions are disabled. libcxx (this component) provides the unwind code wrappers.
76+
# This is to prevent linking of libgcc's unwind code which considerably increases the binary size.
77+
# Also force libnewlib to appear later than libstdc++ in link line since libstdc++ depends on
78+
# some functions in libnewlib, e.g. getentropy().
79+
idf_component_get_property(pthread pthread COMPONENT_LIB)
80+
idf_component_get_property(newlib newlib COMPONENT_LIB)
81+
82+
if(CMAKE_C_COMPILER_ID MATCHES "Clang")
83+
target_link_libraries(stdcpp_deps INTERFACE stdc++ c $<TARGET_FILE:${pthread}> $<TARGET_FILE:${newlib}>)
84+
else()
85+
target_link_libraries(stdcpp_deps INTERFACE stdc++ $<TARGET_FILE:${pthread}> $<TARGET_FILE:${newlib}>)
86+
endif()
87+
endif()
88+
89+
idf_component_get_property(cxx cxx COMPONENT_LIB)
7790
add_library(libgcc_cxx INTERFACE)
7891
target_link_libraries(libgcc_cxx INTERFACE ${CONFIG_COMPILER_RT_LIB_NAME} $<TARGET_FILE:${cxx}>)
7992
target_link_libraries(${COMPONENT_LIB} PUBLIC libgcc_cxx)

components/esp_tee/subproject/main/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ list(APPEND include "${heap_dir}/tlsf")
4646
# esp_app_desc_t configuration structure for TEE
4747
list(APPEND srcs "common/esp_app_desc_tee.c")
4848

49+
# Newlib syscalls stub implementation
50+
list(APPEND srcs "common/syscall_stubs.c")
51+
4952
idf_component_register(SRCS ${srcs}
5053
INCLUDE_DIRS ${include})
5154

@@ -57,6 +60,9 @@ include(${CMAKE_CURRENT_LIST_DIR}/ld/esp_tee_ld.cmake)
5760
# esp_app_desc_t configuration structure for TEE: Linking symbol and trimming project version and name
5861
target_link_libraries(${COMPONENT_LIB} PRIVATE "-u esp_app_desc_tee_include_impl")
5962

63+
# Newlib syscalls stub implementation: Linking symbol
64+
target_link_libraries(${COMPONENT_LIB} PRIVATE "-u esp_tee_include_syscalls_impl")
65+
6066
# cut PROJECT_VER and PROJECT_NAME to required 32 characters.
6167
idf_build_get_property(project_ver PROJECT_VER)
6268
idf_build_get_property(project_name PROJECT_NAME)
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <stdint.h>
8+
#include <stdio.h>
9+
#include <stdlib.h>
10+
#include <stdbool.h>
11+
#include <sys/stat.h>
12+
#include <sys/types.h>
13+
#include <errno.h>
14+
#include <pthread.h>
15+
#include <unistd.h>
16+
17+
#include "esp_random.h"
18+
19+
// NOTE: Remove compile-time warnings for the below newlib-provided functions
20+
struct _reent *__getreent(void)
21+
{
22+
return _GLOBAL_REENT;
23+
}
24+
25+
int _fstat_r(struct _reent *r, int fd, struct stat *st)
26+
{
27+
errno = ENOSYS;
28+
return -1;
29+
}
30+
31+
int _close_r(struct _reent *r, int fd)
32+
{
33+
errno = ENOSYS;
34+
return -1;
35+
}
36+
37+
off_t _lseek_r(struct _reent *r, int fd, off_t offset, int whence)
38+
{
39+
errno = ENOSYS;
40+
return -1;
41+
}
42+
43+
ssize_t _read_r(struct _reent *r, int fd, void *ptr, size_t len)
44+
{
45+
errno = ENOSYS;
46+
return -1;
47+
}
48+
49+
ssize_t _write_r(struct _reent *r, int fd, const void *ptr, size_t len)
50+
{
51+
errno = ENOSYS;
52+
return -1;
53+
}
54+
55+
int _getpid_r(struct _reent *r)
56+
{
57+
return 1;
58+
}
59+
60+
int _kill_r(struct _reent *r, int pid, int sig)
61+
{
62+
errno = ENOSYS;
63+
return -1;
64+
}
65+
66+
int _getentropy_r(struct _reent *r, void *buffer, size_t length)
67+
{
68+
esp_fill_random(buffer, length);
69+
return 0;
70+
}
71+
72+
void *pthread_getspecific(pthread_key_t key)
73+
{
74+
return NULL;
75+
}
76+
77+
int pthread_setspecific(pthread_key_t key, const void *value)
78+
{
79+
return 0;
80+
}
81+
82+
int pthread_key_create(pthread_key_t *key, void (*destructor)(void*))
83+
{
84+
errno = ENOSYS;
85+
return -1;
86+
}
87+
88+
int pthread_key_delete(pthread_key_t key)
89+
{
90+
errno = ENOSYS;
91+
return -1;
92+
}
93+
94+
int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr)
95+
{
96+
errno = ENOSYS;
97+
return -1;
98+
}
99+
100+
int pthread_mutex_destroy(pthread_mutex_t *mutex)
101+
{
102+
errno = ENOSYS;
103+
return -1;
104+
}
105+
106+
int pthread_mutex_lock(pthread_mutex_t *mutex)
107+
{
108+
errno = ENOSYS;
109+
return -1;
110+
}
111+
112+
int pthread_mutex_unlock(pthread_mutex_t *mutex)
113+
{
114+
errno = ENOSYS;
115+
return -1;
116+
}
117+
118+
void *__cxa_get_globals(void)
119+
{
120+
return NULL;
121+
}
122+
123+
void *__cxa_get_globals_fast(void)
124+
{
125+
return NULL;
126+
}
127+
128+
int __cxa_thread_atexit(void (*func)(void *), void *arg, void *dso)
129+
{
130+
return 0;
131+
}
132+
133+
void esp_tee_include_syscalls_impl(void)
134+
{
135+
136+
}

components/esp_tee/subproject/main/core/esp_tee_init.c

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -187,19 +187,3 @@ void __attribute__((noreturn)) esp_tee_init(uint32_t ree_entry_addr, uint32_t re
187187
/* App entry function should not return here. */
188188
ESP_INFINITE_LOOP(); /* WDT will reset us */
189189
}
190-
191-
// NOTE: Remove compile-time warnings for the below newlib-provided functions
192-
struct _reent *__getreent(void)
193-
{
194-
return _GLOBAL_REENT;
195-
}
196-
197-
void _fstat_r(void) {}
198-
199-
void _close_r(void) {}
200-
201-
void _lseek_r(void) {}
202-
203-
void _read_r(void) {}
204-
205-
void _write_r(void) {}

components/nvs_flash/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ elseif(esp_tee_build)
3232
"src/nvs_platform.cpp")
3333

3434
set(requires esp_partition mbedtls)
35-
set(priv_requires spi_flash newlib)
35+
set(priv_requires spi_flash newlib cxx)
3636

3737
idf_component_register(SRCS "${srcs}"
3838
REQUIRES "${requires}"

0 commit comments

Comments
 (0)