Repository navigation
Description
The byte count suffix that is added to symbols with the fastcall, vectorcall and stdcall windows msvc calling conventions is sometimes incorrect, on both i686 and x86_64. That leads to symbol names being wrong and hence to linker errors.
Normally a signature extern "fastcall" fn foo(_: u64) would be mangled as foo@8, with the number being the total sum of the byte count of the arguments, with each argument rounded up to the next multiple of the pointer width. However, rustc sometimes gets this wrong:
https://godbolt.org/z/v7WM9asYT
The issue is that we let LLVM generate the name, and it does so based on the signature that we provide it. When arguments are passed indirectly, LLVM only sees a pointer, and counts the argument as a pointer width. Clang instead calculates the byte count in the frontend, using the true size of the parameter.
Rust is also inconsistent with itself, because we do calculate the byte count ourselves in linking_symbol_name_for_instance_in_crate. The calculation of the byte count there matches Clang.
However that implementation misses that LLVM special-cases names that start with \x01 and ?.
The \x01 prefix stopping mangling is an LLVM feature, that works on other platforms too. In LLVM the \x01 is stripped, but cranelift or gcc keep the character in, leading to linker errors. GCC does implement a * prefix which serves the same purpose.
A ? prefix also stops mangling but the ? is kept (not stripped).
A naive fix for #164749 would exacerbate this issue, because later vector arguments are passed indirectly and the count would be off.
Related to #165279 too, it just tests the current names we emit.
Ultimately I think we'll have to do the byte count and mangling ourselves in all cases, matching clang. Clang does the mangling in the frontend, and uses \x01 to make sure the LLVM backend does not re-mangle the name a second time.
Activity
workingjubilee commentedon Nov 1, 2026
cool, that's easy.
folkertdev commentedon Nov 1, 2026
yeah, the tricky bit is integrating it and we'll have to see about the other backends.
workingjubilee commentedon Nov 1, 2026
we should probably have some way for it to be possible for a codegen backend to ask rustc about how a symbol's name should be handled when we know.
paradoxicalguy commentedon Nov 2, 2026
@rustbot claim
paradoxicalguy commentedon Nov 3, 2026
i have a working prototype for this.
did something like:
added a shared symbol-decoration helper in
rustc_codegen_ssausing the originalFnAbiargument layouts. updated LLVM codegen to use the decorated name with\x01to prevent double mangling. reused the helper in the linker-facing naming path and handled\x01/?prefixes.verified:
x86_64 vectorcall:
vc_big@@8→vc_big@@32for a 32-byte struct.cross-crate and foreign calls correctly reference
vc_big@@32.i686 emits
@fast_big@32,_std_big@32, andvec_big@@32.special-prefix object symbols behave as expected.
currently verifying cranelift/GCC behavior
folkertdev commentedon Nov 3, 2026
Something that is a bit tricky is that the behavior of
\x01is not specified, and backend-specific. In LLVM, it's not a windows-specific thing either, and so we may need to be careful with that.7 remaining items
Rollup merge of rust-lang#165458 - paradoxicalguy:fix-msvc-byte-count…
Rollup merge of rust-lang#165458 - paradoxicalguy:fix-msvc-byte-count…
Rollup merge of rust-lang#165458 - paradoxicalguy:fix-msvc-byte-count…
Unrolled build for #165458 in rollup 165760
Rollup merge of #165458 - paradoxicalguy:fix-msvc-byte-count-clean, r…
Rollup merge of #165458 - paradoxicalguy:fix-msvc-byte-count-clean, r…
folkertdev commentedon Nov 16, 2026
@bors try jobs=-msvc,-mingw-
bjorn3 commentedon Nov 16, 2026
That needs to be done on the PR, not issue 😅
folkertdev commentedon Nov 16, 2026
ah, too many tabs lol
Rollup merge of rust-lang#165881 - paradoxicalguy:fix-msvc-byte-count…
Rollup merge of rust-lang#165881 - paradoxicalguy:fix-msvc-byte-count…
Unrolled build for #165881 in rollup 166059
Rollup merge of #165881 - paradoxicalguy:fix-msvc-byte-count-v2, r=fo…
Rollup merge of #165881 - paradoxicalguy:fix-msvc-byte-count-v2, r=fo…