LibraryLoader.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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(MultiByteToWideChar),
  57. HARDCODED_DEPENDENCY_FUNCTION(RemoveDirectoryW),
  58. HARDCODED_DEPENDENCY_FUNCTION(ReplaceFileW),
  59. HARDCODED_DEPENDENCY_FUNCTION(SetFileAttributesW),
  60. HARDCODED_DEPENDENCY_FUNCTION(SetFileInformationByHandle),
  61. HARDCODED_DEPENDENCY_FUNCTION(GetFileInformationByHandleEx),
  62. // The CopyFile2 method is only required by the class library code for UWP builds.
  63. // It does not exist in Windows 7, so we don't want to use it for Windows Desktop
  64. // builds, since they still support Windows 7.
  65. #if !IL2CPP_TARGET_WINDOWS_DESKTOP
  66. HARDCODED_DEPENDENCY_FUNCTION(CopyFile2),
  67. #endif
  68. #if WINDOWS_SDK_BUILD_VERSION >= 16299
  69. HARDCODED_DEPENDENCY_FUNCTION(SetThreadErrorMode),
  70. HARDCODED_DEPENDENCY_FUNCTION(CopyFileExW),
  71. HARDCODED_DEPENDENCY_FUNCTION(DeleteVolumeMountPointW),
  72. HARDCODED_DEPENDENCY_FUNCTION(GetLogicalDrives),
  73. #endif
  74. };
  75. #if IL2CPP_TARGET_WINRT || IL2CPP_TARGET_WINDOWS_GAMES
  76. const HardcodedPInvokeDependencyFunction kBCryptFunctions[] =
  77. {
  78. HARDCODED_DEPENDENCY_FUNCTION(BCryptGenRandom),
  79. };
  80. #endif
  81. const HardcodedPInvokeDependencyFunction kiphlpapiFunctions[] =
  82. {
  83. HARDCODED_DEPENDENCY_FUNCTION(GetNetworkParams),
  84. #if !IL2CPP_TARGET_XBOXONE
  85. HARDCODED_DEPENDENCY_FUNCTION(GetAdaptersAddresses),
  86. HARDCODED_DEPENDENCY_FUNCTION(GetIfEntry),
  87. #endif
  88. };
  89. #if !IL2CPP_TARGET_WINDOWS_DESKTOP && !IL2CPP_TARGET_WINDOWS_GAMES
  90. const HardcodedPInvokeDependencyFunction kTimezoneFunctions[] =
  91. {
  92. #if !IL2CPP_TARGET_XBOXONE
  93. HARDCODED_DEPENDENCY_FUNCTION(EnumDynamicTimeZoneInformation),
  94. #endif
  95. HARDCODED_DEPENDENCY_FUNCTION(GetDynamicTimeZoneInformation),
  96. #if !IL2CPP_TARGET_XBOXONE
  97. HARDCODED_DEPENDENCY_FUNCTION(GetDynamicTimeZoneInformationEffectiveYears),
  98. #endif
  99. HARDCODED_DEPENDENCY_FUNCTION(GetTimeZoneInformationForYear),
  100. };
  101. #endif
  102. #if IL2CPP_TARGET_WINRT
  103. const HardcodedPInvokeDependencyFunction kWinTypesFunctions[] =
  104. {
  105. HARDCODED_DEPENDENCY_FUNCTION(RoGetBufferMarshaler)
  106. };
  107. #endif
  108. // All these come without ".dll" extension!
  109. const HardcodedPInvokeDependencyLibrary kHardcodedPInvokeDependencies[] =
  110. {
  111. #if IL2CPP_TARGET_WINDOWS_GAMES
  112. HARDCODED_DEPENDENCY_LIBRARY(L"bcrypt", kBCryptFunctions),
  113. #else
  114. #if !IL2CPP_TARGET_WINDOWS_DESKTOP // Some of these functions are win8+s
  115. HARDCODED_DEPENDENCY_LIBRARY(L"advapi32", kAdvapiFunctions),
  116. HARDCODED_DEPENDENCY_LIBRARY(L"api-ms-win-core-timezone-l1-1-0", kTimezoneFunctions),
  117. #endif
  118. HARDCODED_DEPENDENCY_LIBRARY(L"kernel32", kKernel32Functions),
  119. HARDCODED_DEPENDENCY_LIBRARY(L"iphlpapi", kiphlpapiFunctions),
  120. #if IL2CPP_TARGET_WINRT // Win8+, plus needs to be looked up dynamically on Xbox One
  121. HARDCODED_DEPENDENCY_LIBRARY(L"wintypes", kWinTypesFunctions),
  122. HARDCODED_DEPENDENCY_LIBRARY(L"bcrypt", kBCryptFunctions),
  123. #endif
  124. #endif
  125. };
  126. const HardcodedPInvokeDependencyLibrary* LibraryLoader::HardcodedPInvokeDependencies = kHardcodedPInvokeDependencies;
  127. const size_t LibraryLoader::HardcodedPInvokeDependenciesCount = ARRAYSIZE(kHardcodedPInvokeDependencies);
  128. Baselib_DynamicLibrary_Handle LibraryLoader::ProbeForLibrary(const Il2CppNativeChar* libraryName, const size_t /*libraryNameLength*/, std::string& detailedError)
  129. {
  130. return TryOpeningLibrary(libraryName, detailedError);
  131. }
  132. Baselib_DynamicLibrary_Handle LibraryLoader::OpenProgramHandle(Baselib_ErrorState& errorState, bool& needsClosing)
  133. {
  134. needsClosing = false;
  135. return Baselib_DynamicLibrary_FromNativeHandle(reinterpret_cast<uint64_t>(Image::GetImageBase()), Baselib_DynamicLibrary_WinApiHMODULE, &errorState);
  136. }
  137. bool LibraryLoader::EntryNameMatches(const il2cpp::utils::StringView<char>& hardcodedEntryPoint, const il2cpp::utils::StringView<char>& entryPoint)
  138. {
  139. // Handle windows mapping generic to unicode methods. e.g. MoveFileEx -> MoveFileExW
  140. if (hardcodedEntryPoint.Length() == entryPoint.Length() || (hardcodedEntryPoint.Length() - 1 == entryPoint.Length() && hardcodedEntryPoint[hardcodedEntryPoint.Length() - 1] == 'W'))
  141. {
  142. return strncmp(hardcodedEntryPoint.Str(), entryPoint.Str(), entryPoint.Length()) == 0;
  143. }
  144. return false;
  145. }
  146. }
  147. }
  148. #endif