Static ctors in wasm

David Gileadi gileadisNOSPM at gmail.com
Thu Feb 15 22:04:28 UTC 2024


On 2/15/24 2:45 PM, H. S. Teoh wrote:
> On Fri, Feb 09, 2024 at 01:22:28AM +0000, kinke via Digitalmars-d wrote:
>> On Friday, 9 February 2024 at 01:17:26 UTC, kinke wrote:
>>> Oh well, looks like wasm-ld (i.e., lld) supports exactly the same
>>> magic `__{start,stop}_*` symbols as for ELF:
>>> https://github.com/llvm/llvm-project/issues/55839
>>
>> Oh man, I've even added an LDC test for this, but almost 4 years ago
>> and so forgot. :D
>>
>> https://github.com/ldc-developers/ldc/blob/3c21924705aae83f0c16bfe54e673953671afe58/tests/codegen/wasi.d#L22-L39
> 
> Hmm, I'm running into a problem here.  Following the above link I
> declared the symbols like this:
> 
> ```d
> extern(C) extern __gshared
> {
>      void* __start__minfo;
>      void* __stop__minfo;
> }
> ```
> 
> However, when I check their values at runtime:
> 
> ```d
> export void _wasm_start() // this is called from my JS bridge
> {
>      writefln("__start__minfo=0x%x", &__start__minfo);
>      writefln("__end__minfo=0x%x", &__stop__minfo);
> }
> ```
> 
> (I have a bare-bones version of writefln running for debugging purposes.)
> 
> The output says:
> 
> ````
> __start__minfo=0x0
> __end__minfo=0x%0
> ````
> 
> What gives?
> 
> For reference, I'm using LDC 1.36.0 on x86_64--linux-gnu, with these
> compile flags:
> 
> 	-mtriple=wasm32-unknown-unknown-wasm -Iplatform/wasm -O2 -L-allow-undefined -allinst -linkonce-templates --fvisibility=hidden
> 
> I had --fno-rtti before and it didn't work; I removed it but it still
> made no difference. I guess it's unrelated to RTTI.  Also, I tried
> removing --fvisibility-hidden but it also made no difference.
> 
> What am I doing wrong?

While I have zero knowledge of how LDC handles WASM, I did notice one 
difference between your code and the example in the link: the definition 
at the link has three underscores before the `minfo` whereas your code 
has only two.



More information about the Digitalmars-d mailing list