VirtualBox

source: kBuild/trunk/src/kWorker/kWorkerTlsXxxK.c@ 3361

Last change on this file since 3361 was 3361, checked in by bird, 4 years ago

kWorker: Adjustments for VC++ 14.2. Fixed bug in GetFileAttributesExW/A where we'd forget to set the return value on success. Reduced the size of the Tls DLLs, but having to add more as the re-load-same-dll hack doesn't work anymore.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 4.6 KB
Line 
1/* $Id: kWorkerTlsXxxK.c 3361 2020-06-08 19:27:14Z bird $ */
2/** @file
3 * kWorkerTlsXxxK - Loader TLS allocation hack DLL.
4 */
5
6/*
7 * Copyright (c) 2017 knut st. osmundsen <bird-kBuild-spamx@anduin.net>
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*********************************************************************************************************************************/
36typedef 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};
61static char g_abDummy[TLS_SIZE] = {0};
62
63/**
64 * The TLS directory entry. Not possible to get more than one from the linker
65 * and probably also the loader doesn't want more than one anyway.
66 */
67#pragma section(".rdata$T", long, read)
68__declspec(allocate(".rdata$T")) const IMAGE_TLS_DIRECTORY _tls_used =
69{
70 (ULONG_PTR)&g_abDummy,
71 (ULONG_PTR)&g_abDummy + sizeof(g_abDummy),
72 (ULONG_PTR)&g_idxTls,
73 (ULONG_PTR)&g_apfnTlsCallbacks,
74 0, /* This SizeOfZeroFill bugger doesn't work on w10/amd64 from what I can tell! */
75 IMAGE_SCN_ALIGN_32BYTES
76};
77
78
79/*
80 * This is just a dummy TLS callback function.
81 * We'll be replacing g_apfnTlsCallbacks[0] from kWorker.c after loading it.
82 *
83 * Note! W10 doesn't seem to want to process the TLS directory if the DLL
84 * doesn't have any imports (to snap).
85 */
86__declspec(dllexport) void __stdcall DummyTlsCallback(void *hDll, DWORD dwReason, void *pvContext)
87{
88 (void)hDll; (void)dwReason; (void)pvContext;
89 if (dwReason == DLL_PROCESS_ATTACH)
90 {
91 //HMODULE hModExe = (HMODULE)(ULONG_PTR)KWORKER_BASE;
92 HMODULE hModExe = GetModuleHandleW(NULL);
93 KWLDRTLSALLOCATIONHOOK *pfnHook = (KWLDRTLSALLOCATIONHOOK *)GetProcAddress(hModExe, "kwLdrTlsAllocationHook");
94 if (pfnHook)
95 {
96 pfnHook(hDll, g_idxTls, &g_apfnTlsCallbacks[0]);
97 return;
98 }
99 __debugbreak();
100 }
101}
102
103
104/*
105 * Dummy DLL entry point to avoid dragging in unnecessary CRT stuff. kWorkerTls1K!_tls_index
106 */
107BOOL __stdcall DummyDllEntry(void *hDll, DWORD dwReason, void *pvContext)
108{
109 (void)hDll; (void)dwReason; (void)pvContext;
110 return TRUE;
111}
112
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette