diff options
author | Mathieu Malaterre <mathieu.malaterre@gmail.com> | 2006-03-10 18:34:03 (GMT) |
---|---|---|
committer | Mathieu Malaterre <mathieu.malaterre@gmail.com> | 2006-03-10 18:34:03 (GMT) |
commit | f90e7541008116d22209e5e1ffc0d9b4e6bc1a87 (patch) | |
tree | 3ade01051786b12754534a61f37036172e396870 /Source/kwsys/DynamicLoader.cxx | |
parent | 5623ef913fc4120f8d0e9bca92597f4ad9fd9d91 (diff) | |
download | CMake-f90e7541008116d22209e5e1ffc0d9b4e6bc1a87.zip CMake-f90e7541008116d22209e5e1ffc0d9b4e6bc1a87.tar.gz CMake-f90e7541008116d22209e5e1ffc0d9b4e6bc1a87.tar.bz2 |
BUG: Fix DynamicLoader implementation on MacOSX (using old API)
Diffstat (limited to 'Source/kwsys/DynamicLoader.cxx')
-rw-r--r-- | Source/kwsys/DynamicLoader.cxx | 52 |
1 files changed, 34 insertions, 18 deletions
diff --git a/Source/kwsys/DynamicLoader.cxx b/Source/kwsys/DynamicLoader.cxx index 2bf2a0d..07df87a 100644 --- a/Source/kwsys/DynamicLoader.cxx +++ b/Source/kwsys/DynamicLoader.cxx @@ -16,6 +16,12 @@ #include KWSYS_HEADER(Configure.hxx) +#ifdef __APPLE__ +#if MAC_OS_X_VERSION_MIN_REQUIRED < 1030 +#include <string.h> // for strlen +#endif //MAC_OS_X_VERSION_MIN_REQUIRED < 1030 +#endif // __APPLE__ + // Work-around CMake dependency scanning limitation. This must // duplicate the above list of headers. #if 0 @@ -70,20 +76,20 @@ int DynamicLoader::CloseLibrary(LibHandle lib) //---------------------------------------------------------------------------- DynamicLoaderFunction DynamicLoader::GetSymbolAddress(LibHandle 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<DynamicLoaderFunction*>(&result); } //---------------------------------------------------------------------------- const char* DynamicLoader::LibPrefix() -{ +{ return "lib"; } @@ -97,9 +103,9 @@ const char* DynamicLoader::LibExtension() const char* DynamicLoader::LastError() { // TODO: Need implementation with errno/strerror - /* If successful, shl_findsym returns an integer (int) value zero. If - * shl_findsym cannot find sym, it returns -1 and sets errno to zero. - * If any other errors occur, shl_findsym returns -1 and sets errno to one + /* If successful, shl_findsym returns an integer (int) value zero. If + * shl_findsym cannot find sym, it returns -1 and sets errno to zero. + * If any other errors occur, shl_findsym returns -1 and sets errno to one * of these values (defined in <errno.h>): * ENOEXEC * A format error was detected in the specified library. @@ -134,11 +140,13 @@ LibHandle DynamicLoader::OpenLibrary(const char* libname ) NSObjectFileImage image = 0; rc = NSCreateObjectFileImageFromFile(libname, &image); - if(!image) + // rc == NSObjectFileImageInappropriateFile when trying to load a dylib file + if( rc != NSObjectFileImageSuccess ) { return 0; } - return NSLinkModule(image, libname, NSLINKMODULE_OPTION_BINDNOW); + return NSLinkModule(image, libname, + NSLINKMODULE_OPTION_PRIVATE|NSLINKMODULE_OPTION_BINDNOW); } //---------------------------------------------------------------------------- @@ -149,17 +157,22 @@ int DynamicLoader::CloseLibrary( LibHandle lib) } //---------------------------------------------------------------------------- -DynamicLoaderFunction DynamicLoader::GetSymbolAddress(LibHandle /* lib */, const char* sym) +DynamicLoaderFunction DynamicLoader::GetSymbolAddress(LibHandle lib, const char* sym) { void *result=0; - if(NSIsSymbolNameDefined(sym)) + // Need to prepend symbols with '_' on Apple-gcc compilers + size_t len = strlen(sym); + char *rsym = new char[len + 1 + 1]; + strcpy(rsym, "_"); + strcat(rsym+1, sym); + + NSSymbol symbol = NSLookupSymbolInModule(lib, rsym); + if(symbol) { - NSSymbol symbol= NSLookupAndBindSymbol(sym); - if(symbol) - { - result = NSAddressOfSymbol(symbol); - } + result = NSAddressOfSymbol(symbol); } + + delete[] rsym; // Hack to cast pointer-to-data to pointer-to-function. return *reinterpret_cast<DynamicLoaderFunction*>(&result); } @@ -167,13 +180,16 @@ DynamicLoaderFunction DynamicLoader::GetSymbolAddress(LibHandle /* lib */, const //---------------------------------------------------------------------------- const char* DynamicLoader::LibPrefix() { - return ""; + return "lib"; } //---------------------------------------------------------------------------- const char* DynamicLoader::LibExtension() { - return ".dylib"; + // NSCreateObjectFileImageFromFile fail when dealing with dylib image + // it returns NSObjectFileImageInappropriateFile + //return ".dylib"; + return ".so"; } //---------------------------------------------------------------------------- |