ReactOS  0.4.15-dev-3165-gdf6fff7
crtexe.c
Go to the documentation of this file.
1 
7 #define SPECIAL_CRTEXE
8 
9 #include <oscalls.h>
10 #include <internal.h>
11 #include <process.h>
12 #include <signal.h>
13 #include <math.h>
14 #include <stdlib.h>
15 #include <tchar.h>
16 #include <sect_attribs.h>
17 #include <locale.h>
18 #ifdef _MBCS
19 #include <mbstring.h>
20 #endif
21 
22 /* Special handling for ARM & ARM64, __winitenv & __initenv aren't present there. */
23 #if !defined(__arm__) && !defined(__aarch64__)
24 _CRTIMP extern wchar_t** __winitenv;
25 _CRTIMP extern char** __initenv;
26 #endif
27 
28 /* Hack, for bug in ld. Will be removed soon. */
29 #if defined(__GNUC__)
30 #define __ImageBase __MINGW_LSYMBOL(_image_base__)
31 #endif
32 
33 /* This symbol is defined by ld. */
35 
36 extern void __cdecl _fpreset (void);
37 #define SPACECHAR _T(' ')
38 #define DQUOTECHAR _T('\"')
39 
40 extern int _dowildcard;
41 
42 extern _CRTIMP void __cdecl _initterm(_PVFV *, _PVFV *);
43 
44 static int __cdecl check_managed_app (void);
45 
46 extern _CRTALLOC(".CRT$XIA") _PIFV __xi_a[];
47 extern _CRTALLOC(".CRT$XIZ") _PIFV __xi_z[];
48 extern _CRTALLOC(".CRT$XCA") _PVFV __xc_a[];
49 extern _CRTALLOC(".CRT$XCZ") _PVFV __xc_z[];
50 
51 /* TLS initialization hook. */
53 
54 extern _PVFV *__onexitbegin;
55 extern _PVFV *__onexitend;
56 
57 extern int mingw_app_type;
58 
62 
63 static int argc;
64 extern void __main(void);
65 #ifdef WPRFLAG
66 static wchar_t **argv;
67 static wchar_t **envp;
68 #else
69 static char **argv;
70 static char **envp;
71 #endif
72 
73 static int argret;
74 static int mainret=0;
75 static int managedapp;
76 static int has_cctor = 0;
77 static _startupinfo startinfo;
79 
80 extern void _pei386_runtime_relocator (void);
81 long CALLBACK _gnu_exception_handler (EXCEPTION_POINTERS * exception_data);
82 #ifdef WPRFLAG
83 static void duplicate_ppstrings (int ac, wchar_t ***av);
84 #else
85 static void duplicate_ppstrings (int ac, char ***av);
86 #endif
87 
88 static int __cdecl pre_c_init (void);
89 static void __cdecl pre_cpp_init (void);
91 _CRTALLOC(".CRT$XIAA") _PIFV mingw_pcinit = pre_c_init;
93 
95 
96 #ifdef __GNUC__
97 extern void __do_global_dtors(void);
98 #endif
99 
100 static int __cdecl
101 pre_c_init (void)
102 {
103  managedapp = check_managed_app ();
104  if (mingw_app_type)
106  else
108  __onexitbegin = __onexitend = (_PVFV *)(-1);
109 
110 #ifdef WPRFLAG
111  _wsetargv();
112 #else
113  _setargv();
114 #endif
116  {
118  }
119 #ifndef __clang__ /* FIXME: CORE-14042 */
120  if (__globallocalestatus == -1)
121  {
122  }
123 #endif
124  return 0;
125 }
126 
127 static void __cdecl
129 {
130  startinfo.newmode = _newmode;
131 
132 #ifdef WPRFLAG
133  argret = __wgetmainargs(&argc,&argv,&envp,_dowildcard,&startinfo);
134 #else
135  argret = __getmainargs(&argc,&argv,&envp,_dowildcard,&startinfo);
136 #endif
137 }
138 
139 static int __cdecl __tmainCRTStartup (void);
140 
141 int __cdecl WinMainCRTStartup (void);
142 
144 {
145  int ret = 255;
146 #ifdef __SEH__
147  asm ("\t.l_startw:\n"
148  "\t.seh_handler __C_specific_handler, @except\n"
149  "\t.seh_handlerdata\n"
150  "\t.long 1\n"
151  "\t.rva .l_startw, .l_endw, _gnu_exception_handler ,.l_endw\n"
152  "\t.text"
153  );
154 #endif
155  mingw_app_type = 1;
157  ret = __tmainCRTStartup ();
158 #ifdef __SEH__
159  asm ("\tnop\n"
160  "\t.l_endw: nop\n");
161 #endif
162  return ret;
163 }
164 
165 int __cdecl mainCRTStartup (void);
166 BOOL crt_process_init(void);
167 
168 #ifdef _WIN64
169 int __mingw_init_ehandler (void);
170 #endif
171 
173 {
174  int ret = 255;
175 #ifndef _DLL
176  if (!crt_process_init())
177  {
178  return -1;
179  }
180 #endif
181 #ifdef __SEH__
182  asm ("\t.l_start:\n"
183  "\t.seh_handler __C_specific_handler, @except\n"
184  "\t.seh_handlerdata\n"
185  "\t.long 1\n"
186  "\t.rva .l_start, .l_end, _gnu_exception_handler ,.l_end\n"
187  "\t.text"
188  );
189 #endif
190  mingw_app_type = 0;
192  ret = __tmainCRTStartup ();
193 #ifdef __SEH__
194  asm ("\tnop\n"
195  "\t.l_end: nop\n");
196 #endif
197  return ret;
198 }
199 
200 static
202 int __cdecl
203 __tmainCRTStartup (void)
204 {
205  _TCHAR *lpszCommandLine = NULL;
206  STARTUPINFO StartupInfo;
207  WINBOOL inDoubleQuote = FALSE;
208  memset (&StartupInfo, 0, sizeof (STARTUPINFO));
209 
210 #ifndef _WIN64
211  /* We need to make sure that this function is build with frame-pointer
212  and that we align the stack to 16 bytes for the sake of SSE ops in main
213  or in functions inlined into main. */
214  lpszCommandLine = (_TCHAR *) alloca (32);
215  memset (lpszCommandLine, 0xcc, 32);
216 #ifdef __GNUC__
217  asm __volatile__ ("andl $-16, %%esp" : : : "%esp");
218 #endif
219 #endif
220 
221  if (mingw_app_type)
222  GetStartupInfo (&StartupInfo);
223  {
224  void *lock_free = NULL;
225  void *fiberid = ((PNT_TIB)NtCurrentTeb())->StackBase;
226  int nested = FALSE;
227  while((lock_free = InterlockedCompareExchangePointer ((volatile PVOID *) &__native_startup_lock,
228  fiberid, 0)) != 0)
229  {
230  if (lock_free == fiberid)
231  {
232  nested = TRUE;
233  break;
234  }
235  Sleep(1000);
236  }
238  {
239  _amsg_exit (31);
240  }
242  {
244  _initterm ((_PVFV *)(void *)__xi_a, (_PVFV *)(void *) __xi_z);
245  }
246  else
247  has_cctor = 1;
248 
250  {
251  _initterm (__xc_a, __xc_z);
253  }
255  if (! nested)
257 
260 
263 #ifdef _WIN64
265 #endif
267 
268  _fpreset ();
269 
270  if (mingw_app_type)
271  {
272 #ifdef WPRFLAG
273  lpszCommandLine = (_TCHAR *) _wcmdln;
274 #else
275  lpszCommandLine = (char *) _acmdln;
276 #endif
277  while (*lpszCommandLine > SPACECHAR || (*lpszCommandLine && inDoubleQuote))
278  {
279  if (*lpszCommandLine == DQUOTECHAR)
280  inDoubleQuote = !inDoubleQuote;
281 #ifdef _MBCS
282  if (_ismbblead (*lpszCommandLine))
283  {
284  if (lpszCommandLine) /* FIXME: Why this check? Should I check for *lpszCommandLine != 0 too? */
285  lpszCommandLine++;
286  }
287 #endif
288  ++lpszCommandLine;
289  }
290  while (*lpszCommandLine && (*lpszCommandLine <= SPACECHAR))
291  lpszCommandLine++;
292 
294  __mingw_winmain_lpCmdLine = lpszCommandLine;
296  StartupInfo.wShowWindow : SW_SHOWDEFAULT;
297  }
299  __main ();
300 #ifdef WPRFLAG
301 #if !defined(__arm__) && !defined(__aarch64__)
302  __winitenv = envp;
303 #endif
304  /* C++ initialization.
305  gcc inserts this call automatically for a function called main, but not for wmain. */
306  mainret = wmain (argc, argv, envp);
307 #else
308 #if !defined(__arm__) && !defined(__aarch64__)
309  __initenv = envp;
310 #endif
311  mainret = main (argc, argv, envp);
312 #endif
313 
314 #ifdef __GNUC__
316 #endif
317 
318  if (!managedapp)
319  exit (mainret);
320 
321  if (has_cctor == 0)
322  _cexit ();
323  }
324  return mainret;
325 }
326 
327 extern int mingw_initltsdrot_force;
328 extern int mingw_initltsdyn_force;
329 extern int mingw_initltssuo_force;
330 extern int mingw_initcharmax;
331 
332 static int __cdecl
334 {
335  PIMAGE_DOS_HEADER pDOSHeader;
336  PIMAGE_NT_HEADERS pPEHeader;
337  PIMAGE_OPTIONAL_HEADER32 pNTHeader32;
338  PIMAGE_OPTIONAL_HEADER64 pNTHeader64;
339 
340  /* Force to be linked. */
345 
346  pDOSHeader = (PIMAGE_DOS_HEADER) &__ImageBase;
347  if (pDOSHeader->e_magic != IMAGE_DOS_SIGNATURE)
348  return 0;
349 
350  pPEHeader = (PIMAGE_NT_HEADERS)((char *)pDOSHeader + pDOSHeader->e_lfanew);
351  if (pPEHeader->Signature != IMAGE_NT_SIGNATURE)
352  return 0;
353 
354  pNTHeader32 = (PIMAGE_OPTIONAL_HEADER32) &pPEHeader->OptionalHeader;
355  switch (pNTHeader32->Magic)
356  {
359  return 0;
362  pNTHeader64 = (PIMAGE_OPTIONAL_HEADER64)pNTHeader32;
364  return 0;
366  }
367  return 0;
368 }
369 
370 #ifdef WPRFLAG
371 static size_t wbytelen(const wchar_t *p)
372 {
373  size_t ret = 1;
374  while (*p!=0) {
375  ret++,++p;
376  }
377  return ret*2;
378 }
379 static void duplicate_ppstrings (int ac, wchar_t ***av)
380 {
381  wchar_t **avl;
382  int i;
383  wchar_t **n = (wchar_t **) malloc (sizeof (wchar_t *) * (ac + 1));
384 
385  avl=*av;
386  for (i=0; i < ac; i++)
387  {
388  size_t l = wbytelen (avl[i]);
389  n[i] = (wchar_t *) malloc (l);
390  memcpy (n[i], avl[i], l);
391  }
392  n[i] = NULL;
393  *av = n;
394 }
395 #else
396 static void duplicate_ppstrings (int ac, char ***av)
397 {
398  char **avl;
399  int i;
400  char **n = (char **) malloc (sizeof (char *) * (ac + 1));
401 
402  avl=*av;
403  for (i=0; i < ac; i++)
404  {
405  size_t l = strlen (avl[i]) + 1;
406  n[i] = (char *) malloc (l);
407  memcpy (n[i], avl[i], l);
408  }
409  n[i] = NULL;
410  *av = n;
411 }
412 #endif
413 
414 #ifdef __MINGW_SHOW_INVALID_PARAMETER_EXCEPTION
415 #define __UNUSED_PARAM_1(x) x
416 #else
417 #define __UNUSED_PARAM_1 __UNUSED_PARAM
418 #endif
419 static void __cdecl
421  const wchar_t * __UNUSED_PARAM_1(function),
422  const wchar_t * __UNUSED_PARAM_1(file),
423  unsigned int __UNUSED_PARAM_1(line),
424  uintptr_t __UNUSED_PARAM(pReserved))
425 {
426 #ifdef __MINGW_SHOW_INVALID_PARAMETER_EXCEPTION
427  wprintf(L"Invalid parameter detected in function %s. File: %s Line: %u\n", function, file, line);
428  wprintf(L"Expression: %s\n", expression);
429 #endif
430 }
431 
433 
434 static void __cdecl
436 {
438 
439  fIPH = (void*)GetProcAddress (__mingw_get_msvcrt_handle(), "_set_invalid_parameter_handler");
440  if (fIPH)
442 }
#define __UNUSED_PARAM_1
Definition: crtexe.c:417
int mingw_app_type
Definition: mingw_helpers.c:31
int __globallocalestatus
static int argc
Definition: ServiceArgs.c:12
#define SW_SHOWDEFAULT
Definition: winuser.h:774
int _MINGW_INSTALL_DEBUG_MATHERR
Definition: reactos.c:2
void(__cdecl * _PVFV)(void)
Definition: internal.h:33
VOID WINAPI DECLSPEC_HOTPATCH Sleep(IN DWORD dwMilliseconds)
Definition: synch.c:790
static void duplicate_ppstrings(int ac, char ***av)
Definition: crtexe.c:396
IMAGE_DATA_DIRECTORY DataDirectory[IMAGE_NUMBEROF_DIRECTORY_ENTRIES]
Definition: ntddk_ex.h:178
void __main(void)
Definition: gccmain.c:51
void _pei386_runtime_relocator(void)
Definition: mscmain.c:23
int wmain(int argc, WCHAR **argv)
Definition: at.c:753
#define __cdecl
Definition: accygwin.h:79
void __cdecl _fpreset(void)
Definition: _fpreset.c:10
#define SPACECHAR
Definition: crtexe.c:37
int mingw_initltssuo_force
ACPI_SIZE strlen(const char *String)
Definition: utclib.c:269
#define TRUE
Definition: types.h:120
#define DLL_THREAD_ATTACH
Definition: compat.h:132
#define _CRTIMP
Definition: crtdefs.h:72
int main(int argc, char *argv[])
Definition: atactl.cpp:1685
#define IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR
Definition: ntimage.h:489
#define __UNUSED_PARAM(x)
Definition: _mingw.h:74
#define CALLBACK
Definition: compat.h:35
BOOL crt_process_init(void)
Definition: crt_init.c:20
GLdouble n
Definition: glext.h:7729
#define IMAGE_NT_OPTIONAL_HDR32_MAGIC
Definition: ntimage.h:376
_PVFV * __onexitbegin
Definition: atonexit.c:23
volatile __enative_startup_state __native_startup_state
Definition: natstart.c:13
_CRTIMP void __cdecl _initterm(_PVFV *, _PVFV *)
Definition: initterm.c:10
DWORD dwFlags
Definition: winbase.h:836
long CALLBACK _gnu_exception_handler(EXCEPTION_POINTERS *exception_data)
Definition: crt_handler.c:196
static void __cdecl __mingw_prepare_except_for_msvcr80_and_higher(void)
Definition: crtexe.c:435
_CRTIMP char ** __initenv
Definition: environ.c:24
#define wprintf(...)
Definition: whoami.c:18
static int __cdecl __tmainCRTStartup(void)
#define argv
Definition: mplay32.c:18
_CRTALLOC(".CRT$XIA")
Definition: crtexe.c:46
int __cdecl mainCRTStartup(void)
Definition: crtexe.c:172
void __cdecl __security_init_cookie(void)
Definition: gs_support.c:55
#define alloca
Definition: malloc.h:357
void __cdecl _amsg_exit(int errnum)
Definition: amsg.c:44
#define IMAGE_DOS_SIGNATURE
Definition: pedump.c:89
#define noinline
Definition: types.h:60
VOID(NTAPI * PIMAGE_TLS_CALLBACK)(PVOID DllHandle, ULONG Reason, PVOID Reserved)
Definition: ntimage.h:531
_CRTIMP wchar_t ** __winitenv
Definition: environ.c:25
DWORD __mingw_winmain_nShowCmd
#define FALSE
Definition: types.h:117
unsigned int BOOL
Definition: ntddk_ex.h:94
LPTOP_LEVEL_EXCEPTION_FILTER WINAPI DECLSPEC_HOTPATCH SetUnhandledExceptionFilter(IN LPTOP_LEVEL_EXCEPTION_FILTER lpTopLevelExceptionFilter)
Definition: except.c:788
__analysis_noreturn _CRTIMP void __cdecl _cexit(void)
Definition: _exit.c:7
LPTOP_LEVEL_EXCEPTION_FILTER __mingw_oldexcpt_handler
Definition: crt_handler.c:188
#define InterlockedCompareExchangePointer
Definition: interlocked.h:129
int mingw_initcharmax
Definition: charmax.c:12
unsigned int uintptr_t
Definition: crtdefs.h:321
HANDLE __mingw_get_msvcrt_handle(void)
Definition: pesect.c:236
_PVFV * __onexitend
Definition: atonexit.c:24
int __CRTDECL _setargv(void)
Definition: dllargv.c:18
struct _IMAGE_DOS_HEADER * PIMAGE_DOS_HEADER
Definition: parser.c:48
#define _ASSERTE(expr)
Definition: crtdbg.h:111
switch(r->id)
Definition: btrfs.c:2980
#define STARTF_USESHOWWINDOW
Definition: winbase.h:488
char _TCHAR
Definition: tchar.h:1392
r l[0]
Definition: byte_order.h:167
#define IMAGE_NT_SIGNATURE
Definition: pedump.c:93
_CRTIMP void __cdecl __set_app_type(int)
Definition: environ.c:360
#define InterlockedExchangePointer(Target, Value)
Definition: dshow.h:45
void __do_global_dtors(void)
Definition: gccmain.c:20
int32_t WINBOOL
Definition: typedefs.h:58
void __getmainargs(int *argc, char ***argv, char ***envp, int expand_wildcards, int *new_mode)
Definition: getargs.c:182
static void __cdecl __mingw_invalidParameterHandler(const wchar_t *__UNUSED_PARAM_1(expression), const wchar_t *__UNUSED_PARAM_1(function), const wchar_t *__UNUSED_PARAM_1(file), unsigned int __UNUSED_PARAM_1(line), uintptr_t __UNUSED_PARAM(pReserved))
Definition: crtexe.c:420
struct _NT_TIB * PNT_TIB
unsigned long DWORD
Definition: ntddk_ex.h:95
#define mingw_pcppinit
Definition: wcrtexe.c:13
HINSTANCE __mingw_winmain_hInstance
int _newmode
Definition: _newmode.c:7
#define IMAGE_NT_OPTIONAL_HDR64_MAGIC
Definition: ntimage.h:377
IMAGE_DOS_HEADER __ImageBase
Definition: mstscax.cpp:17
static int __cdecl check_managed_app(void)
Definition: crtexe.c:333
HANDLE HINSTANCE
Definition: typedefs.h:77
static __declspec(noinline)
Definition: crtexe.c:201
int ret
static const WCHAR L[]
Definition: oid.c:1250
#define VOID
Definition: acefi.h:82
struct _IMAGE_OPTIONAL_HEADER * PIMAGE_OPTIONAL_HEADER32
#define memcpy(s1, s2, n)
Definition: mkisofs.h:878
wchar_t * _wcmdln
Definition: environ.c:19
void CDECL __wgetmainargs(int *argc, WCHAR ***wargv, WCHAR ***wenvp, int expand_wildcards, int *new_mode)
PTOP_LEVEL_EXCEPTION_FILTER LPTOP_LEVEL_EXCEPTION_FILTER
Definition: winbase.h:1435
IMAGE_OPTIONAL_HEADER32 OptionalHeader
Definition: ntddk_ex.h:184
#define mingw_pcinit
Definition: wcrtexe.c:12
int mingw_initltsdyn_force
const PIMAGE_TLS_CALLBACK __dyn_tls_init_callback
Definition: tlssup.c:107
LPSTR __mingw_winmain_lpCmdLine
int __CRTDECL _matherr(_Inout_ struct _exception *exception)
FORCEINLINE struct _TEB * NtCurrentTeb(VOID)
Definition: psfuncs.h:420
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
int(__cdecl * _PIFV)(void)
Definition: internal.h:34
#define NULL
Definition: types.h:112
WORD wShowWindow
Definition: winbase.h:837
#define _CONSOLE_APP
Definition: internal.h:145
IMAGE_DATA_DIRECTORY DataDirectory[IMAGE_NUMBEROF_DIRECTORY_ENTRIES]
Definition: ntimage.h:370
int __cdecl _ismbblead(unsigned int)
Definition: ismblead.c:20
int __CRTDECL _wsetargv(void)
char * _acmdln
Definition: environ.c:18
int mingw_initltsdrot_force
int _dowildcard
Definition: wildcard.c:32
struct _IMAGE_OPTIONAL_HEADER64 * PIMAGE_OPTIONAL_HEADER64
#define const
Definition: zconf.h:230
#define _GUI_APP
Definition: internal.h:146
#define DQUOTECHAR
Definition: crtexe.c:38
void(__cdecl * _invalid_parameter_handler)(const wchar_t *, const wchar_t *, const wchar_t *, unsigned int, uintptr_t)
Definition: stdlib.h:125
#define GetProcAddress(x, y)
Definition: compat.h:612
#define malloc
Definition: debug_ros.c:4
#define GetStartupInfo
Definition: winbase.h:3697
void exit(int exitcode)
Definition: _exit.c:33
GLfloat GLfloat p
Definition: glext.h:8902
#define memset(x, y, z)
Definition: compat.h:39
int __mingw_init_ehandler(void)
Definition: mscmain.c:27
int __cdecl WinMainCRTStartup(void)
Definition: crtexe.c:143
volatile void * __native_startup_lock
Definition: natstart.c:14
static void __cdecl pre_cpp_init(void)
Definition: crtexe.c:128
void __cdecl __setusermatherr(_UserMathErrorFunctionPointer func)
Definition: usermatherr.c:19
PIMAGE_NT_HEADERS32 PIMAGE_NT_HEADERS
Definition: ntddk_ex.h:187
Definition: fci.c:126