VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/compiler/vcc/initializers-c-cpp-vcc.cpp@ 96589

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

IPRT/nocrt: Split the as much read-only data out of the .rdata segment and put it in a .rodata section when there's a posibility that we're targeting NT 3.x, as these older NT versions cannot deal with a read-only import table. The linker insists on putting the import table in the .rdata section. There are a couple of other structures it generates/manages that goes there too that and seems impossible to move out of it. 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: initializers-c-cpp-vcc.cpp 96589 2022-09-03 02:54:22Z vboxsync $ */
2/** @file
3 * IPRT - Visual C++ Compiler - C & C++ Initializers and Terminators.
4 */
5
6/*
7 * Copyright (C) 2022 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.215389.xyz.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * The contents of this file may alternatively be used under the terms
26 * of the Common Development and Distribution License Version 1.0
27 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
28 * in the VirtualBox distribution, in which case the provisions of the
29 * CDDL are applicable instead of those of the GPL.
30 *
31 * You may elect to license modified versions of this file under the
32 * terms and conditions of either the GPL or the CDDL or both.
33 *
34 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
35 */
36
37
38/*********************************************************************************************************************************
39* Header Files *
40*********************************************************************************************************************************/
41#define IPRT_COMPILER_VCC_WITH_C_INIT_TERM_SECTIONS
42#define IPRT_COMPILER_VCC_WITH_CPP_INIT_SECTIONS
43#include "internal/compiler-vcc.h"
44
45
46/*********************************************************************************************************************************
47* Structures and Typedefs *
48*********************************************************************************************************************************/
49typedef void (__cdecl *PFNVCINITTERM)(void);
50typedef int (__cdecl *PFNVCINITTERMRET)(void);
51
52
53/*********************************************************************************************************************************
54* Global Variables *
55*********************************************************************************************************************************/
56/** @name Initializer arrays.
57 *
58 * The important thing here are the section names, the linker wi.
59 *
60 * @{ */
61/** Start of the C initializer array. */
62__declspec(allocate(".CRT$XIA")) PFNVCINITTERMRET g_apfnRTVccInitializers_C_Start = NULL;
63/** End of the C initializer array. */
64__declspec(allocate(".CRT$XIZ")) PFNVCINITTERMRET g_apfnRTVccInitializers_C_End = NULL;
65
66/** Start of the C pre-terminator array. */
67__declspec(allocate(".CRT$XPA")) PFNVCINITTERM g_apfnRTVccEarlyTerminators_C_Start = NULL;
68/** End of the C pre-terminator array. */
69__declspec(allocate(".CRT$XPZ")) PFNVCINITTERM g_apfnRTVccEarlyTerminators_C_End = NULL;
70
71/** Start of the C terminator array. */
72__declspec(allocate(".CRT$XTA")) PFNVCINITTERM g_apfnRTVccTerminators_C_Start = NULL;
73/** End of the C terminator array. */
74__declspec(allocate(".CRT$XTZ")) PFNVCINITTERM g_apfnRTVccTerminators_C_End = NULL;
75
76/** Start of the C++ initializer array. */
77__declspec(allocate(".CRT$XCA")) PFNVCINITTERM g_apfnRTVccInitializers_Cpp_Start = NULL;
78/** End of the C++ initializer array. */
79__declspec(allocate(".CRT$XCZ")) PFNVCINITTERM g_apfnRTVccInitializers_Cpp_End = NULL;
80
81
82/* Tell the linker to merge the .CRT* sections into .rdata */
83#ifdef IPRT_VCC_USING_RODATA_AS_CONST_SEG
84# pragma comment(linker, "/merge:.CRT=.rodata ")
85#else
86# pragma comment(linker, "/merge:.CRT=.rdata ")
87#endif
88/** @} */
89
90
91/**
92 * Runs the C and C++ initializers.
93 *
94 * @returns 0 on success, non-zero return from C initalizer on failure.
95 */
96int rtVccInitializersRunInit(void)
97{
98 /*
99 * Run the C initializers first.
100 */
101 for (PFNVCINITTERMRET *ppfn = &g_apfnRTVccInitializers_C_Start;
102 (uintptr_t)ppfn < (uintptr_t)&g_apfnRTVccInitializers_C_End;
103 ppfn++)
104 {
105 PFNVCINITTERMRET const pfn = *ppfn;
106 if (pfn)
107 {
108 int const rc = pfn();
109 if (RT_LIKELY(rc == 0))
110 { /* likely */ }
111 else
112 return rc;
113 }
114 }
115
116 /*
117 * Run the C++ initializers.
118 */
119 for (PFNVCINITTERM *ppfn = &g_apfnRTVccInitializers_Cpp_Start;
120 (uintptr_t)ppfn < (uintptr_t)&g_apfnRTVccInitializers_Cpp_End;
121 ppfn++)
122 {
123 PFNVCINITTERM const pfn = *ppfn;
124 if (pfn)
125 pfn();
126 }
127
128 return 0;
129}
130
131
132/**
133 * Runs the C terminator callbacks.
134 */
135void rtVccInitializersRunTerm(void)
136{
137 /*
138 * First the early terminators.
139 */
140 for (PFNVCINITTERM *ppfn = &g_apfnRTVccEarlyTerminators_C_Start;
141 (uintptr_t)ppfn < (uintptr_t)&g_apfnRTVccEarlyTerminators_C_End;
142 ppfn++)
143 {
144 PFNVCINITTERM const pfn = *ppfn;
145 if (pfn)
146 pfn();
147 }
148
149 /*
150 * Then the real terminator list.
151 */
152 for (PFNVCINITTERM *ppfn = &g_apfnRTVccTerminators_C_Start;
153 (uintptr_t)ppfn < (uintptr_t)&g_apfnRTVccTerminators_C_End;
154 ppfn++)
155 {
156 PFNVCINITTERM const pfn = *ppfn;
157 if (pfn)
158 pfn();
159 }
160
161}
162
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