retour\arch/
mod.rs

1/// Architecture specific code
2///
3/// The current implementation requires a module to expose some functionality:
4///
5/// - A standalone `relay_builder` function.
6/// This function creates a relay for targets with large displacement, that
7/// requires special attention. An example would be detours further away than
8/// 2GB on x64. A relative jump is not enough, so the `relay_builder`
9/// generates an absolute jump that the relative jump can reach. If it's
10/// needless, `None` can   be returned.
11///
12/// - A `Patcher`, modifies a target in-memory.
13/// - A `Trampoline`, generates a callable address to the target.
14pub use self::detour::Detour;
15
16use cfg_if::cfg_if;
17
18// TODO: flush instruction cache? __clear_cache
19// See: https://github.com/llvm-mirror/compiler-rt/blob/master/lib/builtins/clear_cache.c
20cfg_if! {
21    if #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] {
22        mod x86;
23        use self::x86::{Patcher, Trampoline, meta};
24    } else {
25        // TODO: Implement ARM/AARCH64/MIPS support!
26    }
27}
28
29mod detour;
30mod memory;
31
32/// Returns true if the displacement is within a certain range.
33pub fn is_within_range(displacement: isize) -> bool {
34  let range = meta::DETOUR_RANGE as i64;
35  (-range..range).contains(&(displacement as i64))
36}