1 import core.runtime; 2 import core.atomic; 3 import core.stdc.string; 4 import core.sys.posix.dlfcn; 5 6 shared uint tlsDtor, dtor; 7 void staticDtorHook() { atomicOp!"+="(tlsDtor, 1); } 8 void sharedStaticDtorHook() { atomicOp!"+="(dtor, 1); } 9 10 void runTest(string name) 11 { 12 auto h = Runtime.loadLibrary(name); 13 assert(h !is null); 14 15 *cast(void function()*).dlsym(h, "_D9lib_1341414staticDtorHookOPFZv") = &staticDtorHook; 16 *cast(void function()*).dlsym(h, "_D9lib_1341420sharedStaticDtorHookOPFZv") = &sharedStaticDtorHook; 17 18 Runtime.unloadLibrary(h); 19 version (CRuntime_Musl) 20 { 21 // On Musl, unloadLibrary is a no-op because dlclose is a no-op 22 assert(tlsDtor == 0); 23 assert(dtor == 0); 24 } 25 else 26 { 27 assert(tlsDtor == 1); 28 assert(dtor == 1); 29 } 30 } 31 32 void main(string[] args) 33 { 34 auto name = args[0] ~ '\0'; 35 const pathlen = strrchr(name.ptr, '/') - name.ptr + 1; 36 name = name[0 .. pathlen] ~ "lib_13414.so"; 37 38 runTest(name); 39 } 40