| 1 | /* $Id: winexepeldr.cpp,v 1.19 2002-07-23 13:51:48 sandervl Exp $ */
|
|---|
| 2 |
|
|---|
| 3 | /*
|
|---|
| 4 | * Win32 PE loader Exe class
|
|---|
| 5 | *
|
|---|
| 6 | * Copyright 1998-2000 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_WIN
|
|---|
| 17 | #include <os2wrap.h> //Odin32 OS/2 api wrappers
|
|---|
| 18 | #include <stdio.h>
|
|---|
| 19 | #include <string.h>
|
|---|
| 20 | #include <stdlib.h>
|
|---|
| 21 | #include <iostream.h>
|
|---|
| 22 | #include <fstream.h>
|
|---|
| 23 | #include <misc.h>
|
|---|
| 24 | #include <win32type.h>
|
|---|
| 25 | #include <win32api.h>
|
|---|
| 26 | #include <winexepeldr.h>
|
|---|
| 27 | #include <wprocess.h>
|
|---|
| 28 | #include <pefile.h>
|
|---|
| 29 |
|
|---|
| 30 | #include "conwin.h" // Windows Header for console only
|
|---|
| 31 | #include "console.h"
|
|---|
| 32 |
|
|---|
| 33 | #include "exceptions.h"
|
|---|
| 34 | #include "exceptutil.h"
|
|---|
| 35 |
|
|---|
| 36 | #include "cio.h"
|
|---|
| 37 | #include "oslibmisc.h"
|
|---|
| 38 |
|
|---|
| 39 | #define DBG_LOCALLOG DBG_winexepeldr
|
|---|
| 40 | #include "dbglocal.h"
|
|---|
| 41 |
|
|---|
| 42 |
|
|---|
| 43 | #ifdef PROFILE
|
|---|
| 44 | #include <perfview.h>
|
|---|
| 45 | #include <profiler.h>
|
|---|
| 46 | #endif /* PROFILE */
|
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 49 | extern char szErrorTitle[];
|
|---|
| 50 | extern char szErrorModule[];
|
|---|
| 51 |
|
|---|
| 52 | BOOL fPeLoader = FALSE;
|
|---|
| 53 |
|
|---|
| 54 | //******************************************************************************
|
|---|
| 55 | //Called by ring 3 pe loader to create win32 executable
|
|---|
| 56 | //PE.EXE command line options:
|
|---|
| 57 | // /OPT:[x1=y,x2=z,..]
|
|---|
| 58 | // x = CURDIR -> set current directory to y
|
|---|
| 59 | // (not other options available at this time)
|
|---|
| 60 | //******************************************************************************
|
|---|
| 61 | BOOL WIN32API CreateWin32PeLdrExe(char *szFileName, char *szCmdLine,
|
|---|
| 62 | char *peoptions,
|
|---|
| 63 | ULONG reservedMem, ULONG ulPEOffset,
|
|---|
| 64 | BOOL fConsoleApp, BOOL fVioConsole)
|
|---|
| 65 | {
|
|---|
| 66 | APIRET rc;
|
|---|
| 67 | PPIB ppib;
|
|---|
| 68 | PTIB ptib;
|
|---|
| 69 | WINEXCEPTION_FRAME exceptFrame;
|
|---|
| 70 | Win32PeLdrExe *WinExe;
|
|---|
| 71 | char *szFullCmdLine;
|
|---|
| 72 |
|
|---|
| 73 | fPeLoader = TRUE;
|
|---|
| 74 |
|
|---|
| 75 | WinExe = new Win32PeLdrExe(szFileName, fConsoleApp);
|
|---|
| 76 |
|
|---|
| 77 | rc = DosGetInfoBlocks(&ptib, &ppib);
|
|---|
| 78 | if(rc) {
|
|---|
| 79 | WinMessageBox(HWND_DESKTOP, HWND_DESKTOP, szInteralErrorMsg, szErrorTitle, 0, MB_OK | MB_ERROR | MB_MOVEABLE);
|
|---|
| 80 | delete WinExe;
|
|---|
| 81 | return FALSE;
|
|---|
| 82 | }
|
|---|
| 83 | //Handle special pe cmd line options here (/OPT:[x1=y,x2=z,..])
|
|---|
| 84 | if(peoptions) {
|
|---|
| 85 | char *option;
|
|---|
| 86 |
|
|---|
| 87 | option = strchr(peoptions, '[');
|
|---|
| 88 | if(option) {
|
|---|
| 89 | option++;
|
|---|
| 90 | option = strstr(option, "CURDIR=");
|
|---|
| 91 | if(option) {
|
|---|
| 92 | char *curdir, *tmp;
|
|---|
| 93 | int curdirlength;
|
|---|
| 94 |
|
|---|
| 95 | option += 7;
|
|---|
| 96 | tmp = option;
|
|---|
| 97 | while(*tmp != ']' && *tmp != ',' && *tmp != 0) {
|
|---|
| 98 | tmp++;
|
|---|
| 99 | }
|
|---|
| 100 | curdirlength = (int)(tmp-option);
|
|---|
| 101 | curdir = (char *)malloc(curdirlength+1);
|
|---|
| 102 | memcpy(curdir, option, curdirlength);
|
|---|
| 103 | curdir[curdirlength] = 0;
|
|---|
| 104 | SetCurrentDirectoryA((LPCSTR)curdir);
|
|---|
| 105 | free(curdir);
|
|---|
| 106 | }
|
|---|
| 107 | }
|
|---|
| 108 | }
|
|---|
| 109 | //exe length + space + (possibly) 2x'"' + cmd line length + 0 terminator
|
|---|
| 110 | szFullCmdLine = (char *)malloc(strlen(szFileName) + 3 + strlen(szCmdLine) + 1);
|
|---|
| 111 | //Enclose executable name in quotes if it (or it's directory) contains spaces
|
|---|
| 112 | if(strchr(szFileName, ' ') != NULL) {
|
|---|
| 113 | sprintf(szFullCmdLine, "\"%s\"", szFileName);
|
|---|
| 114 | }
|
|---|
| 115 | else strcpy(szFullCmdLine, szFileName);
|
|---|
| 116 | strcat(szFullCmdLine, " ");
|
|---|
| 117 | strcat(szFullCmdLine, szCmdLine);
|
|---|
| 118 | InitCommandLine(szFullCmdLine);
|
|---|
| 119 | dprintf(("Cmd line: %s", szFullCmdLine));
|
|---|
| 120 | free(szFullCmdLine);
|
|---|
| 121 |
|
|---|
| 122 | if(getenv("WIN32_IOPL2")) {
|
|---|
| 123 | io_init1();
|
|---|
| 124 | }
|
|---|
| 125 | //Init console before loading executable as dlls might want to print
|
|---|
| 126 | //something on the console while being loaded
|
|---|
| 127 | if(WinExe->isConsoleApp())
|
|---|
| 128 | {
|
|---|
| 129 | dprintf(("Console application!\n"));
|
|---|
| 130 |
|
|---|
| 131 | APIRET rc = iConsoleInit(fVioConsole); /* initialize console subsystem */
|
|---|
| 132 | if (rc != NO_ERROR) /* check for errors */
|
|---|
| 133 | dprintf(("KERNEL32:Win32Image:Init ConsoleInit failed with %u.\n", rc));
|
|---|
| 134 | }
|
|---|
| 135 |
|
|---|
| 136 | OS2SetExceptionHandler(&exceptFrame);
|
|---|
| 137 | if(WinExe->init(reservedMem, ulPEOffset) == FALSE)
|
|---|
| 138 | {
|
|---|
| 139 | if(szErrorModule[0] != 0) {
|
|---|
| 140 | char szErrorMsg[128];
|
|---|
| 141 |
|
|---|
| 142 | sprintf(szErrorMsg, "Can't execute %s due to bad or missing %s", OSLibStripPath(szFileName), szErrorModule);
|
|---|
| 143 | WinMessageBox(HWND_DESKTOP, HWND_DESKTOP, szErrorMsg, szErrorTitle, 0, MB_OK | MB_ERROR | MB_MOVEABLE);
|
|---|
| 144 | }
|
|---|
| 145 | delete WinExe;
|
|---|
| 146 | OS2UnsetExceptionHandler(&exceptFrame);
|
|---|
| 147 | return FALSE;
|
|---|
| 148 | }
|
|---|
| 149 | OS2UnsetExceptionHandler(&exceptFrame);
|
|---|
| 150 |
|
|---|
| 151 | #ifdef PROFILE
|
|---|
| 152 | // Note: after this point, we might start collecting performance
|
|---|
| 153 | // information about the called functions.
|
|---|
| 154 | PerfView_Initialize();
|
|---|
| 155 | ProfilerInitialize();
|
|---|
| 156 | ProfilerEnable(TRUE);
|
|---|
| 157 | #endif /* PROFILE */
|
|---|
| 158 |
|
|---|
| 159 |
|
|---|
| 160 | WinExe->start();
|
|---|
| 161 |
|
|---|
| 162 | delete WinExe;
|
|---|
| 163 |
|
|---|
| 164 | return TRUE;
|
|---|
| 165 | }
|
|---|
| 166 | //******************************************************************************
|
|---|
| 167 | //******************************************************************************
|
|---|
| 168 | Win32PeLdrExe::Win32PeLdrExe(char *szFileName, BOOL fConsoleApp) :
|
|---|
| 169 | Win32ImageBase(-1),
|
|---|
| 170 | Win32ExeBase(-1),
|
|---|
| 171 | Win32PeLdrImage(szFileName, TRUE)
|
|---|
| 172 | {
|
|---|
| 173 | dprintf(("Win32PeLdrExe ctor: %s", szFileName));
|
|---|
| 174 | this->fConsoleApp = fConsoleApp;
|
|---|
| 175 |
|
|---|
| 176 | //SvL: set temporary full path here as console init needs it
|
|---|
| 177 | setFullPath(szFileName);
|
|---|
| 178 | }
|
|---|
| 179 | //******************************************************************************
|
|---|
| 180 | //******************************************************************************
|
|---|
| 181 | Win32PeLdrExe::~Win32PeLdrExe()
|
|---|
| 182 | {
|
|---|
| 183 | fExitProcess = TRUE;
|
|---|
| 184 | }
|
|---|
| 185 | //******************************************************************************
|
|---|
| 186 | //******************************************************************************
|
|---|