Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion src/coreclr/nativeaot/Bootstrap/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ extern "C" bool RhRegisterOSModule(void * pModule,
void * pvUnboxingStubsStartRange, uint32_t cbUnboxingStubsRange,
void ** pClasslibFunctions, uint32_t nClasslibFunctions);

void* PalGetModuleHandleFromPointer(void* pointer);
void* PalGetModuleHandleFromPointer(void* pointer, bool pinModule = false);

#if defined(HOST_X86) && defined(HOST_WINDOWS)
#define STRINGIFY(s) #s
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,8 @@ The .NET Foundation licenses this file to you under the MIT license.
<LinkerArg Include="-Wl,-z,nobtcfi" Condition="'$(_targetOS)' == 'openbsd'" />
<!-- Opt out of OpenBSD's execute-only text so NativeAOT can read its own stub bytes (RhGetCodeTarget). -->
<LinkerArg Include="-Wl,--no-execute-only" Condition="'$(_targetOS)' == 'openbsd'" />
<!-- NativeAOT shared libraries should not be unloaded -->
<LinkerArg Include="-Wl,-z,nodelete" Condition="'$(_IsApplePlatform)' != 'true' and '$(NativeLib)' == 'Shared'" />
<!-- 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
2 changes: 1 addition & 1 deletion src/coreclr/nativeaot/Runtime/Pal.h
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ void PalSleep(uint32_t milliseconds);
UInt32_BOOL PalSwitchToThread();
UInt32_BOOL PalAreShadowStacksEnabled();
HANDLE PalCreateEventW(_In_opt_ LPSECURITY_ATTRIBUTES pEventAttributes, UInt32_BOOL manualReset, UInt32_BOOL initialState, _In_opt_z_ LPCWSTR pName);
HANDLE PalGetModuleHandleFromPointer(_In_ void* pointer);
HANDLE PalGetModuleHandleFromPointer(_In_ void* pointer, bool pinModule = false);

#ifdef TARGET_UNIX
typedef int32_t (*PHARDWARE_EXCEPTION_HANDLER)(uintptr_t faultCode, uintptr_t faultAddress, PAL_LIMITED_CONTEXT* palContext, uintptr_t* arg0Reg, uintptr_t* arg1Reg);
Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/nativeaot/Runtime/startup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ extern "C" bool RhInitialize(bool isDll)
g_safeToShutdownTracing = !isDll;
#endif

if (!InitDLL(PalGetModuleHandleFromPointer((void*)&RhInitialize)))
if (!InitDLL(PalGetModuleHandleFromPointer((void*)&RhInitialize, isDll)))
return false;

return true;
Expand Down
12 changes: 11 additions & 1 deletion src/coreclr/nativeaot/Runtime/unix/PalUnix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -842,7 +842,7 @@ bool PalStartEventPipeHelperThread(_In_ BackgroundCallback callback, _In_opt_ vo
return PalStartBackgroundWork(callback, pCallbackContext, UInt32_FALSE);
}

HANDLE PalGetModuleHandleFromPointer(_In_ void* pointer)
HANDLE PalGetModuleHandleFromPointer(_In_ void* pointer, bool pinModule)
{
HANDLE moduleHandle = NULL;

Expand All @@ -853,6 +853,16 @@ HANDLE PalGetModuleHandleFromPointer(_In_ void* pointer)
int st = dladdr(pointer, &info);
if (st != 0)
{
#if defined(HOST_OSX)
if (pinModule && info.dli_fname != nullptr)
{
// NativeAOT runtime state cannot be safely unloaded.
// Keep the extra reference for the lifetime of the process.
Comment thread
rcj1 marked this conversation as resolved.
// Unloading is disabled via `-z,nodelete` linker option on ELF platforms.
dlopen(info.dli_fname, RTLD_LAZY | RTLD_NOLOAD);
}
#endif

moduleHandle = info.dli_fbase;
}
#endif //!defined(HOST_WASM)
Expand Down
9 changes: 5 additions & 4 deletions src/coreclr/nativeaot/Runtime/windows/PalMinWin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -921,14 +921,15 @@ bool PalStartEventPipeHelperThread(_In_ BackgroundCallback callback, _In_opt_ vo
return PalStartBackgroundWork(callback, pCallbackContext, FALSE);
}

HANDLE PalGetModuleHandleFromPointer(_In_ void* pointer)
HANDLE PalGetModuleHandleFromPointer(_In_ void* pointer, bool pinModule)
{
// The runtime is not designed to be unloadable today. Use GET_MODULE_HANDLE_EX_FLAG_PIN to prevent
// the module from ever unloading.
// The runtime is not designed to be unloadable today.
DWORD flags = GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS |
(pinModule ? GET_MODULE_HANDLE_EX_FLAG_PIN : GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT);

HMODULE module;
if (!GetModuleHandleExW(
GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_PIN,
flags,
(LPCWSTR)pointer,
&module))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,7 @@ int main(int argc, char* argv[])
#ifdef TARGET_WINDOWS
FreeLibrary(handle);
#else
// TODO: How to pin the library in memory on Unix?
// dlclose(handle);
dlclose(handle);
#endif

return 100;
Expand Down