ldc-developers / ldc

The LLVM-based D Compiler.
http://wiki.dlang.org/LDC
Other
1.22k stars 262 forks source link

`-defaultlib=phobos2-ldc-shared,druntime-ldc-shared` breaks static constructor #4380

Open FeepingCreature opened 1 year ago

FeepingCreature commented 1 year ago

Given two files, "a.d":

import std.stdio;
static this() { writefln!"static this()"; }

And "b.d":

import a;
void main() { }

When I build a binary

ldc2 a.d b.d -oftest -defaultlib=phobos2-ldc-shared,druntime-ldc-shared
./test

Then the static constructor is not called.

If I call ldc2 with -link-defaultlib-shared instead, it works.

When module constructors are silently not called, the bug can take a long time to identify, and the resolution yet longer. If -defaultlib=...-shared can't work on its own, then LDC should just error if a -shared lib is passed to -defaultlib.

kinke commented 1 year ago

From the v1.29 changelog:

When linking manually (not via LDC) against shared druntime, it is now required to link the bundled lib/ldc_rt.dso.o[bj] object file into each binary. It replaces the previously Windows-specific dso_windows.obj. (#3850)

To be precise, this is required when not using -link-defaultlib-shared (which additionally takes care of setting a default rpath when linking). I didn't expect anyone to link like you did.

FeepingCreature commented 1 year ago

I just followed the error messages from DMD's commandline. (-defaultlib=libphobos2.so). With other flags, LDC tries to minimize the difference in invocation to DMD, like -of.

Ie. I started with dmd -defaultlib=libphobos2.so, changed it to ldc2 -defaultlib=libphobos2.so, then fixed issues as they arose. This did not lead me to -link-defaultlib-shared, but it did lead me to this bug. :)