diff options
-rw-r--r-- | src/osd/modules/lib/osdlib_uwp.cpp | 212 | ||||
-rw-r--r-- | src/osd/uwp/uwpcompat.cpp | 13 | ||||
-rw-r--r-- | src/osd/uwp/uwpcompat.h | 4 |
3 files changed, 8 insertions, 221 deletions
diff --git a/src/osd/modules/lib/osdlib_uwp.cpp b/src/osd/modules/lib/osdlib_uwp.cpp index 27c0ac4abe1..170f964f74e 100644 --- a/src/osd/modules/lib/osdlib_uwp.cpp +++ b/src/osd/modules/lib/osdlib_uwp.cpp @@ -176,215 +176,3 @@ char *osd_get_clipboard_text(void) return result; } - -//============================================================ -// osd_dynamic_bind -//============================================================ -// for Windows Store universal applications -// This needs to change ASAP as it won't be allowed in the store -typedef HMODULE __stdcall t_LLA(const char *); -typedef FARPROC __stdcall t_GPA(HMODULE H, const char *); - -PIMAGE_NT_HEADERS WINAPI ImageNtHeader(PVOID Base) -{ - return (PIMAGE_NT_HEADERS) - ((LPBYTE)Base + ((PIMAGE_DOS_HEADER)Base)->e_lfanew); -} - -PIMAGE_SECTION_HEADER WINAPI RtlImageRvaToSection(const IMAGE_NT_HEADERS *nt, - HMODULE module, DWORD_PTR rva) -{ - int i; - const IMAGE_SECTION_HEADER *sec; - - sec = (const IMAGE_SECTION_HEADER*)((const char*)&nt->OptionalHeader + - nt->FileHeader.SizeOfOptionalHeader); - for (i = 0; i < nt->FileHeader.NumberOfSections; i++, sec++) - { - if ((sec->VirtualAddress <= rva) && (sec->VirtualAddress + sec->SizeOfRawData > rva)) - return (PIMAGE_SECTION_HEADER)sec; - } - return NULL; -} - -PVOID WINAPI RtlImageRvaToVa(const IMAGE_NT_HEADERS *nt, HMODULE module, - DWORD_PTR rva, IMAGE_SECTION_HEADER **section) -{ - IMAGE_SECTION_HEADER *sec; - - if (section && *section) /* try this section first */ - { - sec = *section; - if ((sec->VirtualAddress <= rva) && (sec->VirtualAddress + sec->SizeOfRawData > rva)) - goto found; - } - if (!(sec = RtlImageRvaToSection(nt, module, rva))) return NULL; -found: - if (section) *section = sec; - return (char *)module + sec->PointerToRawData + (rva - sec->VirtualAddress); -} - -PVOID WINAPI ImageDirectoryEntryToDataEx(PVOID base, BOOLEAN image, USHORT dir, PULONG size, PIMAGE_SECTION_HEADER *section) -{ - const IMAGE_NT_HEADERS *nt; - DWORD_PTR addr; - - *size = 0; - if (section) *section = NULL; - - if (!(nt = ImageNtHeader(base))) return NULL; - if (dir >= nt->OptionalHeader.NumberOfRvaAndSizes) return NULL; - if (!(addr = nt->OptionalHeader.DataDirectory[dir].VirtualAddress)) return NULL; - - *size = nt->OptionalHeader.DataDirectory[dir].Size; - if (image || addr < nt->OptionalHeader.SizeOfHeaders) return (char *)base + addr; - - return RtlImageRvaToVa(nt, (HMODULE)base, addr, section); -} - -// == Windows API GetProcAddress -void *PeGetProcAddressA(void *Base, LPCSTR Name) -{ - DWORD Tmp; - - IMAGE_NT_HEADERS *NT = ImageNtHeader(Base); - IMAGE_EXPORT_DIRECTORY *Exp = (IMAGE_EXPORT_DIRECTORY*)ImageDirectoryEntryToDataEx(Base, TRUE, IMAGE_DIRECTORY_ENTRY_EXPORT, &Tmp, 0); - if (Exp == 0 || Exp->NumberOfFunctions == 0) - { - SetLastError(ERROR_NOT_FOUND); - return 0; - } - - DWORD *Names = (DWORD*)(Exp->AddressOfNames + (DWORD_PTR)Base); - WORD *Ordinals = (WORD*)(Exp->AddressOfNameOrdinals + (DWORD_PTR)Base); - DWORD *Functions = (DWORD*)(Exp->AddressOfFunctions + (DWORD_PTR)Base); - - FARPROC Ret = 0; - - if ((DWORD_PTR)Name<65536) - { - if ((DWORD_PTR)Name - Exp->Base<Exp->NumberOfFunctions) - Ret = (FARPROC)(Functions[(DWORD_PTR)Name - Exp->Base] + (DWORD_PTR)Base); - } - else - { - for (DWORD i = 0; i<Exp->NumberOfNames && Ret == 0; i++) - { - char *Func = (char*)(Names[i] + std::uintptr_t(Base)); - if (Func && strcmp(Func, Name) == 0) - Ret = (FARPROC)(Functions[Ordinals[i]] + std::uintptr_t(Base)); - } - } - - if (Ret) - { - std::uintptr_t ExpStart = NT->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress + std::uintptr_t(Base); - std::uintptr_t ExpSize = NT->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].Size; - if (std::uintptr_t(Ret) >= ExpStart && std::uintptr_t(Ret) <= ExpStart + ExpSize) - { - // Forwarder - return 0; - } - return Ret; - } - - return 0; -} - -t_LLA* g_LoadLibraryA = 0; -t_GPA* g_GetProcAddressA = 0; - -void find_load_exports() -{ - char *Tmp = (char*)GetTickCount64; - Tmp = (char*)((~0xFFF)&(DWORD_PTR)Tmp); - - while (Tmp) - { - __try - { - if (Tmp[0] == 'M' && Tmp[1] == 'Z') - break; - } - __except (EXCEPTION_EXECUTE_HANDLER) - { - } - Tmp -= 0x1000; - } - - if (Tmp == 0) - return; - - g_LoadLibraryA = (t_LLA*)PeGetProcAddressA(Tmp, "LoadLibraryA"); - g_GetProcAddressA = (t_GPA*)PeGetProcAddressA(Tmp, "GetProcAddress"); -} - -#define load_library(filename) g_LoadLibraryA(osd::text::from_wstring(filename).c_str()) -#define get_proc_address(mod, proc) g_GetProcAddressA(mod, proc) - - - -namespace osd { -class dynamic_module_win32_impl : public dynamic_module -{ -public: - dynamic_module_win32_impl(std::vector<std::string> &libraries) - : m_module(nullptr) - { - m_libraries = libraries; - find_load_exports(); - } - - virtual ~dynamic_module_win32_impl() override - { - if (m_module != nullptr) - FreeLibrary(m_module); - }; - -protected: - virtual generic_fptr_t get_symbol_address(char const *symbol) override - { - /* - * given a list of libraries, if a first symbol is successfully loaded from - * one of them, all additional symbols will be loaded from the same library - */ - if (m_module) - { - return reinterpret_cast<generic_fptr_t>(GetProcAddress(m_module, symbol)); - } - - for (auto const &library : m_libraries) - { - osd::text::tstring tempstr = osd::text::to_tstring(library); - HMODULE module = load_library(tempstr.c_str()); - - if (module != nullptr) - { - generic_fptr_t function = reinterpret_cast<generic_fptr_t>(GetProcAddress(module, symbol)); - - if (function != nullptr) - { - m_module = module; - return function; - } - else - { - FreeLibrary(module); - } - } - } - - return nullptr; - } - -private: - std::vector<std::string> m_libraries; - HMODULE m_module; -}; - -dynamic_module::ptr dynamic_module::open(std::vector<std::string> &&names) -{ - return std::make_unique<dynamic_module_win32_impl>(names); -} - -} // namespace osd diff --git a/src/osd/uwp/uwpcompat.cpp b/src/osd/uwp/uwpcompat.cpp index b8f585b4e5b..ef0e07cf081 100644 --- a/src/osd/uwp/uwpcompat.cpp +++ b/src/osd/uwp/uwpcompat.cpp @@ -66,16 +66,14 @@ extern "C" { return osd_ticks(); } + // This is only in here so callers get an error HMODULE WINAPI LoadLibraryExA( _In_ LPCSTR lpLibFileName, _Reserved_ HANDLE hFile, _In_ DWORD dwFlags - ) + ) { - wchar_t libfile_wide[MAX_PATH + 1]; - if (MultiByteToWideChar(CP_ACP, 0, lpLibFileName, strlen(lpLibFileName), libfile_wide, MAX_PATH)) - return LoadPackagedLibrary(libfile_wide, 0); - + SetLastError(ERROR_FILE_NOT_FOUND); return nullptr; } @@ -83,9 +81,10 @@ extern "C" { _In_ LPCWSTR lpLibFileName, _Reserved_ HANDLE hFile, _In_ DWORD dwFlags - ) + ) { - return LoadPackagedLibrary(lpLibFileName, 0); + SetLastError(ERROR_FILE_NOT_FOUND); + return nullptr; } DWORD WINAPI GetFileSize( diff --git a/src/osd/uwp/uwpcompat.h b/src/osd/uwp/uwpcompat.h index f155e225b8c..697d5f9527f 100644 --- a/src/osd/uwp/uwpcompat.h +++ b/src/osd/uwp/uwpcompat.h @@ -72,7 +72,7 @@ LoadLibraryExA( _In_ LPCSTR lpLibFileName, _Reserved_ HANDLE hFile, _In_ DWORD dwFlags - ); +); _Ret_maybenull_ HMODULE @@ -81,7 +81,7 @@ LoadLibraryExW( _In_ LPCWSTR lpLibFileName, _Reserved_ HANDLE hFile, _In_ DWORD dwFlags - ); +); DWORD WINAPI |