Stream: general

Topic: Compile Assembly code into WASM


view this post on Zulip Dennis Zhang (Apr 25 2024 at 20:32):

Hi community, I have a question. If my C project contains assembly code (i.e, .S files), can I using Clang to compile my C project into WASM? Any help will be appreciated!

Best,

view this post on Zulip fitzgen (he/him) (Apr 25 2024 at 21:33):

you can link .S files into Wasm binaries with lld (and therefore I assume with clang) but the .S file needs to be using the wasm text format, not e.g. x86 assembly

view this post on Zulip bjorn3 (Apr 26 2024 at 09:31):

You first have to convert the .S file to a wasm object file using an assembler. Lld doesn't directly accept assembly code.

view this post on Zulip Dennis Zhang (Apr 26 2024 at 14:33):

Thanks so much for the responds! I am really appreciated. BTW, could you guys provide some links that I can take a look of the wasm assembly text format?

view this post on Zulip fitzgen (he/him) (Apr 26 2024 at 16:27):

https://webassembly.github.io/spec/core/text/index.html is the specification of the text format

you can use wasm-tools print and wasm-tools parse to assemble and disassemble: github.com/bytecodealliance/wasm-tools

CLI and Rust libraries for low-level manipulation of WebAssembly modules - GitHub - bytecodealliance/wasm-tools: CLI and Rust libraries for low-level manipulation of WebAssembly modules

view this post on Zulip fitzgen (he/him) (Apr 26 2024 at 16:28):

the official test suite has more examples of the text format (although it is in the extended .wast format rather than .wat, which allows defining multiple modules and assertions and such): https://github.com/WebAssembly/testsuite/

Mirror of the spec testsuite. Contribute to WebAssembly/testsuite development by creating an account on GitHub.

view this post on Zulip Coulson Liang (Apr 26 2024 at 19:48):

Hi guys, I'm also a newbee to WASM libcs, and I'm kind of confused by the assembly code in wasi-libc. For example, longjmp.s, I thought WASM is a architecture agnostic format, but in this part of wasi-libc, the assembly file

.global _longjmp
.global longjmp
.type _longjmp,@function
.type longjmp,@function
_longjmp:
longjmp:
    mov  4(%esp),%edx
    mov  8(%esp),%eax
    cmp       $1,%eax
    adc       $0, %al
    mov   (%edx),%ebx
    mov  4(%edx),%esi
    mov  8(%edx),%edi
    mov 12(%edx),%ebp
    mov 16(%edx),%esp
    jmp *20(%edx)

is included as a part of musl. Then how is this being compiled as a part of the wasm sysroot? I'm kind of interested in this compilation.

view this post on Zulip Chris Fallin (Apr 26 2024 at 19:51):

It shouldn't be; probably that's general code in musl in an architecture-specific part (for x86) that is not included in the wasm build

view this post on Zulip Chris Fallin (Apr 26 2024 at 19:51):

(it's common for programs that require assembly bits, either for performance or for implementing low-level things, to have separate implementations per architecture and select only one during build)

view this post on Zulip Joel Dice (Apr 26 2024 at 20:05):

If you look at the wasi-libc Makefile, you'll see that not all the files in the source tree are included as part of the build, and I'd expect that longjmp.s is not included.

view this post on Zulip Joel Dice (Apr 26 2024 at 20:06):

I believe the approach of importing all of musl but only using part of it is motivated by a desire to make upgrading to later musl versions easier.

view this post on Zulip Coulson Liang (Apr 26 2024 at 20:08):

Ohoh I see, so the actual source for wasi-libc shouldn't include stuff like x86 asm, or at least not necessarily needed. Yeah, their approach definitely makes upgrading for future musl versions easier, thanks!


Last updated: Dec 23 2024 at 12:05 UTC