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
26 pub struct DynamicLibrary { priv handle: *libc::c_void }
28 impl Drop for DynamicLibrary {
30 match do dl::check_for_errors_in {
32 dl::close(self.handle)
36 Err(str) => fail!(str)
42 /// Lazily open a dynamic library. When passed None it gives a
43 /// handle to the calling process
44 pub fn open(filename: Option<&path::Path>) -> Result<DynamicLibrary, ~str> {
45 let open_wrapper = |raw_ptr| {
46 do dl::check_for_errors_in {
48 DynamicLibrary { handle: dl::open(raw_ptr) }
54 Some(name) => do name.to_str().as_c_str |raw_name| {
55 open_wrapper(raw_name)
57 None => open_wrapper(ptr::null())
61 /// Access the value at the symbol of the dynamic library
62 pub unsafe fn symbol<T>(&self, symbol: &str) -> Result<T, ~str> {
63 // This function should have a lifetime constraint of 'self on
64 // T but that feature is still unimplemented
66 do dl::check_for_errors_in {
67 let symbol_value = do symbol.as_c_str |raw_string| {
68 dl::symbol(self.handle, raw_string)
71 cast::transmute(symbol_value)
77 priv fn test_loading_cosine () {
78 // The math library does not need to be loaded since it is already
79 // statically linked in
80 let libm = match DynamicLibrary::open(None) {
81 Err (error) => fail!("Could not load self as module: %s", error),
85 // Unfortunately due to issue #6194 it is not possible to call
86 // this as a C function
87 let cosine: extern fn(libc::c_double) -> libc::c_double = unsafe {
88 match libm.symbol("cos") {
89 Err (error) => fail!("Could not load function cos: %s", error),
95 let expected_result = 1.0;
96 let result = cosine(argument);
97 if result != expected_result {
98 fail!("cos(%?) != %? but equaled %? instead", argument,
99 expected_result, result)
103 #[cfg(target_os = "linux")]
104 #[cfg(target_os = "android")]
105 #[cfg(target_os = "macos")]
106 #[cfg(target_os = "freebsd")]
114 pub unsafe fn open(filename: *libc::c_char) -> *libc::c_void {
115 dlopen(filename, Lazy as libc::c_int)
118 pub fn check_for_errors_in<T>(f: &fn()->T) -> Result<T, ~str> {
120 do task::atomically {
121 let _old_error = dlerror();
125 let last_error = dlerror();
126 if ptr::null() == last_error {
129 Err(str::raw::from_c_str(last_error))
135 pub unsafe fn symbol(handle: *libc::c_void, symbol: *libc::c_char) -> *libc::c_void {
136 dlsym(handle, symbol)
138 pub unsafe fn close(handle: *libc::c_void) {
151 fn dlopen(filename: *libc::c_char, flag: libc::c_int) -> *libc::c_void;
152 fn dlerror() -> *libc::c_char;
153 fn dlsym(handle: *libc::c_void, symbol: *libc::c_char) -> *libc::c_void;
154 fn dlclose(handle: *libc::c_void) -> libc::c_int;
158 #[cfg(target_os = "win32")]
165 pub unsafe fn open(filename: *libc::c_char) -> *libc::c_void {
166 LoadLibrary(filename)
169 pub fn check_for_errors_in<T>(f: &fn()->T) -> Result<T, ~str> {
171 do task::atomically {
176 let error = os::errno();
180 Err(fmt!("Error code %?", error))
185 pub unsafe fn symbol(handle: *libc::c_void, symbol: *libc::c_char) -> *libc::c_void {
186 GetProcAddress(handle, symbol)
188 pub unsafe fn close(handle: *libc::c_void) {
189 FreeLibrary(handle); ()
192 #[link_name = "kernel32"]
194 fn SetLastError(error: u32);
195 fn LoadLibrary(name: *libc::c_char) -> *libc::c_void;
196 fn GetProcAddress(handle: *libc::c_void, name: *libc::c_char) -> *libc::c_void;
197 fn FreeLibrary(handle: *libc::c_void);