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

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

cmd line parser bug fixed

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