Skip to content

rustc emits an incorrect byte count suffix on MSVC #165299

Description

@folkertdev

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

  1. added
    O-windows
    A-ABI
    on Nov 1, 2026
  2. added
    needs-triage
    on Nov 1, 2026
  3. workingjubilee commented on Nov 1, 2026

    @workingjubilee
    Member
        let args_in_bytes: u64 = args
            .iter()
            .map(|abi| abi.layout.size.bytes().next_multiple_of(target.pointer_width as u64 / 8))
            .sum();

    cool, that's easy.

  4. folkertdev commented on Nov 1, 2026

    @folkertdev
    ContributorAuthor

    yeah, the tricky bit is integrating it and we'll have to see about the other backends.

  5. workingjubilee commented on Nov 1, 2026

    @workingjubilee
    Member

    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.

  6. paradoxicalguy commented on Nov 2, 2026

    @paradoxicalguy
    Member

    @rustbot claim

  7. paradoxicalguy commented on Nov 3, 2026

    @paradoxicalguy
    Member

    i have a working prototype for this.

    did something like:
    added a shared symbol-decoration helper in rustc_codegen_ssa using the original FnAbi argument layouts. updated LLVM codegen to use the decorated name with \x01 to 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@@32 for a 32-byte struct.
    cross-crate and foreign calls correctly reference vc_big@@32.
    i686 emits @fast_big@32, _std_big@32, and vec_big@@32.
    special-prefix object symbols behave as expected.

    currently verifying cranelift/GCC behavior

  8. folkertdev commented on Nov 3, 2026

    @folkertdev
    ContributorAuthor

    Something that is a bit tricky is that the behavior of \x01 is 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.

  9. added
    T-compiler
    A-codegen
    A-linkage
    A-LLVM
    and removed
    needs-triage
    on Nov 4, 2026
  10. 7 remaining items

  11. added 3 commits that reference this issue on Nov 10, 2026
    52e1b14
    9a3d123
    6274635
  12. added a commit that references this issue on Nov 10, 2026
    ceb2a18
  13. added a commit that references this issue on Nov 11, 2026
    448a09c
  14. added a commit that references this issue on Nov 12, 2026
    80f53bc
  15. folkertdev commented on Nov 16, 2026

    @folkertdev
    ContributorAuthor

    @bors try jobs=-msvc,-mingw-

  16. bjorn3 commented on Nov 16, 2026

    @bjorn3
    Member

    That needs to be done on the PR, not issue 😅

  17. folkertdev commented on Nov 16, 2026

    @folkertdev
    ContributorAuthor

    ah, too many tabs lol

  18. added 2 commits that reference this issue on Nov 16, 2026
  19. added a commit that references this issue on Nov 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

A-ABIA-LLVMA-codegenA-linkageO-windowsT-compiler

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions