From 74f39b8e1c131530800b6ced472b2a348b5ee6a7 Mon Sep 17 00:00:00 2001 From: Koi Date: Sun, 1 Nov 2026 18:31:30 -0500 Subject: [PATCH] snmalloc-rs: keep LTCG library out of the rlib with build_cc on MSVC With build_cc and lto on MSVC the library is compiled with /GL and cc bundles it into the snmalloc-sys rlib. rustc cannot read the symbol table of LTCG objects, so the bundled copy exports nothing and the final link fails with unresolved sn_rust_* symbols. Mark the library -bundle so it is passed to link.exe directly, as the CMake build does, and skip the duplicate link-lib line that rustc would reject. Add a build_cc lto job to the Rust CI matrix. Fixes #889 --- .github/workflows/rust.yml | 2 ++ snmalloc-rs/snmalloc-sys/Cargo.toml | 2 +- snmalloc-rs/snmalloc-sys/build.rs | 10 ++++++++++ 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 7d2f0245d..726fd2c93 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -51,6 +51,8 @@ jobs: args: "--features local_dynamic_tls" - name: lto args: "--features lto" + - name: build_cc-lto + args: '--features "build_cc lto"' # Don't abort runners if a single one fails fail-fast: false steps: diff --git a/snmalloc-rs/snmalloc-sys/Cargo.toml b/snmalloc-rs/snmalloc-sys/Cargo.toml index ea1411b18..20a6e562c 100644 --- a/snmalloc-rs/snmalloc-sys/Cargo.toml +++ b/snmalloc-rs/snmalloc-sys/Cargo.toml @@ -21,7 +21,7 @@ include = [ # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [build-dependencies] -cc = { version = "1.0", optional = true } +cc = { version = "1.0.74", optional = true } cmake = { version = "0.1", optional = true } [features] diff --git a/snmalloc-rs/snmalloc-sys/build.rs b/snmalloc-rs/snmalloc-sys/build.rs index 31dd5b561..26ce4e9bb 100644 --- a/snmalloc-rs/snmalloc-sys/build.rs +++ b/snmalloc-rs/snmalloc-sys/build.rs @@ -410,6 +410,12 @@ fn configure_platform(config: &mut BuildConfig) { .define("CMAKE_INTERPROCEDURAL_OPTIMIZATION", "TRUE") .define("SNMALLOC_IPO", "ON"); println!("cargo:rustc-link-arg=/LTCG"); + // rustc cannot index /GL (LTCG) objects, so bundling the + // library into the rlib drops its sn_rust_* symbols. Leave it + // out of the rlib and pass it to the final link, as the CMake + // build already does (rust-lang/rust#26003). + #[cfg(feature = "build_cc")] + config.builder.link_lib_modifier("-bundle"); } config.builder @@ -686,6 +692,10 @@ fn main() { println!("cargo:rustc-link-arg=-Wl,--whole-archive"); println!("cargo:rustc-link-lib=static={}", config.target_lib); println!("cargo:rustc-link-arg=-Wl,--no-whole-archive"); + } else if cfg!(feature = "build_cc") && config.is_msvc() { + // cc already emits the link-lib line for the library (with -bundle + // under lto). rustc rejects a second line for the same library when + // either one carries modifiers. } else { println!("cargo:rustc-link-lib={}", config.target_lib); }