00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00035 #include "OW_config.h"
00036 #if defined(OW_USE_DYLD)
00037 #include "OW_dyldSharedLibraryLoader.hpp"
00038 #include "OW_dyldSharedLibrary.hpp"
00039 #include "OW_Format.hpp"
00040
00041 namespace OW_NAMESPACE
00042 {
00043
00044 std::ostream& operator<<(std::ostream& o, NSObjectFileImageReturnCode code)
00045 {
00046 #define SIMPLE_NSOBJ_CASE(X) case X: o << ""#X
00047 switch (code)
00048 {
00049 SIMPLE_NSOBJ_CASE(NSObjectFileImageFailure);
00050 break;
00051 SIMPLE_NSOBJ_CASE(NSObjectFileImageSuccess);
00052 break;
00053 SIMPLE_NSOBJ_CASE(NSObjectFileImageInappropriateFile);
00054 break;
00055 SIMPLE_NSOBJ_CASE(NSObjectFileImageArch);
00056 break;
00057 SIMPLE_NSOBJ_CASE(NSObjectFileImageFormat);
00058 break;
00059 SIMPLE_NSOBJ_CASE(NSObjectFileImageAccess);
00060 break;
00061 }
00062 #undef SIMPLE_NSOBJ_CASE
00063 return o;
00064 }
00065
00067 SharedLibraryRef
00068 dyldSharedLibraryLoader::loadSharedLibrary(const String& filename,
00069 const LoggerRef& logger) const
00070 {
00071 OW_LOG_DEBUG(logger, Format("Load request for %1 received.", filename));
00072 NSObjectFileImage image = 0;
00073 NSObjectFileImageReturnCode dsoerr = NSCreateObjectFileImageFromFile(filename.c_str(), &image);
00074 const char* err_msg = NULL;
00075 NSModule libhandle = NULL;
00076
00077 if (dsoerr == NSObjectFileImageSuccess)
00078 {
00079 libhandle = NSLinkModule(image, filename.c_str(), NSLINKMODULE_OPTION_RETURN_ON_ERROR | NSLINKMODULE_OPTION_PRIVATE);
00080 if (!libhandle)
00081 {
00082 NSLinkEditErrors errors;
00083 int errorNumber;
00084 const char *fileName;
00085 NSLinkEditError(&errors, &errorNumber, &fileName, &err_msg);
00086 }
00087 NSDestroyObjectFileImage(image);
00088 }
00089 else if ((dsoerr == NSObjectFileImageFormat ||
00090 dsoerr == NSObjectFileImageInappropriateFile) &&
00091 NSAddLibrary(filename.c_str()) == TRUE)
00092 {
00093 OW_LOG_ERROR(logger, Format("NSCreateObject: %1 failed with error \"%2\"",
00094 filename, dsoerr));
00095
00096 }
00097 else
00098 {
00099 err_msg = "cannot create object file image or add library";
00100 OW_LOG_ERROR(logger, Format("NSCreateObject: %1 failed with error %2",
00101 filename, dsoerr));
00102 }
00103
00104
00105
00106 if (libhandle)
00107 {
00108 try
00109 {
00110 return SharedLibraryRef( new dyldSharedLibrary(libhandle,
00111 filename));
00112 }
00113 catch (...)
00114 {
00115 NSUnLinkModule(libhandle, FALSE);
00116 throw;
00117 }
00118 }
00119 else
00120 {
00121 OW_LOG_ERROR(logger, Format("dyldSharedLibraryLoader::loadSharedLibrary:"
00122 " %1", err_msg));
00123 return SharedLibraryRef( 0 );
00124 }
00125 }
00127 SharedLibraryLoaderRef
00128 SharedLibraryLoader::createSharedLibraryLoader()
00129 {
00130 return SharedLibraryLoaderRef(new dyldSharedLibraryLoader);
00131 }
00133 dyldSharedLibraryLoader::~dyldSharedLibraryLoader()
00134 {
00135 }
00136
00137 }
00138
00139 #endif // #if defined(OW_USE_DYLD)