LibraryLoader.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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_XBOXONE
  12. #include "os/XboxOne/Win32ApiEmulationForXboxClassLibraries.h"
  13. #elif IL2CPP_TARGET_WINDOWS_GAMES
  14. #include "os/WindowsGames/Win32ApiWindowsGamesEmulation.h"
  15. #endif
  16. namespace il2cpp
  17. {
  18. namespace os
  19. {
  20. #if !IL2CPP_TARGET_WINDOWS_DESKTOP
  21. const HardcodedPInvokeDependencyFunction kAdvapiFunctions[] =
  22. {
  23. #if !IL2CPP_TARGET_XBOXONE
  24. #if WINDOWS_SDK_BUILD_VERSION >= 16299
  25. HARDCODED_DEPENDENCY_FUNCTION(EnumerateTraceGuidsEx),
  26. #endif
  27. HARDCODED_DEPENDENCY_FUNCTION(EventActivityIdControl),
  28. #endif
  29. HARDCODED_DEPENDENCY_FUNCTION(EventRegister),
  30. HARDCODED_DEPENDENCY_FUNCTION(EventSetInformation),
  31. HARDCODED_DEPENDENCY_FUNCTION(EventUnregister),
  32. HARDCODED_DEPENDENCY_FUNCTION(EventWrite),
  33. #if !IL2CPP_TARGET_XBOXONE
  34. HARDCODED_DEPENDENCY_FUNCTION(EventWriteEx),
  35. HARDCODED_DEPENDENCY_FUNCTION(EventWriteString),
  36. HARDCODED_DEPENDENCY_FUNCTION(EventWriteTransfer),
  37. #endif
  38. };
  39. #endif
  40. const HardcodedPInvokeDependencyFunction kKernel32Functions[] =
  41. {
  42. HARDCODED_DEPENDENCY_FUNCTION(FormatMessageW),
  43. HARDCODED_DEPENDENCY_FUNCTION(GetCurrentProcessId),
  44. HARDCODED_DEPENDENCY_FUNCTION(GetDynamicTimeZoneInformation),
  45. HARDCODED_DEPENDENCY_FUNCTION(GetNativeSystemInfo),
  46. HARDCODED_DEPENDENCY_FUNCTION(GetTimeZoneInformation),
  47. HARDCODED_DEPENDENCY_FUNCTION(GetFullPathNameW),
  48. HARDCODED_DEPENDENCY_FUNCTION(GetFileAttributesExW),
  49. HARDCODED_DEPENDENCY_FUNCTION(CreateDirectoryW),
  50. HARDCODED_DEPENDENCY_FUNCTION(CloseHandle),
  51. HARDCODED_DEPENDENCY_FUNCTION(CreateFileW),
  52. HARDCODED_DEPENDENCY_FUNCTION(DeleteFileW),
  53. HARDCODED_DEPENDENCY_FUNCTION(FindFirstFileExW),
  54. HARDCODED_DEPENDENCY_FUNCTION(FindNextFileW),
  55. HARDCODED_DEPENDENCY_FUNCTION(MoveFileExW),
  56. HARDCODED_DEPENDENCY_FUNCTION(RemoveDirectoryW),
  57. HARDCODED_DEPENDENCY_FUNCTION(ReplaceFileW),
  58. HARDCODED_DEPENDENCY_FUNCTION(SetFileAttributesW),
  59. HARDCODED_DEPENDENCY_FUNCTION(SetFileInformationByHandle),
  60. HARDCODED_DEPENDENCY_FUNCTION(GetFileInformationByHandleEx),
  61. // The CopyFile2 method is only required by the class library code for UWP builds.
  62. // It does not exist in Windows 7, so we don't want to use it for Windows Desktop
  63. // builds, since they still support Windows 7.
  64. #if !IL2CPP_TARGET_WINDOWS_DESKTOP
  65. HARDCODED_DEPENDENCY_FUNCTION(CopyFile2),
  66. #endif
  67. #if WINDOWS_SDK_BUILD_VERSION >= 16299
  68. HARDCODED_DEPENDENCY_FUNCTION(SetThreadErrorMode),
  69. HARDCODED_DEPENDENCY_FUNCTION(CopyFileExW),
  70. HARDCODED_DEPENDENCY_FUNCTION(DeleteVolumeMountPointW),
  71. HARDCODED_DEPENDENCY_FUNCTION(GetLogicalDrives),
  72. #endif
  73. };
  74. #if IL2CPP_TARGET_WINRT || IL2CPP_TARGET_WINDOWS_GAMES
  75. const HardcodedPInvokeDependencyFunction kBCryptFunctions[] =
  76. {
  77. HARDCODED_DEPENDENCY_FUNCTION(BCryptGenRandom),
  78. };
  79. #endif
  80. const HardcodedPInvokeDependencyFunction kiphlpapiFunctions[] =
  81. {
  82. HARDCODED_DEPENDENCY_FUNCTION(GetNetworkParams),
  83. #if !IL2CPP_TARGET_XBOXONE
  84. HARDCODED_DEPENDENCY_FUNCTION(GetAdaptersAddresses),
  85. HARDCODED_DEPENDENCY_FUNCTION(GetIfEntry),
  86. #endif
  87. };
  88. #if !IL2CPP_TARGET_WINDOWS_DESKTOP && !IL2CPP_TARGET_WINDOWS_GAMES
  89. const HardcodedPInvokeDependencyFunction kTimezoneFunctions[] =
  90. {
  91. #if !IL2CPP_TARGET_XBOXONE
  92. HARDCODED_DEPENDENCY_FUNCTION(EnumDynamicTimeZoneInformation),
  93. #endif
  94. HARDCODED_DEPENDENCY_FUNCTION(GetDynamicTimeZoneInformation),
  95. #if !IL2CPP_TARGET_XBOXONE
  96. HARDCODED_DEPENDENCY_FUNCTION(GetDynamicTimeZoneInformationEffectiveYears),
  97. #endif
  98. HARDCODED_DEPENDENCY_FUNCTION(GetTimeZoneInformationForYear),
  99. };
  100. #endif
  101. #if IL2CPP_TARGET_WINRT
  102. const HardcodedPInvokeDependencyFunction kWinTypesFunctions[] =
  103. {
  104. HARDCODED_DEPENDENCY_FUNCTION(RoGetBufferMarshaler)
  105. };
  106. #endif
  107. // All these come without ".dll" extension!
  108. const HardcodedPInvokeDependencyLibrary kHardcodedPInvokeDependencies[] =
  109. {
  110. #if IL2CPP_TARGET_WINDOWS_GAMES
  111. HARDCODED_DEPENDENCY_LIBRARY(L"bcrypt", kBCryptFunctions),
  112. #else
  113. #if !IL2CPP_TARGET_WINDOWS_DESKTOP // Some of these functions are win8+s
  114. HARDCODED_DEPENDENCY_LIBRARY(L"advapi32", kAdvapiFunctions),
  115. HARDCODED_DEPENDENCY_LIBRARY(L"api-ms-win-core-timezone-l1-1-0", kTimezoneFunctions),
  116. #endif
  117. HARDCODED_DEPENDENCY_LIBRARY(L"kernel32", kKernel32Functions),
  118. HARDCODED_DEPENDENCY_LIBRARY(L"iphlpapi", kiphlpapiFunctions),
  119. #if IL2CPP_TARGET_WINRT // Win8+, plus needs to be looked up dynamically on Xbox One
  120. HARDCODED_DEPENDENCY_LIBRARY(L"wintypes", kWinTypesFunctions),
  121. HARDCODED_DEPENDENCY_LIBRARY(L"bcrypt", kBCryptFunctions),
  122. #endif
  123. #endif
  124. };
  125. const HardcodedPInvokeDependencyLibrary* LibraryLoader::HardcodedPInvokeDependencies = kHardcodedPInvokeDependencies;
  126. const size_t LibraryLoader::HardcodedPInvokeDependenciesCount = ARRAYSIZE(kHardcodedPInvokeDependencies);
  127. Baselib_DynamicLibrary_Handle LibraryLoader::ProbeForLibrary(const Il2CppNativeChar* libraryName, const size_t /*libraryNameLength*/, std::string& detailedError)
  128. {
  129. return TryOpeningLibrary(libraryName, detailedError);
  130. }
  131. Baselib_DynamicLibrary_Handle LibraryLoader::OpenProgramHandle(Baselib_ErrorState& errorState, bool& needsClosing)
  132. {
  133. needsClosing = false;
  134. return Baselib_DynamicLibrary_FromNativeHandle(reinterpret_cast<uint64_t>(Image::GetImageBase()), Baselib_DynamicLibrary_WinApiHMODULE, &errorState);
  135. }
  136. bool LibraryLoader::EntryNameMatches(const il2cpp::utils::StringView<char>& hardcodedEntryPoint, const il2cpp::utils::StringView<char>& entryPoint)
  137. {
  138. // Handle windows mapping generic to unicode methods. e.g. MoveFileEx -> MoveFileExW
  139. if (hardcodedEntryPoint.Length() == entryPoint.Length() || (hardcodedEntryPoint.Length() - 1 == entryPoint.Length() && hardcodedEntryPoint[hardcodedEntryPoint.Length() - 1] == 'W'))
  140. {
  141. return strncmp(hardcodedEntryPoint.Str(), entryPoint.Str(), entryPoint.Length()) == 0;
  142. }
  143. return false;
  144. }
  145. }
  146. }
  147. #endif