cross-banner

Compiling rust binaries with a custom CRT

Rust shenanigans

Pretty cool space savings huh?

Creating a custom target

First you need a custom target, you can generate a template file for whatever target you need like so

rustc +nightly -Z unstable-options --target=i686-pc-windows-msvc --print target-spec-json

Output:

    {
      "abi-return-struct-as-int": true,
      "arch": "x86",
      "cpu": "pentium4",
      "crt-static-allows-dylibs": true,
      "crt-static-respected": true,
      "data-layout": "e-m:x-p:32:32-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32-a:0:32-S32",
      "dll-prefix": "",
      "dll-suffix": ".dll",
      "dynamic-linking": true,
      "emit-debug-gdb-scripts": false,
      "env": "msvc",
      "exe-suffix": ".exe",
      "executables": true,
      "is-builtin": true,
      "is-like-msvc": true,
      "is-like-windows": true,
      "linker-is-gnu": false,
      "lld-flavor": "link",
      "llvm-target": "i686-pc-windows-msvc",
      "max-atomic-width": 64,
      "no-default-libraries": false,
      "os": "windows",
      "pre-link-args": {
        "lld-link": [
          "/NOLOGO",
          "/LARGEADDRESSAWARE",
          "/SAFESEH"
        ],
        "msvc": [
          "/NOLOGO",
          "/LARGEADDRESSAWARE",
          "/SAFESEH"
        ]
      },
      "requires-uwtable": true,
      "split-debuginfo": "packed",
      "staticlib-prefix": "",
      "staticlib-suffix": ".lib",
      "target-family": [
        "windows"
      ],
      "target-pointer-width": "32",
      "vendor": "pc"
    }

All you need to do is disable SAFESEH , set is-builtin to false, and set no-default-libraries to true.

After that, create a buildscript with

println!("cargo:rustc-link-lib=minicrt"); // or your custom crt of choice 

and set panic to abort in your Cargo.toml

Then, set your target to the custom target.json you created

And finally, compile like so!

cargo build --release -Zbuild-std=std,panic_abort

Now this seems to work for my use case but this is some serious rust brujería so if something doesn’t work for you i probably have no idea why or even how to fix it, sowwy!