This was originally posted on GitHub here: https://github.com/WebAssembly/lld/pull/15
Now that the GitHub repo has been archived, I can't post a reply there! So I'm opening this for discussion despite Sam's lack of enthusiasm...
I said:
The --entry option is there to allow designating the entry-point symbol, but it isn't wired in!
LLD needs to actually set the entrypoint on the Wasm output, using the "start" section.
Sam said:
The reason we do this right now is for parity the expectations of emscripten and also wasm.js in the musl repo (which we use for testing):
https://github.com/jfbastien/musl/blob/wasm-prototype-1/arch/wasm32/wasm.jsBoth these loaders expect to be able to run the main function explicitly and get the return code from it.
There has been some discussion about the role of the start section and how the linker should use it. I think it was originally intented to tune the C/C++ internalizer functions but not run main(). This is certainly one concievable option, but it would involve the linker generating said function, or assuming its name (in musl this i called __libc_start_init for example).
For now I think it makes sense to keep it empty and let the embedder choose to run what it wants when it whats rather than on instantiate.
My response:
I see where you're coming from... but surely the whole point of the "entrypoint" argument is to determine which symbol (if any) should be set as the Wasm module's entry point.
If in fact you'd like it so that the default is not to have an entrypoint, then we could easily change the default to be --entry '' (empty string). But for those of us who do want an entrypoint on our Wasm modules, it would be possible to do that.
I agree that we don't really want "main" to be run as part of the Wasm entrypoint.
Maybe the long-term solution is to fiddle with Musl, to create a new function "_start_wasm" that calls both __init_libc and __libc_start_init (but not main), and make that function name part of the Wasm "linkage conventions".
That would be basically this PR, plus a one-liner to change "_start" to "_start_wasm".
Finally, the reason why I'm interesting in actually using Wasm's "start" functionality is because it seems to me to be a better form level of encapsulation. No-one really wants it to be possible to touch global variables that aren't initialised, I think it is preferable that the WebAssembly instantiation fails if the globals can't be initialised, and make it impossible to invoke exports on an uninitialised module.
Why is this needed? Shouldn't allow-undefined be enough here?