VirtualBox

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

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

IPRT/nocrt: Call C & C++ initializers and terminators. TLS directory (not used by VBoxControl). bugref:10261

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.3 KB
Line 
1/* $Id: initializers-c-cpp-vcc.cpp 95834 2022-07-26 14:46:59Z vboxsync $ */
2/** @file
3 * IPRT - Visual C++ Compiler - C & C++ Initializers and Terminators.
4 */
5
6/*
7 * Copyright (C) 2022 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.215389.xyz. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27
28/*********************************************************************************************************************************
29* Header Files *
30*********************************************************************************************************************************/
31#define IPRT_COMPILER_VCC_WITH_C_INIT_TERM_SECTIONS
32#define IPRT_COMPILER_VCC_WITH_CPP_INIT_SECTIONS
33#include "internal/compiler-vcc.h"
34
35
36/*********************************************************************************************************************************
37* Structures and Typedefs *
38*********************************************************************************************************************************/
39typedef void (__cdecl *PFNVCINITTERM)(void);
40typedef int (__cdecl *PFNVCINITTERMRET)(void);
41
42
43/*********************************************************************************************************************************
44* Global Variables *
45*********************************************************************************************************************************/
46/** @name Initializer arrays.
47 *
48 * The important thing here are the section names, the linker wi.
49 *
50 * @{ */
51/** Start of the C initializer array. */
52__declspec(allocate(".CRT$XIA")) PFNVCINITTERMRET g_apfnRTVccInitializers_C_Start = NULL;
53/** End of the C initializer array. */
54__declspec(allocate(".CRT$XIZ")) PFNVCINITTERMRET g_apfnRTVccInitializers_C_End = NULL;
55
56/** Start of the C pre-terminator array. */
57__declspec(allocate(".CRT$XPA")) PFNVCINITTERM g_apfnRTVccEarlyTerminators_C_Start = NULL;
58/** End of the C pre-terminator array. */
59__declspec(allocate(".CRT$XPZ")) PFNVCINITTERM g_apfnRTVccEarlyTerminators_C_End = NULL;
60
61/** Start of the C terminator array. */
62__declspec(allocate(".CRT$XTA")) PFNVCINITTERM g_apfnRTVccTerminators_C_Start = NULL;
63/** End of the C terminator array. */
64__declspec(allocate(".CRT$XTZ")) PFNVCINITTERM g_apfnRTVccTerminators_C_End = NULL;
65
66/** Start of the C++ initializer array. */
67__declspec(allocate(".CRT$XCA")) PFNVCINITTERM g_apfnRTVccInitializers_Cpp_Start = NULL;
68/** End of the C++ initializer array. */
69__declspec(allocate(".CRT$XCZ")) PFNVCINITTERM g_apfnRTVccInitializers_Cpp_End = NULL;
70
71
72/* Tell the linker to merge the .CRT* sections into .rdata */
73#pragma comment(linker, "/merge:.CRT=.rdata ")
74/** @} */
75
76
77/**
78 * Runs the C and C++ initializers.
79 *
80 * @returns 0 on success, non-zero return from C initalizer on failure.
81 */
82int rtVccInitializersRunInit(void)
83{
84 /*
85 * Run the C initializers first.
86 */
87 for (PFNVCINITTERMRET *ppfn = &g_apfnRTVccInitializers_C_Start;
88 (uintptr_t)ppfn < (uintptr_t)&g_apfnRTVccInitializers_C_End;
89 ppfn++)
90 {
91 PFNVCINITTERMRET const pfn = *ppfn;
92 if (pfn)
93 {
94 int const rc = pfn();
95 if (RT_LIKELY(rc == 0))
96 { /* likely */ }
97 else
98 return rc;
99 }
100 }
101
102 /*
103 * Run the C++ initializers.
104 */
105 for (PFNVCINITTERM *ppfn = &g_apfnRTVccInitializers_Cpp_Start;
106 (uintptr_t)ppfn < (uintptr_t)&g_apfnRTVccInitializers_Cpp_End;
107 ppfn++)
108 {
109 PFNVCINITTERM const pfn = *ppfn;
110 if (pfn)
111 pfn();
112 }
113
114 return 0;
115}
116
117
118/**
119 * Runs the C terminator callbacks.
120 */
121void rtVccInitializersRunTerm(void)
122{
123 /*
124 * First the early terminators.
125 */
126 for (PFNVCINITTERM *ppfn = &g_apfnRTVccEarlyTerminators_C_Start;
127 (uintptr_t)ppfn < (uintptr_t)&g_apfnRTVccEarlyTerminators_C_End;
128 ppfn++)
129 {
130 PFNVCINITTERM const pfn = *ppfn;
131 if (pfn)
132 pfn();
133 }
134
135 /*
136 * Then the real terminator list.
137 */
138 for (PFNVCINITTERM *ppfn = &g_apfnRTVccTerminators_C_Start;
139 (uintptr_t)ppfn < (uintptr_t)&g_apfnRTVccTerminators_C_End;
140 ppfn++)
141 {
142 PFNVCINITTERM const pfn = *ppfn;
143 if (pfn)
144 pfn();
145 }
146
147}
148
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