00001 /******************************************************************************* 00002 * Copyright (C) 2001-2004 Vintela, Inc. All rights reserved. 00003 * 00004 * Redistribution and use in source and binary forms, with or without 00005 * modification, are permitted provided that the following conditions are met: 00006 * 00007 * - Redistributions of source code must retain the above copyright notice, 00008 * this list of conditions and the following disclaimer. 00009 * 00010 * - Redistributions in binary form must reproduce the above copyright notice, 00011 * this list of conditions and the following disclaimer in the documentation 00012 * and/or other materials provided with the distribution. 00013 * 00014 * - Neither the name of Vintela, Inc. nor the names of its 00015 * contributors may be used to endorse or promote products derived from this 00016 * software without specific prior written permission. 00017 * 00018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' 00019 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00020 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00021 * ARE DISCLAIMED. IN NO EVENT SHALL Vintela, Inc. OR THE CONTRIBUTORS 00022 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00023 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 00024 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00025 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00026 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00027 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00028 * POSSIBILITY OF SUCH DAMAGE. 00029 *******************************************************************************/ 00030 00035 #include "OW_config.h" 00036 #if defined(OW_USE_DL) 00037 #include "OW_dlSharedLibraryLoader.hpp" 00038 #include "OW_dlSharedLibrary.hpp" 00039 #include "OW_Format.hpp" 00040 #include <dlfcn.h> 00041 00042 namespace OW_NAMESPACE 00043 { 00044 00046 SharedLibraryRef 00047 dlSharedLibraryLoader::loadSharedLibrary(const String& filename, 00048 const LoggerRef& logger) const 00049 { 00050 // There is a reason to use RTLD_NOW. If some symbols can't be resolved because 00051 // the shared library is built incorrectly or missing some symbols or something, 00052 // then using RTLD_NOW will cause dlopen() to fail, and then the error can be detected 00053 // and handled. If RTLD_LAZY is specified, dlopen() won't fail, but when code is 00054 // executed that tries to use an unresolvable symbol, that will cause a segfault. 00055 // RTLD_GLOBAL is necessary for proper exception and rtti support with gcc. 00056 // See http://gcc.gnu.org/faq.html#dso, so even though it may cause symbol conflicts, 00057 // we have to live with it. 00058 void* libhandle = dlopen(filename.c_str(), RTLD_NOW | RTLD_GLOBAL); 00059 00060 String first_error = dlerror(); 00061 String second_error; 00062 00063 #if defined(OW_USE_FAKE_LIBS) 00064 // This section, if it determines that a library is fake, will attempt 00065 // to load NULL (which, on most dlopen platforms should return a handle 00066 // to the main executable). This handle can be used just like any 00067 // other, assuming that the main executable was linked with the proper 00068 // export flags (--export-dynamic on linux, -bexpall on AIX, etc.). 00069 if ( !libhandle ) 00070 { 00071 if ( dlSharedLibrary::isFakeLibrary(filename) ) 00072 { 00073 libhandle = dlopen(NULL, RTLD_NOW | RTLD_GLOBAL); 00074 00075 if ( !libhandle ) 00076 { 00077 second_error = dlerror(); 00078 } 00079 } 00080 } 00081 #endif /* defined(OW_USE_FAKE_LIBS) */ 00082 if (libhandle) 00083 { 00084 return SharedLibraryRef( new dlSharedLibrary(libhandle, 00085 filename)); 00086 } 00087 else 00088 { 00089 OW_LOG_ERROR(logger, Format("dlSharedLibraryLoader::loadSharedLibrary " 00090 "dlopen returned NULL. Error is: %1", first_error)); 00091 if ( !second_error.empty() ) 00092 { 00093 OW_LOG_ERROR(logger, Format("dlSharedLibraryLoader::loadSharedLibrary (fakelib) " 00094 "dlopen returned NULL. Error is: %1", second_error)); 00095 } 00096 return SharedLibraryRef( 0 ); 00097 } 00098 } 00099 00101 SharedLibraryLoaderRef 00102 SharedLibraryLoader::createSharedLibraryLoader() 00103 { 00104 return SharedLibraryLoaderRef(new dlSharedLibraryLoader); 00105 } 00107 dlSharedLibraryLoader::~dlSharedLibraryLoader() 00108 { 00109 } 00110 00111 } // end namespace OW_NAMESPACE 00112 00113 #endif // OW_USE_DL