1 | /* $Id: windlllx.cpp,v 1.17 2001-02-09 18:31:05 sandervl Exp $ */
|
---|
2 |
|
---|
3 | /*
|
---|
4 | * Win32 LX Dll class (compiled in OS/2 using Odin32 api)
|
---|
5 | *
|
---|
6 | * Copyright 1999-2000 Sander van Leeuwen (sandervl@xs4all.nl)
|
---|
7 | *
|
---|
8 | * TODO: Unloading of dlls probably needs to be fixed due to OS/2 bug
|
---|
9 | * (wrong unload order of dlls)
|
---|
10 | *
|
---|
11 | * Project Odin Software License can be found in LICENSE.TXT
|
---|
12 | *
|
---|
13 | */
|
---|
14 | #define INCL_DOSFILEMGR /* File Manager values */
|
---|
15 | #define INCL_DOSERRORS /* DOS Error values */
|
---|
16 | #define INCL_DOSPROCESS /* DOS Process values */
|
---|
17 | #define INCL_DOSMODULEMGR
|
---|
18 | #define INCL_DOSMISC /* DOS Miscellanous values */
|
---|
19 | #define INCL_WIN
|
---|
20 | #include <os2wrap.h> //Odin32 OS/2 api wrappers
|
---|
21 | #include <os2newapi.h>
|
---|
22 | #include <stdio.h>
|
---|
23 | #include <string.h>
|
---|
24 | #include <stdlib.h>
|
---|
25 | #include <iostream.h>
|
---|
26 | #include <fstream.h>
|
---|
27 | #include <misc.h>
|
---|
28 | #include <win32type.h>
|
---|
29 | #include <windllbase.h>
|
---|
30 | #include <windlllx.h>
|
---|
31 | #include "winexepe2lx.h"
|
---|
32 | #include "winexepeldr.h"
|
---|
33 | #include <odinlx.h>
|
---|
34 | #include <wprocess.h>
|
---|
35 | #include "oslibmisc.h"
|
---|
36 |
|
---|
37 | #include <exe386.h>
|
---|
38 |
|
---|
39 | #define DBG_LOCALLOG DBG_windlllx
|
---|
40 | #include "dbglocal.h"
|
---|
41 |
|
---|
42 | //******************************************************************************
|
---|
43 | //Create LX Dll object and send process attach message
|
---|
44 | //System dlls set EntryPoint to 0
|
---|
45 | //Parameters:
|
---|
46 | // HINSTANCE hInstance - OS/2 module handle
|
---|
47 | // WIN32DLLENTRY EntryPoint - Win32 dll entrypoint address
|
---|
48 | // PVOID pResData - pointer to win32 resource data
|
---|
49 | // DWORD MajorImageVersion - major image/os version (for fake win32 header)
|
---|
50 | // DWORD MinorImageVersion - minor image/os version (for fake win32 header)
|
---|
51 | // DWORD Subsystem - subsystem type (for fake win32 header)
|
---|
52 | // (IMAGE_SUBSYSTEM_WINDOWS_CUI/IMAGE_SUBSYSTEM_WINDOWS_GUI)
|
---|
53 | //
|
---|
54 | //Returns: Odin32 module handle
|
---|
55 | //******************************************************************************
|
---|
56 | DWORD WIN32API RegisterLxDll(HINSTANCE hInstance, WIN32DLLENTRY EntryPoint,
|
---|
57 | PVOID pResData,
|
---|
58 | DWORD MajorImageVersion,
|
---|
59 | DWORD MinorImageVersion,
|
---|
60 | DWORD Subsystem)
|
---|
61 | {
|
---|
62 | APIRET rc;
|
---|
63 | Win32LxDll *windll;
|
---|
64 | Win32DllBase *windlldep;
|
---|
65 | char szFileName[CCHMAXPATH], szErrName[CCHMAXPATH];
|
---|
66 |
|
---|
67 | if(OSLibGetDllName(hInstance, szFileName, sizeof(szFileName)) == FALSE) {
|
---|
68 | dprintf(("ERROR: RegisterLxDll: OSLibGetDllName %x failed!!", hInstance));
|
---|
69 | return 0;
|
---|
70 | }
|
---|
71 | //Make sure DosLoadModule is called at least
|
---|
72 | //once for a dll (to make sure OS/2 doesn't unload the dll when it's
|
---|
73 | //still needed)
|
---|
74 | //testestest
|
---|
75 | rc = DosLoadModule(szErrName, sizeof(szErrName), szFileName, &hInstance);
|
---|
76 | if(rc != 0) {
|
---|
77 | dprintf(("ERROR: RegisterLxDll: DosLoadModule %s failed (rc=%d)!!", szFileName, rc));
|
---|
78 | return 0;
|
---|
79 | }
|
---|
80 | //testestest
|
---|
81 | windll = new Win32LxDll(hInstance, EntryPoint, pResData, MajorImageVersion,
|
---|
82 | MinorImageVersion, Subsystem);
|
---|
83 | if(windll == NULL) {
|
---|
84 | dprintf(("RegisterLxDll: windll == NULL!!!"));
|
---|
85 | return 0;
|
---|
86 | }
|
---|
87 | if(!fPeLoader) {
|
---|
88 | windll->AddRef();
|
---|
89 |
|
---|
90 | if(windll->attachProcess() == 0)
|
---|
91 | return 0;
|
---|
92 |
|
---|
93 | return windll->getInstanceHandle();
|
---|
94 | }
|
---|
95 | IMAGE_DOS_HEADER doshdr;
|
---|
96 | struct e32_exe lxhdr;
|
---|
97 | ULONG offset;
|
---|
98 | char modulename[CCHMAXPATH];
|
---|
99 | char modsize;
|
---|
100 | int i;
|
---|
101 |
|
---|
102 | //SvL: This code reads the import name table of the dll to get the dependencies
|
---|
103 | // on other dlls.
|
---|
104 | //DosQueryHeaderInfo is an undocumented api, but works very well.
|
---|
105 | //(no need to save FS here as we'll return to OS/2 immediately)
|
---|
106 | rc = DosQueryHeaderInfo(hInstance, 0, &doshdr, sizeof(IMAGE_DOS_HEADER), QHINF_READFILE);
|
---|
107 | if(rc) {
|
---|
108 | goto hdrerror;
|
---|
109 | }
|
---|
110 | rc = DosQueryHeaderInfo(hInstance, doshdr.e_lfanew, &lxhdr, sizeof(e32_exe), QHINF_READFILE);
|
---|
111 | if(rc) {
|
---|
112 | goto hdrerror;
|
---|
113 | }
|
---|
114 | offset = doshdr.e_lfanew + lxhdr.e32_impmod;
|
---|
115 | for(i=0;i<lxhdr.e32_impmodcnt;i++) {
|
---|
116 | rc = DosQueryHeaderInfo(hInstance, offset, &modsize, 1, QHINF_READFILE);
|
---|
117 | if(rc) {
|
---|
118 | goto hdrerror;
|
---|
119 | }
|
---|
120 | rc = DosQueryHeaderInfo(hInstance, offset+1, &modulename, min(modsize, sizeof(modulename)), QHINF_READFILE);
|
---|
121 | if(rc) {
|
---|
122 | goto hdrerror;
|
---|
123 | }
|
---|
124 | modulename[modsize] = 0;
|
---|
125 | windlldep = Win32DllBase::findModule(modulename, TRUE);
|
---|
126 | if(windlldep) {
|
---|
127 | dprintf(("RegisterLxDll: Add dependency %s -> %s", windll->getModuleName(), modulename));
|
---|
128 | windll->addDependency(windlldep);
|
---|
129 | }
|
---|
130 | else dprintf(("WARNING: Can't find dll %s referenced by %s", modulename, windll->getModuleName()));
|
---|
131 | offset += modsize + 1;
|
---|
132 | }
|
---|
133 | return windll->getInstanceHandle();
|
---|
134 |
|
---|
135 | hdrerror:
|
---|
136 | dprintf(("DosQueryHeaderInfo returned %d", rc));
|
---|
137 | return windll->getInstanceHandle();
|
---|
138 | }
|
---|
139 | //******************************************************************************
|
---|
140 | //Destroy LX Dll object
|
---|
141 | //******************************************************************************
|
---|
142 | BOOL WIN32API UnregisterLxDll(HINSTANCE hInstance)
|
---|
143 | {
|
---|
144 | Win32DllBase *windll;
|
---|
145 |
|
---|
146 | //Don't proceed for pe2lx/win32k (os/2 dll unload dependency bug)
|
---|
147 | //Don't do it either after ExitProcess has been called
|
---|
148 | if(!fPeLoader || WinExe == NULL)
|
---|
149 | return TRUE;
|
---|
150 |
|
---|
151 | windll = Win32DllBase::findModule(hInstance);
|
---|
152 | if(!windll) {
|
---|
153 | dprintf(("UnregisterLxDll: Can't find dll with handle %x (already deleted)", hInstance));
|
---|
154 | return TRUE; //already deleted by Win32LxDll::Release
|
---|
155 | }
|
---|
156 | dprintf(("UnregisterLxDll %s", windll->getModuleName()));
|
---|
157 | //This can only happen for LX dependencies (i.e. wininet loads wsock32)
|
---|
158 | delete windll;
|
---|
159 | return TRUE;
|
---|
160 | }
|
---|
161 | //******************************************************************************
|
---|
162 | //******************************************************************************
|
---|
163 | Win32LxDll::Win32LxDll(HINSTANCE hInstance, WIN32DLLENTRY EntryPoint, PVOID pResData,
|
---|
164 | DWORD MajorImageVersion, DWORD MinorImageVersion,
|
---|
165 | DWORD Subsystem)
|
---|
166 | : Win32ImageBase(hInstance),
|
---|
167 | Win32LxImage(hInstance, pResData),
|
---|
168 | Win32DllBase(hInstance, EntryPoint)
|
---|
169 | {
|
---|
170 | this->MajorImageVersion = MajorImageVersion;
|
---|
171 | this->MinorImageVersion = MinorImageVersion;
|
---|
172 | this->Subsystem = Subsystem;
|
---|
173 |
|
---|
174 | if(EntryPoint == NULL) {
|
---|
175 | fSkipThreadEntryCalls = TRUE;
|
---|
176 | fAttachedToProcess = TRUE;
|
---|
177 | }
|
---|
178 | hinstanceOS2 = hInstance;
|
---|
179 | //new win32 instance handle must be pointer to PE header
|
---|
180 | hinstance = (HINSTANCE)buildHeader(MajorImageVersion, MinorImageVersion,
|
---|
181 | Subsystem);
|
---|
182 | dprintf(("Fake PE header %x for dll %s", hinstance, getModuleName()));
|
---|
183 | }
|
---|
184 | //******************************************************************************
|
---|
185 | //******************************************************************************
|
---|
186 | Win32LxDll::~Win32LxDll()
|
---|
187 | {
|
---|
188 | }
|
---|
189 | //******************************************************************************
|
---|
190 | //Returns reference count or -1 if load failed (PE loader only!)
|
---|
191 | //******************************************************************************
|
---|
192 | #ifdef DEBUG
|
---|
193 | ULONG Win32LxDll::AddRef(char *parentname)
|
---|
194 | #else
|
---|
195 | ULONG Win32LxDll::AddRef()
|
---|
196 | #endif
|
---|
197 | {
|
---|
198 | Win32DllBase *dll;
|
---|
199 | QueueItem *item;
|
---|
200 | ULONG ret;
|
---|
201 |
|
---|
202 | #ifdef DEBUG
|
---|
203 | ret = Win32DllBase::AddRef(parentname);
|
---|
204 | #else
|
---|
205 | ret = Win32DllBase::AddRef();
|
---|
206 | #endif
|
---|
207 |
|
---|
208 | if(!fPeLoader)
|
---|
209 | return ret;
|
---|
210 |
|
---|
211 | if(referenced == 1)
|
---|
212 | {
|
---|
213 | item = loadedDlls.Head();
|
---|
214 | while(item) {
|
---|
215 | dll = (Win32DllBase *)loadedDlls.getItem(item);
|
---|
216 | if(dll == NULL) {
|
---|
217 | dprintf(("ERROR: Win32DllBase::AddRef: dll item == NULL!!"));
|
---|
218 | DebugInt3();
|
---|
219 | return -1;
|
---|
220 | }
|
---|
221 | #ifdef DEBUG
|
---|
222 | dll->AddRef(getModuleName());
|
---|
223 | #else
|
---|
224 | dll->AddRef();
|
---|
225 | #endif
|
---|
226 | item = loadedDlls.getNext(item);
|
---|
227 | }
|
---|
228 | if(attachProcess() == 0) {
|
---|
229 | dprintf(("WARNING: Dll %s refused to be loaded; aborting", getName()));
|
---|
230 | return -1;
|
---|
231 | }
|
---|
232 | }
|
---|
233 | return ret;
|
---|
234 | }
|
---|
235 | //******************************************************************************
|
---|
236 | //******************************************************************************
|
---|
237 | ULONG Win32LxDll::Release()
|
---|
238 | {
|
---|
239 | HINSTANCE hinst;
|
---|
240 | WIN32DLLENTRY EntryPointTmp = dllEntryPoint;
|
---|
241 | PVOID pResDataTmp = (PVOID)pResRootDir;
|
---|
242 | DWORD MajorImageVersionTmp = MajorImageVersion;
|
---|
243 | DWORD MinorImageVersionTmp = MinorImageVersion;
|
---|
244 | DWORD SubsystemTmp = Subsystem;
|
---|
245 | ULONG ret;
|
---|
246 | APIRET rc;
|
---|
247 | BOOL fNoUnload = fDisableUnload; //only set for kernel32.dll
|
---|
248 | Win32LxDll *pModule;
|
---|
249 |
|
---|
250 | hinst = hinstanceOS2;
|
---|
251 | ret = Win32DllBase::Release();
|
---|
252 | if(ret == 0 && !fNoUnload) {//only set for kernel32.dll (fDisableUnload)
|
---|
253 | //DosFreeModule sends a termination message to the dll.
|
---|
254 | //The LX dll informs us when it's removed (UnregisterDll call)
|
---|
255 | rc = DosFreeModule(hinst);
|
---|
256 | if(rc) {
|
---|
257 | dprintf(("Win32LxDll::Release: DosFreeModule %x returned %d", hinst, rc));
|
---|
258 | if(rc == ERROR_INVALID_ACCESS && !fExitProcess) {
|
---|
259 | //Dll refused to unload because it has an active exitlist handler
|
---|
260 | //or depends on a dll that registered an exitlist handler
|
---|
261 | //In this case the handle remains valid and the entrypoint of
|
---|
262 | //the dll is NOT called for DLL_PROCESS_DETACH
|
---|
263 | //WORKAROUND: Re-register the dll so future LoadLibrary calls
|
---|
264 | // don't fail!
|
---|
265 | dprintf(("WORKAROUND: Re-register the dll so future LoadLibrary calls don't fail!"));
|
---|
266 | RegisterLxDll(hinst, EntryPointTmp, pResDataTmp,
|
---|
267 | MajorImageVersionTmp,
|
---|
268 | MinorImageVersionTmp,
|
---|
269 | SubsystemTmp);
|
---|
270 |
|
---|
271 | /* OS/2 dll, system dll, converted dll or win32k took care of it. */
|
---|
272 | pModule = Win32LxDll::findModuleByOS2Handle(hinst);
|
---|
273 | if(pModule)
|
---|
274 | {
|
---|
275 | pModule->setDllHandleOS2(hinst);
|
---|
276 | if(fPeLoader)
|
---|
277 | {
|
---|
278 | if(pModule->AddRef() == -1) {//-1 -> load failed (attachProcess)
|
---|
279 | dprintf(("ERROR Dll refused to be loaded; aborting"));
|
---|
280 | DebugInt3();
|
---|
281 | delete pModule;
|
---|
282 | return 0;
|
---|
283 | }
|
---|
284 | }
|
---|
285 | pModule->incDynamicLib();
|
---|
286 | }
|
---|
287 | else DebugInt3();
|
---|
288 | }
|
---|
289 | }
|
---|
290 | }
|
---|
291 | return(ret);
|
---|
292 | }
|
---|
293 | //******************************************************************************
|
---|
294 | //******************************************************************************
|
---|
295 | BOOL Win32LxDll::isLxDll()
|
---|
296 | {
|
---|
297 | return TRUE;
|
---|
298 | }
|
---|
299 | //******************************************************************************
|
---|
300 | //******************************************************************************
|
---|
301 | void Win32LxDll::setDllHandleOS2(HINSTANCE hInstanceOS2)
|
---|
302 | {
|
---|
303 | //Loaded with LoadLibrary(Ex); no need for a 2nd DosLoadModule
|
---|
304 | //Dlls that are indirectly loaded (i.e. GDI32->KERNEL32 dependancy) need
|
---|
305 | //this additional DosLoadModule (and setDllHandleOS2 isn't called for those)
|
---|
306 | if(this->hinstanceOS2) {
|
---|
307 | DosFreeModule(this->hinstanceOS2);
|
---|
308 | }
|
---|
309 | this->hinstanceOS2 = hInstanceOS2;
|
---|
310 | }
|
---|
311 | //******************************************************************************
|
---|
312 | //******************************************************************************
|
---|
313 | Win32LxDll *Win32LxDll::findModuleByOS2Handle(HINSTANCE hinstance)
|
---|
314 | {
|
---|
315 | dlllistmutex.enter();
|
---|
316 |
|
---|
317 | Win32DllBase *mod = Win32DllBase::getFirst();
|
---|
318 | while(mod != NULL) {
|
---|
319 | if(mod->isLxDll()) {
|
---|
320 | Win32LxDll *lxdll = (Win32LxDll *)mod;
|
---|
321 | if(lxdll->hinstanceOS2 == hinstance) {
|
---|
322 | dlllistmutex.leave();
|
---|
323 | return(lxdll);
|
---|
324 | }
|
---|
325 | }
|
---|
326 | mod = mod->getNext();
|
---|
327 | }
|
---|
328 | dlllistmutex.leave();
|
---|
329 | return(NULL);
|
---|
330 | }
|
---|
331 | //******************************************************************************
|
---|
332 | //******************************************************************************
|
---|