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