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