Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
parasyte opened this issue Jul 25, 2018 · 5 comments

Comments

@parasyte
Copy link

$ 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 <jay@kodewerx.org>"]

[profile.dev]
panic = "abort"

[profile.release]
panic = "abort"

[dependencies]
@parasyte
Copy link
Author

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"
}

@sfackler
Copy link
Member

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

@parasyte
Copy link
Author

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.

@alexcrichton
Copy link
Member

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

@theronic
Copy link

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 :/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants