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