#ld-preload #interposition


Dynamic function call interposition / hooking (LD_PRELOAD) for Rust

7 releases (2 stable)

Uses old Rust 2015

2.0.0 Sep 13, 2020
1.0.0 Mar 31, 2019
0.1.2 Jun 5, 2018
0.1.1 Mar 30, 2016
0.0.2 Apr 25, 2015

#1 in #ld-preload

Download history 79/week @ 2021-07-04 30/week @ 2021-07-11 38/week @ 2021-07-18 52/week @ 2021-07-25 47/week @ 2021-08-01 41/week @ 2021-08-08 35/week @ 2021-08-15 29/week @ 2021-08-22 12/week @ 2021-08-29 21/week @ 2021-09-05 26/week @ 2021-09-12 11/week @ 2021-09-19 27/week @ 2021-09-26 41/week @ 2021-10-03 97/week @ 2021-10-10 196/week @ 2021-10-17

146 downloads per month
Used in 4 crates (3 directly)


115 lines


redhook is a helper crate for writing interposition libraries (LD_PRELOAD, DYLD_INSERT_LIBRARIES, etc.) in Rust.

To use redhook, edit your Cargo.toml to add redhook as a dependency and configure your library to build as a dylib:

redhook = "2.0"

name = "mylib"
crate_type = ["dylib"]

Then use the hook! macro to declare the function you want to hook, and the name you want to give to your hook function:

redhook::hook! {
    unsafe fn existing_function(x: i32) -> i32 => my_function {

To access the underlying function, use redhook::real!(existing_function).

Build your library as usual, find the resulting .so file (or .dylib, for macOS) inside the target directory, and set the LD_PRELOAD (or DYLD_INSERT_LIBRARIES) environment variable to that.

(You can also use redhook without Cargo, with rustc --crate-type=dylib.)

There are a few examples included, as separate crates:

geofft@cactuar:~/src/rust/redhook/examples/readlinkspy$ cargo build
   Compiling redhook v0.0.1 (file:///home/geofft/src/rust/redhook/examples/readlinkspy)
   Compiling redhook_ex_readlinkspy v0.0.1 (file:///home/geofft/src/rust/redhook/examples/readlinkspy)
geofft@cactuar:~/src/rust/redhook/examples/readlinkspy$ LD_PRELOAD=target/debug/libreadlinkspy.so ls -l /bin/sh
lrwxrwxrwx 1 root root 4 Feb 19  2014 /bin/sh -> dash
geofft@cactuar:~/src/rust/redhook/examples/fakeroot$ cargo build
geofft@cactuar:~/src/rust/redhook/examples/fakeroot$ LD_PRELOAD=target/debug/libfakeroot.so id
uid=0(root) gid=1001(geofft) euid=1001(geofft) groups=1001(geofft),27(sudo),111(sbuild)

redhook currently supports building interposition libraries for LD_PRELOAD on glibc (GNU/Linux) and DYLD_INSERT_LIBRARIES on Apple's libc (macOS) from the same source code. If you're interested in support for other platforms, please file an issue or pull request.

redhook is named after the Red Hook neighborhood in Brooklyn, New York. Portions of the implementation borrow heavily from concepts in @Kimundi's lazy_static! macro.

redhook is free software, available under the terms of the 2-clause BSD license.

Minimum supported Rust version: 1.32

Build Status