BUG: Fix DynamicLoader implementation on MacOSX (using old API)

This commit is contained in:
Mathieu Malaterre 2006-03-10 13:34:03 -05:00
parent 5623ef913f
commit f90e754100
2 changed files with 35 additions and 19 deletions

View File

@ -515,7 +515,7 @@ IF(KWSYS_C_SRCS OR KWSYS_CXX_SRCS)
# If kwsys contains the DynamicLoader, need extra library # If kwsys contains the DynamicLoader, need extra library
IF(KWSYS_USE_DynamicLoader) IF(KWSYS_USE_DynamicLoader)
ADD_LIBRARY(testDynload SHARED testDynload.c) ADD_LIBRARY(testDynload MODULE testDynload.c)
IF(UNIX) IF(UNIX)
TARGET_LINK_LIBRARIES(${KWSYS_NAMESPACE} ${CMAKE_DL_LIBS}) TARGET_LINK_LIBRARIES(${KWSYS_NAMESPACE} ${CMAKE_DL_LIBS})
ENDIF(UNIX) ENDIF(UNIX)

View File

@ -16,6 +16,12 @@
#include KWSYS_HEADER(Configure.hxx) #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 // Work-around CMake dependency scanning limitation. This must
// duplicate the above list of headers. // duplicate the above list of headers.
#if 0 #if 0
@ -134,11 +140,13 @@ LibHandle DynamicLoader::OpenLibrary(const char* libname )
NSObjectFileImage image = 0; NSObjectFileImage image = 0;
rc = NSCreateObjectFileImageFromFile(libname, &image); rc = NSCreateObjectFileImageFromFile(libname, &image);
if(!image) // rc == NSObjectFileImageInappropriateFile when trying to load a dylib file
if( rc != NSObjectFileImageSuccess )
{ {
return 0; 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; void *result=0;
if(NSIsSymbolNameDefined(sym)) // Need to prepend symbols with '_' on Apple-gcc compilers
{ size_t len = strlen(sym);
NSSymbol symbol= NSLookupAndBindSymbol(sym); char *rsym = new char[len + 1 + 1];
strcpy(rsym, "_");
strcat(rsym+1, sym);
NSSymbol symbol = NSLookupSymbolInModule(lib, rsym);
if(symbol) if(symbol)
{ {
result = NSAddressOfSymbol(symbol); result = NSAddressOfSymbol(symbol);
} }
}
delete[] rsym;
// Hack to cast pointer-to-data to pointer-to-function. // Hack to cast pointer-to-data to pointer-to-function.
return *reinterpret_cast<DynamicLoaderFunction*>(&result); return *reinterpret_cast<DynamicLoaderFunction*>(&result);
} }
@ -167,13 +180,16 @@ DynamicLoaderFunction DynamicLoader::GetSymbolAddress(LibHandle /* lib */, const
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------
const char* DynamicLoader::LibPrefix() const char* DynamicLoader::LibPrefix()
{ {
return ""; return "lib";
} }
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------
const char* DynamicLoader::LibExtension() const char* DynamicLoader::LibExtension()
{ {
return ".dylib"; // NSCreateObjectFileImageFromFile fail when dealing with dylib image
// it returns NSObjectFileImageInappropriateFile
//return ".dylib";
return ".so";
} }
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------