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