#linking #c #resolve #linker #block #symbols #blocks #line #extern #strict

strict_linking

Requires extern "C" blocks to resolve to symbols during linking

5 releases

Uses new Rust 2021

0.1.4 Apr 10, 2022
0.1.3 Apr 4, 2022
0.1.2 Apr 4, 2022
0.1.1 Apr 3, 2022
0.1.0 Apr 3, 2022

#137 in Build Utils

MIT/Apache

8KB
159 lines

strict_linking_rs

Enforces the requirement that all functions defined inside your crate's extern "C" blocks must be resolved in the final executable.

Warning

This software has not been tested on many systems and is known to be incompatible with some project setups. It's considered to be "In Beta". Our aim is to be compatible with as much as possible, however linkers are moody and fickle. I welcome issue reports and minimal reproductions. Additionally, using this may significantly increase your build time.

To use

In your build.rs file add this line to your main() function

strict_linking::init();

And then in your Cargo.toml file add this

[build-dependencies]
strict_linking = "0.1"

How does it work?

First, we use cargo +nightly rustc --profile=check -- -Zunpretty-expanded to expand macros inside the code for your crate. Then we parse that with the syn crate, and walk down the syntax tree recursively, looking for extern "C" blocks. We use the information from these to build a linker script, which we'll then feed into your linker via cargo:rustc-link-arg. The linker script uses platform specific flags like /INCLUDE and --undefined so that the linker will not link successfully if one of those symbols is missing.

Dependencies

~200–580KB
~14K SLoC