1
0

mptLibrary.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  1. /*
  2. * mptLibrary.cpp
  3. * --------------
  4. * Purpose: Shared library handling.
  5. * Notes : (currently none)
  6. * Authors: OpenMPT Devs
  7. * The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
  8. */
  9. #include "stdafx.h"
  10. #include "mptLibrary.h"
  11. #include "mpt/osinfo/windows_version.hpp"
  12. #if MPT_OS_WINDOWS
  13. #include <windows.h>
  14. #elif MPT_OS_ANDROID
  15. #include <dlfcn.h>
  16. #elif defined(MPT_WITH_LTDL)
  17. #include <ltdl.h>
  18. #elif defined(MPT_WITH_DL)
  19. #include <dlfcn.h>
  20. #endif
  21. OPENMPT_NAMESPACE_BEGIN
  22. namespace mpt
  23. {
  24. #if MPT_OS_WINDOWS
  25. // KB2533623 / Win8
  26. #ifndef LOAD_LIBRARY_SEARCH_DEFAULT_DIRS
  27. #define LOAD_LIBRARY_SEARCH_DEFAULT_DIRS 0x00001000
  28. #endif
  29. #ifndef LOAD_LIBRARY_SEARCH_APPLICATION_DIR
  30. #define LOAD_LIBRARY_SEARCH_APPLICATION_DIR 0x00000200
  31. #endif
  32. #ifndef LOAD_LIBRARY_SEARCH_SYSTEM32
  33. #define LOAD_LIBRARY_SEARCH_SYSTEM32 0x00000800
  34. #endif
  35. #ifndef LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR
  36. #define LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR 0x00000100
  37. #endif
  38. class LibraryHandle
  39. {
  40. private:
  41. HMODULE hModule;
  42. public:
  43. LibraryHandle(const mpt::LibraryPath &path)
  44. : hModule(NULL)
  45. {
  46. #if MPT_OS_WINDOWS_WINRT
  47. #if (_WIN32_WINNT < 0x0602)
  48. (void)path;
  49. hModule = NULL; // unsupported
  50. #else
  51. switch(path.GetSearchPath())
  52. {
  53. case mpt::LibrarySearchPath::Default:
  54. hModule = LoadPackagedLibrary(path.GetFileName().AsNative().c_str(), 0);
  55. break;
  56. case mpt::LibrarySearchPath::Application:
  57. hModule = LoadPackagedLibrary(path.GetFileName().AsNative().c_str(), 0);
  58. break;
  59. case mpt::LibrarySearchPath::System:
  60. hModule = NULL; // Only application packaged libraries can be loaded dynamically in WinRT
  61. break;
  62. case mpt::LibrarySearchPath::FullPath:
  63. hModule = NULL; // Absolute path is not supported in WinRT
  64. break;
  65. case mpt::LibrarySearchPath::Invalid:
  66. MPT_ASSERT_NOTREACHED();
  67. break;
  68. }
  69. #endif
  70. #else // !MPT_OS_WINDOWS_WINRT
  71. #if (_WIN32_WINNT >= 0x0602)
  72. bool hasKB2533623 = true;
  73. #else
  74. // Check for KB2533623:
  75. bool hasKB2533623 = false;
  76. mpt::osinfo::windows::Version WindowsVersion = mpt::osinfo::windows::Version::Current();
  77. if(WindowsVersion.IsAtLeast(mpt::osinfo::windows::Version::Win8))
  78. {
  79. hasKB2533623 = true;
  80. } else if(WindowsVersion.IsAtLeast(mpt::osinfo::windows::Version::WinVista))
  81. {
  82. HMODULE hKernel32DLL = LoadLibrary(TEXT("kernel32.dll"));
  83. if(hKernel32DLL)
  84. {
  85. if(::GetProcAddress(hKernel32DLL, "SetDefaultDllDirectories") != nullptr)
  86. {
  87. hasKB2533623 = true;
  88. }
  89. FreeLibrary(hKernel32DLL);
  90. hKernel32DLL = NULL;
  91. }
  92. }
  93. #endif
  94. MPT_MAYBE_CONSTANT_IF(hasKB2533623)
  95. {
  96. switch(path.GetSearchPath())
  97. {
  98. case mpt::LibrarySearchPath::Default:
  99. hModule = LoadLibraryEx(path.GetFileName().AsNative().c_str(), NULL, LOAD_LIBRARY_SEARCH_DEFAULT_DIRS);
  100. break;
  101. case mpt::LibrarySearchPath::System:
  102. hModule = LoadLibraryEx(path.GetFileName().AsNative().c_str(), NULL, LOAD_LIBRARY_SEARCH_SYSTEM32);
  103. break;
  104. #if defined(MODPLUG_TRACKER)
  105. // Using restricted search paths applies to potential DLL dependencies
  106. // recursively.
  107. // This fails loading for e.g. Codec or Plugin DLLs in application
  108. // directory if they depend on the MSVC C or C++ runtime (which is
  109. // located in the system directory).
  110. // Just rely on the default search path here.
  111. case mpt::LibrarySearchPath::Application:
  112. {
  113. const mpt::PathString dllPath = mpt::GetExecutablePath();
  114. if(!dllPath.empty() && mpt::PathIsAbsolute(dllPath) && dllPath.IsDirectory())
  115. {
  116. hModule = LoadLibrary((dllPath + path.GetFileName()).AsNative().c_str());
  117. }
  118. }
  119. break;
  120. case mpt::LibrarySearchPath::FullPath:
  121. hModule = LoadLibrary(path.GetFileName().AsNative().c_str());
  122. break;
  123. #else
  124. // For libopenmpt, do the safe thing.
  125. case mpt::LibrarySearchPath::Application:
  126. hModule = LoadLibraryEx(path.GetFileName().AsNative().c_str(), NULL, LOAD_LIBRARY_SEARCH_APPLICATION_DIR);
  127. break;
  128. case mpt::LibrarySearchPath::FullPath:
  129. hModule = LoadLibraryEx(path.GetFileName().AsNative().c_str(), NULL, LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR);
  130. break;
  131. #endif
  132. case mpt::LibrarySearchPath::Invalid:
  133. MPT_ASSERT_NOTREACHED();
  134. break;
  135. }
  136. } else
  137. {
  138. switch(path.GetSearchPath())
  139. {
  140. case mpt::LibrarySearchPath::Default:
  141. hModule = LoadLibrary(path.GetFileName().AsNative().c_str());
  142. break;
  143. case mpt::LibrarySearchPath::Application:
  144. {
  145. const mpt::PathString dllPath = mpt::GetExecutablePath();
  146. if(!dllPath.empty() && mpt::PathIsAbsolute(dllPath) && dllPath.IsDirectory())
  147. {
  148. hModule = LoadLibrary((dllPath + path.GetFileName()).AsNative().c_str());
  149. }
  150. }
  151. break;
  152. case mpt::LibrarySearchPath::System:
  153. {
  154. const mpt::PathString dllPath = mpt::GetSystemPath();
  155. if(!dllPath.empty() && mpt::PathIsAbsolute(dllPath) && dllPath.IsDirectory())
  156. {
  157. hModule = LoadLibrary((dllPath + path.GetFileName()).AsNative().c_str());
  158. }
  159. }
  160. break;
  161. case mpt::LibrarySearchPath::FullPath:
  162. hModule = LoadLibrary(path.GetFileName().AsNative().c_str());
  163. break;
  164. case mpt::LibrarySearchPath::Invalid:
  165. MPT_ASSERT_NOTREACHED();
  166. break;
  167. }
  168. }
  169. #endif // MPT_OS_WINDOWS_WINRT
  170. }
  171. LibraryHandle(const LibraryHandle &) = delete;
  172. LibraryHandle & operator=(const LibraryHandle &) = delete;
  173. ~LibraryHandle()
  174. {
  175. if(IsValid())
  176. {
  177. FreeLibrary(hModule);
  178. }
  179. hModule = NULL;
  180. }
  181. public:
  182. bool IsValid() const
  183. {
  184. return (hModule != NULL);
  185. }
  186. FuncPtr GetProcAddress(const std::string &symbol) const
  187. {
  188. if(!IsValid())
  189. {
  190. return nullptr;
  191. }
  192. return reinterpret_cast<FuncPtr>(::GetProcAddress(hModule, symbol.c_str()));
  193. }
  194. };
  195. #elif MPT_OS_ANDROID
  196. // Fake implementation.
  197. // Load shared objects from the JAVA side of things.
  198. class LibraryHandle
  199. {
  200. public:
  201. LibraryHandle(const mpt::LibraryPath &path)
  202. {
  203. return;
  204. }
  205. LibraryHandle(const LibraryHandle &) = delete;
  206. LibraryHandle & operator=(const LibraryHandle &) = delete;
  207. ~LibraryHandle()
  208. {
  209. return;
  210. }
  211. public:
  212. bool IsValid() const
  213. {
  214. return true;
  215. }
  216. FuncPtr GetProcAddress(const std::string &symbol) const
  217. {
  218. if(!IsValid())
  219. {
  220. return nullptr;
  221. }
  222. return reinterpret_cast<FuncPtr>(dlsym(0, symbol.c_str()));
  223. }
  224. };
  225. #elif defined(MPT_WITH_LTDL)
  226. class LibraryHandle
  227. {
  228. private:
  229. bool inited;
  230. lt_dlhandle handle;
  231. public:
  232. LibraryHandle(const mpt::LibraryPath &path)
  233. : inited(false)
  234. , handle(0)
  235. {
  236. if(lt_dlinit() != 0)
  237. {
  238. return;
  239. }
  240. inited = true;
  241. handle = lt_dlopenext(path.GetFileName().AsNative().c_str());
  242. }
  243. LibraryHandle(const LibraryHandle &) = delete;
  244. LibraryHandle & operator=(const LibraryHandle &) = delete;
  245. ~LibraryHandle()
  246. {
  247. if(IsValid())
  248. {
  249. lt_dlclose(handle);
  250. }
  251. handle = 0;
  252. if(inited)
  253. {
  254. lt_dlexit();
  255. inited = false;
  256. }
  257. }
  258. public:
  259. bool IsValid() const
  260. {
  261. return handle != 0;
  262. }
  263. FuncPtr GetProcAddress(const std::string &symbol) const
  264. {
  265. if(!IsValid())
  266. {
  267. return nullptr;
  268. }
  269. return reinterpret_cast<FuncPtr>(lt_dlsym(handle, symbol.c_str()));
  270. }
  271. };
  272. #elif defined(MPT_WITH_DL)
  273. class LibraryHandle
  274. {
  275. private:
  276. void* handle;
  277. public:
  278. LibraryHandle(const mpt::LibraryPath &path)
  279. : handle(NULL)
  280. {
  281. handle = dlopen(path.GetFileName().AsNative().c_str(), RTLD_NOW);
  282. }
  283. LibraryHandle(const LibraryHandle &) = delete;
  284. LibraryHandle & operator=(const LibraryHandle &) = delete;
  285. ~LibraryHandle()
  286. {
  287. if(IsValid())
  288. {
  289. dlclose(handle);
  290. }
  291. handle = NULL;
  292. }
  293. public:
  294. bool IsValid() const
  295. {
  296. return handle != NULL;
  297. }
  298. FuncPtr GetProcAddress(const std::string &symbol) const
  299. {
  300. if(!IsValid())
  301. {
  302. return NULL;
  303. }
  304. return reinterpret_cast<FuncPtr>(dlsym(handle, symbol.c_str()));
  305. }
  306. };
  307. #else // MPT_OS
  308. // dummy implementation
  309. class LibraryHandle
  310. {
  311. public:
  312. LibraryHandle(const mpt::LibraryPath &path)
  313. {
  314. MPT_UNREFERENCED_PARAMETER(path);
  315. return;
  316. }
  317. LibraryHandle(const LibraryHandle &) = delete;
  318. LibraryHandle & operator=(const LibraryHandle &) = delete;
  319. ~LibraryHandle()
  320. {
  321. return;
  322. }
  323. public:
  324. bool IsValid() const
  325. {
  326. return false;
  327. }
  328. FuncPtr GetProcAddress(const std::string &symbol) const
  329. {
  330. MPT_UNREFERENCED_PARAMETER(symbol);
  331. if(!IsValid())
  332. {
  333. return nullptr;
  334. }
  335. return nullptr;
  336. }
  337. };
  338. #endif // MPT_OS
  339. LibraryPath::LibraryPath(mpt::LibrarySearchPath searchPath, const mpt::PathString &fileName)
  340. : searchPath(searchPath)
  341. , fileName(fileName)
  342. {
  343. return;
  344. }
  345. mpt::LibrarySearchPath LibraryPath::GetSearchPath() const
  346. {
  347. return searchPath;
  348. }
  349. mpt::PathString LibraryPath::GetFileName() const
  350. {
  351. return fileName;
  352. }
  353. mpt::PathString LibraryPath::GetDefaultPrefix()
  354. {
  355. #if MPT_OS_WINDOWS
  356. return P_("");
  357. #elif MPT_OS_ANDROID
  358. return P_("lib");
  359. #elif defined(MPT_WITH_LTDL)
  360. return P_("lib");
  361. #elif defined(MPT_WITH_DL)
  362. return P_("lib");
  363. #else
  364. return P_("lib");
  365. #endif
  366. }
  367. mpt::PathString LibraryPath::GetDefaultSuffix()
  368. {
  369. #if MPT_OS_WINDOWS
  370. return P_(".dll");
  371. #elif MPT_OS_ANDROID
  372. return P_(".so");
  373. #elif defined(MPT_WITH_LTDL)
  374. return P_(""); // handled by libltdl
  375. #elif defined(MPT_WITH_DL)
  376. return P_(".so");
  377. #else
  378. return mpt::PathString();
  379. #endif
  380. }
  381. LibraryPath LibraryPath::App(const mpt::PathString &basename)
  382. {
  383. return LibraryPath(mpt::LibrarySearchPath::Application, GetDefaultPrefix() + basename + GetDefaultSuffix());
  384. }
  385. LibraryPath LibraryPath::AppFullName(const mpt::PathString &fullname)
  386. {
  387. return LibraryPath(mpt::LibrarySearchPath::Application, fullname + GetDefaultSuffix());
  388. }
  389. LibraryPath LibraryPath::System(const mpt::PathString &basename)
  390. {
  391. return LibraryPath(mpt::LibrarySearchPath::System, GetDefaultPrefix() + basename + GetDefaultSuffix());
  392. }
  393. LibraryPath LibraryPath::FullPath(const mpt::PathString &path)
  394. {
  395. return LibraryPath(mpt::LibrarySearchPath::FullPath, path);
  396. }
  397. Library::Library()
  398. {
  399. return;
  400. }
  401. Library::Library(const mpt::LibraryPath &path)
  402. {
  403. if(path.GetSearchPath() == mpt::LibrarySearchPath::Invalid)
  404. {
  405. return;
  406. }
  407. if(path.GetFileName().empty())
  408. {
  409. return;
  410. }
  411. m_Handle = std::make_shared<LibraryHandle>(path);
  412. }
  413. void Library::Unload()
  414. {
  415. *this = mpt::Library();
  416. }
  417. bool Library::IsValid() const
  418. {
  419. return m_Handle && m_Handle->IsValid();
  420. }
  421. FuncPtr Library::GetProcAddress(const std::string &symbol) const
  422. {
  423. if(!IsValid())
  424. {
  425. return nullptr;
  426. }
  427. return m_Handle->GetProcAddress(symbol);
  428. }
  429. } // namespace mpt
  430. OPENMPT_NAMESPACE_END