-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from robinyuan1002/main
Update buildGlibcWASM.md
- Loading branch information
Showing
3 changed files
with
72 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,68 @@ | ||
# Troubleshooting Compiling WASM | ||
|
||
**Assuming using `clang-18` provided by the `wasi-sdk-22.0`.** | ||
## Compiler version | ||
`clang-16` and `clang-18` can be use, if you use other version make sure it works. I will use `clang-16` as an example. | ||
|
||
## Frequently Used Flags | ||
- `--target=wasm32-unkown-wasi` for compiling to wasm | ||
- `-c` for compiling as a library without main executable | ||
- `-pthread` then the compiler to understand `__tls_base` etc | ||
- `--sysroot` specifying the stand library path | ||
|
||
## Inspecting an Object File | ||
Use `wasm-objdump -x` to show all functions, globals, etc included in the object file. Use `-d` to disassemble and see text instructions. | ||
|
||
## Locating Type Mismatch Error | ||
Even successfully compile a source into .wasm, a typical error due to mismatch will manifest during secondary compilation, like this | ||
``` | ||
root@2299d4e20d1f:/lind-glibc/replace-sysroot/test# /wasmer/target/debug/wasmer run newhello.wasm | ||
error: Unable to compile "newhello.wasm" | ||
╰─▶ 1: Validation error: type mismatch: values remaining on stack at end of block (at offset 0x26a) | ||
``` | ||
One can also verify and get the same error by `wasm2wat`, which gives a more detailed message. To debug, we can use `wasm-objdump -x` to see the function types, a typical output is this | ||
|
||
``` | ||
Section Details: | ||
Type[16]: | ||
- type[0] (i32, i32) -> i32 | ||
- type[1] (i32) -> nil | ||
- type[2] (i32, i32, i32, i32, i32, i32, i32, i32) -> i32 | ||
- type[3] (i32, i64, i64, i64, i64, i64, i64, i64) -> i32 | ||
- type[4] () -> nil | ||
- type[5] () -> i32 | ||
- type[6] (i32, i32, i32) -> i32 | ||
- type[7] (i32) -> i32 | ||
- type[8] (i32, i32, i32, i64) -> i32 | ||
- type[9] (i32, i32, i32) -> nil | ||
- type[10] (i32, i32, i32, i32) -> i32 | ||
...... | ||
Import[1]: | ||
- func[0] sig=3 <__imported_wasi_snapshot_preview1_lind_syscall> <- wasi_snapshot_preview1.lind_syscall | ||
Function[92]: | ||
- func[1] sig=4 <_start> | ||
- func[2] sig=4 <__wasm_call_dtors> | ||
- func[3] sig=5 <__original_main> | ||
- func[4] sig=6 <__libc_write> | ||
- func[5] sig=7 <__brk> | ||
- func[6] sig=7 <__sbrk> | ||
- func[7] sig=0 <__clock_gettime64> | ||
- func[8] sig=0 <__GI___munmap> | ||
- func[9] sig=6 <__GI___madvise> | ||
- func[10] sig=6 <__open64_nocancel> | ||
...... | ||
``` | ||
|
||
Note that the `Type` section defines all the function args/ret types, and for each function, the `sig=X` just means the function has `type[X]`. | ||
## Install clang-16 | ||
Download `clang-16` from this link | ||
|
||
``` | ||
https://github.com/llvm/llvm-project/releases/download/llvmorg-16.0.4/clang+llvm-16.0.4-x86_64-linux-gnu-ubuntu-22.04.tar.xz | ||
``` | ||
|
||
Unzip clang+llvm-16.0.4-x86_64-linux-gnu-ubuntu-22.04.tar.xz | ||
|
||
``` | ||
tar -xf clang+llvm-16.0.4-x86_64-linux-gnu-ubuntu-22.04.tar.xz | ||
``` | ||
|
||
We move `libclang_rt.builtins-wasm32.a` from `lind-wasm/glibc/wasi` to `/home/clang+llvm-16.0.4-x86_64-linux-gnu-ubuntu-22.04/lib/clang/16/lib/` using | ||
|
||
``` | ||
mv /home/glibc/wasi /home/clang+llvm-16.0.4-x86_64-linux-gnu-ubuntu-22.04/lib/clang/16/lib | ||
``` | ||
|
||
Modify the file `stubs.h` located in `lind-wasm/glibc/target/include/gnu` to | ||
|
||
``` | ||
/* This file is automatically generated. | ||
This file selects the right generated file of `__stub_FUNCTION' macros | ||
based on the architecture being compiled for. */ | ||
//#if !defined __x86_64__ | ||
//# include <gnu/stubs-32.h> | ||
//#endif | ||
#if defined __x86_64__ && defined __LP64__ | ||
# include <gnu/stubs-64.h> | ||
#endif | ||
#if defined __x86_64__ && defined __ILP32__ | ||
# include <gnu/stubs-x32.h> | ||
#endif | ||
``` | ||
|
||
## Compile C to wasm | ||
If you don't need to use glibc, modify `add.c` to the c file you want to compile and `add.wasm` is the wasm file you get(you can modify add to the name you want). Modify `/home/clang+llvm-16.0.4-x86_64-linux-gnu-ubuntu-22.04/bin/clang-16` to you compiler's path | ||
|
||
``` | ||
/home/clang+llvm-16.0.4-x86_64-linux-gnu-ubuntu-22.04/bin/clang-16 --target=wasm32 -nostdlib -Wl,--no-entry -Wl,--export-all -o add.wasm add.c | ||
``` | ||
|
||
If you need to use glibc(such as printf, printf.c is located in lind-wasm/lind-wasm-tests), modify `printf.c` to the c file you want to compile and `printf.wasm` is the wasm file you get(you can modify printf to the name you want). Modify `/home/clang+llvm-16.0.4-x86_64-linux-gnu-ubuntu-22.04/bin/clang-16` to you compiler's path | ||
|
||
``` | ||
/home/clang+llvm-16.0.4-x86_64-linux-gnu-ubuntu-22.04/bin/clang --target=wasm32-unknown-wasi --sysroot /home/lind-wasm/glibc/sysroot printf.c -g -O0 -o printf.wasm | ||
``` | ||
|
||
## Run wasmtime | ||
Run the `.wasm` file, modify the wasmtime path to your own | ||
|
||
``` | ||
/home/lind-wasm/wasmtime/target/debug/wasmtime add.wasm | ||
``` | ||
|
||
For printf.c, you should get `Hello World!`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters