Skip to content

Commit e75438a

Browse files
committed
Execute thread-local-storage callbacks (GitHub #11).
Pull request by @glysbaysb, cleaned up and adjusted to match code style.
1 parent 147fdde commit e75438a

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

MemoryModule.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,24 @@ FinalizeSections(PMEMORYMODULE module)
184184
#endif
185185
}
186186

187+
static void
188+
ExecuteTLS(PMEMORYMODULE module)
189+
{
190+
unsigned char *codeBase = module->codeBase;
191+
192+
PIMAGE_DATA_DIRECTORY directory = GET_HEADER_DICTIONARY(module, IMAGE_DIRECTORY_ENTRY_TLS);
193+
if (directory->VirtualAddress > 0) {
194+
PIMAGE_TLS_DIRECTORY tls = (PIMAGE_TLS_DIRECTORY) (codeBase + directory->VirtualAddress);
195+
PIMAGE_TLS_CALLBACK* callback = (PIMAGE_TLS_CALLBACK *) tls->AddressOfCallBacks;
196+
if (callback) {
197+
while (*callback) {
198+
(*callback)((LPVOID) codeBase, DLL_PROCESS_ATTACH, NULL);
199+
callback++;
200+
}
201+
}
202+
}
203+
}
204+
187205
static void
188206
PerformBaseRelocation(PMEMORYMODULE module, SIZE_T delta)
189207
{
@@ -419,6 +437,9 @@ HMEMORYMODULE MemoryLoadLibraryEx(const void *data,
419437
// sections that are marked as "discardable"
420438
FinalizeSections(result);
421439

440+
// TLS callbacks are executed BEFORE the main loading
441+
ExecuteTLS(result);
442+
422443
// get entry point of loaded library
423444
if (result->headers->OptionalHeader.AddressOfEntryPoint != 0) {
424445
DllEntry = (DllEntryProc) (code + result->headers->OptionalHeader.AddressOfEntryPoint);

0 commit comments

Comments
 (0)