Add support for TLS callbacks on Windows. (#15423)
* Add support for TLS callbacks on Windows.
Mono runtime depends on DllMain to be called by OS in order to correctly
detach threads from the runtime. If this doesn't happen,
threads from native thread pools (not owned by runtime) and attached
using native->managed callback, won't detach resulting in attached
threads no longer running. Since threads are still attached
to runtime, next GC will try to suspend the thread and that will fail
and that in turn will put the state machine in an incorrect state,
bringing down the runtime at next GC (when the thread state is revisited).
This problem is currently handled by DllMain, but since DllMain only exist
in DLL's, statically link Mono runtime will get us into the above scenario.
This commit add support to hook up a TLS callback using the same mechanism
used by MSVC linker and c-runtime (also available under MINGW). The
callback will be included in a section/segment of an object file and included
in final image (DLL or EXE) by linker and called by OS loader, solving the
problem with static linked Mono runtime not being able to detach terminating
threads.
The commit keeps current DllMain method when building a DLL but
includes a mechanism making sure we only use one of the callback techniques
at runtime, if both have been included in final image. This will simplify the build
of libmini since we can always build the object including the callback, regardless
how the library will be consumed.