rust-lang / rust

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

riscv32imac-unknown-xous-elf: `rust-lld` gives `offset is outside the section` errors #103949

Open xobs opened 1 year ago

xobs commented 1 year ago

Version it worked on

It most recently worked on: Rust 1.64.0

Version with regression

rustc --version --verbose:

rustc 1.65.0 (897e37553 2022-11-02)
binary: rustc
commit-hash: 897e37553bba8b42751c67658967889d11ecd120
commit-date: 2022-11-02
host: x86_64-pc-windows-msvc
release: 1.65.0
LLVM version: 15.0.0

Description

Building crates for the riscv32imac-unknown-xous-elf target sometimes results in a link error. This began with Rust 1.65.0. For example:

error: linking with `rust-lld` failed: exit code: 1
  |
  = note: "rust-lld" "-flavor" "gnu" "C:\\Users\\Sean\\AppData\\Local\\Temp\\rustcR77RGd\\symbols.o" "E:\\Code\\Xous\\Core\\target\\riscv32imac-unknown-xous-elf\\release\\deps\\pddb-8aba67b084ba8fc1.pddb-b3f235aec8c95441.n927u1iqdxcieg4.rcgu.o.rcgu.o" "--as-needed" "-L" "E:\\Code\\Xous\\Core\\target\\riscv32imac-unknown-xous-elf\\release\\deps" "-L" "E:\\Code\\Xous\\Core\\target\\release\\deps" "-L" "E:\\Code\\Xous\\Core\\target\\riscv32imac-unknown-xous-elf\\release\\build\\xous-08f083a692e5c29b\\out" "-L" "C:\\Users\\Sean\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\riscv32imac-unknown-xous-elf\\lib" "-Bstatic" "C:\\Users\\Sean\\AppData\\Local\\Temp\\rustcR77RGd\\libxous-26655dcc7bb206b6.rlib" "C:\\Users\\Sean\\AppData\\Local\\Temp\\rustcR77RGd\\libxous-d175a8531300a7f7.rlib" "C:\\Users\\Sean\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\riscv32imac-unknown-xous-elf\\lib\\libcompiler_builtins-67716ddc427f4546.rlib" "-Bdynamic" "--eh-frame-hdr" "-znoexecstack" "-L" "C:\\Users\\Sean\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\riscv32imac-unknown-xous-elf\\lib" "-o" "E:\\Code\\Xous\\Core\\target\\riscv32imac-unknown-xous-elf\\release\\deps\\pddb-8aba67b084ba8fc1" "--gc-sections" "-O1"
  = note: rust-lld: error: E:\Code\Xous\Core\target\riscv32imac-unknown-xous-elf\release\deps\pddb-8aba67b084ba8fc1.pddb-b3f235aec8c95441.n927u1iqdxcieg4.rcgu.o.rcgu.o:(.rodata.cst32): offset is outside the section

error: could not compile `pddb` due to previous error

Background

We build a custom libstd for the riscv32imac-unknown-xous-elf target, which is a Tier-3 target. This repository is at https://github.com/betrusted-io/rust. We are working on preparing patches for upstream that do not add any external dependencies, but this work is taking time. As a result, we generally forward-port our patchset every six weeks once the stable build is released.

When building packages for the Betrusted platform, our build system compiles around 27 packages. Many of these packages succeed, but with Rust 1.65.0 we have three packages fail.

This target does not have a linker script, and instead relies on the default one provided by rust-lld.

xobs commented 1 year ago

I managed to link the project using riscv32-esp-elf-gcc and it worked just fine, so this seems like a regression with rust-lld.

apiraino commented 1 year ago

WG-prioritization assigning priority (Zulip discussion)

@xobs can you provide an example of a crate failing to build? Could be interesting to bisect where this started. We usually use https://github.com/rust-lang/cargo-bisect-rustc for bisecting (if anyone has some time to help).

@rustbot label -I-prioritize +P-low e-needs-mcve e-needs-bisection

xobs commented 1 year ago

This issue is what caused me to submit https://github.com/rust-lang/rust/pull/104101 to get our libstd upstream. Until now it's been out-of-tree.

I did put up an example of the crash at https://github.com/xobs/gam-crash. This crate is buildable provided you have support for riscv32imac-unknown-xous-elf. This patchset is available in https://github.com/rust-lang/rust/pull/104101

Note that this crate can be linked with ld.

I've verified that gam-crash fails on both Windows and Linux. Furthermore, removing debug=true OR codegen-units=1 from Cargo.toml makes it no longer fail.

I'm following these steps:

  1. Building the libstd in https://github.com/rust-lang/rust/pull/104101
  2. Running cargo +nightly build --release --target riscv32imac-unknown-xous-elf

Given that the difference between 1.65.0 and 1.64.0 wasn't too great -- the only changes were to the arguments to read_buf() and the addition of _sigpipe to init(), it may not be too difficult to to the bisect even with our wonky build system. I'll need to learn how to use cargo-bisect-rustc though.

xobs commented 1 year ago

Using nothing but the choices of chewing gums, the finest of balling wire, and the crudest of scripts https://gist.github.com/xobs/2ae7231284a58df6600b2c0d758ed5f0 I narrowed it down to e2b52ff73edc8b0b7c74bc28760d618187731fe8 as the source of the crash.

Which makes sense, because the crash is in rust-lld and goes away when I replace it with ld. I'm sure it'd work just fine if I transplanted rust-lld from 1.64.0 into 1.65.0.

What's the next step?

apiraino commented 1 year ago

thanks @xobs for the help! I'll mention #99464 cc: @nikic for more context