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