├── .gitignore ├── .travis.yml ├── Cargo.toml ├── LICENSE-APACHE ├── LICENSE-MIT ├── demo ├── Cargo.toml └── src │ └── main.rs └── src ├── dynamic_library.rs └── lib.rs /.gitignore: -------------------------------------------------------------------------------- 1 | target 2 | Cargo.lock 3 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: rust 2 | 3 | after_success: 4 | - | 5 | [ $TRAVIS_BRANCH = master ] && 6 | [ $TRAVIS_PULL_REQUEST = false ] && 7 | cargo publish --token ${CRATESIO_TOKEN} 8 | - | 9 | [ $TRAVIS_BRANCH = master ] && 10 | [ $TRAVIS_PULL_REQUEST = false ] && 11 | cargo doc && 12 | echo '' > target/doc/index.html && 13 | git clone https://github.com/davisp/ghp-import && 14 | ./ghp-import/ghp-import -n target/doc && 15 | git push -fq https://${GH_TOKEN}@github.com/${TRAVIS_REPO_SLUG}.git gh-pages 16 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "shared_library" 3 | version = "0.1.9" 4 | authors = ["Pierre Krieger "] 5 | description = "Easily bind to and load shared libraries" 6 | license = "Apache-2.0/MIT" 7 | repository = "https://github.com/tomaka/shared_library/" 8 | 9 | [dependencies] 10 | libc = "0.2" 11 | lazy_static = "1" 12 | -------------------------------------------------------------------------------- /LICENSE-APACHE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. -------------------------------------------------------------------------------- /LICENSE-MIT: -------------------------------------------------------------------------------- 1 | Copyright (c) 2017 Pierre Krieger 2 | 3 | Permission is hereby granted, free of charge, to any 4 | person obtaining a copy of this software and associated 5 | documentation files (the "Software"), to deal in the 6 | Software without restriction, including without 7 | limitation the rights to use, copy, modify, merge, 8 | publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software 10 | is furnished to do so, subject to the following 11 | conditions: 12 | 13 | The above copyright notice and this permission notice 14 | shall be included in all copies or substantial portions 15 | of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF 18 | ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 19 | TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 20 | PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT 21 | SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 22 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 23 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR 24 | IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 25 | DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /demo/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "demo" 3 | version = "0.1.0" 4 | authors = ["Pierre Krieger "] 5 | 6 | [dependencies.shared_library] 7 | path = ".." 8 | 9 | [dependencies] 10 | libc = "0.1" 11 | -------------------------------------------------------------------------------- /demo/src/main.rs: -------------------------------------------------------------------------------- 1 | #[macro_use] 2 | extern crate shared_library; 3 | extern crate libc; 4 | 5 | use std::path::Path; 6 | 7 | shared_library!(Test1, 8 | pub fn hello() -> libc::c_int, 9 | fn hello2(), 10 | ); 11 | 12 | shared_library!(Test2, "libtest.dll", 13 | pub fn hello() -> libc::c_int, 14 | fn hello2(), 15 | 16 | static CONSTANT: &'static u32, 17 | ); 18 | 19 | fn main() { 20 | unsafe { hello() }; 21 | 22 | let test1 = Test1::open(Path::new("libtest.dll")).unwrap(); 23 | unsafe { (test1.hello)() }; 24 | unsafe { (test1.hello2)() }; 25 | } 26 | -------------------------------------------------------------------------------- /src/dynamic_library.rs: -------------------------------------------------------------------------------- 1 | // Copyright 2013-2015 The Rust Project Developers. See the COPYRIGHT 2 | // file at the top-level directory of this distribution and at 3 | // http://rust-lang.org/COPYRIGHT. 4 | // 5 | // Licensed under the Apache License, Version 2.0 or the MIT license 7 | // , at your 8 | // option. This file may not be copied, modified, or distributed 9 | // except according to those terms. 10 | 11 | //! Dynamic library facilities. 12 | //! 13 | //! A simple wrapper over the platform's dynamic library facilities 14 | 15 | #![allow(missing_docs)] 16 | 17 | use std::env; 18 | use std::ffi::{CString, OsString}; 19 | use std::mem; 20 | use std::path::{Path, PathBuf}; 21 | use libc; 22 | 23 | pub struct DynamicLibrary { 24 | handle: *mut u8 25 | } 26 | 27 | unsafe impl Send for DynamicLibrary {} 28 | unsafe impl Sync for DynamicLibrary {} 29 | 30 | impl Drop for DynamicLibrary { 31 | fn drop(&mut self) { 32 | if let Err(str) = dl::check_for_errors_in(|| unsafe { 33 | dl::close(self.handle) 34 | }) { 35 | panic!("{}", str) 36 | } 37 | } 38 | } 39 | 40 | /// Special handles to be used with the `symbol_special` function. These are 41 | /// provided by a GNU only extension and are not included as part of the POSIX 42 | /// standard. 43 | /// 44 | /// See https://linux.die.net/man/3/dlsym for their behaviour. 45 | #[cfg(target_os = "linux")] 46 | pub enum SpecialHandles { 47 | Next, 48 | Default, 49 | } 50 | 51 | impl DynamicLibrary { 52 | // FIXME (#12938): Until DST lands, we cannot decompose &str into 53 | // & and str, so we cannot usefully take ToCStr arguments by 54 | // reference (without forcing an additional & around &str). So we 55 | // are instead temporarily adding an instance for &Path, so that 56 | // we can take ToCStr as owned. When DST lands, the &Path instance 57 | // should be removed, and arguments bound by ToCStr should be 58 | // passed by reference. (Here: in the `open` method.) 59 | 60 | /// Lazily loads the dynamic library named `filename` into memory and 61 | /// then returns an opaque "handle" for that dynamic library. 62 | /// 63 | /// Returns a handle to the calling process when passed `None`. 64 | pub fn open(filename: Option<&Path>) -> Result { 65 | // The dynamic library must not be constructed if there is 66 | // an error opening the library so the destructor does not 67 | // run. 68 | dl::open(filename.map(|path| path.as_os_str())) 69 | .map(|handle| DynamicLibrary { handle }) 70 | } 71 | 72 | /// Prepends a path to this process's search path for dynamic libraries 73 | pub fn prepend_search_path(path: &Path) { 74 | let mut search_path = Self::search_path(); 75 | search_path.insert(0, path.to_path_buf()); 76 | env::set_var(Self::envvar(), &Self::create_path(&search_path)); 77 | } 78 | 79 | /// From a slice of paths, create a new vector which is suitable to be an 80 | /// environment variable for this platforms dylib search path. 81 | pub fn create_path(path: &[PathBuf]) -> OsString { 82 | let mut newvar = OsString::new(); 83 | for (i, path) in path.iter().enumerate() { 84 | if i > 0 { newvar.push(Self::separator()); } 85 | newvar.push(path); 86 | } 87 | newvar 88 | } 89 | 90 | /// Returns the environment variable for this process's dynamic library 91 | /// search path 92 | pub fn envvar() -> &'static str { 93 | if cfg!(windows) { 94 | "PATH" 95 | } else if cfg!(target_os = "macos") { 96 | "DYLD_LIBRARY_PATH" 97 | } else { 98 | "LD_LIBRARY_PATH" 99 | } 100 | } 101 | 102 | //TODO: turn this and `envvar` into associated constants 103 | fn separator() -> &'static str { 104 | if cfg!(windows) { ";" } else { ":" } 105 | } 106 | 107 | /// Returns the current search path for dynamic libraries being used by this 108 | /// process 109 | pub fn search_path() -> Vec { 110 | match env::var_os(Self::envvar()) { 111 | Some(var) => env::split_paths(&var).collect(), 112 | None => Vec::new(), 113 | } 114 | } 115 | 116 | /// Returns the address of where symbol `symbol` was loaded into memory. 117 | /// 118 | /// In POSIX compliant systems, we return 'Err' if the symbol was not found, 119 | /// in this library or any of the libraries that were automatically loaded 120 | /// when this library was loaded. 121 | pub unsafe fn symbol(&self, symbol: &str) -> Result<*mut T, String> { 122 | // This function should have a lifetime constraint of 'a on 123 | // T but that feature is still unimplemented 124 | 125 | let raw_string = CString::new(symbol).unwrap(); 126 | // The value must not be constructed if there is an error so 127 | // the destructor does not run. 128 | dl::check_for_errors_in(|| { 129 | dl::symbol(self.handle as *mut libc::c_void, raw_string.as_ptr() as *const _) 130 | }) 131 | .map(|sym| mem::transmute(sym)) 132 | } 133 | 134 | /// Returns the address of the first occurance of symbol `symbol` using the 135 | /// default library search order if you use `SpecialHandles::Default`. 136 | /// 137 | /// Returns the address of the next occurance of symbol `symbol` after the 138 | /// current library in the default library search order if you use 139 | /// `SpecialHandles::Next`. 140 | #[cfg(target_os = "linux")] 141 | pub unsafe fn symbol_special(handle: SpecialHandles, symbol: &str) -> Result<*mut T, String> { 142 | // This function should have a lifetime constraint of 'a on 143 | // T but that feature is still unimplemented 144 | 145 | let handle = match handle { 146 | SpecialHandles::Next => mem::transmute::(-1), 147 | SpecialHandles::Default => ::std::ptr::null_mut(), 148 | }; 149 | 150 | let raw_string = CString::new(symbol).unwrap(); 151 | // The value must not be constructed if there is an error so 152 | // the destructor does not run. 153 | dl::check_for_errors_in(|| { 154 | dl::symbol(handle, raw_string.as_ptr() as *const _) 155 | }) 156 | .map(|sym| mem::transmute(sym)) 157 | } 158 | } 159 | 160 | #[cfg(all(test, not(target_os = "ios")))] 161 | mod test { 162 | use super::*; 163 | use std::mem; 164 | use std::path::Path; 165 | 166 | #[test] 167 | #[cfg_attr(any(windows, target_os = "android"), ignore)] // FIXME #8818, #10379 168 | fn test_loading_cosine() { 169 | // The math library does not need to be loaded since it is already 170 | // statically linked in 171 | let libm = match DynamicLibrary::open(None) { 172 | Err(error) => panic!("Could not load self as module: {}", error), 173 | Ok(libm) => libm 174 | }; 175 | 176 | let cosine: extern fn(libc::c_double) -> libc::c_double = unsafe { 177 | match libm.symbol("cos") { 178 | Err(error) => panic!("Could not load function cos: {}", error), 179 | Ok(cosine) => mem::transmute::<*mut u8, _>(cosine) 180 | } 181 | }; 182 | 183 | let argument = 0.0; 184 | let expected_result = 1.0; 185 | let result = cosine(argument); 186 | if result != expected_result { 187 | panic!("cos({}) != {} but equaled {} instead", argument, 188 | expected_result, result) 189 | } 190 | } 191 | 192 | #[test] 193 | #[cfg(any(target_os = "linux", 194 | target_os = "macos", 195 | target_os = "freebsd", 196 | target_os = "fuchsia", 197 | target_os = "netbsd", 198 | target_os = "dragonfly", 199 | target_os = "bitrig", 200 | target_os = "openbsd", 201 | target_os = "solaris"))] 202 | fn test_errors_do_not_crash() { 203 | // Open /dev/null as a library to get an error, and make sure 204 | // that only causes an error, and not a crash. 205 | let path = Path::new("/dev/null"); 206 | match DynamicLibrary::open(Some(&path)) { 207 | Err(_) => {} 208 | Ok(_) => panic!("Successfully opened the empty library.") 209 | } 210 | } 211 | } 212 | 213 | //TODO: use `unix` shortcut? 214 | #[cfg(any(target_os = "linux", 215 | target_os = "android", 216 | target_os = "macos", 217 | target_os = "ios", 218 | target_os = "fuchsia", 219 | target_os = "freebsd", 220 | target_os = "netbsd", 221 | target_os = "dragonfly", 222 | target_os = "bitrig", 223 | target_os = "openbsd", 224 | target_os = "solaris", 225 | target_os = "emscripten"))] 226 | mod dl { 227 | use std::ffi::{CString, CStr, OsStr}; 228 | use std::os::unix::ffi::OsStrExt; 229 | use std::str; 230 | use libc; 231 | use std::ptr; 232 | use std::sync::Mutex; 233 | 234 | lazy_static! { 235 | static ref LOCK: Mutex<()> = Mutex::new(()); 236 | } 237 | 238 | pub fn open(filename: Option<&OsStr>) -> Result<*mut u8, String> { 239 | check_for_errors_in(|| unsafe { 240 | match filename { 241 | Some(filename) => open_external(filename), 242 | None => open_internal(), 243 | } 244 | }) 245 | } 246 | 247 | const LAZY: libc::c_int = 1; 248 | 249 | unsafe fn open_external(filename: &OsStr) -> *mut u8 { 250 | let s = CString::new(filename.as_bytes().to_vec()).unwrap(); 251 | dlopen(s.as_ptr() as *const _, LAZY) as *mut u8 252 | } 253 | 254 | unsafe fn open_internal() -> *mut u8 { 255 | dlopen(ptr::null(), LAZY) as *mut u8 256 | } 257 | 258 | pub fn check_for_errors_in(f: F) -> Result where 259 | F: FnOnce() -> T, 260 | { 261 | unsafe { 262 | // dlerror isn't thread safe, so we need to lock around this entire 263 | // sequence 264 | let _guard = LOCK.lock(); 265 | let _old_error = dlerror(); 266 | 267 | let result = f(); 268 | 269 | let last_error = dlerror() as *const _; 270 | let ret = if ptr::null() == last_error { 271 | Ok(result) 272 | } else { 273 | let s = CStr::from_ptr(last_error).to_bytes(); 274 | Err(str::from_utf8(s).unwrap().to_string()) 275 | }; 276 | 277 | ret 278 | } 279 | } 280 | 281 | pub unsafe fn symbol( 282 | handle: *mut libc::c_void, 283 | symbol: *const libc::c_char, 284 | ) -> *mut u8 { 285 | dlsym(handle, symbol) as *mut u8 286 | } 287 | 288 | pub unsafe fn close(handle: *mut u8) { 289 | dlclose(handle as *mut libc::c_void); () 290 | } 291 | 292 | extern { 293 | fn dlopen( 294 | filename: *const libc::c_char, 295 | flag: libc::c_int, 296 | ) -> *mut libc::c_void; 297 | fn dlerror() -> *mut libc::c_char; 298 | fn dlsym( 299 | handle: *mut libc::c_void, 300 | symbol: *const libc::c_char, 301 | ) -> *mut libc::c_void; 302 | fn dlclose( 303 | handle: *mut libc::c_void, 304 | ) -> libc::c_int; 305 | } 306 | } 307 | 308 | #[cfg(target_os = "windows")] 309 | mod dl { 310 | use std::ffi::OsStr; 311 | use std::iter::Iterator; 312 | use libc; 313 | use std::ops::FnOnce; 314 | use std::io::Error as IoError; 315 | use std::os::windows::prelude::*; 316 | use std::option::Option::{self, Some, None}; 317 | use std::ptr; 318 | use std::result::Result; 319 | use std::result::Result::{Ok, Err}; 320 | use std::string::String; 321 | use std::vec::Vec; 322 | 323 | pub fn open(filename: Option<&OsStr>) -> Result<*mut u8, String> { 324 | // disable "dll load failed" error dialog. 325 | let prev_error_mode = unsafe { 326 | // SEM_FAILCRITICALERRORS 0x01 327 | let new_error_mode = 1; 328 | SetErrorMode(new_error_mode) 329 | }; 330 | 331 | unsafe { 332 | SetLastError(0); 333 | } 334 | 335 | let result = match filename { 336 | Some(filename) => { 337 | let filename_str: Vec<_> = 338 | filename.encode_wide().chain(Some(0).into_iter()).collect(); 339 | let result = unsafe { 340 | LoadLibraryW(filename_str.as_ptr() as *const libc::c_void) 341 | }; 342 | // beware: Vec/String may change errno during drop! 343 | // so we get error here. 344 | if result == ptr::null_mut() { 345 | Err(format!("{}", IoError::last_os_error())) 346 | } else { 347 | Ok(result as *mut u8) 348 | } 349 | } 350 | None => { 351 | let mut handle = ptr::null_mut(); 352 | let succeeded = unsafe { 353 | GetModuleHandleExW(0, ptr::null(), &mut handle) 354 | }; 355 | if succeeded == 0 { 356 | Err(format!("{}", IoError::last_os_error())) 357 | } else { 358 | Ok(handle as *mut u8) 359 | } 360 | } 361 | }; 362 | 363 | unsafe { 364 | SetErrorMode(prev_error_mode); 365 | } 366 | 367 | result 368 | } 369 | 370 | pub fn check_for_errors_in(f: F) -> Result where 371 | F: FnOnce() -> T, 372 | { 373 | unsafe { 374 | SetLastError(0); 375 | 376 | let result = f(); 377 | 378 | let error = IoError::last_os_error(); 379 | if 0 == error.raw_os_error().unwrap() { 380 | Ok(result) 381 | } else { 382 | Err(format!("{}", error)) 383 | } 384 | } 385 | } 386 | 387 | pub unsafe fn symbol(handle: *mut libc::c_void, symbol: *const libc::c_char) -> *mut u8 { 388 | GetProcAddress(handle, symbol) as *mut u8 389 | } 390 | pub unsafe fn close(handle: *mut u8) { 391 | FreeLibrary(handle as *mut libc::c_void); () 392 | } 393 | 394 | #[allow(non_snake_case)] 395 | extern "system" { 396 | fn SetLastError(error: libc::size_t); 397 | fn LoadLibraryW(name: *const libc::c_void) -> *mut libc::c_void; 398 | fn GetModuleHandleExW( 399 | dwFlags: u32, 400 | name: *const u16, 401 | handle: *mut *mut libc::c_void, 402 | ) -> i32; 403 | fn GetProcAddress( 404 | handle: *mut libc::c_void, 405 | name: *const libc::c_char, 406 | ) -> *mut libc::c_void; 407 | fn FreeLibrary(handle: *mut libc::c_void); 408 | fn SetErrorMode(uMode: libc::c_uint) -> libc::c_uint; 409 | } 410 | } 411 | -------------------------------------------------------------------------------- /src/lib.rs: -------------------------------------------------------------------------------- 1 | extern crate libc; 2 | 3 | #[macro_use] 4 | extern crate lazy_static; 5 | 6 | pub mod dynamic_library; 7 | 8 | /// Error that can happen while loading the shared library. 9 | #[derive(Debug, Clone)] 10 | pub enum LoadingError { 11 | /// 12 | LibraryNotFound { 13 | descr: String, 14 | }, 15 | 16 | /// One of the symbols could not be found in the library. 17 | SymbolNotFound { 18 | /// The symbol. 19 | symbol: &'static str, 20 | } 21 | } 22 | 23 | #[macro_export] 24 | macro_rules! shared_library { 25 | ($struct_name:ident, pub $($rest:tt)+) => { 26 | shared_library!(__impl $struct_name [] [] [] pub $($rest)+); 27 | }; 28 | 29 | ($struct_name:ident, fn $($rest:tt)+) => { 30 | shared_library!(__impl $struct_name [] [] [] fn $($rest)+); 31 | }; 32 | 33 | ($struct_name:ident, static $($rest:tt)+) => { 34 | shared_library!(__impl $struct_name [] [] [] static $($rest)+); 35 | }; 36 | 37 | ($struct_name:ident, $def_path:expr, $($rest:tt)+) => { 38 | shared_library!(__impl $struct_name [] [$def_path] [] $($rest)+); 39 | }; 40 | 41 | (__impl $struct_name:ident 42 | [$($p1:tt)*] [$($p2:tt)*] [$($p3:tt)*] 43 | , $($rest:tt)* 44 | ) => { 45 | shared_library!(__impl $struct_name [$($p1)*] [$($p2)*] [$($p3)*] $($rest)*); 46 | }; 47 | 48 | (__impl $struct_name:ident 49 | [$($p1:tt)*] [$($p2:tt)*] [$($p3:tt)*] 50 | pub $($rest:tt)* 51 | ) => { 52 | shared_library!(__impl $struct_name 53 | [$($p1)*] [$($p2)*] [$($p3)* pub] $($rest)*); 54 | }; 55 | 56 | (__impl $struct_name:ident 57 | [$($p1:tt)*] [$($p2:tt)*] [$($p3:tt)*] 58 | fn $name:ident($($p:ident:$ty:ty),*) -> $ret:ty, $($rest:tt)* 59 | ) => { 60 | shared_library!(__impl $struct_name 61 | [$($p1)*, $name:unsafe extern fn($($p:$ty),*) -> $ret] 62 | [$($p2)*] 63 | [$($p3)* 64 | unsafe fn $name($($p:$ty),*) -> $ret { 65 | #![allow(dead_code)] 66 | ($struct_name::get_static_ref().$name)($($p),*) 67 | } 68 | ] $($rest)*); 69 | }; 70 | 71 | (__impl $struct_name:ident 72 | [$($p1:tt)*] [$($p2:tt)*] [$($p3:tt)*] 73 | static $name:ident:$ty:ty, $($rest:tt)* 74 | ) => { 75 | shared_library!(__impl $struct_name 76 | [$($p1)*, $name: $ty] 77 | [$($p2)*] 78 | [$($p3)*] $($rest)*); 79 | }; 80 | 81 | (__impl $struct_name:ident 82 | [$($p1:tt)*] [$($p2:tt)*] [$($p3:tt)*] 83 | fn $name:ident($($p:ident:$ty:ty),*), $($rest:tt)* 84 | ) => { 85 | shared_library!(__impl $struct_name 86 | [$($p1)*] [$($p2)*] [$($p3)*] 87 | fn $name($($p:$ty),*) -> (), $($rest)*); 88 | }; 89 | 90 | (__impl $struct_name:ident [$(,$mem_n:ident:$mem_t:ty)+] [$($p2:tt)*] [$($p3:tt)*]) => { 91 | /// Symbols loaded from a shared library. 92 | #[allow(non_snake_case)] 93 | pub struct $struct_name { 94 | _library_guard: $crate::dynamic_library::DynamicLibrary, 95 | $( 96 | pub $mem_n: $mem_t, 97 | )+ 98 | } 99 | 100 | impl $struct_name { 101 | /// Tries to open the dynamic library. 102 | #[allow(non_snake_case)] 103 | pub fn open(path: &::std::path::Path) -> Result<$struct_name, $crate::LoadingError> { 104 | use std::mem; 105 | 106 | let dylib = match $crate::dynamic_library::DynamicLibrary::open(Some(path)) { 107 | Ok(l) => l, 108 | Err(reason) => return Err($crate::LoadingError::LibraryNotFound { descr: reason }) 109 | }; 110 | 111 | $( 112 | let $mem_n: *mut () = match unsafe { dylib.symbol(stringify!($mem_n)) } { 113 | Ok(s) => s, 114 | Err(_) => return Err($crate::LoadingError::SymbolNotFound { symbol: stringify!($mem_n) }), 115 | }; 116 | )+ 117 | 118 | Ok($struct_name { 119 | _library_guard: dylib, 120 | $( 121 | $mem_n: unsafe { mem::transmute($mem_n) }, 122 | )+ 123 | }) 124 | } 125 | } 126 | 127 | shared_library!(__write_static_fns $struct_name [] [$($p2)*] [$($p3)*]); 128 | }; 129 | 130 | (__write_static_fns $struct_name:ident [$($p1:tt)*] [] [$($p3:tt)*]) => { 131 | }; 132 | 133 | (__write_static_fns $struct_name:ident [$($p1:tt)*] [$defpath:expr] [$($standalones:item)+]) => { 134 | impl $struct_name { 135 | /// This function is used by the regular functions. 136 | fn get_static_ref() -> &'static $struct_name { 137 | $struct_name::try_loading().ok() 138 | .expect(concat!("Could not open dynamic \ 139 | library `", stringify!($struct_name), 140 | "`")) 141 | } 142 | 143 | /// Try loading the static symbols linked to this library. 144 | pub fn try_loading() -> Result<&'static $struct_name, $crate::LoadingError> { 145 | use std::sync::{Mutex, Once, ONCE_INIT}; 146 | use std::mem; 147 | 148 | unsafe { 149 | static mut DATA: *const Mutex> = 0 as *const _; 150 | 151 | static mut INIT: Once = ONCE_INIT; 152 | INIT.call_once(|| { 153 | let data = Box::new(Mutex::new(None)); 154 | DATA = &*data; 155 | mem::forget(data); 156 | }); 157 | 158 | let data: &Mutex> = &*DATA; 159 | let mut data = data.lock().unwrap(); 160 | 161 | if let Some(ref data) = *data { 162 | return Ok(mem::transmute(data)); 163 | } 164 | 165 | let path = ::std::path::Path::new($defpath); 166 | let result = try!($struct_name::open(path)); 167 | *data = Some(result); 168 | Ok(mem::transmute(data.as_ref().unwrap())) 169 | } 170 | } 171 | } 172 | 173 | $($standalones)+ 174 | }; 175 | } 176 | --------------------------------------------------------------------------------