Skip to content

Commit

Permalink
add _initialize rationale and cleanup NativeAOT_StaticInitialization
Browse files Browse the repository at this point in the history
  • Loading branch information
yowl committed Jul 30, 2023
1 parent 7dace36 commit e9d1f02
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 27 deletions.
35 changes: 17 additions & 18 deletions src/coreclr/nativeaot/Bootstrap/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ static char& __unbox_z = __stop___unbox;
#endif // _MSC_VER

extern "C" bool RhInitialize();
extern "C" void RhSetRuntimeInitializationCallback(int (*fPtr)());

extern "C" bool RhRegisterOSModule(void * pModule,
void * pvManagedCodeStartRange, uint32_t cbManagedCodeRange,
Expand Down Expand Up @@ -169,10 +168,21 @@ extern "C" int __managed__Main(int argc, char* argv[]);
#else
#define NATIVEAOT_ENTRYPOINT __managed__Startup
extern "C" void __managed__Startup();
// _initialize is a function generated by the WASI SDK libc that calls the LLVM synthesized __wasm_call_ctors function for reactor components:
// https://github.com/WebAssembly/wasi-libc/blob/9f51a7102085ec6a6ced5778f0864c9af9f50000/libc-bottom-half/crt/crt1-reactor.c#L7-L27
// 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
// when calling into WebAssembly Component Model components
#if defined(TARGET_WASI)
extern "C" void _initialize();
#endif // TARGET_WASI
#endif // !NATIVEAOT_DLL

static int InitializeRuntime()
{
#if defined(NATIVEAOT_DLL) && defined(TARGET_WASI)
_initialize();
#endif

if (!RhInitialize())
return -1;

Expand Down Expand Up @@ -200,6 +210,12 @@ static int InitializeRuntime()
return 0;
}

#ifdef NATIVEAOT_DLL
int (*g_RuntimeInitializationCallback)() = &InitializeRuntime;
#else
int (*g_RuntimeInitializationCallback)() = nullptr;
#endif

#ifndef NATIVEAOT_DLL

#ifdef ENSURE_PRIMARY_STACK_SIZE
Expand Down Expand Up @@ -229,20 +245,3 @@ int main(int argc, char* argv[])
return __managed__Main(argc, argv);
}
#endif // !NATIVEAOT_DLL

#ifdef NATIVEAOT_DLL
static struct InitializeRuntimePointerHelper
{
InitializeRuntimePointerHelper()
{
RhSetRuntimeInitializationCallback(&InitializeRuntime);
}
} initializeRuntimePointerHelper;

extern "C" void* NativeAOT_StaticInitialization();

void* NativeAOT_StaticInitialization()
{
return &initializeRuntimePointerHelper;
}
#endif // NATIVEAOT_DLL
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,6 @@ The .NET Foundation licenses this file to you under the MIT license.
<LinkerArg Include="-Wl,-z,relro" Condition="'$(_IsApplePlatform)' != 'true'" />
<!-- binskim warning BA3011 The BIND_NOW flag is missing -->
<LinkerArg Include="-Wl,-z,now" Condition="'$(_IsApplePlatform)' != 'true'" />
<LinkerArg Include="-Wl,-u,$(_SymbolPrefix)NativeAOT_StaticInitialization" Condition="'$(NativeLib)' == 'Shared' or '$(CustomNativeMain)' == 'true'" />
<!-- this workaround can be deleted once the minimum supported glibc version
(runtime's official build machine's glibc version) is at least 2.33
see https://github.com/bminor/glibc/commit/99468ed45f5a58f584bab60364af937eb6f8afda -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ The .NET Foundation licenses this file to you under the MIT license.
<LinkerArg Include="/INCREMENTAL:NO" />
<LinkerArg Condition="'$(LinkerSubsystem)' != ''" Include="/SUBSYSTEM:$(LinkerSubsystem)" />
<LinkerArg Condition="'$(OutputType)' == 'WinExe' or '$(OutputType)' == 'Exe'" Include="/ENTRY:$(EntryPointSymbol) /NOEXP /NOIMPLIB" />
<LinkerArg Condition="'$(NativeLib)' == 'Shared' or '$(CustomNativeMain)' == 'true'" Include="/INCLUDE:NativeAOT_StaticInitialization" />
<LinkerArg Include="/NATVIS:&quot;$(MSBuildThisFileDirectory)NativeAOT.natvis&quot;" />
<LinkerArg Condition="'$(ControlFlowGuard)' == 'Guard'" Include="/guard:cf" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,6 @@ The .NET Foundation licenses this file to you under the MIT license.
<CustomLinkerArg Include="-o &quot;$(NativeBinary.Replace(&quot;\&quot;, &quot;/&quot;))&quot;" />
<CustomLinkerArg Condition="'$(NativeDebugSymbols)' == 'true'" Include="-g3" />
<CustomLinkerArg Condition="'$(Optimize)' != 'true'" Include="$(WasmOptimizationSetting) -flto" />
<CustomLinkerArg Condition="$(NativeLib) == 'Shared' or '$(CustomNativeMain)' == 'true'" Include="-Wl,--export,NativeAOT_StaticInitialization" />
<CustomLinkerArg Condition="'$(IlcLlvmTarget)' != ''" Include="-target $(IlcLlvmTarget)" />
</ItemGroup>

Expand Down
7 changes: 1 addition & 6 deletions src/coreclr/nativeaot/Runtime/thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ EXTERN_C NATIVEAOT_API void* REDHAWK_CALLCONV RhpHandleAlloc(void* pObject, int
EXTERN_C NATIVEAOT_API void REDHAWK_CALLCONV RhHandleSet(void* handle, void* pObject);
EXTERN_C NATIVEAOT_API void REDHAWK_CALLCONV RhHandleFree(void* handle);

static int (*g_RuntimeInitializationCallback)();
extern int (*g_RuntimeInitializationCallback)();
static Thread* g_RuntimeInitializingThread;

#endif //!DACCESS_COMPILE
Expand Down Expand Up @@ -1173,11 +1173,6 @@ FORCEINLINE bool Thread::InlineTryFastReversePInvoke(ReversePInvokeFrame * pFram
return true;
}

EXTERN_C void RhSetRuntimeInitializationCallback(int (*fPtr)())
{
g_RuntimeInitializationCallback = fPtr;
}

void Thread::ReversePInvokeAttachOrTrapThread(ReversePInvokeFrame * pFrame)
{
if (!IsStateSet(TSF_Attached))
Expand Down

0 comments on commit e9d1f02

Please sign in to comment.