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

Support lld linker #357

Closed
wants to merge 5 commits into from
Closed

Conversation

MabezDev
Copy link
Member

@MabezDev MabezDev commented Jan 23, 2023

Adds LLD support for all Xtensa chips (Direct boot scripts not yet changed for s3).

To test just comment the GNU parts and uncomment the LLD parts. You'll need a copy of Xtensa enabled lld from here: https://github.com/espressif/llvm-project/releases/tag/esp-15.0.0-20221201. If you try and build it will tell you where you need to copy the executable, and remember to rename it to rust-lld! In the future you won't need to bother with this as we will ship with the toolchain.

To add some context to the changes, I'll break them down into two parts. The first part is straightforward, LLD does not have the same semantics when using absolute addresses inside sections, see esp-rs/xtensa-lx-rt#52 for more details. The second part is where it gets interesting. We have some hand-rolled assembly inside that handles interrupts. Part of that code is responsible for setting the processor state (PS special register) which looks like movi a0, (\level | PS_WOE). PS_WOE is just something we need to enable to allow the window ABI to work correctly, the level is the current interrupt level, e.g 1. The resultant value of this is 0x40001. The movi instruction can only hold values up to 2048 (see below), so the assembler does something clever. It "relaxes" the movi instruction to a l32r instruction. The l32r allows reading a 32-bit value from memory (known as a literal), with a negative offset from the current program counter value; because the offset must be negative, this means the literal we want to read MUST be placed before the code that wishes to read it! Hence the literals section placement now comes before the text section definitions and allows the program to run correctly. So why did the original scripts work with the GNU toolchain? It worked because there is a flag that is enabled by default called text-section-literals which merges the literal and text sections and ensures the relevant literals are placed before the reference to them in the code.

image
image

To Do

  • Update xtensa-lx-rt dependency once a new version has been published
  • Update the direct-boot linker scripts for the ESP32-S3

@bjoernQ
Copy link
Contributor

bjoernQ commented Jan 24, 2023

Thanks for the detailed writeup - the changes now make perfect sense to me

Just need to try it and it should be fine from my side

Copy link
Contributor

@bjoernQ bjoernQ left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM - Thanks for taking care of this

Only inconvenience on Windows was it also needs libLLVM-15.dll besides lld.exe renamed to rust-lld.exe

@MabezDev
Copy link
Member Author

LGTM - Thanks for taking care of this

Only inconvenience on Windows was it also needs libLLVM-15.dll besides lld.exe renamed to rust-lld.exe

Once we ship LLD with our Rust toolchain that dependency issue should be solved :).

@jessebraham jessebraham marked this pull request as draft January 24, 2023 16:02
@jessebraham
Copy link
Member

Converted this to a draft so it doesn't accidentally get merged before the two points I added to the original comment are resolved. I'll take a look at this either today or tomorrow.

@jessebraham
Copy link
Member

Sorry, as you can tell I completely forgot about this. Will try to revisit it in the next week or two here.

@jessebraham
Copy link
Member

This seems to be working! So once this is rebased and the direct-boot linker scripts for the S3 have been updated I see no reason not to merge this.

@MabezDev
Copy link
Member Author

Xtensa LLD is still not ready for use at the moment, and our linker scripts have diverged a lot since this PR, so I will close this and open a new one once LLD is ready.

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

Successfully merging this pull request may close these issues.

3 participants