shared_library_impl.hpp 7.1 KB

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