No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LibraryLoader.cpp 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #include "il2cpp-config.h"
  2. #if IL2CPP_TARGET_WINDOWS
  3. #include "il2cpp-runtime-metadata.h"
  4. #include "os/LibraryLoader.h"
  5. #include "os/Image.h"
  6. #include "utils/StringUtils.h"
  7. #include "WindowsHelpers.h"
  8. #include "Evntprov.h"
  9. #define WINNT // All functions in Evntrace.h are under this define.. Why? I have no idea!
  10. #include "Evntrace.h"
  11. #if IL2CPP_TARGET_WINDOWS_GAMES
  12. #include "os/WindowsGames/Win32ApiWindowsGamesEmulation.h"
  13. #endif
  14. #include <bcrypt.h>
  15. namespace il2cpp
  16. {
  17. namespace os
  18. {
  19. const HardcodedPInvokeDependencyFunction kAdvapiFunctions[] =
  20. {
  21. HARDCODED_DEPENDENCY_FUNCTION(EnumerateTraceGuidsEx),
  22. HARDCODED_DEPENDENCY_FUNCTION(EventActivityIdControl),
  23. HARDCODED_DEPENDENCY_FUNCTION(EventRegister),
  24. HARDCODED_DEPENDENCY_FUNCTION(EventSetInformation),
  25. HARDCODED_DEPENDENCY_FUNCTION(EventUnregister),
  26. HARDCODED_DEPENDENCY_FUNCTION(EventWrite),
  27. HARDCODED_DEPENDENCY_FUNCTION(EventWriteEx),
  28. HARDCODED_DEPENDENCY_FUNCTION(EventWriteString),
  29. HARDCODED_DEPENDENCY_FUNCTION(EventWriteTransfer),
  30. };
  31. const HardcodedPInvokeDependencyFunction kKernel32Functions[] =
  32. {
  33. HARDCODED_DEPENDENCY_FUNCTION(FormatMessageW),
  34. HARDCODED_DEPENDENCY_FUNCTION(GetCurrentProcessId),
  35. HARDCODED_DEPENDENCY_FUNCTION(GetDynamicTimeZoneInformation),
  36. HARDCODED_DEPENDENCY_FUNCTION(GetNativeSystemInfo),
  37. HARDCODED_DEPENDENCY_FUNCTION(GetTimeZoneInformation),
  38. HARDCODED_DEPENDENCY_FUNCTION(GetFullPathNameW),
  39. HARDCODED_DEPENDENCY_FUNCTION(GetFileAttributesExW),
  40. HARDCODED_DEPENDENCY_FUNCTION(CreateDirectoryW),
  41. HARDCODED_DEPENDENCY_FUNCTION(CloseHandle),
  42. HARDCODED_DEPENDENCY_FUNCTION(CreateFileW),
  43. HARDCODED_DEPENDENCY_FUNCTION(DeleteFileW),
  44. HARDCODED_DEPENDENCY_FUNCTION(FindFirstFileExW),
  45. HARDCODED_DEPENDENCY_FUNCTION(FindNextFileW),
  46. HARDCODED_DEPENDENCY_FUNCTION(MoveFileExW),
  47. HARDCODED_DEPENDENCY_FUNCTION(RemoveDirectoryW),
  48. HARDCODED_DEPENDENCY_FUNCTION(ReplaceFileW),
  49. HARDCODED_DEPENDENCY_FUNCTION(SetFileAttributesW),
  50. HARDCODED_DEPENDENCY_FUNCTION(SetFileInformationByHandle),
  51. HARDCODED_DEPENDENCY_FUNCTION(GetFileInformationByHandleEx),
  52. HARDCODED_DEPENDENCY_FUNCTION(CopyFile2),
  53. HARDCODED_DEPENDENCY_FUNCTION(SetThreadErrorMode),
  54. HARDCODED_DEPENDENCY_FUNCTION(GetCurrentThread),
  55. HARDCODED_DEPENDENCY_FUNCTION(GetThreadDescription),
  56. HARDCODED_DEPENDENCY_FUNCTION(CopyFileExW),
  57. HARDCODED_DEPENDENCY_FUNCTION(DeleteVolumeMountPointW),
  58. HARDCODED_DEPENDENCY_FUNCTION(GetLogicalDrives),
  59. HARDCODED_DEPENDENCY_FUNCTION(LocalAlloc),
  60. HARDCODED_DEPENDENCY_FUNCTION(LocalReAlloc),
  61. HARDCODED_DEPENDENCY_FUNCTION(LocalFree),
  62. };
  63. const HardcodedPInvokeDependencyFunction kBCryptFunctions[] =
  64. {
  65. HARDCODED_DEPENDENCY_FUNCTION(BCryptGenRandom),
  66. };
  67. const HardcodedPInvokeDependencyFunction kiphlpapiFunctions[] =
  68. {
  69. HARDCODED_DEPENDENCY_FUNCTION(GetNetworkParams),
  70. HARDCODED_DEPENDENCY_FUNCTION(GetAdaptersAddresses),
  71. HARDCODED_DEPENDENCY_FUNCTION(GetIfEntry),
  72. };
  73. #if !IL2CPP_TARGET_WINDOWS_GAMES
  74. const HardcodedPInvokeDependencyFunction kTimezoneFunctions[] =
  75. {
  76. HARDCODED_DEPENDENCY_FUNCTION(EnumDynamicTimeZoneInformation),
  77. HARDCODED_DEPENDENCY_FUNCTION(GetDynamicTimeZoneInformation),
  78. HARDCODED_DEPENDENCY_FUNCTION(GetDynamicTimeZoneInformationEffectiveYears),
  79. HARDCODED_DEPENDENCY_FUNCTION(GetTimeZoneInformationForYear),
  80. };
  81. const HardcodedPInvokeDependencyFunction kWinTypesFunctions[] =
  82. {
  83. HARDCODED_DEPENDENCY_FUNCTION(RoGetBufferMarshaler)
  84. };
  85. #endif
  86. // All these come without ".dll" extension!
  87. const HardcodedPInvokeDependencyLibrary kHardcodedPInvokeDependencies[] =
  88. {
  89. #if IL2CPP_TARGET_WINDOWS_GAMES
  90. HARDCODED_DEPENDENCY_LIBRARY(L"bcrypt", kBCryptFunctions),
  91. #else
  92. HARDCODED_DEPENDENCY_LIBRARY(L"advapi32", kAdvapiFunctions),
  93. HARDCODED_DEPENDENCY_LIBRARY(L"api-ms-win-core-timezone-l1-1-0", kTimezoneFunctions),
  94. HARDCODED_DEPENDENCY_LIBRARY(L"kernel32", kKernel32Functions),
  95. HARDCODED_DEPENDENCY_LIBRARY(L"iphlpapi", kiphlpapiFunctions),
  96. HARDCODED_DEPENDENCY_LIBRARY(L"wintypes", kWinTypesFunctions),
  97. HARDCODED_DEPENDENCY_LIBRARY(L"bcrypt", kBCryptFunctions),
  98. #endif
  99. };
  100. const HardcodedPInvokeDependencyLibrary* LibraryLoader::HardcodedPInvokeDependencies = kHardcodedPInvokeDependencies;
  101. const size_t LibraryLoader::HardcodedPInvokeDependenciesCount = ARRAYSIZE(kHardcodedPInvokeDependencies);
  102. Baselib_DynamicLibrary_Handle LibraryLoader::ProbeForLibrary(const Il2CppNativeChar* libraryName, const size_t /*libraryNameLength*/, std::string& detailedError)
  103. {
  104. return TryOpeningLibrary(libraryName, detailedError);
  105. }
  106. Baselib_DynamicLibrary_Handle LibraryLoader::OpenProgramHandle(Baselib_ErrorState& errorState, bool& needsClosing)
  107. {
  108. needsClosing = false;
  109. return Baselib_DynamicLibrary_FromNativeHandle(reinterpret_cast<uint64_t>(Image::GetImageBase()), Baselib_DynamicLibrary_WinApiHMODULE, &errorState);
  110. }
  111. bool LibraryLoader::EntryNameMatches(const il2cpp::utils::StringView<char>& hardcodedEntryPoint, const il2cpp::utils::StringView<char>& entryPoint)
  112. {
  113. // Handle windows mapping generic to unicode methods. e.g. MoveFileEx -> MoveFileExW
  114. if (hardcodedEntryPoint.Length() == entryPoint.Length() || (hardcodedEntryPoint.Length() - 1 == entryPoint.Length() && hardcodedEntryPoint[hardcodedEntryPoint.Length() - 1] == 'W'))
  115. {
  116. return strncmp(hardcodedEntryPoint.Str(), entryPoint.Str(), entryPoint.Length()) == 0;
  117. }
  118. return false;
  119. }
  120. }
  121. }
  122. #endif