blocxx
dlSharedLibraryLoader.cpp
Go to the documentation of this file.
1 /*******************************************************************************
2 * Copyright (C) 2005, Vintela, Inc. All rights reserved.
3 * Copyright (C) 2006, Novell, Inc. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * * Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * * Neither the name of
14 * Vintela, Inc.,
15 * nor Novell, Inc.,
16 * nor the names of its contributors or employees may be used to
17 * endorse or promote products derived from this software without
18 * specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 *******************************************************************************/
32 
33 
38 #include "blocxx/BLOCXX_config.h"
39 #if defined(BLOCXX_USE_DL)
42 #include "blocxx/Format.hpp"
43 #include "blocxx/GlobalString.hpp"
44 #include <dlfcn.h>
45 
46 namespace BLOCXX_NAMESPACE
47 {
48 
49 namespace
50 {
51  GlobalString COMPONENT_NAME = BLOCXX_GLOBAL_STRING_INIT("blocxx.dlSharedLibraryLoader");
52 }
55 dlSharedLibraryLoader::loadSharedLibrary(const String& filename) const
56 {
57  // There is a reason to use RTLD_NOW. If some symbols can't be resolved because
58  // the shared library is built incorrectly or missing some symbols or something,
59  // then using RTLD_NOW will cause dlopen() to fail, and then the error can be detected
60  // and handled. If RTLD_LAZY is specified, dlopen() won't fail, but when code is
61  // executed that tries to use an unresolvable symbol, that will cause a segfault.
62  // RTLD_GLOBAL is necessary for proper exception and rtti support with gcc.
63  // See http://gcc.gnu.org/faq.html#dso, so even though it may cause symbol conflicts,
64  // we have to live with it.
65  void* libhandle = dlopen(filename.c_str(), RTLD_NOW | RTLD_GLOBAL);
66 
67  String first_error = dlerror();
68  String second_error;
69 
70 #if defined(BLOCXX_USE_FAKE_LIBS)
71  // This section, if it determines that a library is fake, will attempt
72  // to load NULL (which, on most dlopen platforms should return a handle
73  // to the main executable). This handle can be used just like any
74  // other, assuming that the main executable was linked with the proper
75  // export flags (--export-dynamic on linux, -bexpall on AIX, etc.).
76  if ( !libhandle )
77  {
78  if ( dlSharedLibrary::isFakeLibrary(filename) )
79  {
80  libhandle = dlopen(NULL, RTLD_NOW | RTLD_GLOBAL);
81 
82  if ( !libhandle )
83  {
84  second_error = dlerror();
85  }
86  }
87  }
88 #endif /* defined(BLOCXX_USE_FAKE_LIBS) */
89  if (libhandle)
90  {
91  return SharedLibraryRef( new dlSharedLibrary(libhandle,
92  filename));
93  }
94  else
95  {
96  Logger logger(COMPONENT_NAME);
97  BLOCXX_LOG_ERROR(logger, Format("dlSharedLibraryLoader::loadSharedLibrary "
98  "dlopen returned NULL. Error is: %1", first_error));
99  if ( !second_error.empty() )
100  {
101  BLOCXX_LOG_ERROR(logger, Format("dlSharedLibraryLoader::loadSharedLibrary (fakelib) "
102  "dlopen returned NULL. Error is: %1", second_error));
103  }
104  return SharedLibraryRef( 0 );
105  }
106 }
107 
111 {
112  return SharedLibraryLoaderRef(new dlSharedLibraryLoader);
113 }
115 dlSharedLibraryLoader::~dlSharedLibraryLoader()
116 {
117 }
118 
119 } // end namespace BLOCXX_NAMESPACE
120 
121 #endif // BLOCXX_USE_DL