shared_library_impl.hpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. // Copyright 2014 Renato Tegon Forti, Antony Polukhin.
  2. // Copyright 2015-2016 Antony Polukhin.
  3. //
  4. // Distributed under the Boost Software License, Version 1.0.
  5. // (See accompanying file LICENSE_1_0.txt
  6. // or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. #ifndef BOOST_DLL_SHARED_LIBRARY_IMPL_HPP
  8. #define BOOST_DLL_SHARED_LIBRARY_IMPL_HPP
  9. #include <boost/config.hpp>
  10. #include <boost/dll/shared_library_load_mode.hpp>
  11. #include <boost/dll/detail/posix/path_from_handle.hpp>
  12. #include <boost/dll/detail/posix/program_location_impl.hpp>
  13. #include <boost/move/utility.hpp>
  14. #include <boost/swap.hpp>
  15. #include <boost/filesystem/path.hpp>
  16. #include <boost/filesystem/operations.hpp>
  17. #include <boost/predef/os.h>
  18. #include <dlfcn.h>
  19. #include <cstring> // strncmp
  20. #if !BOOST_OS_MACOS && !BOOST_OS_IOS && !BOOST_OS_QNX
  21. # include <link.h>
  22. #elif BOOST_OS_QNX
  23. // QNX's copy of <elf.h> and <link.h> reside in sys folder
  24. # include <sys/link.h>
  25. #endif
  26. #ifdef BOOST_HAS_PRAGMA_ONCE
  27. # pragma once
  28. #endif
  29. namespace boost { namespace dll { namespace detail {
  30. class shared_library_impl {
  31. BOOST_MOVABLE_BUT_NOT_COPYABLE(shared_library_impl)
  32. public:
  33. typedef void* native_handle_t;
  34. shared_library_impl() BOOST_NOEXCEPT
  35. : handle_(NULL)
  36. {}
  37. ~shared_library_impl() BOOST_NOEXCEPT {
  38. unload();
  39. }
  40. shared_library_impl(BOOST_RV_REF(shared_library_impl) sl) BOOST_NOEXCEPT
  41. : handle_(sl.handle_)
  42. {
  43. sl.handle_ = NULL;
  44. }
  45. shared_library_impl & operator=(BOOST_RV_REF(shared_library_impl) sl) BOOST_NOEXCEPT {
  46. swap(sl);
  47. return *this;
  48. }
  49. void load(boost::filesystem::path sl, load_mode::type mode, boost::system::error_code &ec) {
  50. typedef int native_mode_t;
  51. unload();
  52. // Do not allow opening NULL paths. User must use program_location() instead
  53. if (sl.empty()) {
  54. boost::dll::detail::reset_dlerror();
  55. ec = boost::system::error_code(
  56. boost::system::errc::bad_file_descriptor,
  57. boost::system::generic_category()
  58. );
  59. return;
  60. }
  61. // Fixing modes
  62. if (!(mode & load_mode::rtld_now)) {
  63. mode |= load_mode::rtld_lazy;
  64. }
  65. if (!(mode & load_mode::rtld_global)) {
  66. mode |= load_mode::rtld_local;
  67. }
  68. #if BOOST_OS_LINUX || BOOST_OS_ANDROID
  69. if (!sl.has_parent_path() && !(mode & load_mode::search_system_folders)) {
  70. sl = "." / sl;
  71. }
  72. #else
  73. if (!sl.is_absolute() && !(mode & load_mode::search_system_folders)) {
  74. boost::system::error_code current_path_ec;
  75. boost::filesystem::path prog_loc = boost::filesystem::current_path(current_path_ec);
  76. if (!current_path_ec) {
  77. prog_loc /= sl;
  78. sl.swap(prog_loc);
  79. }
  80. }
  81. #endif
  82. mode &= ~load_mode::search_system_folders;
  83. // Trying to open with appended decorations
  84. if (!!(mode & load_mode::append_decorations)) {
  85. mode &= ~load_mode::append_decorations;
  86. boost::filesystem::path actual_path = (
  87. std::strncmp(sl.filename().string().c_str(), "lib", 3)
  88. ? (sl.has_parent_path() ? sl.parent_path() / L"lib" : L"lib").native() + sl.filename().native()
  89. : sl
  90. );
  91. actual_path += suffix();
  92. handle_ = dlopen(actual_path.c_str(), static_cast<native_mode_t>(mode));
  93. if (handle_) {
  94. boost::dll::detail::reset_dlerror();
  95. return;
  96. }
  97. }
  98. // Opening by exactly specified path
  99. handle_ = dlopen(sl.c_str(), static_cast<native_mode_t>(mode));
  100. if (handle_) {
  101. boost::dll::detail::reset_dlerror();
  102. return;
  103. }
  104. ec = boost::system::error_code(
  105. boost::system::errc::bad_file_descriptor,
  106. boost::system::generic_category()
  107. );
  108. // Maybe user wanted to load the executable itself? Checking...
  109. // We assume that usually user wants to load a dynamic library not the executable itself, that's why
  110. // we try this only after traditional load fails.
  111. boost::system::error_code prog_loc_err;
  112. boost::filesystem::path loc = boost::dll::detail::program_location_impl(prog_loc_err);
  113. if (!prog_loc_err && boost::filesystem::equivalent(sl, loc, prog_loc_err) && !prog_loc_err) {
  114. // As is known the function dlopen() loads the dynamic library file
  115. // named by the null-terminated string filename and returns an opaque
  116. // "handle" for the dynamic library. If filename is NULL, then the
  117. // returned handle is for the main program.
  118. ec.clear();
  119. boost::dll::detail::reset_dlerror();
  120. handle_ = dlopen(NULL, static_cast<native_mode_t>(mode));
  121. if (!handle_) {
  122. ec = boost::system::error_code(
  123. boost::system::errc::bad_file_descriptor,
  124. boost::system::generic_category()
  125. );
  126. }
  127. }
  128. }
  129. bool is_loaded() const BOOST_NOEXCEPT {
  130. return (handle_ != 0);
  131. }
  132. void unload() BOOST_NOEXCEPT {
  133. if (!is_loaded()) {
  134. return;
  135. }
  136. dlclose(handle_);
  137. handle_ = 0;
  138. }
  139. void swap(shared_library_impl& rhs) BOOST_NOEXCEPT {
  140. boost::swap(handle_, rhs.handle_);
  141. }
  142. boost::filesystem::path full_module_path(boost::system::error_code &ec) const {
  143. return boost::dll::detail::path_from_handle(handle_, ec);
  144. }
  145. static boost::filesystem::path suffix() {
  146. // https://sourceforge.net/p/predef/wiki/OperatingSystems/
  147. #if BOOST_OS_MACOS || BOOST_OS_IOS
  148. return ".dylib";
  149. #else
  150. return ".so";
  151. #endif
  152. }
  153. void* symbol_addr(const char* sb, boost::system::error_code &ec) const BOOST_NOEXCEPT {
  154. // dlsym - obtain the address of a symbol from a dlopen object
  155. void* const symbol = dlsym(handle_, sb);
  156. if (symbol == NULL) {
  157. ec = boost::system::error_code(
  158. boost::system::errc::invalid_seek,
  159. boost::system::generic_category()
  160. );
  161. }
  162. // If handle does not refer to a valid object opened by dlopen(),
  163. // or if the named symbol cannot be found within any of the objects
  164. // associated with handle, dlsym() shall return NULL.
  165. // More detailed diagnostic information shall be available through dlerror().
  166. return symbol;
  167. }
  168. native_handle_t native() const BOOST_NOEXCEPT {
  169. return handle_;
  170. }
  171. private:
  172. native_handle_t handle_;
  173. };
  174. }}} // boost::dll::detail
  175. #endif // BOOST_DLL_SHARED_LIBRARY_IMPL_HPP