Skip to content

Commit

Permalink
Merge pull request #7 from robinyuan1002/main
Browse files Browse the repository at this point in the history
Update buildGlibcWASM.md
  • Loading branch information
yzhang71 authored Jul 21, 2024
2 parents 7aa8d77 + 5a927db commit bb3c125
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 51 deletions.
12 changes: 10 additions & 2 deletions docs/buildGlibcWASM.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ docker pull ubuntu:22.04
docker run -it ubuntu
```

Update apt and apt-get

```
cd home
apt-get update
apt update
```

We need glibc from lind-wasm, if you did it already then ignore it
[https://github.com/Lind-Project/lind-wasm.git](https://github.com/Lind-Project/lind-wasm.git)

Expand All @@ -37,7 +45,7 @@ First we should install some apt essential
apt install build-essential
```

Second we need to compile wasm-sdk. Before this we need to access to wasm-sdk, you need to replace `cd wasi-sdk` to what your need
Second we need to compile wasi-sdk. Before this we need to access to wasi-sdk, you need to replace `cd wasi-sdk` to what your need

```
cd wasi-sdk
Expand All @@ -52,7 +60,7 @@ if terimal tells you, you don't have "make" or thing like that just use "apt-get
apt-get install make
```

Third switch branch which is related with github(cd to lind-wasm/glicb). Find out which branch you are on currently and switch to branch "main"
Third switch branch which is related with github(cd to lind-wasm/glibc). Find out which branch you are on currently and switch to branch "main"

```
git branch -a
Expand Down
109 changes: 61 additions & 48 deletions docs/wasmCompile.md
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!`.
2 changes: 1 addition & 1 deletion mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ nav:
- understandTls.md
- Building WASM glibc: buildGlibcWASM.md
- Compile safeposix-rust: compile-safeposix-rust.md
- Compiling C to WASM: wasmCompile.md
- Compiling Wasmtime from Source: compile-wasmtime.md
- Compiling C to WASM: wasmCompile.md
- Debugging WASM module with Wasmtime: wasmtime-debug.md
- How to add docs: writeDoc.md
- Team: team.md
Expand Down

0 comments on commit bb3c125

Please sign in to comment.