[LLVMdev] Implement implicit TLS on Windows - need advice

Kai kai at redstar.de
Sun Dec 4 09:18:45 PST 2011


Hi!

LLVM currently does not implement the implicit TLS model on Windows. 
This model is easy:

- a thread local variable ends up in the .tls section
- to access a thread local variable, you have to do
   (1) load pointer to thread local storage from TEB
       On x86_64, this is gs:0x58, on x86 it is fs:0x2C.
   (2) load pointer to thread local state. In general, the index is 
stored in variable _tls_index. For a .exe, _tls_index is always 0.
   (3) load offset of variable to start of .tls section.
   (4) the thread local variable can now accessed with the add of step 2 
and 3.

For x86_64, something like the following should be generated for the 
tls1.ll test case:

(1)     mov     rdx, qword [gs:abs 58H]
(2)     mov     ecx, dword [rel _tls_index]
         mov     rcx, qword [rdx+rcx*8]
(3)     mov     eax, .tls$:i
(4)     mov     eax, dword [rax+rcx]
         ret

(See the PECOFF spec, chapter 5.7 and http://www.nynaeve.net/?p=185 for 
reference.)

I tried to implement this. With the attached patch tls1.patch, a thread 
local variable ends up in the .tls section. This looks fine.
With the second patch tls2.patch I try to implement the code sequence. 
Here I have a couple of questions:

- To get the offset to start of .tls section, I have created a new 
MachineOperand flag. Is this the right approach? If yes then I need a 
hint where to implement this in the WinCOFFObjectWriter.
- How can I code the load of variable _tls_index in SelectionDAG? I have 
some trouble using DAG.getExternalSymbol and then loading the value.

Thanks for your help.

Kai
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: tls2.patch
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20111204/2878ec05/attachment.ksh>
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: tls1.patch
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20111204/2878ec05/attachment-0001.ksh>


More information about the llvm-dev mailing list