`#![no_main]` wasi program imports main?

While I was poking around inside wasm32-wasi programs generated by Rust, I noticed that the incredibly simple program

#![no_main]

curiously imports "env" "main", which is (indirectly) called by _start, in place of where the crate's usual main function would be called

This behavior is pretty strange to me, I'd expect it to do one of the following:

  • Create a empty main function (which it looks like it does, in main.1 and __main_void, but those ultimately call the imported main)
  • Simple do nothing where main would normally be called
  • Have a completely empty _start

But it doesn't do any of those, and instead yields its execution path to the host environment. I guess if that import mapped to another non-wasi module's main function, it could be used to wrap it to be called like it was wasi-compliant

Any ideas why it does this?

Full output .wat (gist)

#![no_main] still requires you to provide your own #[no_mangle] fn main(argc: c_int, argv: *const *const c_char) -> c_int as required by libc for executables. It only disables the automatic geneneration of this function to initialize libstd and then call your own fn main().

1 Like

That makes sense, I'm just confused why it decides to import a main function instead if you don't do that.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.