1 | /* $Id: windllpeldr.cpp,v 1.10 2002-07-18 11:52:56 achimha Exp $ */
|
---|
2 |
|
---|
3 | /*
|
---|
4 | * Win32 PE loader Dll class
|
---|
5 | *
|
---|
6 | * Copyright 1999 Sander van Leeuwen (sandervl@xs4all.nl)
|
---|
7 | *
|
---|
8 | *
|
---|
9 | * Project Odin Software License can be found in LICENSE.TXT
|
---|
10 | *
|
---|
11 | */
|
---|
12 | #define INCL_DOSFILEMGR /* File Manager values */
|
---|
13 | #define INCL_DOSERRORS /* DOS Error values */
|
---|
14 | #define INCL_DOSPROCESS /* DOS Process values */
|
---|
15 | #define INCL_DOSMODULEMGR
|
---|
16 | #define INCL_DOSMISC /* DOS Miscellanous values */
|
---|
17 | #define INCL_WIN
|
---|
18 | #include <os2wrap.h> //Odin32 OS/2 api wrappers
|
---|
19 | #include <stdio.h>
|
---|
20 | #include <string.h>
|
---|
21 | #include <stdlib.h>
|
---|
22 | #include <iostream.h>
|
---|
23 | #include <fstream.h>
|
---|
24 | #include <misc.h>
|
---|
25 | #include <win32type.h>
|
---|
26 | #include <pefile.h>
|
---|
27 | #include <windllpeldr.h>
|
---|
28 | #include <wprocess.h>
|
---|
29 |
|
---|
30 | #include "oslibmisc.h"
|
---|
31 | #include "oslibdos.h"
|
---|
32 |
|
---|
33 | #define DBG_LOCALLOG DBG_windllpeldr
|
---|
34 | #include "dbglocal.h"
|
---|
35 |
|
---|
36 | //******************************************************************************
|
---|
37 | // Design information on PE DLL memory layout - AH 2002-07-18
|
---|
38 | //
|
---|
39 | // We are currently using (high) private memory for all PE objects, including the
|
---|
40 | // read/execute code segments, constant data segments and global data segments.
|
---|
41 | // Global data segments might not be implemented correctly at all as we've never
|
---|
42 | // encountered any applictions making use of them. Therefore we are actually
|
---|
43 | // wasting memory when running multiple processes using the same PE DLLs.
|
---|
44 | //
|
---|
45 | // There are several reasons for this design decisions. Both OS/2 (LX) and
|
---|
46 | // Windows NT put all DLL segments into the shared arena. What they do for
|
---|
47 | // instance data is map it for each process to read-only pages initially. When
|
---|
48 | // a write attempt is made by a process, an exception will be triggered. This
|
---|
49 | // makes the operating system to copy the data to a new page that is read/write
|
---|
50 | // and change the page table of the process to map the linear process in the
|
---|
51 | // shared arena to private memory (this is called "copy-on-write").
|
---|
52 | // Even though an application is not guaranteed any virtual address for instance
|
---|
53 | // data segments, they always end up in the shared region and the virtual addreses
|
---|
54 | // are contiguous. An application could therefore make nasty assumptions.
|
---|
55 | // Unfortunately, it is not possible for us from ring 3 to replicate the behavior
|
---|
56 | // for our PE loader. While we can make the page read only and catch the
|
---|
57 | // exception, we have no method to remap the pages to private memory.
|
---|
58 | //
|
---|
59 | // One solution would be to create another reagion with the private region,
|
---|
60 | // i.e. define some address space range as reserved in Odin (configurable to
|
---|
61 | // workaround issues with certain PE images requiring those addresses). We
|
---|
62 | // could then load the instance data segments of PE DLLs into this private
|
---|
63 | // memory arena and still guarantee identical virtual addresses for each
|
---|
64 | // process.
|
---|
65 | //
|
---|
66 | // While the above method should work fine (assuming an application does not
|
---|
67 | // make any nasty assumptions), there is one major problem. If we enable the
|
---|
68 | // PE on-demand loader (i.e. the mmap loads each page from the PE file when
|
---|
69 | // it is accesses for the first time - very much like NT), then we would have
|
---|
70 | // nasty concurrency issues. A process could access a page for the first time
|
---|
71 | // and the exception is triggered. We commit the page read the data in using
|
---|
72 | // a call to DosRead. If the very same page is accessed from a different
|
---|
73 | // process after we have committed it but before we have finished the DosRead,
|
---|
74 | // we would run into problems. Unfortunately, there does not seem to be any
|
---|
75 | // solution for this.
|
---|
76 | //
|
---|
77 | // The bottomline is that we put everything into private memory and accept the
|
---|
78 | // drawback of wasting memory.
|
---|
79 | //******************************************************************************
|
---|
80 |
|
---|
81 |
|
---|
82 | //******************************************************************************
|
---|
83 | //******************************************************************************
|
---|
84 | Win32PeLdrDll::Win32PeLdrDll(char *szDllName, Win32ImageBase *parentImage)
|
---|
85 | : Win32ImageBase(-1),
|
---|
86 | Win32DllBase(-1, 0, parentImage),
|
---|
87 | Win32PeLdrImage(szDllName, FALSE)
|
---|
88 | {
|
---|
89 | dprintf(("Win32PeLdrDll::Win32PeLdrDll %s %s loaded by %s", szFileName, szModule,
|
---|
90 | (parentImage) ? parentImage->getModuleName() : "Unknown"));
|
---|
91 | }
|
---|
92 | //******************************************************************************
|
---|
93 | //******************************************************************************
|
---|
94 | Win32PeLdrDll::~Win32PeLdrDll()
|
---|
95 | {
|
---|
96 | dprintf(("Win32PeLdrDll::~Win32PeLdrDll %s", szModule));
|
---|
97 | }
|
---|
98 | //******************************************************************************
|
---|
99 | //******************************************************************************
|
---|
100 | BOOL Win32PeLdrDll::init(ULONG reservedMem)
|
---|
101 | {
|
---|
102 | char modname[CCHMAXPATH];
|
---|
103 | char *syspath;
|
---|
104 | HFILE dllfile;
|
---|
105 | APIRET rc;
|
---|
106 | BOOL fRet;
|
---|
107 |
|
---|
108 | strupr(szFileName);
|
---|
109 | if(!strchr(szFileName, (int)'.')) {
|
---|
110 | strcat(szFileName, DLL_EXTENSION);
|
---|
111 | }
|
---|
112 | dllfile = OSLibDosOpen(szFileName, OSLIB_ACCESS_READONLY|OSLIB_ACCESS_SHAREDENYNONE);
|
---|
113 | if(dllfile == NULL) {//search in libpath for dll
|
---|
114 | syspath = getenv("WIN32LIBPATH");
|
---|
115 | if(syspath) {
|
---|
116 | strcpy(modname, syspath);
|
---|
117 | if(modname[strlen(modname)-1] != '\\') {
|
---|
118 | strcat(modname, "\\");
|
---|
119 | }
|
---|
120 | strcat(modname, szFileName);
|
---|
121 | strcpy(szFileName, modname);
|
---|
122 | }
|
---|
123 | }
|
---|
124 | else OSLibDosClose(dllfile);
|
---|
125 | fRet = Win32PeLdrImage::init(0);
|
---|
126 | dllEntryPoint = (WIN32DLLENTRY)entryPoint;
|
---|
127 |
|
---|
128 | if(!(fh.Characteristics & IMAGE_FILE_DLL)) {
|
---|
129 | //executable loaded as dll; don't call entrypoint
|
---|
130 | dprintf(("WARNING: Exe %s loaded as dll; entrypoint not called", szFileName));
|
---|
131 | dllEntryPoint = NULL;
|
---|
132 | }
|
---|
133 | return fRet;
|
---|
134 | }
|
---|
135 | //******************************************************************************
|
---|
136 | //******************************************************************************
|
---|
137 | BOOL Win32PeLdrDll::isPe2LxDll() const
|
---|
138 | {
|
---|
139 | return FALSE;
|
---|
140 | }
|
---|
141 | //******************************************************************************
|
---|
142 | //******************************************************************************
|
---|
143 | BOOL Win32PeLdrDll::isLxDll() const
|
---|
144 | {
|
---|
145 | return FALSE;
|
---|
146 | }
|
---|
147 | //******************************************************************************
|
---|
148 | //******************************************************************************
|
---|