shared_library_impl.hpp 7.3 KB

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