| 1 | /* $Id: pe.cpp,v 1.16 2000-04-14 22:56:11 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 |  | 
|---|
| 87 | if(argc >= 2) { | 
|---|
| 88 | if(DosGetInfoBlocks(&ptib, &ppib) == 0) { | 
|---|
| 89 | cmdline = ppib->pib_pchcmd; | 
|---|
| 90 | cmdline += strlen(cmdline)+1;   //skip pe.exe | 
|---|
| 91 | while(*cmdline == ' ')  cmdline++; //skip leading space | 
|---|
| 92 | if(*cmdline == '"')     cmdline++; | 
|---|
| 93 | win32cmdline = cmdline; | 
|---|
| 94 |  | 
|---|
| 95 | strncpy(exeName, cmdline, sizeof(exeName)-1); | 
|---|
| 96 | exeName[sizeof(exeName)-1] = 0; | 
|---|
| 97 | strupr(exeName); | 
|---|
| 98 | cmdline = strstr(exeName, ".EXE"); | 
|---|
| 99 | if(cmdline) { | 
|---|
| 100 | cmdline[4] = 0; | 
|---|
| 101 | win32cmdline += ((ULONG)cmdline - (ULONG)exeName) + 4; | 
|---|
| 102 | } | 
|---|
| 103 | else { | 
|---|
| 104 | cmdline = exeName; | 
|---|
| 105 | while(*cmdline && *cmdline != ' ')      cmdline++; //skip exe name | 
|---|
| 106 | *cmdline = 0; | 
|---|
| 107 | win32cmdline += ((ULONG)cmdline - (ULONG)exeName); | 
|---|
| 108 | if(strstr(exeName, ".") == NULL) { | 
|---|
| 109 | strcat(exeName, ".EXE"); | 
|---|
| 110 | } | 
|---|
| 111 | } | 
|---|
| 112 | while(*win32cmdline == ' ') win32cmdline++; //skip spaces | 
|---|
| 113 |  | 
|---|
| 114 | cmdline = exeName + strlen(exeName) - 1; | 
|---|
| 115 | while(*cmdline == ' ') cmdline--; | 
|---|
| 116 | cmdline[1] = 0; | 
|---|
| 117 | if(DosQueryPathInfo(exeName, FIL_QUERYFULLNAME, (PVOID)fullpath, sizeof(fullpath)) == 0) { | 
|---|
| 118 | strcpy(exeName, fullpath); | 
|---|
| 119 | } | 
|---|
| 120 | } | 
|---|
| 121 | else {//should never happen! | 
|---|
| 122 | DebugInt3(); | 
|---|
| 123 | rc = DosLoadModule(exeName, sizeof(exeName), "PMWIN.DLL", &hmodPMWin); | 
|---|
| 124 | rc = DosQueryProcAddr(hmodPMWin, ORD_WIN32MESSAGEBOX, NULL, (PFN *)&MyWinMessageBox); | 
|---|
| 125 | MyWinMessageBox(HWND_DESKTOP, NULL, szDosInfoBlocks, szErrorTitle, 0, MB_OK | MB_ERROR | MB_MOVEABLE); | 
|---|
| 126 | goto fail; | 
|---|
| 127 | } | 
|---|
| 128 | AllocateExeMem(exeName); | 
|---|
| 129 | } | 
|---|
| 130 |  | 
|---|
| 131 | rc = DosLoadModule(exeName, sizeof(exeName), "PMWIN.DLL", &hmodPMWin); | 
|---|
| 132 | rc = DosQueryProcAddr(hmodPMWin, ORD_WIN32INITIALIZE, NULL, (PFN *)&MyWinInitialize); | 
|---|
| 133 | rc = DosQueryProcAddr(hmodPMWin, ORD_WIN32TERMINATE, NULL, (PFN *)&MyWinTerminate); | 
|---|
| 134 | rc = DosQueryProcAddr(hmodPMWin, ORD_WIN32CREATEMSGQUEUE, NULL, (PFN *)&MyWinCreateMsgQueue); | 
|---|
| 135 | rc = DosQueryProcAddr(hmodPMWin, ORD_WIN32DESTROYMSGQUEUE, NULL, (PFN *)&MyWinDestroyMsgQueue); | 
|---|
| 136 | rc = DosQueryProcAddr(hmodPMWin, ORD_WIN32MESSAGEBOX, NULL, (PFN *)&MyWinMessageBox); | 
|---|
| 137 |  | 
|---|
| 138 | if ((hab = MyWinInitialize(0)) == 0L) /* Initialize PM     */ | 
|---|
| 139 | goto fail; | 
|---|
| 140 |  | 
|---|
| 141 | hmq = MyWinCreateMsgQueue(hab, 0); | 
|---|
| 142 |  | 
|---|
| 143 | if(argc < 2) { | 
|---|
| 144 | MyWinMessageBox(HWND_DESKTOP, NULL, INFO_BANNER, szErrorTitle, 0, MB_OK | MB_ERROR | MB_MOVEABLE); | 
|---|
| 145 | goto fail; | 
|---|
| 146 | } | 
|---|
| 147 |  | 
|---|
| 148 | rc = DosLoadModule(exeName, sizeof(exeName), "KERNEL32.DLL", &hmodKernel32); | 
|---|
| 149 | if(rc) { | 
|---|
| 150 | sprintf(exeName, szNoKernel32Msg, rc); | 
|---|
| 151 | MyWinMessageBox(HWND_DESKTOP, NULL, exeName, szErrorTitle, 0, MB_OK | MB_ERROR | MB_MOVEABLE); | 
|---|
| 152 | goto fail; | 
|---|
| 153 | } | 
|---|
| 154 | rc = DosQueryProcAddr(hmodKernel32, 0, "_CreateWin32PeLdrExe@12", (PFN *)&CreateWin32Exe); | 
|---|
| 155 |  | 
|---|
| 156 | if(CreateWin32Exe(exeName, win32cmdline, reservedMemory) == FALSE) { | 
|---|
| 157 | goto fail; | 
|---|
| 158 | } | 
|---|
| 159 |  | 
|---|
| 160 | if(hmq) MyWinDestroyMsgQueue( hmq );             /* Tidy up...                   */ | 
|---|
| 161 | MyWinTerminate( hab );                   /* Terminate the application    */ | 
|---|
| 162 |  | 
|---|
| 163 | DosFreeModule(hmodPMWin); | 
|---|
| 164 | DosFreeModule(hmodKernel32); | 
|---|
| 165 | return 0; | 
|---|
| 166 |  | 
|---|
| 167 | fail: | 
|---|
| 168 | if(hmq) MyWinDestroyMsgQueue( hmq );             /* Tidy up...                   */ | 
|---|
| 169 | if(hab) MyWinTerminate( hab );                   /* Terminate the application    */ | 
|---|
| 170 |  | 
|---|
| 171 | if(hmodPMWin)         DosFreeModule(hmodPMWin); | 
|---|
| 172 | if(hmodKernel32)      DosFreeModule(hmodKernel32); | 
|---|
| 173 | return(1); | 
|---|
| 174 | } | 
|---|
| 175 | //****************************************************************************** | 
|---|
| 176 | //SvL: Reserve memory for win32 exes without fixups | 
|---|
| 177 | //     This is done before any Odin or PMWIN dll is loaded, so we'll get | 
|---|
| 178 | //     a very low virtual address. (which is exactly what we want) | 
|---|
| 179 | //****************************************************************************** | 
|---|
| 180 | void AllocateExeMem(char *filename) | 
|---|
| 181 | { | 
|---|
| 182 | HFILE  dllfile = 0; | 
|---|
| 183 | char   szFileName[CCHMAXPATH], *tmp; | 
|---|
| 184 | char   szResult[CCHMAXPATH]; | 
|---|
| 185 | ULONG  action, ulRead, signature; | 
|---|
| 186 | APIRET rc; | 
|---|
| 187 | IMAGE_DOS_HEADER doshdr; | 
|---|
| 188 | IMAGE_OPTIONAL_HEADER oh; | 
|---|
| 189 | IMAGE_FILE_HEADER     fh; | 
|---|
| 190 | ULONG  address = 0; | 
|---|
| 191 | ULONG  *memallocs; | 
|---|
| 192 | ULONG  alloccnt = 0; | 
|---|
| 193 | ULONG  diff, i, baseAddress; | 
|---|
| 194 | ULONG  ulSysinfo, flAllocMem = 0; | 
|---|
| 195 |  | 
|---|
| 196 | strcpy(szFileName, filename); | 
|---|
| 197 |  | 
|---|
| 198 | 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); | 
|---|
| 199 | if(rc != 0) { | 
|---|
| 200 | if(!strstr(szFileName, ".EXE")) { | 
|---|
| 201 | strcat(szFileName,".EXE"); | 
|---|
| 202 | } | 
|---|
| 203 | } | 
|---|
| 204 | else  DosClose(dllfile); | 
|---|
| 205 |  | 
|---|
| 206 | 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); | 
|---|
| 207 | if(rc) { | 
|---|
| 208 | if(DosSearchPath(SEARCH_IGNORENETERRS|SEARCH_ENVIRONMENT, "PATH", | 
|---|
| 209 | szFileName, szResult, sizeof(szResult)) != 0) { | 
|---|
| 210 | goto end; //oops | 
|---|
| 211 | } | 
|---|
| 212 | 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); | 
|---|
| 213 | if(rc) { | 
|---|
| 214 | goto end; //oops | 
|---|
| 215 | } | 
|---|
| 216 | } | 
|---|
| 217 |  | 
|---|
| 218 | //read dos header | 
|---|
| 219 | if(DosRead(dllfile, (LPVOID)&doshdr, sizeof(doshdr), &ulRead)) { | 
|---|
| 220 | goto end; | 
|---|
| 221 | } | 
|---|
| 222 | if(DosSetFilePtr(dllfile, doshdr.e_lfanew, FILE_BEGIN, &ulRead)) { | 
|---|
| 223 | goto end; | 
|---|
| 224 | } | 
|---|
| 225 | //read signature dword | 
|---|
| 226 | if(DosRead(dllfile, (LPVOID)&signature, sizeof(signature), &ulRead)) { | 
|---|
| 227 | goto end; | 
|---|
| 228 | } | 
|---|
| 229 | //read pe header | 
|---|
| 230 | if(DosRead(dllfile, (LPVOID)&fh, sizeof(fh), &ulRead)) { | 
|---|
| 231 | goto end; | 
|---|
| 232 | } | 
|---|
| 233 | //read optional header | 
|---|
| 234 | if(DosRead(dllfile, (LPVOID)&oh, sizeof(oh), &ulRead)) { | 
|---|
| 235 | goto end; | 
|---|
| 236 | } | 
|---|
| 237 | if(doshdr.e_magic != IMAGE_DOS_SIGNATURE || signature != IMAGE_NT_SIGNATURE) { | 
|---|
| 238 | goto end; | 
|---|
| 239 | } | 
|---|
| 240 | if(!(fh.Characteristics & IMAGE_FILE_RELOCS_STRIPPED)) { | 
|---|
| 241 | goto end; //no need to allocate anything now | 
|---|
| 242 | } | 
|---|
| 243 |  | 
|---|
| 244 | // check for high memory support | 
|---|
| 245 | rc = DosQuerySysInfo(QSV_VIRTUALADDRESSLIMIT, QSV_VIRTUALADDRESSLIMIT, &ulSysinfo, sizeof(ulSysinfo)); | 
|---|
| 246 | if (rc == 0 && ulSysinfo > 512)   //VirtualAddresslimit is in MB | 
|---|
| 247 | { | 
|---|
| 248 | flAllocMem = PAG_ANY;      // high memory support. Let's use it! | 
|---|
| 249 | } | 
|---|
| 250 |  | 
|---|
| 251 | //Reserve enough space to store 4096 pointers to 1MB memory chunks | 
|---|
| 252 | memallocs = (ULONG *)alloca(4096*sizeof(ULONG *)); | 
|---|
| 253 | if(memallocs == NULL) { | 
|---|
| 254 | goto end; //oops | 
|---|
| 255 | } | 
|---|
| 256 |  | 
|---|
| 257 | if(oh.ImageBase < 512*1024*1024) { | 
|---|
| 258 | flAllocMem = 0; | 
|---|
| 259 | } | 
|---|
| 260 | else { | 
|---|
| 261 | if(flAllocMem == 0) { | 
|---|
| 262 | goto end; //no support for > 512 MB | 
|---|
| 263 | } | 
|---|
| 264 | } | 
|---|
| 265 | while(TRUE) { | 
|---|
| 266 | rc = DosAllocMem((PPVOID)&address, FALLOC_SIZE, PAG_READ | flAllocMem); | 
|---|
| 267 | if(rc) break; | 
|---|
| 268 |  | 
|---|
| 269 | if(address + FALLOC_SIZE >= oh.ImageBase) { | 
|---|
| 270 | if(address > oh.ImageBase) {//we've passed it! | 
|---|
| 271 | DosFreeMem((PVOID)address); | 
|---|
| 272 | break; | 
|---|
| 273 | } | 
|---|
| 274 | //found the right address | 
|---|
| 275 | DosFreeMem((PVOID)address); | 
|---|
| 276 |  | 
|---|
| 277 | diff = oh.ImageBase - address; | 
|---|
| 278 | if(diff) { | 
|---|
| 279 | rc = DosAllocMem((PPVOID)&address, diff, PAG_READ | flAllocMem); | 
|---|
| 280 | if(rc) break; | 
|---|
| 281 | } | 
|---|
| 282 | rc = DosAllocMem((PPVOID)&baseAddress, oh.SizeOfImage, PAG_READ | PAG_WRITE | flAllocMem); | 
|---|
| 283 | if(rc) break; | 
|---|
| 284 |  | 
|---|
| 285 | if(diff) DosFreeMem((PVOID)address); | 
|---|
| 286 |  | 
|---|
| 287 | reservedMemory = baseAddress; | 
|---|
| 288 | break; | 
|---|
| 289 | } | 
|---|
| 290 | memallocs[alloccnt++] = address; | 
|---|
| 291 | } | 
|---|
| 292 | for(i=0;i<alloccnt;i++) { | 
|---|
| 293 | DosFreeMem((PVOID)memallocs[i]); | 
|---|
| 294 | } | 
|---|
| 295 | end: | 
|---|
| 296 | if(dllfile) DosClose(dllfile); | 
|---|
| 297 | return; | 
|---|
| 298 | } | 
|---|
| 299 | //****************************************************************************** | 
|---|
| 300 | //****************************************************************************** | 
|---|