WebAssembly/wasi-sdk

Does clang currently support adding the start section?

bathtub-01 opened this issue · 4 comments

The WebAssembly specification defines the start section, which specifies the function to be executed during instantiation. I am trying to implement a physical processor for WebAssembly and I think the start section can be useful during the starting stage.

I have noticed that clang generates a _start function that serves as the entry point for wasm runtimes. However, the compilation output does not include the start section. I am wondering if there is a way to add the start section during compilation, such as by using some compilation flags or __attribute__ sections in C code.

Some discussions on this topic can be found, but they seem to be outdated. I would appreciate any updates or guidance on this matter. Thank you!

We can't use a start section as the C program entrypoint, because imported functions such as WASI functions need access to the module's exports, in particular the "memory" export, and the spec says that those exports aren't available until after the start function returns.

because imported functions such as WASI functions need access to the module's exports

Thank you for the reply. I can get your point, but I don't need standard system interfaces in my project (my design is quite naïve). I am planning to compile C code with a simplified custom crt.

The latest clang documentation does not mention any __attribute__ for my purpose. And I guess I have to add a start section manually in .wat. :)

sbc100 commented

Indeed, wasm-ld will sometimes use the start section for automatically generated linker code (i.e. applying relocations, or initializing memory). See https://github.com/llvm/llvm-project/blob/1077a343911127452615c6f5441c121de06be6d5/lld/wasm/Writer.cpp#L1413-L1415. But it never places user code there, for the reasons Dan already stated).

User startup code (.e.g static initializers) gets called via _start (for applications commands) or via _initialize (for reactors), or by directly calling __wasm_call_ctors yourself. If you want to use the llvm tools (such as wasm-ld) you would need to follow this conventions.

If order to change this we would likely need some kind of change to the wasm core spec (such as some kind second phase start section).

Thank you for your explanation. It seems that I misunderstood the usage of the start section before.