source: branches/gcc-kmk/src/kernel32/winexepeldr.cpp@ 21737

Last change on this file since 21737 was 21737, checked in by dmik, 14 years ago

Common compiler warnings and errors.

File size: 5.8 KB
Line 
1/* $Id: winexepeldr.cpp,v 1.24 2004-01-15 10:39:10 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#ifndef __GNUC__
22#include <iostream.h>
23#include <fstream.h>
24#endif
25#include <misc.h>
26#include <win32type.h>
27#include <win32api.h>
28#include "winexepeldr.h"
29#include <wprocess.h>
30#include <pefile.h>
31
32#include "conwin.h" // Windows Header for console only
33#include "console.h"
34
35#include "exceptions.h"
36#include "exceptutil.h"
37
38#include "oslibmisc.h"
39
40#define DBG_LOCALLOG DBG_winexepeldr
41#include "dbglocal.h"
42
43
44#ifdef PROFILE
45#include <perfview.h>
46#include <profiler.h>
47#endif /* PROFILE */
48
49
50extern char szErrorModule[];
51
52BOOL 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//******************************************************************************
61DWORD WIN32API CreateWin32PeLdrExe(char *szFileName, char *szCmdLine,
62 char *peoptions,
63 ULONG reservedMem, ULONG ulPEOffset,
64 BOOL fConsoleApp, BOOL fVioConsole,
65 char *pszErrorModule, ULONG cbErrorModule)
66{
67 APIRET rc;
68 PPIB ppib;
69 PTIB ptib;
70 WINEXCEPTION_FRAME exceptFrame;
71 Win32PeLdrExe *WinExe;
72 char *szFullCmdLine;
73
74 fPeLoader = TRUE;
75
76 WinExe = new Win32PeLdrExe(szFileName, fConsoleApp);
77
78 rc = DosGetInfoBlocks(&ptib, &ppib);
79 if(rc) {
80 delete WinExe;
81 return LDRERROR_INTERNAL;
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 //Init console before loading executable as dlls might want to print
123 //something on the console while being loaded
124 if(WinExe->isConsoleApp())
125 {
126 dprintf(("Console application!\n"));
127
128 rc = iConsoleInit(fVioConsole); /* initialize console subsystem */
129 if (rc != NO_ERROR) /* check for errors */
130 dprintf(("KERNEL32:Win32Image:Init ConsoleInit failed with %u.\n", rc));
131 }
132
133 OS2SetExceptionHandler(&exceptFrame);
134 rc = WinExe->init(reservedMem, ulPEOffset);
135 if(rc != LDRERROR_SUCCESS)
136 {
137 if(szErrorModule[0] != 0) {
138 strncpy(pszErrorModule, szErrorModule, cbErrorModule-1);
139 pszErrorModule[cbErrorModule-1] = 0;
140 }
141 delete WinExe;
142 OS2UnsetExceptionHandler(&exceptFrame);
143 return rc;
144 }
145 OS2UnsetExceptionHandler(&exceptFrame);
146
147#ifdef PROFILE
148 // Note: after this point, we might start collecting performance
149 // information about the called functions.
150 PerfView_Initialize();
151 ProfilerInitialize();
152 ProfilerEnable(TRUE);
153#endif /* PROFILE */
154
155
156 WinExe->start();
157
158 delete WinExe;
159
160 return LDRERROR_SUCCESS;
161}
162//******************************************************************************
163//******************************************************************************
164Win32PeLdrExe::Win32PeLdrExe(char *szFileName, BOOL fConsoleApp) :
165 Win32ImageBase(-1),
166 Win32ExeBase(-1),
167 Win32PeLdrImage(szFileName, TRUE)
168{
169 dprintf(("Win32PeLdrExe ctor: %s", szFileName));
170 this->fConsoleApp = fConsoleApp;
171
172 //SvL: set temporary full path here as console init needs it
173 setFullPath(szFileName);
174}
175//******************************************************************************
176//******************************************************************************
177Win32PeLdrExe::~Win32PeLdrExe()
178{
179 fExitProcess = TRUE;
180}
181//******************************************************************************
182//Default stack size is 1MB; the PE loader reads it from the executable header
183//******************************************************************************
184ULONG Win32PeLdrExe::getDefaultStackSize()
185{
186 //Note: MUST use 128kb as a minimum. Or else the workarounds for out of
187 // stack space in 16 bits code (thread entrypoint) might fail.
188 return (poh->SizeOfStackReserve > 128*1024) ? poh->SizeOfStackReserve : 128*1024;
189}
190//******************************************************************************
191//******************************************************************************
Note: See TracBrowser for help on using the repository browser.