[*] Dynamic linking on non-desktop, NT platforms

This commit is contained in:
Reece Wilson 2023-09-17 22:06:48 +01:00
parent 9be76adb23
commit d387657e04
3 changed files with 76 additions and 9 deletions

View File

@ -9,18 +9,63 @@
#include <RuntimeInternal.hpp>
#include "Source/Threading/Primitives/AuConditionMutex.NT.hpp"
#define AURORA_HAS_LOAD_PGKD
#define AURORA_HAS_GET_PROC_NONWIN32
namespace Aurora
{
static bool gShouldResPathDoNothing {};
#if !defined(AURORA_PLATFORM_WIN32)
HMODULE UWPLibraryW(LPCWSTR lpLibFileName)
{
auto pos = std::basic_string_view<wchar_t>(lpLibFileName).find_last_of('/');
if (pos == std::basic_string_view<wchar_t>::npos)
{
#if defined(AURORA_HAS_LOAD_PGKD)
return (HMODULE)::LoadPackagedLibrary(lpLibFileName, 0);
#else
return (HMODULE)lpLibFileName;
#endif
}
else
{
auto pString = &lpLibFileName[pos];
#if defined(AURORA_HAS_LOAD_PGKD)
return (HMODULE)::LoadPackagedLibrary(pString, 0);
#else
return (HMODULE)pString;
#endif
}
}
FARPROC UWPProcAddress(HMODULE hModule,
LPCSTR lpProcName)
{
if (!hModule)
{
return nullptr;
}
#if defined(AURORA_HAS_GET_PROC_NONWIN32)
return GetProcAddress(hModule, lpProcName);
#endif
return nullptr;
}
#endif
void InitNTAddresses()
{
#if defined(AURORA_PLATFORM_WIN32)
pLoadLibraryW = LoadLibraryW;
pGetProcAddress = GetProcAddress;
#define ADD_LOAD_LIB(name) \
auto h ## name = GetModuleHandleW(k## name ## DllName); \
if (!h ## name) \
{ \
h ## name = LoadLibraryW(k## name ## DllName); \
h ## name = pLoadLibraryW(k## name ## DllName); \
}
ADD_LOAD_LIB(Kernel32);
@ -41,20 +86,20 @@ namespace Aurora
#define ADD_GET_PROC(name, proc) \
if (h ## name) \
{ \
p ## proc = AuReinterpretCast<decltype(p ## proc)>(GetProcAddress(h ## name, #proc)); \
p ## proc = AuReinterpretCast<decltype(p ## proc)>(pGetProcAddress(h ## name, #proc)); \
}
#define ADD_GET_PROC_BI(name, name2, proc) \
p ## proc = nullptr; \
if (h ## name) \
{ \
p ## proc = AuReinterpretCast<decltype(p ## proc)>(GetProcAddress(h ## name, #proc)); \
p ## proc = AuReinterpretCast<decltype(p ## proc)>(pGetProcAddress(h ## name, #proc)); \
} \
if (!p ## proc) \
{ \
if (h ## name2) \
{ \
p ## proc = AuReinterpretCast<decltype(p ## proc)>(GetProcAddress(h ## name2, #proc)); \
p ## proc = AuReinterpretCast<decltype(p ## proc)>(pGetProcAddress(h ## name2, #proc));\
} \
}
@ -62,20 +107,20 @@ namespace Aurora
p ## proc2 = nullptr; \
if (h ## name) \
{ \
p ## proc2 = AuReinterpretCast<decltype(p ## proc2)>(GetProcAddress(h ## name, #proc)); \
p ## proc2 = AuReinterpretCast<decltype(p ## proc2)>(pGetProcAddress(h ## name, #proc)); \
} \
if (!p ## proc2) \
{ \
if (h ## name2) \
{ \
p ## proc2 = AuReinterpretCast<decltype(p ## proc2)>(GetProcAddress(h ## name2, #proc2)); \
p ## proc2 = AuReinterpretCast<decltype(p ## proc2)>(pGetProcAddress(h ## name2, #proc2)); \
} \
}
#define ADD_GET_PROC_INTERNAL_MAP(name, proc, symbol) \
if (h ## name) \
{ \
p ## proc = AuReinterpretCast<decltype(p ## proc)>(GetProcAddress(h ## name, #symbol)); \
p ## proc = AuReinterpretCast<decltype(p ## proc)>(pGetProcAddress(h ## name, #symbol)); \
}
if (pRtlGetVersion)
@ -179,6 +224,9 @@ namespace Aurora
}
#else
pLoadLibraryW = UWPLibraryW;
pGetProcAddress = UWPProcAddress;
pCreateFile2W = CreateFile2FromAppW;
pWaitOnAddress = WaitOnAddress;

View File

@ -577,4 +577,13 @@ namespace Aurora
DWORD dwFlags = 0,
DWORD dwAttributes = 0
);
inline HMODULE (*pLoadLibraryW)(
LPCWSTR lpLibFileName
);
inline FARPROC (*pGetProcAddress)(
HMODULE hModule,
LPCSTR lpProcName
);
}

View File

@ -273,7 +273,12 @@ namespace Aurora::Process
static bool LoadModule(const AuString &name, const AuString &path)
{
#if defined(AURORA_IS_MODERNNT_DERIVED)
auto handle = LoadLibraryW(Locale::ConvertFromUTF8(path).c_str());
if (!pLoadLibraryW)
{
return false;
}
auto handle = pLoadLibraryW(Locale::ConvertFromUTF8(path).c_str());
if (handle == INVALID_HANDLE_VALUE)
{
SysPushErrorNested("Could't link dynamic library {}", path);
@ -541,8 +546,13 @@ namespace Aurora::Process
}
auto handle = h->second;
if (!pGetProcAddress)
{
return {};
}
#if defined(AURORA_IS_MODERNNT_DERIVED)
auto ret = reinterpret_cast<AuMach>(::GetProcAddress(reinterpret_cast<HMODULE>(handle), symbol.c_str()));
auto ret = reinterpret_cast<AuMach>(pGetProcAddress(reinterpret_cast<HMODULE>(handle), symbol.c_str()));
#else
auto ret = reinterpret_cast<AuMach>(dlsym(handle, symbol.c_str()));
#endif