1 | /* $Id: kWorkerTlsXxxK.c 3366 2020-06-09 23:53:39Z 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 KWLDRTLSCALLBACK(void *hDll, DWORD dwReason, void *pvContext, void *pvWorkerModule);
|
---|
37 | typedef KWLDRTLSCALLBACK *PKWLDRTLSCALLBACK;
|
---|
38 | typedef PKWLDRTLSCALLBACK KWLDRTLSALLOCATIONHOOK(void *hDll, ULONG idxTls, char *pabInitData, void **ppvWorkerModule);
|
---|
39 |
|
---|
40 |
|
---|
41 | /*********************************************************************************************************************************
|
---|
42 | * Internal Functions *
|
---|
43 | *********************************************************************************************************************************/
|
---|
44 | __declspec(dllexport) void __stdcall DummyTlsCallback(void *hDll, DWORD dwReason, void *pvContext);
|
---|
45 |
|
---|
46 |
|
---|
47 | /*********************************************************************************************************************************
|
---|
48 | * Global Variables *
|
---|
49 | *********************************************************************************************************************************/
|
---|
50 | /** The TLS pointer array. The 2nd entry is NULL and serve to terminate the array.
|
---|
51 | * The first entry can be used by kWorker if it needs to. */
|
---|
52 | __declspec(dllexport) PIMAGE_TLS_CALLBACK g_apfnTlsCallbacks[2] = { DummyTlsCallback, NULL };
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * The TLS index.
|
---|
56 | */
|
---|
57 | __declspec(dllexport) ULONG g_idxTls = ~(ULONG)0;
|
---|
58 |
|
---|
59 | /**
|
---|
60 | * Callback context.
|
---|
61 | */
|
---|
62 | __declspec(dllexport) void *g_pvWorkerModule = NULL;
|
---|
63 |
|
---|
64 | /**
|
---|
65 | * Regular callback method (returned by kwLdrTlsAllocationHook).
|
---|
66 | */
|
---|
67 | __declspec(dllexport) PKWLDRTLSCALLBACK g_pfnWorkerCallback = NULL;
|
---|
68 |
|
---|
69 |
|
---|
70 |
|
---|
71 | /**
|
---|
72 | * Initialization data.
|
---|
73 | * kWorker will copy the init data of the target DLL here.
|
---|
74 | */
|
---|
75 | static char g_abInitData[TLS_SIZE] = {0};
|
---|
76 |
|
---|
77 | /**
|
---|
78 | * The TLS directory entry. Not possible to get more than one from the linker
|
---|
79 | * and probably also the loader doesn't want more than one anyway.
|
---|
80 | */
|
---|
81 | #pragma section(".rdata$T", long, read)
|
---|
82 | __declspec(allocate(".rdata$T")) const IMAGE_TLS_DIRECTORY _tls_used =
|
---|
83 | {
|
---|
84 | (ULONG_PTR)&g_abInitData,
|
---|
85 | (ULONG_PTR)&g_abInitData + sizeof(g_abInitData),
|
---|
86 | (ULONG_PTR)&g_idxTls,
|
---|
87 | (ULONG_PTR)&g_apfnTlsCallbacks,
|
---|
88 | 0, /* This SizeOfZeroFill bugger doesn't work on w10/amd64 from what I can tell! */
|
---|
89 | IMAGE_SCN_ALIGN_32BYTES
|
---|
90 | };
|
---|
91 |
|
---|
92 |
|
---|
93 | /**
|
---|
94 | * Just a dummy callback function in case the allocation hook gambit fails below
|
---|
95 | * (see KWLDRTLSCALLBACK).
|
---|
96 | */
|
---|
97 | static void DummyWorkerCallback(void *hDll, DWORD dwReason, void *pvContext, void *pvWorkerModule)
|
---|
98 | {
|
---|
99 | (void)hDll; (void)dwReason; (void)pvContext; (void)pvWorkerModule;
|
---|
100 | }
|
---|
101 |
|
---|
102 |
|
---|
103 | /*
|
---|
104 | * This is just a dummy TLS callback function.
|
---|
105 | * We'll be replacing g_apfnTlsCallbacks[0] from kWorker.c after loading it.
|
---|
106 | *
|
---|
107 | * Note! W10 doesn't seem to want to process the TLS directory if the DLL
|
---|
108 | * doesn't have any imports (to snap).
|
---|
109 | */
|
---|
110 | __declspec(dllexport) void __stdcall DummyTlsCallback(void *hDll, DWORD dwReason, void *pvContext)
|
---|
111 | {
|
---|
112 | if (g_pfnWorkerCallback)
|
---|
113 | g_pfnWorkerCallback(hDll, dwReason, pvContext, g_pvWorkerModule);
|
---|
114 | else
|
---|
115 | {
|
---|
116 | g_pfnWorkerCallback = DummyWorkerCallback;
|
---|
117 | if (dwReason == DLL_PROCESS_ATTACH)
|
---|
118 | {
|
---|
119 | HMODULE hModExe = GetModuleHandleW(NULL);
|
---|
120 | KWLDRTLSALLOCATIONHOOK *pfnHook = (KWLDRTLSALLOCATIONHOOK *)GetProcAddress(hModExe, "kwLdrTlsAllocationHook");
|
---|
121 | if (pfnHook)
|
---|
122 | g_pfnWorkerCallback = pfnHook(hDll, g_idxTls, g_abInitData, &g_pvWorkerModule);
|
---|
123 | else
|
---|
124 | __debugbreak();
|
---|
125 | }
|
---|
126 | }
|
---|
127 | }
|
---|
128 |
|
---|
129 |
|
---|
130 | /*
|
---|
131 | * Dummy DLL entry point to avoid dragging in unnecessary CRT stuff. kWorkerTls1K!_tls_index
|
---|
132 | */
|
---|
133 | BOOL __stdcall DummyDllEntry(void *hDll, DWORD dwReason, void *pvContext)
|
---|
134 | {
|
---|
135 | (void)hDll; (void)dwReason; (void)pvContext;
|
---|
136 | return TRUE;
|
---|
137 | }
|
---|
138 |
|
---|