Bug 17707 – unimported modules in libraries do not have their module constructors run

Status
RESOLVED
Resolution
WONTFIX
Severity
major
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
x86_64
OS
All
Creation time
2017-07-31T16:44:44Z
Last change time
2023-04-22T19:57:01Z
Assigned to
No Owner
Creator
John Colvin

Comments

Comment #0 by john.loughran.colvin — 2017-07-31T16:44:44Z
First with object files: % ls app.d foo.d % cat app.d void main() { import core.stdc.stdio; puts("and from here"); } % cat foo.d static this() { import core.stdc.stdio; puts("hello"); } % dmd -c app.d foo.d % ls app.d app.o foo.d foo.o % gcc app.o foo.o -lphobos2 % ./a.out hello and from here Everything looks fine there % ar rcs foo.a foo.o % gcc app.o foo.a -lphobos2 % ./a.out and from here the module constructor from foo.d has gone missing! test on macOS and linux, with many different permutations. Splitting the object file back out of the library makes it work again, so no information is lost. On macOS it was easy to discover that the result of getsectbynamefromheader_64 for __DATA __minfodata is missing an entry for foo in the library case, but not in the object case.
Comment #1 by john.loughran.colvin — 2018-08-14T13:25:56Z
The same behaviour is observed when the library is made with dmd, when the linking is done with dmd, etc... Essentially dmd seems to be doing something different with the __modctor it gets from an object file or a library
Comment #2 by alphaglosined — 2023-04-21T19:11:33Z
This is the expected behavior of static libraries. The linker is eliding object files that are contained within a static library when it sees that the object file has no references to it. This behavior also exists on Windows.
Comment #3 by kinke — 2023-04-22T19:57:01Z
Yep, this is expected default behavior. But linkers support force-pulling all object files from static libs: * ld: --whole-archive libfoo.a --no-whole-archive * Apple ld64: -force_load libfoo.a * MS link.exe: /WHOLEARCHIVE:foo.lib