When we remove the CRT we broke the static initializer in the code.
Now the static variables are not initialized anymore.
Also setting the _tls_index to 0 is wrong and will result in static variables inside function to not be initialized.
When there is a static variable inside a function the _tls_index get look to see if that variable is initialized or not.
Indeed _tls_index work with _Init_thread_header to ensure that the given static var would be initialized one.
I figured out this because in my code base I have the fix for the static variable initialization (using __xc_a and initerm) but when I was implementing the debug system, the first approach was to use static variables and the DEBUGInitializeValue function was never called.
Upon inspecting the CRT source code I'm now able to make that statics works as well.
Here are the gist of my msvc CRT replacament if any one is interested.
PS: This code work only on 64bit builds
PS2: new link now it is fixed there was a stupid error in the _Init_thread_footer func.
Best regards, Alberto.