need to modify PE headerrelated code to support TLS, anyone here wanna help me figure out how to do it?

Jonathan Wilson jonwil@tpgi.com.au
Mon Oct 13 02:47:00 GMT 2003
Basicly, the PE format has a DataDirectory array that points at special 
structures in the PE file.
Acording to MSDN[1], entry 9 (aka IMAGE_DIRECTORY_ENTRY_TLS) of this array 
points to an IMAGE_TLS_DIRECTORY structure.
In the implementation I am working on (which is the same as how both 
Microsoft Visual C++ and Open Watcom do it), here is what it should do/how 
it will work:
1.somehow, the compiler/linker/whatever will identify that the program is a 
multi-thread program and/or that it has Thread Local Storage (perhaps via 
the -mthreads parameter or via a new -mtls parameter or something)
2.when it identifies this, it will pull in tlssup.o and atlssup.o into the link
3.it should contain code in the linker script like so:
   .tls : {
	*(.tls)
	*(.tls$)
	SORT (.tls$*)
   }
and 4.somewhere in the generation code for the exe file, it should do the 
following:
A.search for the symbol __tls_used (which is defined as _tls_used in the 
tlssup.c file I am working on)
B.if found, it should acquire the Linear Address of that symbol.
and C.it should set the RVA field of the IMAGE_DIRECTORY_ENTRY_TLS entry to 
this address.
As for the size field, I am not 100% clear on what it should be set to, I 
am trying to figure that out.

item 1 will probobly be done via a modification to the specs file that gets 
used when GCC is build as MingW GCC or Cygwin GCC.
item 2 will probobly be done via the same thing (tlssup.o and atlssup.o 
will come from MingW-Runtime)
item 3 will come from the win32 PE linker script
item 4 will require changes to LD (and is where I need the help)

So, can someone help me figure out how to implement the stuff given in point 4?

Once it is possible to take visual C++ obj files that use TLS and link it 
ith LD and MingW-Runtime and w32api and get a working exe file (something 
you can already do for visual C++ obj files that dont use TLS), then we can 
add support to GCC itself for TLS.

[1]
see:
http://msdn.microsoft.com/msdnmag/issues/02/02/PE/default.aspx
and
http://msdn.microsoft.com/msdnmag/issues/02/03/PE2/default.aspx



More information about the Binutils mailing list