1 // Copyright 2014 Renato Tegon Forti, Antony Polukhin.
2 // Copyright Antony Polukhin, 2015-2022.
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)
8 #ifndef BOOST_DLL_SHARED_LIBRARY_IMPL_HPP
9 #define BOOST_DLL_SHARED_LIBRARY_IMPL_HPP
11 #include <boost/dll/config.hpp>
12 #include <boost/dll/shared_library_load_mode.hpp>
13 #include <boost/dll/detail/posix/path_from_handle.hpp>
14 #include <boost/dll/detail/posix/program_location_impl.hpp>
16 #include <boost/move/utility.hpp>
17 #include <boost/swap.hpp>
18 #include <boost/predef/os.h>
21 #include <cstring> // strncmp
22 #if !BOOST_OS_MACOS && !BOOST_OS_IOS && !BOOST_OS_QNX
25 // QNX's copy of <elf.h> and <link.h> reside in sys folder
26 # include <sys/link.h>
29 #ifdef BOOST_HAS_PRAGMA_ONCE
33 namespace boost { namespace dll { namespace detail {
35 class shared_library_impl {
37 BOOST_MOVABLE_BUT_NOT_COPYABLE(shared_library_impl)
40 typedef void* native_handle_t;
42 shared_library_impl() BOOST_NOEXCEPT
46 ~shared_library_impl() BOOST_NOEXCEPT {
50 shared_library_impl(BOOST_RV_REF(shared_library_impl) sl) BOOST_NOEXCEPT
56 shared_library_impl & operator=(BOOST_RV_REF(shared_library_impl) sl) BOOST_NOEXCEPT {
62 static boost::dll::fs::path decorate(const boost::dll::fs::path & sl) {
63 boost::dll::fs::path actual_path = (
64 std::strncmp(sl.filename().string().c_str(), "lib", 3)
65 ? boost::dll::fs::path((sl.has_parent_path() ? sl.parent_path() / "lib" : "lib").native() + sl.filename().native())
68 actual_path += suffix();
72 void load(boost::dll::fs::path sl, load_mode::type portable_mode, boost::dll::fs::error_code &ec) {
73 typedef int native_mode_t;
74 native_mode_t native_mode = static_cast<native_mode_t>(portable_mode);
77 // Do not allow opening NULL paths. User must use program_location() instead
79 boost::dll::detail::reset_dlerror();
80 ec = boost::dll::fs::make_error_code(
81 boost::dll::fs::errc::bad_file_descriptor
88 if (!(native_mode & load_mode::rtld_now)) {
89 native_mode |= load_mode::rtld_lazy;
92 if (!(native_mode & load_mode::rtld_global)) {
93 native_mode |= load_mode::rtld_local;
96 #if BOOST_OS_LINUX || BOOST_OS_ANDROID
97 if (!sl.has_parent_path() && !(native_mode & load_mode::search_system_folders)) {
101 if (!sl.is_absolute() && !(native_mode & load_mode::search_system_folders)) {
102 boost::dll::fs::error_code current_path_ec;
103 boost::dll::fs::path prog_loc = boost::dll::fs::current_path(current_path_ec);
104 if (!current_path_ec) {
111 native_mode = static_cast<unsigned>(native_mode) & ~static_cast<unsigned>(load_mode::search_system_folders);
113 // Trying to open with appended decorations
114 if (!!(native_mode & load_mode::append_decorations)) {
115 native_mode = static_cast<unsigned>(native_mode) & ~static_cast<unsigned>(load_mode::append_decorations);
117 boost::dll::fs::path actual_path = decorate(sl);
118 handle_ = dlopen(actual_path.c_str(), native_mode);
120 boost::dll::detail::reset_dlerror();
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 (boost::dll::fs::exists(actual_path) && !boost::dll::fs::equivalent(sl, loc, prog_loc_err)) {
126 // decorated path exists : current error is not a bad file descriptor and we are not trying to load the executable itself
127 ec = boost::dll::fs::make_error_code(
128 boost::dll::fs::errc::executable_format_error
134 // Opening by exactly specified path
135 handle_ = dlopen(sl.c_str(), native_mode);
137 boost::dll::detail::reset_dlerror();
141 ec = boost::dll::fs::make_error_code(
142 boost::dll::fs::errc::bad_file_descriptor
145 // Maybe user wanted to load the executable itself? Checking...
146 // We assume that usually user wants to load a dynamic library not the executable itself, that's why
147 // we try this only after traditional load fails.
148 boost::dll::fs::error_code prog_loc_err;
149 boost::dll::fs::path loc = boost::dll::detail::program_location_impl(prog_loc_err);
150 if (!prog_loc_err && boost::dll::fs::equivalent(sl, loc, prog_loc_err) && !prog_loc_err) {
151 // As is known the function dlopen() loads the dynamic library file
152 // named by the null-terminated string filename and returns an opaque
153 // "handle" for the dynamic library. If filename is NULL, then the
154 // returned handle is for the main program.
156 boost::dll::detail::reset_dlerror();
157 handle_ = dlopen(NULL, native_mode);
159 ec = boost::dll::fs::make_error_code(
160 boost::dll::fs::errc::bad_file_descriptor
166 bool is_loaded() const BOOST_NOEXCEPT {
167 return (handle_ != 0);
170 void unload() BOOST_NOEXCEPT {
179 void swap(shared_library_impl& rhs) BOOST_NOEXCEPT {
180 boost::swap(handle_, rhs.handle_);
183 boost::dll::fs::path full_module_path(boost::dll::fs::error_code &ec) const {
184 return boost::dll::detail::path_from_handle(handle_, ec);
187 static boost::dll::fs::path suffix() {
188 // https://sourceforge.net/p/predef/wiki/OperatingSystems/
189 #if BOOST_OS_MACOS || BOOST_OS_IOS
196 void* symbol_addr(const char* sb, boost::dll::fs::error_code &ec) const BOOST_NOEXCEPT {
197 // dlsym - obtain the address of a symbol from a dlopen object
198 void* const symbol = dlsym(handle_, sb);
199 if (symbol == NULL) {
200 ec = boost::dll::fs::make_error_code(
201 boost::dll::fs::errc::invalid_seek
205 // If handle does not refer to a valid object opened by dlopen(),
206 // or if the named symbol cannot be found within any of the objects
207 // associated with handle, dlsym() shall return NULL.
208 // More detailed diagnostic information shall be available through dlerror().
213 native_handle_t native() const BOOST_NOEXCEPT {
218 native_handle_t handle_;
221 }}} // boost::dll::detail
223 #endif // BOOST_DLL_SHARED_LIBRARY_IMPL_HPP