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

Last change on this file since 5120 was 5015, checked in by sandervl, 25 years ago

cmd line parse fix

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