[win][aarch64] Fix linking statics on Arm64EC, take 2 by dpaoliello · Pull Request #142742 · rust-lang/rust (original) (raw)

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.

This change broke dynamic linking, as far as I can tell. With DATA, the linker doesn't put the bare static name into the import lib, only the __imp_-prefixed one.

For example, the import lib for tests\ui\statics\auxiliary\check_static_recursion_foreign_helper.rs previously had:

    7 public symbols

      3A0 __IMPORT_DESCRIPTOR_check_static_recursion_foreign_helper
      642 __NULL_IMPORT_DESCRIPTOR
      796 check_static_recursion_foreign_helper_NULL_THUNK_DATA
      9EC __imp_test_static
      9EC test_static
      92C __imp_rust_metadata_check_static_recursion_foreign_helper_31b3b31070780c5b
      92C rust_metadata_check_static_recursion_foreign_helper_31b3b31070780c5b

now it has only:

    5 public symbols

      2F4 __IMPORT_DESCRIPTOR_check_static_recursion_foreign_helper
      596 __NULL_IMPORT_DESCRIPTOR
      6EA check_static_recursion_foreign_helper_NULL_THUNK_DATA
      940 __imp_test_static
      880 __imp_rust_metadata_check_static_recursion_foreign_helper_31b3b31070780c5b

The test happens to pass because it doesn't really access the static cross-crate. When it's modified to read test_static from its main function, it fails to link (x64 MSVC linker):

a.check_recursion_foreign.55e77c91ee99656d-cgu.0.rcgu.o : error LNK2019: unresolved external symbol test_static referenced in function _ZN23check_recursion_foreign4main17h519e10ac4c6da3d1E

The broken scenario doesn't look all that niche to me so maybe this should be fixed ASAP or reverted?
cc @bjorn3 @wesleywiser