|
2 | 2 |
|
3 | 3 | This tutorial assumes you have the |
4 | 4 | [rlbox_wasm2c_sandbox](https://github.com/PLSysSec/rlbox_wasm2c_sandbox/) git |
5 | | -repo is in the path $(RLBOX_WASM2C_PATH), and you have installed |
| 5 | +repo is in the path $RLBOX_WASM2C_PATH, and you have installed |
6 | 6 | [wasi-sdk](https://github.com/WebAssembly/wasi-sdk/releases) on your computer in |
7 | | -the path $(WASI_SDK_PATH) |
| 7 | +the path $WASI_SDK_PATH |
8 | 8 |
|
9 | 9 | Build the sources of your library along with the file |
10 | | -`$(RLBOX_WASM2C_PATH)/c_src/wasm2c_sandbox_wrapper.c`. Pass the flags `-Wl,--export-all -Wl,--stack-first -Wl,-z,stack-size=262144 -Wl,--no-entry -Wl,--growable-table -Wl,--import-memory -Wl,--import-table` |
| 10 | +`$RLBOX_WASM2C_PATH/c_src/wasm2c_sandbox_wrapper.c`. Pass the flags `-Wl,--export-all -Wl,--stack-first -Wl,-z,stack-size=262144 -Wl,--no-entry -Wl,--growable-table -Wl,--import-memory -Wl,--import-table` |
11 | 11 | to the linker using the wasi-clang compiler. This will produce a wasm module. |
12 | 12 |
|
13 | 13 | To edit an existing Make based build system, you can run the commmand. |
14 | 14 |
|
15 | 15 | ```bash |
16 | | -$(WASI_SDK_PATH)/bin/clang --sysroot $(WASI_SDK_PATH)/share/wasi-sysroot $(RLBOX_WASM2C_PATH)/c_src/wasm2c_sandbox_wrapper.c -c -o $(RLBOX_WASM2C_PATH)/c_src/wasm2c_sandbox_wrapper.o |
17 | | - |
18 | | -AR=$(WASI_SDK_PATH)/bin/ar \ |
19 | | -CC=$(WASI_SDK_PATH)/bin/clang \ |
20 | | -CXX=$(WASI_SDK_PATH)/bin/clang++ \ |
21 | | -CFLAGS="--sysroot $(WASI_SDK_PATH)/share/wasi-sysroot" \ |
22 | | -LD=$(WASI_SDK_PATH)/bin/wasm-ld \ |
23 | | -LDLIBS=$(RLBOX_WASM2C_PATH)/c_src/wasm2c_sandbox_wrapper.o \ |
| 16 | +$WASI_SDK_PATH/bin/clang --sysroot $WASI_SDK_PATH/share/wasi-sysroot $RLBOX_WASM2C_PATH/c_src/wasm2c_sandbox_wrapper.c -c -o $RLBOX_WASM2C_PATH/c_src/wasm2c_sandbox_wrapper.o |
| 17 | + |
| 18 | +AR=$WASI_SDK_PATH/bin/ar \ |
| 19 | +CC=$WASI_SDK_PATH/bin/clang \ |
| 20 | +CXX=$WASI_SDK_PATH/bin/clang++ \ |
| 21 | +CFLAGS="--sysroot $WASI_SDK_PATH/share/wasi-sysroot" \ |
| 22 | +LD=$WASI_SDK_PATH/bin/wasm-ld \ |
| 23 | +LDLIBS=$RLBOX_WASM2C_PATH/c_src/wasm2c_sandbox_wrapper.o \ |
24 | 24 | LDFLAGS="-Wl,--export-all -Wl,--stack-first -Wl,-z,stack-size=262144 -Wl,--no-entry -Wl,--growable-table -Wl,--import-memory -Wl,--import-table" \ |
25 | 25 | make |
26 | 26 | ``` |
| 27 | + |
| 28 | +If the Makefile you are modifying produces a binary executable, this will change it produce a Wasm executable instead and that should be it. |
| 29 | + |
| 30 | +If the Makefile produces a static library (e.g., `mylib.a`) instead. You need |
| 31 | +one additional step to make it usable as a Wasm library that can be imported by |
| 32 | +rlbox. |
| 33 | + |
| 34 | +First create a dummy main file somewhere called `dummy-main.c` consisting of |
| 35 | + |
| 36 | +```c |
| 37 | +int main(int argc, char**argv){ |
| 38 | + return 0; |
| 39 | +} |
| 40 | +``` |
| 41 | +
|
| 42 | +Then you can run the following command |
| 43 | +
|
| 44 | +```bash |
| 45 | +$WASI_SDK_PATH/bin/clang --sysroot $WASI_SDK_PATH/share/wasi-sysroot -o mylib.wasm dummy-main.c -Wl,--whole-archive mylib.a -Wl,--no-whole-archive -Wl,--export-all |
| 46 | +``` |
| 47 | + |
| 48 | +This compiles a Wasm file with all symbols from the library included and exported. |
0 commit comments