Skip to content

Fix linking statics on Arm64EC #140176

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

dpaoliello
Copy link
Contributor

@dpaoliello dpaoliello commented Apr 22, 2025

Arm64EC builds recently started to fail due to the linker not finding a symbol:

symbols.o : error LNK2001: unresolved external symbol #_ZN3std9panicking11EMPTY_PANIC17hc8d2b903527827f1E (EC Symbol)
          C:\Code\hello-world\target\arm64ec-pc-windows-msvc\debug\deps\hello_world.exe : fatal error LNK1120: 1 unresolved externals

It turns out that EMPTY_PANIC is a new static variable that was being exported then imported from the standard library, but when exporting LLVM didn't prepend the name with # (as only functions are prefixed with this character), whereas Rust was prefixing with # when attempting to import it.

The fix is to have Rust not prefix statics with # when importing.

Adding tests discovered another issue: we need to correctly mark static exported from dylibs with DATA, otherwise MSVC's linker assumes they are functions and complains that there is no exit thunk for them.

CI found another bug: we only apply DllImport to non-local statics that aren't foreign items (i.e., in an extern block), that is we want to use DllImport for statics coming from other Rust crates. However, __rust_no_alloc_shim_is_unstable is a static generated by the Rust compiler if required, but downstream crates consider it a foreign item since it is declared in an extern "Rust" block, thus they do not apply DllImport to it and so fails to link if it is exported by the previous crate as DATA. The fix is to apply DllImport to foreign items that are marked with the rustc_std_internal_symbol attribute (i.e., we assume they aren't actually foreign and will be in some Rust crate).

Fixes #138541


try-job: dist-aarch64-msvc
try-job: dist-x86_64-msvc
try-job: x86_64-msvc-1
try-job: x86_64-msvc-2

@rustbot
Copy link
Collaborator

rustbot commented Apr 22, 2025

r? @Nadrieril

rustbot has assigned @Nadrieril.
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

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Apr 22, 2025
@rustbot
Copy link
Collaborator

rustbot commented Apr 22, 2025

Some changes occurred in compiler/rustc_codegen_ssa

cc @WaffleLapkin

@dpaoliello dpaoliello changed the title [Arm64EC] Only decorate functions with # [Arm64EC] Only decorate functions, not statics, with # Apr 22, 2025
Copy link
Member

@wesleywiser wesleywiser left a comment

Choose a reason for hiding this comment

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

Changes look good to me. Is it possible to write a regression test? (Either a full-scale integration test that links an exported static from one crate to another or a test that imports a static and dumps the symbol name or something?)

@Nadrieril
Copy link
Member

r? @wesleywiser

@rustbot rustbot assigned wesleywiser and unassigned Nadrieril Apr 23, 2025
@wesleywiser
Copy link
Member

@rustbot author

@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 Apr 25, 2025
@rustbot
Copy link
Collaborator

rustbot commented Apr 25, 2025

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@rustbot rustbot added the A-run-make Area: port run-make Makefiles to rmake.rs label Apr 25, 2025
@rustbot
Copy link
Collaborator

rustbot commented Apr 25, 2025

This PR modifies run-make tests.

cc @jieyouxu

@dpaoliello
Copy link
Contributor Author

Changes look good to me. Is it possible to write a regression test? (Either a full-scale integration test that links an exported static from one crate to another or a test that imports a static and dumps the symbol name or something?)

Good call, found another bug: we need to correctly export variables as DATA otherwise the linker expects them to have thunks.

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Apr 25, 2025
@dpaoliello dpaoliello changed the title [Arm64EC] Only decorate functions, not statics, with # Fix linking statics on Arm64EC Apr 25, 2025
@rust-log-analyzer

This comment has been minimized.

@wesleywiser
Copy link
Member

Thanks @dpaoliello!

@bors r+ rollup

Also nominating for potential beta backport as this fixes a serious issue for the Tier 2 aarch64-pc-windows-msvc.

@bors
Copy link
Collaborator

bors commented Apr 29, 2025

📌 Commit 7370c5c has been approved by wesleywiser

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 29, 2025
@wesleywiser wesleywiser added beta-nominated Nominated for backporting to the compiler in the beta channel. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 29, 2025
ChrisDenton added a commit to ChrisDenton/rust that referenced this pull request Apr 30, 2025
Fix linking statics on Arm64EC

Arm64EC builds recently started to fail due to the linker not finding a symbol:
```
symbols.o : error LNK2001: unresolved external symbol #_ZN3std9panicking11EMPTY_PANIC17hc8d2b903527827f1E (EC Symbol)
          C:\Code\hello-world\target\arm64ec-pc-windows-msvc\debug\deps\hello_world.exe : fatal error LNK1120: 1 unresolved externals
```

It turns out that `EMPTY_PANIC` is a new static variable that was being exported then imported from the standard library, but when exporting LLVM didn't prepend the name with `#` (as only functions are prefixed with this character), whereas Rust was prefixing with `#` when attempting to import it.

The fix is to have Rust not prefix statics with `#` when importing.

Adding tests discovered another issue: we need to correctly mark static exported from dylibs with `DATA`, otherwise MSVC's linker assumes they are functions and complains that there is no exit thunk for them.

Fixes rust-lang#138541
bors added a commit to rust-lang-ci/rust that referenced this pull request Apr 30, 2025
…enton

Rollup of 9 pull requests

Successful merges:

 - rust-lang#139192 (mention provenance in the pointer::wrapping_offset docs)
 - rust-lang#140176 (Fix linking statics on Arm64EC)
 - rust-lang#140404 (rm `TypeVistable` impls for `Canonical`)
 - rust-lang#140437 (enable msa feature for mips in codegen tests)
 - rust-lang#140438 (Add `rust.debug-assertions-tools` option)
 - rust-lang#140446 (chore: fix some tests)
 - rust-lang#140470 (CI: rfl: move job forward to Linux v6.15-rc4)
 - rust-lang#140476 (chore: delete unused ui/auxiliary crates)
 - rust-lang#140481 (Require sanitizers be enabled for asan_odr_windows.rs)

r? `@ghost`
`@rustbot` modify labels: rollup
@Zalathar
Copy link
Contributor

Possibly failed in rollup? #140495 (comment)

I'll kick off some try jobs to try to check whether this PR was the cause.

@bors r-

@bors bors 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-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Apr 30, 2025
@Zalathar
Copy link
Contributor

@bors try

bors added a commit to rust-lang-ci/rust that referenced this pull request Apr 30, 2025
Fix linking statics on Arm64EC

Arm64EC builds recently started to fail due to the linker not finding a symbol:
```
symbols.o : error LNK2001: unresolved external symbol #_ZN3std9panicking11EMPTY_PANIC17hc8d2b903527827f1E (EC Symbol)
          C:\Code\hello-world\target\arm64ec-pc-windows-msvc\debug\deps\hello_world.exe : fatal error LNK1120: 1 unresolved externals
```

It turns out that `EMPTY_PANIC` is a new static variable that was being exported then imported from the standard library, but when exporting LLVM didn't prepend the name with `#` (as only functions are prefixed with this character), whereas Rust was prefixing with `#` when attempting to import it.

The fix is to have Rust not prefix statics with `#` when importing.

Adding tests discovered another issue: we need to correctly mark static exported from dylibs with `DATA`, otherwise MSVC's linker assumes they are functions and complains that there is no exit thunk for them.

Fixes rust-lang#138541

---
try-job: dist-aarch64-msvc
try-job: dist-x86_64-msvc
try-job: x86_64-msvc-1
try-job: x86_64-msvc-2
@bors
Copy link
Collaborator

bors commented Apr 30, 2025

⌛ Trying commit 7370c5c with merge fcf787f...

@rust-log-analyzer

This comment has been minimized.

@bors
Copy link
Collaborator

bors commented Apr 30, 2025

💔 Test failed - checks-actions

@jieyouxu
Copy link
Member

@bors rollup=iffy (linkage gaming)

@dpaoliello
Copy link
Contributor Author

I can repro this locally, will investigate.

@rustbot author

@bors
Copy link
Collaborator

bors commented Apr 30, 2025

☔ The latest upstream changes (presumably #140520) made this pull request unmergeable. Please resolve the merge conflicts.

@jieyouxu
Copy link
Member

jieyouxu commented May 1, 2025

This was discussed in today's compiler triage meeting. The team will revisit the backport decision once the PR merges.

@wesleywiser
Copy link
Member

@bors try

bors added a commit to rust-lang-ci/rust that referenced this pull request May 1, 2025
Fix linking statics on Arm64EC

Arm64EC builds recently started to fail due to the linker not finding a symbol:
```
symbols.o : error LNK2001: unresolved external symbol #_ZN3std9panicking11EMPTY_PANIC17hc8d2b903527827f1E (EC Symbol)
          C:\Code\hello-world\target\arm64ec-pc-windows-msvc\debug\deps\hello_world.exe : fatal error LNK1120: 1 unresolved externals
```

It turns out that `EMPTY_PANIC` is a new static variable that was being exported then imported from the standard library, but when exporting LLVM didn't prepend the name with `#` (as only functions are prefixed with this character), whereas Rust was prefixing with `#` when attempting to import it.

The fix is to have Rust not prefix statics with `#` when importing.

Adding tests discovered another issue: we need to correctly mark static exported from dylibs with `DATA`, otherwise MSVC's linker assumes they are functions and complains that there is no exit thunk for them.

CI found another bug: we only apply `DllImport` to non-local statics that aren't foreign items (i.e., in an `extern` block), that is we want to use `DllImport` for statics coming from other Rust crates. However, `__rust_no_alloc_shim_is_unstable` is a static generated by the Rust compiler if required, but downstream crates consider it a foreign item since it is declared in an `extern "Rust"` block, thus they do not apply `DllImport` to it and so fails to link if it is exported by the previous crate as `DATA`. The fix is to apply `DllImport` to foreign items that are marked with the `rustc_std_internal_symbol` attribute (i.e., we assume they aren't actually foreign and will be in some Rust crate).

Fixes rust-lang#138541

---
try-job: dist-aarch64-msvc
try-job: dist-x86_64-msvc
try-job: x86_64-msvc-1
try-job: x86_64-msvc-2
@bors
Copy link
Collaborator

bors commented May 1, 2025

⌛ Trying commit fc4135f with merge 7db0add...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-run-make Area: port run-make Makefiles to rmake.rs beta-nominated Nominated for backporting to the compiler in the beta channel. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Linking error when compiled to arm64ec-pc-windows-msvc
8 participants