Skip to content

Commit e9d1f02

Browse files
committed
add _initialize rationale and cleanup NativeAOT_StaticInitialization
1 parent 7dace36 commit e9d1f02

File tree

5 files changed

+18
-27
lines changed

5 files changed

+18
-27
lines changed

src/coreclr/nativeaot/Bootstrap/main.cpp

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ static char& __unbox_z = __stop___unbox;
9494
#endif // _MSC_VER
9595

9696
extern "C" bool RhInitialize();
97-
extern "C" void RhSetRuntimeInitializationCallback(int (*fPtr)());
9897

9998
extern "C" bool RhRegisterOSModule(void * pModule,
10099
void * pvManagedCodeStartRange, uint32_t cbManagedCodeRange,
@@ -169,10 +168,21 @@ extern "C" int __managed__Main(int argc, char* argv[]);
169168
#else
170169
#define NATIVEAOT_ENTRYPOINT __managed__Startup
171170
extern "C" void __managed__Startup();
171+
// _initialize is a function generated by the WASI SDK libc that calls the LLVM synthesized __wasm_call_ctors function for reactor components:
172+
// https://github.com/WebAssembly/wasi-libc/blob/9f51a7102085ec6a6ced5778f0864c9af9f50000/libc-bottom-half/crt/crt1-reactor.c#L7-L27
173+
// We define and call it for NATIVEAOT_DLL and TARGET_WASI to call all the global c++ static constructors. This ensures the runtime is intialized
174+
// when calling into WebAssembly Component Model components
175+
#if defined(TARGET_WASI)
176+
extern "C" void _initialize();
177+
#endif // TARGET_WASI
172178
#endif // !NATIVEAOT_DLL
173179

174180
static int InitializeRuntime()
175181
{
182+
#if defined(NATIVEAOT_DLL) && defined(TARGET_WASI)
183+
_initialize();
184+
#endif
185+
176186
if (!RhInitialize())
177187
return -1;
178188

@@ -200,6 +210,12 @@ static int InitializeRuntime()
200210
return 0;
201211
}
202212

213+
#ifdef NATIVEAOT_DLL
214+
int (*g_RuntimeInitializationCallback)() = &InitializeRuntime;
215+
#else
216+
int (*g_RuntimeInitializationCallback)() = nullptr;
217+
#endif
218+
203219
#ifndef NATIVEAOT_DLL
204220

205221
#ifdef ENSURE_PRIMARY_STACK_SIZE
@@ -229,20 +245,3 @@ int main(int argc, char* argv[])
229245
return __managed__Main(argc, argv);
230246
}
231247
#endif // !NATIVEAOT_DLL
232-
233-
#ifdef NATIVEAOT_DLL
234-
static struct InitializeRuntimePointerHelper
235-
{
236-
InitializeRuntimePointerHelper()
237-
{
238-
RhSetRuntimeInitializationCallback(&InitializeRuntime);
239-
}
240-
} initializeRuntimePointerHelper;
241-
242-
extern "C" void* NativeAOT_StaticInitialization();
243-
244-
void* NativeAOT_StaticInitialization()
245-
{
246-
return &initializeRuntimePointerHelper;
247-
}
248-
#endif // NATIVEAOT_DLL

src/coreclr/nativeaot/BuildIntegration/Microsoft.NETCore.Native.Unix.targets

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,6 @@ The .NET Foundation licenses this file to you under the MIT license.
142142
<LinkerArg Include="-Wl,-z,relro" Condition="'$(_IsApplePlatform)' != 'true'" />
143143
<!-- binskim warning BA3011 The BIND_NOW flag is missing -->
144144
<LinkerArg Include="-Wl,-z,now" Condition="'$(_IsApplePlatform)' != 'true'" />
145-
<LinkerArg Include="-Wl,-u,$(_SymbolPrefix)NativeAOT_StaticInitialization" Condition="'$(NativeLib)' == 'Shared' or '$(CustomNativeMain)' == 'true'" />
146145
<!-- this workaround can be deleted once the minimum supported glibc version
147146
(runtime's official build machine's glibc version) is at least 2.33
148147
see https://github.com/bminor/glibc/commit/99468ed45f5a58f584bab60364af937eb6f8afda -->

src/coreclr/nativeaot/BuildIntegration/Microsoft.NETCore.Native.Windows.targets

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ The .NET Foundation licenses this file to you under the MIT license.
8787
<LinkerArg Include="/INCREMENTAL:NO" />
8888
<LinkerArg Condition="'$(LinkerSubsystem)' != ''" Include="/SUBSYSTEM:$(LinkerSubsystem)" />
8989
<LinkerArg Condition="'$(OutputType)' == 'WinExe' or '$(OutputType)' == 'Exe'" Include="/ENTRY:$(EntryPointSymbol) /NOEXP /NOIMPLIB" />
90-
<LinkerArg Condition="'$(NativeLib)' == 'Shared' or '$(CustomNativeMain)' == 'true'" Include="/INCLUDE:NativeAOT_StaticInitialization" />
9190
<LinkerArg Include="/NATVIS:&quot;$(MSBuildThisFileDirectory)NativeAOT.natvis&quot;" />
9291
<LinkerArg Condition="'$(ControlFlowGuard)' == 'Guard'" Include="/guard:cf" />
9392
</ItemGroup>

src/coreclr/nativeaot/BuildIntegration/Microsoft.NETCore.Native.targets

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,6 @@ The .NET Foundation licenses this file to you under the MIT license.
508508
<CustomLinkerArg Include="-o &quot;$(NativeBinary.Replace(&quot;\&quot;, &quot;/&quot;))&quot;" />
509509
<CustomLinkerArg Condition="'$(NativeDebugSymbols)' == 'true'" Include="-g3" />
510510
<CustomLinkerArg Condition="'$(Optimize)' != 'true'" Include="$(WasmOptimizationSetting) -flto" />
511-
<CustomLinkerArg Condition="$(NativeLib) == 'Shared' or '$(CustomNativeMain)' == 'true'" Include="-Wl,--export,NativeAOT_StaticInitialization" />
512511
<CustomLinkerArg Condition="'$(IlcLlvmTarget)' != ''" Include="-target $(IlcLlvmTarget)" />
513512
</ItemGroup>
514513

src/coreclr/nativeaot/Runtime/thread.cpp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ EXTERN_C NATIVEAOT_API void* REDHAWK_CALLCONV RhpHandleAlloc(void* pObject, int
3333
EXTERN_C NATIVEAOT_API void REDHAWK_CALLCONV RhHandleSet(void* handle, void* pObject);
3434
EXTERN_C NATIVEAOT_API void REDHAWK_CALLCONV RhHandleFree(void* handle);
3535

36-
static int (*g_RuntimeInitializationCallback)();
36+
extern int (*g_RuntimeInitializationCallback)();
3737
static Thread* g_RuntimeInitializingThread;
3838

3939
#endif //!DACCESS_COMPILE
@@ -1173,11 +1173,6 @@ FORCEINLINE bool Thread::InlineTryFastReversePInvoke(ReversePInvokeFrame * pFram
11731173
return true;
11741174
}
11751175

1176-
EXTERN_C void RhSetRuntimeInitializationCallback(int (*fPtr)())
1177-
{
1178-
g_RuntimeInitializationCallback = fPtr;
1179-
}
1180-
11811176
void Thread::ReversePInvokeAttachOrTrapThread(ReversePInvokeFrame * pFrame)
11821177
{
11831178
if (!IsStateSet(TSF_Attached))

0 commit comments

Comments
 (0)