]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/dll/detail/posix/shared_library_impl.hpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / boost / dll / detail / posix / shared_library_impl.hpp
1 // Copyright 2014 Renato Tegon Forti, Antony Polukhin.
2 // Copyright 2015-2016 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/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/filesystem/path.hpp>
19 #include <boost/filesystem/operations.hpp>
20 #include <boost/predef/os.h>
21
22 #include <dlfcn.h>
23 #include <cstring> // strncmp
24 #if !BOOST_OS_MACOS && !BOOST_OS_IOS && !BOOST_OS_QNX
25 # include <link.h>
26 #elif BOOST_OS_QNX
27 // QNX's copy of <elf.h> and <link.h> reside in sys folder
28 # include <sys/link.h>
29 #endif
30
31 #ifdef BOOST_HAS_PRAGMA_ONCE
32 # pragma once
33 #endif
34
35 namespace boost { namespace dll { namespace detail {
36
37 class shared_library_impl {
38
39 BOOST_MOVABLE_BUT_NOT_COPYABLE(shared_library_impl)
40
41 public:
42 typedef void* native_handle_t;
43
44 shared_library_impl() BOOST_NOEXCEPT
45 : handle_(NULL)
46 {}
47
48 ~shared_library_impl() BOOST_NOEXCEPT {
49 unload();
50 }
51
52 shared_library_impl(BOOST_RV_REF(shared_library_impl) sl) BOOST_NOEXCEPT
53 : handle_(sl.handle_)
54 {
55 sl.handle_ = NULL;
56 }
57
58 shared_library_impl & operator=(BOOST_RV_REF(shared_library_impl) sl) BOOST_NOEXCEPT {
59 swap(sl);
60 return *this;
61 }
62
63 void load(boost::filesystem::path sl, load_mode::type mode, boost::system::error_code &ec) {
64 typedef int native_mode_t;
65 unload();
66
67 // Do not allow opening NULL paths. User must use program_location() instead
68 if (sl.empty()) {
69 boost::dll::detail::reset_dlerror();
70 ec = boost::system::error_code(
71 boost::system::errc::bad_file_descriptor,
72 boost::system::generic_category()
73 );
74
75 return;
76 }
77
78 // Fixing modes
79 if (!(mode & load_mode::rtld_now)) {
80 mode |= load_mode::rtld_lazy;
81 }
82
83 if (!(mode & load_mode::rtld_global)) {
84 mode |= load_mode::rtld_local;
85 }
86
87 #if BOOST_OS_LINUX || BOOST_OS_ANDROID
88 if (!sl.has_parent_path() && !(mode & load_mode::search_system_folders)) {
89 sl = "." / sl;
90 }
91 #else
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) {
96 prog_loc /= sl;
97 sl.swap(prog_loc);
98 }
99 }
100 #endif
101
102 mode &= ~load_mode::search_system_folders;
103
104 // Trying to open with appended decorations
105 if (!!(mode & load_mode::append_decorations)) {
106 mode &= ~load_mode::append_decorations;
107
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()
111 : sl
112 );
113 actual_path += suffix();
114
115 handle_ = dlopen(actual_path.c_str(), static_cast<native_mode_t>(mode));
116 if (handle_) {
117 boost::dll::detail::reset_dlerror();
118 return;
119 }
120 }
121
122 // Opening by exactly specified path
123 handle_ = dlopen(sl.c_str(), static_cast<native_mode_t>(mode));
124 if (handle_) {
125 boost::dll::detail::reset_dlerror();
126 return;
127 }
128
129 ec = boost::system::error_code(
130 boost::system::errc::bad_file_descriptor,
131 boost::system::generic_category()
132 );
133
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.
144 ec.clear();
145 boost::dll::detail::reset_dlerror();
146 handle_ = dlopen(NULL, static_cast<native_mode_t>(mode));
147 if (!handle_) {
148 ec = boost::system::error_code(
149 boost::system::errc::bad_file_descriptor,
150 boost::system::generic_category()
151 );
152 }
153 }
154 }
155
156 bool is_loaded() const BOOST_NOEXCEPT {
157 return (handle_ != 0);
158 }
159
160 void unload() BOOST_NOEXCEPT {
161 if (!is_loaded()) {
162 return;
163 }
164
165 dlclose(handle_);
166 handle_ = 0;
167 }
168
169 void swap(shared_library_impl& rhs) BOOST_NOEXCEPT {
170 boost::swap(handle_, rhs.handle_);
171 }
172
173 boost::filesystem::path full_module_path(boost::system::error_code &ec) const {
174 return boost::dll::detail::path_from_handle(handle_, ec);
175 }
176
177 static boost::filesystem::path suffix() {
178 // https://sourceforge.net/p/predef/wiki/OperatingSystems/
179 #if BOOST_OS_MACOS || BOOST_OS_IOS
180 return ".dylib";
181 #else
182 return ".so";
183 #endif
184 }
185
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()
193 );
194 }
195
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().
200
201 return symbol;
202 }
203
204 native_handle_t native() const BOOST_NOEXCEPT {
205 return handle_;
206 }
207
208 private:
209 native_handle_t handle_;
210 };
211
212 }}} // boost::dll::detail
213
214 #endif // BOOST_DLL_SHARED_LIBRARY_IMPL_HPP
215