I’m trying to produce a statically linked binary on OpenBSD. The reason it needs to be statically linked is that I intend for this binary to run early in the boot process. The binary would live in
/sbin and should have the same linkage characteristics as the other tools in there:
$ ldd /sbin/mount /sbin/mount: Start End Type Open Ref GrpRef Name 000008457a4e3000 000008457a71c000 dlib 1 0 0 /sbin/mount
I’ve done a lot of research but most of the information out there appears largely irrelevant as it pertains to the inability of glibc to be statically linked, the use of musl libc, or Windows. As far as I can tell OpenBSD libc is perfectly capable of being statically linked. I just can’t work out how to convince Rust of this.
Some of the resources I’ve read so far:
Things I’ve tried/considered:
I’ve tried setting
RUSTFLAGS="-C target-feature=+crt-static". It appears the intention of this flag is to enable static linking but it’s ignored on all platforms except Windows and Linux + musl.
I also tried a custom build script (
build.rs) the emits lines like
cargo:rustc-link-lib=static=c but that results in duplicate symbols as it’s trying to dynamically link the same library as well (at least I think that’s what’s happening).
Another approach that Shepmaster proposed in a StackOverflow answer is to take linking into my own hands via a custom target specification and linker invocation. I haven’t gone down this path yet as I’m trying to avoid building the linker invocation manually, but it could be an option.
crt-static kinda looks like what I want I considered changing the libc crate to work like musl on Linux. I’m not sure how I’d get the rust compiler to use this version of libc though, perhaps this is possible with a replace section in Cargo.toml?
Any other thoughts or ideas?