Skip to content

panic_unwind: Use global_asm! for IMGREL relocations - #160183

Open
Darksonn wants to merge 3 commits into
rust-lang:mainfrom
Darksonn:seh-imgrel
Open

panic_unwind: Use global_asm! for IMGREL relocations#160183
Darksonn wants to merge 3 commits into
rust-lang:mainfrom
Darksonn:seh-imgrel

Conversation

@Darksonn

@Darksonn Darksonn commented Jul 29, 2026

Copy link
Copy Markdown
Member

View all comments

Rust consteval cannot construct 32-bit values that contain the offset between two symbols, but this is required for panic_unwind on some architectures because unwinding on Windows SEH uses image-base-relative pointers for the panic information (which makes them fit in 32-bit rather than 64-bit).

Currently, this is worked around by initializing these globals on panic with atomic stores. This works, but is undefined behavior as the panic runtime reads the values with non-atomic loads, which leads to a data race.

Instead, utilize global_asm! to create the globals with @IMGREL relocations so that the linker constructs the relative pointers for us at link or load time.

For context: #t-compiler > relative pointers in windows SEH panic_unwind @ 💬

AI assistance was involved with writing the assembly code.

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Jul 29, 2026
@rustbot

rustbot commented Jul 29, 2026

Copy link
Copy Markdown
Collaborator

r? @LawnGnome

rustbot has assigned @LawnGnome.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: libs
  • libs expanded to 12 candidates
  • Random selection from 7 candidates

Comment thread library/panic_unwind/src/seh.rs Outdated
Comment thread library/panic_unwind/src/seh.rs Outdated
Comment thread library/panic_unwind/src/seh.rs Outdated
Comment thread library/panic_unwind/src/seh.rs Outdated
Comment thread library/panic_unwind/src/seh.rs Outdated
Comment on lines +80 to +81
// offsets from the `__ImageBase` symbol. It's not currently possible to create
// a offset that is `__ImageBase` relative in Rust code, so this is done using

@Fulgen301 Fulgen301 Jul 29, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

(Nitpick: I'd clarify that they're offsets from the image base and not __ImageBase, even if that's the exact same address.)

View changes since the review

@LawnGnome

Copy link
Copy Markdown
Contributor

Re-rolling, as my asm is absolutely not good enough for this.

@rustbot reroll

@rustbot rustbot assigned Mark-Simulacrum and unassigned LawnGnome Jul 30, 2026
Comment thread library/panic_unwind/src/seh.rs Outdated
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants