Skip to content

Fails to link verbatim libary in 1.82 on macOS #132264

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
anatawa12 opened this issue Oct 28, 2024 · 5 comments · May be fixed by #138753
Open

Fails to link verbatim libary in 1.82 on macOS #132264

anatawa12 opened this issue Oct 28, 2024 · 5 comments · May be fixed by #138753
Labels
A-linkage Area: linking into static, shared libraries and binaries C-bug Category: This is a bug. O-macos Operating system: macOS regression-untriaged Untriaged performance or correctness regression. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@anatawa12
Copy link
Contributor

anatawa12 commented Oct 28, 2024

Code

Full project to reproduce this bug is at https://github.com/anatawa12/rust-1.82-linker-regression

I tried to link .a file at $OUT_DIR/testlib.a with the following build.rs code.

fn main() {
    let out_dir = PathBuf::from(std::env::var_os("OUT_DIR").unwrap());
    std::fs::create_dir_all(&out_dir).unwrap();
    let object = out_dir.join("testlib.o");
    let archive = out_dir.join("testlib.a");

    // buiid code here

    println!(
        "cargo:rustc-link-search=native={path}",
        path = out_dir.display()
    );
    println!("cargo:rustc-link-lib=static:+verbatim=testlib.a");
}

I ran cargo +stable test, linker reported ld: library 'testlib.a' not found, but with rust 1.81.0 (cargo +1.81.0 test) no linker error was reported.

Rust 1.82.0 on linux works well AFAIK (not tested with this code above, but works with real-world case I found this regression)

full build log with verbose
$ cargo +stable test --verbose
   Compiling rust-1-82-linker-regression v0.1.0 (/Users/anatawa12/RustroverProjects/rust-1-82-linker-regression)
     Running `/Users/anatawa12/.rustup/toolchains/stable-aarch64-apple-darwin/bin/rustc --crate-name build_script_build --edition=2021 build.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts,future-incompat --diagnostic-width=192 --crate-type bin --emit=dep-info,link -C embed-bitcode=no --check-cfg 'cfg(docsrs)' --check-cfg 'cfg(feature, values())' -C metadata=d7bb1aa767972a7a -C extra-filename=-d7bb1aa767972a7a --out-dir /Users/anatawa12/RustroverProjects/rust-1-82-linker-regression/target/debug/build/rust-1-82-linker-regression-d7bb1aa767972a7a -C incremental=/Users/anatawa12/RustroverProjects/rust-1-82-linker-regression/target/debug/incremental -L dependency=/Users/anatawa12/RustroverProjects/rust-1-82-linker-regression/target/debug/deps`
     Running `/Users/anatawa12/RustroverProjects/rust-1-82-linker-regression/target/debug/build/rust-1-82-linker-regression-d7bb1aa767972a7a/build-script-build`
     Running `/Users/anatawa12/.rustup/toolchains/stable-aarch64-apple-darwin/bin/rustc --crate-name rust_1_82_linker_regression --edition=2021 src/lib.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts,future-incompat --diagnostic-width=192 --crate-type lib --emit=dep-info,metadata,link -C embed-bitcode=no -C debuginfo=2 -C split-debuginfo=unpacked --check-cfg 'cfg(docsrs)' --check-cfg 'cfg(feature, values())' -C metadata=add4a5facfd2f7ab -C extra-filename=-add4a5facfd2f7ab --out-dir /Users/anatawa12/RustroverProjects/rust-1-82-linker-regression/target/debug/deps -C incremental=/Users/anatawa12/RustroverProjects/rust-1-82-linker-regression/target/debug/incremental -L dependency=/Users/anatawa12/RustroverProjects/rust-1-82-linker-regression/target/debug/deps -L native=/Users/anatawa12/RustroverProjects/rust-1-82-linker-regression/target/debug/build/rust-1-82-linker-regression-a47462767d058db3/out -l 'static:+verbatim=testlib.a'`
     Running `/Users/anatawa12/.rustup/toolchains/stable-aarch64-apple-darwin/bin/rustc --crate-name rust_1_82_linker_regression --edition=2021 src/lib.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts,future-incompat --diagnostic-width=192 --emit=dep-info,link -C embed-bitcode=no -C debuginfo=2 -C split-debuginfo=unpacked --test --check-cfg 'cfg(docsrs)' --check-cfg 'cfg(feature, values())' -C metadata=963aac10ba26161b -C extra-filename=-963aac10ba26161b --out-dir /Users/anatawa12/RustroverProjects/rust-1-82-linker-regression/target/debug/deps -C incremental=/Users/anatawa12/RustroverProjects/rust-1-82-linker-regression/target/debug/incremental -L dependency=/Users/anatawa12/RustroverProjects/rust-1-82-linker-regression/target/debug/deps -L native=/Users/anatawa12/RustroverProjects/rust-1-82-linker-regression/target/debug/build/rust-1-82-linker-regression-a47462767d058db3/out -l 'static:+verbatim=testlib.a'`
error: linking with `cc` failed: exit status: 1
  |
  = note: env -u IPHONEOS_DEPLOYMENT_TARGET -u TVOS_DEPLOYMENT_TARGET -u XROS_DEPLOYMENT_TARGET LC_ALL="C" PATH="/Users/anatawa12/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/bin:/Users/anatawa12/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/bin:/Users/anatawa12/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/bin:/Users/anatawa12/.cargo/bin:/Users/anatawa12/.opam/default/bin:/opt/homebrew/opt/coreutils/libexec/gnubin:/opt/homebrew/opt/grep/libexec/gnubin:/Users/anatawa12/.pyenv/shims:/Users/anatawa12/.rbenv/bin:/Users/anatawa12/.rbenv/shims:/opt/local/bin:/opt/local/sbin:/opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/appleinternal/bin:/opt/X11/bin:/Library/Apple/usr/bin:/Applications/Wireshark.app/Contents/MacOS:/usr/local/share/dotnet:~/.dotnet/tools:/Library/Frameworks/Mono.framework/Versions/Current/Commands:/Users/anatawa12/.cargo/bin:/opt/n/bin:/Users/anatawa12/bin:/Users/anatawa12/.dotnet/tools:/Users/anatawa12/go/bin" VSLANG="1033" ZERO_AR_DATE="1" "cc" "-arch" "arm64" "/var/folders/kw/yhxddc7x2_b9f__07m789msm0000gn/T/rustcxMuggb/symbols.o" "/Users/anatawa12/RustroverProjects/rust-1-82-linker-regression/target/debug/deps/rust_1_82_linker_regression-963aac10ba26161b.230w3ltx1tyin48jyz9gg62ve.rcgu.o" "/Users/anatawa12/RustroverProjects/rust-1-82-linker-regression/target/debug/deps/rust_1_82_linker_regression-963aac10ba26161b.2kpsu0kkm1iu0u33iw506w5qe.rcgu.o" "/Users/anatawa12/RustroverProjects/rust-1-82-linker-regression/target/debug/deps/rust_1_82_linker_regression-963aac10ba26161b.2rra8qibc8xluhhphpeac63l6.rcgu.o" "/Users/anatawa12/RustroverProjects/rust-1-82-linker-regression/target/debug/deps/rust_1_82_linker_regression-963aac10ba26161b.63fa677y8o4kupyjrhw2e06nv.rcgu.o" "/Users/anatawa12/RustroverProjects/rust-1-82-linker-regression/target/debug/deps/rust_1_82_linker_regression-963aac10ba26161b.dozsgtx5r5kuvil9ikjcppamh.rcgu.o" "/Users/anatawa12/RustroverProjects/rust-1-82-linker-regression/target/debug/deps/rust_1_82_linker_regression-963aac10ba26161b.4mrr65f7zswfitye8l3bs5h4p.rcgu.o" "-ltestlib.a" "/Users/anatawa12/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/lib/libtest-85a9da6da381f636.rlib" "/Users/anatawa12/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/lib/libgetopts-78018899fd21502d.rlib" "/Users/anatawa12/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/lib/libunicode_width-869d479eca56b68b.rlib" "/Users/anatawa12/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/lib/librustc_std_workspace_std-4410c8ee795fd46e.rlib" "/Users/anatawa12/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/lib/libstd-0b4a354a5d882f18.rlib" "/Users/anatawa12/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/lib/libpanic_unwind-00e89274fccf37d9.rlib" "/Users/anatawa12/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/lib/libobject-16cb3fa9562569e5.rlib" "/Users/anatawa12/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/lib/libmemchr-726032628236814d.rlib" "/Users/anatawa12/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/lib/libaddr2line-3a2050392888e443.rlib" "/Users/anatawa12/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/lib/libgimli-22481e5f0a7ce87c.rlib" "/Users/anatawa12/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/lib/librustc_demangle-99efda3e7d60c7d0.rlib" "/Users/anatawa12/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/lib/libstd_detect-97279fd1ed4043e6.rlib" "/Users/anatawa12/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/lib/libhashbrown-14aec4517faeb95b.rlib" "/Users/anatawa12/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/lib/librustc_std_workspace_alloc-ce8f05ca22908841.rlib" "/Users/anatawa12/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/lib/libminiz_oxide-45050aa8ac83ac5c.rlib" "/Users/anatawa12/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/lib/libadler-3a0b6babb2efec9c.rlib" "/Users/anatawa12/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/lib/libunwind-41b79f23e35577db.rlib" "/Users/anatawa12/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/lib/libcfg_if-7831295d10c91204.rlib" "/Users/anatawa12/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/lib/liblibc-f86e480d5dcc309d.rlib" "/Users/anatawa12/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/lib/liballoc-a7504b44dda8a2a3.rlib" "/Users/anatawa12/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/lib/librustc_std_workspace_core-589a9d977c5a484f.rlib" "/Users/anatawa12/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/lib/libcore-a17e2a568e77fc15.rlib" "/Users/anatawa12/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/lib/libcompiler_builtins-791901bf82ad4f12.rlib" "-lSystem" "-lc" "-lm" "-L" "/Users/anatawa12/RustroverProjects/rust-1-82-linker-regression/target/debug/build/rust-1-82-linker-regression-a47462767d058db3/out" "-o" "/Users/anatawa12/RustroverProjects/rust-1-82-linker-regression/target/debug/deps/rust_1_82_linker_regression-963aac10ba26161b" "-Wl,-dead_strip" "-nodefaultlibs"
  = note: ld: library 'testlib.a' not found
          clang: error: linker command failed with exit code 1 (use -v to see invocation)
          

error: could not compile `rust-1-82-linker-regression` (lib test) due to 1 previous error

Caused by:
  process didn't exit successfully: `/Users/anatawa12/.rustup/toolchains/stable-aarch64-apple-darwin/bin/rustc --crate-name rust_1_82_linker_regression --edition=2021 src/lib.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts,future-incompat --diagnostic-width=192 --emit=dep-info,link -C embed-bitcode=no -C debuginfo=2 -C split-debuginfo=unpacked --test --check-cfg 'cfg(docsrs)' --check-cfg 'cfg(feature, values())' -C metadata=963aac10ba26161b -C extra-filename=-963aac10ba26161b --out-dir /Users/anatawa12/RustroverProjects/rust-1-82-linker-regression/target/debug/deps -C incremental=/Users/anatawa12/RustroverProjects/rust-1-82-linker-regression/target/debug/incremental -L dependency=/Users/anatawa12/RustroverProjects/rust-1-82-linker-regression/target/debug/deps -L native=/Users/anatawa12/RustroverProjects/rust-1-82-linker-regression/target/debug/build/rust-1-82-linker-regression-a47462767d058db3/out -l 'static:+verbatim=testlib.a'` (exit status: 1)

Version it worked on

It most recently worked on: Rust 1.80 or 1.81 beta, and 1.81 stable on github actions

Version with regression

rustc --version --verbose:

rustc 1.82.0 (f6e511eec 2024-10-15)
binary: rustc
commit-hash: f6e511eec7342f59a25f7c0534f1dbea00d01b14
commit-date: 2024-10-15
host: aarch64-apple-darwin
release: 1.82.0
LLVM version: 19.1.1

Backtrace

No Compiler Crashes

Backtrace

<backtrace>

@anatawa12 anatawa12 added C-bug Category: This is a bug. regression-untriaged Untriaged performance or correctness regression. labels Oct 28, 2024
@rustbot rustbot added I-prioritize Issue: Indicates that prioritization has been requested for this issue. needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Oct 28, 2024
@anatawa12
Copy link
Contributor Author

other crates using this lib crate will build successfully. it fails only for tests on the crate

@ehuss
Copy link
Contributor

ehuss commented Oct 28, 2024

Looks like this was changed in #128400 to remove a backwards compatibility hack for tests. Maybe @petrochenkov could explain why it also affects verbatim?

@jieyouxu jieyouxu added the A-linkage Area: linking into static, shared libraries and binaries label Oct 28, 2024
@anatawa12
Copy link
Contributor Author

if !whole_archive {
self.link_or_cc_arg(format!("-l{colon}{name}"));
} else if self.sess.target.is_like_osx {
// -force_load is the macOS equivalent of --whole-archive, but it
// involves passing the full path to the library to link.
self.link_arg("-force_load");
self.link_arg(find_native_static_library(name, verbatim, self.sess));
} else {

I found that rustc solves path for archive with find_native_static_library for osx-like platforms if whole-archive enabled but rustc does not if whole-archive is disabled. this might be related.

I think it's very confusing that works for library crates and binary crates using them, but not working for test (or possibly binary crates with build.rs) builds.

@jieyouxu jieyouxu added O-macos Operating system: macOS T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Oct 30, 2024
@madsmtm
Copy link
Contributor

madsmtm commented Oct 31, 2024

I don't think this is specific to cargo test? Replacing lib.rs with a main.rs, I get the same error, and I get the error regardless of Rust version.

I notice that rustc is passing -ltestlib.a to the macOS linker, but that's just wrong? ld64 only supports verbatim .o in that position - the docs even call this difference in ld64 out, but the code doesn't seem to implement it...

bors added a commit to rust-lang-ci/rust that referenced this issue Oct 31, 2024
…try>

Apple: Fix direct linking with +verbatim

Linking with `+verbatim` somewhat worked before, but only when the library was included as part of an rlib.

Fixes rust-lang#132264.

CC `@petrochenkov,` since you've worked on `+verbatim` before.

`@rustbot` label O-apple A-linkage

try-job: aarch64-apple
try-job: x86_64-apple-2
bors added a commit to rust-lang-ci/rust that referenced this issue Oct 31, 2024
…try>

Apple: Fix direct linking with +verbatim

Linking with `+verbatim` somewhat worked before, but only when the library was included as part of an rlib.

Fixes rust-lang#132264.

CC `@petrochenkov,` since you've worked on `+verbatim` before.

`@rustbot` label O-apple A-linkage

try-job: aarch64-apple
try-job: x86_64-apple-2
@anatawa12
Copy link
Contributor Author

I don't think this is specific to cargo test? Replacing lib.rs with a main.rs, I get the same error, and I get the error regardless of Rust version.

Sorry I did not test that.

@jieyouxu jieyouxu removed the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Nov 11, 2024
@apiraino apiraino removed the I-prioritize Issue: Indicates that prioritization has been requested for this issue. label Nov 20, 2024
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
A-linkage Area: linking into static, shared libraries and binaries C-bug Category: This is a bug. O-macos Operating system: macOS regression-untriaged Untriaged performance or correctness regression. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
6 participants