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

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

changes for setting current dir of new process

File size: 11.1 KB
Line 
1/* $Id: pe.cpp,v 1.23 2000-10-06 15:15:25 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);
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;
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 if(*win32cmdline == '"') {
144 win32cmdline++;
145 cmdline = win32cmdline + strlen(win32cmdline) - 1;
146 while(*cmdline == ' ') cmdline--;
147 if(*cmdline == '"') {
148 *cmdline = 0;
149 }
150 }
151 cmdline = exeName + strlen(exeName) - 1;
152 while(*cmdline == ' ') cmdline--;
153 cmdline[1] = 0;
154 if(DosQueryPathInfo(exeName, FIL_QUERYFULLNAME, (PVOID)fullpath, sizeof(fullpath)) == 0) {
155 strcpy(exeName, fullpath);
156 }
157 FILESTATUS3 fstat3;
158 if(DosQueryPathInfo(exeName, FIL_STANDARD, (PVOID)&fstat3, sizeof(fstat3)))
159 {
160 nrTries++;
161 if(*win32cmdline != NULL && !fQuote) {
162 goto tryagain;
163 }
164 }
165 }
166 else {//should never happen!
167filenotfound:
168 DebugInt3();
169 rc = DosLoadModule(exeName, sizeof(exeName), "PMWIN.DLL", &hmodPMWin);
170 rc = DosQueryProcAddr(hmodPMWin, ORD_WIN32MESSAGEBOX, NULL, (PFN *)&MyWinMessageBox);
171 MyWinMessageBox(HWND_DESKTOP, NULL, szDosInfoBlocks, szErrorTitle, 0, MB_OK | MB_ERROR | MB_MOVEABLE);
172 goto fail;
173 }
174 AllocateExeMem(exeName);
175 }
176
177 rc = DosLoadModule(exeName, sizeof(exeName), "PMWIN.DLL", &hmodPMWin);
178 rc = DosQueryProcAddr(hmodPMWin, ORD_WIN32INITIALIZE, NULL, (PFN *)&MyWinInitialize);
179 rc = DosQueryProcAddr(hmodPMWin, ORD_WIN32TERMINATE, NULL, (PFN *)&MyWinTerminate);
180 rc = DosQueryProcAddr(hmodPMWin, ORD_WIN32CREATEMSGQUEUE, NULL, (PFN *)&MyWinCreateMsgQueue);
181 rc = DosQueryProcAddr(hmodPMWin, ORD_WIN32DESTROYMSGQUEUE, NULL, (PFN *)&MyWinDestroyMsgQueue);
182 rc = DosQueryProcAddr(hmodPMWin, ORD_WIN32MESSAGEBOX, NULL, (PFN *)&MyWinMessageBox);
183
184 if ((hab = MyWinInitialize(0)) == 0L) /* Initialize PM */
185 goto fail;
186
187 hmq = MyWinCreateMsgQueue(hab, 0);
188
189 if(argc < 2) {
190 MyWinMessageBox(HWND_DESKTOP, NULL, INFO_BANNER, szErrorTitle, 0, MB_OK | MB_ERROR | MB_MOVEABLE);
191 goto fail;
192 }
193
194 rc = DosLoadModule(exeName, sizeof(exeName), "KERNEL32.DLL", &hmodKernel32);
195 if(rc) {
196 sprintf(exeName, szNoKernel32Msg, rc);
197 MyWinMessageBox(HWND_DESKTOP, NULL, exeName, szErrorTitle, 0, MB_OK | MB_ERROR | MB_MOVEABLE);
198 goto fail;
199 }
200 rc = DosQueryProcAddr(hmodKernel32, 0, "_CreateWin32PeLdrExe@20", (PFN *)&CreateWin32Exe);
201
202 if(CreateWin32Exe(exeName, win32cmdline, peoptions, reservedMemory, fConsoleApp) == FALSE) {
203 goto fail;
204 }
205
206 if(hmq) MyWinDestroyMsgQueue( hmq ); /* Tidy up... */
207 MyWinTerminate( hab ); /* Terminate the application */
208
209 DosFreeModule(hmodPMWin);
210 DosFreeModule(hmodKernel32);
211 return 0;
212
213fail:
214 if(hmq) MyWinDestroyMsgQueue( hmq ); /* Tidy up... */
215 if(hab) MyWinTerminate( hab ); /* Terminate the application */
216
217 if(hmodPMWin) DosFreeModule(hmodPMWin);
218 if(hmodKernel32) DosFreeModule(hmodKernel32);
219 return(1);
220}
221//******************************************************************************
222//SvL: Reserve memory for win32 exes without fixups
223// This is done before any Odin or PMWIN dll is loaded, so we'll get
224// a very low virtual address. (which is exactly what we want)
225//******************************************************************************
226void AllocateExeMem(char *filename)
227{
228 HFILE dllfile = 0;
229 char szFileName[CCHMAXPATH], *tmp;
230 char szResult[CCHMAXPATH];
231 ULONG action, ulRead, signature;
232 APIRET rc;
233 IMAGE_DOS_HEADER doshdr;
234 IMAGE_OPTIONAL_HEADER oh;
235 IMAGE_FILE_HEADER fh;
236 ULONG address = 0;
237 ULONG *memallocs;
238 ULONG alloccnt = 0;
239 ULONG diff, i, baseAddress;
240 ULONG ulSysinfo, flAllocMem = 0;
241
242 strcpy(szFileName, filename);
243
244 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);
245 if(rc != 0) {
246 if(!strstr(szFileName, ".EXE")) {
247 strcat(szFileName,".EXE");
248 }
249 }
250 else DosClose(dllfile);
251
252 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);
253 if(rc) {
254 if(DosSearchPath(SEARCH_IGNORENETERRS|SEARCH_ENVIRONMENT, "PATH",
255 szFileName, szResult, sizeof(szResult)) != 0) {
256 goto end; //oops
257 }
258 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);
259 if(rc) {
260 goto end; //oops
261 }
262 }
263
264 //read dos header
265 if(DosRead(dllfile, (LPVOID)&doshdr, sizeof(doshdr), &ulRead)) {
266 goto end;
267 }
268 if(DosSetFilePtr(dllfile, doshdr.e_lfanew, FILE_BEGIN, &ulRead)) {
269 goto end;
270 }
271 //read signature dword
272 if(DosRead(dllfile, (LPVOID)&signature, sizeof(signature), &ulRead)) {
273 goto end;
274 }
275 //read pe header
276 if(DosRead(dllfile, (LPVOID)&fh, sizeof(fh), &ulRead)) {
277 goto end;
278 }
279 //read optional header
280 if(DosRead(dllfile, (LPVOID)&oh, sizeof(oh), &ulRead)) {
281 goto end;
282 }
283 if(doshdr.e_magic != IMAGE_DOS_SIGNATURE || signature != IMAGE_NT_SIGNATURE) {
284 goto end;
285 }
286 fConsoleApp = (oh.Subsystem == IMAGE_SUBSYSTEM_WINDOWS_CUI);
287
288 // check for high memory support
289 rc = DosQuerySysInfo(QSV_VIRTUALADDRESSLIMIT, QSV_VIRTUALADDRESSLIMIT, &ulSysinfo, sizeof(ulSysinfo));
290 if (rc == 0 && ulSysinfo > 512) //VirtualAddresslimit is in MB
291 {
292 flAllocMem = PAG_ANY; // high memory support. Let's use it!
293 }
294
295 //Reserve enough space to store 4096 pointers to 1MB memory chunks
296 memallocs = (ULONG *)alloca(4096*sizeof(ULONG *));
297 if(memallocs == NULL) {
298 goto end; //oops
299 }
300
301 if(oh.ImageBase < 512*1024*1024) {
302 flAllocMem = 0;
303 }
304 else {
305 if(flAllocMem == 0) {
306 goto end; //no support for > 512 MB
307 }
308 }
309 while(TRUE) {
310 rc = DosAllocMem((PPVOID)&address, FALLOC_SIZE, PAG_READ | flAllocMem);
311 if(rc) break;
312
313 if(address + FALLOC_SIZE >= oh.ImageBase) {
314 if(address > oh.ImageBase) {//we've passed it!
315 DosFreeMem((PVOID)address);
316 break;
317 }
318 //found the right address
319 DosFreeMem((PVOID)address);
320
321 diff = oh.ImageBase - address;
322 if(diff) {
323 rc = DosAllocMem((PPVOID)&address, diff, PAG_READ | flAllocMem);
324 if(rc) break;
325 }
326 rc = DosAllocMem((PPVOID)&baseAddress, oh.SizeOfImage, PAG_READ | PAG_WRITE | flAllocMem);
327 if(rc) break;
328
329 if(diff) DosFreeMem((PVOID)address);
330
331 reservedMemory = baseAddress;
332 break;
333 }
334 memallocs[alloccnt++] = address;
335 }
336 for(i=0;i<alloccnt;i++) {
337 DosFreeMem((PVOID)memallocs[i]);
338 }
339end:
340 if(dllfile) DosClose(dllfile);
341 return;
342}
343//******************************************************************************
344//******************************************************************************
Note: See TracBrowser for help on using the repository browser.