1

For research purpose, I'm trying to build an image for my cortex-m4 microcontroller with rwpi relocation model. However, during linking stage, lld throws me the error:

rust-lld: error: SBREL relocation to __sbss without static base

Below is an easy way to reproduce this error. This follows the tutorial in The Embedded Rust Book.

  1. cargo install cargo-generate to install an handy tool.
  2. cargo generate --git https://github.com/rust-embedded/cortex-m-quickstart to download the example code.
  3. cd into the cloned directory. Modify .cargo/config. Add "-C", "relocation-model=rwpi" to rustflags.
  4. Run cargo build.

The example code in main.rs uses the cortex-m-rt crate, which during compile time will generate a link script for the linker. The generated link script can be found at ./target/thumbv7m-none-eabi/debug/build/cortex-m-rt-xxxxxxxxxxxxxxxx/out/link.x. I suppose that some information is missing for the linker. What should I do to resolve the issue?

Zhiyao
  • 4,152
  • 2
  • 12
  • 21
  • Found any solution ? – PinkTurtle Dec 28 '22 at 21:03
  • @PinkTurtle I gave up directly using `cortex-m-rt` and wrote my own bootstrap code by studying this crate. The problem originated from the declaration of the `__sbss` variable in `cortex-m-rt`. I remember they declare it as `extern "C" { static __sbss: &u32; }` or something similar. Declare it as `extern "C" { static __sbss: u32; }` can resolve it. (Remove `&`) Though theoretically we should place `&` there. It works, but sincerely I don't know why. – Zhiyao Dec 28 '22 at 21:16
  • It turns out my problem was "Initialized globals are unfortunately not supported." (writing a Ledger embedded app). Maybe that was your issue? – PinkTurtle Dec 29 '22 at 00:01
  • @PinkTurtle I have no idea on this... Hope you can figure out a solution soon – Zhiyao Dec 29 '22 at 15:29
  • yes I did. Just had to initialize globals through an init function. – PinkTurtle Dec 29 '22 at 19:28

0 Answers0