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

Last change on this file since 2572 was 2572, checked in by sandervl, 26 years ago

fixed error messages for images without fixups

File size: 8.3 KB
Line 
1/* $Id: pe.cpp,v 1.13 2000-01-30 14:48:51 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 <winexepeldr.h>
28#include <wprocess.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";
40
41char fullpath[CCHMAXPATH];
42
43typedef HAB (* APIENTRY WININITIALIZEPROC)(ULONG flOptions);
44typedef BOOL (* APIENTRY WINTERMINATEPROC)(HAB hab);
45typedef HMQ (* APIENTRY WINCREATEMSGQUEUEPROC) (HAB hab, LONG cmsg);
46typedef BOOL (* APIENTRY WINDESTROYMSGQUEUEPROC) (HMQ hmq);
47typedef ULONG (* APIENTRY WINMESSAGEBOXPROC) (HWND hwndParent,
48 HWND hwndOwner,
49 PCSZ pszText,
50 PCSZ pszCaption,
51 ULONG idWindow,
52 ULONG flStyle);
53typedef void (* KRNL32EXCEPTPROC) (void *exceptframe);
54
55WININITIALIZEPROC MyWinInitialize = 0;
56WINTERMINATEPROC MyWinTerminate = 0;
57WINCREATEMSGQUEUEPROC MyWinCreateMsgQueue = 0;
58WINDESTROYMSGQUEUEPROC MyWinDestroyMsgQueue = 0;
59WINMESSAGEBOXPROC MyWinMessageBox = 0;
60KRNL32EXCEPTPROC Krnl32SetExceptionHandler = 0;
61KRNL32EXCEPTPROC Krnl32UnsetExceptionHandler = 0;
62
63WIN32CTOR CreateWin32Exe = 0;
64
65ULONG reservedMemory = 0;
66
67void AllocateExeMem(char *filename);
68
69//******************************************************************************
70//******************************************************************************
71int main(int argc, char *argv[])
72{
73 HAB hab = 0; /* PM anchor block handle */
74 HMQ hmq = 0; /* Message queue handle */
75 char exeName[CCHMAXPATH];
76 APIRET rc;
77 HMODULE hmodPMWin = 0, hmodKernel32 = 0;
78
79 if(argc >= 2) {
80 strcpy(exeName, argv[1]);
81 strupr(exeName);
82 AllocateExeMem(exeName);
83 }
84
85 rc = DosLoadModule(exeName, sizeof(exeName), "PMWIN.DLL", &hmodPMWin);
86 rc = DosQueryProcAddr(hmodPMWin, ORD_WIN32INITIALIZE, NULL, (PFN *)&MyWinInitialize);
87 rc = DosQueryProcAddr(hmodPMWin, ORD_WIN32TERMINATE, NULL, (PFN *)&MyWinTerminate);
88 rc = DosQueryProcAddr(hmodPMWin, ORD_WIN32CREATEMSGQUEUE, NULL, (PFN *)&MyWinCreateMsgQueue);
89 rc = DosQueryProcAddr(hmodPMWin, ORD_WIN32DESTROYMSGQUEUE, NULL, (PFN *)&MyWinDestroyMsgQueue);
90 rc = DosQueryProcAddr(hmodPMWin, ORD_WIN32MESSAGEBOX, NULL, (PFN *)&MyWinMessageBox);
91
92 if ((hab = MyWinInitialize(0)) == 0L) /* Initialize PM */
93 goto fail;
94
95 hmq = MyWinCreateMsgQueue(hab, 0);
96
97 if(argc < 2) {
98 MyWinMessageBox(HWND_DESKTOP, NULL, INFO_BANNER, szErrorTitle, 0, MB_OK | MB_ERROR | MB_MOVEABLE);
99 goto fail;
100 }
101
102 rc = DosLoadModule(exeName, sizeof(exeName), "KERNEL32.DLL", &hmodKernel32);
103 if(rc) {
104 MyWinMessageBox(HWND_DESKTOP, NULL, szNoKernel32Msg, szErrorTitle, 0, MB_OK | MB_ERROR | MB_MOVEABLE);
105 goto fail;
106 }
107 rc = DosQueryProcAddr(hmodKernel32, 0, "_CreateWin32PeLdrExe@8", (PFN *)&CreateWin32Exe);
108
109 strcpy(exeName, argv[1]);
110 strupr(exeName);
111 if(CreateWin32Exe(exeName, reservedMemory) == FALSE) {
112 goto fail;
113 }
114
115 if(hmq) MyWinDestroyMsgQueue( hmq ); /* Tidy up... */
116 MyWinTerminate( hab ); /* Terminate the application */
117
118 DosFreeModule(hmodPMWin);
119 DosFreeModule(hmodKernel32);
120 return 0;
121
122fail:
123 if(hmq) MyWinDestroyMsgQueue( hmq ); /* Tidy up... */
124 if(hab) MyWinTerminate( hab ); /* Terminate the application */
125
126 if(hmodPMWin) DosFreeModule(hmodPMWin);
127 if(hmodKernel32) DosFreeModule(hmodKernel32);
128 return(1);
129}
130//******************************************************************************
131//SvL: Reserve memory for win32 exes without fixups
132// This is done before any Odin or PMWIN dll is loaded, so we'll get
133// a very low virtual address. (which is exactly what we want)
134//******************************************************************************
135void AllocateExeMem(char *filename)
136{
137 HFILE dllfile = 0;
138 char szFileName[CCHMAXPATH], *tmp;
139 ULONG action, ulRead, signature;
140 APIRET rc;
141 IMAGE_DOS_HEADER doshdr;
142 IMAGE_OPTIONAL_HEADER oh;
143 IMAGE_FILE_HEADER fh;
144 ULONG address = 0;
145 ULONG *memallocs;
146 ULONG alloccnt = 0;
147 ULONG diff, i, baseAddress;
148 ULONG ulSysinfo, flAllocMem = 0;
149
150 strcpy(szFileName, filename);
151 tmp = szFileName;
152 while(*tmp != ' ' && *tmp != 0)
153 tmp++;
154 *tmp = 0;
155
156 if(!strchr(szFileName, '.')) {
157 strcat(szFileName,".EXE");
158 }
159 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);
160 if(rc != 0) {
161 if(!strstr(szFileName, ".EXE")) {
162 strcat(szFileName,".EXE");
163 }
164 }
165 else DosClose(dllfile);
166
167 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);
168 if(rc) {
169 goto end; //oops
170 }
171
172 //read dos header
173 if(DosRead(dllfile, (LPVOID)&doshdr, sizeof(doshdr), &ulRead)) {
174 goto end;
175 }
176 if(DosSetFilePtr(dllfile, doshdr.e_lfanew, FILE_BEGIN, &ulRead)) {
177 goto end;
178 }
179 //read signature dword
180 if(DosRead(dllfile, (LPVOID)&signature, sizeof(signature), &ulRead)) {
181 goto end;
182 }
183 //read pe header
184 if(DosRead(dllfile, (LPVOID)&fh, sizeof(fh), &ulRead)) {
185 goto end;
186 }
187 //read optional header
188 if(DosRead(dllfile, (LPVOID)&oh, sizeof(oh), &ulRead)) {
189 goto end;
190 }
191 if(doshdr.e_magic != IMAGE_DOS_SIGNATURE || signature != IMAGE_NT_SIGNATURE) {
192 goto end;
193 }
194 if(!(fh.Characteristics & IMAGE_FILE_RELOCS_STRIPPED)) {
195 goto end; //no need to allocate anything now
196 }
197
198 // check for high memory support
199 rc = DosQuerySysInfo(QSV_VIRTUALADDRESSLIMIT, QSV_VIRTUALADDRESSLIMIT, &ulSysinfo, sizeof(ulSysinfo));
200 if (rc == 0 && ulSysinfo > 512) //VirtualAddresslimit is in MB
201 {
202 flAllocMem = PAG_ANY; // high memory support. Let's use it!
203 }
204
205 //Reserve enough space to store 4096 pointers to 1MB memory chunks
206 memallocs = (ULONG *)alloca(4096*sizeof(ULONG *));
207 if(memallocs == NULL) {
208 goto end; //oops
209 }
210
211 if(oh.ImageBase < 512*1024*1024) {
212 flAllocMem = 0;
213 }
214 else {
215 if(flAllocMem == 0) {
216 goto end; //no support for > 512 MB
217 }
218 }
219 while(TRUE) {
220 rc = DosAllocMem((PPVOID)&address, FALLOC_SIZE, PAG_READ | flAllocMem);
221 if(rc) break;
222
223 if(address + FALLOC_SIZE >= oh.ImageBase) {
224 if(address > oh.ImageBase) {//we've passed it!
225 DosFreeMem((PVOID)address);
226 break;
227 }
228 //found the right address
229 DosFreeMem((PVOID)address);
230
231 diff = oh.ImageBase - address;
232 if(diff) {
233 rc = DosAllocMem((PPVOID)&address, diff, PAG_READ | flAllocMem);
234 if(rc) break;
235 }
236 rc = DosAllocMem((PPVOID)&baseAddress, oh.SizeOfImage, PAG_READ | PAG_WRITE | flAllocMem);
237 if(rc) break;
238
239 if(diff) DosFreeMem((PVOID)address);
240
241 reservedMemory = baseAddress;
242 break;
243 }
244 memallocs[alloccnt++] = address;
245 }
246 for(i=0;i<alloccnt;i++) {
247 DosFreeMem((PVOID)memallocs[i]);
248 }
249end:
250 if(dllfile) DosClose(dllfile);
251 return;
252}
253//******************************************************************************
254//******************************************************************************
Note: See TracBrowser for help on using the repository browser.