Skip to content

Commit 36225d1

Browse files
committed
wip: linked build
1 parent 83589f6 commit 36225d1

File tree

13 files changed

+252
-79
lines changed

13 files changed

+252
-79
lines changed

boards.txt

Lines changed: 38 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
menu.debug=Mode
1+
menu.mode=Mode
22

33
giga.name=Arduino Giga R1
44
giga.build.core=arduino
55
giga.build.crossprefix=arm-zephyr-eabi-
66
giga.build.compiler_path={runtime.tools.arm-zephyr-eabi-0.16.8.path}/bin/
77

8-
giga.menu.debug.false=Standard
9-
giga.menu.debug.true=Debug
8+
giga.menu.mode.llext=llext
9+
giga.menu.mode.linked=linked
1010

11-
giga.menu.debug.false.post_build_arg=
12-
giga.menu.debug.true.post_build_arg=debug
11+
giga.menu.mode.false.post_build_arg=
12+
giga.menu.mode.true.post_build_arg=debug
1313

1414
giga.build.variant=arduino_giga_r1_m7
1515
giga.build.mcu=cortex-m7
@@ -78,11 +78,11 @@ nano33ble.build.core=arduino
7878
nano33ble.build.crossprefix=arm-zephyr-eabi-
7979
nano33ble.build.compiler_path={runtime.tools.arm-zephyr-eabi-0.16.8.path}/bin/
8080

81-
nano33ble.menu.debug.false=Standard
82-
nano33ble.menu.debug.true=Debug
81+
nano33ble.menu.mode.false=Standard
82+
nano33ble.menu.mode.true=Debug
8383

84-
nano33ble.menu.debug.false.post_build_arg=
85-
nano33ble.menu.debug.true.post_build_arg=debug
84+
nano33ble.menu.mode.false.post_build_arg=
85+
nano33ble.menu.mode.true.post_build_arg=debug
8686

8787
nano33ble.build.variant=arduino_nano_33_ble_sense
8888
nano33ble.build.mcu=cortex-m4
@@ -148,11 +148,11 @@ ek_ra8d1.build.core=arduino
148148
ek_ra8d1.build.crossprefix=arm-zephyr-eabi-
149149
ek_ra8d1.build.compiler_path={runtime.tools.arm-zephyr-eabi-0.16.8.path}/bin/
150150

151-
ek_ra8d1.menu.debug.false=Standard
152-
ek_ra8d1.menu.debug.true=Debug
151+
ek_ra8d1.menu.mode.false=Standard
152+
ek_ra8d1.menu.mode.true=Debug
153153

154-
ek_ra8d1.menu.debug.false.post_build_arg=
155-
ek_ra8d1.menu.debug.true.post_build_arg=debug
154+
ek_ra8d1.menu.mode.false.post_build_arg=
155+
ek_ra8d1.menu.mode.true.post_build_arg=debug
156156

157157
ek_ra8d1.build.variant=ek_ra8d1
158158
ek_ra8d1.build.mcu=cortex-m85+nomve
@@ -214,11 +214,11 @@ frdm_mcxn947.build.core=arduino
214214
frdm_mcxn947.build.crossprefix=arm-zephyr-eabi-
215215
frdm_mcxn947.build.compiler_path={runtime.tools.arm-zephyr-eabi-0.16.8.path}/bin/
216216

217-
frdm_mcxn947.menu.debug.false=Standard
218-
frdm_mcxn947.menu.debug.true=Debug
217+
frdm_mcxn947.menu.mode.false=Standard
218+
frdm_mcxn947.menu.mode.true=Debug
219219

220-
frdm_mcxn947.menu.debug.false.post_build_arg=
221-
frdm_mcxn947.menu.debug.true.post_build_arg=debug
220+
frdm_mcxn947.menu.mode.false.post_build_arg=
221+
frdm_mcxn947.menu.mode.true.post_build_arg=debug
222222

223223
frdm_mcxn947.build.variant=frdm_mcxn947_mcxn947_cpu0
224224
frdm_mcxn947.build.mcu=cortex-m33
@@ -274,11 +274,11 @@ portentah7.build.core=arduino
274274
portentah7.build.crossprefix=arm-zephyr-eabi-
275275
portentah7.build.compiler_path={runtime.tools.arm-zephyr-eabi-0.16.8.path}/bin/
276276

277-
portentah7.menu.debug.false=Standard
278-
portentah7.menu.debug.true=Debug
277+
portentah7.menu.mode.false=Standard
278+
portentah7.menu.mode.true=Debug
279279

280-
portentah7.menu.debug.false.post_build_arg=
281-
portentah7.menu.debug.true.post_build_arg=debug
280+
portentah7.menu.mode.false.post_build_arg=
281+
portentah7.menu.mode.true.post_build_arg=debug
282282

283283
portentah7.build.variant=arduino_portenta_h7
284284
portentah7.build.mcu=cortex-m7
@@ -346,11 +346,11 @@ frdm_rw612.build.core=arduino
346346
frdm_rw612.build.crossprefix=arm-zephyr-eabi-
347347
frdm_rw612.build.compiler_path={runtime.tools.arm-zephyr-eabi-0.16.8.path}/bin/
348348

349-
frdm_rw612.menu.debug.false=Standard
350-
frdm_rw612.menu.debug.true=Debug
349+
frdm_rw612.menu.mode.false=Standard
350+
frdm_rw612.menu.mode.true=Debug
351351

352-
frdm_rw612.menu.debug.false.post_build_arg=
353-
frdm_rw612.menu.debug.true.post_build_arg=debug
352+
frdm_rw612.menu.mode.false.post_build_arg=
353+
frdm_rw612.menu.mode.true.post_build_arg=debug
354354

355355
frdm_rw612.build.variant=frdm_rw612
356356
frdm_rw612.build.mcu=cortex-m33+nodsp
@@ -405,11 +405,11 @@ niclasense.build.core=arduino
405405
niclasense.build.crossprefix=arm-zephyr-eabi-
406406
niclasense.build.compiler_path={runtime.tools.arm-zephyr-eabi-0.16.8.path}/bin/
407407

408-
niclasense.menu.debug.false=Standard
409-
niclasense.menu.debug.true=Debug
408+
niclasense.menu.mode.false=Standard
409+
niclasense.menu.mode.true=Debug
410410

411-
niclasense.menu.debug.false.post_build_arg=
412-
niclasense.menu.debug.true.post_build_arg=debug
411+
niclasense.menu.mode.false.post_build_arg=
412+
niclasense.menu.mode.true.post_build_arg=debug
413413

414414
niclasense.build.variant=arduino_nicla_sense_me
415415
niclasense.build.mcu=cortex-m4
@@ -481,11 +481,15 @@ portentac33.build.core=arduino
481481
portentac33.build.crossprefix=arm-zephyr-eabi-
482482
portentac33.build.compiler_path={runtime.tools.arm-zephyr-eabi-0.16.8.path}/bin/
483483

484-
portentac33.menu.debug.false=Standard
485-
portentac33.menu.debug.true=Debug
484+
portentac33.menu.mode.llext=llext
485+
portentac33.menu.mode.linked=linked
486486

487-
portentac33.menu.debug.false.post_build_arg=
488-
portentac33.menu.debug.true.post_build_arg=debug
487+
portentac33.menu.mode.linked.build.extra_extra_ldflags=-lc -lm -lgcc -L{build.variant.path}
488+
portentac33.menu.mode.linked.build.llext_link_flags=
489+
portentac33.menu.mode.linked.build.suffix=_linked
490+
portentac33.menu.mode.linked.build.ldscript={runtime.platform.path}/variants/linked/linker_script.ld
491+
portentac33.menu.mode.linked.upload.externsion=bin.dfu
492+
portentac33.menu.mode.linked.post_build_arg=linked
489493

490494
portentac33.build.variant=arduino_portenta_c33
491495
portentac33.build.mcu=cortex-m33
@@ -506,7 +510,7 @@ portentac33.compiler.zephyr.ldflags=-fno-exceptions -fno-rtti -fno-threadsafe-st
506510
portentac33.compiler.zephyr.cflags=-fno-strict-aliasing -fno-printf-return-value -fno-common -mthumb -mabi=aapcs -mfp16-format=ieee -mtp=soft -Wall -Wformat -Wformat-security -Wno-format-zero-length -Wdouble-promotion -Wno-pointer-sign -Wpointer-arith -Wexpansion-to-defined -Wno-unused-but-set-variable -Werror=implicit-int -fno-asynchronous-unwind-tables -ftls-model=local-exec -fno-reorder-functions --param=min-pagesize=0 -fno-defer-pop --specs=picolibc.specs -D_POSIX_THREADS -mlong-calls -nodefaultlibs
507511
portentac33.compiler.zephyr.cxxflags={compiler.zephyr.ldflags} -fdata-sections -ffunction-sections -fno-unwind-tables -fno-strict-aliasing -fno-printf-return-value -fno-common -mthumb -mabi=aapcs -mfp16-format=ieee -mtp=soft -Wall -Wformat -Wformat-security -Wno-format-zero-length -Wdouble-promotion -Wpointer-arith -Wexpansion-to-defined -Wno-unused-but-set-variable -fno-asynchronous-unwind-tables -ftls-model=local-exec -fno-reorder-functions --param=min-pagesize=0 -fno-defer-pop --specs=picolibc.specs -D_POSIX_THREADS -mlong-calls -nodefaultlibs
508512
portentac33.compiler.zephyr.includes={build.variant.path}/includes.txt
509-
portentac33.compiler.zephyr.extra_ldflags=-lstdc++ -lsupc++ -lnosys -nostdlib
513+
portentac33.compiler.zephyr.extra_ldflags=-lstdc++ -lsupc++ -lnosys -nostdlib {build.extra_extra_ldflags}
510514
portentac33.compiler.zephyr=
511515
portentac33.vid.0=0x2341
512516
portentac33.pid.0=0x0068

cores/arduino/main.cpp

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66

77
#include "Arduino.h"
8+
#include <cstdint>
89
#ifdef CONFIG_LLEXT
910
#include <zephyr/llext/symbol.h>
1011
#endif
@@ -36,4 +37,40 @@ int main(void) {
3637

3738
#ifdef CONFIG_LLEXT
3839
LL_EXTENSION_SYMBOL(main);
39-
#endif
40+
#endif
41+
42+
/* These magic symbols are provided by the linker. */
43+
extern void (*__preinit_array_start []) (void) __attribute__((weak));
44+
extern void (*__preinit_array_end []) (void) __attribute__((weak));
45+
extern void (*__init_array_start []) (void) __attribute__((weak));
46+
extern void (*__init_array_end []) (void) __attribute__((weak));
47+
48+
static void __libc_init_array (void)
49+
{
50+
size_t count;
51+
size_t i;
52+
53+
count = __preinit_array_end - __preinit_array_start;
54+
for (i = 0; i < count; i++)
55+
__preinit_array_start[i] ();
56+
57+
count = __init_array_end - __init_array_start;
58+
for (i = 0; i < count; i++)
59+
__init_array_start[i] ();
60+
}
61+
62+
extern "C" __attribute__((section(".entry_point"), used)) void entry_point(k_thread_stack_t* stack, size_t stack_size) {
63+
// copy .data in the right place
64+
// .bss should already be in the right place
65+
// call constructors
66+
extern uintptr_t _sidata;
67+
extern uintptr_t _sdata;
68+
extern uintptr_t _edata;
69+
extern uintptr_t _sbss;
70+
extern uintptr_t _ebss;
71+
//__asm volatile ("cpsie i");
72+
memcpy(&_sdata, &_sidata, &_edata - &_sdata);
73+
memset(&_sbss, 0, &_ebss - &_sbss);
74+
__libc_init_array();
75+
main();
76+
}

extra/build.sh

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ set -e
66

77
board=$1
88
variant=$2
9-
shield=$3
9+
third_arg=$3
10+
extra_args=""
1011

1112
if [[ $# -eq 0 ]]; then
1213
board=arduino_giga_r1//m7
@@ -15,13 +16,11 @@ fi
1516

1617
source venv/bin/activate
1718

18-
if [ -n "$shield" ]; then
19-
shield_arg="--shield $shield"
20-
else
21-
shield_arg=""
19+
if [ "$third_arg" != "" ]; then
20+
extra_args="--shield $shield"
2221
fi
2322

24-
(west build loader -b $board -p $shield_arg && west build -t llext-edk)
23+
(west build loader -b $board -p $extra_args && west build -t llext-edk)
2524
(tar xfp build/zephyr/llext-edk.tar.xz --directory variants/$variant/)
2625

2726
(cp build/zephyr/zephyr.elf firmwares/zephyr-$variant.elf)
@@ -31,3 +30,11 @@ elif [ -f build/zephyr/zephyr.hex ]; then
3130
cp build/zephyr/zephyr.hex firmwares/zephyr-$variant.hex
3231
fi
3332

33+
# Generate the provides.ld file for linked builds
34+
echo "Exporting provides.ld"
35+
arm-none-eabi-readelf --wide -s build/zephyr/zephyr.elf | c++filt | grep FUNC | awk -F' ' '{print "PROVIDE("$8" = 0x"$2");"}' > variants/$variant/provides.ld
36+
arm-none-eabi-readelf --wide -s build/zephyr/zephyr.elf | c++filt | grep llext_stack | awk -F' ' '{print "PROVIDE("$8" = 0x"$2");"}' >> variants/$variant/provides.ld
37+
arm-none-eabi-readelf --wide -s build/zephyr/zephyr.elf | c++filt | grep llext_stack | awk -F' ' '{print "PROVIDE(llext_stack_size = "$3");"}' >> variants/$variant/provides.ld
38+
cat build/zephyr/zephyr.map | grep __device_dts_ord | grep -v rodata | grep -v llext_const_symbol | awk -F' ' '{print "PROVIDE("$2" = "$1");"}' >> variants/$variant/provides.ld
39+
TEXT_START=`cat loader/boards/$variant.overlay | grep user_sketch: | cut -f2 -d"@" | cut -f1 -d"{"`
40+
echo "PROVIDE(_sketch_start = 0x$TEXT_START);" >> variants/$variant/provides.ld

extra/post_build_tool/main.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,15 @@ func main() {
1313

1414
filename := os.Args[1]
1515
debug := 0
16+
linked := 0
1617

17-
if len(os.Args) == 3 {
18+
if len(os.Args) >= 3 {
1819
if os.Args[2] == "debug" {
1920
debug = 1
2021
}
22+
if os.Args[2] == "linked" {
23+
linked = 1
24+
}
2125
}
2226

2327
// Read the file content
@@ -35,7 +39,7 @@ func main() {
3539

3640
// Create the new content with the length in front
3741
len_str := fmt.Sprintf("%d", length)
38-
newContent := append([]byte(len_str), 0, byte(debug))
42+
newContent := append([]byte(len_str), 0, byte(debug), byte(linked))
3943
// make newContent 16 bytes
4044
tmp := make([]byte, 16-len(newContent))
4145
newContent = append(newContent, tmp...)

loader/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
88

99
project(app LANGUAGES C CXX)
1010

11-
include(${CMAKE_CURRENT_SOURCE_DIR}/blobs/CMakeLists.txt)
11+
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/blobs)
1212

1313
FILE(GLOB app_sources *.c)
1414
target_sources(app PRIVATE ${app_sources})

loader/blobs/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ if (CONFIG_BOARD_ARDUINO_PORTENTA_C33)
66
set(gen_dir ${gen_inc_dir}/c33_bl_patch)
77
generate_inc_file_for_target(
88
${ZEPHYR_CURRENT_LIBRARY}
9-
${CMAKE_CURRENT_SOURCE_DIR}/blobs/c33_bl.bin
9+
${CMAKE_CURRENT_SOURCE_DIR}/c33_bl.bin
1010
${gen_dir}/c33_bl.bin.inc
1111
)
1212
endif()

loader/boards/arduino_giga_r1_m7.overlay

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -331,29 +331,11 @@
331331
};
332332
};
333333

334-
/ {
335-
soc {
336-
flash-controller@52002000 {
337-
flash0: flash@8000000 {
338-
compatible = "st,stm32-nv-flash", "soc-nv-flash";
339-
write-block-size = <32>;
340-
erase-block-size = <DT_SIZE_K(128)>;
341-
/* maximum erase time for a 128K sector */
342-
max-erase-time = <4000>;
343-
344-
reg = <0x08000000 DT_SIZE_K(1024)>;
345-
status = "okay";
346-
347-
partitions {
348-
compatible = "fixed-partitions";
349-
#address-cells = <1>;
350-
#size-cells = <1>;
351-
352-
user_sketch: partition@e0000 {
353-
reg = <0x0E0000 0x20000>;
354-
};
355-
};
356-
};
334+
335+
&flash0 {
336+
partitions {
337+
user_sketch: partition@e0000 {
338+
reg = <0x0E0000 0x20000>;
357339
};
358340
};
359341
};

loader/boards/arduino_portenta_c33.overlay

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272

7373
pwm-pin-gpios = <&ioport6 0 0>;
7474

75-
serials = <&cdc_acm_uart0>,<&sci9>;
75+
serials = <&cdc_acm_uart0>,<&uart9>;
7676
cdc-acm = <&cdc_acm_uart0>;
7777
i2cs = <&iic1>;
7878
spis = <&spi1>;

loader/boards/arduino_portenta_h7_m7.overlay

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
#address-cells = <1>;
5656
#size-cells = <1>;
5757

58-
user_sketch: partition@0 {
58+
user_sketch: partition@e0000 {
5959
reg = <0x0E0000 0x20000>;
6060
};
6161
};

loader/fixups.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ int smh_init(void) {
102102
SYS_INIT(smh_init, POST_KERNEL, CONFIG_CLOCK_CONTROL_PWM_INIT_PRIORITY);
103103
#endif
104104

105-
#if defined(CONFIG_BOARD_ARDUINO_PORTENTA_C33)
105+
#if defined(CONFIG_BOARD_ARDUINO_PORTENTA_C33) && defined(CONFIG_LLEXT)
106106
#include <zephyr/kernel.h>
107107
#include <zephyr/storage/flash_map.h>
108108

0 commit comments

Comments
 (0)