• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2014 Renato Tegon Forti, Antony Polukhin.
2 // Copyright 2015-2019 Antony Polukhin.
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 
8 #ifndef BOOST_DLL_SHARED_LIBRARY_IMPL_HPP
9 #define BOOST_DLL_SHARED_LIBRARY_IMPL_HPP
10 
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>
15 
16 #include <boost/move/utility.hpp>
17 #include <boost/swap.hpp>
18 #include <boost/predef/os.h>
19 
20 #include <dlfcn.h>
21 #include <cstring> // strncmp
22 #if !BOOST_OS_MACOS && !BOOST_OS_IOS && !BOOST_OS_QNX
23 #   include <link.h>
24 #elif BOOST_OS_QNX
25 // QNX's copy of <elf.h> and <link.h> reside in sys folder
26 #   include <sys/link.h>
27 #endif
28 
29 #ifdef BOOST_HAS_PRAGMA_ONCE
30 # pragma once
31 #endif
32 
33 namespace boost { namespace dll { namespace detail {
34 
35 class shared_library_impl {
36 
37     BOOST_MOVABLE_BUT_NOT_COPYABLE(shared_library_impl)
38 
39 public:
40     typedef void* native_handle_t;
41 
shared_library_impl()42     shared_library_impl() BOOST_NOEXCEPT
43         : handle_(NULL)
44     {}
45 
~shared_library_impl()46     ~shared_library_impl() BOOST_NOEXCEPT {
47         unload();
48     }
49 
shared_library_impl(BOOST_RV_REF (shared_library_impl)sl)50     shared_library_impl(BOOST_RV_REF(shared_library_impl) sl) BOOST_NOEXCEPT
51         : handle_(sl.handle_)
52     {
53         sl.handle_ = NULL;
54     }
55 
operator =(BOOST_RV_REF (shared_library_impl)sl)56     shared_library_impl & operator=(BOOST_RV_REF(shared_library_impl) sl) BOOST_NOEXCEPT {
57         swap(sl);
58         return *this;
59     }
60 
61 
decorate(const boost::dll::fs::path & sl)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())
66             : sl
67         );
68         actual_path += suffix();
69         return actual_path;
70     }
71 
load(boost::dll::fs::path sl,load_mode::type portable_mode,boost::dll::fs::error_code & ec)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);
75         unload();
76 
77         // Do not allow opening NULL paths. User must use program_location() instead
78         if (sl.empty()) {
79             boost::dll::detail::reset_dlerror();
80             ec = boost::dll::fs::make_error_code(
81                 boost::dll::fs::errc::bad_file_descriptor
82             );
83 
84             return;
85         }
86 
87         // Fixing modes
88         if (!(native_mode & load_mode::rtld_now)) {
89             native_mode |= load_mode::rtld_lazy;
90         }
91 
92         if (!(native_mode & load_mode::rtld_global)) {
93             native_mode |= load_mode::rtld_local;
94         }
95 
96 #if BOOST_OS_LINUX || BOOST_OS_ANDROID
97         if (!sl.has_parent_path() && !(native_mode & load_mode::search_system_folders)) {
98             sl = "." / sl;
99         }
100 #else
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) {
105                 prog_loc /= sl;
106                 sl.swap(prog_loc);
107             }
108         }
109 #endif
110 
111         native_mode = static_cast<unsigned>(native_mode) & ~static_cast<unsigned>(load_mode::search_system_folders);
112 
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);
116 
117             boost::dll::fs::path actual_path = decorate(sl);
118             handle_ = dlopen(actual_path.c_str(), native_mode);
119             if (handle_) {
120                 boost::dll::detail::reset_dlerror();
121                 return;
122             }
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
129                 );
130                 return;
131             }
132         }
133 
134         // Opening by exactly specified path
135         handle_ = dlopen(sl.c_str(), native_mode);
136         if (handle_) {
137             boost::dll::detail::reset_dlerror();
138             return;
139         }
140 
141         ec = boost::dll::fs::make_error_code(
142             boost::dll::fs::errc::bad_file_descriptor
143         );
144 
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.
155             ec.clear();
156             boost::dll::detail::reset_dlerror();
157             handle_ = dlopen(NULL, native_mode);
158             if (!handle_) {
159                 ec = boost::dll::fs::make_error_code(
160                     boost::dll::fs::errc::bad_file_descriptor
161                 );
162             }
163         }
164     }
165 
is_loaded() const166     bool is_loaded() const BOOST_NOEXCEPT {
167         return (handle_ != 0);
168     }
169 
unload()170     void unload() BOOST_NOEXCEPT {
171         if (!is_loaded()) {
172             return;
173         }
174 
175         dlclose(handle_);
176         handle_ = 0;
177     }
178 
swap(shared_library_impl & rhs)179     void swap(shared_library_impl& rhs) BOOST_NOEXCEPT {
180         boost::swap(handle_, rhs.handle_);
181     }
182 
full_module_path(boost::dll::fs::error_code & ec) const183     boost::dll::fs::path full_module_path(boost::dll::fs::error_code &ec) const {
184         return boost::dll::detail::path_from_handle(handle_, ec);
185     }
186 
suffix()187     static boost::dll::fs::path suffix() {
188         // https://sourceforge.net/p/predef/wiki/OperatingSystems/
189 #if BOOST_OS_MACOS || BOOST_OS_IOS
190         return ".dylib";
191 #else
192         return ".so";
193 #endif
194     }
195 
symbol_addr(const char * sb,boost::dll::fs::error_code & ec) const196     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
202             );
203         }
204 
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().
209 
210         return symbol;
211     }
212 
native() const213     native_handle_t native() const BOOST_NOEXCEPT {
214         return handle_;
215     }
216 
217 private:
218     native_handle_t         handle_;
219 };
220 
221 }}} // boost::dll::detail
222 
223 #endif // BOOST_DLL_SHARED_LIBRARY_IMPL_HPP
224