VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/win/nocrt-startup-exe-win.cpp@ 96588

Last change on this file since 96588 was 96588, checked in by vboxsync, 3 years ago

IPRT/nocrt: Added workaround for NT3.1 not knowing about Misc.VirtualSize and not clearing uninitialized data at the end of sections. bugref:10261

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.7 KB
Line 
1/* $Id: nocrt-startup-exe-win.cpp 96588 2022-09-03 02:40:14Z 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-2022 Oracle and/or its affiliates.
10 *
11 * This file is part of VirtualBox base platform packages, as
12 * available from https://www.215389.xyz.
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*********************************************************************************************************************************/
59extern int main(int argc, char **argv, char **envp); /* in program */
60#ifndef IPRT_NO_CRT
61extern DECLHIDDEN(void) InitStdHandles(PRTL_USER_PROCESS_PARAMETERS pParams); /* nocrt-streams-win.cpp */ /** @todo put in header */
62#endif
63
64
65static 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 NtTerminateProcess(NtCurrentProcess(), rcExit);
85}
86
87
88DECLASM(void) CustomMainEntrypoint(PPEB pPeb)
89{
90 /*
91 * Initialize stuff.
92 */
93#ifdef IPRT_NO_CRT
94# ifdef RT_ARCH_X86
95 rtVccWinInitBssOnNt3(pPeb->ImageBaseAddress);
96# endif
97 rtVccInitSecurityCookie();
98#else
99 InitStdHandles(pPeb->ProcessParameters);
100#endif
101 rtVccWinInitProcExecPath();
102
103 RTEXITCODE rcExit;
104#ifdef IPRT_NO_CRT
105 AssertCompile(sizeof(rcExit) == sizeof(int));
106 rcExit = (RTEXITCODE)rtVccInitializersRunInit();
107 if (rcExit == RTEXITCODE_SUCCESS)
108#endif
109 {
110 /*
111 * Get and convert the command line to argc/argv format.
112 */
113 rcExit = RTEXITCODE_INIT;
114 UNICODE_STRING const *pCmdLine = pPeb->ProcessParameters ? &pPeb->ProcessParameters->CommandLine : NULL;
115 if (pCmdLine)
116 {
117 char *pszCmdLine = NULL;
118 int rc = RTUtf16ToUtf8Ex(pCmdLine->Buffer, pCmdLine->Length / sizeof(WCHAR), &pszCmdLine, 0, NULL);
119 if (RT_SUCCESS(rc))
120 {
121 char **papszArgv;
122 int cArgs = 0;
123 rc = RTGetOptArgvFromString(&papszArgv, &cArgs, pszCmdLine,
124 RTGETOPTARGV_CNV_MODIFY_INPUT | RTGETOPTARGV_CNV_QUOTE_MS_CRT, NULL);
125 if (RT_SUCCESS(rc))
126 {
127 /*
128 * Call the main function.
129 */
130 AssertCompile(sizeof(rcExit) == sizeof(int));
131 rcExit = (RTEXITCODE)main(cArgs, papszArgv, NULL /*envp*/);
132 }
133 else
134#ifdef IPRT_NOCRT_WITHOUT_FATAL_WRITE
135 RTMsgError("Error parsing command line: %Rrc\n", rc);
136#else
137 rtNoCrtFatalMsgWithRc(RT_STR_TUPLE("Error parsing command line: "), rc);
138#endif
139 }
140 else
141#ifdef IPRT_NOCRT_WITHOUT_FATAL_WRITE
142 RTMsgError("Failed to convert command line to UTF-8: %Rrc\n", rc);
143#else
144 rtNoCrtFatalMsgWithRc(RT_STR_TUPLE("Failed to convert command line to UTF-8: "), rc);
145#endif
146 }
147 else
148#ifdef IPRT_NOCRT_WITHOUT_FATAL_WRITE
149 RTMsgError("No command line\n");
150#else
151 rtNoCrtFatalMsg(RT_STR_TUPLE("No command line\r\n"));
152#endif
153 rtTerminateProcess(rcExit, true /*fDoAtExit*/);
154 }
155#ifdef IPRT_NO_CRT
156 else
157 {
158# ifdef IPRT_NOCRT_WITHOUT_FATAL_WRITE
159 RTMsgError("A C static initializor failed (%d)\n", rcExit);
160# else
161 rtNoCrtFatalWriteBegin(RT_STR_TUPLE("A C static initializor failed ("));
162 rtNoCrtFatalWriteWinRc(rcExit);
163 rtNoCrtFatalWriteEnd(RT_STR_TUPLE("\r\n"));
164# endif
165 rtTerminateProcess(rcExit, false /*fDoAtExit*/);
166 }
167#endif
168}
169
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette