source: trunk/src/peldr/pe.cpp@ 6823

Last change on this file since 6823 was 6325, checked in by sandervl, 24 years ago

Fix for parsing cmd line consisting of directory with spaces

File size: 13.4 KB
Line 
1/* $Id: pe.cpp,v 1.31 2001-07-14 07:44: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
36char INFO_BANNER[] = "Usage: PE winexe commandline";
37char szErrorTitle[] = "Odin";
38char szLoadErrorMsg[] = "Can't load executable";
39char szFileNotFound[] = "File not found.";
40char szFileErrorMsg[] = "File IO error";
41char szPEErrorMsg[] = "Not a valid win32 exe. (perhaps 16 bits windows)";
42char szCPUErrorMsg[] = "Executable doesn't run on x86 machines";
43char szExeErrorMsg[] = "File isn't an executable";
44char szInteralErrorMsg[]= "Internal Error";
45char szNoKernel32Msg[] = "Can't load/find kernel32.dll (rc=%d, module %s)";
46char szDosInfoBlocks[] = "DosInfoBlocks failed!";
47char szErrDosStartSession[] = "Failed to start win16 session (rc=%d)";
48
49char fullpath[CCHMAXPATH];
50
51typedef HAB (* APIENTRY WININITIALIZEPROC)(ULONG flOptions);
52typedef BOOL (* APIENTRY WINTERMINATEPROC)(HAB hab);
53typedef HMQ (* APIENTRY WINCREATEMSGQUEUEPROC) (HAB hab, LONG cmsg);
54typedef BOOL (* APIENTRY WINDESTROYMSGQUEUEPROC) (HMQ hmq);
55typedef ULONG (* APIENTRY WINMESSAGEBOXPROC) (HWND hwndParent,
56 HWND hwndOwner,
57 PCSZ pszText,
58 PCSZ pszCaption,
59 ULONG idWindow,
60 ULONG flStyle);
61typedef void (* KRNL32EXCEPTPROC) (void *exceptframe);
62
63WININITIALIZEPROC MyWinInitialize = 0;
64WINTERMINATEPROC MyWinTerminate = 0;
65WINCREATEMSGQUEUEPROC MyWinCreateMsgQueue = 0;
66WINDESTROYMSGQUEUEPROC MyWinDestroyMsgQueue = 0;
67WINMESSAGEBOXPROC MyWinMessageBox = 0;
68KRNL32EXCEPTPROC Krnl32SetExceptionHandler = 0;
69KRNL32EXCEPTPROC Krnl32UnsetExceptionHandler = 0;
70
71//should be the same as in ..\kernel32\winexepeldr.h
72typedef BOOL (* WIN32API WIN32CTOR)(char *, char *, char *, ULONG, BOOL, BOOL);
73
74WIN32CTOR CreateWin32Exe = 0;
75ULONG reservedMemory = 0;
76BOOL fConsoleApp = FALSE;
77
78BOOL AllocateExeMem(char *filename, BOOL *fNEExe);
79
80//******************************************************************************
81//******************************************************************************
82int 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, fEndOfCmdLine = FALSE;
96 int nrTries = 1;
97
98 if(argc >= 2) {
99 if(DosGetInfoBlocks(&ptib, &ppib) == 0) {
100tryagain:
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 > 1 && *p == 0) {
135 fEndOfCmdLine = TRUE;
136 }
137 }
138 *p = 0;
139 strupr(exeName);
140 cmdline = strstr(exeName, ".EXE");
141 if(cmdline) {
142 cmdline[4] = 0;
143 win32cmdline += ((ULONG)cmdline - (ULONG)exeName) + 4;
144 }
145 else {
146 win32cmdline += strlen(exeName);
147 if(strstr(exeName, ".") == NULL) {
148 strcat(exeName, ".EXE");
149 }
150 }
151 if(fQuote) win32cmdline++;
152 while(*win32cmdline == ' ') win32cmdline++; //skip spaces
153
154 cmdline = exeName + strlen(exeName) - 1;
155 while(*cmdline == ' ') cmdline--;
156 cmdline[1] = 0;
157 if(DosQueryPathInfo(exeName, FIL_QUERYFULLNAME, (PVOID)fullpath, sizeof(fullpath)) == 0) {
158 strcpy(exeName, fullpath);
159 }
160 FILESTATUS3 fstat3;
161 if(DosQueryPathInfo(exeName, FIL_STANDARD, (PVOID)&fstat3, sizeof(fstat3)))
162 {
163 nrTries++;
164 if(fEndOfCmdLine) {
165 pszErrorMsg = szFileNotFound;
166 goto failerror;
167 }
168
169 if(*win32cmdline != NULL && !fQuote) {
170 goto tryagain;
171 }
172 }
173 }
174 else {//should never happen!
175 pszErrorMsg = szDosInfoBlocks;
176 goto failerror;
177 }
178 AllocateExeMem(exeName, &fIsNEExe);
179 if(fIsNEExe) {
180 STARTDATA sdata = {0};
181 ULONG idSession;
182 PID pid;
183
184 sdata.Length = sizeof(sdata);
185 sdata.PgmName = "w16odin.exe";
186 strcpy(fullpath, exeName);
187 strcat(fullpath, " ");
188 strcat(fullpath, win32cmdline);
189 sdata.PgmInputs = fullpath;
190 sdata.FgBg = SSF_FGBG_FORE;
191 sdata.SessionType = SSF_TYPE_WINDOWEDVDM;
192 rc = DosStartSession(&sdata, &idSession, &pid);
193 if(rc) {
194 sprintf(fullpath, szErrDosStartSession, rc);
195 pszErrorMsg = fullpath;
196 goto failerror;
197 }
198 return 0;
199 }
200 }
201
202#ifdef COMMAND_LINE_VERSION
203 if(DosGetInfoBlocks(&ptib, &ppib) == 0) {
204 //switch process type to PM so the command line app can create PM
205 //windows
206 ppib->pib_ultype = 3;
207 }
208#endif
209
210 rc = DosLoadModule(exeName, sizeof(exeName), "PMWIN.DLL", &hmodPMWin);
211 rc = DosQueryProcAddr(hmodPMWin, ORD_WIN32INITIALIZE, NULL, (PFN *)&MyWinInitialize);
212 rc = DosQueryProcAddr(hmodPMWin, ORD_WIN32TERMINATE, NULL, (PFN *)&MyWinTerminate);
213 rc = DosQueryProcAddr(hmodPMWin, ORD_WIN32CREATEMSGQUEUE, NULL, (PFN *)&MyWinCreateMsgQueue);
214 rc = DosQueryProcAddr(hmodPMWin, ORD_WIN32DESTROYMSGQUEUE, NULL, (PFN *)&MyWinDestroyMsgQueue);
215 rc = DosQueryProcAddr(hmodPMWin, ORD_WIN32MESSAGEBOX, NULL, (PFN *)&MyWinMessageBox);
216
217 if ((hab = MyWinInitialize(0)) == 0L) /* Initialize PM */
218 goto fail;
219
220 hmq = MyWinCreateMsgQueue(hab, 0);
221
222 if(argc < 2) {
223 MyWinMessageBox(HWND_DESKTOP, NULL, INFO_BANNER, szErrorTitle, 0, MB_OK | MB_ERROR | MB_MOVEABLE);
224 goto fail;
225 }
226
227 errorMod[0] = 0;
228 rc = DosLoadModule(errorMod, sizeof(errorMod), "KERNEL32.DLL", &hmodKernel32);
229 if(rc) {
230 sprintf(fullpath, szNoKernel32Msg, rc, errorMod);
231 MyWinMessageBox(HWND_DESKTOP, NULL, fullpath, szErrorTitle, 0, MB_OK | MB_ERROR | MB_MOVEABLE);
232 goto fail;
233 }
234 rc = DosQueryProcAddr(hmodKernel32, 0, "_CreateWin32PeLdrExe@24", (PFN *)&CreateWin32Exe);
235
236#ifdef COMMAND_LINE_VERSION
237 fVioConsole = TRUE;
238#else
239 fVioConsole = FALSE;
240#endif
241 if(CreateWin32Exe(exeName, win32cmdline, peoptions, reservedMemory, fConsoleApp, fVioConsole) == FALSE) {
242 goto fail;
243 }
244
245 if(hmq) MyWinDestroyMsgQueue( hmq ); /* Tidy up... */
246 MyWinTerminate( hab ); /* Terminate the application */
247
248 DosFreeModule(hmodPMWin);
249 DosFreeModule(hmodKernel32);
250 return 0;
251
252fail:
253 if(hmq) MyWinDestroyMsgQueue( hmq ); /* Tidy up... */
254 if(hab) MyWinTerminate( hab ); /* Terminate the application */
255
256 if(hmodPMWin) DosFreeModule(hmodPMWin);
257 if(hmodKernel32) DosFreeModule(hmodKernel32);
258 return(1);
259
260failerror:
261 rc = DosLoadModule(exeName, sizeof(exeName), "PMWIN.DLL", &hmodPMWin);
262 rc = DosQueryProcAddr(hmodPMWin, ORD_WIN32INITIALIZE, NULL, (PFN *)&MyWinInitialize);
263 rc = DosQueryProcAddr(hmodPMWin, ORD_WIN32TERMINATE, NULL, (PFN *)&MyWinTerminate);
264 rc = DosQueryProcAddr(hmodPMWin, ORD_WIN32CREATEMSGQUEUE, NULL, (PFN *)&MyWinCreateMsgQueue);
265 rc = DosQueryProcAddr(hmodPMWin, ORD_WIN32DESTROYMSGQUEUE, NULL, (PFN *)&MyWinDestroyMsgQueue);
266 rc = DosQueryProcAddr(hmodPMWin, ORD_WIN32MESSAGEBOX, NULL, (PFN *)&MyWinMessageBox);
267 if(rc == 0) {
268 if ((hab = MyWinInitialize(0)) == 0L) /* Initialize PM */
269 goto fail;
270
271 hmq = MyWinCreateMsgQueue(hab, 0);
272
273 MyWinMessageBox(HWND_DESKTOP, NULL, pszErrorMsg, szErrorTitle, 0, MB_OK | MB_ERROR | MB_MOVEABLE);
274 }
275 if(hmq) MyWinDestroyMsgQueue( hmq ); /* Tidy up... */
276 if(hab) MyWinTerminate( hab ); /* Terminate the application */
277 if(hmodPMWin) DosFreeModule(hmodPMWin);
278 return 1;
279}
280//******************************************************************************
281//SvL: Reserve memory for win32 exes without fixups
282// This is done before any Odin or PMWIN dll is loaded, so we'll get
283// a very low virtual address. (which is exactly what we want)
284//******************************************************************************
285BOOL AllocateExeMem(char *filename, BOOL *fNEExe)
286{
287 HFILE dllfile = 0;
288 char szFileName[CCHMAXPATH], *tmp;
289 char szResult[CCHMAXPATH];
290 ULONG action, ulRead, signature;
291 APIRET rc;
292 IMAGE_DOS_HEADER doshdr;
293 IMAGE_OPTIONAL_HEADER oh;
294 IMAGE_FILE_HEADER fh;
295 ULONG address = 0;
296 ULONG *memallocs;
297 ULONG alloccnt = 0;
298 ULONG diff, i, baseAddress;
299 ULONG ulSysinfo, flAllocMem = 0;
300 BOOL ret = FALSE;
301
302 *fNEExe = FALSE;
303 strcpy(szFileName, filename);
304
305 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);
306 if(rc != 0) {
307 if(!strstr(szFileName, ".EXE")) {
308 strcat(szFileName,".EXE");
309 }
310 }
311 else DosClose(dllfile);
312
313 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);
314 if(rc) {
315 if(DosSearchPath(SEARCH_IGNORENETERRS|SEARCH_ENVIRONMENT, "PATH",
316 szFileName, szResult, sizeof(szResult)) != 0) {
317 goto end; //oops
318 }
319 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);
320 if(rc) {
321 goto end; //oops
322 }
323 }
324
325 //read dos header
326 if(DosRead(dllfile, (LPVOID)&doshdr, sizeof(doshdr), &ulRead)) {
327 goto end;
328 }
329 if(DosSetFilePtr(dllfile, doshdr.e_lfanew, FILE_BEGIN, &ulRead)) {
330 goto end;
331 }
332 //read signature dword
333 if(DosRead(dllfile, (LPVOID)&signature, sizeof(signature), &ulRead)) {
334 goto end;
335 }
336 //read pe header
337 if(DosRead(dllfile, (LPVOID)&fh, sizeof(fh), &ulRead)) {
338 goto end;
339 }
340 //read optional header
341 if(DosRead(dllfile, (LPVOID)&oh, sizeof(oh), &ulRead)) {
342 goto end;
343 }
344 if(doshdr.e_magic != IMAGE_DOS_SIGNATURE || signature != IMAGE_NT_SIGNATURE) {
345 if(LOWORD(signature) == IMAGE_OS2_SIGNATURE) {
346 *fNEExe = TRUE;
347 }
348 goto end;
349 }
350 fConsoleApp = (oh.Subsystem == IMAGE_SUBSYSTEM_WINDOWS_CUI);
351
352 // check for high memory support
353 rc = DosQuerySysInfo(QSV_VIRTUALADDRESSLIMIT, QSV_VIRTUALADDRESSLIMIT, &ulSysinfo, sizeof(ulSysinfo));
354 if (rc == 0 && ulSysinfo > 512) //VirtualAddresslimit is in MB
355 {
356 flAllocMem = PAG_ANY; // high memory support. Let's use it!
357 }
358
359 //Reserve enough space to store 4096 pointers to 1MB memory chunks
360 memallocs = (ULONG *)alloca(4096*sizeof(ULONG *));
361 if(memallocs == NULL) {
362 goto end; //oops
363 }
364
365 if(oh.ImageBase < 512*1024*1024) {
366 flAllocMem = 0;
367 }
368 else {
369 if(flAllocMem == 0) {
370 goto end; //no support for > 512 MB
371 }
372 }
373 while(TRUE) {
374 rc = DosAllocMem((PPVOID)&address, FALLOC_SIZE, PAG_READ | flAllocMem);
375 if(rc) break;
376
377 if(address + FALLOC_SIZE >= oh.ImageBase) {
378 if(address > oh.ImageBase) {//we've passed it!
379 DosFreeMem((PVOID)address);
380 break;
381 }
382 //found the right address
383 DosFreeMem((PVOID)address);
384
385 diff = oh.ImageBase - address;
386 if(diff) {
387 rc = DosAllocMem((PPVOID)&address, diff, PAG_READ | flAllocMem);
388 if(rc) break;
389 }
390 rc = DosAllocMem((PPVOID)&baseAddress, oh.SizeOfImage, PAG_READ | PAG_WRITE | flAllocMem);
391 if(rc) break;
392
393 if(diff) DosFreeMem((PVOID)address);
394
395 reservedMemory = baseAddress;
396 break;
397 }
398 memallocs[alloccnt++] = address;
399 }
400 for(i=0;i<alloccnt;i++) {
401 DosFreeMem((PVOID)memallocs[i]);
402 }
403 ret = TRUE;
404end:
405 if(dllfile) DosClose(dllfile);
406 return ret;
407}
408//******************************************************************************
409//******************************************************************************
Note: See TracBrowser for help on using the repository browser.