1 // Copyright 2014 Renato Tegon Forti, Antony Polukhin.
2 // Copyright 2015-2016 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/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/filesystem/path.hpp>
19 #include <boost/filesystem/operations.hpp>
20 #include <boost/predef/os.h>
23 #include <cstring> // strncmp
24 #if !BOOST_OS_MACOS && !BOOST_OS_IOS && !BOOST_OS_QNX
27 // QNX's copy of <elf.h> and <link.h> reside in sys folder
28 # include <sys/link.h>
31 #ifdef BOOST_HAS_PRAGMA_ONCE
35 namespace boost { namespace dll { namespace detail {
37 class shared_library_impl {
39 BOOST_MOVABLE_BUT_NOT_COPYABLE(shared_library_impl)
42 typedef void* native_handle_t;
44 shared_library_impl() BOOST_NOEXCEPT
48 ~shared_library_impl() BOOST_NOEXCEPT {
52 shared_library_impl(BOOST_RV_REF(shared_library_impl) sl) BOOST_NOEXCEPT
58 shared_library_impl & operator=(BOOST_RV_REF(shared_library_impl) sl) BOOST_NOEXCEPT {
63 void load(boost::filesystem::path sl, load_mode::type mode, boost::system::error_code &ec) {
64 typedef int native_mode_t;
67 // Do not allow opening NULL paths. User must use program_location() instead
69 boost::dll::detail::reset_dlerror();
70 ec = boost::system::error_code(
71 boost::system::errc::bad_file_descriptor,
72 boost::system::generic_category()
79 if (!(mode & load_mode::rtld_now)) {
80 mode |= load_mode::rtld_lazy;
83 if (!(mode & load_mode::rtld_global)) {
84 mode |= load_mode::rtld_local;
87 #if BOOST_OS_LINUX || BOOST_OS_ANDROID
88 if (!sl.has_parent_path() && !(mode & load_mode::search_system_folders)) {
92 if (!sl.is_absolute() && !(mode & load_mode::search_system_folders)) {
93 boost::system::error_code current_path_ec;
94 boost::filesystem::path prog_loc = boost::filesystem::current_path(current_path_ec);
95 if (!current_path_ec) {
102 mode &= ~load_mode::search_system_folders;
104 // Trying to open with appended decorations
105 if (!!(mode & load_mode::append_decorations)) {
106 mode &= ~load_mode::append_decorations;
108 boost::filesystem::path actual_path = (
109 std::strncmp(sl.filename().string().c_str(), "lib", 3)
110 ? (sl.has_parent_path() ? sl.parent_path() / L"lib" : L"lib").native() + sl.filename().native()
113 actual_path += suffix();
115 handle_ = dlopen(actual_path.c_str(), static_cast<native_mode_t>(mode));
117 boost::dll::detail::reset_dlerror();
122 // Opening by exactly specified path
123 handle_ = dlopen(sl.c_str(), static_cast<native_mode_t>(mode));
125 boost::dll::detail::reset_dlerror();
129 ec = boost::system::error_code(
130 boost::system::errc::bad_file_descriptor,
131 boost::system::generic_category()
134 // Maybe user wanted to load the executable itself? Checking...
135 // We assume that usually user wants to load a dynamic library not the executable itself, that's why
136 // we try this only after traditional load fails.
137 boost::system::error_code prog_loc_err;
138 boost::filesystem::path loc = boost::dll::detail::program_location_impl(prog_loc_err);
139 if (!prog_loc_err && boost::filesystem::equivalent(sl, loc, prog_loc_err) && !prog_loc_err) {
140 // As is known the function dlopen() loads the dynamic library file
141 // named by the null-terminated string filename and returns an opaque
142 // "handle" for the dynamic library. If filename is NULL, then the
143 // returned handle is for the main program.
145 boost::dll::detail::reset_dlerror();
146 handle_ = dlopen(NULL, static_cast<native_mode_t>(mode));
148 ec = boost::system::error_code(
149 boost::system::errc::bad_file_descriptor,
150 boost::system::generic_category()
156 bool is_loaded() const BOOST_NOEXCEPT {
157 return (handle_ != 0);
160 void unload() BOOST_NOEXCEPT {
169 void swap(shared_library_impl& rhs) BOOST_NOEXCEPT {
170 boost::swap(handle_, rhs.handle_);
173 boost::filesystem::path full_module_path(boost::system::error_code &ec) const {
174 return boost::dll::detail::path_from_handle(handle_, ec);
177 static boost::filesystem::path suffix() {
178 // https://sourceforge.net/p/predef/wiki/OperatingSystems/
179 #if BOOST_OS_MACOS || BOOST_OS_IOS
186 void* symbol_addr(const char* sb, boost::system::error_code &ec) const BOOST_NOEXCEPT {
187 // dlsym - obtain the address of a symbol from a dlopen object
188 void* const symbol = dlsym(handle_, sb);
189 if (symbol == NULL) {
190 ec = boost::system::error_code(
191 boost::system::errc::invalid_seek,
192 boost::system::generic_category()
196 // If handle does not refer to a valid object opened by dlopen(),
197 // or if the named symbol cannot be found within any of the objects
198 // associated with handle, dlsym() shall return NULL.
199 // More detailed diagnostic information shall be available through dlerror().
204 native_handle_t native() const BOOST_NOEXCEPT {
209 native_handle_t handle_;
212 }}} // boost::dll::detail
214 #endif // BOOST_DLL_SHARED_LIBRARY_IMPL_HPP