Skip to content

Commit 84f5894

Browse files
committed
loader: Redirect sketch malloc to kernel heap.
Replace the libc malloc/free with Zephyr's kernel heap functions (k_malloc/k_free) for sketch memory allocation. This is achieved by using EXPORT_SYMBOL_NAMED to alias k_malloc as malloc, k_free as free, k_calloc as calloc, and k_realloc as realloc in the llext symbol table. When sketches call malloc(), the llext loader resolves these to the kernel heap functions. Signed-off-by: iabdalkader <i.abdalkader@gmail.com>
1 parent 4a47969 commit 84f5894

File tree

3 files changed

+7
-14
lines changed

3 files changed

+7
-14
lines changed

extra/build.sh

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -100,15 +100,10 @@ cp ${BUILD_DIR}/zephyr/.config firmwares/zephyr-$variant.config
100100
# Generate the provides.ld file for linked builds
101101
echo "Generating exported symbol scripts"
102102
extra/gen_provides.py "${BUILD_DIR}/zephyr/zephyr.elf" -L > ${VARIANT_DIR}/syms-dynamic.ld
103-
extra/gen_provides.py "${BUILD_DIR}/zephyr/zephyr.elf" -LF \
103+
extra/gen_provides.py "${BUILD_DIR}/zephyr/zephyr.elf" -L \
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/llext_exports.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,10 @@ EXPORT_SYMBOL(strchr);
2424
EXPORT_SYMBOL(strcat);
2525
EXPORT_SYMBOL(memmove);
2626

27-
EXPORT_SYMBOL(k_malloc);
28-
EXPORT_SYMBOL(k_free);
29-
EXPORT_SYMBOL(malloc);
30-
EXPORT_SYMBOL(realloc);
31-
EXPORT_SYMBOL(calloc);
32-
EXPORT_SYMBOL(free);
27+
EXPORT_SYMBOL_NAMED(k_malloc, malloc);
28+
EXPORT_SYMBOL_NAMED(k_free, free);
29+
EXPORT_SYMBOL_NAMED(k_calloc, calloc);
30+
EXPORT_SYMBOL_NAMED(k_realloc, realloc);
3331
EXPORT_SYMBOL(rand);
3432
EXPORT_SYMBOL(srand);
3533

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)