1 // Copyright 2014 Renato Tegon Forti, Antony Polukhin.
2 // Copyright 2015-2019 Antony Polukhin.
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() / L"lib" : L"lib").native() + sl.filename().native())
68 actual_path += suffix();
72 void load(boost::dll::fs::path sl, load_mode::type mode, boost::dll::fs::error_code &ec) {
73 typedef int native_mode_t;
76 // Do not allow opening NULL paths. User must use program_location() instead
78 boost::dll::detail::reset_dlerror();
79 ec = boost::dll::fs::make_error_code(
80 boost::dll::fs::errc::bad_file_descriptor
87 if (!(mode & load_mode::rtld_now)) {
88 mode |= load_mode::rtld_lazy;
91 if (!(mode & load_mode::rtld_global)) {
92 mode |= load_mode::rtld_local;
95 #if BOOST_OS_LINUX || BOOST_OS_ANDROID
96 if (!sl.has_parent_path() && !(mode & load_mode::search_system_folders)) {
100 if (!sl.is_absolute() && !(mode & load_mode::search_system_folders)) {
101 boost::dll::fs::error_code current_path_ec;
102 boost::dll::fs::path prog_loc = boost::dll::fs::current_path(current_path_ec);
103 if (!current_path_ec) {
110 mode &= ~load_mode::search_system_folders;
112 // Trying to open with appended decorations
113 if (!!(mode & load_mode::append_decorations)) {
114 mode &= ~load_mode::append_decorations;
116 boost::dll::fs::path actual_path = decorate(sl);
117 handle_ = dlopen(actual_path.c_str(), static_cast<native_mode_t>(mode));
119 boost::dll::detail::reset_dlerror();
122 boost::dll::fs::error_code prog_loc_err;
123 boost::dll::fs::path loc = boost::dll::detail::program_location_impl(prog_loc_err);
124 if (boost::dll::fs::exists(actual_path) && !boost::dll::fs::equivalent(sl, loc, prog_loc_err)) {
125 // decorated path exists : current error is not a bad file descriptor and we are not trying to load the executable itself
126 ec = boost::dll::fs::make_error_code(
127 boost::dll::fs::errc::executable_format_error
133 // Opening by exactly specified path
134 handle_ = dlopen(sl.c_str(), static_cast<native_mode_t>(mode));
136 boost::dll::detail::reset_dlerror();
140 ec = boost::dll::fs::make_error_code(
141 boost::dll::fs::errc::bad_file_descriptor
144 // Maybe user wanted to load the executable itself? Checking...
145 // We assume that usually user wants to load a dynamic library not the executable itself, that's why
146 // we try this only after traditional load fails.
147 boost::dll::fs::error_code prog_loc_err;
148 boost::dll::fs::path loc = boost::dll::detail::program_location_impl(prog_loc_err);
149 if (!prog_loc_err && boost::dll::fs::equivalent(sl, loc, prog_loc_err) && !prog_loc_err) {
150 // As is known the function dlopen() loads the dynamic library file
151 // named by the null-terminated string filename and returns an opaque
152 // "handle" for the dynamic library. If filename is NULL, then the
153 // returned handle is for the main program.
155 boost::dll::detail::reset_dlerror();
156 handle_ = dlopen(NULL, static_cast<native_mode_t>(mode));
158 ec = boost::dll::fs::make_error_code(
159 boost::dll::fs::errc::bad_file_descriptor
165 bool is_loaded() const BOOST_NOEXCEPT {
166 return (handle_ != 0);
169 void unload() BOOST_NOEXCEPT {
178 void swap(shared_library_impl& rhs) BOOST_NOEXCEPT {
179 boost::swap(handle_, rhs.handle_);
182 boost::dll::fs::path full_module_path(boost::dll::fs::error_code &ec) const {
183 return boost::dll::detail::path_from_handle(handle_, ec);
186 static boost::dll::fs::path suffix() {
187 // https://sourceforge.net/p/predef/wiki/OperatingSystems/
188 #if BOOST_OS_MACOS || BOOST_OS_IOS
195 void* symbol_addr(const char* sb, boost::dll::fs::error_code &ec) const BOOST_NOEXCEPT {
196 // dlsym - obtain the address of a symbol from a dlopen object
197 void* const symbol = dlsym(handle_, sb);
198 if (symbol == NULL) {
199 ec = boost::dll::fs::make_error_code(
200 boost::dll::fs::errc::invalid_seek
204 // If handle does not refer to a valid object opened by dlopen(),
205 // or if the named symbol cannot be found within any of the objects
206 // associated with handle, dlsym() shall return NULL.
207 // More detailed diagnostic information shall be available through dlerror().
212 native_handle_t native() const BOOST_NOEXCEPT {
217 native_handle_t handle_;
220 }}} // boost::dll::detail
222 #endif // BOOST_DLL_SHARED_LIBRARY_IMPL_HPP