Firm Bugtracker - libFirm
Viewing Issue Advanced Details
41 backend minor always 2009-03-03 01:34 2017-05-08 16:14
confirmed 1.16.0  
0000041: TLS does not work on Cygwin
In MinGW mode, libFirm generates invalid assembler code for testcase firm_testsuite/opt/thread.c which uses "__thread" modifiers (invalid section declaration: '.section .tbss,"awT",@nobits').
Just removing ",@nobits" results in the GAS warning "unknown section attribute 'T'".
The generated code tries to get a TLS pointer from gs:[0] which is simply not valid on Windows.

Trying to build this testcase with gcc 3.4.4 on Cygwin results in this error messages:
opt/thread.c:5: error: thread-local storage not supported for this target
opt/thread.c:6: error: thread-local storage not supported for this target
Issue History
2009-03-03 01:34 moritz New Issue
2009-03-03 21:16 Matze Note Added: 0000061
2009-03-03 21:16 Matze Status new => confirmed
2009-03-03 21:20 Matze version => 1.16.0
2009-03-04 12:26 moritz Note Added: 0000066
2009-03-04 17:11 moritz Note Added: 0000067
2017-04-07 05:49 travm1 Note Added: 0000288
2017-05-08 16:14 waldgrasgeniesser Note Added: 0000293

2009-03-03 21:16   
so what's the correct solution, disallowing thread local storage on mingw target?
2009-03-04 12:26   
Looking at how Visual C compiles thread.c it uses a special .tls PE section:

PUBLIC _tls_i
PUBLIC _tls_j
_tls_i DD 0aH DUP (?)
_tls_j DD 05H
PUBLIC _test
EXTRN __tls_array:DWORD
EXTRN __tls_index:DWORD


; 9 : tls_j = i;

    mov eax, DWORD PTR __tls_index
    mov ecx, DWORD PTR fs:__tls_array
    mov edx, DWORD PTR [ecx+eax*4]
    mov eax, DWORD PTR _i$[ebp]
    mov DWORD PTR _tls_j[edx], eax

More info on this can be found at: [^]

According to [^] and [^] binutils already have some support for the .tls table ("Fortunately, the linker already supports setting the TLS directory entry in the PE header if a symbol named "__tls_used" exists.").
Sadly I didn't find a proper example on how an according .s file should look like. Trying

    .section .tls
.globl __tls_used
    .long 2
.globl _tls_i
    .p2align 2
    .space 40
.globl _tls_j
    .p2align 2
    .long 0x00000005

causes an segmentation fault in ld...
2009-03-04 17:11   
The definition of __tls_used was missing. I fixed the bug in ld nonetheless ( [^]
Now __tls_used must only be filled with some sensible data (IMAGE_TLS_DIRECTORY). A C example can be found here: [^]
2017-04-07 05:49   
The wiki page for Cygwin has some interesting points: [^]
2017-05-08 16:14   
I don't care that you broke your elbow.