source: trunk/src/kernel32/wintls.cpp@ 3615

Last change on this file since 3615 was 3615, checked in by sandervl, 25 years ago

Don't call entrypoint of exe loaded as dll

File size: 5.5 KB
Line 
1/* $Id: wintls.cpp,v 1.10 2000-05-27 11:30:36 sandervl Exp $ */
2/*
3 * Win32 TLS API functions
4 *
5 * Copyright 1998 Sander van Leeuwen (sandervl@xs4all.nl)
6 *
7 *
8 * Project Odin Software License can be found in LICENSE.TXT
9 *
10 */
11#include <os2win.h>
12#include <string.h>
13#include <winimagebase.h>
14#include <thread.h>
15#include <wprocess.h>
16#include "exceptutil.h"
17
18#define DBG_LOCALLOG DBG_wintls
19#include "dbglocal.h"
20
21//******************************************************************************
22//******************************************************************************
23void Win32ImageBase::tlsAlloc() //Allocate TLS index for this module
24{
25 if(!tlsAddress)
26 return;
27
28 tlsIndex = TlsAlloc();
29 if(tlsIndex >= TLS_MINIMUM_AVAILABLE) {
30 dprintf(("tlsAttachThread: invalid tlsIndex %x!!!!", tlsIndex));
31 DebugInt3();
32 return;
33 }
34 dprintf(("Win32ImageBase::tlsAlloc (%d) for module %x", tlsIndex, hinstance));
35}
36//******************************************************************************
37//******************************************************************************
38void Win32ImageBase::tlsDelete() //Free TLS index for this module
39{
40 if(!tlsAddress)
41 return;
42
43 if(tlsIndex >= TLS_MINIMUM_AVAILABLE) {
44 dprintf(("tlsAttachThread: invalid tlsIndex %x!!!!", tlsIndex));
45 DebugInt3();
46 return;
47 }
48 dprintf(("Win32ImageBase::tlsDestroy (%d) for module %x", tlsIndex, hinstance));
49 TlsFree(tlsIndex);
50 tlsIndex = -1;
51}
52//******************************************************************************
53//******************************************************************************
54void Win32ImageBase::tlsAttachThread() //setup TLS structures for new thread
55{
56 EXCEPTION_FRAME exceptFrame;
57 PIMAGE_TLS_CALLBACK *pCallback;
58 TEB *winteb;
59 char *tibmem;
60
61 if(!tlsAddress)
62 return;
63
64 if(tlsIndex >= TLS_MINIMUM_AVAILABLE) {
65 dprintf(("tlsAttachThread: invalid tlsIndex %x!!!!", tlsIndex));
66 DebugInt3();
67 return;
68 }
69
70 dprintf(("Win32ImageBase::tlsAttachThread for module %x, thread id %x", hinstance, GetCurrentThreadId()));
71 dprintf(("tlsAddress: %x", tlsAddress));
72 dprintf(("tlsInitSize: %x", tlsInitSize));
73 dprintf(("tlsTotalSize %x", tlsTotalSize));
74 dprintf(("tlsIndexAddr %x", tlsIndexAddr));
75 dprintf(("tlsCallbackAddr %x", tlsCallBackAddr));
76 dprintf(("*tlsCallbackAddr %x", (tlsCallBackAddr) ? *tlsCallBackAddr : 0));
77 tibmem = (char *)VirtualAlloc(0, tlsTotalSize, MEM_RESERVE|MEM_COMMIT, PAGE_READWRITE);
78 if(tibmem == NULL) {
79 dprintf(("tlsAttachThread: tibmem == NULL!!!!"));
80 DebugInt3();
81 return;
82 }
83 memset(tibmem, 0, tlsTotalSize);
84 memcpy(tibmem, tlsAddress, tlsInitSize);
85
86 winteb = (TEB *)*TIBFlatPtr;
87 winteb->tls_ptr[tlsIndex] = tibmem;
88 *tlsIndexAddr = tlsIndex;
89
90 if(tlsCallBackAddr && (ULONG)*tlsCallBackAddr != 0) {
91 pCallback = tlsCallBackAddr;
92 while(*pCallback) {
93 dprintf(("tlsAttachThread: calling TLS Callback %x", *pCallback));
94
95 (*pCallback)((LPVOID)hinstance, DLL_THREAD_ATTACH, 0);
96
97 dprintf(("tlsAttachThread: finished calling TLS Callback %x", *pCallback));
98 *pCallback++;
99 }
100 }
101 return;
102}
103//******************************************************************************
104//******************************************************************************
105void Win32ImageBase::tlsDetachThread() //destroy TLS structures
106{
107 EXCEPTION_FRAME exceptFrame;
108 PIMAGE_TLS_CALLBACK *pCallback;
109 TEB *winteb;
110
111 if(!tlsAddress)
112 return;
113
114 dprintf(("Win32ImageBase::tlsDetachThread for module %x, thread id %x", hinstance, GetCurrentThreadId()));
115
116 if(tlsCallBackAddr && (ULONG)*tlsCallBackAddr != 0) {
117 pCallback = tlsCallBackAddr;
118 while(*pCallback) {
119 dprintf(("tlsDetachThread: calling TLS Callback %x", *pCallback));
120
121 (*pCallback)((LPVOID)hinstance, DLL_THREAD_DETACH, 0);
122
123 dprintf(("tlsDetachThread: finished calling TLS Callback %x", *pCallback));
124 *pCallback++;
125 }
126 }
127 winteb = (TEB *)*TIBFlatPtr;
128 VirtualFree(winteb->tls_ptr[tlsIndex], tlsTotalSize, MEM_RELEASE);
129 winteb->tls_ptr[tlsIndex] = 0;
130}
131//******************************************************************************
132//******************************************************************************
133
134//******************************************************************************
135//******************************************************************************
136DWORD WIN32API TlsAlloc()
137{
138 DWORD index;
139
140 index = O32_TlsAlloc();
141 dprintf(("KERNEL32: TlsAlloc returned %d", index));
142 return index;
143}
144//******************************************************************************
145//******************************************************************************
146BOOL WIN32API TlsFree(DWORD index)
147{
148 dprintf(("KERNEL32: TlsFree %d", index));
149 return(O32_TlsFree(index));
150}
151//******************************************************************************
152//******************************************************************************
153LPVOID WIN32API TlsGetValue(DWORD index)
154{
155 LPVOID rc;
156
157 rc = O32_TlsGetValue(index);
158 dprintf2(("KERNEL32: TlsGetValue %d returned %X\n", index, rc));
159 return(rc);
160}
161//******************************************************************************
162//******************************************************************************
163BOOL WIN32API TlsSetValue(DWORD index, LPVOID val)
164{
165 dprintf2(("KERNEL32: TlsSetValue\n"));
166 return(O32_TlsSetValue(index, val));
167}
168//******************************************************************************
169//******************************************************************************
Note: See TracBrowser for help on using the repository browser.