Skip to content

Fix ThinLTO dropping exported symbols whose name starts with \x01 - #165790

Closed
cezarbbb wants to merge 1 commit into
rust-lang:mainfrom
cezarbbb:fix-thinlto-x01-preserve
Closed

cezarbbb wants to merge 1 commit into
rust-lang:mainfrom
cezarbbb:fix-thinlto-x01-preserve

Conversation

@cezarbbb

@cezarbbb cezarbbb commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Now that #165458 has landed, the LTO preserve list can carry both the undecorated and the decorated spelling of an exported symbol.

However, one case is still missed: names that already start with \x01. The preserve list is hashed as written, but ThinLTO summary GUIDs are computed from the name with that marker stripped, so these symbols are never seeded as live roots, get internalized by the thin link, and disappear from optimized builds that use local ThinLTO (for example an -O staticlib).

This PR extends symbol_names_for_lto to emit both spellings for such names as well, and a run-make test is added.

Example:

// lib.rs
#![crate_type = "staticlib"]

#[unsafe(export_name = "\x01ctl_export")]
pub extern "C" fn ctl_export(_: u32) {}

#[unsafe(no_mangle)]
pub extern "C" fn plain_export(_: u32) {}
// main.c
extern void ctl_export(unsigned);

int main(void) {
    ctl_export(1);
    return 0;
}

Output:

$ rustc -O lib.rs -o libctl.a
$ nm libctl.a | grep -E 'ctl_export|plain_export'
0000000000000000 T plain_export
$ cc main.c libctl.a -o main
/usr/bin/ld: /tmp/ccLHN2r0.o: in function `main':
main.c:(.text+0xe): undefined reference to `ctl_export'
collect2: error: ld returned 1 exit status

LLM disclosure: The code and text are written by hand.

r? @folkertdev

@rustbot rustbot added A-run-make Area: port run-make Makefiles to rmake.rs 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 Nov 11, 2026
@rust-log-analyzer

This comment has been minimized.

@cezarbbb
cezarbbb force-pushed the fix-thinlto-x01-preserve branch from 8607a41 to 2a3bb0a Compare November 11, 2026 04:45
@bjorn3

bjorn3 commented Nov 11, 2026

Copy link
Copy Markdown
Member

but ThinLTO summary GUIDs are computed from the name with that marker stripped

So \x01foo and foo would get the same GUID on Windows despite ending up with entirely separate symbols in the object file? That if we only want to export one of the two?

@cezarbbb

Copy link
Copy Markdown
Contributor Author

but ThinLTO summary GUIDs are computed from the name with that marker stripped

So \x01foo and foo would get the same GUID on Windows despite ending up with entirely separate symbols in the object file? That if we only want to export one of the two?

I think this case is even less likely than I originally assumed, because \x01foo and foo appearing together can only come from an unmangled definition the user wrote themselves (#[no_mangle]/export_name), I suppose?

And as for which spelling gets to be "the accepted one", I'd go with the one users are more likely to write. The GUID is computed from the marker-stripped name, so the plain spelling is what actually matches, since this seems to be a limitation of LLVM's GUID design.

Do you have a better idea?

@cezarbbb

Copy link
Copy Markdown
Contributor Author

Oh, it seems #165458 has been reverted:< in that case, this PR should probably also be reconsidered.

@folkertdev

Copy link
Copy Markdown
Contributor

It's reverted for perf reasons, but I think the perf problems can be solved. We'll re-land it but that might take a couple of days.

@rust-bors

rust-bors Bot commented Nov 12, 2026

Copy link
Copy Markdown
Contributor

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

JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Nov 16, 2026
…-v2, r=folkertdev

fix MSVC calling convention byte counts

performance fixed version of rust-lang#165458
original fix _reverted_ in rust-lang#165856 due to perf issues.

fixes rust-lang#165299
companion test pr: rust-lang#165771

the regression came from unnecessary `fn_abi_of_instance` queries on targets that don't need symbol decoration. this now skips those queries while keeping non-windows `vectorcall` handling.

perf: local rustc-perf benchmarks on aarch64 (instructions:u):
original fix: +0.83%
optimized fix: ~0.00%

testing:
fastcall, stdcall and vectorcall codegen tests
i686/x86_64 msvc linking with and without thinlto, verified dll exports
lto smoke tests
also handles `\x01`-prefixed symbols under thinlto (related: rust-lang#165790).

r? @folkertdev
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Nov 16, 2026
…-v2, r=folkertdev

fix MSVC calling convention byte counts

performance fixed version of rust-lang#165458
original fix _reverted_ in rust-lang#165856 due to perf issues.

fixes rust-lang#165299
companion test pr: rust-lang#165771

the regression came from unnecessary `fn_abi_of_instance` queries on targets that don't need symbol decoration. this now skips those queries while keeping non-windows `vectorcall` handling.

perf: local rustc-perf benchmarks on aarch64 (instructions:u):
original fix: +0.83%
optimized fix: ~0.00%

testing:
fastcall, stdcall and vectorcall codegen tests
i686/x86_64 msvc linking with and without thinlto, verified dll exports
lto smoke tests
also handles `\x01`-prefixed symbols under thinlto (related: rust-lang#165790).

r? @folkertdev
rust-bors Bot pushed a commit that referenced this pull request Nov 16, 2026
Rollup merge of #165881 - paradoxicalguy:fix-msvc-byte-count-v2, r=folkertdev

fix MSVC calling convention byte counts

performance fixed version of #165458
original fix _reverted_ in #165856 due to perf issues.

fixes #165299
companion test pr: #165771

the regression came from unnecessary `fn_abi_of_instance` queries on targets that don't need symbol decoration. this now skips those queries while keeping non-windows `vectorcall` handling.

perf: local rustc-perf benchmarks on aarch64 (instructions:u):
original fix: +0.83%
optimized fix: ~0.00%

testing:
fastcall, stdcall and vectorcall codegen tests
i686/x86_64 msvc linking with and without thinlto, verified dll exports
lto smoke tests
also handles `\x01`-prefixed symbols under thinlto (related: #165790).

r? @folkertdev
@cezarbbb

Copy link
Copy Markdown
Contributor Author

Since #165881 has been merged, I will close this PR:)

@cezarbbb cezarbbb closed this Nov 17, 2026
@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Nov 17, 2026
sushink70 pushed a commit to sushink70/miri that referenced this pull request Nov 17, 2026
…lkertdev

fix MSVC calling convention byte counts

performance fixed version of rust-lang/rust#165458
original fix _reverted_ in rust-lang/rust#165856 due to perf issues.

fixes rust-lang/rust#165299
companion test pr: rust-lang/rust#165771

the regression came from unnecessary `fn_abi_of_instance` queries on targets that don't need symbol decoration. this now skips those queries while keeping non-windows `vectorcall` handling.

perf: local rustc-perf benchmarks on aarch64 (instructions:u):
original fix: +0.83%
optimized fix: ~0.00%

testing:
fastcall, stdcall and vectorcall codegen tests
i686/x86_64 msvc linking with and without thinlto, verified dll exports
lto smoke tests
also handles `\x01`-prefixed symbols under thinlto (related: rust-lang/rust#165790).

r? @folkertdev
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 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.

5 participants