Segfault when adding a static destructor in druntime/src/rt/sections_elf_shared.d

RazvanN razvan.nitu1305 at
Tue Jan 8 12:54:11 UTC 2019

Hi all,

I am working on issue 14650 [1] and I would like to implement a 
solution where static destructors are destroying global 
variables. However, I have the following problem in 

struct ThreadDSO
     DSO* _pdso;
     static if (_pdso.sizeof == 8) uint _refCnt, _addCnt;
     else static if (_pdso.sizeof == 4) ushort _refCnt, _addCnt;
     else static assert(0, "unimplemented");
     void[] _tlsRange;
     alias _pdso this;
     // update the _tlsRange for the executing thread
     void updateTLSRange() nothrow @nogc
         _tlsRange = _pdso.tlsRange();
Array!(ThreadDSO) _loadedDSOs;

For this code, I would have to create the following static 

static ~this() { _loadedDSOs.__dtor(); }

Because Array defines a destructor which sets its length to 0.

However this code leads to segfault when compiling any program 
with the runtime (betterC code compiles successfully). In my 
attempt to debug it, I dropped my patch and added the above 
mentioned static destructor manually in druntime which lead to 
the same effect. Interestingly, _loadedDSOs.__dtor runs 
successfully, the segmentation fault comes from somewhere higher 
in the call path (outside of the _d_run_main function (in 
rt/dmain2.d)). I'm thinking that the static destructor somehow 
screws up the object which is later referenced after the main 
program finished executing. Does someone well versed in druntime 
has any ideas what's happening?



More information about the Digitalmars-d-learn mailing list