commonlibsse_ng\rel/mod.rs
1//! REL dir portion of `CommonLibSSE-NG` written by hand.
2
3pub mod id;
4pub mod module;
5pub mod offset;
6pub mod relocation;
7pub mod version;
8
9use self::id::DataBaseError;
10use self::module::{ModuleState, ModuleStateError};
11use core::ffi::c_void;
12use core::num::NonZeroUsize;
13use core::ptr::NonNull;
14
15/// A trait for resolving an absolute address based on an offset.
16///
17/// Implementing types must provide an `offset()` method that returns the offset
18/// used to compute the final address.
19///
20/// The base address is retrieved using the `base()` method, which fetches
21/// the module's base address.
22///
23/// # Errors
24/// - If the offset cannot be resolved, `offset()` returns a `DataBaseError`.
25/// - If the base address cannot be retrieved, `base()` returns a `ModuleStateError`.
26pub trait ResolvableAddress {
27 /// Returns the offset associated with this instance.
28 ///
29 /// # Errors
30 /// Returns an error if the offset cannot be determined.
31 fn offset(&self) -> Result<NonZeroUsize, DataBaseError>;
32
33 /// Computes the absolute address by adding the offset to the module's base address.
34 ///
35 /// # Errors
36 ///- If the offset is `0`.
37 /// - Returns `DataBaseError` if the offset cannot be determined.
38 /// - Returns `ModuleStateError` if the base address is unavailable.
39 #[inline]
40 fn address(&self) -> Result<NonNull<c_void>, DataBaseError> {
41 let offset = self.offset()?;
42 Ok(unsafe { Self::base()?.byte_add(offset.get()) })
43 }
44
45 /// Retrieves the base address of the module.
46 ///
47 /// # Errors
48 /// Returns an error if the module is in an invalid state.
49 #[inline]
50 fn base() -> Result<NonNull<c_void>, ModuleStateError> {
51 ModuleState::map_or_init(|module| module.base.0)
52 }
53}