1 | /* $Id: pe.cpp,v 1.29 2001-07-06 13:47:40 sandervl Exp $ */
|
---|
2 |
|
---|
3 | /*
|
---|
4 | * PELDR main exe loader code
|
---|
5 | *
|
---|
6 | * Copyright 1998-2001 Sander van Leeuwen (sandervl@xs4all.nl)
|
---|
7 | *
|
---|
8 | * Command line options:
|
---|
9 | * /OPT:[x1=y,x2=z,..]
|
---|
10 | * x = CURDIR -> set current directory to y
|
---|
11 | * (not other options available at this time)
|
---|
12 | *
|
---|
13 | * Project Odin Software License can be found in LICENSE.TXT
|
---|
14 | *
|
---|
15 | */
|
---|
16 | #define INCL_DOSFILEMGR /* File Manager values */
|
---|
17 | #define INCL_DOSERRORS /* DOS Error values */
|
---|
18 | #define INCL_DOSPROCESS /* DOS Process values */
|
---|
19 | #define INCL_DOSMISC /* DOS Miscellanous values */
|
---|
20 | #define INCL_DOSMODULEMGR
|
---|
21 | #define INCL_DOSSESMGR
|
---|
22 | #define INCL_WIN
|
---|
23 | #include <os2.h>
|
---|
24 | #include <bseord.h>
|
---|
25 | #include <stdio.h>
|
---|
26 | #include <string.h>
|
---|
27 | #include <stdlib.h>
|
---|
28 | #include <string.h>
|
---|
29 | #include <assert.h>
|
---|
30 | #include <win32type.h>
|
---|
31 | #include <misc.h>
|
---|
32 | #include <wprocess.h>
|
---|
33 | #include <win\peexe.h>
|
---|
34 | #include "pe.h"
|
---|
35 |
|
---|
36 | char INFO_BANNER[] = "Usage: PE winexe commandline";
|
---|
37 | char szErrorTitle[] = "Odin";
|
---|
38 | char szLoadErrorMsg[] = "Can't load executable";
|
---|
39 | char szFileNotFound[] = "File not found.";
|
---|
40 | char szFileErrorMsg[] = "File IO error";
|
---|
41 | char szPEErrorMsg[] = "Not a valid win32 exe. (perhaps 16 bits windows)";
|
---|
42 | char szCPUErrorMsg[] = "Executable doesn't run on x86 machines";
|
---|
43 | char szExeErrorMsg[] = "File isn't an executable";
|
---|
44 | char szInteralErrorMsg[]= "Internal Error";
|
---|
45 | char szNoKernel32Msg[] = "Can't load/find kernel32.dll (rc=%d, module %s)";
|
---|
46 | char szDosInfoBlocks[] = "DosInfoBlocks failed!";
|
---|
47 | char szErrDosStartSession[] = "Failed to start win16 session (rc=%d)";
|
---|
48 |
|
---|
49 | char fullpath[CCHMAXPATH];
|
---|
50 |
|
---|
51 | typedef HAB (* APIENTRY WININITIALIZEPROC)(ULONG flOptions);
|
---|
52 | typedef BOOL (* APIENTRY WINTERMINATEPROC)(HAB hab);
|
---|
53 | typedef HMQ (* APIENTRY WINCREATEMSGQUEUEPROC) (HAB hab, LONG cmsg);
|
---|
54 | typedef BOOL (* APIENTRY WINDESTROYMSGQUEUEPROC) (HMQ hmq);
|
---|
55 | typedef ULONG (* APIENTRY WINMESSAGEBOXPROC) (HWND hwndParent,
|
---|
56 | HWND hwndOwner,
|
---|
57 | PCSZ pszText,
|
---|
58 | PCSZ pszCaption,
|
---|
59 | ULONG idWindow,
|
---|
60 | ULONG flStyle);
|
---|
61 | typedef void (* KRNL32EXCEPTPROC) (void *exceptframe);
|
---|
62 |
|
---|
63 | WININITIALIZEPROC MyWinInitialize = 0;
|
---|
64 | WINTERMINATEPROC MyWinTerminate = 0;
|
---|
65 | WINCREATEMSGQUEUEPROC MyWinCreateMsgQueue = 0;
|
---|
66 | WINDESTROYMSGQUEUEPROC MyWinDestroyMsgQueue = 0;
|
---|
67 | WINMESSAGEBOXPROC MyWinMessageBox = 0;
|
---|
68 | KRNL32EXCEPTPROC Krnl32SetExceptionHandler = 0;
|
---|
69 | KRNL32EXCEPTPROC Krnl32UnsetExceptionHandler = 0;
|
---|
70 |
|
---|
71 | //should be the same as in ..\kernel32\winexepeldr.h
|
---|
72 | typedef BOOL (* WIN32API WIN32CTOR)(char *, char *, char *, ULONG, BOOL, BOOL);
|
---|
73 |
|
---|
74 | WIN32CTOR CreateWin32Exe = 0;
|
---|
75 | ULONG reservedMemory = 0;
|
---|
76 | BOOL fConsoleApp = FALSE;
|
---|
77 |
|
---|
78 | BOOL AllocateExeMem(char *filename, BOOL *fNEExe);
|
---|
79 |
|
---|
80 | //******************************************************************************
|
---|
81 | //******************************************************************************
|
---|
82 | int main(int argc, char *argv[])
|
---|
83 | {
|
---|
84 | HAB hab = 0; /* PM anchor block handle */
|
---|
85 | HMQ hmq = 0; /* Message queue handle */
|
---|
86 | char exeName[CCHMAXPATH];
|
---|
87 | char fullpath[CCHMAXPATH];
|
---|
88 | char errorMod[CCHMAXPATH];
|
---|
89 | char *pszErrorMsg = NULL;
|
---|
90 | APIRET rc;
|
---|
91 | HMODULE hmodPMWin = 0, hmodKernel32 = 0;
|
---|
92 | PTIB ptib;
|
---|
93 | PPIB ppib;
|
---|
94 | char *cmdline, *win32cmdline, *peoptions, *newcmdline;
|
---|
95 | BOOL fQuote = FALSE, fVioConsole, fIsNEExe;
|
---|
96 | int nrTries = 1;
|
---|
97 |
|
---|
98 | if(argc >= 2) {
|
---|
99 | if(DosGetInfoBlocks(&ptib, &ppib) == 0) {
|
---|
100 | tryagain:
|
---|
101 | cmdline = ppib->pib_pchcmd;
|
---|
102 | cmdline += strlen(cmdline)+1; //skip pe.exe
|
---|
103 | peoptions = strstr(cmdline, "/OPT:[");
|
---|
104 | if(peoptions) {
|
---|
105 | newcmdline = strchr(peoptions, ']');
|
---|
106 | if(newcmdline) {
|
---|
107 | cmdline = newcmdline+1;
|
---|
108 | }
|
---|
109 | #ifdef DEBUG
|
---|
110 | else _interrupt(3); //should not happen!
|
---|
111 | #endif
|
---|
112 | }
|
---|
113 | while(*cmdline == ' ') cmdline++; //skip leading space
|
---|
114 | if(*cmdline == '"') {
|
---|
115 | cmdline++;
|
---|
116 | fQuote = TRUE;
|
---|
117 | }
|
---|
118 | win32cmdline = cmdline;
|
---|
119 |
|
---|
120 | strncpy(exeName, cmdline, sizeof(exeName)-1);
|
---|
121 | exeName[sizeof(exeName)-1] = 0;
|
---|
122 | char *p = exeName;
|
---|
123 | if(fQuote) {
|
---|
124 | while(*p != '"' && *p != 0) p++;
|
---|
125 | }
|
---|
126 | else {
|
---|
127 | for(int i=0;i<nrTries;i++) {
|
---|
128 | while(*p != ' ' && *p != 0) p++;
|
---|
129 | if(*p == 0) break;
|
---|
130 | if(i != nrTries-1) {
|
---|
131 | while(*p == ' ' && *p != 0) p++;
|
---|
132 | }
|
---|
133 | }
|
---|
134 | if(nrTries && *p == 0) {
|
---|
135 | pszErrorMsg = szFileNotFound;
|
---|
136 | goto failerror;
|
---|
137 | }
|
---|
138 | }
|
---|
139 | *p = 0;
|
---|
140 | strupr(exeName);
|
---|
141 | cmdline = strstr(exeName, ".EXE");
|
---|
142 | if(cmdline) {
|
---|
143 | cmdline[4] = 0;
|
---|
144 | win32cmdline += ((ULONG)cmdline - (ULONG)exeName) + 4;
|
---|
145 | }
|
---|
146 | else {
|
---|
147 | win32cmdline += strlen(exeName);
|
---|
148 | if(strstr(exeName, ".") == NULL) {
|
---|
149 | strcat(exeName, ".EXE");
|
---|
150 | }
|
---|
151 | }
|
---|
152 | if(fQuote) win32cmdline++;
|
---|
153 | while(*win32cmdline == ' ') win32cmdline++; //skip spaces
|
---|
154 |
|
---|
155 | cmdline = exeName + strlen(exeName) - 1;
|
---|
156 | while(*cmdline == ' ') cmdline--;
|
---|
157 | cmdline[1] = 0;
|
---|
158 | if(DosQueryPathInfo(exeName, FIL_QUERYFULLNAME, (PVOID)fullpath, sizeof(fullpath)) == 0) {
|
---|
159 | strcpy(exeName, fullpath);
|
---|
160 | }
|
---|
161 | FILESTATUS3 fstat3;
|
---|
162 | if(DosQueryPathInfo(exeName, FIL_STANDARD, (PVOID)&fstat3, sizeof(fstat3)))
|
---|
163 | {
|
---|
164 | nrTries++;
|
---|
165 | if(*win32cmdline != NULL && !fQuote) {
|
---|
166 | goto tryagain;
|
---|
167 | }
|
---|
168 | }
|
---|
169 | }
|
---|
170 | else {//should never happen!
|
---|
171 | pszErrorMsg = szDosInfoBlocks;
|
---|
172 | goto failerror;
|
---|
173 | }
|
---|
174 | AllocateExeMem(exeName, &fIsNEExe);
|
---|
175 | if(fIsNEExe) {
|
---|
176 | STARTDATA sdata = {0};
|
---|
177 | ULONG idSession;
|
---|
178 | PID pid;
|
---|
179 |
|
---|
180 | sdata.Length = sizeof(sdata);
|
---|
181 | sdata.PgmName = "w16odin.exe";
|
---|
182 | strcpy(fullpath, exeName);
|
---|
183 | strcat(fullpath, " ");
|
---|
184 | strcat(fullpath, win32cmdline);
|
---|
185 | sdata.PgmInputs = fullpath;
|
---|
186 | sdata.FgBg = SSF_FGBG_FORE;
|
---|
187 | sdata.SessionType = SSF_TYPE_WINDOWEDVDM;
|
---|
188 | rc = DosStartSession(&sdata, &idSession, &pid);
|
---|
189 | if(rc) {
|
---|
190 | sprintf(fullpath, szErrDosStartSession, rc);
|
---|
191 | pszErrorMsg = fullpath;
|
---|
192 | goto failerror;
|
---|
193 | }
|
---|
194 | return 0;
|
---|
195 | }
|
---|
196 | }
|
---|
197 |
|
---|
198 | #ifdef COMMAND_LINE_VERSION
|
---|
199 | if(DosGetInfoBlocks(&ptib, &ppib) == 0) {
|
---|
200 | //switch process type to PM so the command line app can create PM
|
---|
201 | //windows
|
---|
202 | ppib->pib_ultype = 3;
|
---|
203 | }
|
---|
204 | #endif
|
---|
205 |
|
---|
206 | rc = DosLoadModule(exeName, sizeof(exeName), "PMWIN.DLL", &hmodPMWin);
|
---|
207 | rc = DosQueryProcAddr(hmodPMWin, ORD_WIN32INITIALIZE, NULL, (PFN *)&MyWinInitialize);
|
---|
208 | rc = DosQueryProcAddr(hmodPMWin, ORD_WIN32TERMINATE, NULL, (PFN *)&MyWinTerminate);
|
---|
209 | rc = DosQueryProcAddr(hmodPMWin, ORD_WIN32CREATEMSGQUEUE, NULL, (PFN *)&MyWinCreateMsgQueue);
|
---|
210 | rc = DosQueryProcAddr(hmodPMWin, ORD_WIN32DESTROYMSGQUEUE, NULL, (PFN *)&MyWinDestroyMsgQueue);
|
---|
211 | rc = DosQueryProcAddr(hmodPMWin, ORD_WIN32MESSAGEBOX, NULL, (PFN *)&MyWinMessageBox);
|
---|
212 |
|
---|
213 | if ((hab = MyWinInitialize(0)) == 0L) /* Initialize PM */
|
---|
214 | goto fail;
|
---|
215 |
|
---|
216 | hmq = MyWinCreateMsgQueue(hab, 0);
|
---|
217 |
|
---|
218 | if(argc < 2) {
|
---|
219 | MyWinMessageBox(HWND_DESKTOP, NULL, INFO_BANNER, szErrorTitle, 0, MB_OK | MB_ERROR | MB_MOVEABLE);
|
---|
220 | goto fail;
|
---|
221 | }
|
---|
222 |
|
---|
223 | errorMod[0] = 0;
|
---|
224 | rc = DosLoadModule(errorMod, sizeof(errorMod), "KERNEL32.DLL", &hmodKernel32);
|
---|
225 | if(rc) {
|
---|
226 | sprintf(fullpath, szNoKernel32Msg, rc, errorMod);
|
---|
227 | MyWinMessageBox(HWND_DESKTOP, NULL, fullpath, szErrorTitle, 0, MB_OK | MB_ERROR | MB_MOVEABLE);
|
---|
228 | goto fail;
|
---|
229 | }
|
---|
230 | rc = DosQueryProcAddr(hmodKernel32, 0, "_CreateWin32PeLdrExe@24", (PFN *)&CreateWin32Exe);
|
---|
231 |
|
---|
232 | #ifdef COMMAND_LINE_VERSION
|
---|
233 | fVioConsole = TRUE;
|
---|
234 | #else
|
---|
235 | fVioConsole = FALSE;
|
---|
236 | #endif
|
---|
237 | if(CreateWin32Exe(exeName, win32cmdline, peoptions, reservedMemory, fConsoleApp, fVioConsole) == FALSE) {
|
---|
238 | goto fail;
|
---|
239 | }
|
---|
240 |
|
---|
241 | if(hmq) MyWinDestroyMsgQueue( hmq ); /* Tidy up... */
|
---|
242 | MyWinTerminate( hab ); /* Terminate the application */
|
---|
243 |
|
---|
244 | DosFreeModule(hmodPMWin);
|
---|
245 | DosFreeModule(hmodKernel32);
|
---|
246 | return 0;
|
---|
247 |
|
---|
248 | fail:
|
---|
249 | if(hmq) MyWinDestroyMsgQueue( hmq ); /* Tidy up... */
|
---|
250 | if(hab) MyWinTerminate( hab ); /* Terminate the application */
|
---|
251 |
|
---|
252 | if(hmodPMWin) DosFreeModule(hmodPMWin);
|
---|
253 | if(hmodKernel32) DosFreeModule(hmodKernel32);
|
---|
254 | return(1);
|
---|
255 |
|
---|
256 | failerror:
|
---|
257 | rc = DosLoadModule(exeName, sizeof(exeName), "PMWIN.DLL", &hmodPMWin);
|
---|
258 | rc = DosQueryProcAddr(hmodPMWin, ORD_WIN32INITIALIZE, NULL, (PFN *)&MyWinInitialize);
|
---|
259 | rc = DosQueryProcAddr(hmodPMWin, ORD_WIN32TERMINATE, NULL, (PFN *)&MyWinTerminate);
|
---|
260 | rc = DosQueryProcAddr(hmodPMWin, ORD_WIN32CREATEMSGQUEUE, NULL, (PFN *)&MyWinCreateMsgQueue);
|
---|
261 | rc = DosQueryProcAddr(hmodPMWin, ORD_WIN32DESTROYMSGQUEUE, NULL, (PFN *)&MyWinDestroyMsgQueue);
|
---|
262 | rc = DosQueryProcAddr(hmodPMWin, ORD_WIN32MESSAGEBOX, NULL, (PFN *)&MyWinMessageBox);
|
---|
263 | if(rc == 0) {
|
---|
264 | if ((hab = MyWinInitialize(0)) == 0L) /* Initialize PM */
|
---|
265 | goto fail;
|
---|
266 |
|
---|
267 | hmq = MyWinCreateMsgQueue(hab, 0);
|
---|
268 |
|
---|
269 | MyWinMessageBox(HWND_DESKTOP, NULL, pszErrorMsg, szErrorTitle, 0, MB_OK | MB_ERROR | MB_MOVEABLE);
|
---|
270 | }
|
---|
271 | if(hmq) MyWinDestroyMsgQueue( hmq ); /* Tidy up... */
|
---|
272 | if(hab) MyWinTerminate( hab ); /* Terminate the application */
|
---|
273 | if(hmodPMWin) DosFreeModule(hmodPMWin);
|
---|
274 | return 1;
|
---|
275 | }
|
---|
276 | //******************************************************************************
|
---|
277 | //SvL: Reserve memory for win32 exes without fixups
|
---|
278 | // This is done before any Odin or PMWIN dll is loaded, so we'll get
|
---|
279 | // a very low virtual address. (which is exactly what we want)
|
---|
280 | //******************************************************************************
|
---|
281 | BOOL AllocateExeMem(char *filename, BOOL *fNEExe)
|
---|
282 | {
|
---|
283 | HFILE dllfile = 0;
|
---|
284 | char szFileName[CCHMAXPATH], *tmp;
|
---|
285 | char szResult[CCHMAXPATH];
|
---|
286 | ULONG action, ulRead, signature;
|
---|
287 | APIRET rc;
|
---|
288 | IMAGE_DOS_HEADER doshdr;
|
---|
289 | IMAGE_OPTIONAL_HEADER oh;
|
---|
290 | IMAGE_FILE_HEADER fh;
|
---|
291 | ULONG address = 0;
|
---|
292 | ULONG *memallocs;
|
---|
293 | ULONG alloccnt = 0;
|
---|
294 | ULONG diff, i, baseAddress;
|
---|
295 | ULONG ulSysinfo, flAllocMem = 0;
|
---|
296 | BOOL ret = FALSE;
|
---|
297 |
|
---|
298 | *fNEExe = FALSE;
|
---|
299 | strcpy(szFileName, filename);
|
---|
300 |
|
---|
301 | rc = DosOpen(szFileName, &dllfile, &action, 0, FILE_READONLY, OPEN_ACTION_OPEN_IF_EXISTS|OPEN_ACTION_FAIL_IF_NEW, OPEN_SHARE_DENYNONE|OPEN_ACCESS_READONLY, NULL);
|
---|
302 | if(rc != 0) {
|
---|
303 | if(!strstr(szFileName, ".EXE")) {
|
---|
304 | strcat(szFileName,".EXE");
|
---|
305 | }
|
---|
306 | }
|
---|
307 | else DosClose(dllfile);
|
---|
308 |
|
---|
309 | rc = DosOpen(szFileName, &dllfile, &action, 0, FILE_READONLY, OPEN_ACTION_OPEN_IF_EXISTS|OPEN_ACTION_FAIL_IF_NEW, OPEN_SHARE_DENYNONE|OPEN_ACCESS_READONLY, NULL);
|
---|
310 | if(rc) {
|
---|
311 | if(DosSearchPath(SEARCH_IGNORENETERRS|SEARCH_ENVIRONMENT, "PATH",
|
---|
312 | szFileName, szResult, sizeof(szResult)) != 0) {
|
---|
313 | goto end; //oops
|
---|
314 | }
|
---|
315 | rc = DosOpen(szResult, &dllfile, &action, 0, FILE_READONLY, OPEN_ACTION_OPEN_IF_EXISTS|OPEN_ACTION_FAIL_IF_NEW, OPEN_SHARE_DENYNONE|OPEN_ACCESS_READONLY, NULL);
|
---|
316 | if(rc) {
|
---|
317 | goto end; //oops
|
---|
318 | }
|
---|
319 | }
|
---|
320 |
|
---|
321 | //read dos header
|
---|
322 | if(DosRead(dllfile, (LPVOID)&doshdr, sizeof(doshdr), &ulRead)) {
|
---|
323 | goto end;
|
---|
324 | }
|
---|
325 | if(DosSetFilePtr(dllfile, doshdr.e_lfanew, FILE_BEGIN, &ulRead)) {
|
---|
326 | goto end;
|
---|
327 | }
|
---|
328 | //read signature dword
|
---|
329 | if(DosRead(dllfile, (LPVOID)&signature, sizeof(signature), &ulRead)) {
|
---|
330 | goto end;
|
---|
331 | }
|
---|
332 | //read pe header
|
---|
333 | if(DosRead(dllfile, (LPVOID)&fh, sizeof(fh), &ulRead)) {
|
---|
334 | goto end;
|
---|
335 | }
|
---|
336 | //read optional header
|
---|
337 | if(DosRead(dllfile, (LPVOID)&oh, sizeof(oh), &ulRead)) {
|
---|
338 | goto end;
|
---|
339 | }
|
---|
340 | if(doshdr.e_magic != IMAGE_DOS_SIGNATURE || signature != IMAGE_NT_SIGNATURE) {
|
---|
341 | if(LOWORD(signature) == IMAGE_OS2_SIGNATURE) {
|
---|
342 | *fNEExe = TRUE;
|
---|
343 | }
|
---|
344 | goto end;
|
---|
345 | }
|
---|
346 | fConsoleApp = (oh.Subsystem == IMAGE_SUBSYSTEM_WINDOWS_CUI);
|
---|
347 |
|
---|
348 | // check for high memory support
|
---|
349 | rc = DosQuerySysInfo(QSV_VIRTUALADDRESSLIMIT, QSV_VIRTUALADDRESSLIMIT, &ulSysinfo, sizeof(ulSysinfo));
|
---|
350 | if (rc == 0 && ulSysinfo > 512) //VirtualAddresslimit is in MB
|
---|
351 | {
|
---|
352 | flAllocMem = PAG_ANY; // high memory support. Let's use it!
|
---|
353 | }
|
---|
354 |
|
---|
355 | //Reserve enough space to store 4096 pointers to 1MB memory chunks
|
---|
356 | memallocs = (ULONG *)alloca(4096*sizeof(ULONG *));
|
---|
357 | if(memallocs == NULL) {
|
---|
358 | goto end; //oops
|
---|
359 | }
|
---|
360 |
|
---|
361 | if(oh.ImageBase < 512*1024*1024) {
|
---|
362 | flAllocMem = 0;
|
---|
363 | }
|
---|
364 | else {
|
---|
365 | if(flAllocMem == 0) {
|
---|
366 | goto end; //no support for > 512 MB
|
---|
367 | }
|
---|
368 | }
|
---|
369 | while(TRUE) {
|
---|
370 | rc = DosAllocMem((PPVOID)&address, FALLOC_SIZE, PAG_READ | flAllocMem);
|
---|
371 | if(rc) break;
|
---|
372 |
|
---|
373 | if(address + FALLOC_SIZE >= oh.ImageBase) {
|
---|
374 | if(address > oh.ImageBase) {//we've passed it!
|
---|
375 | DosFreeMem((PVOID)address);
|
---|
376 | break;
|
---|
377 | }
|
---|
378 | //found the right address
|
---|
379 | DosFreeMem((PVOID)address);
|
---|
380 |
|
---|
381 | diff = oh.ImageBase - address;
|
---|
382 | if(diff) {
|
---|
383 | rc = DosAllocMem((PPVOID)&address, diff, PAG_READ | flAllocMem);
|
---|
384 | if(rc) break;
|
---|
385 | }
|
---|
386 | rc = DosAllocMem((PPVOID)&baseAddress, oh.SizeOfImage, PAG_READ | PAG_WRITE | flAllocMem);
|
---|
387 | if(rc) break;
|
---|
388 |
|
---|
389 | if(diff) DosFreeMem((PVOID)address);
|
---|
390 |
|
---|
391 | reservedMemory = baseAddress;
|
---|
392 | break;
|
---|
393 | }
|
---|
394 | memallocs[alloccnt++] = address;
|
---|
395 | }
|
---|
396 | for(i=0;i<alloccnt;i++) {
|
---|
397 | DosFreeMem((PVOID)memallocs[i]);
|
---|
398 | }
|
---|
399 | ret = TRUE;
|
---|
400 | end:
|
---|
401 | if(dllfile) DosClose(dllfile);
|
---|
402 | return ret;
|
---|
403 | }
|
---|
404 | //******************************************************************************
|
---|
405 | //******************************************************************************
|
---|