Rust: "ld: unknown option: --as-needed" when trying to cross-compile on macOS High Sierra

Created on 25 Jul 2018  路  5Comments  路  Source: rust-lang/rust

$ rustc --version
rustc 1.29.0-nightly (6a1c0637c 2018-07-23)
$ cargo --version
cargo 1.29.0-nightly (506eea76e 2018-07-17)
$ system_profiler SPSoftwareDataType | grep Version
      System Version: macOS 10.13.6 (17G65)
      Kernel Version: Darwin 17.7.0

Trying to cross-compile an empty project produces the following error:

$ cargo xbuild --target arm-unknown-linux-gnueabi
   Compiling core v0.0.0 (file:///Users/parasyte/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/src/rust/src/libcore)
    Finished release [optimized] target(s) in 14.30s
   Compiling compiler_builtins v0.1.0 (file:///Users/parasyte/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/src/rust/src/libcompiler_builtins)
    Finished release [optimized] target(s) in 2.35s
   Compiling alloc v0.0.0 (file:///var/folders/f0/yjjsr3hj2418r3lpmkk9gnt40000gn/T/xargo.udCKkKuWiteB)
    Finished release [optimized] target(s) in 3.63s
   Compiling hello v0.1.0 (file:///Users/parasyte/projects/hello)
error: linking with `cc` failed: exit code: 1
  |
  = note: "cc" "-Wl,--as-needed" "-Wl,-z,noexecstack" "-L" "/Users/parasyte/projects/hello/target/sysroot/lib/rustlib/arm-unknown-linux-gnueabi/lib" "/Users/parasyte/projects/hello/target/arm-unknown-linux-gnueabi/debug/deps/hello-340ef4d54d297181.3t4xfi2w3lflthfy.rcgu.o" "-o" "/Users/parasyte/projects/hello/target/arm-unknown-linux-gnueabi/debug/deps/hello-340ef4d54d297181" "-Wl,--gc-sections" "-pie" "-Wl,-z,relro,-z,now" "-nodefaultlibs" "-L" "/Users/parasyte/projects/hello/target/arm-unknown-linux-gnueabi/debug/deps" "-L" "/Users/parasyte/projects/hello/target/debug/deps" "-L" "/Users/parasyte/projects/hello/target/sysroot/lib/rustlib/arm-unknown-linux-gnueabi/lib" "-Wl,-Bstatic" "/Users/parasyte/projects/hello/target/sysroot/lib/rustlib/arm-unknown-linux-gnueabi/lib/libcore-74a284b34eb3c484.rlib" "/Users/parasyte/projects/hello/target/sysroot/lib/rustlib/arm-unknown-linux-gnueabi/lib/libcompiler_builtins-15e6f22f9525c765.rlib" "-Wl,-Bdynamic"
  = 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)

Same failure with the mips-unknown-linux-gnu target triple, as well.

Here's the project source:

// src/main.rs

#![feature(panic_implementation)]
#![no_std]
#![no_main]

use core::panic::PanicInfo;

#[no_mangle]
pub extern "C" fn _start() -> ! {
    loop {}
}

/// This function is called on panic.
#[panic_implementation]
#[no_mangle]
pub fn panic(_info: &PanicInfo) -> ! {
    loop {}
}
# Cargo.toml

[package]
name = "hello"
version = "0.1.0"
authors = ["Jay Oster <[email protected]>"]

[profile.dev]
panic = "abort"

[profile.release]
panic = "abort"

[dependencies]

Most helpful comment

That's not really helpful. 馃槙 I can cross-compile just fine by taking an extra step to change the linker to lld in any of these target specs.

All 5 comments

FWIW, it looks like the --as-needed flag is specified in the target triple JSON spec:

$ rustc -Z unstable-options --target arm-unknown-linux-gnueabi --print target-spec-json
{
  "abi-blacklist": [
    "stdcall",
    "fastcall",
    "vectorcall",
    "thiscall",
    "win64",
    "sysv64"
  ],
  "arch": "arm",
  "data-layout": "e-m:e-p:32:32-i64:64-v128:64:128-a:0:32-n32-S64",
  "dynamic-linking": true,
  "env": "gnu",
  "exe-allocation-crate": "alloc_jemalloc",
  "executables": true,
  "features": "+strict-align,+v6",
  "has-elf-tls": true,
  "has-rpath": true,
  "is-builtin": true,
  "linker-flavor": "gcc",
  "linker-is-gnu": true,
  "llvm-target": "arm-unknown-linux-gnueabi",
  "max-atomic-width": 64,
  "os": "linux",
  "position-independent-executables": true,
  "pre-link-args": {
    "gcc": [
      "-Wl,--as-needed",
      "-Wl,-z,noexecstack"
    ]
  },
  "relro-level": "full",
  "target-c-int-width": "32",
  "target-endian": "little",
  "target-family": "unix",
  "target-pointer-width": "32",
  "vendor": "unknown"
}

But not in the macOS triple:

$ rustc -Z unstable-options --target x86_64-apple-darwin --print target-spec-json
{
  "abi-return-struct-as-int": true,
  "arch": "x86_64",
  "archive-format": "bsd",
  "cpu": "core2",
  "data-layout": "e-m:o-i64:64-f80:128-n8:16:32:64-S128",
  "dll-suffix": ".dylib",
  "dynamic-linking": true,
  "eliminate-frame-pointer": false,
  "emit-debug-gdb-scripts": false,
  "env": "",
  "exe-allocation-crate": "alloc_jemalloc",
  "executables": true,
  "function-sections": false,
  "has-elf-tls": true,
  "has-rpath": true,
  "is-builtin": true,
  "is-like-osx": true,
  "linker-flavor": "gcc",
  "llvm-target": "x86_64-apple-darwin",
  "max-atomic-width": 128,
  "os": "macos",
  "pre-link-args": {
    "gcc": [
      "-m64"
    ]
  },
  "stack-probes": true,
  "target-c-int-width": "32",
  "target-endian": "little",
  "target-family": "unix",
  "target-pointer-width": "64",
  "vendor": "apple"
}

You'll need a cross compilation toolchain to cross compile.

That's not really helpful. 馃槙 I can cross-compile just fine by taking an extra step to change the linker to lld in any of these target specs.

Ah yes as @sfackler mentioned here the issue is the wrong toolchain is being used, but using an appropriate toolchain should solve the issue!

I am also struggling to cross-compile any Rust code for MIPS.
How do I find and use an appropriate toolchain from OSX?

I have an open question on StackOverflow detailing my attempts. Have spent a day on this without luck :/

Was this page helpful?
0 / 5 - 0 ratings