Skip to content

Commit adf2077

Browse files
committed
build: Add support for checking an llext build at compile time
This commit adds compile-time checking of the sketch to verify that all symbols not directly resolved in the sketch are defined in the Zephyr core. This reuses the 'linked' logic, but defines the llext-specific symbols only. Signed-off-by: Luca Burelli <[email protected]>
1 parent 7b5343f commit adf2077

File tree

9 files changed

+50
-27
lines changed

9 files changed

+50
-27
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ llext-edk/
66
cflags.txt
77
cxxflags.txt
88
includes.txt
9-
provides.ld
9+
syms-llext.ld
10+
syms-linked.ld

boards.txt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -447,10 +447,7 @@ portentac33.menu.debug.true.postbuild_debug=-debug
447447
portentac33.menu.mode.llext=llext
448448
portentac33.menu.mode.linked=linked
449449

450-
portentac33.menu.mode.linked.build.extra_ldflags=-lc -lm -lgcc -L{build.variant.path} -Wl,--wrap=random -Wl,--wrap=calloc -Wl,--wrap=free -Wl,--wrap=malloc -Wl,--wrap=realloc
451-
portentac33.menu.mode.linked.build.llext_link_flags=
452-
portentac33.menu.mode.linked.build.suffix=_linked
453-
portentac33.menu.mode.linked.build.ldscript={runtime.platform.path}/variants/_linked/linker_script.ld
450+
portentac33.menu.mode.linked.build.link_mode=linked
454451
portentac33.menu.mode.linked.upload.extension=bin-zsk.bin
455452
portentac33.menu.mode.linked.postbuild_mode=-prelinked
456453

extra/build.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ done
9191

9292
# Generate the provides.ld file for linked builds
9393
echo "Generating exported symbol scripts"
94+
extra/gen_provides.py "${BUILD_DIR}/zephyr/zephyr.elf" -L > ${VARIANT_DIR}/syms-llext.ld
9495
extra/gen_provides.py "${BUILD_DIR}/zephyr/zephyr.elf" -LF \
9596
"+kheap_llext_heap" \
9697
"+kheap__system_heap" \
@@ -100,6 +101,6 @@ extra/gen_provides.py "${BUILD_DIR}/zephyr/zephyr.elf" -LF \
100101
"free=__wrap_free" \
101102
"realloc=__wrap_realloc" \
102103
"calloc=__wrap_calloc" \
103-
"random=__wrap_random" > ${VARIANT_DIR}/provides.ld
104+
"random=__wrap_random" > ${VARIANT_DIR}/syms-linked.ld
104105

105106
cmake -P extra/gen_arduino_files.cmake $variant

extra/package_core.inc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,4 @@ README.md
1313
doc/
1414
cores/
1515
libraries/
16-
variants/_llext/
17-
variants/_linked/
16+
variants/_ldscripts/

platform.txt

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ compiler.path={build.compiler_path}
1919
compiler.c.cmd={build.crossprefix}gcc
2020
compiler.c.flags=-g -c {compiler.warning_flags} {compiler.zephyr.macros} "@{compiler.zephyr.cflags_file}" {compiler.zephyr.arch.define} -MMD -mcpu={build.mcu} {build.float-abi} {build.fpu}
2121
compiler.c.elf.cmd={build.crossprefix}g++
22-
compiler.c.elf.flags={build.llext_link_flags} -Wl,--gc-sections -mcpu={build.mcu} {build.float-abi} {build.fpu} -std=gnu++17
22+
compiler.c.elf.flags=-Wl,--gc-sections -mcpu={build.mcu} {build.float-abi} {build.fpu} -std=gnu++17
2323
compiler.S.cmd={build.crossprefix}g++
2424
compiler.S.flags=-c -x assembler-with-cpp -mcpu={build.mcu} {build.fpu}
2525
compiler.cpp.cmd={build.crossprefix}g++
@@ -49,12 +49,26 @@ compiler.zephyr.extra_ldflags=-lstdc++ -lsupc++
4949
# this can be overriden in boards.txt
5050
build.extra_flags=
5151
build.extra_ldflags=
52+
build.link_mode=llext
5253
compiler.zephyr=
5354

54-
build.ldscript={runtime.platform.path}/variants/_llext/linker_script.ld
55-
build.llext_link_flags=-r -e main
56-
build.extra_extra_ldflags=
57-
build.suffix=
55+
build.ldscript.path={runtime.platform.path}/variants/_ldscripts
56+
build.link_command="{compiler.path}{compiler.c.elf.cmd}" "-L{build.path}" {compiler.c.elf.flags} {compiler.c.elf.extra_flags} {build.extra_flags} {build.extra_ldflags} {compiler.zephyr.common_ldflags} --specs=picolibc.specs --specs=nosys.specs {compiler.ldflags} {object_files} -Wl,--start-group "{build.path}/{archive_file}" {compiler.zephyr} {compiler.zephyr.extra_ldflags} {compiler.libraries.ldflags} -Wl,--end-group {build.link_args.{build.link_mode}}
57+
58+
# link_args.* are included by any link_command depending on the link_mode
59+
build.link_args.llext=-e main
60+
build.link_args.linked=-lc -lm -lgcc -L{build.variant.path} -Wl,--wrap=random -Wl,--wrap=calloc -Wl,--wrap=free -Wl,--wrap=malloc -Wl,--wrap=realloc
61+
# link_args.check-* are used to check the build. Only LLEXT needs these to emulate a linked build (no -r!).
62+
build.link_args.check-llext="-T{build.variant.path}/syms-llext.ld" "-T{build.ldscript.path}/memory-check.ld" "-T{build.ldscript.path}/build-linked.ld"
63+
# link_args.build-* are used to create the final binary. -common are added to both llext and linked.
64+
build.link_args.build-llext="-T{build.ldscript.path}/build-llext.ld" -r
65+
build.link_args.build-linked="-T{build.variant.path}/syms-linked.ld" "-T{build.ldscript.path}/memory-linked.ld" "-T{build.ldscript.path}/build-linked.ld"
66+
build.link_args.build-common="-Wl,-Map,{build.path}/{build.project_name}.map" -o "{build.path}/{build.project_name}_debug.elf"
67+
68+
build.check_command-llext={build.link_command} {build.link_args.check-llext} -o "{build.path}/{build.project_name}_check.tmp"
69+
build.check_command-linked=
70+
build.combine_command={build.link_command} {build.link_args.build-{build.link_mode}} {build.link_args.build-common}
71+
5872
upload.extension=elf-zsk.bin
5973
postbuild_debug=
6074
postbuild_mode=
@@ -99,7 +113,9 @@ recipe.S.o.pattern="{compiler.path}{compiler.S.cmd}" {compiler.S.flags} -DARDUIN
99113
recipe.ar.pattern="{compiler.path}{compiler.ar.cmd}" {compiler.ar.flags} {compiler.ar.extra_flags} "{archive_file_path}" "{object_file}"
100114

101115
## Combine gc-sections, archives, and objects
102-
recipe.c.combine.pattern="{compiler.path}{compiler.c.elf.cmd}" "-L{build.path}" {compiler.c.elf.flags} {compiler.c.elf.extra_flags} {build.extra_flags} {build.extra_ldflags} {compiler.zephyr.common_ldflags} "-T{build.ldscript}" "-Wl,-Map,{build.path}/{build.project_name}.map" --specs=picolibc.specs --specs=nosys.specs {compiler.ldflags} -o "{build.path}/{build.project_name}_debug.elf" {object_files} -Wl,--start-group "{build.path}/{archive_file}" {compiler.zephyr} {compiler.zephyr.extra_ldflags} {compiler.libraries.ldflags} -Wl,--end-group
116+
recipe.c.combine.1.pattern={build.check_command-{build.link_mode}}
117+
recipe.c.combine.2.pattern={build.combine_command}
118+
recipe.c.combine.pattern={build.combine_command}
103119
recipe.hooks.linking.postlink.1.pattern="{compiler.path}{build.crossprefix}strip" --strip-debug "{build.path}/{build.project_name}_debug.elf" -o "{build.path}/{build.project_name}.elf"
104120

105121
## Create eeprom

variants/_linked/linker_script.ld renamed to variants/_ldscripts/build-linked.ld

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,5 @@
1-
INCLUDE "provides.ld"
2-
3-
MEMORY
4-
{
5-
/* 16 below compensates for the zsk header that is added before the .bin */
6-
FLASH (rx) : ORIGIN = _sketch_start + 16, LENGTH = _sketch_max_size - 16
7-
RAM (rwx) : ORIGIN = kheap_llext_heap, LENGTH = kheap_llext_heap_size
8-
}
9-
10-
PROVIDE(__heap_start = kheap__system_heap);
11-
PROVIDE(__heap_end = kheap__system_heap + kheap__system_heap_size);
12-
131
ENTRY(entry_point)
142
SECTIONS {
15-
163
/DISCARD/ : {
174
*(.ARM.attributes)
185
*(.ARM.attributes.*)
File renamed without changes.

variants/_ldscripts/memory-check.ld

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
MEMORY
2+
{
3+
FLASH (rx) : ORIGIN = 0x00000000, LENGTH = 0x10000000
4+
RAM (rwx) : ORIGIN = 0x20000000, LENGTH = 0x10000000
5+
}
6+
7+
PROVIDE(__heap_start = 0x40000000);
8+
PROVIDE(__heap_end = 0x50000000);
9+
PROVIDE(kheap__system_heap = 0x40000000);
10+
PROVIDE(kheap__system_heap_size = 0x10000000);
11+
12+
PROVIDE(kheap_llext_heap = 0x60000000);
13+
PROVIDE(kheap_llext_heap_size = 0x10000000);

variants/_ldscripts/memory-linked.ld

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
MEMORY
2+
{
3+
/* 16 below compensates for the zsk header that is added before the .bin */
4+
FLASH (rx) : ORIGIN = _sketch_start + 16, LENGTH = _sketch_max_size - 16
5+
RAM (rwx) : ORIGIN = kheap_llext_heap, LENGTH = kheap_llext_heap_size
6+
}
7+
8+
PROVIDE(__heap_start = kheap__system_heap);
9+
PROVIDE(__heap_end = kheap__system_heap + kheap__system_heap_size);

0 commit comments

Comments
 (0)