1 | /* $Id: nocrt-startup-exe-win.cpp 106061 2024-09-16 14:03:52Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - No-CRT - Windows EXE startup code.
|
---|
4 | *
|
---|
5 | * @note Does not run static constructors and destructors!
|
---|
6 | */
|
---|
7 |
|
---|
8 | /*
|
---|
9 | * Copyright (C) 2006-2024 Oracle and/or its affiliates.
|
---|
10 | *
|
---|
11 | * This file is part of VirtualBox base platform packages, as
|
---|
12 | * available from https://www.virtualbox.org.
|
---|
13 | *
|
---|
14 | * This program is free software; you can redistribute it and/or
|
---|
15 | * modify it under the terms of the GNU General Public License
|
---|
16 | * as published by the Free Software Foundation, in version 3 of the
|
---|
17 | * License.
|
---|
18 | *
|
---|
19 | * This program is distributed in the hope that it will be useful, but
|
---|
20 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
22 | * General Public License for more details.
|
---|
23 | *
|
---|
24 | * You should have received a copy of the GNU General Public License
|
---|
25 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
26 | *
|
---|
27 | * The contents of this file may alternatively be used under the terms
|
---|
28 | * of the Common Development and Distribution License Version 1.0
|
---|
29 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
30 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
31 | * CDDL are applicable instead of those of the GPL.
|
---|
32 | *
|
---|
33 | * You may elect to license modified versions of this file under the
|
---|
34 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
35 | *
|
---|
36 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
37 | */
|
---|
38 |
|
---|
39 |
|
---|
40 | /*********************************************************************************************************************************
|
---|
41 | * Header Files *
|
---|
42 | *********************************************************************************************************************************/
|
---|
43 | #include "internal/nocrt.h"
|
---|
44 | #include "internal/process.h"
|
---|
45 |
|
---|
46 | #include <iprt/nt/nt-and-windows.h>
|
---|
47 | #include <iprt/getopt.h>
|
---|
48 | #include <iprt/message.h>
|
---|
49 | #include <iprt/path.h>
|
---|
50 | #include <iprt/string.h>
|
---|
51 | #include <iprt/utf16.h>
|
---|
52 |
|
---|
53 | #include "internal/compiler-vcc.h"
|
---|
54 |
|
---|
55 |
|
---|
56 | /*********************************************************************************************************************************
|
---|
57 | * External Symbols *
|
---|
58 | *********************************************************************************************************************************/
|
---|
59 | extern int main(int argc, char **argv, char **envp); /* in program */
|
---|
60 | #ifndef IPRT_NO_CRT
|
---|
61 | extern DECLHIDDEN(void) InitStdHandles(PRTL_USER_PROCESS_PARAMETERS pParams); /* nocrt-streams-win.cpp */ /** @todo put in header */
|
---|
62 | #endif
|
---|
63 |
|
---|
64 |
|
---|
65 | static int rtTerminateProcess(int32_t rcExit, bool fDoAtExit)
|
---|
66 | {
|
---|
67 | #ifdef IPRT_NO_CRT
|
---|
68 | /*
|
---|
69 | * Run atexit callback in reverse order.
|
---|
70 | */
|
---|
71 | if (fDoAtExit)
|
---|
72 | {
|
---|
73 | rtVccTermRunAtExit();
|
---|
74 | rtVccInitializersRunTerm();
|
---|
75 | }
|
---|
76 | #else
|
---|
77 | RT_NOREF(fDoAtExit);
|
---|
78 | #endif
|
---|
79 |
|
---|
80 | /*
|
---|
81 | * Terminate.
|
---|
82 | */
|
---|
83 | for (;;)
|
---|
84 | #if 1 /* Using NtTerminateProcess triggers heuristics in some annoying AV scanner. */
|
---|
85 | TerminateProcess(NtCurrentProcess(), rcExit);
|
---|
86 | #else
|
---|
87 | NtTerminateProcess(NtCurrentProcess(), rcExit);
|
---|
88 | #endif
|
---|
89 | }
|
---|
90 |
|
---|
91 |
|
---|
92 | DECLASM(void) CustomMainEntrypoint(void)
|
---|
93 | {
|
---|
94 | /* Looks like might have gotten the PPEB as parameter here before NT4,
|
---|
95 | however, there the EXE entry function clearly takes no parameters.
|
---|
96 | So, we have to retrieve the PEB our selves here. */
|
---|
97 | PPEB_COMMON const pPeb = RTNtCurrentPeb();
|
---|
98 |
|
---|
99 | /*
|
---|
100 | * Initialize stuff.
|
---|
101 | */
|
---|
102 | #ifdef IPRT_NO_CRT
|
---|
103 | # ifdef RT_ARCH_X86
|
---|
104 | rtVccWinInitBssOnNt3(pPeb->ImageBaseAddress);
|
---|
105 | # endif
|
---|
106 | rtVccInitSecurityCookie();
|
---|
107 | #else
|
---|
108 | InitStdHandles(pPeb->ProcessParameters);
|
---|
109 | #endif
|
---|
110 | rtVccWinInitProcExecPath();
|
---|
111 |
|
---|
112 | RTEXITCODE rcExit;
|
---|
113 | #ifdef IPRT_NO_CRT
|
---|
114 | AssertCompile(sizeof(rcExit) == sizeof(int));
|
---|
115 | rcExit = (RTEXITCODE)rtVccInitializersRunInit();
|
---|
116 | if (rcExit == RTEXITCODE_SUCCESS)
|
---|
117 | #endif
|
---|
118 | {
|
---|
119 | /*
|
---|
120 | * Get and convert the command line to argc/argv format.
|
---|
121 | */
|
---|
122 | rcExit = RTEXITCODE_INIT;
|
---|
123 | UNICODE_STRING const *pCmdLine = pPeb->ProcessParameters ? &pPeb->ProcessParameters->CommandLine : NULL;
|
---|
124 | if (pCmdLine)
|
---|
125 | {
|
---|
126 | char *pszCmdLine = NULL;
|
---|
127 | int rc = RTUtf16ToUtf8Ex(pCmdLine->Buffer, pCmdLine->Length / sizeof(WCHAR), &pszCmdLine, 0, NULL);
|
---|
128 | if (RT_SUCCESS(rc))
|
---|
129 | {
|
---|
130 | char **papszArgv;
|
---|
131 | int cArgs = 0;
|
---|
132 | rc = RTGetOptArgvFromString(&papszArgv, &cArgs, pszCmdLine,
|
---|
133 | RTGETOPTARGV_CNV_MODIFY_INPUT | RTGETOPTARGV_CNV_QUOTE_MS_CRT, NULL);
|
---|
134 | if (RT_SUCCESS(rc))
|
---|
135 | {
|
---|
136 | /*
|
---|
137 | * Call the main function.
|
---|
138 | */
|
---|
139 | AssertCompile(sizeof(rcExit) == sizeof(int));
|
---|
140 | rcExit = (RTEXITCODE)main(cArgs, papszArgv, NULL /*envp*/);
|
---|
141 | }
|
---|
142 | else
|
---|
143 | #ifdef IPRT_NOCRT_WITHOUT_FATAL_WRITE
|
---|
144 | RTMsgError("Error parsing command line: %Rrc\n", rc);
|
---|
145 | #else
|
---|
146 | rtNoCrtFatalMsgWithRc(RT_STR_TUPLE("Error parsing command line: "), rc);
|
---|
147 | #endif
|
---|
148 | }
|
---|
149 | else
|
---|
150 | #ifdef IPRT_NOCRT_WITHOUT_FATAL_WRITE
|
---|
151 | RTMsgError("Failed to convert command line to UTF-8: %Rrc\n", rc);
|
---|
152 | #else
|
---|
153 | rtNoCrtFatalMsgWithRc(RT_STR_TUPLE("Failed to convert command line to UTF-8: "), rc);
|
---|
154 | #endif
|
---|
155 | }
|
---|
156 | else
|
---|
157 | #ifdef IPRT_NOCRT_WITHOUT_FATAL_WRITE
|
---|
158 | RTMsgError("No command line\n");
|
---|
159 | #else
|
---|
160 | rtNoCrtFatalMsg(RT_STR_TUPLE("No command line\r\n"));
|
---|
161 | #endif
|
---|
162 | rtTerminateProcess(rcExit, true /*fDoAtExit*/);
|
---|
163 | }
|
---|
164 | #ifdef IPRT_NO_CRT
|
---|
165 | else
|
---|
166 | {
|
---|
167 | # ifdef IPRT_NOCRT_WITHOUT_FATAL_WRITE
|
---|
168 | RTMsgError("A C static initializor failed (%d)\n", rcExit);
|
---|
169 | # else
|
---|
170 | rtNoCrtFatalWriteBegin(RT_STR_TUPLE("A C static initializor failed ("));
|
---|
171 | rtNoCrtFatalWriteWinRc(rcExit);
|
---|
172 | rtNoCrtFatalWriteEnd(RT_STR_TUPLE("\r\n"));
|
---|
173 | # endif
|
---|
174 | rtTerminateProcess(rcExit, false /*fDoAtExit*/);
|
---|
175 | }
|
---|
176 | #endif
|
---|
177 | }
|
---|
178 |
|
---|