| 1 |
/** |
| 2 |
* This module provides OS specific helper function for DLL support |
| 3 |
* |
| 4 |
* Copyright: Copyright Digital Mars 2010 - 2012. |
| 5 |
* License: Distributed under the |
| 6 |
* $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost Software License 1.0). |
| 7 |
* (See accompanying file LICENSE) |
| 8 |
* Authors: Rainer Schuetze |
| 9 |
* Source: $(DRUNTIMESRC core/sys/windows/_dll.d) |
| 10 |
*/ |
| 11 |
|
| 12 |
module core.sys.windows.dll; |
| 13 |
version (Windows): |
| 14 |
|
| 15 |
import core.sys.windows.winbase; |
| 16 |
import core.sys.windows.winnt; |
| 17 |
import core.stdc.string; |
| 18 |
import core.runtime; |
| 19 |
|
| 20 |
public import core.sys.windows.threadaux; |
| 21 |
|
| 22 |
/////////////////////////////////////////////////////////////////// |
| 23 |
// support fixing implicit TLS for dynamically loaded DLLs on Windows XP |
| 24 |
|
| 25 |
// in this special case, we have to treat _tlsstart and _tlsend as non-TLS variables |
| 26 |
// as they are used to simulate TLS when it is not set up under XP. In this case we must |
| 27 |
// not access tls_array[tls_index] as needed for thread local _tlsstart and _tlsend |
| 28 |
extern (C) |
| 29 |
{ |
| 30 |
version (Win32) |
| 31 |
{ |
| 32 |
version (CRuntime_Microsoft) |
| 33 |
{ |
| 34 |
extern __gshared byte _tls_start; |
| 35 |
extern __gshared byte _tls_end; |
| 36 |
extern __gshared void* __xl_a; |
| 37 |
alias _tls_start _tlsstart; |
| 38 |
alias _tls_end _tlsend; |
| 39 |
alias __xl_a _tls_callbacks_a; |
| 40 |
} |
| 41 |
extern __gshared int _tls_index; |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
extern (C) // rt.minfo |
| 46 |
{ |
| 47 |
void rt_moduleTlsCtor(); |
| 48 |
void rt_moduleTlsDtor(); |
| 49 |
} |
| 50 |
|
| 51 |
private: |
| 52 |
struct dll_aux |
| 53 |
{ |
| 54 |
// don't let symbols leak into other modules |
| 55 |
version (Win32) |
| 56 |
{ |
| 57 |
struct LdrpTlsListEntry |
| 58 |
{ |
| 59 |
LdrpTlsListEntry* next; |
| 60 |
LdrpTlsListEntry* prev; |
| 61 |
void* tlsstart; |
| 62 |
void* tlsend; |
| 63 |
void* ptr_tlsindex; |
| 64 |
void* callbacks; |
| 65 |
void* zerofill; |
| 66 |
int tlsindex; |
| 67 |
} |
| 68 |
|
| 69 |
alias fnRtlAllocateHeap = extern(Windows) |
| 70 |
void* function(void* HeapHandle, uint Flags, size_t Size) nothrow; |
| 71 |
|
| 72 |
// find a code sequence and return the address after the sequence |
| 73 |
static void* findCodeSequence( void* adr, int len, ref ubyte[] pattern ) nothrow |
| 74 |
{ |
| 75 |
if ( !adr ) |
| 76 |
return null; |
| 77 |
|
| 78 |
ubyte* code = cast(ubyte*) adr; |
| 79 |
for ( int p = 0; p < len; p++ ) |
| 80 |
{ |
| 81 |
if ( code[ p .. p + pattern.length ] == pattern[ 0 .. $ ] ) |
| 82 |
{ |
| 83 |
ubyte* padr = code + p + pattern.length; |
| 84 |
return padr; |
| 85 |
} |
| 86 |
} |
| 87 |
return null; |
| 88 |
} |
| 89 |
|
| 90 |
// find a code sequence and return the (relative) address that follows |
| 91 |
static void* findCodeReference( void* adr, int len, ref ubyte[] pattern, bool relative ) nothrow |
| 92 |
{ |
| 93 |
if ( !adr ) |
| 94 |
return null; |
| 95 |
|
| 96 |
ubyte* padr = cast(ubyte*) findCodeSequence( adr, len, pattern ); |
| 97 |
if ( padr ) |
| 98 |
{ |
| 99 |
if ( relative ) |
| 100 |
return padr + 4 + *cast(int*) padr; |
| 101 |
return *cast(void**) padr; |
| 102 |
} |
| 103 |
return null; |
| 104 |
} |
| 105 |
|
| 106 |
// crawl through ntdll to find function _LdrpAllocateTls@0 and references |
| 107 |
// to _LdrpNumberOfTlsEntries, _NtdllBaseTag and _LdrpTlsList |
| 108 |
// LdrInitializeThunk |
| 109 |
// -> _LdrpInitialize@12 |
| 110 |
// -> _LdrpInitializeThread@4 |
| 111 |
// -> _LdrpAllocateTls@0 |
| 112 |
// -> je chunk |
| 113 |
// _LdrpNumberOfTlsEntries - number of entries in TlsList |
| 114 |
// _NtdllBaseTag - tag used for RtlAllocateHeap |
| 115 |
// _LdrpTlsList - root of the double linked list with TlsList entries |
| 116 |
|
| 117 |
__gshared int* pNtdllBaseTag; // remembered for reusage in addTlsData |
| 118 |
|
| 119 |
__gshared ubyte[] jmp_LdrpInitialize = [ 0x33, 0xED, 0xE9 ]; // xor ebp,ebp; jmp _LdrpInitialize |
| 120 |
__gshared ubyte[] jmp__LdrpInitialize = [ 0x5D, 0xE9 ]; // pop ebp; jmp __LdrpInitialize |
| 121 |
__gshared ubyte[] jmp__LdrpInitialize_xp64 = [ 0x5D, 0x90, 0x90, 0x90, 0x90, 0x90 ]; // pop ebp; nop; nop; nop; nop; nop; |
| 122 |
__gshared ubyte[] call_LdrpInitializeThread = [ 0xFF, 0x75, 0x08, 0xE8 ]; // push [ebp+8]; call _LdrpInitializeThread |
| 123 |
__gshared ubyte[] call_LdrpAllocateTls = [ 0x00, 0x00, 0xE8 ]; // jne 0xc3; call _LdrpAllocateTls |
| 124 |
__gshared ubyte[] call_LdrpAllocateTls_svr03 = [ 0x65, 0xfc, 0x00, 0xE8 ]; // and [ebp+fc], 0; call _LdrpAllocateTls |
| 125 |
__gshared ubyte[] jne_LdrpAllocateTls = [ 0x0f, 0x85 ]; // jne body_LdrpAllocateTls |
| 126 |
__gshared ubyte[] mov_LdrpNumberOfTlsEntries = [ 0x8B, 0x0D ]; // mov ecx, _LdrpNumberOfTlsEntries |
| 127 |
__gshared ubyte[] mov_NtdllBaseTag = [ 0x51, 0x8B, 0x0D ]; // push ecx; mov ecx, _NtdllBaseTag |
| 128 |
__gshared ubyte[] mov_NtdllBaseTag_srv03 = [ 0x50, 0xA1 ]; // push eax; mov eax, _NtdllBaseTag |
| 129 |
__gshared ubyte[] mov_LdrpTlsList = [ 0x8B, 0x3D ]; // mov edi, _LdrpTlsList |
| 130 |
|
| 131 |
static LdrpTlsListEntry* addTlsListEntry( void** peb, void* tlsstart, void* tlsend, void* tls_callbacks_a, int* tlsindex ) nothrow |
| 132 |
{ |
| 133 |
HANDLE hnd = GetModuleHandleA( "NTDLL" ); |
| 134 |
assert( hnd, "cannot get module handle for ntdll" ); |
| 135 |
ubyte* fn = cast(ubyte*) GetProcAddress( hnd, "LdrInitializeThunk" ); |
| 136 |
assert( fn, "cannot find LdrInitializeThunk in ntdll" ); |
| 137 |
|
| 138 |
void* pLdrpInitialize = findCodeReference( fn, 20, jmp_LdrpInitialize, true ); |
| 139 |
void* p_LdrpInitialize = findCodeReference( pLdrpInitialize, 40, jmp__LdrpInitialize, true ); |
| 140 |
if ( !p_LdrpInitialize ) |
| 141 |
p_LdrpInitialize = findCodeSequence( pLdrpInitialize, 40, jmp__LdrpInitialize_xp64 ); |
| 142 |
void* pLdrpInitializeThread = findCodeReference( p_LdrpInitialize, 200, call_LdrpInitializeThread, true ); |
| 143 |
void* pLdrpAllocateTls = findCodeReference( pLdrpInitializeThread, 40, call_LdrpAllocateTls, true ); |
| 144 |
if (!pLdrpAllocateTls) |
| 145 |
pLdrpAllocateTls = findCodeReference( pLdrpInitializeThread, 100, call_LdrpAllocateTls_svr03, true ); |
| 146 |
void* pBodyAllocateTls = findCodeReference( pLdrpAllocateTls, 40, jne_LdrpAllocateTls, true ); |
| 147 |
|
| 148 |
int* pLdrpNumberOfTlsEntries = cast(int*) findCodeReference( pBodyAllocateTls, 60, mov_LdrpNumberOfTlsEntries, false ); |
| 149 |
pNtdllBaseTag = cast(int*) findCodeReference( pBodyAllocateTls, 30, mov_NtdllBaseTag, false ); |
| 150 |
if (!pNtdllBaseTag) |
| 151 |
pNtdllBaseTag = cast(int*) findCodeReference( pBodyAllocateTls, 30, mov_NtdllBaseTag_srv03, false ); |
| 152 |
LdrpTlsListEntry* pLdrpTlsList = cast(LdrpTlsListEntry*)findCodeReference( pBodyAllocateTls, 80, mov_LdrpTlsList, false ); |
| 153 |
|
| 154 |
if ( !pLdrpNumberOfTlsEntries || !pNtdllBaseTag || !pLdrpTlsList ) |
| 155 |
return null; |
| 156 |
|
| 157 |
fnRtlAllocateHeap fnAlloc = cast(fnRtlAllocateHeap) GetProcAddress( hnd, "RtlAllocateHeap" ); |
| 158 |
if ( !fnAlloc ) |
| 159 |
return null; |
| 160 |
|
| 161 |
// allocate new TlsList entry (adding 0xC0000 to the tag is obviously a flag also usesd by |
| 162 |
// the nt-loader, could be the result of HEAP_MAKE_TAG_FLAGS(0,HEAP_NO_SERIALIZE|HEAP_GROWABLE) |
| 163 |
// but this is not documented in the msdn entry for RtlAlloateHeap |
| 164 |
void* heap = peb[6]; |
| 165 |
LdrpTlsListEntry* entry = cast(LdrpTlsListEntry*) (*fnAlloc)( heap, *pNtdllBaseTag | 0xc0000, LdrpTlsListEntry.sizeof ); |
| 166 |
if ( !entry ) |
| 167 |
return null; |
| 168 |
|
| 169 |
// fill entry |
| 170 |
entry.tlsstart = tlsstart; |
| 171 |
entry.tlsend = tlsend; |
| 172 |
entry.ptr_tlsindex = tlsindex; |
| 173 |
entry.callbacks = tls_callbacks_a; |
| 174 |
entry.zerofill = null; |
| 175 |
entry.tlsindex = *pLdrpNumberOfTlsEntries; |
| 176 |
|
| 177 |
// and add it to the end of TlsList |
| 178 |
*tlsindex = *pLdrpNumberOfTlsEntries; |
| 179 |
entry.next = pLdrpTlsList; |
| 180 |
entry.prev = pLdrpTlsList.prev; |
| 181 |
pLdrpTlsList.prev.next = entry; |
| 182 |
pLdrpTlsList.prev = entry; |
| 183 |
(*pLdrpNumberOfTlsEntries)++; |
| 184 |
|
| 185 |
return entry; |
| 186 |
} |
| 187 |
|
| 188 |
// reallocate TLS array and create a copy of the TLS data section |
| 189 |
static bool addTlsData( void** teb, void* tlsstart, void* tlsend, int tlsindex ) nothrow |
| 190 |
{ |
| 191 |
HANDLE hnd = GetModuleHandleA( "NTDLL" ); |
| 192 |
assert( hnd, "cannot get module handle for ntdll" ); |
| 193 |
|
| 194 |
fnRtlAllocateHeap fnAlloc = cast(fnRtlAllocateHeap) GetProcAddress( hnd, "RtlAllocateHeap" ); |
| 195 |
if ( !fnAlloc || !pNtdllBaseTag ) |
| 196 |
return false; |
| 197 |
|
| 198 |
void** peb = cast(void**) teb[12]; |
| 199 |
void* heap = peb[6]; |
| 200 |
|
| 201 |
auto sz = tlsend - tlsstart; |
| 202 |
void* tlsdata = cast(void*) (*fnAlloc)( heap, *pNtdllBaseTag | 0xc0000, sz ); |
| 203 |
if ( !tlsdata ) |
| 204 |
return false; |
| 205 |
|
| 206 |
// no relocations! not even self-relocations. Windows does not do them. |
| 207 |
core.stdc.string.memcpy( tlsdata, tlsstart, sz ); |
| 208 |
|
| 209 |
// create copy of tls pointer array |
| 210 |
void** array = cast(void**) (*fnAlloc)( heap, *pNtdllBaseTag | 0xc0000, (tlsindex + 1) * (void*).sizeof ); |
| 211 |
if ( !array ) |
| 212 |
return false; |
| 213 |
|
| 214 |
if ( tlsindex > 0 && teb[11] ) |
| 215 |
core.stdc.string.memcpy( array, teb[11], tlsindex * (void*).sizeof); |
| 216 |
array[tlsindex] = tlsdata; |
| 217 |
teb[11] = cast(void*) array; |
| 218 |
|
| 219 |
// let the old array leak, in case a oncurrent thread is still relying on it |
| 220 |
return true; |
| 221 |
} |
| 222 |
} // Win32 |
| 223 |
|
| 224 |
alias bool BOOLEAN; |
| 225 |
|
| 226 |
struct UNICODE_STRING |
| 227 |
{ |
| 228 |
short Length; |
| 229 |
short MaximumLength; |
| 230 |
wchar* Buffer; |
| 231 |
} |
| 232 |
|
| 233 |
struct LIST_ENTRY |
| 234 |
{ |
| 235 |
LIST_ENTRY* next; |
| 236 |
LIST_ENTRY* prev; |
| 237 |
} |
| 238 |
|
| 239 |
// the following structures can be found here: |
| 240 |
// https://www.geoffchappell.com/studies/windows/win32/ntdll/structs/ldr_data_table_entry.htm |
| 241 |
// perhaps this should be same as LDR_DATA_TABLE_ENTRY, which is introduced with PEB_LDR_DATA |
| 242 |
struct LDR_MODULE |
| 243 |
{ |
| 244 |
LIST_ENTRY InLoadOrderModuleList; |
| 245 |
LIST_ENTRY InMemoryOrderModuleList; |
| 246 |
LIST_ENTRY InInitializationOrderModuleList; |
| 247 |
PVOID BaseAddress; |
| 248 |
PVOID EntryPoint; |
| 249 |
SIZE_T SizeOfImage; |
| 250 |
UNICODE_STRING FullDllName; |
| 251 |
UNICODE_STRING BaseDllName; |
| 252 |
ULONG Flags; |
| 253 |
SHORT LoadCount; // obsolete after Version 6.1 |
| 254 |
SHORT TlsIndex; |
| 255 |
LIST_ENTRY HashTableEntry; |
| 256 |
ULONG TimeDateStamp; |
| 257 |
PVOID EntryPointActivationContext; |
| 258 |
PVOID PatchInformation; |
| 259 |
LDR_DDAG_NODE *DdagNode; // starting with Version 6.2 |
| 260 |
} |
| 261 |
|
| 262 |
struct LDR_DDAG_NODE |
| 263 |
{ |
| 264 |
LIST_ENTRY Modules; |
| 265 |
void* ServiceTagList; // LDR_SERVICE_TAG_RECORD |
| 266 |
ULONG LoadCount; |
| 267 |
ULONG ReferenceCount; // Version 10: ULONG LoadWhileUnloadingCount; |
| 268 |
ULONG DependencyCount; // Version 10: ULONG LowestLink; |
| 269 |
} |
| 270 |
|
| 271 |
struct PEB_LDR_DATA |
| 272 |
{ |
| 273 |
ULONG Length; |
| 274 |
BOOLEAN Initialized; |
| 275 |
PVOID SsHandle; |
| 276 |
LIST_ENTRY InLoadOrderModuleList; |
| 277 |
LIST_ENTRY InMemoryOrderModuleList; |
| 278 |
LIST_ENTRY InInitializationOrderModuleList; |
| 279 |
} |
| 280 |
|
| 281 |
static LDR_MODULE* findLdrModule( HINSTANCE hInstance, void** peb ) nothrow @nogc |
| 282 |
{ |
| 283 |
PEB_LDR_DATA* ldrData = cast(PEB_LDR_DATA*) peb[3]; |
| 284 |
LIST_ENTRY* root = &ldrData.InLoadOrderModuleList; |
| 285 |
for (LIST_ENTRY* entry = root.next; entry != root; entry = entry.next) |
| 286 |
{ |
| 287 |
LDR_MODULE *ldrMod = cast(LDR_MODULE*) entry; |
| 288 |
if (ldrMod.BaseAddress == hInstance) |
| 289 |
return ldrMod; |
| 290 |
} |
| 291 |
return null; |
| 292 |
} |
| 293 |
|
| 294 |
static bool setDllTlsUsage( HINSTANCE hInstance, void** peb ) nothrow |
| 295 |
{ |
| 296 |
LDR_MODULE *thisMod = findLdrModule( hInstance, peb ); |
| 297 |
if ( !thisMod ) |
| 298 |
return false; |
| 299 |
|
| 300 |
thisMod.TlsIndex = -1; // uses TLS (not the index itself) |
| 301 |
thisMod.LoadCount = -1; // never unload |
| 302 |
return true; |
| 303 |
} |
| 304 |
} |
| 305 |
|
| 306 |
public: |
| 307 |
/* ***************************************************** |
| 308 |
* Fix implicit thread local storage for the case when a DLL is loaded |
| 309 |
* dynamically after process initialization. |
| 310 |
* The link time variables are passed to allow placing this function into |
| 311 |
* an RTL DLL itself. |
| 312 |
* The problem is described in Bugzilla 3342 and |
| 313 |
* http://www.nynaeve.net/?p=187, to quote from the latter: |
| 314 |
* |
| 315 |
* "When a DLL using implicit TLS is loaded, because the loader doesn't process the TLS |
| 316 |
* directory, the _tls_index value is not initialized by the loader, nor is there space |
| 317 |
* allocated for module's TLS data in the ThreadLocalStoragePointer arrays of running |
| 318 |
* threads. The DLL continues to load, however, and things will appear to work... until the |
| 319 |
* first access to a __declspec(thread) variable occurs, that is." |
| 320 |
* |
| 321 |
* _tls_index is initialized by the compiler to 0, so we can use this as a test. |
| 322 |
* Returns: |
| 323 |
* true for success, false for failure |
| 324 |
*/ |
| 325 |
bool dll_fixTLS( HINSTANCE hInstance, void* tlsstart, void* tlsend, void* tls_callbacks_a, int* tlsindex ) nothrow |
| 326 |
{ |
| 327 |
version (GNU_EMUTLS) |
| 328 |
return true; |
| 329 |
else version (Win64) |
| 330 |
return true; // fixed |
| 331 |
else version (Win32) |
| 332 |
{ |
| 333 |
/* If the OS has allocated a TLS slot for us, we don't have to do anything |
| 334 |
* tls_index 0 means: the OS has not done anything, or it has allocated slot 0 |
| 335 |
* Vista and later Windows systems should do this correctly and not need |
| 336 |
* this function. |
| 337 |
*/ |
| 338 |
if ( *tlsindex != 0 ) |
| 339 |
return true; |
| 340 |
|
| 341 |
void** peb; |
| 342 |
asm pure nothrow @nogc |
| 343 |
{ |
| 344 |
mov EAX,FS:[0x30]; |
| 345 |
mov peb, EAX; |
| 346 |
} |
| 347 |
dll_aux.LDR_MODULE *ldrMod = dll_aux.findLdrModule( hInstance, peb ); |
| 348 |
if ( !ldrMod ) |
| 349 |
return false; // not in module list, bail out |
| 350 |
if ( ldrMod.TlsIndex != 0 ) |
| 351 |
return true; // the OS has already setup TLS |
| 352 |
|
| 353 |
dll_aux.LdrpTlsListEntry* entry = dll_aux.addTlsListEntry( peb, tlsstart, tlsend, tls_callbacks_a, tlsindex ); |
| 354 |
if ( !entry ) |
| 355 |
return false; |
| 356 |
|
| 357 |
scope (failure) assert(0); // enforce nothrow, Bugzilla 13561 |
| 358 |
|
| 359 |
if ( !enumProcessThreads( |
| 360 |
function (uint id, void* context) nothrow { |
| 361 |
dll_aux.LdrpTlsListEntry* entry = cast(dll_aux.LdrpTlsListEntry*) context; |
| 362 |
return dll_aux.addTlsData( getTEB( id ), entry.tlsstart, entry.tlsend, entry.tlsindex ); |
| 363 |
}, entry ) ) |
| 364 |
return false; |
| 365 |
|
| 366 |
ldrMod.TlsIndex = -1; // flag TLS usage (not the index itself) |
| 367 |
ldrMod.LoadCount = -1; // prevent unloading of the DLL, |
| 368 |
// since XP does not keep track of used TLS entries |
| 369 |
return true; |
| 370 |
} |
| 371 |
} |
| 372 |
|
| 373 |
private extern (Windows) ULONGLONG VerSetConditionMask(ULONGLONG, DWORD, BYTE) nothrow @nogc; |
| 374 |
|
| 375 |
private bool isWindows8OrLater() nothrow @nogc |
| 376 |
{ |
| 377 |
OSVERSIONINFOEXW osvi; |
| 378 |
osvi.dwOSVersionInfoSize = osvi.sizeof; |
| 379 |
DWORDLONG dwlConditionMask = VerSetConditionMask( |
| 380 |
VerSetConditionMask( |
| 381 |
VerSetConditionMask( |
| 382 |
0, VER_MAJORVERSION, VER_GREATER_EQUAL), |
| 383 |
VER_MINORVERSION, VER_GREATER_EQUAL), |
| 384 |
VER_SERVICEPACKMAJOR, VER_GREATER_EQUAL); |
| 385 |
|
| 386 |
osvi.dwMajorVersion = 6; |
| 387 |
osvi.dwMinorVersion = 2; |
| 388 |
osvi.wServicePackMajor = 0; |
| 389 |
|
| 390 |
return VerifyVersionInfoW(&osvi, VER_MAJORVERSION | VER_MINORVERSION | VER_SERVICEPACKMAJOR, dwlConditionMask) != FALSE; |
| 391 |
} |
| 392 |
|
| 393 |
/* ***************************************************** |
| 394 |
* Get the process reference count for the given DLL handle |
| 395 |
* Params: |
| 396 |
* hInstance = DLL instance handle |
| 397 |
* Returns: |
| 398 |
* the reference count for the DLL in the current process, |
| 399 |
* -1 if the DLL is implicitely loaded with the process |
| 400 |
* or -2 if the DLL handle is invalid |
| 401 |
*/ |
| 402 |
int dll_getRefCount( HINSTANCE hInstance ) nothrow @nogc |
| 403 |
{ |
| 404 |
void** peb; |
| 405 |
version (D_InlineAsm_X86_64) |
| 406 |
{ |
| 407 |
asm pure nothrow @nogc |
| 408 |
{ |
| 409 |
mov RAX, 0x60; |
| 410 |
mov RAX,GS:[RAX]; |
| 411 |
mov peb, RAX; |
| 412 |
} |
| 413 |
} |
| 414 |
else version (D_InlineAsm_X86) |
| 415 |
{ |
| 416 |
asm pure nothrow @nogc |
| 417 |
{ |
| 418 |
mov EAX,FS:[0x30]; |
| 419 |
mov peb, EAX; |
| 420 |
} |
| 421 |
} |
| 422 |
else version (GNU_InlineAsm) |
| 423 |
{ |
| 424 |
version (X86_64) |
| 425 |
asm pure nothrow @nogc { "movq %%gs:0x60, %0;" : "=r" (peb); } |
| 426 |
else version (X86) |
| 427 |
asm pure nothrow @nogc { "movl %%fs:0x30, %0;" : "=r" (peb); } |
| 428 |
} |
| 429 |
dll_aux.LDR_MODULE *ldrMod = dll_aux.findLdrModule( hInstance, peb ); |
| 430 |
if ( !ldrMod ) |
| 431 |
return -2; // not in module list, bail out |
| 432 |
if (isWindows8OrLater()) |
| 433 |
return ldrMod.DdagNode.LoadCount; |
| 434 |
return ldrMod.LoadCount; |
| 435 |
} |
| 436 |
|
| 437 |
/***************************** |
| 438 |
* To be called from DllMain with reason DLL_PROCESS_ATTACH |
| 439 |
* |
| 440 |
* fixup TLS storage, initialize runtime and attach to threads |
| 441 |
* Returns: |
| 442 |
* true = success, false = failure |
| 443 |
*/ |
| 444 |
bool dll_process_attach( HINSTANCE hInstance, bool attach_threads, |
| 445 |
void* tlsstart, void* tlsend, void* tls_callbacks_a, int* tlsindex ) |
| 446 |
{ |
| 447 |
version (Win32) |
| 448 |
{ |
| 449 |
if ( !dll_fixTLS( hInstance, tlsstart, tlsend, tls_callbacks_a, tlsindex ) ) |
| 450 |
return false; |
| 451 |
} |
| 452 |
|
| 453 |
Runtime.initialize(); |
| 454 |
|
| 455 |
if ( !attach_threads ) |
| 456 |
return true; |
| 457 |
|
| 458 |
// attach to all other threads |
| 459 |
return enumProcessThreads( |
| 460 |
function (uint id, void* context) { |
| 461 |
if ( !thread_findByAddr( id ) && !findLowLevelThread( id ) ) |
| 462 |
{ |
| 463 |
// if the OS has not prepared TLS for us, don't attach to the thread |
| 464 |
if ( GetTlsDataAddress( id ) ) |
| 465 |
{ |
| 466 |
thread_attachByAddr( id ); |
| 467 |
thread_moduleTlsCtor( id ); |
| 468 |
} |
| 469 |
} |
| 470 |
return true; |
| 471 |
}, null ); |
| 472 |
} |
| 473 |
|
| 474 |
version (Shared) version (DigitalMars) private extern(C) extern __gshared void* __ImageBase; |
| 475 |
|
| 476 |
/** same as above, but checking for shared runtime |
| 477 |
*/ |
| 478 |
pragma(inline, false) // version (Shared) only set when compiling druntime |
| 479 |
bool dll_process_attach( HINSTANCE hInstance, bool attach_threads = true ) |
| 480 |
{ |
| 481 |
version (Shared) version (DigitalMars) |
| 482 |
{ |
| 483 |
// cannot declare rt_initSharedModule globally as it then conflicts with the definition in sections_win64.d |
| 484 |
pragma(mangle, "rt_initSharedModule") extern(C) bool rt_initSharedModule( void* handle ); |
| 485 |
if ( hInstance != &__ImageBase ) |
| 486 |
return rt_initSharedModule( hInstance ); |
| 487 |
} |
| 488 |
version (Win64) |
| 489 |
{ |
| 490 |
return dll_process_attach( hInstance, attach_threads, |
| 491 |
null, null, null, null ); |
| 492 |
} |
| 493 |
else version (Win32) |
| 494 |
{ |
| 495 |
return dll_process_attach( hInstance, attach_threads, |
| 496 |
&_tlsstart, &_tlsend, &_tls_callbacks_a, &_tls_index ); |
| 497 |
} |
| 498 |
} |
| 499 |
|
| 500 |
/** |
| 501 |
* to be called from DllMain with reason DLL_PROCESS_DETACH |
| 502 |
*/ |
| 503 |
pragma(inline, false) // version (Shared) only set when compiling druntime |
| 504 |
void dll_process_detach( HINSTANCE hInstance, bool detach_threads = true ) |
| 505 |
{ |
| 506 |
version (Shared) version (DigitalMars) |
| 507 |
{ |
| 508 |
// cannot declare rt_termSharedModule globally as it then conflicts with the definition in sections_win64.d |
| 509 |
pragma(mangle, "rt_termSharedModule") extern(C) bool rt_termSharedModule( void* handle ); |
| 510 |
if ( hInstance != &__ImageBase ) |
| 511 |
return cast(void) rt_termSharedModule( hInstance ); |
| 512 |
} |
| 513 |
// notify core.thread.joinLowLevelThread that the DLL is about to be unloaded |
| 514 |
thread_DLLProcessDetaching = true; |
| 515 |
|
| 516 |
// detach from all other threads |
| 517 |
if ( detach_threads ) |
| 518 |
enumProcessThreads( |
| 519 |
function (uint id, void* context) |
| 520 |
{ |
| 521 |
if ( id != GetCurrentThreadId() ) |
| 522 |
{ |
| 523 |
if ( auto t = thread_findByAddr( id ) ) |
| 524 |
{ |
| 525 |
thread_moduleTlsDtor( id ); |
| 526 |
if ( !t.isMainThread() ) |
| 527 |
thread_detachByAddr( id ); |
| 528 |
} |
| 529 |
} |
| 530 |
return true; |
| 531 |
}, null ); |
| 532 |
|
| 533 |
Runtime.terminate(); |
| 534 |
} |
| 535 |
|
| 536 |
/***************************** |
| 537 |
* Check whether the D runtime is built as a DLL or linked statically |
| 538 |
* |
| 539 |
* Returns: |
| 540 |
* true = DLL, false = static library |
| 541 |
*/ |
| 542 |
pragma(inline, false) // version (Shared) only set when compiling druntime |
| 543 |
bool isSharedDRuntime() |
| 544 |
{ |
| 545 |
version (Shared) |
| 546 |
return true; |
| 547 |
else |
| 548 |
return false; |
| 549 |
} |
| 550 |
|
| 551 |
/* Make sure that tlsCtorRun is itself a tls variable |
| 552 |
*/ |
| 553 |
static bool tlsCtorRun; |
| 554 |
static this() { tlsCtorRun = true; } |
| 555 |
static ~this() { tlsCtorRun = false; } |
| 556 |
|
| 557 |
/** |
| 558 |
* To be called from DllMain with reason DLL_THREAD_ATTACH |
| 559 |
* Returns: |
| 560 |
* true for success, false for failure |
| 561 |
*/ |
| 562 |
pragma(inline, false) // version (Shared) only set when compiling druntime |
| 563 |
bool dll_thread_attach( bool attach_thread = true, bool initTls = true, HINSTANCE hInstance = null ) |
| 564 |
{ |
| 565 |
version (Shared) version (DigitalMars) |
| 566 |
{ |
| 567 |
if ( hInstance && hInstance != &__ImageBase ) |
| 568 |
return true; |
| 569 |
} |
| 570 |
|
| 571 |
// if the OS has not prepared TLS for us, don't attach to the thread |
| 572 |
// (happened when running under x64 OS) |
| 573 |
auto tid = GetCurrentThreadId(); |
| 574 |
if ( !GetTlsDataAddress( tid ) ) |
| 575 |
return false; |
| 576 |
if ( !thread_findByAddr( tid ) && !findLowLevelThread( tid ) ) |
| 577 |
{ |
| 578 |
// only attach to thread and initalize it if it is not in the thread list (so it's not created by "new Thread") |
| 579 |
if ( attach_thread ) |
| 580 |
thread_attachThis(); |
| 581 |
if ( initTls && !tlsCtorRun ) // avoid duplicate calls |
| 582 |
rt_moduleTlsCtor(); |
| 583 |
} |
| 584 |
return true; |
| 585 |
} |
| 586 |
|
| 587 |
/** |
| 588 |
* To be called from DllMain with reason DLL_THREAD_DETACH |
| 589 |
* Returns: |
| 590 |
* true for success, false for failure |
| 591 |
*/ |
| 592 |
pragma(inline, false) // version (Shared) only set when compiling druntime |
| 593 |
bool dll_thread_detach( bool detach_thread = true, bool exitTls = true, HINSTANCE hInstance = null ) |
| 594 |
{ |
| 595 |
version (Shared) version (DigitalMars) |
| 596 |
{ |
| 597 |
if ( hInstance && hInstance != &__ImageBase ) |
| 598 |
return true; |
| 599 |
} |
| 600 |
|
| 601 |
// if the OS has not prepared TLS for us, we did not attach to the thread |
| 602 |
if ( !GetTlsDataAddress( GetCurrentThreadId() ) ) |
| 603 |
return false; |
| 604 |
if ( thread_findByAddr( GetCurrentThreadId() ) ) |
| 605 |
{ |
| 606 |
if ( exitTls && tlsCtorRun ) // avoid dtors to be run twice |
| 607 |
rt_moduleTlsDtor(); |
| 608 |
if ( detach_thread ) |
| 609 |
thread_detachThis(); |
| 610 |
} |
| 611 |
return true; |
| 612 |
} |
| 613 |
|
| 614 |
/********************************** |
| 615 |
* A mixin to provide a $(D DllMain) which calls the necessary |
| 616 |
* D runtime initialization and termination functions automatically. |
| 617 |
* |
| 618 |
* Example: |
| 619 |
* --- |
| 620 |
* module dllmain; |
| 621 |
* import core.sys.windows.dll; |
| 622 |
* mixin SimpleDllMain; |
| 623 |
* --- |
| 624 |
*/ |
| 625 |
mixin template SimpleDllMain() |
| 626 |
{ |
| 627 |
import core.sys.windows.windef : HINSTANCE, BOOL, DWORD, LPVOID; |
| 628 |
|
| 629 |
extern(Windows) |
| 630 |
BOOL DllMain(HINSTANCE hInstance, DWORD ulReason, LPVOID reserved) |
| 631 |
{ |
| 632 |
import core.sys.windows.winnt; |
| 633 |
import core.sys.windows.dll : |
| 634 |
dll_process_attach, dll_process_detach, |
| 635 |
dll_thread_attach, dll_thread_detach; |
| 636 |
switch (ulReason) |
| 637 |
{ |
| 638 |
default: assert(0); |
| 639 |
case DLL_PROCESS_ATTACH: |
| 640 |
return dll_process_attach( hInstance, true ); |
| 641 |
|
| 642 |
case DLL_PROCESS_DETACH: |
| 643 |
dll_process_detach( hInstance, true ); |
| 644 |
return true; |
| 645 |
|
| 646 |
case DLL_THREAD_ATTACH: |
| 647 |
return dll_thread_attach( true, true, hInstance ); |
| 648 |
|
| 649 |
case DLL_THREAD_DETACH: |
| 650 |
return dll_thread_detach( true, true, hInstance ); |
| 651 |
} |
| 652 |
} |
| 653 |
} |