shared_library_impl.hpp 7.2 KB

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