Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NativeAOT-LLVM: Init callback declaration #2364

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 24 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,28 @@ 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
jkotas marked this conversation as resolved.
Show resolved Hide resolved
// when calling into WebAssembly Component Model components
#if defined(NATIVEAOT_DLL) && defined(TARGET_WASI)
extern "C" void _initialize();
jkotas marked this conversation as resolved.
Show resolved Hide resolved

// CustomNativeMain programs are built using the same libbootstrapperdll as NATIVEAOT_DLL but wasi-libc will not provide an _initialize implemeentation,
jkotas marked this conversation as resolved.
Show resolved Hide resolved
// so create a dummy one here and make it weak to allow wasi-libc to provide the real implmentation for WASI reactor components.
jkotas marked this conversation as resolved.
Show resolved Hide resolved
__attribute__((weak)) 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 +217,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 +252,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 All @@ -523,7 +522,7 @@ The .NET Foundation licenses this file to you under the MIT license.
<CustomLinkerArg Condition="'$(WasmEnableExceptionHandling)' == 'true'" Include="-fwasm-exceptions" />
</ItemGroup>

<ItemGroup Condition ="'$(_targetOS)' == 'wasi'" >
<ItemGroup Condition ="'$(_targetOS)' == 'wasi'" >
<!-- Wasi has lots of undefined symbols currently, mostly globalization -->
<CustomLinkerArg Include="-Wl,--unresolved-symbols=ignore-all -lstdc++ -Wl,--error-limit=0" />
<CustomLinkerArg Include="--sysroot=&quot;$(WASI_SDK_PATH.Replace(&quot;\&quot;, &quot;/&quot;))/share/wasi-sysroot&quot;" />
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