Skip to content

Commit 0b9ed75

Browse files
committed
loader: Redirect malloc to kernel heap.
Replace the libc's malloc, free etc.. with wrappers that redirect memory allocation Zephyr's kernel heap functions (k_malloc/k_free). Signed-off-by: iabdalkader <[email protected]>
1 parent 6af6d0d commit 0b9ed75

File tree

5 files changed

+35
-10
lines changed

5 files changed

+35
-10
lines changed

extra/build.sh

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,6 @@ extra/gen_provides.py "${BUILD_DIR}/zephyr/zephyr.elf" -LF \
104104
"+kheap_llext_heap" \
105105
"+kheap__system_heap" \
106106
"*sketch_base_addr=_sketch_start" \
107-
"*sketch_max_size=_sketch_max_size" \
108-
"malloc=__wrap_malloc" \
109-
"free=__wrap_free" \
110-
"realloc=__wrap_realloc" \
111-
"calloc=__wrap_calloc" \
112-
"random=__wrap_random" > ${VARIANT_DIR}/syms-static.ld
107+
"*sketch_max_size=_sketch_max_size" > ${VARIANT_DIR}/syms-static.ld
113108

114109
cmake -P extra/gen_arduino_files.cmake $variant

loader/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,12 @@ FILE(GLOB app_sources *.c)
2525
target_sources(app PRIVATE ${app_sources})
2626
target_compile_definitions(app PUBLIC _XOPEN_SOURCE=700)
2727

28+
# Wrap malloc, free, calloc and realloc
29+
zephyr_ld_options(
30+
-Wl,--wrap=malloc
31+
-Wl,--wrap=free
32+
-Wl,--wrap=calloc
33+
-Wl,--wrap=realloc
34+
)
35+
2836
target_link_libraries(app PUBLIC blobs)

loader/llext_exports.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -276,10 +276,7 @@ FORCE_EXPORT_SYM(__aeabi_idivmod);
276276
FORCE_EXPORT_SYM(__aeabi_ldivmod);
277277
FORCE_EXPORT_SYM(__aeabi_ul2f);
278278
FORCE_EXPORT_SYM(__aeabi_dcmpge);
279-
280-
#if defined (CONFIG_CPP)
281279
FORCE_EXPORT_SYM(__cxa_pure_virtual);
282-
#endif
283280

284281
#if defined(CONFIG_BOARD_ARDUINO_UNO_Q)
285282
FORCE_EXPORT_SYM(matrixBegin);

loader/malloc_wrapper.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright (C) 2025 Arduino SA
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* Wrapper to redirect malloc/free to k_malloc/k_free using ld --wrap
7+
*/
8+
9+
#include <zephyr/kernel.h>
10+
11+
void *__wrap_malloc(size_t size) {
12+
return k_malloc(size);
13+
}
14+
15+
void __wrap_free(void *ptr) {
16+
k_free(ptr);
17+
}
18+
19+
void *__wrap_calloc(size_t nmemb, size_t size) {
20+
return k_calloc(nmemb, size);
21+
}
22+
23+
void *__wrap_realloc(void *ptr, size_t size) {
24+
return k_realloc(ptr, size);
25+
}

platform.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ build.combine_command={build.link_command} {build.link_args.build-{build.link_mo
6363

6464
# link_args.* are included by any link_command depending on the link_mode
6565
build.link_args.dynamic=-e main
66-
build.link_args.static=-lc -lm -lgcc -Wl,--wrap=random -Wl,--wrap=calloc -Wl,--wrap=free -Wl,--wrap=malloc -Wl,--wrap=realloc
66+
build.link_args.static=-lc -lm -lgcc
6767

6868
# link_args.check-* are used to check the build. Only LLEXT needs these to emulate a static build (no -r!).
6969
build.link_args.check-dynamic="-T{build.variant.path}/syms-dynamic.ld" "-T{build.ldscript.path}/memory-check.ld" "-T{build.ldscript.path}/build-static.ld"

0 commit comments

Comments
 (0)