1import core.runtime;
2import core.atomic;
3import core.stdc.string;
4import core.sys.posix.dlfcn;
5
6shared uint tlsDtor, dtor;
7void staticDtorHook() { atomicOp!"+="(tlsDtor, 1); }
8void sharedStaticDtorHook() { atomicOp!"+="(dtor, 1); }
9
10void 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    assert(tlsDtor == 1);
20    assert(dtor == 1);
21}
22
23void main(string[] args)
24{
25    auto name = args[0] ~ '\0';
26    const pathlen = strrchr(name.ptr, '/') - name.ptr + 1;
27    name = name[0 .. pathlen] ~ "lib_13414.so";
28
29    runTest(name);
30}
31