1 // Copyright 2013 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.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
13 Dynamic library facilities.
15 A simple wrapper over the platforms dynamic library facilities
25 pub struct DynamicLibrary { priv handle: *libc::c_void }
27 impl Drop for DynamicLibrary {
29 match do dl::check_for_errors_in {
31 dl::close(self.handle)
35 Err(str) => fail!(str)
41 /// Lazily open a dynamic library. When passed None it gives a
42 /// handle to the calling process
43 pub fn open(filename: Option<&path::Path>) -> Result<DynamicLibrary, ~str> {
45 let maybe_library = do dl::check_for_errors_in {
47 Some(name) => dl::open_external(name),
48 None => dl::open_internal()
52 // The dynamic library must not be constructed if there is
53 // an error opening the library so the destructor does not
57 Ok(handle) => Ok(DynamicLibrary { handle: handle })
62 /// Access the value at the symbol of the dynamic library
63 pub unsafe fn symbol<T>(&self, symbol: &str) -> Result<T, ~str> {
64 // This function should have a lifetime constraint of 'self on
65 // T but that feature is still unimplemented
67 let maybe_symbol_value = do dl::check_for_errors_in {
68 do symbol.as_c_str |raw_string| {
69 dl::symbol(self.handle, raw_string)
73 // The value must not be constructed if there is an error so
74 // the destructor does not run.
75 match maybe_symbol_value {
77 Ok(symbol_value) => Ok(cast::transmute(symbol_value))
92 fn test_loading_cosine() {
93 // The math library does not need to be loaded since it is already
94 // statically linked in
95 let libm = match DynamicLibrary::open(None) {
96 Err(error) => fail!("Could not load self as module: %s", error),
100 // Unfortunately due to issue #6194 it is not possible to call
101 // this as a C function
102 let cosine: extern fn(libc::c_double) -> libc::c_double = unsafe {
103 match libm.symbol("cos") {
104 Err(error) => fail!("Could not load function cos: %s", error),
110 let expected_result = 1.0;
111 let result = cosine(argument);
112 if result != expected_result {
113 fail!("cos(%?) != %? but equaled %? instead", argument,
114 expected_result, result)
119 #[cfg(target_os = "linux")]
120 #[cfg(target_os = "macos")]
121 #[cfg(target_os = "freebsd")]
122 fn test_errors_do_not_crash() {
123 // Open /dev/null as a library to get an error, and make sure
124 // that only causes an error, and not a crash.
125 let path = GenericPath::from_str("/dev/null");
126 match DynamicLibrary::open(Some(&path)) {
128 Ok(_) => fail!("Successfully opened the empty library.")
133 #[cfg(target_os = "linux")]
134 #[cfg(target_os = "android")]
135 #[cfg(target_os = "macos")]
136 #[cfg(target_os = "freebsd")]
142 use unstable::sync::atomically;
145 pub unsafe fn open_external(filename: &path::Path) -> *libc::c_void {
146 do filename.to_str().as_c_str |raw_name| {
147 dlopen(raw_name, Lazy as libc::c_int)
151 pub unsafe fn open_internal() -> *libc::c_void {
152 dlopen(ptr::null(), Lazy as libc::c_int)
155 pub fn check_for_errors_in<T>(f: &fn()->T) -> Result<T, ~str> {
158 let _old_error = dlerror();
162 let last_error = dlerror();
163 if ptr::null() == last_error {
166 Err(str::raw::from_c_str(last_error))
172 pub unsafe fn symbol(handle: *libc::c_void, symbol: *libc::c_char) -> *libc::c_void {
173 dlsym(handle, symbol)
175 pub unsafe fn close(handle: *libc::c_void) {
188 fn dlopen(filename: *libc::c_char, flag: libc::c_int) -> *libc::c_void;
189 fn dlerror() -> *libc::c_char;
190 fn dlsym(handle: *libc::c_void, symbol: *libc::c_char) -> *libc::c_void;
191 fn dlclose(handle: *libc::c_void) -> libc::c_int;
195 #[cfg(target_os = "win32")]
201 use unstable::sync::atomically;
204 pub unsafe fn open_external(filename: &path::Path) -> *libc::c_void {
205 do os::win32::as_utf16_p(filename.to_str()) |raw_name| {
206 LoadLibraryW(raw_name)
210 pub unsafe fn open_internal() -> *libc::c_void {
211 let handle = ptr::null();
212 GetModuleHandleExW(0 as libc::DWORD, ptr::null(), &handle as **libc::c_void);
216 pub fn check_for_errors_in<T>(f: &fn()->T) -> Result<T, ~str> {
223 let error = os::errno();
227 Err(fmt!("Error code %?", error))
232 pub unsafe fn symbol(handle: *libc::c_void, symbol: *libc::c_char) -> *libc::c_void {
233 GetProcAddress(handle, symbol)
235 pub unsafe fn close(handle: *libc::c_void) {
236 FreeLibrary(handle); ()
239 #[link_name = "kernel32"]
241 fn SetLastError(error: u32);
242 fn LoadLibraryW(name: *u16) -> *libc::c_void;
243 fn GetModuleHandleExW(dwFlags: libc::DWORD, name: *u16,
244 handle: **libc::c_void) -> *libc::c_void;
245 fn GetProcAddress(handle: *libc::c_void, name: *libc::c_char) -> *libc::c_void;
246 fn FreeLibrary(handle: *libc::c_void);