summaryrefslogtreecommitdiff
path: root/Source/cmDynamicLoader.cxx
diff options
context:
space:
mode:
authorBrad King <brad.king@kitware.com>2002-09-11 09:54:03 -0400
committerBrad King <brad.king@kitware.com>2002-09-11 09:54:03 -0400
commitb9db890ebc8a5c900439c23cf7bdaa44721a35c3 (patch)
treed4f75ff12d69c59b976b7849257463f14d289337 /Source/cmDynamicLoader.cxx
parent994e30091436a22c035bc378c525ad90355a9d20 (diff)
downloadcmake-b9db890ebc8a5c900439c23cf7bdaa44721a35c3.tar.gz
ENH: Pointer-to-function to pointer-to-data casts are not even allowed in strict C. Re-implemented this conversion in pure C++ using a casting trick with an extra level of indirection.
Diffstat (limited to 'Source/cmDynamicLoader.cxx')
-rw-r--r--Source/cmDynamicLoader.cxx70
1 files changed, 56 insertions, 14 deletions
diff --git a/Source/cmDynamicLoader.cxx b/Source/cmDynamicLoader.cxx
index 891f4c2928..05431767a8 100644
--- a/Source/cmDynamicLoader.cxx
+++ b/Source/cmDynamicLoader.cxx
@@ -16,20 +16,7 @@
=========================================================================*/
#include "cmDynamicLoader.h"
-extern "C"
-{
-cmDynamicLoaderFunction cmDynamicLoaderGetSymbolAddress(cmLibHandle,
- const char*);
-}
-
-// Dispatch to C implementation.
-cmDynamicLoaderFunction cmDynamicLoader::GetSymbolAddress(cmLibHandle lib,
- const char* sym)
-{
- return cmDynamicLoaderGetSymbolAddress(lib, sym);
-}
-
-// This file is actually 4 different implementations.
+// This file is actually several different implementations.
// 1. HP machines which uses shl_load
// 2. Apple OSX which uses NSLinkModule
// 3. Windows which uses LoadLibrary
@@ -53,6 +40,19 @@ int cmDynamicLoader::CloseLibrary(cmLibHandle lib)
return 0;
}
+cmDynamicLoaderFunction
+cmDynamicLoader::GetSymbolAddress(cmLibHandle lib, const char* sym)
+{
+ void* addr;
+ int status;
+
+ status = shl_findsym (&lib, sym, TYPE_PROCEDURE, &addr);
+ void* result = (status < 0) ? (void*)0 : addr;
+
+ // Hack to cast pointer-to-data to pointer-to-function.
+ return *reinterpret_cast<cmDynamicLoaderFunction*>(&result);
+}
+
const char* cmDynamicLoader::LibPrefix()
{
return "lib";
@@ -92,6 +92,22 @@ int cmDynamicLoader::CloseLibrary(cmLibHandle lib)
return 0;
}
+cmDynamicLoaderFunction
+cmDynamicLoader::GetSymbolAddress(cmLibHandle lib, const char* sym)
+{
+ void *result=0;
+ if(NSIsSymbolNameDefined(sym))
+ {
+ NSSymbol symbol= NSLookupAndBindSymbol(sym);
+ if(symbol)
+ {
+ result = NSAddressOfSymbol(symbol);
+ }
+ }
+ // Hack to cast pointer-to-data to pointer-to-function.
+ return *reinterpret_cast<cmDynamicLoaderFunction*>(&result);
+}
+
const char* cmDynamicLoader::LibPrefix()
{
return "";
@@ -136,6 +152,23 @@ int cmDynamicLoader::CloseLibrary(cmLibHandle lib)
return (int)FreeLibrary(lib);
}
+cmDynamicLoaderFunction
+cmDynamicLoader::GetSymbolAddress(cmLibHandle lib, const char* sym)
+{
+ void* result = 0;
+#ifdef UNICODE
+ wchar_t *wsym = new wchar_t [mbstowcs(NULL, sym, 32000)];
+ mbstowcs(wsym, sym, 32000);
+ void *ret = GetProcAddress(lib, wsym);
+ delete [] wsym;
+ result = ret;
+#else
+ result = GetProcAddress(lib, sym);
+#endif
+ // Hack to cast pointer-to-data to pointer-to-function.
+ return *reinterpret_cast<cmDynamicLoaderFunction*>(&result);
+}
+
const char* cmDynamicLoader::LibPrefix()
{
return "";
@@ -188,6 +221,15 @@ int cmDynamicLoader::CloseLibrary(cmLibHandle lib)
return (int)dlclose(lib);
}
+cmDynamicLoaderFunction
+cmDynamicLoader::GetSymbolAddress(cmLibHandle lib, const char* sym)
+{
+ void* result = dlsym(lib, sym);
+
+ // Hack to cast pointer-to-data to pointer-to-function.
+ return *reinterpret_cast<cmDynamicLoaderFunction*>(&result);
+}
+
const char* cmDynamicLoader::LibPrefix()
{
return "lib";