Linker version script

Hello,

im currently writing a shared library that i want to preload into other programs to intercept system calls. The current version is written in C but I am thinking about rewriting it in Rust to make use of it's higher level abstractions as the logic of the library is getting more complex.

As I want to preload the library I am currently using a version script to only export the symbols I want to intercept.

My Question: I found no way to specify the needed version script linker option using cargo. Is there any way to achieve this?

You'll need to pass the -C link-arg=-Wl,--version-script,... argument to rustc. You can do this in 3 ways:

  1. Run cargo rustc instead of cargo build. This lets you control the command line for the final rustc invocation.
  2. Set the RUSTFLAGS environment variable. This will be passed to every rustc invocation by Cargo, so this may be problematic if you have build scripts.
  3. Set the rustflags field in the target configuration in your Cargo configuration (this is not the same as your Cargo.toml!).
2 Likes

Ok, thanks, I got it working with cargo rustc when only compiling this crate. As --version-script expects a file argument I also had to add a build script that copies that file to OUT_DIR.
But I currently have this crate within a workspace. As far as I found in the documentation I can only add -l and -L to RUSTFLAGS through the build script and that only the topmost .cargo/config is considered when using cargo build which would then affect all crates built within the workspace.
Is there any way to add these options to only one crate within a workspace?

AFAIK the only ways to pass the argument are those I mentioned before.

1 Like

I've now used a Makefile with:

cargo rustc -p preload -- -C link-arg=-Wl,--version-script=ld.version
cargo build --all --exclude preload

Thank you for your help!

1 Like

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