1 | /* $Id: winexepe2lx.cpp,v 1.14 2004-01-15 10:39:10 sandervl Exp $ */
|
---|
2 |
|
---|
3 | /*
|
---|
4 | * Win32 PE2LX Exe class
|
---|
5 | *
|
---|
6 | * Copyright 1998-1999 Sander van Leeuwen (sandervl@xs4all.nl)
|
---|
7 | * Copyright 1999-2000 knut st. osmundsen (knut.stange.osmundsen@mynd.no)
|
---|
8 | *
|
---|
9 | * Project Odin Software License can be found in LICENSE.TXT
|
---|
10 | *
|
---|
11 | */
|
---|
12 |
|
---|
13 | /*******************************************************************************
|
---|
14 | * Defined Constants And Macros *
|
---|
15 | *******************************************************************************/
|
---|
16 | #define INCL_DOSERRORS /* DOS Error values */
|
---|
17 |
|
---|
18 |
|
---|
19 | /*******************************************************************************
|
---|
20 | * Header Files *
|
---|
21 | *******************************************************************************/
|
---|
22 | #include <os2wrap.h> //Odin32 OS/2 api wrappers
|
---|
23 |
|
---|
24 | #include <stdlib.h> //getenv
|
---|
25 |
|
---|
26 | #include <misc.h>
|
---|
27 | #include <win32type.h>
|
---|
28 | #include <win32k.h>
|
---|
29 | #include "winexepe2lx.h"
|
---|
30 | #include <cpuhlp.h>
|
---|
31 | #include <wprocess.h>
|
---|
32 | #include <win32api.h>
|
---|
33 | #include <odinpe.h>
|
---|
34 |
|
---|
35 | #include "oslibmisc.h" // OSLibGetDllName
|
---|
36 | #include "conwin.h" // Windows Header for console only
|
---|
37 | #include "console.h"
|
---|
38 |
|
---|
39 | #include "exceptions.h"
|
---|
40 | #include "exceptutil.h"
|
---|
41 |
|
---|
42 | #define DBG_LOCALLOG DBG_winexepe2lx
|
---|
43 | #include "dbglocal.h"
|
---|
44 |
|
---|
45 |
|
---|
46 | /*******************************************************************************
|
---|
47 | * Global Variables *
|
---|
48 | *******************************************************************************/
|
---|
49 | BOOL Win32Pe2LxExe::fEarlyInit = FALSE;
|
---|
50 |
|
---|
51 | extern "C" {
|
---|
52 |
|
---|
53 | /**
|
---|
54 | * Register a Pe2Lx Executable module.
|
---|
55 | * This is called from the TIBFix code in the Pe2Lx exe. It creates the WinExe object from
|
---|
56 | * the instance handle passed in.
|
---|
57 | * @param ulPe2LxVersion Pe2Lx version number.
|
---|
58 | * @param hinstance Module handle.
|
---|
59 | * @param ulReserved Reserved.
|
---|
60 | * @sketch I/O init.
|
---|
61 | * Check that pe2lx version matches the version of kernel32.dll.
|
---|
62 | * Frees WinExe if is not NULL - should never happen!
|
---|
63 | * Write info to the log.
|
---|
64 | * Create Pe2Lx Exe object.
|
---|
65 | * Call start (which calls the entry point).
|
---|
66 | * @status completely implemented.
|
---|
67 | * @author Sander van Leeuwen, knut st. osmundsen
|
---|
68 | */
|
---|
69 | void WIN32API RegisterPe2LxExe(ULONG ulPe2LxVersion, HINSTANCE hinstance, ULONG ulReserved)
|
---|
70 | {
|
---|
71 | Win32Pe2LxExe *pWinPe2LxExe;
|
---|
72 |
|
---|
73 | /* Check that pe2lx version matches the version of kernel32.dll. */
|
---|
74 | CheckVersion(ulPe2LxVersion & ~0x80000000UL, OSLibGetDllName(hinstance));
|
---|
75 |
|
---|
76 | /* Write info to the log. */
|
---|
77 | dprintf(("RegisterPe2LxExe: ulPe2LxVersion = %#x\n", ulPe2LxVersion));
|
---|
78 | dprintf(("RegisterPe2LxExe: hinstance = %#x\n", hinstance));
|
---|
79 | dprintf(("RegisterPe2LxExe: ulReserved = %#x\n", ulReserved));
|
---|
80 | dprintf(("RegisterPe2LxExe: name = %s\n", OSLibGetDllName(hinstance)));
|
---|
81 |
|
---|
82 | /* Might allready be initiated because of early init. */
|
---|
83 | if ( WinExe != NULL
|
---|
84 | && ( (ulPe2LxVersion & 0x80000000UL) != 0x80000000UL)
|
---|
85 | || !Win32Pe2LxExe::fEarlyInit)
|
---|
86 | {
|
---|
87 | delete WinExe;
|
---|
88 | WinExe = NULL;
|
---|
89 | }
|
---|
90 |
|
---|
91 | if (WinExe == NULL)
|
---|
92 | {
|
---|
93 | /* Create Pe2Lx Exe object. */
|
---|
94 | pWinPe2LxExe = new Win32Pe2LxExe(hinstance, (ulPe2LxVersion & 0x80000000UL) == 0x80000000UL);
|
---|
95 | if (pWinPe2LxExe == NULL)
|
---|
96 | {
|
---|
97 | eprintf(("RegisterPe2LxExe: new returned a NULL-pointer\n"));
|
---|
98 | return;
|
---|
99 | }
|
---|
100 | if (pWinPe2LxExe->init() != LDRERROR_SUCCESS)
|
---|
101 | {
|
---|
102 | eprintf(("RegisterPe2LxExe: init-method failed.\n"));
|
---|
103 | delete pWinPe2LxExe;
|
---|
104 | return;
|
---|
105 | }
|
---|
106 | }
|
---|
107 | else
|
---|
108 | pWinPe2LxExe = (Win32Pe2LxExe*)WinExe;
|
---|
109 |
|
---|
110 | /* Call start (which calls the entry point). */
|
---|
111 | /*DebugInt3();*/
|
---|
112 | pWinPe2LxExe->start();
|
---|
113 | }
|
---|
114 |
|
---|
115 |
|
---|
116 | /**
|
---|
117 | * Constructor - creates an pe2lx exe object from a module handle to a pe2lx exe module.
|
---|
118 | * @param hinstance Module handle.
|
---|
119 | * @param fWin32k TRUE: Win32k module.
|
---|
120 | * FALSE: Pe2Lx module.
|
---|
121 | * @status completely implmented.
|
---|
122 | * @author Sander van Leeuwen, knut st. osmundsen
|
---|
123 | * @remark Win32Pe2LxImage may throw an exception!
|
---|
124 | */
|
---|
125 | Win32Pe2LxExe::Win32Pe2LxExe(HINSTANCE hinstance, BOOL fWin32k)
|
---|
126 | : Win32ImageBase(hinstance),
|
---|
127 | Win32ExeBase(hinstance),
|
---|
128 | Win32Pe2LxImage(hinstance, fWin32k)
|
---|
129 | {
|
---|
130 | }
|
---|
131 |
|
---|
132 |
|
---|
133 | /**
|
---|
134 | * Destructor - does nothing.
|
---|
135 | * @status completely implemented.
|
---|
136 | * @author Sander van Leeuwen
|
---|
137 | */
|
---|
138 | Win32Pe2LxExe::~Win32Pe2LxExe()
|
---|
139 | {
|
---|
140 |
|
---|
141 | }
|
---|
142 |
|
---|
143 |
|
---|
144 | /**
|
---|
145 | * Init object.
|
---|
146 | * Must be called immedeately after the object construction.
|
---|
147 | * @returns Success indicator. (TRUE == success)
|
---|
148 | * @sketch
|
---|
149 | * @status completely implemented.
|
---|
150 | * @author knut st. osmundsen
|
---|
151 | */
|
---|
152 | BOOL Win32Pe2LxExe::init()
|
---|
153 | {
|
---|
154 | if (Win32Pe2LxImage::init())
|
---|
155 | {
|
---|
156 | fConsoleApp = pNtHdrs->OptionalHeader.Subsystem == IMAGE_SUBSYSTEM_WINDOWS_CUI;
|
---|
157 |
|
---|
158 | /* console app? */
|
---|
159 | if (fConsoleApp)
|
---|
160 | {
|
---|
161 | APIRET rc;
|
---|
162 |
|
---|
163 | dprintf(("Console application!\n"));
|
---|
164 |
|
---|
165 | rc = iConsoleInit(TRUE); /* initialize console subsystem */
|
---|
166 | if (rc != NO_ERROR) /* check for errors */
|
---|
167 | dprintf(("KERNEL32:Win32Image:Init ConsoleInit failed with %u.\n", rc));
|
---|
168 | }
|
---|
169 | }
|
---|
170 | else
|
---|
171 | return FALSE;
|
---|
172 | return TRUE;
|
---|
173 | }
|
---|
174 |
|
---|
175 |
|
---|
176 |
|
---|
177 | /**
|
---|
178 | * Preinitiate the executable before RegisterPe2LxExe is called.
|
---|
179 | * This is done by the first Pe2Lx DLL which is loaded.
|
---|
180 | *
|
---|
181 | * @returns Success idicator.
|
---|
182 | * @status
|
---|
183 | * @author knut st. osmundsen (kosmunds@csc.no)
|
---|
184 | * @remark
|
---|
185 | */
|
---|
186 | BOOL Win32Pe2LxExe::earlyInit()
|
---|
187 | {
|
---|
188 | /*
|
---|
189 | * Try make an win32k loaded executable object.
|
---|
190 | */
|
---|
191 | Win32Pe2LxExe * pExe = new Win32Pe2LxExe((HINSTANCE)OSLibGetPIB(PIB_TASKHNDL), libWin32kInstalled());
|
---|
192 | if (pExe)
|
---|
193 | {
|
---|
194 | if (pExe->init() == LDRERROR_SUCCESS)
|
---|
195 | {
|
---|
196 | WinExe = pExe;
|
---|
197 | return fEarlyInit = TRUE;
|
---|
198 | }
|
---|
199 | }
|
---|
200 |
|
---|
201 | return FALSE;
|
---|
202 | }
|
---|
203 | //******************************************************************************
|
---|
204 | //******************************************************************************
|
---|
205 | ULONG Win32Pe2LxExe::start()
|
---|
206 | {
|
---|
207 | WINEXCEPTION_FRAME exceptFrame;
|
---|
208 | ULONG rc;
|
---|
209 |
|
---|
210 | dprintf(("Start executable %X\n", WinExe));
|
---|
211 |
|
---|
212 | fExeStarted = TRUE;
|
---|
213 |
|
---|
214 | //Allocate TLS index for this module
|
---|
215 | tlsAlloc();
|
---|
216 | tlsAttachThread(); //setup TLS (main thread)
|
---|
217 |
|
---|
218 | //Note: The Win32 exception structure references by FS:[0] is the same
|
---|
219 | // in OS/2
|
---|
220 | OS2SetExceptionHandler((void *)&exceptFrame);
|
---|
221 | USHORT sel = SetWin32TIB(isPEImage() ? TIB_SWITCH_FORCE_WIN32 : TIB_SWITCH_DEFAULT);
|
---|
222 |
|
---|
223 | //Set FPU control word to 0x27F (same as in NT)
|
---|
224 | CONTROL87(0x27F, 0xFFF);
|
---|
225 | dprintf(("KERNEL32: Win32ExeBase::start exe at %08xh\n",
|
---|
226 | (void*)entryPoint ));
|
---|
227 | rc = CallEntryPoint(entryPoint, NULL);
|
---|
228 |
|
---|
229 | SetFS(sel); //restore FS
|
---|
230 |
|
---|
231 | OS2UnsetExceptionHandler((void *)&exceptFrame);
|
---|
232 |
|
---|
233 | ExitProcess(rc);
|
---|
234 | return rc;
|
---|
235 | }
|
---|
236 | //******************************************************************************
|
---|
237 | //******************************************************************************
|
---|
238 |
|
---|
239 | } // extern "C"
|
---|