blocxx

dyldSharedLibraryLoader.cpp

Go to the documentation of this file.
00001 /*******************************************************************************
00002 * Copyright (C) 2005, Vintela, Inc. All rights reserved.
00003 * Copyright (C) 2006, Novell, Inc. All rights reserved.
00004 * 
00005 * Redistribution and use in source and binary forms, with or without
00006 * modification, are permitted provided that the following conditions are met:
00007 * 
00008 *     * Redistributions of source code must retain the above copyright notice,
00009 *       this list of conditions and the following disclaimer.
00010 *     * Redistributions in binary form must reproduce the above copyright
00011 *       notice, this list of conditions and the following disclaimer in the
00012 *       documentation and/or other materials provided with the distribution.
00013 *     * Neither the name of 
00014 *       Vintela, Inc., 
00015 *       nor Novell, Inc., 
00016 *       nor the names of its contributors or employees may be used to 
00017 *       endorse or promote products derived from this software without 
00018 *       specific prior written permission.
00019 * 
00020 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00021 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00022 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00023 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00024 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00025 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00026 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00027 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00028 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00029 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00030 * POSSIBILITY OF SUCH DAMAGE.
00031 *******************************************************************************/
00032 
00033 
00038 #include "blocxx/BLOCXX_config.h"
00039 #if defined(BLOCXX_USE_DYLD)
00040 #include "blocxx/dyldSharedLibraryLoader.hpp"
00041 #include "blocxx/dyldSharedLibrary.hpp"
00042 #include "blocxx/Format.hpp"
00043 
00044 namespace BLOCXX_NAMESPACE
00045 {
00046 
00047 namespace
00048 {
00049    String COMPONENT_NAME("blocxx.dyldSharedLibraryLoader");
00050 }
00051 
00052 std::ostream& operator<<(std::ostream& o, NSObjectFileImageReturnCode code)
00053 {
00054 #define SIMPLE_NSOBJ_CASE(X) case X: o << ""#X
00055    switch (code)
00056    {
00057       SIMPLE_NSOBJ_CASE(NSObjectFileImageFailure);
00058       break;
00059       SIMPLE_NSOBJ_CASE(NSObjectFileImageSuccess);
00060       break;
00061       SIMPLE_NSOBJ_CASE(NSObjectFileImageInappropriateFile);
00062       break;
00063       SIMPLE_NSOBJ_CASE(NSObjectFileImageArch);
00064       break;
00065       SIMPLE_NSOBJ_CASE(NSObjectFileImageFormat);
00066       break;
00067       SIMPLE_NSOBJ_CASE(NSObjectFileImageAccess);
00068       break;
00069    }
00070 #undef SIMPLE_NSOBJ_CASE
00071    return o;
00072 }
00073 
00075 SharedLibraryRef
00076 dyldSharedLibraryLoader::loadSharedLibrary(const String& filename) const
00077 {
00078    Logger logger(COMPONENT_NAME);
00079    BLOCXX_LOG_DEBUG2(logger, Format("Load request for %1 received.", filename));
00080    NSObjectFileImage image = 0;
00081    NSObjectFileImageReturnCode dsoerr = NSCreateObjectFileImageFromFile(filename.c_str(), &image);
00082    const char* err_msg = NULL;
00083    NSModule libhandle = NULL; 
00084 
00085    if (dsoerr == NSObjectFileImageSuccess)
00086    {
00087       libhandle = NSLinkModule(image, filename.c_str(), NSLINKMODULE_OPTION_RETURN_ON_ERROR | NSLINKMODULE_OPTION_PRIVATE);      
00088       if (!libhandle)
00089       {
00090          NSLinkEditErrors errors;
00091          int errorNumber;
00092          const char *fileName;
00093          NSLinkEditError(&errors, &errorNumber, &fileName, &err_msg);
00094       }
00095       NSDestroyObjectFileImage(image);
00096    }
00097    else
00098    {
00099       err_msg = "cannot create object file image";
00100       BLOCXX_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       BLOCXX_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 } // end namespace BLOCXX_NAMESPACE
00138 
00139 #endif // #if defined(BLOCXX_USE_DYLD)