[thread] Any way to avoid creating the .tls section on Windows

Yet another bizarre question... (the easy ones have all been answered by now) This is a Windows-specific question. We build a dll that is loaded with the LoadLibrary function. Indirectly (through a third-party library), that dll links against the boost thread library. For reasons that I won't go into, the existence of the .tls section in the resulting binary is problematic for us. I looked at the code and found this comment inside the file tss_pe.cpp: //The following line has an important side effect: //if the TLS directory is not already there, it will //be created by the linker. In other words, it forces a tls //directory to be generated by the linker even when static tls //(i.e. __declspec(thread)) is not used. //The volatile should prevent the optimizer //from removing the reference. I'm not much of a Windows guy and I currently don't understand what is going on in tss_pe.cpp. I did a search of the boost sources and I don't find __declspec(thread) used anywhere, which was encouraging. I'm wondering two things: 1) Why does the code force a .tls section to be created, even when static tls is not being used? 2) If we know that static tls is not used in any of the code, is it safe to remove the .tls section from the PE file? (We know a lot about manipulating PE files, so definitely know how to do this.) We have experimented with removing the .tls section by hand and running the dll, and everything seems to work, but I'm really hoping that Anthony Williams might take pity on me and provide some information. Thanks, Rush

Rush Manbert <rush@manbert.com> writes:
1) Why does the code force a .tls section to be created, even when static tls is not being used?
The static TLS section is used to register the on-thread-exit callbacks. These are used to ensure cleanup of thread-local data belonging to boost.thread on threads not wrapped in a boost::thread object (such as the main thread, or any threads launched with CreateThread or _beginthread)
2) If we know that static tls is not used in any of the code, is it safe to remove the .tls section from the PE file? (We know a lot about manipulating PE files, so definitely know how to do this.)
Only if you don't use thread_specific_ptr or this_thread::get_id() or at_thread_exit (and possibly something else, I can't remember if that's the full list) from a thread not started with boost::thread (such as the main thread). Anthony -- Author of C++ Concurrency in Action http://www.stdthread.co.uk/book/ just::thread C++0x thread library http://www.stdthread.co.uk Just Software Solutions Ltd http://www.justsoftwaresolutions.co.uk 15 Carrallack Mews, St Just, Cornwall, TR19 7UL, UK. Company No. 5478976
participants (2)
-
Anthony Williams
-
Rush Manbert