Invalid code generated for extern thead_local variables
I'm closing this, as invalid, for the following reasons:—
In any case, since you are using tools originating from some other project, (which, BTW, is abusing our registered trademark, without authorization), we cannot help you.
Compiler produces some strange code when using C++11 global thread local variables. The produced binary also permamently crashes when built in some circumstances. The problem is visible on all optimization levels above -O0, the binary compiled with -O0 looks good.
I use MSYS2 build environment with gcc 8.3.0 installed. Extract the attached minimal example and run the following commands:
Then inspect the produced code with the GDB:
The strange thing is instruction at <+9> "lea -0x703813bb(%rip),%rax" which calculates some address, the result is tested against zero at <+16> and call to this address is executed at <+21> in case it is non-zero. In this particular example the result is zero, so it does not crash. But in my case the result was not zero and still invalid which causes a crash. Unfortunately my example is not easy reproducible, it probably was triggered after I statically linked my DLL with large library (ffmpeg) and the calculated address became non-zero. Besides that even in the attached example the idea behind this strange address manipulation is not clear and looks like some code optimization bug.
Compare this code with one compiled with -O0:
It is seen that TLS wrapper is not inlined in this case and instead of calculating some address, some real variable read is generated at <+8> which looks more correct. The calling address at <+20> is probably replaced with a correct one when runtime-linked to some process context. In my case it looks like this:
And it does not crash and works as expected, and crashing if compiled with -O1, -O2 or -O3. It would be nice to know what is the idea behind the address calculation instead of variable reading in debug build, which also significantly changes application logic between release and debug build which is very suspicious. For now, it looks like some optimization bug.
I also tried to compile the sample with clang, it failed to link: