1 | /* $Id: kWorkerTlsXxxK.c 3042 2017-05-11 10:23:12Z bird $ */
|
---|
2 | /** @file
|
---|
3 | * kWorkerTlsXxxK - Loader TLS allocation hack DLL.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (c) 2017 knut st. osmundsen <[email protected]>
|
---|
8 | *
|
---|
9 | * This file is part of kBuild.
|
---|
10 | *
|
---|
11 | * kBuild is free software; you can redistribute it and/or modify
|
---|
12 | * it under the terms of the GNU General Public License as published by
|
---|
13 | * the Free Software Foundation; either version 3 of the License, or
|
---|
14 | * (at your option) any later version.
|
---|
15 | *
|
---|
16 | * kBuild is distributed in the hope that it will be useful,
|
---|
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
19 | * GNU General Public License for more details.
|
---|
20 | *
|
---|
21 | * You should have received a copy of the GNU General Public License
|
---|
22 | * along with kBuild. If not, see <http://www.gnu.org/licenses/>
|
---|
23 | *
|
---|
24 | */
|
---|
25 |
|
---|
26 |
|
---|
27 | /*********************************************************************************************************************************
|
---|
28 | * Header Files *
|
---|
29 | *********************************************************************************************************************************/
|
---|
30 | #include <windows.h>
|
---|
31 |
|
---|
32 |
|
---|
33 | /*********************************************************************************************************************************
|
---|
34 | * Structures and Typedefs *
|
---|
35 | *********************************************************************************************************************************/
|
---|
36 | typedef void KWLDRTLSALLOCATIONHOOK(void *hDll, ULONG idxTls, PIMAGE_TLS_CALLBACK *ppfnTlsCallback);
|
---|
37 |
|
---|
38 |
|
---|
39 | /*********************************************************************************************************************************
|
---|
40 | * Internal Functions *
|
---|
41 | *********************************************************************************************************************************/
|
---|
42 | __declspec(dllexport) void __stdcall DummyTlsCallback(void *hDll, DWORD dwReason, void *pvContext);
|
---|
43 |
|
---|
44 |
|
---|
45 | /*********************************************************************************************************************************
|
---|
46 | * Global Variables *
|
---|
47 | *********************************************************************************************************************************/
|
---|
48 | /** The TLS pointer array. The 2nd entry is NULL and serve to terminate the array.
|
---|
49 | * The first entry can be used by kWorker if it needs to. */
|
---|
50 | __declspec(dllexport) PIMAGE_TLS_CALLBACK g_apfnTlsCallbacks[2] = { DummyTlsCallback, NULL };
|
---|
51 |
|
---|
52 | /**
|
---|
53 | * The TLS index.
|
---|
54 | */
|
---|
55 | __declspec(dllexport) ULONG g_idxTls = ~(ULONG)0;
|
---|
56 |
|
---|
57 | /**
|
---|
58 | * Initialization data.
|
---|
59 | */
|
---|
60 | static char const g_abDummy[TLS_SIZE] = {0x42};
|
---|
61 |
|
---|
62 | /**
|
---|
63 | * The TLS directory entry. Not possible to get more than one from the linker
|
---|
64 | * and probably also the loader doesn't want more than one anyway.
|
---|
65 | */
|
---|
66 | #pragma section(".rdata$T", long, read)
|
---|
67 | __declspec(allocate(".rdata$T")) const IMAGE_TLS_DIRECTORY _tls_used =
|
---|
68 | {
|
---|
69 | (ULONG_PTR)&g_abDummy,
|
---|
70 | (ULONG_PTR)&g_abDummy + sizeof(g_abDummy),
|
---|
71 | (ULONG_PTR)&g_idxTls,
|
---|
72 | (ULONG_PTR)&g_apfnTlsCallbacks,
|
---|
73 | 0, /* This SizeOfZeroFill bugger doesn't work on w10/amd64 from what I can tell! */
|
---|
74 | IMAGE_SCN_ALIGN_32BYTES
|
---|
75 | };
|
---|
76 |
|
---|
77 |
|
---|
78 | /*
|
---|
79 | * This is just a dummy TLS callback function.
|
---|
80 | * We'll be replacing g_apfnTlsCallbacks[0] from kWorker.c after loading it.
|
---|
81 | *
|
---|
82 | * Note! W10 doesn't seem to want to process the TLS directory if the DLL
|
---|
83 | * doesn't have any imports (to snap).
|
---|
84 | */
|
---|
85 | __declspec(dllexport) void __stdcall DummyTlsCallback(void *hDll, DWORD dwReason, void *pvContext)
|
---|
86 | {
|
---|
87 | (void)hDll; (void)dwReason; (void)pvContext;
|
---|
88 | if (dwReason == DLL_PROCESS_ATTACH)
|
---|
89 | {
|
---|
90 | HMODULE hModExe = (HMODULE)(ULONG_PTR)KWORKER_BASE;
|
---|
91 | KWLDRTLSALLOCATIONHOOK *pfnHook = (KWLDRTLSALLOCATIONHOOK *)GetProcAddress(hModExe, "kwLdrTlsAllocationHook");
|
---|
92 | if (pfnHook)
|
---|
93 | {
|
---|
94 | pfnHook(hDll, g_idxTls, &g_apfnTlsCallbacks[0]);
|
---|
95 | return;
|
---|
96 | }
|
---|
97 | __debugbreak();
|
---|
98 | }
|
---|
99 | }
|
---|
100 |
|
---|
101 |
|
---|
102 | /*
|
---|
103 | * Dummy DLL entry point to avoid dragging in unnecessary CRT stuff. kWorkerTls1K!_tls_index
|
---|
104 | */
|
---|
105 | BOOL __stdcall DummyDllEntry(void *hDll, DWORD dwReason, void *pvContext)
|
---|
106 | {
|
---|
107 | (void)hDll; (void)dwReason; (void)pvContext;
|
---|
108 | return TRUE;
|
---|
109 | }
|
---|
110 |
|
---|