esp-rs / rust

Rust for the xtensa architecture. Built in targets for the ESP32 and ESP8266
https://www.rust-lang.org
Other
743 stars 39 forks source link

error: linking with `xtensa-lx106-elf-gcc` failed: exit status: 1 #93

Closed jessebraham closed 2 years ago

jessebraham commented 3 years ago

Using rustc-1.56.0.1 installed from the rust-build repository. Building for the ESP8266 in dev mode seems to result in the following error:

error: linking with `xtensa-lx106-elf-gcc` failed: exit status: 1
  |
  = note: "xtensa-lx106-elf-gcc" "/Users/jesse/Work/esp8266-hal/target/xtensa-esp8266-none-elf/debug/examples/blinky-e0818fc35520ce14.panic_halt-43f7212d7c513c73.panic_halt.f3fb034b-cgu.0.rcgu.o.rcgu.o" "-Wl,--as-needed" "-L" "/Users/jesse/Work/esp8266-hal/target/xtensa-esp8266-none-elf/debug/deps" "-L" "/Users/jesse/Work/esp8266-hal/target/debug/deps" "-L" "/Users/jesse/Work/esp8266-hal/target/xtensa-esp8266-none-elf/debug/build/esp8266-hal-2d03f26e6b6aa8d6/out" "-L" "/Users/jesse/Work/esp8266-hal/target/xtensa-esp8266-none-elf/debug/build/xtensa-lx-9a48fc8303ca0ffa/out" "-L" "/Users/jesse/Work/esp8266-hal/target/xtensa-esp8266-none-elf/debug/build/xtensa-lx-rt-02f5e2b7b68604e9/out" "-L" "/Users/jesse/.rustup/toolchains/esp/lib/rustlib/xtensa-esp8266-none-elf/lib" "-Wl,--start-group" "-Wl,--end-group" "-Wl,-Bstatic" "/Users/jesse/Work/esp8266-hal/target/xtensa-esp8266-none-elf/debug/deps/libcompiler_builtins-8b73f41dc98a2660.rlib" "-Wl,-Bdynamic" "-Wl,--eh-frame-hdr" "-Wl,-znoexecstack" "-L" "/Users/jesse/.rustup/toolchains/esp/lib/rustlib/xtensa-esp8266-none-elf/lib" "-o" "/Users/jesse/Work/esp8266-hal/target/xtensa-esp8266-none-elf/debug/examples/blinky-e0818fc35520ce14" "-Wl,--gc-sections" "-no-pie" "-nodefaultlibs" "-nostartfiles" "-Wl,-Tlink.x"
  = note: /Users/jesse/.xtensa/xtensa-lx106-elf/bin/../lib/gcc/xtensa-lx106-elf/8.4.0/../../../../xtensa-lx106-elf/bin/ld: /Users/jesse/Work/esp8266-hal/target/xtensa-esp8266-none-elf/debug/deps/libcompiler_builtins-8b73f41dc98a2660.rlib(compiler_builtins-8b73f41dc98a2660.compiler_builtins.77efb3e9-cgu.3.rcgu.o):(.literal._ZN17compiler_builtins3int19specialized_div_rem23u32_normalization_shift17h93377cdcc12e9cdcE+0x8): undefined reference to `core::panicking::panic'
          collect2: error: ld returned 1 exit status

  = help: some `extern` functions couldn't be found; some native libraries may need to be installed or have their path specified
  = note: use the `-l` flag to specify native libraries to link
  = note: use the `cargo:rustc-link-lib` directive to specify the native libraries to link with Cargo (see https://doc.rust-lang.org/cargo/reference/build-scripts.html#cargorustc-link-libkindname)

error: could not compile `esp8266-hal` due to previous error

Curiously, building in release seems to resolve the issue. This can be reproduced by executing the following:

$ git clone https://github.com/esp-rs/esp8266-hal.git
$ cd esp8266-hal/
$ cargo +esp build --example=blinky           # fails!
$ cargo +esp build --example=blinky --release # succeeds!

The Cargo.toml manifest for this repository contains the following configuration for build profiles, not sure if lto is the culprit or if it's something else:

[profile.dev]
lto = true
opt-level = 1

[profile.release]
lto = true
jessebraham commented 3 years ago

I've disabled lto for the dev profile and this seems to have resolved the linker error. Not sure why it won't work for dev but will for release.

MabezDev commented 3 years ago

I think this is related to https://github.com/rust-lang/compiler-builtins/issues/347. Doesn't look like there is a solution yet, just some work arounds & theories.

MabezDev commented 2 years ago

Just following up on this, seems although this hasn't been fixed yet its been tracked down to https://github.com/rust-lang/cargo/issues/10118#issuecomment-1006000210. This is why it works in release, because there are no overflow checks etc.

MabezDev commented 2 years ago

So I think this is almost certainly LTO's fault, but I think because we are using a GCC linker. I guess it is discarding some important symbols... like panic :D. I've only just come to this conclusion because I'm messing around with LTO on the STD port, and running into the same issues.

I am hoping that with LLD support tracked in https://github.com/espressif/llvm-project/issues/11, these issues will go away. Closing for that reason.