4

When I try to cross-compile a Cargo starter main.rs file for little-endian MIPS (MT7688 processor running OpenWRT) on OSX using rustc 1.32.0-nightly, I get the following error:

cargo build --target=mipsel-unknown-linux-musl
   Compiling led v0.1.0 (/Users/petrus/Projects/led)
error: linking with `cc` failed: exit code: 1
  |
  = note: "cc" "-Wl,--as-needed" "-Wl,-z,noexecstack" "-Wl,--eh-frame-hdr" "-L" "/Users/petrus/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/mipsel-unknown-linux-musl/lib" "/Users/petrus/Projects/led/target/mipsel-unknown-linux-musl/debug/deps/led-fb2a88ec64854daf.2kthj4han7xcr84g.rcgu.o" "/Users/petrus/Projects/led/target/mipsel-unknown-linux-musl/debug/deps/led-fb2a88ec64854daf.440j3fk0cam0b0pu.rcgu.o" "/Users/petrus/Projects/led/target/mipsel-unknown-linux-musl/debug/deps/led-fb2a88ec64854daf.482rers61msj2848.rcgu.o" "/Users/petrus/Projects/led/target/mipsel-unknown-linux-musl/debug/deps/led-fb2a88ec64854daf.498udk2wtgjsw4ib.rcgu.o" "/Users/petrus/Projects/led/target/mipsel-unknown-linux-musl/debug/deps/led-fb2a88ec64854daf.4uh6jdaux45o6bh1.rcgu.o" "/Users/petrus/Projects/led/target/mipsel-unknown-linux-musl/debug/deps/led-fb2a88ec64854daf.5fq5qqfqkwjepod2.rcgu.o" "-o" "/Users/petrus/Projects/led/target/mipsel-unknown-linux-musl/debug/deps/led-fb2a88ec64854daf" "/Users/petrus/Projects/led/target/mipsel-unknown-linux-musl/debug/deps/led-fb2a88ec64854daf.49n6u1xnbbs66t12.rcgu.o" "-Wl,--gc-sections" "-pie" "-Wl,-zrelro" "-Wl,-znow" "-nodefaultlibs" "-L" "/Users/petrus/Projects/led/target/mipsel-unknown-linux-musl/debug/deps" "-L" "/Users/petrus/Projects/led/target/debug/deps" "-L" "/Users/petrus/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/mipsel-unknown-linux-musl/lib" "-Wl,--start-group" "-Wl,-Bstatic" "/Users/petrus/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/mipsel-unknown-linux-musl/lib/libstd-27a0dffe96f2ad3e.rlib" "/Users/petrus/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/mipsel-unknown-linux-musl/lib/libpanic_unwind-fe8b80af161a561e.rlib" "/var/folders/rl/b7dvjss14wj6415t0v4f7z3m0000gn/T/rustcFd840X/libunwind-ecc019d8c6cb3f8a.rlib" "/var/folders/rl/b7dvjss14wj6415t0v4f7z3m0000gn/T/rustcFd840X/liblibc-5d68beb2e4661bda.rlib" "/Users/petrus/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/mipsel-unknown-linux-musl/lib/liballoc-de565f8c0b8e0971.rlib" "/Users/petrus/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/mipsel-unknown-linux-musl/lib/librustc_std_workspace_core-0a55bee6cc97e346.rlib" "/Users/petrus/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/mipsel-unknown-linux-musl/lib/libcore-4075eb2b0854a330.rlib" "-Wl,--end-group" "/Users/petrus/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/mipsel-unknown-linux-musl/lib/libcompiler_builtins-ecf284221505c8a3.rlib" "-Wl,-Bdynamic" "-lgcc_s" "-lc"
  = note: clang: warning: argument unused during compilation: '-pie' [-Wunused-command-line-argument]
          ld: unknown option: --as-needed
          clang: error: linker command failed with exit code 1 (use -v to see invocation)

Running with RUSTFLAGS="-C linker=lld" throws:

RUSTFLAGS="-C linker=lld" cargo build --target=mipsel-unknown-linux-musl
   Compiling led v0.1.0 (/Users/petrus/Projects/led)
error: linker `lld` not found
  |
  = note: No such file or directory (os error 2)

error: aborting due to previous error

error: Could not compile `led`.

To learn more, run the command again with --verbose.

Also tried with RUSTFLAGS="-C linker=rust-lld", which throws:

rust-lld: error: unable to find library -lgcc_s
rust-lld: error: unable to find library -lc

As I understand, I need to specify a linker toolchain, which I don't know how to procure. I also tried compiling with xargo and cross and dockcross/linux-mipsel, with no luck.

I would build it on the device itself, but rustup won't install Rust on the MT7688. How do I get and specify the correct MIPS (little-endian) linker toolchain?

Update: I found a pre-compiled MIPS GCC for macOS, which throws `mipsel-elf/bin/ld: unrecognized option '-Wl,--as-needed' or "error adding symbols: Bad value" when I try to specify mipsel-elf-gcc it as the linker:

rustc --target=mipsel-unknown-linux-musl -C linker=/Users/petrus/Projects/wallfly/respeaker/bin/gcc-mips/bin/mipsel-elf-gcc ./src/main.rs
error: linking with `/Users/petrus/Projects/bin/gcc-mips/bin/mipsel-elf-gcc` failed: exit code: 1
  |
  = note: "/Users/petrus/Projects/bin/gcc-mips/bin/mipsel-elf-gcc" "-Wl,--as-needed" "-Wl,-z,noexecstack" "-Wl,--eh-frame-hdr" "-L" "/Users/petrus/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/mipsel-unknown-linux-musl/lib" "main.main.7rcbfp3g-cgu.0.rcgu.o" "main.main.7rcbfp3g-cgu.1.rcgu.o" "main.main.7rcbfp3g-cgu.2.rcgu.o" "main.main.7rcbfp3g-cgu.3.rcgu.o" "main.main.7rcbfp3g-cgu.4.rcgu.o" "main.main.7rcbfp3g-cgu.5.rcgu.o" "-o" "main" "main.4s37gsrti678ik8u.rcgu.o" "-Wl,--gc-sections" "-pie" "-Wl,-zrelro" "-Wl,-znow" "-nodefaultlibs" "-L" "/Users/petrus/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/mipsel-unknown-linux-musl/lib" "-Wl,--start-group" "-Wl,-Bstatic" "/Users/petrus/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/mipsel-unknown-linux-musl/lib/libstd-27a0dffe96f2ad3e.rlib" "/Users/petrus/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/mipsel-unknown-linux-musl/lib/libpanic_unwind-fe8b80af161a561e.rlib" "/var/folders/rl/b7dvjss14wj6415t0v4f7z3m0000gn/T/rustcLDTUyV/libunwind-ecc019d8c6cb3f8a.rlib" "/var/folders/rl/b7dvjss14wj6415t0v4f7z3m0000gn/T/rustcLDTUyV/liblibc-5d68beb2e4661bda.rlib" "/Users/petrus/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/mipsel-unknown-linux-musl/lib/liballoc-de565f8c0b8e0971.rlib" "/Users/petrus/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/mipsel-unknown-linux-musl/lib/librustc_std_workspace_core-0a55bee6cc97e346.rlib" "/Users/petrus/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/mipsel-unknown-linux-musl/lib/libcore-4075eb2b0854a330.rlib" "-Wl,--end-group" "/Users/petrus/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/mipsel-unknown-linux-musl/lib/libcompiler_builtins-ecf284221505c8a3.rlib" "-Wl,-Bdynamic" "-lgcc_s" "-lc"
  = note: /Users/petrus/Projects/bin/gcc-mips/bin/../lib/gcc/mipsel-elf/7.1.0/../../../../mipsel-elf/bin/ld: /Users/petrus/Projects/bin/gcc-mips/bin/../lib/gcc/mipsel-elf/7.1.0/crtbegin.o: relocation R_MIPS_HI16 against `a local symbol' can not be used when making a shared object; recompile with -fPIC
          /Users/petrus/Projects/bin/gcc-mips/bin/../lib/gcc/mipsel-elf/7.1.0/crtbegin.o: error adding symbols: Bad value
          collect2: error: ld returned 1 exit status

Maybe I'm specifying it wrong?

hellow
  • 9,446
  • 5
  • 41
  • 61
Petrus Theron
  • 25,051
  • 30
  • 137
  • 263
  • 1
    Which rust version do you use? Afaik `lld` is the default linker for rust, but your error message states that it uses `ld`. Can you try to change that by creating a `.cargo/config` with `rustflags = [ "-C", "linker=lld" ]` – hellow Feb 11 '19 at 09:48
  • @hellow updated question with version (1.32.0 nightly) and linker attempts. I found a pre-compiled macOS GCC for MIPS - not sure how to specify it though: https://github.com/a3f/cross-gcc-hosted-on-macOS/releases – Petrus Theron Feb 11 '19 at 10:12
  • Same as with lld, just with the correct name (and an absolute path propably). You could install lld though or use the precompiled binaries via `rustup component add llvm-tools-preview`) – hellow Feb 11 '19 at 10:14
  • It sounds like [this question](https://stackoverflow.com/q/34403155/1233251) will help. – SE_net4 the downvoter Feb 11 '19 at 10:18
  • To be honest, this is not a good question for SO. Maybe you should better ask this at https://users.rust-lang.org – hellow Feb 11 '19 at 10:44
  • 2
    @hellow why is this MIPS cross-compilation problem, shared by many embedded engineers, not a good question for SO? – Petrus Theron Feb 11 '19 at 11:05
  • Because it's your setup that doesn't work. It isn't a specific problem with rust or the code you are using (which you didn't show, but it doesn't matter this time). You are lacking the correct binaries. SuperUser would be also a good platform to ask. Why I choose the forum is, because one have to ask for certain tools and your environment you are developing on. – hellow Feb 11 '19 at 11:08

0 Answers0