Skip to content
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

Fix Access Violation when using lld & ThinLTO on windows-msvc #103353

Merged
merged 2 commits into from
Nov 9, 2022

Commits on Nov 3, 2022

  1. Add test case

    wesleywiser committed Nov 3, 2022
    Configuration menu
    Copy the full SHA
    cf6efe8 View commit details
    Browse the repository at this point in the history
  2. Fix Access Violation when using lld & ThinLTO on windows-msvc

    Users report an AV at runtime of the compiled binary when using lld and
    ThinLTO on windows-msvc. The AV occurs when accessing a static value
    which is defined in one crate but used in another. Based on the
    disassembly of the cross-crate use, it appears that the use is not
    correctly linked with the definition and is instead assigned a garbage
    pointer value.
    
    If we look at the symbol tables for each crates' obj file, we can see
    what is happening:
    
    *lib.obj*:
    
    ```
    COFF SYMBOL TABLE
    ...
    00E 00000000 SECT2  notype       External     | _ZN10reproducer7memrchr2FN17h612b61ca0e168901E
    ...
    ```
    
    *bin.obj*:
    
    ```
    COFF SYMBOL TABLE
    ...
    010 00000000 UNDEF  notype       External     | __imp__ZN10reproducer7memrchr2FN17h612b61ca0e168901E
    ...
    ```
    
    The use of the symbol has the "import" style symbol name but the
    declaration doesn't generate any symbol with the same name. As a result,
    linking the files generates a warning from lld:
    
    > rust-lld: warning: bin.obj: locally defined symbol imported: reproducer::memrchr::FN::h612b61ca0e168901 (defined in lib.obj) [LNK4217]
    
    and the symbol reference remains undefined at runtime leading to the AV.
    
    To fix this, we just need to detect that we are performing ThinLTO (and
    thus, static linking) and omit the `dllimport` attribute on the extern
    item in LLVM IR.
    wesleywiser committed Nov 3, 2022
    Configuration menu
    Copy the full SHA
    296489c View commit details
    Browse the repository at this point in the history