rust-lang / rust

Empowering everyone to build reliable and efficient software.
https://www.rust-lang.org
Other
98.14k stars 12.69k forks source link

`rust-lld` won't link my RISC-V assembly code with `.p2align` alignment settings #73060

Closed luojia65 closed 3 years ago

luojia65 commented 4 years ago

I tried to build my HAL crate here after I got latest 1.45.0-nightly Rust toolchain. I expected to see it compiles as normal; but there is an error during link process:

Error message

I used `cargo build --example vf103c-start-esig --verbose`: ``` gd32vf103-hal on  master [!?] via 🦀 v1.44.0 took 22s ❯ cargo build --example vf103c-start-esig --verbose Fresh semver-parser v0.7.0 Fresh unicode-xid v0.1.0 Fresh bit_field v0.9.0 Fresh rand_core v0.4.2 Fresh nb v0.1.2 Fresh vcell v0.1.2 Fresh void v1.0.2 Fresh r0 v0.2.2 Fresh panic-halt v0.2.0 Fresh semver v0.9.0 Fresh rand_core v0.3.1 Fresh embedded-hal v0.2.3 Fresh rustc_version v0.2.3 Fresh rand v0.5.6 Fresh proc-macro2 v0.4.30 Fresh quote v0.6.13 Fresh syn v0.15.44 Fresh bare-metal v0.2.5 Fresh riscv-rt-macros v0.1.6 Fresh riscv v0.5.4 Fresh gd32vf103-pac v0.2.0 Fresh riscv-rt v0.6.1 Fresh gd32vf103-hal v0.0.4 (/Users/mac/2020/RustProjects/gd32vf103-hal/gd32vf103-hal) warning: unused import: `bkp` --> gd32vf103-hal/src/backup.rs:3:18 | 3 | use crate::pac::{bkp, BKP, PMU}; | ^^^ | = note: `#[warn(unused_imports)]` on by default warning: associated function is never used: `cfg1` --> gd32vf103-hal/src/rcu.rs:127:19 | 127 | pub(crate) fn cfg1(&mut self) -> &rcu::CFG1 { | ^^^^ | = note: `#[warn(dead_code)]` on by default warning: associated function is never used: `bdctl` --> gd32vf103-hal/src/rcu.rs:510:19 | 510 | pub(crate) fn bdctl(&mut self) -> &rcu::BDCTL { | ^^^^^ warning: field is never read: `wwdgt` --> gd32vf103-hal/src/wdog.rs:132:5 | 132 | wwdgt: WWDGT, | ^^^^^^^^^^^^ warning: 4 warnings emitted Compiling gd32vf103-hal-example v0.1.0 (/Users/mac/2020/RustProjects/gd32vf103-hal/gd32vf103-hal-example) Running `rustc --crate-name vf103c_start_esig --edition=2018 gd32vf103-hal-example/examples/vf103c-start-esig.rs --error-format=json --json=diagnostic-rendered-ansi --crate-type bin --emit=dep-info,link -Cembed-bitcode=no -C codegen-units=1 -C debuginfo=2 -C metadata=aa7c502d2e7730cf -C extra-filename=-aa7c502d2e7730cf --out-dir /Users/mac/2020/RustProjects/gd32vf103-hal/target/riscv32imac-unknown-none-elf/debug/examples --target riscv32imac-unknown-none-elf -C incremental=/Users/mac/2020/RustProjects/gd32vf103-hal/target/riscv32imac-unknown-none-elf/debug/incremental -L dependency=/Users/mac/2020/RustProjects/gd32vf103-hal/target/riscv32imac-unknown-none-elf/debug/deps -L dependency=/Users/mac/2020/RustProjects/gd32vf103-hal/target/debug/deps --extern gd32vf103_hal=/Users/mac/2020/RustProjects/gd32vf103-hal/target/riscv32imac-unknown-none-elf/debug/deps/libgd32vf103_hal-f42a8d35ac089d48.rlib --extern gd32vf103_hal_example=/Users/mac/2020/RustProjects/gd32vf103-hal/target/riscv32imac-unknown-none-elf/debug/deps/libgd32vf103_hal_example-d5e983c1d7968e18.rlib --extern nb=/Users/mac/2020/RustProjects/gd32vf103-hal/target/riscv32imac-unknown-none-elf/debug/deps/libnb-7fd70db5ee5c67a1.rlib --extern panic_halt=/Users/mac/2020/RustProjects/gd32vf103-hal/target/riscv32imac-unknown-none-elf/debug/deps/libpanic_halt-98b282aedf2a8030.rlib --extern riscv_rt=/Users/mac/2020/RustProjects/gd32vf103-hal/target/riscv32imac-unknown-none-elf/debug/deps/libriscv_rt-44ff22cf93571932.rlib -C link-arg=-Tmemory.x -C link-arg=-Tlink.x -L /Users/mac/2020/RustProjects/gd32vf103-hal/target/riscv32imac-unknown-none-elf/debug/build/gd32vf103-hal-example-f0c6c3176eafd22c/out -L /Users/mac/2020/RustProjects/gd32vf103-hal/target/riscv32imac-unknown-none-elf/debug/build/gd32vf103-hal-6e8ed43ae5d3666e/out -L /Users/mac/2020/RustProjects/gd32vf103-hal/target/riscv32imac-unknown-none-elf/debug/build/riscv-79c72d77cf93788c/out -L /Users/mac/2020/RustProjects/gd32vf103-hal/target/riscv32imac-unknown-none-elf/debug/build/riscv-rt-0600a4a977c9220b/out -L /Users/mac/2020/RustProjects/gd32vf103-hal/target/riscv32imac-unknown-none-elf/debug/build/riscv-rt-0600a4a977c9220b/out` error: linking with `rust-lld` failed: exit code: 1 | = note: "rust-lld" "-flavor" "gnu" "-L" "/Users/mac/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/riscv32imac-unknown-none-elf/lib" "/Users/mac/2020/RustProjects/gd32vf103-hal/target/riscv32imac-unknown-none-elf/debug/examples/vf103c_start_esig-aa7c502d2e7730cf.10phd9fldl0atuwd.rcgu.o" "-o" "/Users/mac/2020/RustProjects/gd32vf103-hal/target/riscv32imac-unknown-none-elf/debug/examples/vf103c_start_esig-aa7c502d2e7730cf" "--gc-sections" "-L" "/Users/mac/2020/RustProjects/gd32vf103-hal/target/riscv32imac-unknown-none-elf/debug/deps" "-L" "/Users/mac/2020/RustProjects/gd32vf103-hal/target/debug/deps" "-L" "/Users/mac/2020/RustProjects/gd32vf103-hal/target/riscv32imac-unknown-none-elf/debug/build/gd32vf103-hal-example-f0c6c3176eafd22c/out" "-L" "/Users/mac/2020/RustProjects/gd32vf103-hal/target/riscv32imac-unknown-none-elf/debug/build/gd32vf103-hal-6e8ed43ae5d3666e/out" "-L" "/Users/mac/2020/RustProjects/gd32vf103-hal/target/riscv32imac-unknown-none-elf/debug/build/riscv-79c72d77cf93788c/out" "-L" "/Users/mac/2020/RustProjects/gd32vf103-hal/target/riscv32imac-unknown-none-elf/debug/build/riscv-rt-0600a4a977c9220b/out" "-L" "/Users/mac/2020/RustProjects/gd32vf103-hal/target/riscv32imac-unknown-none-elf/debug/build/riscv-rt-0600a4a977c9220b/out" "-L" "/Users/mac/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/riscv32imac-unknown-none-elf/lib" "-Bstatic" "/Users/mac/2020/RustProjects/gd32vf103-hal/target/riscv32imac-unknown-none-elf/debug/deps/libriscv_rt-44ff22cf93571932.rlib" "/Users/mac/2020/RustProjects/gd32vf103-hal/target/riscv32imac-unknown-none-elf/debug/deps/libr0-05fbe84302510151.rlib" "--start-group" "/Users/mac/2020/RustProjects/gd32vf103-hal/target/riscv32imac-unknown-none-elf/debug/deps/libpanic_halt-98b282aedf2a8030.rlib" "/Users/mac/2020/RustProjects/gd32vf103-hal/target/riscv32imac-unknown-none-elf/debug/deps/libgd32vf103_hal-f42a8d35ac089d48.rlib" "/Users/mac/2020/RustProjects/gd32vf103-hal/target/riscv32imac-unknown-none-elf/debug/deps/libembedded_hal-6e313f092d75e7e9.rlib" "/Users/mac/2020/RustProjects/gd32vf103-hal/target/riscv32imac-unknown-none-elf/debug/deps/libvoid-0a33e079cdaac7b0.rlib" "/Users/mac/2020/RustProjects/gd32vf103-hal/target/riscv32imac-unknown-none-elf/debug/deps/libnb-7fd70db5ee5c67a1.rlib" "/Users/mac/2020/RustProjects/gd32vf103-hal/target/riscv32imac-unknown-none-elf/debug/deps/libgd32vf103_pac-484c8058615e380a.rlib" "/Users/mac/2020/RustProjects/gd32vf103-hal/target/riscv32imac-unknown-none-elf/debug/deps/libvcell-2496022d491ebea2.rlib" "/Users/mac/2020/RustProjects/gd32vf103-hal/target/riscv32imac-unknown-none-elf/debug/deps/libriscv-b586d926dc5ad500.rlib" "/Users/mac/2020/RustProjects/gd32vf103-hal/target/riscv32imac-unknown-none-elf/debug/deps/libbit_field-f20bf3d5a8b3be7a.rlib" "/Users/mac/2020/RustProjects/gd32vf103-hal/target/riscv32imac-unknown-none-elf/debug/deps/libbare_metal-2d5a6fd0be96659a.rlib" "/Users/mac/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/riscv32imac-unknown-none-elf/lib/librustc_std_workspace_core-32f2eb928693d665.rlib" "/Users/mac/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/riscv32imac-unknown-none-elf/lib/libcore-66e3c4e6069e3299.rlib" "--end-group" "/Users/mac/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/riscv32imac-unknown-none-elf/lib/libcompiler_builtins-b074779c04fb5646.rlib" "-Tmemory.x" "-Tlink.x" "-Bdynamic" = note: rust-lld: error: asm.S:179:(.trap+0x0): relocation R_RISCV_ALIGN requires unimplemented linker relaxation; recompile with -mno-relax rust-lld: error: asm.S:179:(.trap+0xB9): relocation R_RISCV_ALIGN requires unimplemented linker relaxation; recompile with -mno-relax error: aborting due to previous error error: could not compile `gd32vf103-hal-example`. Caused by: process didn't exit successfully: `rustc --crate-name vf103c_start_esig --edition=2018 gd32vf103-hal-example/examples/vf103c-start-esig.rs --error-format=json --json=diagnostic-rendered-ansi --crate-type bin --emit=dep-info,link -Cembed-bitcode=no -C codegen-units=1 -C debuginfo=2 -C metadata=aa7c502d2e7730cf -C extra-filename=-aa7c502d2e7730cf --out-dir /Users/mac/2020/RustProjects/gd32vf103-hal/target/riscv32imac-unknown-none-elf/debug/examples --target riscv32imac-unknown-none-elf -C incremental=/Users/mac/2020/RustProjects/gd32vf103-hal/target/riscv32imac-unknown-none-elf/debug/incremental -L dependency=/Users/mac/2020/RustProjects/gd32vf103-hal/target/riscv32imac-unknown-none-elf/debug/deps -L dependency=/Users/mac/2020/RustProjects/gd32vf103-hal/target/debug/deps --extern gd32vf103_hal=/Users/mac/2020/RustProjects/gd32vf103-hal/target/riscv32imac-unknown-none-elf/debug/deps/libgd32vf103_hal-f42a8d35ac089d48.rlib --extern gd32vf103_hal_example=/Users/mac/2020/RustProjects/gd32vf103-hal/target/riscv32imac-unknown-none-elf/debug/deps/libgd32vf103_hal_example-d5e983c1d7968e18.rlib --extern nb=/Users/mac/2020/RustProjects/gd32vf103-hal/target/riscv32imac-unknown-none-elf/debug/deps/libnb-7fd70db5ee5c67a1.rlib --extern panic_halt=/Users/mac/2020/RustProjects/gd32vf103-hal/target/riscv32imac-unknown-none-elf/debug/deps/libpanic_halt-98b282aedf2a8030.rlib --extern riscv_rt=/Users/mac/2020/RustProjects/gd32vf103-hal/target/riscv32imac-unknown-none-elf/debug/deps/libriscv_rt-44ff22cf93571932.rlib -C link-arg=-Tmemory.x -C link-arg=-Tlink.x -L /Users/mac/2020/RustProjects/gd32vf103-hal/target/riscv32imac-unknown-none-elf/debug/build/gd32vf103-hal-example-f0c6c3176eafd22c/out -L /Users/mac/2020/RustProjects/gd32vf103-hal/target/riscv32imac-unknown-none-elf/debug/build/gd32vf103-hal-6e8ed43ae5d3666e/out -L /Users/mac/2020/RustProjects/gd32vf103-hal/target/riscv32imac-unknown-none-elf/debug/build/riscv-79c72d77cf93788c/out -L /Users/mac/2020/RustProjects/gd32vf103-hal/target/riscv32imac-unknown-none-elf/debug/build/riscv-rt-0600a4a977c9220b/out -L /Users/mac/2020/RustProjects/gd32vf103-hal/target/riscv32imac-unknown-none-elf/debug/build/riscv-rt-0600a4a977c9220b/out` (exit code: 1) ```

However when I switched back to 1.44.0 stable Rust toolchain, it compiles successfully without any error.

rustc --version --verbose:

My latest Rust version (won't compile my code):

rustc 1.45.0-nightly (56daaf669 2020-06-03)
binary: rustc
commit-hash: 56daaf669ebc3d5083db5cded719f780dc31104e
commit-date: 2020-06-03
host: x86_64-apple-darwin
release: 1.45.0-nightly
LLVM version: 10.0

Stable Rust version (that compiles my code):

rustc 1.44.0 (49cae5576 2020-06-01)
binary: rustc
commit-hash: 49cae55760da0a43428eba73abcb659bb70cf2e4
commit-date: 2020-06-01
host: x86_64-apple-darwin
release: 1.44.0
LLVM version: 9.0

Suspected assembly code (guessed from R_RISCV_ALIGN in error message):

    lw a6, 15*REGBYTES(sp)
    lw a7, 16*REGBYTES(sp)
.endm

.section .trap, "ax"
.p2align 6 ; 4-byte aligned      # <-- Maybe this line caused error
.globl _gd32vf103_trap_entry
.weak _gd32vf103_trap_entry
_gd32vf103_trap_entry:
; .globl _start_trap
spastorino commented 4 years ago

Assigning P-medium as discussed as part of the Prioritization Working Group process and removing I-prioritize.

nagisa commented 4 years ago

It is more likely that the failure before was just silent and the linked code didn’t contain the necessary relocations, potentially resulting in unsound output. The new error is most likely a result of LLVM upgrade.

nagisa commented 4 years ago

https://reviews.llvm.org/D79105

jrtc27 commented 4 years ago

Early LLD versions for RISC-V just silently ignored R_RISCV_ALIGN relocations. I fixed it to give an appropriate error message, as they are not supported, and need special handling to not break things in subtle ways. As the error message says, "recompile with -mno-relax" (which means dropping +relax or adding -relax to the attributes, much like +m for enabling the M extension, if dealing with various low-level LLVM bits yourself and bypassing the Clang driver).

jrtc27 commented 4 years ago

Ok, so the problem is your assemble.sh. Replace:

$compiler $src_file -o $o_file -march=rv32imac -mabi=ilp32 -c -g

with

$compiler $src_file -o $o_file -march=rv32imac -mabi=ilp32 -mno-relax -Wa,-mno-relax -c -g

(and similarly in assemble.ps1). So, not a rust bug; learn to read error messages.

luojia65 commented 4 years ago

Thank you all! I'll try it out.