| 1 | /* $Id: windllbase.cpp,v 1.16 2000-08-11 10:56:18 sandervl Exp $ */
|
|---|
| 2 |
|
|---|
| 3 | /*
|
|---|
| 4 | * Win32 Dll base class
|
|---|
| 5 | *
|
|---|
| 6 | * Copyright 1998-1999 Sander van Leeuwen (sandervl@xs4all.nl)
|
|---|
| 7 | *
|
|---|
| 8 | * Unloading of a dll always happens in order of dependency (taking nr of
|
|---|
| 9 | * loads into account)
|
|---|
| 10 | * Unloading of dynamically loaded dll (with LoadLibrary) in deleteAll
|
|---|
| 11 | * is done in LIFO order (NT exhibits the same behaviour)
|
|---|
| 12 | *
|
|---|
| 13 | * RemoveCircularDependency: TODO: Send process detach message here??
|
|---|
| 14 | *
|
|---|
| 15 | * Project Odin Software License can be found in LICENSE.TXT
|
|---|
| 16 | *
|
|---|
| 17 | */
|
|---|
| 18 | #define INCL_DOSFILEMGR /* File Manager values */
|
|---|
| 19 | #define INCL_DOSERRORS /* DOS Error values */
|
|---|
| 20 | #define INCL_DOSPROCESS /* DOS Process values */
|
|---|
| 21 | #define INCL_DOSMODULEMGR
|
|---|
| 22 | #define INCL_DOSMISC /* DOS Miscellanous values */
|
|---|
| 23 | #define INCL_WIN
|
|---|
| 24 | #include <os2wrap.h> //Odin32 OS/2 api wrappers
|
|---|
| 25 | #include <stdio.h>
|
|---|
| 26 | #include <string.h>
|
|---|
| 27 | #include <stdlib.h>
|
|---|
| 28 | #include <iostream.h>
|
|---|
| 29 | #include <fstream.h>
|
|---|
| 30 | #include <misc.h>
|
|---|
| 31 | #include <win32api.h>
|
|---|
| 32 | #include <pefile.h>
|
|---|
| 33 | #include <windllbase.h>
|
|---|
| 34 | #include <wprocess.h>
|
|---|
| 35 | #include "exceptions.h"
|
|---|
| 36 | #include "exceptutil.h"
|
|---|
| 37 | #include "cio.h"
|
|---|
| 38 | #include "vmutex.h"
|
|---|
| 39 | #include "oslibmisc.h"
|
|---|
| 40 | #include "oslibdos.h"
|
|---|
| 41 | #include "profile.h"
|
|---|
| 42 |
|
|---|
| 43 | #define DBG_LOCALLOG DBG_windllbase
|
|---|
| 44 | #include "dbglocal.h"
|
|---|
| 45 |
|
|---|
| 46 | VMutex dlllistmutex; //protects linked lists of heaps
|
|---|
| 47 |
|
|---|
| 48 | //******************************************************************************
|
|---|
| 49 | //******************************************************************************
|
|---|
| 50 | Win32DllBase::Win32DllBase(HINSTANCE hinstance, WIN32DLLENTRY DllEntryPoint,
|
|---|
| 51 | Win32ImageBase *parent)
|
|---|
| 52 | : Win32ImageBase(hinstance),
|
|---|
| 53 | referenced(0), fSkipThreadEntryCalls(FALSE), next(NULL), fInserted(FALSE),
|
|---|
| 54 | fAttachedToProcess(FALSE), fUnloaded(FALSE),
|
|---|
| 55 | nrDynamicLibRef(0), fDisableUnload(FALSE)
|
|---|
| 56 | {
|
|---|
| 57 | dllEntryPoint = DllEntryPoint;
|
|---|
| 58 |
|
|---|
| 59 | setUnloadOrder(parent);
|
|---|
| 60 |
|
|---|
| 61 | dprintf(("Win32DllBase::Win32DllBase %x %s", hinstance, szModule));
|
|---|
| 62 | }
|
|---|
| 63 | //******************************************************************************
|
|---|
| 64 | //******************************************************************************
|
|---|
| 65 | Win32DllBase::~Win32DllBase()
|
|---|
| 66 | {
|
|---|
| 67 | dprintf(("Win32DllBase::~Win32DllBase %s", szModule));
|
|---|
| 68 |
|
|---|
| 69 | if(errorState == NO_ERROR && !fUnloaded)
|
|---|
| 70 | {
|
|---|
| 71 | detachProcess();
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|
| 74 | dlllistmutex.enter();
|
|---|
| 75 | if(head == this) {
|
|---|
| 76 | head = next;
|
|---|
| 77 | }
|
|---|
| 78 | else {
|
|---|
| 79 | Win32DllBase *dll = head;
|
|---|
| 80 | while(dll && dll->next != this) {
|
|---|
| 81 | dll = dll->next;
|
|---|
| 82 | }
|
|---|
| 83 | if(dll == NULL) {
|
|---|
| 84 | dprintf(("~Win32DllBase: Can't find dll!\n"));
|
|---|
| 85 | dlllistmutex.leave();
|
|---|
| 86 | return;
|
|---|
| 87 | }
|
|---|
| 88 | dll->next = next;
|
|---|
| 89 | }
|
|---|
| 90 | dlllistmutex.leave();
|
|---|
| 91 | }
|
|---|
| 92 | //******************************************************************************
|
|---|
| 93 | //******************************************************************************
|
|---|
| 94 | void Win32DllBase::incDynamicLib()
|
|---|
| 95 | {
|
|---|
| 96 | if(nrDynamicLibRef == 0) {
|
|---|
| 97 | //NOTE:
|
|---|
| 98 | //Must be called *after* attachprocess, since attachprocess may also
|
|---|
| 99 | //trigger LoadLibrary calls
|
|---|
| 100 | //Those dlls must not be put in front of this dll in the dynamic
|
|---|
| 101 | //dll list; or else the unload order is wrong:
|
|---|
| 102 | //i.e. RPAP3260 loads PNRS3260 in DLL_PROCESS_ATTACH
|
|---|
| 103 | // this means that in ExitProcess, PNRS3260 needs to be removed
|
|---|
| 104 | // first since RPAP3260 depends on it
|
|---|
| 105 |
|
|---|
| 106 | dlllistmutex.enter();
|
|---|
| 107 | loadLibDlls.Push((ULONG)this);
|
|---|
| 108 | dlllistmutex.leave();
|
|---|
| 109 | }
|
|---|
| 110 | nrDynamicLibRef++;
|
|---|
| 111 | }
|
|---|
| 112 | //******************************************************************************
|
|---|
| 113 | //******************************************************************************
|
|---|
| 114 | void Win32DllBase::decDynamicLib()
|
|---|
| 115 | {
|
|---|
| 116 | nrDynamicLibRef--;
|
|---|
| 117 | if(nrDynamicLibRef == 0) {
|
|---|
| 118 | dlllistmutex.enter();
|
|---|
| 119 | loadLibDlls.Remove((ULONG)this);
|
|---|
| 120 | dlllistmutex.leave();
|
|---|
| 121 | }
|
|---|
| 122 | }
|
|---|
| 123 | //******************************************************************************
|
|---|
| 124 | //unload of dlls needs to be done in reverse order of dependencies
|
|---|
| 125 | //Note: Only necessary for pe loader; the OS/2 loader takes care of this
|
|---|
| 126 | //for win32k/pe2lx
|
|---|
| 127 | //******************************************************************************
|
|---|
| 128 | void Win32DllBase::setUnloadOrder(Win32ImageBase *parent)
|
|---|
| 129 | {
|
|---|
| 130 | Win32DllBase *dll;
|
|---|
| 131 | Win32DllBase *parentdll = NULL;
|
|---|
| 132 |
|
|---|
| 133 | dlllistmutex.enter();
|
|---|
| 134 | if(parent) {
|
|---|
| 135 | dll = head;
|
|---|
| 136 | while(dll) {
|
|---|
| 137 | if(dll->getInstanceHandle() == parent->getInstanceHandle()) {
|
|---|
| 138 | parentdll = dll;
|
|---|
| 139 | break;
|
|---|
| 140 | }
|
|---|
| 141 | dll = dll->next;
|
|---|
| 142 | }
|
|---|
| 143 | }
|
|---|
| 144 |
|
|---|
| 145 | //first check if this dll is already at a lower position (further down the list)
|
|---|
| 146 | //than the parent
|
|---|
| 147 | if(parentdll && fInserted) {//already in the list?
|
|---|
| 148 | dll = parentdll->next;
|
|---|
| 149 | while(dll) {
|
|---|
| 150 | if(dll->getInstanceHandle() == getInstanceHandle()) {
|
|---|
| 151 | dlllistmutex.leave();
|
|---|
| 152 | return; //it's at a lower position, so no need to change anything
|
|---|
| 153 | }
|
|---|
| 154 | dll = dll->next;
|
|---|
| 155 | }
|
|---|
| 156 |
|
|---|
| 157 | //it's already in the list but not at the right position; remove it now
|
|---|
| 158 | if(head == this) {
|
|---|
| 159 | head = next;
|
|---|
| 160 | }
|
|---|
| 161 | else {
|
|---|
| 162 | dll = head;
|
|---|
| 163 | while(dll->next) {
|
|---|
| 164 | if(dll->next == this) {
|
|---|
| 165 | dll->next = next;
|
|---|
| 166 | break;
|
|---|
| 167 | }
|
|---|
| 168 | dll = dll->next;
|
|---|
| 169 | }
|
|---|
| 170 | }
|
|---|
| 171 | }
|
|---|
| 172 | else
|
|---|
| 173 | if(fInserted) {//already in the list?
|
|---|
| 174 | dlllistmutex.leave();
|
|---|
| 175 | return;
|
|---|
| 176 | }
|
|---|
| 177 | //(re)insert it in the list after it's parent
|
|---|
| 178 | if(parentdll) {
|
|---|
| 179 | next = parentdll->next;
|
|---|
| 180 | parentdll->next = this;
|
|---|
| 181 | }
|
|---|
| 182 | else {//no parent or exe, just add it at the start of the list
|
|---|
| 183 | next = head;
|
|---|
| 184 | head = this;
|
|---|
| 185 | }
|
|---|
| 186 | fInserted = TRUE;
|
|---|
| 187 |
|
|---|
| 188 | //Now do the same thing for the child dependencies
|
|---|
| 189 | QueueItem *item;
|
|---|
| 190 |
|
|---|
| 191 | item = loadedDlls.Head();
|
|---|
| 192 | while(item) {
|
|---|
| 193 | dll = (Win32DllBase *)loadedDlls.getItem(item);
|
|---|
| 194 | //Check for circular dependencies (i.e. in Lotus Notes)
|
|---|
| 195 | if(dll != parentdll) {
|
|---|
| 196 | dll->setUnloadOrder(this);
|
|---|
| 197 | }
|
|---|
| 198 |
|
|---|
| 199 | item = loadedDlls.getNext(item);
|
|---|
| 200 | }
|
|---|
| 201 |
|
|---|
| 202 | dlllistmutex.leave();
|
|---|
| 203 | }
|
|---|
| 204 | //******************************************************************************
|
|---|
| 205 | //******************************************************************************
|
|---|
| 206 | #ifdef DEBUG
|
|---|
| 207 | ULONG Win32DllBase::AddRef(char *parentname)
|
|---|
| 208 | #else
|
|---|
| 209 | ULONG Win32DllBase::AddRef()
|
|---|
| 210 | #endif
|
|---|
| 211 | {
|
|---|
| 212 | Win32DllBase *dll;
|
|---|
| 213 |
|
|---|
| 214 | dprintf(("Win32DllBase::AddRef %s->%s %d", parentname, getModuleName(), referenced+1));
|
|---|
| 215 | ++referenced;
|
|---|
| 216 | #ifdef DEBUG
|
|---|
| 217 | if(referenced == 1) {
|
|---|
| 218 | #ifdef DEBUG_ENABLELOG_LEVEL2
|
|---|
| 219 | printListOfDlls();
|
|---|
| 220 | #endif
|
|---|
| 221 | //printDependencies(NULL);
|
|---|
| 222 | }
|
|---|
| 223 | #endif
|
|---|
| 224 |
|
|---|
| 225 | return referenced;
|
|---|
| 226 | }
|
|---|
| 227 | //******************************************************************************
|
|---|
| 228 | //******************************************************************************
|
|---|
| 229 | ULONG Win32DllBase::Release()
|
|---|
| 230 | {
|
|---|
| 231 | Queue queue;
|
|---|
| 232 | QueueItem *item;
|
|---|
| 233 | Win32DllBase *dll;
|
|---|
| 234 | LONG ret;
|
|---|
| 235 |
|
|---|
| 236 | dprintf(("Win32DllBase::Release %s %d", getModuleName(), referenced-1));
|
|---|
| 237 |
|
|---|
| 238 | ret = --referenced;
|
|---|
| 239 | if(ret <= 0) {
|
|---|
| 240 | //make copy of linked list of dependencies
|
|---|
| 241 | queue = loadedDlls;
|
|---|
| 242 |
|
|---|
| 243 | //remove any circular dependencies on this dll that might be present
|
|---|
| 244 | item = queue.Head();
|
|---|
| 245 | while(item) {
|
|---|
| 246 | dll = (Win32DllBase *)queue.getItem(item);
|
|---|
| 247 | if(dll == NULL) {
|
|---|
| 248 | dprintf(("ERROR: Win32DllBase::Release: dll item == NULL!!"));
|
|---|
| 249 | DebugInt3();
|
|---|
| 250 | return -1;
|
|---|
| 251 | }
|
|---|
| 252 | dll->RemoveCircularDependency(this);
|
|---|
| 253 | item = queue.getNext(item);
|
|---|
| 254 | }
|
|---|
| 255 | #ifdef DEBUG
|
|---|
| 256 | //printDependencies(NULL);
|
|---|
| 257 | #endif
|
|---|
| 258 | dprintf(("Win32DllBase::Release %s referenced == 0", getModuleName()));
|
|---|
| 259 |
|
|---|
| 260 | //delete dll object
|
|---|
| 261 | delete this;
|
|---|
| 262 |
|
|---|
| 263 | //unreference all of it's dependencies
|
|---|
| 264 | item = queue.Head();
|
|---|
| 265 | while(item) {
|
|---|
| 266 | dll = (Win32DllBase *)queue.getItem(item);
|
|---|
| 267 | if(dll == NULL) {
|
|---|
| 268 | dprintf(("ERROR: Win32DllBase::Release: dll item == NULL!!"));
|
|---|
| 269 | DebugInt3();
|
|---|
| 270 | return -1;
|
|---|
| 271 | }
|
|---|
| 272 | dll->Release();
|
|---|
| 273 | item = queue.getNext(item);
|
|---|
| 274 | }
|
|---|
| 275 | }
|
|---|
| 276 | return(ret);
|
|---|
| 277 | }
|
|---|
| 278 | //******************************************************************************
|
|---|
| 279 | //Lotus Notes has several ugly circular dependencies in it's dlls.
|
|---|
| 280 | //Remove them before they cause problems.
|
|---|
| 281 | //******************************************************************************
|
|---|
| 282 | BOOL Win32DllBase::RemoveCircularDependency(Win32DllBase *parent)
|
|---|
| 283 | {
|
|---|
| 284 | QueueItem *item, *tmp;
|
|---|
| 285 | Win32DllBase *dll;
|
|---|
| 286 | BOOL ret = FALSE;
|
|---|
| 287 |
|
|---|
| 288 | //remove any circular dependencies on this dll that might be present
|
|---|
| 289 | item = loadedDlls.Head();
|
|---|
| 290 | while(item) {
|
|---|
| 291 | dll = (Win32DllBase *)loadedDlls.getItem(item);
|
|---|
| 292 | if(dll == NULL) {
|
|---|
| 293 | dprintf(("ERROR: Win32DllBase::Release: dll item == NULL!!"));
|
|---|
| 294 | DebugInt3();
|
|---|
| 295 | return FALSE;
|
|---|
| 296 | }
|
|---|
| 297 | tmp = loadedDlls.getNext(item);
|
|---|
| 298 | if(dll == parent) {
|
|---|
| 299 | dprintf(("Removing CIRCULAR dependency %s->%s", parent->getModuleName(), dll->getModuleName()));
|
|---|
| 300 | loadedDlls.Remove(item);
|
|---|
| 301 | ret = TRUE;
|
|---|
| 302 | }
|
|---|
| 303 | //// else ret |= dll->RemoveCircularDependency(parent);
|
|---|
| 304 | item = tmp;
|
|---|
| 305 | }
|
|---|
| 306 | //TODO: Send process detach message here??
|
|---|
| 307 | return ret;
|
|---|
| 308 | }
|
|---|
| 309 | //******************************************************************************
|
|---|
| 310 | //There's a slight problem with our dependency procedure.
|
|---|
| 311 | //example: gdi32 is loaded -> depends on kernel32 (which is already loaded)
|
|---|
| 312 | // kernel32's reference count isn't updated
|
|---|
| 313 | // (when we load gdi32, we don't know which dlls it depends on and which
|
|---|
| 314 | // of those are already loaded and which aren't)
|
|---|
| 315 | //-> solution: Determine reference counts of dependant lx dlls and update those
|
|---|
| 316 | // reference counts
|
|---|
| 317 | //******************************************************************************
|
|---|
| 318 | void Win32DllBase::updateDependencies()
|
|---|
| 319 | {
|
|---|
| 320 | QueueItem *item;
|
|---|
| 321 | Win32DllBase *dll, *depdll;
|
|---|
| 322 | ULONG refcount;
|
|---|
| 323 |
|
|---|
| 324 | dlllistmutex.enter();
|
|---|
| 325 | item = loadedDlls.Head();
|
|---|
| 326 | while(item) {
|
|---|
| 327 | depdll = (Win32DllBase *)loadedDlls.getItem(item);
|
|---|
| 328 | if(depdll == NULL) {
|
|---|
| 329 | dprintf(("updateDependencies: depdll == NULL!!"));
|
|---|
| 330 | DebugInt3();
|
|---|
| 331 | return;
|
|---|
| 332 | }
|
|---|
| 333 | refcount = 0;
|
|---|
| 334 | dll = head;
|
|---|
| 335 |
|
|---|
| 336 | while(dll) {
|
|---|
| 337 | if(dll->dependsOn(depdll)) {
|
|---|
| 338 | refcount++;
|
|---|
| 339 | }
|
|---|
| 340 | dll = dll->getNext();
|
|---|
| 341 | }
|
|---|
| 342 | if(refcount > depdll->referenced) {
|
|---|
| 343 | dprintf(("Win32DllBase::updateDependencies changing refcount of %s to %d (old=%d)", depdll->getModuleName(), refcount, depdll->referenced));
|
|---|
| 344 | depdll->referenced = refcount;
|
|---|
| 345 | }
|
|---|
| 346 |
|
|---|
| 347 | item = loadedDlls.getNext(item);
|
|---|
| 348 | }
|
|---|
| 349 | dlllistmutex.leave();
|
|---|
| 350 | }
|
|---|
| 351 | //******************************************************************************
|
|---|
| 352 | //******************************************************************************
|
|---|
| 353 | #ifdef DEBUG
|
|---|
| 354 | void Win32DllBase::printDependencies(char *parent)
|
|---|
| 355 | {
|
|---|
| 356 | QueueItem *item;
|
|---|
| 357 | Win32DllBase *dll;
|
|---|
| 358 | ULONG ret;
|
|---|
| 359 |
|
|---|
| 360 | dprintf(("Dependency list: %s->%s %d", parent, getModuleName(), referenced));
|
|---|
| 361 | item = loadedDlls.Head();
|
|---|
| 362 | while(item) {
|
|---|
| 363 | dll = (Win32DllBase *)loadedDlls.getItem(item);
|
|---|
| 364 | if(dll == NULL) {
|
|---|
| 365 | return;
|
|---|
| 366 | }
|
|---|
| 367 | dll->printDependencies(getModuleName());
|
|---|
| 368 | item = loadedDlls.getNext(item);
|
|---|
| 369 | }
|
|---|
| 370 | }
|
|---|
| 371 | //******************************************************************************
|
|---|
| 372 | //******************************************************************************
|
|---|
| 373 | #ifdef DEBUG_ENABLELOG_LEVEL2
|
|---|
| 374 | void Win32DllBase::printListOfDlls()
|
|---|
| 375 | {
|
|---|
| 376 | Win32DllBase *dll;
|
|---|
| 377 |
|
|---|
| 378 | dll = head;
|
|---|
| 379 |
|
|---|
| 380 | dprintf2(("Win32DllBase::Win32DllBase: List of loaded dlls:"));
|
|---|
| 381 | while(dll) {
|
|---|
| 382 | dprintf2(("DLL %s %d", dll->szModule, dll->referenced));
|
|---|
| 383 | dll = dll->next;
|
|---|
| 384 | }
|
|---|
| 385 | }
|
|---|
| 386 | #endif
|
|---|
| 387 | #endif
|
|---|
| 388 | //******************************************************************************
|
|---|
| 389 | //******************************************************************************
|
|---|
| 390 | BOOL Win32DllBase::attachProcess()
|
|---|
| 391 | {
|
|---|
| 392 | WINEXCEPTION_FRAME exceptFrame;
|
|---|
| 393 | USHORT sel;
|
|---|
| 394 | THDB *thdb;
|
|---|
| 395 | BOOL rc, fSetExceptionHandler;
|
|---|
| 396 |
|
|---|
| 397 | if(fAttachedToProcess)
|
|---|
| 398 | return TRUE;
|
|---|
| 399 |
|
|---|
| 400 | fAttachedToProcess = TRUE;
|
|---|
| 401 |
|
|---|
| 402 | thdb = GetThreadTHDB();
|
|---|
| 403 | fSetExceptionHandler = (!thdb || thdb->teb_sel != GetFS());
|
|---|
| 404 |
|
|---|
| 405 | //Note: The Win32 exception structure references by FS:[0] is the same
|
|---|
| 406 | // in OS/2
|
|---|
| 407 | if(fSetExceptionHandler) {
|
|---|
| 408 | OS2SetExceptionHandler((void *)&exceptFrame);
|
|---|
| 409 | sel = SetWin32TIB();
|
|---|
| 410 | }
|
|---|
| 411 |
|
|---|
| 412 | //Allocate TLS index for this module
|
|---|
| 413 | tlsAlloc();
|
|---|
| 414 | tlsAttachThread(); //setup TLS (main thread)
|
|---|
| 415 |
|
|---|
| 416 | if(dllEntryPoint == NULL) {
|
|---|
| 417 | dprintf(("attachProcess not required for dll %s", szModule));
|
|---|
| 418 | if(fSetExceptionHandler) {
|
|---|
| 419 | SetFS(sel);
|
|---|
| 420 | OS2UnsetExceptionHandler((void *)&exceptFrame);
|
|---|
| 421 | }
|
|---|
| 422 | return(TRUE);
|
|---|
| 423 | }
|
|---|
| 424 |
|
|---|
| 425 | dprintf(("attachProcess to dll %s", szModule));
|
|---|
| 426 |
|
|---|
| 427 | // @@@PH 2000/06/13 lpvReserved, Starcraft STORM.DLL
|
|---|
| 428 | // if DLL_PROCESS_ATTACH, lpvReserved is NULL for dynamic loads
|
|---|
| 429 | // and non-NULL for static loads.
|
|---|
| 430 | // same goes for process termination
|
|---|
| 431 | LPVOID lpvReserved;
|
|---|
| 432 |
|
|---|
| 433 | if (isDynamicLib())
|
|---|
| 434 | lpvReserved = NULL;
|
|---|
| 435 | else
|
|---|
| 436 | lpvReserved = (LPVOID)0xdeadface; // some arbitrary value
|
|---|
| 437 |
|
|---|
| 438 | rc = dllEntryPoint(hinstance, DLL_PROCESS_ATTACH, lpvReserved);
|
|---|
| 439 |
|
|---|
| 440 | dprintf(("attachProcess to dll %s DONE", szModule));
|
|---|
| 441 |
|
|---|
| 442 | if(fSetExceptionHandler) {
|
|---|
| 443 | SetFS(sel);
|
|---|
| 444 | OS2UnsetExceptionHandler((void *)&exceptFrame);
|
|---|
| 445 | }
|
|---|
| 446 | else
|
|---|
| 447 | if(thdb) {
|
|---|
| 448 | if(thdb->teb_sel != GetFS()) {
|
|---|
| 449 | dprintf(("Win32DllBase::attachProcess: FS was changed by dll entrypoint!!!!"));
|
|---|
| 450 | DebugInt3();
|
|---|
| 451 | }
|
|---|
| 452 | }
|
|---|
| 453 | return rc;
|
|---|
| 454 | }
|
|---|
| 455 | //******************************************************************************
|
|---|
| 456 | //******************************************************************************
|
|---|
| 457 | BOOL Win32DllBase::detachProcess()
|
|---|
| 458 | {
|
|---|
| 459 | WINEXCEPTION_FRAME exceptFrame;
|
|---|
| 460 | USHORT sel;
|
|---|
| 461 | BOOL rc;
|
|---|
| 462 |
|
|---|
| 463 | if(dllEntryPoint == NULL) {
|
|---|
| 464 | tlsDetachThread(); //destroy TLS (main thread)
|
|---|
| 465 | fUnloaded = TRUE;
|
|---|
| 466 | return(TRUE);
|
|---|
| 467 | }
|
|---|
| 468 |
|
|---|
| 469 | dprintf(("detachProcess from dll %s", szModule));
|
|---|
| 470 |
|
|---|
| 471 | //Note: The Win32 exception structure references by FS:[0] is the same
|
|---|
| 472 | // in OS/2
|
|---|
| 473 | OS2SetExceptionHandler((void *)&exceptFrame);
|
|---|
| 474 |
|
|---|
| 475 | fUnloaded = TRUE;
|
|---|
| 476 | sel = SetWin32TIB();
|
|---|
| 477 |
|
|---|
| 478 | // @@@PH 2000/06/13 lpvReserved, Starcraft STORM.DLL
|
|---|
| 479 | // if DLL_PROCESS_ATTACH, lpvReserved is NULL for dynamic loads
|
|---|
| 480 | // and non-NULL for static loads.
|
|---|
| 481 | // same goes for process termination
|
|---|
| 482 | LPVOID lpvReserved;
|
|---|
| 483 |
|
|---|
| 484 | if (isDynamicLib())
|
|---|
| 485 | lpvReserved = NULL;
|
|---|
| 486 | else
|
|---|
| 487 | lpvReserved = (LPVOID)0xdeadface; // some arbitrary value
|
|---|
| 488 |
|
|---|
| 489 | rc = dllEntryPoint(hinstance, DLL_PROCESS_DETACH, lpvReserved);
|
|---|
| 490 |
|
|---|
| 491 | SetFS(sel);
|
|---|
| 492 | tlsDetachThread(); //destroy TLS (main thread)
|
|---|
| 493 | tlsDelete();
|
|---|
| 494 |
|
|---|
| 495 | OS2UnsetExceptionHandler((void *)&exceptFrame);
|
|---|
| 496 |
|
|---|
| 497 | return rc;
|
|---|
| 498 | }
|
|---|
| 499 | //******************************************************************************
|
|---|
| 500 | //******************************************************************************
|
|---|
| 501 | BOOL Win32DllBase::attachThread()
|
|---|
| 502 | {
|
|---|
| 503 | WINEXCEPTION_FRAME exceptFrame;
|
|---|
| 504 | BOOL rc;
|
|---|
| 505 |
|
|---|
| 506 | if(fSkipThreadEntryCalls || dllEntryPoint == NULL)
|
|---|
| 507 | return(TRUE);
|
|---|
| 508 |
|
|---|
| 509 | dprintf(("attachThread to dll %s", szModule));
|
|---|
| 510 |
|
|---|
| 511 | rc = dllEntryPoint(hinstance, DLL_THREAD_ATTACH, 0);
|
|---|
| 512 |
|
|---|
| 513 | dprintf(("attachThread to dll %s DONE", szModule));
|
|---|
| 514 |
|
|---|
| 515 | return rc;
|
|---|
| 516 | }
|
|---|
| 517 | //******************************************************************************
|
|---|
| 518 | //******************************************************************************
|
|---|
| 519 | BOOL Win32DllBase::detachThread()
|
|---|
| 520 | {
|
|---|
| 521 | WINEXCEPTION_FRAME exceptFrame;
|
|---|
| 522 | BOOL rc;
|
|---|
| 523 |
|
|---|
| 524 | if(fSkipThreadEntryCalls || dllEntryPoint == NULL)
|
|---|
| 525 | return(TRUE);
|
|---|
| 526 |
|
|---|
| 527 | dprintf(("detachThread from dll %s", szModule));
|
|---|
| 528 |
|
|---|
| 529 | rc = dllEntryPoint(hinstance, DLL_THREAD_DETACH, 0);
|
|---|
| 530 | return rc;
|
|---|
| 531 | }
|
|---|
| 532 | //******************************************************************************
|
|---|
| 533 | //Send DLL_THREAD_ATTACH message to all dlls for a new thread
|
|---|
| 534 | //******************************************************************************
|
|---|
| 535 | void Win32DllBase::attachThreadToAllDlls()
|
|---|
| 536 | {
|
|---|
| 537 | dlllistmutex.enter();
|
|---|
| 538 | Win32DllBase *dll = Win32DllBase::head;
|
|---|
| 539 | while(dll) {
|
|---|
| 540 | dll->attachThread();
|
|---|
| 541 | dll = dll->getNext();
|
|---|
| 542 | }
|
|---|
| 543 | dlllistmutex.leave();
|
|---|
| 544 | }
|
|---|
| 545 | //******************************************************************************
|
|---|
| 546 | //Send DLL_THREAD_DETACH message to all dlls for thread that's about to die
|
|---|
| 547 | //******************************************************************************
|
|---|
| 548 | void Win32DllBase::detachThreadFromAllDlls()
|
|---|
| 549 | {
|
|---|
| 550 | dlllistmutex.enter();
|
|---|
| 551 | Win32DllBase *dll = Win32DllBase::head;
|
|---|
| 552 | while(dll) {
|
|---|
| 553 | dll->detachThread();
|
|---|
| 554 | dll = dll->getNext();
|
|---|
| 555 | }
|
|---|
| 556 | dlllistmutex.leave();
|
|---|
| 557 | }
|
|---|
| 558 | //******************************************************************************
|
|---|
| 559 | //Setup TLS structure for all dlls for a new thread
|
|---|
| 560 | //******************************************************************************
|
|---|
| 561 | void Win32DllBase::tlsAttachThreadToAllDlls()
|
|---|
| 562 | {
|
|---|
| 563 | dlllistmutex.enter();
|
|---|
| 564 | Win32DllBase *dll = Win32DllBase::head;
|
|---|
| 565 | while(dll) {
|
|---|
| 566 | dll->tlsAttachThread();
|
|---|
| 567 | dll = dll->getNext();
|
|---|
| 568 | }
|
|---|
| 569 | dlllistmutex.leave();
|
|---|
| 570 | }
|
|---|
| 571 | //******************************************************************************
|
|---|
| 572 | //Destroy TLS structure for all dlls for a thread that's about to die
|
|---|
| 573 | //******************************************************************************
|
|---|
| 574 | void Win32DllBase::tlsDetachThreadFromAllDlls()
|
|---|
| 575 | {
|
|---|
| 576 | dlllistmutex.enter();
|
|---|
| 577 | Win32DllBase *dll = Win32DllBase::head;
|
|---|
| 578 | while(dll) {
|
|---|
| 579 | dll->tlsDetachThread();
|
|---|
| 580 | dll = dll->getNext();
|
|---|
| 581 | }
|
|---|
| 582 | dlllistmutex.leave();
|
|---|
| 583 | }
|
|---|
| 584 | //******************************************************************************
|
|---|
| 585 | //******************************************************************************
|
|---|
| 586 | void Win32DllBase::deleteAll()
|
|---|
| 587 | {
|
|---|
| 588 | Win32DllBase *dll = Win32DllBase::head;
|
|---|
| 589 |
|
|---|
| 590 | dprintf(("Win32DllBase::deleteAll"));
|
|---|
| 591 |
|
|---|
| 592 | #ifdef DEBUG_ENABLELOG_LEVEL2
|
|---|
| 593 | if(dll) dll->printListOfDlls();
|
|---|
| 594 | #endif
|
|---|
| 595 |
|
|---|
| 596 | dlllistmutex.enter();
|
|---|
| 597 | while(dll) {
|
|---|
| 598 | dll->Release();
|
|---|
| 599 | dll = Win32DllBase::head;
|
|---|
| 600 | }
|
|---|
| 601 | dlllistmutex.leave();
|
|---|
| 602 | dprintf(("Win32DllBase::deleteAll Done!"));
|
|---|
| 603 | }
|
|---|
| 604 | //******************************************************************************
|
|---|
| 605 | //Delete dlls loaded by LoadLibrary(Ex) in LIFO order
|
|---|
| 606 | //******************************************************************************
|
|---|
| 607 | void Win32DllBase::deleteDynamicLibs()
|
|---|
| 608 | {
|
|---|
| 609 | Win32DllBase *dll = head;
|
|---|
| 610 | QueueItem *item;
|
|---|
| 611 |
|
|---|
| 612 | dprintf(("Win32DllBase::deleteDynamicLibs"));
|
|---|
| 613 | #ifdef DEBUG_ENABLELOG_LEVEL2
|
|---|
| 614 | if(dll) dll->printListOfDlls();
|
|---|
| 615 | #endif
|
|---|
| 616 |
|
|---|
| 617 | dlllistmutex.enter();
|
|---|
| 618 |
|
|---|
| 619 | item = loadLibDlls.Head();
|
|---|
| 620 | while(item) {
|
|---|
| 621 | dll = (Win32DllBase *)loadLibDlls.getItem(item);
|
|---|
| 622 | int dynref = dll->nrDynamicLibRef;
|
|---|
| 623 | if(dynref) {
|
|---|
| 624 | while(dynref) {
|
|---|
| 625 | dynref--;
|
|---|
| 626 | dll->decDynamicLib();
|
|---|
| 627 | dll->Release();
|
|---|
| 628 | }
|
|---|
| 629 | }
|
|---|
| 630 | else DebugInt3();
|
|---|
| 631 | item = loadLibDlls.Head(); //queue could have been changed, so start from the beginning
|
|---|
| 632 | }
|
|---|
| 633 |
|
|---|
| 634 | dlllistmutex.leave();
|
|---|
| 635 | dprintf(("Win32DllBase::deleteDynamicLibs end"));
|
|---|
| 636 | }
|
|---|
| 637 | //******************************************************************************
|
|---|
| 638 | //******************************************************************************
|
|---|
| 639 | Win32DllBase *Win32DllBase::getFirst()
|
|---|
| 640 | {
|
|---|
| 641 | return head;
|
|---|
| 642 | }
|
|---|
| 643 | //******************************************************************************
|
|---|
| 644 | //Add renaming profile strings for ole32 & netapi32 to odin.ini if they aren't
|
|---|
| 645 | //already there
|
|---|
| 646 | //******************************************************************************
|
|---|
| 647 | void Win32DllBase::setDefaultRenaming()
|
|---|
| 648 | {
|
|---|
| 649 | char renameddll[CCHMAXPATH];
|
|---|
| 650 |
|
|---|
| 651 | if(ODIN_PROFILE_GetOdinIniString(DLLRENAMEWIN_SECTION, "OLE32", "", renameddll,
|
|---|
| 652 | sizeof(renameddll)-1) <= 1)
|
|---|
| 653 | {
|
|---|
| 654 | ODIN_PROFILE_SetOdinIniString(DLLRENAMEWIN_SECTION, "OLE32", "OLE32OS2");
|
|---|
| 655 | ODIN_PROFILE_SetOdinIniString(DLLRENAMEOS2_SECTION, "OLE32OS2", "OLE32");
|
|---|
| 656 | }
|
|---|
| 657 | if(ODIN_PROFILE_GetOdinIniString(DLLRENAMEWIN_SECTION, "OLEAUT32", "", renameddll,
|
|---|
| 658 | sizeof(renameddll)-1) <= 1)
|
|---|
| 659 | {
|
|---|
| 660 | ODIN_PROFILE_SetOdinIniString(DLLRENAMEWIN_SECTION, "OLEAUT32", "OLAUTOS2");
|
|---|
| 661 | ODIN_PROFILE_SetOdinIniString(DLLRENAMEOS2_SECTION, "OLAUTOS2", "OLEAUT32");
|
|---|
| 662 | }
|
|---|
| 663 | if(ODIN_PROFILE_GetOdinIniString(DLLRENAMEWIN_SECTION, "NETAPI32", "", renameddll,
|
|---|
| 664 | sizeof(renameddll)-1) <= 1)
|
|---|
| 665 | {
|
|---|
| 666 | ODIN_PROFILE_SetOdinIniString(DLLRENAMEWIN_SECTION, "NETAPI32", "WNETAP32");
|
|---|
| 667 | ODIN_PROFILE_SetOdinIniString(DLLRENAMEOS2_SECTION, "WNETAP32", "NETAPI32");
|
|---|
| 668 | }
|
|---|
| 669 | }
|
|---|
| 670 | //******************************************************************************
|
|---|
| 671 | //rename dll if necessary:
|
|---|
| 672 | // Win32 to OS/2 : (i.e. OLE32 -> OLE32OS2)
|
|---|
| 673 | // or
|
|---|
| 674 | // OS/2 to Win32 : (i.e. OLE32OS2 -> OLE32)
|
|---|
| 675 | //******************************************************************************
|
|---|
| 676 | void Win32DllBase::renameDll(char *dllname, BOOL fWinToOS2)
|
|---|
| 677 | {
|
|---|
| 678 | char modname[CCHMAXPATH];
|
|---|
| 679 | char renameddll[CCHMAXPATH];
|
|---|
| 680 | char *namestart;
|
|---|
| 681 | char *sectionname;
|
|---|
| 682 |
|
|---|
| 683 | if(fWinToOS2) {
|
|---|
| 684 | sectionname = DLLRENAMEWIN_SECTION;
|
|---|
| 685 | }
|
|---|
| 686 | else {
|
|---|
| 687 | sectionname = DLLRENAMEOS2_SECTION;
|
|---|
| 688 | }
|
|---|
| 689 | namestart = OSLibStripPath(dllname);
|
|---|
| 690 | strcpy(modname, namestart);
|
|---|
| 691 | char *dot = strrchr(modname, '.');
|
|---|
| 692 | if(dot)
|
|---|
| 693 | *dot = 0;
|
|---|
| 694 | strupr(modname);
|
|---|
| 695 | if(ODIN_PROFILE_GetOdinIniString(sectionname, modname, "", renameddll,
|
|---|
| 696 | sizeof(renameddll)-1) > 1)
|
|---|
| 697 | {
|
|---|
| 698 | if(namestart == dllname) {
|
|---|
| 699 | strcpy(dllname, renameddll);
|
|---|
| 700 | }
|
|---|
| 701 | else {
|
|---|
| 702 | *namestart = 0;
|
|---|
| 703 | strcat(dllname, renameddll);
|
|---|
| 704 | }
|
|---|
| 705 | strcat(dllname, ".dll");
|
|---|
| 706 | }
|
|---|
| 707 | return;
|
|---|
| 708 | }
|
|---|
| 709 | //******************************************************************************
|
|---|
| 710 | //******************************************************************************
|
|---|
| 711 | Win32DllBase *Win32DllBase::findModule(char *dllname, BOOL fRenameFirst)
|
|---|
| 712 | {
|
|---|
| 713 | Win32DllBase *dll;
|
|---|
| 714 | char szDllName[CCHMAXPATH];
|
|---|
| 715 | char *dot, *temp;
|
|---|
| 716 |
|
|---|
| 717 | //// dprintf2(("findModule %s", dllname));
|
|---|
| 718 |
|
|---|
| 719 | strcpy(szDllName, OSLibStripPath(dllname));
|
|---|
| 720 | strupr(szDllName);
|
|---|
| 721 |
|
|---|
| 722 | if(fRenameFirst) {
|
|---|
| 723 | renameDll(szDllName, FALSE);
|
|---|
| 724 | }
|
|---|
| 725 |
|
|---|
| 726 | dot = strstr(szDllName, ".");
|
|---|
| 727 | if(dot)
|
|---|
| 728 | *dot = 0;
|
|---|
| 729 |
|
|---|
| 730 | dlllistmutex.enter();
|
|---|
| 731 | dll = head;
|
|---|
| 732 | while(dll) {
|
|---|
| 733 | if(strcmpi(szDllName, dll->szModule) == 0) {
|
|---|
| 734 | dlllistmutex.leave();
|
|---|
| 735 | return(dll);
|
|---|
| 736 | }
|
|---|
| 737 |
|
|---|
| 738 | dll = dll->next;
|
|---|
| 739 | }
|
|---|
| 740 | dlllistmutex.leave();
|
|---|
| 741 | return(NULL);
|
|---|
| 742 | }
|
|---|
| 743 | //******************************************************************************
|
|---|
| 744 | //******************************************************************************
|
|---|
| 745 | Win32DllBase *Win32DllBase::findModule(WIN32DLLENTRY DllEntryPoint)
|
|---|
| 746 | {
|
|---|
| 747 | dprintf2(("findModule %X", DllEntryPoint));
|
|---|
| 748 |
|
|---|
| 749 | dlllistmutex.enter();
|
|---|
| 750 | Win32DllBase *mod = Win32DllBase::head;
|
|---|
| 751 | while(mod != NULL) {
|
|---|
| 752 | dbgCheckObj(mod);
|
|---|
| 753 | if(mod->dllEntryPoint == DllEntryPoint) {
|
|---|
| 754 | dlllistmutex.leave();
|
|---|
| 755 | return(mod);
|
|---|
| 756 | }
|
|---|
| 757 | mod = mod->next;
|
|---|
| 758 | }
|
|---|
| 759 | dlllistmutex.leave();
|
|---|
| 760 | return(NULL);
|
|---|
| 761 | }
|
|---|
| 762 | //******************************************************************************
|
|---|
| 763 | //******************************************************************************
|
|---|
| 764 | Win32DllBase *Win32DllBase::findModule(HINSTANCE hinstance)
|
|---|
| 765 | {
|
|---|
| 766 | dlllistmutex.enter();
|
|---|
| 767 |
|
|---|
| 768 | Win32DllBase *mod = Win32DllBase::head;
|
|---|
| 769 | while(mod != NULL) {
|
|---|
| 770 | dbgCheckObj(mod);
|
|---|
| 771 | if(mod->hinstance == hinstance) {
|
|---|
| 772 | dlllistmutex.leave();
|
|---|
| 773 | return(mod);
|
|---|
| 774 | }
|
|---|
| 775 | mod = mod->next;
|
|---|
| 776 | }
|
|---|
| 777 | dlllistmutex.leave();
|
|---|
| 778 | return(NULL);
|
|---|
| 779 | }
|
|---|
| 780 | //******************************************************************************
|
|---|
| 781 | //******************************************************************************
|
|---|
| 782 | Win32DllBase *Win32DllBase::findModuleByAddr(ULONG address)
|
|---|
| 783 | {
|
|---|
| 784 | dlllistmutex.enter();
|
|---|
| 785 |
|
|---|
| 786 | Win32DllBase *mod = Win32DllBase::head;
|
|---|
| 787 | while(mod != NULL) {
|
|---|
| 788 | dbgCheckObj(mod);
|
|---|
| 789 | if(mod->insideModule(address)) {
|
|---|
| 790 | dlllistmutex.leave();
|
|---|
| 791 | return(mod);
|
|---|
| 792 | }
|
|---|
| 793 | mod = mod->next;
|
|---|
| 794 | }
|
|---|
| 795 | dlllistmutex.leave();
|
|---|
| 796 | return(NULL);
|
|---|
| 797 | }
|
|---|
| 798 | //******************************************************************************
|
|---|
| 799 | //******************************************************************************
|
|---|
| 800 | BOOL Win32DllBase::isDll()
|
|---|
| 801 | {
|
|---|
| 802 | return TRUE;
|
|---|
| 803 | }
|
|---|
| 804 | //******************************************************************************
|
|---|
| 805 | //******************************************************************************
|
|---|
| 806 | Win32DllBase *Win32DllBase::head = NULL;
|
|---|
| 807 | Queue Win32DllBase::loadLibDlls;
|
|---|