Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add function for getting raw handle from library #10

Merged
merged 3 commits into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions dlopen2/src/raw/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@ use std::ffi::{CStr, CString, OsStr};
#[cfg(unix)]
use super::unix::{
addr_info_cleanup, addr_info_init, addr_info_obtain, close_lib, get_sym, open_lib, open_self,
Handle,
};
#[cfg(windows)]
use super::windows::{
addr_info_cleanup, addr_info_init, addr_info_obtain, close_lib, get_sym, open_lib, open_self,
Handle,
};

#[cfg(unix)]
pub use super::unix::Handle;
#[cfg(windows)]
pub use super::windows::Handle;

use std::mem::{size_of, transmute_copy};

/**
Expand All @@ -37,6 +40,7 @@ pub struct Library {
}

impl Library {

/**
Open a dynamic library.

Expand Down Expand Up @@ -116,7 +120,7 @@ impl Library {
}

/**
Obtain symbol from opened library.
Obtains a symbol from the opened library.

**Note:** the `T` template type needs to have a size of a pointer.
Because Rust does not support static casts at the moment, the size of the type
Expand Down Expand Up @@ -152,6 +156,7 @@ impl Library {
let cname = CString::new(name)?;
self.symbol_cstr(cname.as_ref())
}

///Equivalent of the `symbol` method but takes `CStr` as a argument.
pub unsafe fn symbol_cstr<T>(&self, name: &CStr) -> Result<T, Error> {
//TODO: convert it to some kind of static assertion (not yet supported in Rust)
Expand All @@ -169,6 +174,15 @@ impl Library {
Ok(transmute_copy(&raw))
}
}

/**
Returns the raw OS handle for the opened library.

This is `HMODULE` on Windows and `*mut c_void` on Unix systems. Don't use unless absolutely necessary.
*/
pub unsafe fn into_raw(&self) -> Handle {
return self.handle;
}
}

impl Drop for Library {
Expand Down
2 changes: 1 addition & 1 deletion dlopen2/src/raw/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@ mod unix;
#[cfg(windows)]
mod windows;

pub use self::common::{AddressInfo, AddressInfoObtainer, Library, OverlappingSymbol};
pub use self::common::{AddressInfo, AddressInfoObtainer, Library, OverlappingSymbol, Handle};
11 changes: 11 additions & 0 deletions dlopen2/src/symbor/container.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use crate::raw;

use super::super::Error;
use super::api::SymBorApi;
use super::Library;
Expand Down Expand Up @@ -73,6 +75,15 @@ where
let api = T::load(static_ref)?;
Ok(Self { api, lib })
}

/**
Returns the raw OS handle for the opened library.

This is `HMODULE` on Windows and `*mut c_void` on Unix systems. Don't use unless absolutely necessary.
*/
pub unsafe fn into_raw(&self) -> raw::Handle {
self.lib.into_raw()
}
}

impl<T> Deref for Container<T>
Expand Down
11 changes: 11 additions & 0 deletions dlopen2/src/symbor/library.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use crate::raw;

use super::super::raw::Library as RawLib;
use super::ptr_or_null::PtrOrNull;
use super::ptr_or_null_mut::PtrOrNullMut;
Expand Down Expand Up @@ -143,6 +145,15 @@ impl Library {
pub unsafe fn reference_mut_cstr<T>(&self, name: &CStr) -> Result<&mut T, Error> {
self.lib.symbol_cstr(name)
}

/**
Returns the raw OS handle for the opened library.

This is `HMODULE` on Windows and `*mut c_void` on Unix systems. Don't use unless absolutely necessary.
*/
pub unsafe fn into_raw(&self) -> raw::Handle {
self.lib.into_raw()
}
}

unsafe impl Send for Library {}
Expand Down
15 changes: 12 additions & 3 deletions dlopen2/src/wrapper/container.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use crate::raw;

use super::super::raw::Library;
use super::super::Error;
use super::api::WrapperApi;
Expand Down Expand Up @@ -56,7 +58,7 @@ impl<T> Container<T>
where
T: WrapperApi,
{
///Open the library using provided file name or path and load all symbols.
/// Open the library using provided file name or path and load all symbols.
pub unsafe fn load<S>(name: S) -> Result<Container<T>, Error>
where
S: AsRef<OsStr>,
Expand All @@ -65,7 +67,7 @@ where
let api = T::load(&lib)?;
Ok(Self { lib, api })
}
///Load all symbols from the program itself.
/// Load all symbols from the program itself.
///
/// This allows a shared library to load symbols of the program it was
/// loaded into.
Expand All @@ -75,7 +77,14 @@ where
Ok(Self { lib, api })
}

///Same as load(), except specify flags used by libc::dlopen
/// Returns the raw OS handle for the opened library.
///
/// This is `HMODULE` on Windows and `*mut c_void` on Unix systems. Don't use unless absolutely necessary.
pub unsafe fn into_raw(&self) -> raw::Handle {
self.lib.into_raw()
}

/// Same as load(), except specify flags used by libc::dlopen
pub unsafe fn load_with_flags<S>(name: S, flags: Option<i32>) -> Result<Container<T>, Error>
where
S: AsRef<OsStr>,
Expand Down
Loading