1 | /* $Id: winimagebase.cpp,v 1.25 2000-08-12 16:58:40 sandervl Exp $ */
|
---|
2 |
|
---|
3 | /*
|
---|
4 | * Win32 PE Image base class
|
---|
5 | *
|
---|
6 | * Copyright 1998-1999 Sander van Leeuwen (sandervl@xs4all.nl)
|
---|
7 | * Copyright 1998 Knut St. Osmundsen
|
---|
8 | *
|
---|
9 | * Project Odin Software License can be found in LICENSE.TXT
|
---|
10 | *
|
---|
11 | */
|
---|
12 |
|
---|
13 | #define INCL_DOSFILEMGR /* File Manager values */
|
---|
14 | #define INCL_DOSMODULEMGR
|
---|
15 | #define INCL_DOSERRORS /* DOS Error values */
|
---|
16 | #define INCL_DOSPROCESS /* DOS Process values */
|
---|
17 | #define INCL_DOSMISC /* DOS Miscellanous values */
|
---|
18 | #define INCL_WIN
|
---|
19 | #define INCL_BASE
|
---|
20 | #include <os2wrap.h> //Odin32 OS/2 api wrappers
|
---|
21 |
|
---|
22 | #include <stdio.h>
|
---|
23 | #include <string.h>
|
---|
24 | #include <stdlib.h>
|
---|
25 |
|
---|
26 | #include <assert.h>
|
---|
27 | #include <misc.h>
|
---|
28 | #include <win32type.h>
|
---|
29 | #include <winimagebase.h>
|
---|
30 | #include <windllbase.h>
|
---|
31 | #include <winexebase.h>
|
---|
32 | #include <pefile.h>
|
---|
33 | #include <unicode.h>
|
---|
34 | #include "oslibmisc.h"
|
---|
35 | #include "oslibdos.h"
|
---|
36 | #include "initterm.h"
|
---|
37 | #include "directory.h"
|
---|
38 | #include <win\virtual.h>
|
---|
39 | #include <winconst.h>
|
---|
40 |
|
---|
41 | #define DBG_LOCALLOG DBG_winimagebase
|
---|
42 | #include "dbglocal.h"
|
---|
43 |
|
---|
44 | //******************************************************************************
|
---|
45 | //******************************************************************************
|
---|
46 | Win32ImageBase::Win32ImageBase(HINSTANCE hInstance) :
|
---|
47 | errorState(NO_ERROR), entryPoint(0), fullpath(NULL),
|
---|
48 | tlsAddress(0), tlsIndexAddr(0), tlsInitSize(0), tlsTotalSize(0),
|
---|
49 | tlsCallBackAddr(0), tlsIndex(-1), pResRootDir(NULL),
|
---|
50 | ulRVAResourceSection(0)
|
---|
51 | {
|
---|
52 | magic = MAGIC_WINIMAGE;
|
---|
53 |
|
---|
54 | if(hInstance != -1) {
|
---|
55 | this->hinstance = hInstance;
|
---|
56 |
|
---|
57 | char *name = OSLibGetDllName(hinstance);
|
---|
58 | strcpy(szFileName, name);
|
---|
59 | strupr(szFileName);
|
---|
60 |
|
---|
61 | //rename dll (os/2 -> win32) if necessary (i.e. OLE32OS2 -> OLE32)
|
---|
62 | Win32DllBase::renameDll(szFileName, FALSE);
|
---|
63 |
|
---|
64 | name = strrchr(szFileName, '\\')+1;
|
---|
65 | strcpy(szModule, name);
|
---|
66 |
|
---|
67 | char *dot = strrchr(szModule, '.');
|
---|
68 | if(dot)
|
---|
69 | *dot = 0;
|
---|
70 | }
|
---|
71 | else {
|
---|
72 | szModule[0] = 0;
|
---|
73 | this->hinstance = -1;
|
---|
74 | }
|
---|
75 | }
|
---|
76 | //******************************************************************************
|
---|
77 | //******************************************************************************
|
---|
78 | Win32ImageBase::~Win32ImageBase()
|
---|
79 | {
|
---|
80 | if(fullpath)
|
---|
81 | free(fullpath);
|
---|
82 | }
|
---|
83 | //******************************************************************************
|
---|
84 | //******************************************************************************
|
---|
85 | void Win32ImageBase::setFullPath(char *name)
|
---|
86 | {
|
---|
87 | dassert(name, ("setFullPath, name == NULL"));
|
---|
88 | fullpath = (char *)malloc(strlen(name)+1);
|
---|
89 | dassert(fullpath, ("setFullPath, fullpath == NULL"));
|
---|
90 | strcpy(fullpath, name);
|
---|
91 | }
|
---|
92 | //******************************************************************************
|
---|
93 | //Add image to dependency list of this image
|
---|
94 | //******************************************************************************
|
---|
95 | void Win32ImageBase::addDependency(Win32DllBase *image)
|
---|
96 | {
|
---|
97 | loadedDlls.Push((ULONG)image);
|
---|
98 | }
|
---|
99 | //******************************************************************************
|
---|
100 | //******************************************************************************
|
---|
101 | BOOL Win32ImageBase::dependsOn(Win32DllBase *image)
|
---|
102 | {
|
---|
103 | QueueItem *item;
|
---|
104 | BOOL ret = FALSE;
|
---|
105 |
|
---|
106 | dlllistmutex.enter();
|
---|
107 | item = loadedDlls.Head();
|
---|
108 | while(item) {
|
---|
109 | if(loadedDlls.getItem(item) == (ULONG)image) {
|
---|
110 | ret = TRUE;
|
---|
111 | break;
|
---|
112 | }
|
---|
113 | item = loadedDlls.getNext(item);
|
---|
114 | }
|
---|
115 | dlllistmutex.leave();
|
---|
116 | return ret;
|
---|
117 | }
|
---|
118 | //******************************************************************************
|
---|
119 | //Returns required OS version for this image
|
---|
120 | //******************************************************************************
|
---|
121 | ULONG Win32ImageBase::getVersion()
|
---|
122 | {
|
---|
123 | dprintf(("Win32ImageBase::getVersion: NOT IMPLEMENTED!"));
|
---|
124 | return 0x40000; //NT 4
|
---|
125 | }
|
---|
126 | //******************************************************************************
|
---|
127 | //******************************************************************************
|
---|
128 | BOOL Win32ImageBase::insideModule(ULONG address)
|
---|
129 | {
|
---|
130 | //dummy
|
---|
131 | return FALSE;
|
---|
132 | }
|
---|
133 | //******************************************************************************
|
---|
134 | //******************************************************************************
|
---|
135 | BOOL Win32ImageBase::insideModuleCode(ULONG address)
|
---|
136 | {
|
---|
137 | //dummy
|
---|
138 | return FALSE;
|
---|
139 | }
|
---|
140 | //******************************************************************************
|
---|
141 | //******************************************************************************
|
---|
142 | ULONG Win32ImageBase::getImageSize()
|
---|
143 | {
|
---|
144 | //dummy
|
---|
145 | return 0;
|
---|
146 | }
|
---|
147 | //******************************************************************************
|
---|
148 | //******************************************************************************
|
---|
149 | BOOL Win32ImageBase::findDll(const char *szFileName, char *szFullName,
|
---|
150 | int cchFullFileName, const char *pszAltPath)
|
---|
151 | {
|
---|
152 | char modname[CCHMAXPATH];
|
---|
153 | HFILE dllfile = NULL;
|
---|
154 | char *imagepath;
|
---|
155 |
|
---|
156 | strcpy(szFullName, szFileName);
|
---|
157 | strupr(szFullName);
|
---|
158 | if(!strchr(szFullName, (int)'.')) {
|
---|
159 | strcat(szFullName,".DLL");
|
---|
160 | }
|
---|
161 |
|
---|
162 | //search order:
|
---|
163 | //1) exe dir
|
---|
164 | //2) current dir
|
---|
165 | //3) windows system dir (kernel32 path)
|
---|
166 | //4) windows dir
|
---|
167 | //5) path
|
---|
168 | if(WinExe) {
|
---|
169 | strcpy(modname, WinExe->getFullPath());
|
---|
170 | //remove file name from full path
|
---|
171 | imagepath = modname + strlen(modname) - 1;
|
---|
172 | while(*imagepath != '\\') imagepath--;
|
---|
173 | imagepath[1] = 0;
|
---|
174 | strcat(modname, szFullName);
|
---|
175 | dllfile = OSLibDosOpen(modname, OSLIB_ACCESS_READONLY|OSLIB_ACCESS_SHAREDENYNONE);
|
---|
176 | }
|
---|
177 | if(dllfile == NULL) {
|
---|
178 | strcpy(modname, szFullName);
|
---|
179 | dllfile = OSLibDosOpen(szFullName, OSLIB_ACCESS_READONLY|OSLIB_ACCESS_SHAREDENYNONE);
|
---|
180 | if(dllfile == NULL) {
|
---|
181 | strcpy(modname, InternalGetSystemDirectoryA());
|
---|
182 | strcat(modname, "\\");
|
---|
183 | strcat(modname, szFullName);
|
---|
184 | dllfile = OSLibDosOpen(modname, OSLIB_ACCESS_READONLY|OSLIB_ACCESS_SHAREDENYNONE);
|
---|
185 | if(dllfile == NULL) {
|
---|
186 | strcpy(modname, InternalGetWindowsDirectoryA());
|
---|
187 | strcat(modname, "\\");
|
---|
188 | strcat(modname, szFullName);
|
---|
189 | dllfile = OSLibDosOpen(modname, OSLIB_ACCESS_READONLY|OSLIB_ACCESS_SHAREDENYNONE);
|
---|
190 | if(dllfile == NULL) {
|
---|
191 | if(OSLibDosSearchPath(OSLIB_SEARCHENV, "PATH", szFullName, modname, sizeof(modname)) == 0) {
|
---|
192 | return FALSE;
|
---|
193 | }
|
---|
194 | }
|
---|
195 | }
|
---|
196 | }
|
---|
197 | }
|
---|
198 | strcpy(szFullName, modname);
|
---|
199 | if(dllfile) OSLibDosClose(dllfile);
|
---|
200 | return TRUE;
|
---|
201 | }
|
---|
202 | //******************************************************************************
|
---|
203 | //returns ERROR_SUCCESS or error code
|
---|
204 | //******************************************************************************
|
---|
205 | ULONG Win32ImageBase::isPEImage(char *szFileName)
|
---|
206 | {
|
---|
207 | char filename[CCHMAXPATH];
|
---|
208 | char *syspath;
|
---|
209 | HFILE dllfile;
|
---|
210 | IMAGE_FILE_HEADER fh;
|
---|
211 | HFILE win32handle;
|
---|
212 | ULONG ulAction = 0; /* Action taken by DosOpen */
|
---|
213 | ULONG ulLocal = 0; /* File pointer position after DosSetFilePtr */
|
---|
214 | APIRET rc = NO_ERROR; /* Return code */
|
---|
215 | LPVOID win32file = NULL;
|
---|
216 | ULONG ulRead;
|
---|
217 | int nSections, i;
|
---|
218 |
|
---|
219 | if (!findDll(szFileName, filename, sizeof(filename)))
|
---|
220 | {
|
---|
221 | dprintf(("KERNEL32:Win32ImageBase::isPEImage(%s) findDll failed to find the file.\n",
|
---|
222 | szFileName, rc));
|
---|
223 | return ERROR_FILE_NOT_FOUND_W;
|
---|
224 | }
|
---|
225 | rc = DosOpen(filename, /* File path name */
|
---|
226 | &win32handle, /* File handle */
|
---|
227 | &ulAction, /* Action taken */
|
---|
228 | 0L, /* File primary allocation */
|
---|
229 | 0L, /* File attribute */
|
---|
230 | OPEN_ACTION_FAIL_IF_NEW |
|
---|
231 | OPEN_ACTION_OPEN_IF_EXISTS, /* Open function type */
|
---|
232 | OPEN_FLAGS_NOINHERIT |
|
---|
233 | OPEN_SHARE_DENYNONE |
|
---|
234 | OPEN_ACCESS_READONLY, /* Open mode of the file */
|
---|
235 | 0L); /* No extended attribute */
|
---|
236 |
|
---|
237 | if (rc != NO_ERROR)
|
---|
238 | {
|
---|
239 | dprintf(("KERNEL32:Win32ImageBase::isPEImage(%s) failed with %u\n",
|
---|
240 | szFileName, rc));
|
---|
241 | return ERROR_FILE_NOT_FOUND_W;
|
---|
242 | }
|
---|
243 |
|
---|
244 | /* Move the file pointer back to the beginning of the file */
|
---|
245 | DosSetFilePtr(win32handle, 0L, FILE_BEGIN, &ulLocal);
|
---|
246 |
|
---|
247 | IMAGE_DOS_HEADER *pdoshdr = (IMAGE_DOS_HEADER *)malloc(sizeof(IMAGE_DOS_HEADER));
|
---|
248 | if(pdoshdr == NULL) {
|
---|
249 | DosClose(win32handle); /* Close the file */
|
---|
250 | return ERROR_INVALID_EXE_SIGNATURE_W;
|
---|
251 | }
|
---|
252 | rc = DosRead(win32handle, pdoshdr, sizeof(IMAGE_DOS_HEADER), &ulRead);
|
---|
253 | if(rc != NO_ERROR || ulRead != sizeof(IMAGE_DOS_HEADER)) {
|
---|
254 | free(pdoshdr);
|
---|
255 | DosClose(win32handle); /* Close the file */
|
---|
256 | return ERROR_INVALID_EXE_SIGNATURE_W;
|
---|
257 | }
|
---|
258 | if(pdoshdr->e_magic != IMAGE_DOS_SIGNATURE) {
|
---|
259 | free(pdoshdr);
|
---|
260 | DosClose(win32handle); /* Close the file */
|
---|
261 | return ERROR_INVALID_EXE_SIGNATURE_W;
|
---|
262 | }
|
---|
263 | ULONG hdrsize = pdoshdr->e_lfanew + SIZE_OF_NT_SIGNATURE + sizeof(IMAGE_FILE_HEADER);
|
---|
264 | free(pdoshdr);
|
---|
265 |
|
---|
266 | /* Move the file pointer back to the beginning of the file */
|
---|
267 | DosSetFilePtr(win32handle, 0L, FILE_BEGIN, &ulLocal);
|
---|
268 |
|
---|
269 | win32file = malloc(hdrsize);
|
---|
270 | if(win32file == NULL) {
|
---|
271 | DosClose(win32handle); /* Close the file */
|
---|
272 | return ERROR_NOT_ENOUGH_MEMORY_W;
|
---|
273 | }
|
---|
274 | rc = DosRead(win32handle, win32file, hdrsize, &ulRead);
|
---|
275 | if(rc != NO_ERROR || ulRead != hdrsize) {
|
---|
276 | goto failure;
|
---|
277 | }
|
---|
278 |
|
---|
279 | if(GetPEFileHeader (win32file, &fh) == FALSE) {
|
---|
280 | goto failure;
|
---|
281 | }
|
---|
282 |
|
---|
283 | if(!(fh.Characteristics & IMAGE_FILE_EXECUTABLE_IMAGE)) {//not valid
|
---|
284 | goto failure;
|
---|
285 | }
|
---|
286 | if(fh.Machine != IMAGE_FILE_MACHINE_I386) {
|
---|
287 | goto failure;
|
---|
288 | }
|
---|
289 | //IMAGE_FILE_SYSTEM == only drivers (device/file system/video etc)?
|
---|
290 | if(fh.Characteristics & IMAGE_FILE_SYSTEM) {
|
---|
291 | goto failure;
|
---|
292 | }
|
---|
293 | DosClose(win32handle);
|
---|
294 | return ERROR_SUCCESS_W;
|
---|
295 |
|
---|
296 | failure:
|
---|
297 | free(win32file);
|
---|
298 | DosClose(win32handle);
|
---|
299 | return ERROR_INVALID_EXE_SIGNATURE_W;
|
---|
300 | }
|
---|
301 | //******************************************************************************
|
---|
302 | //******************************************************************************
|
---|
303 | /**
|
---|
304 | * Static helper which finds the Win32ImageBase object corresponding to hModule.
|
---|
305 | * @returns Pointer to Win32ImageBase object corresponding to hModule.
|
---|
306 | * @param hModule Odin32 modulehandler. 0 and -1 is aliases for the executable module
|
---|
307 | * @status completely implemented and tested.
|
---|
308 | * @author knut st. osmundsen
|
---|
309 | */
|
---|
310 | Win32ImageBase * Win32ImageBase::findModule(HMODULE hModule)
|
---|
311 | {
|
---|
312 | Win32ImageBase *pRet;
|
---|
313 |
|
---|
314 | if (hModule == -1 || hModule == 0 || /* note: WinNt 4, SP4 don't accept -1 as the EXE handle.*/
|
---|
315 | (WinExe != NULL && hModule == WinExe->getInstanceHandle())
|
---|
316 | )
|
---|
317 | pRet = WinExe;
|
---|
318 | else
|
---|
319 | pRet = Win32DllBase::findModule(hModule);
|
---|
320 |
|
---|
321 | if (pRet == NULL)
|
---|
322 | {
|
---|
323 | if (WinExe == NULL)
|
---|
324 | dprintf(("Win32ImageBase::findModule: Module not found. WinExe is NULL, hModule=%#x\n", hModule));
|
---|
325 | else
|
---|
326 | dprintf(("Win32ImageBase::findModule: Module not found, hModule=%#x\n", hModule));
|
---|
327 | }
|
---|
328 |
|
---|
329 | return pRet;
|
---|
330 | }
|
---|
331 |
|
---|
332 |
|
---|