DynamicLoader.cxx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing#kwsys for details. */
  3. #include "kwsysPrivate.h"
  4. #include KWSYS_HEADER(DynamicLoader.hxx)
  5. #include KWSYS_HEADER(Configure.hxx)
  6. // Work-around CMake dependency scanning limitation. This must
  7. // duplicate the above list of headers.
  8. #if 0
  9. #include "Configure.hxx.in"
  10. #include "DynamicLoader.hxx.in"
  11. #endif
  12. // This file is actually 3 different implementations.
  13. // 1. HP machines which uses shl_load
  14. // 2. Mac OS X 10.2.x and earlier which uses NSLinkModule
  15. // 3. Windows which uses LoadLibrary
  16. // 4. Most unix systems (including Mac OS X 10.3 and later) which use dlopen
  17. // (default) Each part of the ifdef contains a complete implementation for
  18. // the static methods of DynamicLoader.
  19. // ---------------------------------------------------------------
  20. // 1. Implementation for HPUX machines
  21. #ifdef __hpux
  22. #include <dl.h>
  23. #include <errno.h>
  24. #define DYNAMICLOADER_DEFINED 1
  25. namespace KWSYS_NAMESPACE {
  26. //----------------------------------------------------------------------------
  27. DynamicLoader::LibraryHandle DynamicLoader::OpenLibrary(
  28. const std::string& libname)
  29. {
  30. return shl_load(libname.c_str(), BIND_DEFERRED | DYNAMIC_PATH, 0L);
  31. }
  32. //----------------------------------------------------------------------------
  33. int DynamicLoader::CloseLibrary(DynamicLoader::LibraryHandle lib)
  34. {
  35. if (!lib) {
  36. return 0;
  37. }
  38. return !shl_unload(lib);
  39. }
  40. //----------------------------------------------------------------------------
  41. DynamicLoader::SymbolPointer DynamicLoader::GetSymbolAddress(
  42. DynamicLoader::LibraryHandle lib, const std::string& sym)
  43. {
  44. void* addr;
  45. int status;
  46. /* TYPE_PROCEDURE Look for a function or procedure. (This used to be default)
  47. * TYPE_DATA Look for a symbol in the data segment (for example,
  48. * variables).
  49. * TYPE_UNDEFINED Look for any symbol.
  50. */
  51. status = shl_findsym(&lib, sym.c_str(), TYPE_UNDEFINED, &addr);
  52. void* result = (status < 0) ? (void*)0 : addr;
  53. // Hack to cast pointer-to-data to pointer-to-function.
  54. return *reinterpret_cast<DynamicLoader::SymbolPointer*>(&result);
  55. }
  56. const char* DynamicLoader::LastError()
  57. {
  58. // TODO: Need implementation with errno/strerror
  59. /* If successful, shl_findsym returns an integer (int) value zero. If
  60. * shl_findsym cannot find sym, it returns -1 and sets errno to zero.
  61. * If any other errors occur, shl_findsym returns -1 and sets errno to one
  62. * of these values (defined in <errno.h>):
  63. * ENOEXEC
  64. * A format error was detected in the specified library.
  65. * ENOSYM
  66. * A symbol on which sym depends could not be found.
  67. * EINVAL
  68. * The specified handle is invalid.
  69. */
  70. if (errno == ENOEXEC || errno == ENOSYM || errno == EINVAL) {
  71. return strerror(errno);
  72. }
  73. // else
  74. return 0;
  75. }
  76. } // namespace KWSYS_NAMESPACE
  77. #endif //__hpux
  78. // ---------------------------------------------------------------
  79. // 2. Implementation for Mac OS X 10.2.x and earlier
  80. #ifdef __APPLE__
  81. #if MAC_OS_X_VERSION_MAX_ALLOWED < 1030
  82. #include <mach-o/dyld.h>
  83. #include <string.h> // for strlen
  84. #define DYNAMICLOADER_DEFINED 1
  85. namespace KWSYS_NAMESPACE {
  86. //----------------------------------------------------------------------------
  87. DynamicLoader::LibraryHandle DynamicLoader::OpenLibrary(
  88. const std::string& libname)
  89. {
  90. NSObjectFileImageReturnCode rc;
  91. NSObjectFileImage image = 0;
  92. rc = NSCreateObjectFileImageFromFile(libname.c_str(), &image);
  93. // rc == NSObjectFileImageInappropriateFile when trying to load a dylib file
  94. if (rc != NSObjectFileImageSuccess) {
  95. return 0;
  96. }
  97. NSModule handle =
  98. NSLinkModule(image, libname.c_str(), NSLINKMODULE_OPTION_BINDNOW |
  99. NSLINKMODULE_OPTION_RETURN_ON_ERROR);
  100. NSDestroyObjectFileImage(image);
  101. return handle;
  102. }
  103. //----------------------------------------------------------------------------
  104. int DynamicLoader::CloseLibrary(DynamicLoader::LibraryHandle lib)
  105. {
  106. // NSUNLINKMODULE_OPTION_KEEP_MEMORY_MAPPED
  107. // With this option the memory for the module is not deallocated
  108. // allowing pointers into the module to still be valid.
  109. // You should use this option instead if your code experience some problems
  110. // reported against Panther 10.3.9 (fixed in Tiger 10.4.2 and up)
  111. bool success = NSUnLinkModule(lib, NSUNLINKMODULE_OPTION_NONE);
  112. return success;
  113. }
  114. //----------------------------------------------------------------------------
  115. DynamicLoader::SymbolPointer DynamicLoader::GetSymbolAddress(
  116. DynamicLoader::LibraryHandle lib, const std::string& sym)
  117. {
  118. void* result = 0;
  119. // Need to prepend symbols with '_' on Apple-gcc compilers
  120. size_t len = sym.size();
  121. char* rsym = new char[len + 1 + 1];
  122. strcpy(rsym, "_");
  123. strcat(rsym + 1, sym.c_str());
  124. NSSymbol symbol = NSLookupSymbolInModule(lib, rsym);
  125. if (symbol) {
  126. result = NSAddressOfSymbol(symbol);
  127. }
  128. delete[] rsym;
  129. // Hack to cast pointer-to-data to pointer-to-function.
  130. return *reinterpret_cast<DynamicLoader::SymbolPointer*>(&result);
  131. }
  132. //----------------------------------------------------------------------------
  133. const char* DynamicLoader::LastError()
  134. {
  135. return 0;
  136. }
  137. } // namespace KWSYS_NAMESPACE
  138. #endif // MAC_OS_X_VERSION_MAX_ALLOWED < 1030
  139. #endif // __APPLE__
  140. // ---------------------------------------------------------------
  141. // 3. Implementation for Windows win32 code but not cygwin
  142. #if defined(_WIN32) && !defined(__CYGWIN__)
  143. #include <windows.h>
  144. #define DYNAMICLOADER_DEFINED 1
  145. namespace KWSYS_NAMESPACE {
  146. //----------------------------------------------------------------------------
  147. DynamicLoader::LibraryHandle DynamicLoader::OpenLibrary(
  148. const std::string& libname)
  149. {
  150. DynamicLoader::LibraryHandle lh;
  151. int length = MultiByteToWideChar(CP_UTF8, 0, libname.c_str(), -1, NULL, 0);
  152. wchar_t* wchars = new wchar_t[length + 1];
  153. wchars[0] = '\0';
  154. MultiByteToWideChar(CP_UTF8, 0, libname.c_str(), -1, wchars, length);
  155. lh = LoadLibraryW(wchars);
  156. delete[] wchars;
  157. return lh;
  158. }
  159. //----------------------------------------------------------------------------
  160. int DynamicLoader::CloseLibrary(DynamicLoader::LibraryHandle lib)
  161. {
  162. return (int)FreeLibrary(lib);
  163. }
  164. //----------------------------------------------------------------------------
  165. DynamicLoader::SymbolPointer DynamicLoader::GetSymbolAddress(
  166. DynamicLoader::LibraryHandle lib, const std::string& sym)
  167. {
  168. // TODO: The calling convention affects the name of the symbol. We
  169. // should have a tool to help get the symbol with the desired
  170. // calling convention. Currently we assume cdecl.
  171. //
  172. // Borland:
  173. // __cdecl = "_func" (default)
  174. // __fastcall = "@_func"
  175. // __stdcall = "func"
  176. //
  177. // Watcom:
  178. // __cdecl = "_func"
  179. // __fastcall = "@_func@X"
  180. // __stdcall = "_func@X"
  181. // __watcall = "func_" (default)
  182. //
  183. // MSVC:
  184. // __cdecl = "func" (default)
  185. // __fastcall = "@_func@X"
  186. // __stdcall = "_func@X"
  187. //
  188. // Note that the "@X" part of the name above is the total size (in
  189. // bytes) of the arguments on the stack.
  190. void* result;
  191. #if defined(__BORLANDC__) || defined(__WATCOMC__)
  192. // Need to prepend symbols with '_'
  193. size_t len = sym.size();
  194. char* rsym = new char[len + 1 + 1];
  195. strcpy(rsym, "_");
  196. strcat(rsym, sym.c_str());
  197. #else
  198. const char* rsym = sym.c_str();
  199. #endif
  200. result = (void*)GetProcAddress(lib, rsym);
  201. #if defined(__BORLANDC__) || defined(__WATCOMC__)
  202. delete[] rsym;
  203. #endif
  204. // Hack to cast pointer-to-data to pointer-to-function.
  205. #ifdef __WATCOMC__
  206. return *(DynamicLoader::SymbolPointer*)(&result);
  207. #else
  208. return *reinterpret_cast<DynamicLoader::SymbolPointer*>(&result);
  209. #endif
  210. }
  211. //----------------------------------------------------------------------------
  212. const char* DynamicLoader::LastError()
  213. {
  214. LPVOID lpMsgBuf = NULL;
  215. FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
  216. NULL, GetLastError(),
  217. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
  218. (LPTSTR)&lpMsgBuf, 0, NULL);
  219. if (!lpMsgBuf) {
  220. return NULL;
  221. }
  222. static char* str = 0;
  223. delete[] str;
  224. str = strcpy(new char[strlen((char*)lpMsgBuf) + 1], (char*)lpMsgBuf);
  225. // Free the buffer.
  226. LocalFree(lpMsgBuf);
  227. return str;
  228. }
  229. } // namespace KWSYS_NAMESPACE
  230. #endif //_WIN32
  231. // ---------------------------------------------------------------
  232. // 4. Implementation for BeOS
  233. #if defined __BEOS__
  234. #include <string.h> // for strerror()
  235. #include <be/kernel/image.h>
  236. #include <be/support/Errors.h>
  237. #define DYNAMICLOADER_DEFINED 1
  238. namespace KWSYS_NAMESPACE {
  239. static image_id last_dynamic_err = B_OK;
  240. //----------------------------------------------------------------------------
  241. DynamicLoader::LibraryHandle DynamicLoader::OpenLibrary(
  242. const std::string& libname)
  243. {
  244. // image_id's are integers, errors are negative. Add one just in case we
  245. // get a valid image_id of zero (is that even possible?).
  246. image_id rc = load_add_on(libname.c_str());
  247. if (rc < 0) {
  248. last_dynamic_err = rc;
  249. return 0;
  250. }
  251. return rc + 1;
  252. }
  253. //----------------------------------------------------------------------------
  254. int DynamicLoader::CloseLibrary(DynamicLoader::LibraryHandle lib)
  255. {
  256. if (!lib) {
  257. last_dynamic_err = B_BAD_VALUE;
  258. return 0;
  259. } else {
  260. // The function dlclose() returns 0 on success, and non-zero on error.
  261. status_t rc = unload_add_on(lib - 1);
  262. if (rc != B_OK) {
  263. last_dynamic_err = rc;
  264. return 0;
  265. }
  266. }
  267. return 1;
  268. }
  269. //----------------------------------------------------------------------------
  270. DynamicLoader::SymbolPointer DynamicLoader::GetSymbolAddress(
  271. DynamicLoader::LibraryHandle lib, const std::string& sym)
  272. {
  273. // Hack to cast pointer-to-data to pointer-to-function.
  274. union
  275. {
  276. void* pvoid;
  277. DynamicLoader::SymbolPointer psym;
  278. } result;
  279. result.psym = NULL;
  280. if (!lib) {
  281. last_dynamic_err = B_BAD_VALUE;
  282. } else {
  283. // !!! FIXME: BeOS can do function-only lookups...does this ever
  284. // !!! FIXME: actually _want_ a data symbol lookup, or was this union
  285. // !!! FIXME: a leftover of dlsym()? (s/ANY/TEXT for functions only).
  286. status_t rc =
  287. get_image_symbol(lib - 1, sym.c_str(), B_SYMBOL_TYPE_ANY, &result.pvoid);
  288. if (rc != B_OK) {
  289. last_dynamic_err = rc;
  290. result.psym = NULL;
  291. }
  292. }
  293. return result.psym;
  294. }
  295. //----------------------------------------------------------------------------
  296. const char* DynamicLoader::LastError()
  297. {
  298. const char* retval = strerror(last_dynamic_err);
  299. last_dynamic_err = B_OK;
  300. return retval;
  301. }
  302. } // namespace KWSYS_NAMESPACE
  303. #endif
  304. // ---------------------------------------------------------------
  305. // 5. Implementation for systems without dynamic libs
  306. // __gnu_blrts__ is IBM BlueGene/L
  307. // __LIBCATAMOUNT__ is defined on Catamount on Cray compute nodes
  308. #if defined(__gnu_blrts__) || defined(__LIBCATAMOUNT__) || \
  309. defined(__CRAYXT_COMPUTE_LINUX_TARGET)
  310. #include <string.h> // for strerror()
  311. #define DYNAMICLOADER_DEFINED 1
  312. namespace KWSYS_NAMESPACE {
  313. //----------------------------------------------------------------------------
  314. DynamicLoader::LibraryHandle DynamicLoader::OpenLibrary(
  315. const std::string& libname)
  316. {
  317. return 0;
  318. }
  319. //----------------------------------------------------------------------------
  320. int DynamicLoader::CloseLibrary(DynamicLoader::LibraryHandle lib)
  321. {
  322. if (!lib) {
  323. return 0;
  324. }
  325. return 1;
  326. }
  327. //----------------------------------------------------------------------------
  328. DynamicLoader::SymbolPointer DynamicLoader::GetSymbolAddress(
  329. DynamicLoader::LibraryHandle lib, const std::string& sym)
  330. {
  331. return 0;
  332. }
  333. //----------------------------------------------------------------------------
  334. const char* DynamicLoader::LastError()
  335. {
  336. return "General error";
  337. }
  338. } // namespace KWSYS_NAMESPACE
  339. #endif
  340. #ifdef __MINT__
  341. #define DYNAMICLOADER_DEFINED 1
  342. #define _GNU_SOURCE /* for program_invocation_name */
  343. #include <dld.h>
  344. #include <errno.h>
  345. #include <malloc.h>
  346. #include <string.h>
  347. namespace KWSYS_NAMESPACE {
  348. //----------------------------------------------------------------------------
  349. DynamicLoader::LibraryHandle DynamicLoader::OpenLibrary(
  350. const std::string& libname)
  351. {
  352. char* name = (char*)calloc(1, libname.size() + 1);
  353. dld_init(program_invocation_name);
  354. strncpy(name, libname.c_str(), libname.size());
  355. dld_link(libname.c_str());
  356. return (void*)name;
  357. }
  358. //----------------------------------------------------------------------------
  359. int DynamicLoader::CloseLibrary(DynamicLoader::LibraryHandle lib)
  360. {
  361. dld_unlink_by_file((char*)lib, 0);
  362. free(lib);
  363. return 0;
  364. }
  365. //----------------------------------------------------------------------------
  366. DynamicLoader::SymbolPointer DynamicLoader::GetSymbolAddress(
  367. DynamicLoader::LibraryHandle lib, const std::string& sym)
  368. {
  369. // Hack to cast pointer-to-data to pointer-to-function.
  370. union
  371. {
  372. void* pvoid;
  373. DynamicLoader::SymbolPointer psym;
  374. } result;
  375. result.pvoid = dld_get_symbol(sym.c_str());
  376. return result.psym;
  377. }
  378. //----------------------------------------------------------------------------
  379. const char* DynamicLoader::LastError()
  380. {
  381. return dld_strerror(dld_errno);
  382. }
  383. } // namespace KWSYS_NAMESPACE
  384. #endif
  385. // ---------------------------------------------------------------
  386. // 6. Implementation for default UNIX machines.
  387. // if nothing has been defined then use this
  388. #ifndef DYNAMICLOADER_DEFINED
  389. #define DYNAMICLOADER_DEFINED 1
  390. // Setup for most unix machines
  391. #include <dlfcn.h>
  392. namespace KWSYS_NAMESPACE {
  393. //----------------------------------------------------------------------------
  394. DynamicLoader::LibraryHandle DynamicLoader::OpenLibrary(
  395. const std::string& libname)
  396. {
  397. return dlopen(libname.c_str(), RTLD_LAZY);
  398. }
  399. //----------------------------------------------------------------------------
  400. int DynamicLoader::CloseLibrary(DynamicLoader::LibraryHandle lib)
  401. {
  402. if (lib) {
  403. // The function dlclose() returns 0 on success, and non-zero on error.
  404. return !dlclose(lib);
  405. }
  406. // else
  407. return 0;
  408. }
  409. //----------------------------------------------------------------------------
  410. DynamicLoader::SymbolPointer DynamicLoader::GetSymbolAddress(
  411. DynamicLoader::LibraryHandle lib, const std::string& sym)
  412. {
  413. // Hack to cast pointer-to-data to pointer-to-function.
  414. union
  415. {
  416. void* pvoid;
  417. DynamicLoader::SymbolPointer psym;
  418. } result;
  419. result.pvoid = dlsym(lib, sym.c_str());
  420. return result.psym;
  421. }
  422. //----------------------------------------------------------------------------
  423. const char* DynamicLoader::LastError()
  424. {
  425. return dlerror();
  426. }
  427. } // namespace KWSYS_NAMESPACE
  428. #endif