Handle .init_array link_section specially on wasm by ratmice · Pull Request #121533 · rust-lang/rust (original) (raw)

Given that wasm-ld now has support for .init_array, it appears we can easily implement that section by falling through to the normal path rather than taking the typical custom_section path for wasm.

The wasm-ld appears to have a bunch of limitations. Only one static with the link_section in a crate or else you hit the fatal error in the link above "only one .init_array section fragment supported". They do not get merged.

You can still call multiple constructors by setting it to an array.

unsafe extern "C" fn ctor() {
    println!("foo");
}
#[used]
#[link_section = ".init_array"]
static FOO: [unsafe extern "C" fn(); 2] = [ctor, ctor];

Another issue appears to be that if crate A depends on crate B, but A doesn't call any symbols from B and B doesn't #[export_name = ...] any symbols, then crate B's constructor will not be called. The workaround to this is to provide an exported symbol in crate B.