Commit 8b63c5a7 authored by Andy Cedilnik's avatar Andy Cedilnik

ENH: Cleanup DynamicLoader so that the symbols have more consistent names,...

ENH: Cleanup DynamicLoader so that the symbols have more consistent names, start using dynamic loader from kwsys in CMake
parent 917d98f5
......@@ -37,8 +37,8 @@
// 1. HP machines which uses shl_load
// 2. Mac OS X 10.2.x and earlier which uses NSLinkModule
// 3. Windows which uses LoadLibrary
// 4. Most unix systems (including Mac OS X 10.3 and later) which use dlopen (default)
// Each part of the ifdef contains a complete implementation for
// 4. Most unix systems (including Mac OS X 10.3 and later) which use dlopen
// (default) Each part of the ifdef contains a complete implementation for
// the static methods of DynamicLoader.
namespace KWSYS_NAMESPACE
......@@ -66,25 +66,25 @@ namespace KWSYS_NAMESPACE
{
//----------------------------------------------------------------------------
LibHandle DynamicLoader::OpenLibrary(const char* libname )
DynamicLoader::LibraryHandle DynamicLoader::OpenLibrary(const char* libname )
{
return shl_load(libname, BIND_DEFERRED | DYNAMIC_PATH, 0L);
}
//----------------------------------------------------------------------------
int DynamicLoader::CloseLibrary(LibHandle lib)
int DynamicLoader::CloseLibrary(DynamicLoader::LibraryHandle lib)
{
return !shl_unload(lib);
}
//----------------------------------------------------------------------------
DynamicLoaderFunction
DynamicLoader::GetSymbolAddress(LibHandle lib, const char* sym)
DynamicLoader::SymbolPointer
DynamicLoader::GetSymbolAddress(DynamicLoader::LibraryHandle lib, const char* sym)
{
void* addr;
int status;
/* TYPE_PROCEDURE Look for a function or procedure.
/* TYPE_PROCEDURE Look for a function or procedure. (This used to be default)
* TYPE_DATA Look for a symbol in the data segment (for example, variables).
* TYPE_UNDEFINED Look for any symbol.
*/
......@@ -92,7 +92,7 @@ DynamicLoader::GetSymbolAddress(LibHandle lib, const char* sym)
void* result = (status < 0) ? (void*)0 : addr;
// Hack to cast pointer-to-data to pointer-to-function.
return *reinterpret_cast<DynamicLoaderFunction*>(&result);
return *reinterpret_cast<DynamicLoader::SymbolPointer*>(&result);
}
//----------------------------------------------------------------------------
......@@ -149,7 +149,7 @@ namespace KWSYS_NAMESPACE
{
//----------------------------------------------------------------------------
LibHandle DynamicLoader::OpenLibrary(const char* libname )
DynamicLoader::LibraryHandle DynamicLoader::OpenLibrary(const char* libname )
{
NSObjectFileImageReturnCode rc;
NSObjectFileImage image = 0;
......@@ -167,7 +167,7 @@ LibHandle DynamicLoader::OpenLibrary(const char* libname )
}
//----------------------------------------------------------------------------
int DynamicLoader::CloseLibrary( LibHandle lib)
int DynamicLoader::CloseLibrary( DynamicLoader::LibraryHandle lib)
{
// NSUNLINKMODULE_OPTION_KEEP_MEMORY_MAPPED
// With this option the memory for the module is not deallocated
......@@ -179,7 +179,8 @@ int DynamicLoader::CloseLibrary( LibHandle lib)
}
//----------------------------------------------------------------------------
DynamicLoaderFunction DynamicLoader::GetSymbolAddress(LibHandle lib, const char* sym)
DynamicLoader::SymbolPointer DynamicLoader::GetSymbolAddress(
DynamicLoader::LibraryHandle lib, const char* sym)
{
void *result=0;
// Need to prepend symbols with '_' on Apple-gcc compilers
......@@ -196,7 +197,7 @@ DynamicLoaderFunction DynamicLoader::GetSymbolAddress(LibHandle lib, const char*
delete[] rsym;
// Hack to cast pointer-to-data to pointer-to-function.
return *reinterpret_cast<DynamicLoaderFunction*>(&result);
return *reinterpret_cast<DynamicLoader::SymbolPointer*>(&result);
}
//----------------------------------------------------------------------------
......@@ -235,9 +236,9 @@ namespace KWSYS_NAMESPACE
{
//----------------------------------------------------------------------------
LibHandle DynamicLoader::OpenLibrary(const char* libname)
DynamicLoader::LibraryHandle DynamicLoader::OpenLibrary(const char* libname)
{
LibHandle lh;
DynamicLoader::LibraryHandle lh;
#ifdef UNICODE
wchar_t libn[MB_CUR_MAX];
mbstowcs(libn, libname, MB_CUR_MAX);
......@@ -249,13 +250,14 @@ LibHandle DynamicLoader::OpenLibrary(const char* libname)
}
//----------------------------------------------------------------------------
int DynamicLoader::CloseLibrary(LibHandle lib)
int DynamicLoader::CloseLibrary(DynamicLoader::LibraryHandle lib)
{
return (int)FreeLibrary(lib);
}
//----------------------------------------------------------------------------
DynamicLoaderFunction DynamicLoader::GetSymbolAddress(LibHandle lib, const char* sym)
DynamicLoader::SymbolPointer DynamicLoader::GetSymbolAddress(
DynamicLoader::LibraryHandle lib, const char* sym)
{
void *result;
#ifdef __BORLANDC__
......@@ -278,7 +280,7 @@ DynamicLoaderFunction DynamicLoader::GetSymbolAddress(LibHandle lib, const char*
delete[] rsym;
#endif
// Hack to cast pointer-to-data to pointer-to-function.
return *reinterpret_cast<DynamicLoaderFunction*>(&result);
return *reinterpret_cast<DynamicLoader::SymbolPointer*>(&result);
}
//----------------------------------------------------------------------------
......@@ -336,13 +338,13 @@ namespace KWSYS_NAMESPACE
{
//----------------------------------------------------------------------------
LibHandle DynamicLoader::OpenLibrary(const char* libname )
DynamicLoader::LibraryHandle DynamicLoader::OpenLibrary(const char* libname )
{
return dlopen(libname, RTLD_LAZY);
}
//----------------------------------------------------------------------------
int DynamicLoader::CloseLibrary(LibHandle lib)
int DynamicLoader::CloseLibrary(DynamicLoader::LibraryHandle lib)
{
if (lib)
{
......@@ -354,12 +356,13 @@ int DynamicLoader::CloseLibrary(LibHandle lib)
}
//----------------------------------------------------------------------------
DynamicLoaderFunction DynamicLoader::GetSymbolAddress(LibHandle lib, const char* sym)
DynamicLoader::SymbolPointer DynamicLoader::GetSymbolAddress(
DynamicLoader::LibraryHandle lib, const char* sym)
{
void* result = dlsym(lib, sym);
// Hack to cast pointer-to-data to pointer-to-function.
return *reinterpret_cast<DynamicLoaderFunction*>(&result);
return *reinterpret_cast<DynamicLoader::SymbolPointer*>(&result);
}
//----------------------------------------------------------------------------
......
......@@ -16,46 +16,8 @@
#include <@KWSYS_NAMESPACE@/Configure.h>
// Ugly stuff for library handles
// They are different on several different OS's
#if defined(__hpux)
#include <dl.h>
namespace @KWSYS_NAMESPACE@
{
typedef shl_t LibHandle;
} // namespace @KWSYS_NAMESPACE@
#elif defined(_WIN32)
#include <windows.h>
namespace @KWSYS_NAMESPACE@
{
typedef HMODULE LibHandle;
} // namespace @KWSYS_NAMESPACE@
#elif defined(__APPLE__)
#include <AvailabilityMacros.h>
#if MAC_OS_X_VERSION_MIN_REQUIRED < 1030
#include <mach-o/dyld.h>
namespace @KWSYS_NAMESPACE@
{
typedef NSModule LibHandle;
} // namespace @KWSYS_NAMESPACE@
#else
namespace @KWSYS_NAMESPACE@
{
typedef void* LibHandle;
} // namespace @KWSYS_NAMESPACE@
#endif
#else
namespace @KWSYS_NAMESPACE@
{
typedef void* LibHandle;
} // namespace @KWSYS_NAMESPACE@
#endif
namespace @KWSYS_NAMESPACE@
{
// Return type from DynamicLoader::GetSymbolAddress.
typedef void (*DynamicLoaderFunction)();
/** \class DynamicLoader
* \brief Portable loading of dynamic libraries or dll's.
*
......@@ -66,31 +28,54 @@ typedef void (*DynamicLoaderFunction)();
* operating systems
*
* \warning dlopen on *nix system works the following way:
* If filename contains a slash ("/"), then it is interpreted as a (relative or absolute)
* pathname. Otherwise, the dynamic linker searches for the library as follows :
* see ld.so(8) for further details):
* Whereas this distinction does not exist on Win32. Therefore ideally you should be doing
* full path to garantee to have a consistent way of dealing with dynamic loading of shared
* library.
* If filename contains a slash ("/"), then it is interpreted as a (relative
* or absolute) pathname. Otherwise, the dynamic linker searches for the
* library as follows : see ld.so(8) for further details):
* Whereas this distinction does not exist on Win32. Therefore ideally you
* should be doing full path to garantee to have a consistent way of dealing
* with dynamic loading of shared library.
*/
class @KWSYS_NAMESPACE@_EXPORT DynamicLoader
{
public:
// Ugly stuff for library handles
// They are different on several different OS's
#if defined(__hpux)
#include <dl.h>
typedef shl_t LibraryHandle;
#elif defined(_WIN32)
#include <windows.h>
typedef HMODULE LibraryHandle;
#elif defined(__APPLE__)
#include <AvailabilityMacros.h>
#if MAC_OS_X_VERSION_MIN_REQUIRED < 1030
#include <mach-o/dyld.h>
typedef NSModule LibraryHandle;
#else
typedef void* LibraryHandle;
#endif
#else
typedef void* LibraryHandle;
#endif
// Return type from DynamicLoader::GetSymbolAddress.
typedef void (*SymbolPointer)();
DynamicLoader();
~DynamicLoader();
/** Load a dynamic library into the current process.
* The returned LibHandle can be used to access the symbols in the
* The returned LibraryHandle can be used to access the symbols in the
* library. */
static LibHandle OpenLibrary(const char*);
static LibraryHandle OpenLibrary(const char*);
/** Attempt to detach a dynamic library from the
* process. A value of true is returned if it is sucessful. */
static int CloseLibrary(LibHandle);
static int CloseLibrary(LibraryHandle);
/** Find the address of the symbol in the given library. */
static DynamicLoaderFunction GetSymbolAddress(LibHandle, const char*);
static SymbolPointer GetSymbolAddress(LibraryHandle, const char*);
/** Return the library prefix for the given architecture */
static const char* LibPrefix();
......
......@@ -53,7 +53,8 @@ kwsys_stl::string GetLibName(const char* lname)
int TestDynamicLoader(const char* libname, const char* symbol, int r1, int r2, int r3)
{
kwsys_ios::cerr << "Testing: " << libname << kwsys_ios::endl;
kwsys::LibHandle l = kwsys::DynamicLoader::OpenLibrary(libname);
kwsys::DynamicLoader::LibraryHandle l
= kwsys::DynamicLoader::OpenLibrary(libname);
// If result is incompatible with expectation just fails (xor):
if( (r1 && !l) || (!r1 && l) )
{
......@@ -61,7 +62,8 @@ int TestDynamicLoader(const char* libname, const char* symbol, int r1, int r2, i
<< kwsys::DynamicLoader::LastError() << kwsys_ios::endl;
return 1;
}
kwsys::DynamicLoaderFunction f = kwsys::DynamicLoader::GetSymbolAddress(l, symbol);
kwsys::DynamicLoader::SymbolPointer f
= kwsys::DynamicLoader::GetSymbolAddress(l, symbol);
if( (r2 && !f) || (!r2 && f) )
{
kwsys_ios::cerr
......@@ -104,8 +106,8 @@ int main(int argc, char *argv[])
// Now try on the generated library
kwsys_stl::string libname = GetLibName("testDynload");
res += TestDynamicLoader(libname.c_str(), "dummy",1,0,1);
res += TestDynamicLoader(libname.c_str(), "TestDynamicLoaderFunction",1,1,1);
res += TestDynamicLoader(libname.c_str(), "_TestDynamicLoaderFunction",1,0,1);
res += TestDynamicLoader(libname.c_str(), "TestDynamicLoaderSymbolPointer",1,1,1);
res += TestDynamicLoader(libname.c_str(), "_TestDynamicLoaderSymbolPointer",1,0,1);
res += TestDynamicLoader(libname.c_str(), "TestDynamicLoaderData",1,1,1);
res += TestDynamicLoader(libname.c_str(), "_TestDynamicLoaderData",1,0,1);
......
......@@ -6,6 +6,6 @@
DL_EXPORT int TestDynamicLoaderData = 0;
DL_EXPORT void TestDynamicLoaderFunction()
DL_EXPORT void TestDynamicLoaderSymbolPointer()
{
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment