Assigning function to wasm `start` section

In WebAssembly, there is a start section which defines a function that is called during instantiation of the module. I'm trying to use this to initialize statics (purely to see if I can) but I can't figure out how to hook a function to this. I've tried #[link_section = "start"] but that didn't do anything.

Is there any way to do this?

Isn't start meant to be the name of the function, not the section it is in?

pub extern "C" fn start() {

I also don't think start is guaranteed to be executed when a WebAssembly module is instantiated. My understanding is that was something you needed to do manually and it's just that wasm_bindgen calls start() for you when you import its JavaScript glue code.

From the spec:

The start component of a module declares the function index of a start function that is automatically invoked when the module is instantiated, after tables and memories have been initialized.

Note: The start function is intended for initializing the state of a module. The module and its exports are not accessible before this initialization has completed.

start is the name of the section that defines the function, the name of the function is irrelevant

You may be thinking of WASI's _start function, which is the name of an export and is invoked manually

Technically it doesn't have a name (only custom sections can have a name, #[link_section = "start"] probably results in a custom section named start.) and the type is 8 which indicates a start section.

Based on ⚙ D40759 [WebAssembly] Implement @llvm.global_ctors and @llvm.global_dtors it seems that a start function is automatically generated that calls all global constructors. There doesn't seem to be any way to override this.

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.