Override c standard functions with ld_preload (without using crates)

I want to override c standard functions (like free() or fopen()) using LD_PRELOAD in linux. In C the solution is simple. You just need to override the function, and compile it as a shared object library.

But how can I do this in Rust ? (Without using crates like redhook)

You can compile your crate as a shared object library by setting the crate-type to cdylib.

# Cargo.toml

crate-type = ["cdylib"]

When it gets compiled, you should then get a target/debug/libfoo.so (or target/release/libfoo.so if compiling with optimisations).

From there you'll need to set LD_PRELOAD, and make sure your library exports (see the #[no_mangle] attribute) the desired functions.

See the reference and edition guide for more. You may also want to look at The Reference's FFI Chapter for tips on writing FFI code in Rust, if you've never done this before.


This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.