DynamicLoader.cxx 15 KB

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