# Introduction

Thread Local Storage, or TLS, is a Windows mechanism that allows threads to have their own localized storage space within a process, and includes functions like TlsAlloc and TlsFree to facilitate this.

TLS callbacks are an integral part of the TLS architecture. These special functions are called upon the creation of new threads within a process, and are primarily used as a way for developers to set up a thread’s local storage in a predetermined way.

These callbacks are stored within the PE file’s TLS directory, a data directory that can be accessed via an offset found inside the optional header.\


<figure><img src="https://193590189-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FD64jwLvItf6a00Ucqelv%2Fuploads%2FaUwCTU6b2jm3WuhHxkJQ%2Fdiagram.drawio.png?alt=media&#x26;token=a6958f07-75c1-424f-af6b-81e672e075cc" alt="" width="337"><figcaption></figcaption></figure>

\


Each TLS directory contains an array of pointers, which can be accessed via it’s AddressOfCallbacks member.

```cpp
PIMAGE_TLS_DIRECTORY    pImgTlsDirectory = 
    (PIMAGE_TLS_DIRECTORY)(pPeBaseAddress + PeHdrs.pEntryTLSDataDir->VirtualAddress);

// Get the address of the TLS Callbacks from the TLS Directory.
PIMAGE_TLS_CALLBACK*    pImgTlsCallback = 
    (PIMAGE_TLS_CALLBACK*)(pImgTlsDirectory->AddressOfCallBacks);

```
