Skip to content

panic_unwind: Use global_asm! for IMGREL relocations - #160183

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

panic_unwind: Use global_asm! for IMGREL relocations#160183
Darksonn wants to merge 5 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

@Mark-Simulacrum Mark-Simulacrum left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Definitely not familiar with the Windows aspects of this... but happy to approve given Bjorn has taken a look with a response to the comment (either telling me I'm wrong or changing things :)

View changes since this review

Comment thread library/panic_unwind/src/seh.rs Outdated
"lea {}, [rip + 2f]"
}
any(target_arch = "arm", target_arch = "aarch64", target_arch = "arm64ec") => {
"adr {}, 2f"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Isn't the range of this offset too small?

Is the program label whose address is to be calculated. It is an offset from the address of this instruction, in the range ±1MB.

(https://support.arm.com/documentation/dui0802/b/A64-General-Instructions/ADR)

I would expect we need to support programs where the instructions and rdata are farther apart. Should we be using adrl here?

(Not sure if lea has the same problem).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Using adrl makes sense to me. For lea, it can be encoded with a 16bit, 32bit or 64bit offset. I don't know which one LLVM picks. Probably either 32bit or 64bit.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm hoping this is the cause of the exit code: 0xc0000005, STATUS_ACCESS_VIOLATION in the try job, but once fixed we should re-run that. If it is the cause, I'm sad that LLVM / rustc didn't complain about the relocation overflowing, but maybe it's not that surprising.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

It looks like using wider offsets fixed the issue.

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Aug 8, 2026
@Mark-Simulacrum

Copy link
Copy Markdown
Member

@bors try jobs=msvc

@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Aug 8, 2026
panic_unwind: Use global_asm! for IMGREL relocations


try-job: *msvc*
@rust-log-analyzer

This comment has been minimized.

@rust-bors

rust-bors Bot commented Aug 9, 2026

Copy link
Copy Markdown
Contributor

💔 Test for ee5da48 failed: CI. Failed jobs:

@Darksonn

Darksonn commented Aug 9, 2026

Copy link
Copy Markdown
Member Author

@bors try jobs=msvc

@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Aug 9, 2026
panic_unwind: Use global_asm! for IMGREL relocations


try-job: *msvc*
@rust-log-analyzer

This comment has been minimized.

@rust-bors

rust-bors Bot commented Aug 9, 2026

Copy link
Copy Markdown
Contributor

💔 Test for aa22e3c failed: CI. Failed jobs:

@Darksonn

Copy link
Copy Markdown
Member Author

@bors try jobs=msvc

@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Aug 10, 2026
panic_unwind: Use global_asm! for IMGREL relocations


try-job: *msvc*
@rust-bors

rust-bors Bot commented Aug 10, 2026

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: 976338d (976338db6bad68e99abbdc7370002f113822c140)
Base parent: 7088e4b (7088e4b63a9516ebfbfe2ab2d999cf01a528ac14)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. 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.

7 participants