Cross compile Rust for mipsel-linux-muslsf

Published: 05.02.2024 | 643 Words | 4 minutes
Instructions how to setup rust to cross compile for a Microtik RouterBoard RB75Gr3 running OpenWrt with a MIPS 1004Kc cpu.

Information about the cpu- 32Bit

  • 32 Bit

  • Litle endian

  • There is a model 1004Kf which has support for floating point numbers but ours doesn’t [1]

Output from cpuinfo
root@OpenWrt:~# cat /proc/cpuinfo
system type		: MediaTek MT7621 ver:1 eco:4
machine			: MikroTik RouterBOARD 750Gr3
processor		: 0
cpu model		: MIPS 1004Kc V2.15
BogoMIPS		: 586.13
wait instruction	: yes
microsecond timers	: yes
tlb_entries		: 32
extra interrupt vector	: yes
hardware watchpoint	: yes, count: 4, address/irw mask: [0x0ffc, 0x0ffc, 0x0ffb, 0x0ffb]
isa			: mips1 mips2 mips32r1 mips32r2
ASEs implemented	: mips16 dsp mt
Options implemented	: tlb 4kex 4k_cache prefetch mcheck ejtag llsc pindexed_dcache userlocal vint perf_cntr_intr_bit cdmm perf
shadow register sets	: 1
kscratch registers	: 0
package			: 0
core			: 0
VPE			: 0
VCED exceptions		: not available
VCEI exceptions		: not available

processor		: 1
cpu model		: MIPS 1004Kc V2.15
BogoMIPS		: 586.13
wait instruction	: yes
microsecond timers	: yes
tlb_entries		: 32
extra interrupt vector	: yes
hardware watchpoint	: yes, count: 4, address/irw mask: [0x0ffc, 0x0ffc, 0x0ffb, 0x0ffb]
isa			: mips1 mips2 mips32r1 mips32r2
ASEs implemented	: mips16 dsp mt
Options implemented	: tlb 4kex 4k_cache prefetch mcheck ejtag llsc pindexed_dcache userlocal vint perf_cntr_intr_bit cdmm perf
shadow register sets	: 1
kscratch registers	: 0
package			: 0
core			: 0
VPE			: 1
VCED exceptions		: not available
VCEI exceptions		: not available

processor		: 2
cpu model		: MIPS 1004Kc V2.15
BogoMIPS		: 586.13
wait instruction	: yes
microsecond timers	: yes
tlb_entries		: 32
extra interrupt vector	: yes
hardware watchpoint	: yes, count: 4, address/irw mask: [0x0ffc, 0x0ffc, 0x0ffb, 0x0ffb]
isa			: mips1 mips2 mips32r1 mips32r2
ASEs implemented	: mips16 dsp mt
Options implemented	: tlb 4kex 4k_cache prefetch mcheck ejtag llsc pindexed_dcache userlocal vint perf_cntr_intr_bit cdmm perf
shadow register sets	: 1
kscratch registers	: 0
package			: 0
core			: 1
VPE			: 0
VCED exceptions		: not available
VCEI exceptions		: not available

processor		: 3
cpu model		: MIPS 1004Kc V2.15
BogoMIPS		: 586.13
wait instruction	: yes
microsecond timers	: yes
tlb_entries		: 32
extra interrupt vector	: yes
hardware watchpoint	: yes, count: 4, address/irw mask: [0x0ffc, 0x0ffc, 0x0ffb, 0x0ffb]
isa			: mips1 mips2 mips32r1 mips32r2
ASEs implemented	: mips16 dsp mt
Options implemented	: tlb 4kex 4k_cache prefetch mcheck ejtag llsc pindexed_dcache userlocal vint perf_cntr_intr_bit cdmm perf
shadow register sets	: 1
kscratch registers	: 0
package			: 0
core			: 1
VPE			: 1
VCED exceptions		: not available
VCEI exceptions		: not available

Add the rust toolchain

rustup target add mipsel-unknown-linux-musl

Download binarys for ld, gcc, ar

Download the archive of the binarys to a folder and extract it from https://musl.cc/ or from here (backup build 23-Nov-2021).

  • sha256: a61c3bbf9fbb0be80fe2abdb4ea8b6f5afdf664b5b4104a3784a326270905216

Configure cargo to use the mips linker

Add the following entry to your ~/.cargo/config.toml Change the paths so that you end up in in the extracted files.

Example 1. ~/.cargo/config.toml
[target.mipsel-unknown-linux-musl]
linker = "project/mipsel-linux-muslsf-cross/bin/mipsel-linux-muslsf-gcc"
ar = "project/mipsel-linux-muslsf-cross/bin/mipsel-linux-muslsf-ar"
rustflags = ["-Ctarget-feature=+crt-static"] # static linked 1
1 Add this line if you want your binary to static, if you omit it the binary will be smaller but to run your programm you will additionaly need:
  • /lib/ld-musl-mipsel-sf.so.1 (0x77dac000)

  • libgcc_s.so.1 ⇒ /lib/libgcc_s.so.1 (0x77d0d000)

  • libc.so ⇒ /lib/ld-musl-mipsel-sf.so.1 (0x77dac000)

Build with cargo

cargo build --release --target mipsel-unknown-linux-musl

The binary will be in the target/mipsel-unknown-linux-musl/{debug, release} folder.

Shrinking binary size

You can add the follwing optimzations to your Cargo.toml file to minimize the size of release builds:

Example 2. Cargo.toml
[profile.release]
opt-level = 'z' # Optimize for size
lto = true
codegen-units = 1

You can also run strip to shrink the binary even more, remember to use the mipsel-linux-muslsf-strip version you downloaded earlier with the tar.gz.


That's all ;)
Back to the top