| 1 | /* $Id: pe.cpp,v 1.27 2001-04-10 17:09:08 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, module %s)";
|
|---|
| 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 | char errorMod[CCHMAXPATH];
|
|---|
| 86 | APIRET rc;
|
|---|
| 87 | HMODULE hmodPMWin = 0, hmodKernel32 = 0;
|
|---|
| 88 | PTIB ptib;
|
|---|
| 89 | PPIB ppib;
|
|---|
| 90 | char *cmdline, *win32cmdline, *peoptions, *newcmdline;
|
|---|
| 91 | BOOL fQuote = FALSE, fVioConsole;
|
|---|
| 92 | int nrTries = 1;
|
|---|
| 93 |
|
|---|
| 94 | if(argc >= 2) {
|
|---|
| 95 | if(DosGetInfoBlocks(&ptib, &ppib) == 0) {
|
|---|
| 96 | tryagain:
|
|---|
| 97 | cmdline = ppib->pib_pchcmd;
|
|---|
| 98 | cmdline += strlen(cmdline)+1; //skip pe.exe
|
|---|
| 99 | peoptions = strstr(cmdline, "/OPT:[");
|
|---|
| 100 | if(peoptions) {
|
|---|
| 101 | newcmdline = strchr(peoptions, ']');
|
|---|
| 102 | if(newcmdline) {
|
|---|
| 103 | cmdline = newcmdline+1;
|
|---|
| 104 | }
|
|---|
| 105 | #ifdef DEBUG
|
|---|
| 106 | else _interrupt(3); //should not happen!
|
|---|
| 107 | #endif
|
|---|
| 108 | }
|
|---|
| 109 | while(*cmdline == ' ') cmdline++; //skip leading space
|
|---|
| 110 | if(*cmdline == '"') {
|
|---|
| 111 | cmdline++;
|
|---|
| 112 | fQuote = TRUE;
|
|---|
| 113 | }
|
|---|
| 114 | win32cmdline = cmdline;
|
|---|
| 115 |
|
|---|
| 116 | strncpy(exeName, cmdline, sizeof(exeName)-1);
|
|---|
| 117 | exeName[sizeof(exeName)-1] = 0;
|
|---|
| 118 | char *p = exeName;
|
|---|
| 119 | if(fQuote) {
|
|---|
| 120 | while(*p != '"' && *p != 0) p++;
|
|---|
| 121 | }
|
|---|
| 122 | else {
|
|---|
| 123 | for(int i=0;i<nrTries;i++) {
|
|---|
| 124 | while(*p != ' ' && *p != 0) p++;
|
|---|
| 125 | if(*p == 0) break;
|
|---|
| 126 | if(i != nrTries-1) {
|
|---|
| 127 | while(*p == ' ' && *p != 0) p++;
|
|---|
| 128 | }
|
|---|
| 129 | }
|
|---|
| 130 | }
|
|---|
| 131 | *p = 0;
|
|---|
| 132 | strupr(exeName);
|
|---|
| 133 | cmdline = strstr(exeName, ".EXE");
|
|---|
| 134 | if(cmdline) {
|
|---|
| 135 | cmdline[4] = 0;
|
|---|
| 136 | win32cmdline += ((ULONG)cmdline - (ULONG)exeName) + 4;
|
|---|
| 137 | }
|
|---|
| 138 | else {
|
|---|
| 139 | win32cmdline += strlen(exeName);
|
|---|
| 140 | if(strstr(exeName, ".") == NULL) {
|
|---|
| 141 | strcat(exeName, ".EXE");
|
|---|
| 142 | }
|
|---|
| 143 | }
|
|---|
| 144 | if(fQuote) win32cmdline++;
|
|---|
| 145 | while(*win32cmdline == ' ') win32cmdline++; //skip spaces
|
|---|
| 146 |
|
|---|
| 147 | cmdline = exeName + strlen(exeName) - 1;
|
|---|
| 148 | while(*cmdline == ' ') cmdline--;
|
|---|
| 149 | cmdline[1] = 0;
|
|---|
| 150 | if(DosQueryPathInfo(exeName, FIL_QUERYFULLNAME, (PVOID)fullpath, sizeof(fullpath)) == 0) {
|
|---|
| 151 | strcpy(exeName, fullpath);
|
|---|
| 152 | }
|
|---|
| 153 | FILESTATUS3 fstat3;
|
|---|
| 154 | if(DosQueryPathInfo(exeName, FIL_STANDARD, (PVOID)&fstat3, sizeof(fstat3)))
|
|---|
| 155 | {
|
|---|
| 156 | nrTries++;
|
|---|
| 157 | if(*win32cmdline != NULL && !fQuote) {
|
|---|
| 158 | goto tryagain;
|
|---|
| 159 | }
|
|---|
| 160 | }
|
|---|
| 161 | }
|
|---|
| 162 | else {//should never happen!
|
|---|
| 163 | filenotfound:
|
|---|
| 164 | rc = DosLoadModule(exeName, sizeof(exeName), "PMWIN.DLL", &hmodPMWin);
|
|---|
| 165 | rc = DosQueryProcAddr(hmodPMWin, ORD_WIN32MESSAGEBOX, NULL, (PFN *)&MyWinMessageBox);
|
|---|
| 166 | MyWinMessageBox(HWND_DESKTOP, NULL, szDosInfoBlocks, szErrorTitle, 0, MB_OK | MB_ERROR | MB_MOVEABLE);
|
|---|
| 167 | goto fail;
|
|---|
| 168 | }
|
|---|
| 169 | AllocateExeMem(exeName);
|
|---|
| 170 | }
|
|---|
| 171 |
|
|---|
| 172 | #ifdef COMMAND_LINE_VERSION
|
|---|
| 173 | if(DosGetInfoBlocks(&ptib, &ppib) == 0) {
|
|---|
| 174 | //switch process type to PM so the command line app can create PM
|
|---|
| 175 | //windows
|
|---|
| 176 | ppib->pib_ultype = 3;
|
|---|
| 177 | }
|
|---|
| 178 | #endif
|
|---|
| 179 |
|
|---|
| 180 | rc = DosLoadModule(exeName, sizeof(exeName), "PMWIN.DLL", &hmodPMWin);
|
|---|
| 181 | rc = DosQueryProcAddr(hmodPMWin, ORD_WIN32INITIALIZE, NULL, (PFN *)&MyWinInitialize);
|
|---|
| 182 | rc = DosQueryProcAddr(hmodPMWin, ORD_WIN32TERMINATE, NULL, (PFN *)&MyWinTerminate);
|
|---|
| 183 | rc = DosQueryProcAddr(hmodPMWin, ORD_WIN32CREATEMSGQUEUE, NULL, (PFN *)&MyWinCreateMsgQueue);
|
|---|
| 184 | rc = DosQueryProcAddr(hmodPMWin, ORD_WIN32DESTROYMSGQUEUE, NULL, (PFN *)&MyWinDestroyMsgQueue);
|
|---|
| 185 | rc = DosQueryProcAddr(hmodPMWin, ORD_WIN32MESSAGEBOX, NULL, (PFN *)&MyWinMessageBox);
|
|---|
| 186 |
|
|---|
| 187 | if ((hab = MyWinInitialize(0)) == 0L) /* Initialize PM */
|
|---|
| 188 | goto fail;
|
|---|
| 189 |
|
|---|
| 190 | hmq = MyWinCreateMsgQueue(hab, 0);
|
|---|
| 191 |
|
|---|
| 192 | if(argc < 2) {
|
|---|
| 193 | MyWinMessageBox(HWND_DESKTOP, NULL, INFO_BANNER, szErrorTitle, 0, MB_OK | MB_ERROR | MB_MOVEABLE);
|
|---|
| 194 | goto fail;
|
|---|
| 195 | }
|
|---|
| 196 |
|
|---|
| 197 | errorMod[0] = 0;
|
|---|
| 198 | rc = DosLoadModule(errorMod, sizeof(errorMod), "KERNEL32.DLL", &hmodKernel32);
|
|---|
| 199 | if(rc) {
|
|---|
| 200 | sprintf(fullpath, szNoKernel32Msg, rc, errorMod);
|
|---|
| 201 | MyWinMessageBox(HWND_DESKTOP, NULL, fullpath, szErrorTitle, 0, MB_OK | MB_ERROR | MB_MOVEABLE);
|
|---|
| 202 | goto fail;
|
|---|
| 203 | }
|
|---|
| 204 | rc = DosQueryProcAddr(hmodKernel32, 0, "_CreateWin32PeLdrExe@24", (PFN *)&CreateWin32Exe);
|
|---|
| 205 |
|
|---|
| 206 | #ifdef COMMAND_LINE_VERSION
|
|---|
| 207 | fVioConsole = TRUE;
|
|---|
| 208 | #else
|
|---|
| 209 | fVioConsole = FALSE;
|
|---|
| 210 | #endif
|
|---|
| 211 | if(CreateWin32Exe(exeName, win32cmdline, peoptions, reservedMemory, fConsoleApp, fVioConsole) == FALSE) {
|
|---|
| 212 | goto fail;
|
|---|
| 213 | }
|
|---|
| 214 |
|
|---|
| 215 | if(hmq) MyWinDestroyMsgQueue( hmq ); /* Tidy up... */
|
|---|
| 216 | MyWinTerminate( hab ); /* Terminate the application */
|
|---|
| 217 |
|
|---|
| 218 | DosFreeModule(hmodPMWin);
|
|---|
| 219 | DosFreeModule(hmodKernel32);
|
|---|
| 220 | return 0;
|
|---|
| 221 |
|
|---|
| 222 | fail:
|
|---|
| 223 | if(hmq) MyWinDestroyMsgQueue( hmq ); /* Tidy up... */
|
|---|
| 224 | if(hab) MyWinTerminate( hab ); /* Terminate the application */
|
|---|
| 225 |
|
|---|
| 226 | if(hmodPMWin) DosFreeModule(hmodPMWin);
|
|---|
| 227 | if(hmodKernel32) DosFreeModule(hmodKernel32);
|
|---|
| 228 | return(1);
|
|---|
| 229 | }
|
|---|
| 230 | //******************************************************************************
|
|---|
| 231 | //SvL: Reserve memory for win32 exes without fixups
|
|---|
| 232 | // This is done before any Odin or PMWIN dll is loaded, so we'll get
|
|---|
| 233 | // a very low virtual address. (which is exactly what we want)
|
|---|
| 234 | //******************************************************************************
|
|---|
| 235 | void AllocateExeMem(char *filename)
|
|---|
| 236 | {
|
|---|
| 237 | HFILE dllfile = 0;
|
|---|
| 238 | char szFileName[CCHMAXPATH], *tmp;
|
|---|
| 239 | char szResult[CCHMAXPATH];
|
|---|
| 240 | ULONG action, ulRead, signature;
|
|---|
| 241 | APIRET rc;
|
|---|
| 242 | IMAGE_DOS_HEADER doshdr;
|
|---|
| 243 | IMAGE_OPTIONAL_HEADER oh;
|
|---|
| 244 | IMAGE_FILE_HEADER fh;
|
|---|
| 245 | ULONG address = 0;
|
|---|
| 246 | ULONG *memallocs;
|
|---|
| 247 | ULONG alloccnt = 0;
|
|---|
| 248 | ULONG diff, i, baseAddress;
|
|---|
| 249 | ULONG ulSysinfo, flAllocMem = 0;
|
|---|
| 250 |
|
|---|
| 251 | strcpy(szFileName, filename);
|
|---|
| 252 |
|
|---|
| 253 | 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);
|
|---|
| 254 | if(rc != 0) {
|
|---|
| 255 | if(!strstr(szFileName, ".EXE")) {
|
|---|
| 256 | strcat(szFileName,".EXE");
|
|---|
| 257 | }
|
|---|
| 258 | }
|
|---|
| 259 | else DosClose(dllfile);
|
|---|
| 260 |
|
|---|
| 261 | 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);
|
|---|
| 262 | if(rc) {
|
|---|
| 263 | if(DosSearchPath(SEARCH_IGNORENETERRS|SEARCH_ENVIRONMENT, "PATH",
|
|---|
| 264 | szFileName, szResult, sizeof(szResult)) != 0) {
|
|---|
| 265 | goto end; //oops
|
|---|
| 266 | }
|
|---|
| 267 | 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);
|
|---|
| 268 | if(rc) {
|
|---|
| 269 | goto end; //oops
|
|---|
| 270 | }
|
|---|
| 271 | }
|
|---|
| 272 |
|
|---|
| 273 | //read dos header
|
|---|
| 274 | if(DosRead(dllfile, (LPVOID)&doshdr, sizeof(doshdr), &ulRead)) {
|
|---|
| 275 | goto end;
|
|---|
| 276 | }
|
|---|
| 277 | if(DosSetFilePtr(dllfile, doshdr.e_lfanew, FILE_BEGIN, &ulRead)) {
|
|---|
| 278 | goto end;
|
|---|
| 279 | }
|
|---|
| 280 | //read signature dword
|
|---|
| 281 | if(DosRead(dllfile, (LPVOID)&signature, sizeof(signature), &ulRead)) {
|
|---|
| 282 | goto end;
|
|---|
| 283 | }
|
|---|
| 284 | //read pe header
|
|---|
| 285 | if(DosRead(dllfile, (LPVOID)&fh, sizeof(fh), &ulRead)) {
|
|---|
| 286 | goto end;
|
|---|
| 287 | }
|
|---|
| 288 | //read optional header
|
|---|
| 289 | if(DosRead(dllfile, (LPVOID)&oh, sizeof(oh), &ulRead)) {
|
|---|
| 290 | goto end;
|
|---|
| 291 | }
|
|---|
| 292 | if(doshdr.e_magic != IMAGE_DOS_SIGNATURE || signature != IMAGE_NT_SIGNATURE) {
|
|---|
| 293 | goto end;
|
|---|
| 294 | }
|
|---|
| 295 | fConsoleApp = (oh.Subsystem == IMAGE_SUBSYSTEM_WINDOWS_CUI);
|
|---|
| 296 |
|
|---|
| 297 | // check for high memory support
|
|---|
| 298 | rc = DosQuerySysInfo(QSV_VIRTUALADDRESSLIMIT, QSV_VIRTUALADDRESSLIMIT, &ulSysinfo, sizeof(ulSysinfo));
|
|---|
| 299 | if (rc == 0 && ulSysinfo > 512) //VirtualAddresslimit is in MB
|
|---|
| 300 | {
|
|---|
| 301 | flAllocMem = PAG_ANY; // high memory support. Let's use it!
|
|---|
| 302 | }
|
|---|
| 303 |
|
|---|
| 304 | //Reserve enough space to store 4096 pointers to 1MB memory chunks
|
|---|
| 305 | memallocs = (ULONG *)alloca(4096*sizeof(ULONG *));
|
|---|
| 306 | if(memallocs == NULL) {
|
|---|
| 307 | goto end; //oops
|
|---|
| 308 | }
|
|---|
| 309 |
|
|---|
| 310 | if(oh.ImageBase < 512*1024*1024) {
|
|---|
| 311 | flAllocMem = 0;
|
|---|
| 312 | }
|
|---|
| 313 | else {
|
|---|
| 314 | if(flAllocMem == 0) {
|
|---|
| 315 | goto end; //no support for > 512 MB
|
|---|
| 316 | }
|
|---|
| 317 | }
|
|---|
| 318 | while(TRUE) {
|
|---|
| 319 | rc = DosAllocMem((PPVOID)&address, FALLOC_SIZE, PAG_READ | flAllocMem);
|
|---|
| 320 | if(rc) break;
|
|---|
| 321 |
|
|---|
| 322 | if(address + FALLOC_SIZE >= oh.ImageBase) {
|
|---|
| 323 | if(address > oh.ImageBase) {//we've passed it!
|
|---|
| 324 | DosFreeMem((PVOID)address);
|
|---|
| 325 | break;
|
|---|
| 326 | }
|
|---|
| 327 | //found the right address
|
|---|
| 328 | DosFreeMem((PVOID)address);
|
|---|
| 329 |
|
|---|
| 330 | diff = oh.ImageBase - address;
|
|---|
| 331 | if(diff) {
|
|---|
| 332 | rc = DosAllocMem((PPVOID)&address, diff, PAG_READ | flAllocMem);
|
|---|
| 333 | if(rc) break;
|
|---|
| 334 | }
|
|---|
| 335 | rc = DosAllocMem((PPVOID)&baseAddress, oh.SizeOfImage, PAG_READ | PAG_WRITE | flAllocMem);
|
|---|
| 336 | if(rc) break;
|
|---|
| 337 |
|
|---|
| 338 | if(diff) DosFreeMem((PVOID)address);
|
|---|
| 339 |
|
|---|
| 340 | reservedMemory = baseAddress;
|
|---|
| 341 | break;
|
|---|
| 342 | }
|
|---|
| 343 | memallocs[alloccnt++] = address;
|
|---|
| 344 | }
|
|---|
| 345 | for(i=0;i<alloccnt;i++) {
|
|---|
| 346 | DosFreeMem((PVOID)memallocs[i]);
|
|---|
| 347 | }
|
|---|
| 348 | end:
|
|---|
| 349 | if(dllfile) DosClose(dllfile);
|
|---|
| 350 | return;
|
|---|
| 351 | }
|
|---|
| 352 | //******************************************************************************
|
|---|
| 353 | //******************************************************************************
|
|---|