Windows PE TLS Callbacks Execute Code Before the Program Entry Point
Attack Tradecraft · 4 sources · Updated 3mo ago · First seen Sep 5, 2026

A Windows PE demonstration shows that Thread Local Storage (TLS) callbacks execute during process initialization before the executable entry point and C main. MinGW-w64 callback pointers placed in .CRT$XLB and .CRT$XLBB invoke two callbacks in order, producing 12 when both return normally. The first example ran raw x64 MessageBox payload through EnumDesktopsA, but its thread-exit behavior could end the initializing thread before the second callback and main executed; a revised payload restored the stack and returned normally, allowing initialization to complete.
TLS callbacks are legitimate PE and runtime infrastructure, but they can conceal behavior from analysts who begin debugging only at main. Defenders should inspect the PE TLS directory and callback-address array, then assess the callback code and its execution effects rather than treating TLS callback presence alone as malicious. Because this code runs amid loader initialization, unsafe operations and synchronization can also cause deadlocks or crashes under the loader lock; Microsoft recommends deferring complex initialization outside loader-managed entry paths.


