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