rust-lang / rust

Empowering everyone to build reliable and efficient software.
https://www.rust-lang.org
Other
98.15k stars 12.69k forks source link

Failed to add native library libssl.a when compiling openssl-vendored from macos to linux. #131407

Closed xuxiaocheng0201 closed 3 weeks ago

xuxiaocheng0201 commented 3 weeks ago

Code

Simply cross-compile openssl from aarch64-apple-darwin to x86_64-unknown-linux-musl with vendored feature.

Cargo.toml:

[package]
name = "tester"
version = "0.1.0"
edition = "2021"

[dependencies]
openssl = { version = "~0.10", features = ["vendored"] }

main.rs:

fn main() {}

Command: cargo build --target x86_64-unknown-linux-musl

Meta

rustc --version --verbose:

rustc 1.81.0 (eeb90cda1 2024-09-04)
binary: rustc
commit-hash: eeb90cda1969383f56a2637cbd3037bdf598841c
commit-date: 2024-09-04
host: aarch64-apple-darwin
release: 1.81.0
LLVM version: 18.1.7

Then I tried nightly, But it gives a different output:

error: failed to add native library /Users/xuxiaocheng/Rust/tester/target/x86_64-unknown-linux-musl/debug/build/openssl-sys-46078b20ef0b4c0a/out/openssl-build/install/lib/libssl.a: invalid utf-8 sequence of 1 bytes from index 0

Error output

thread 'rustc' panicked at compiler/rustc_codegen_llvm/src/back/archive.rs:418:67:
   Compiling openssl v0.10.66
called `Option::unwrap()` on a `None` value
stack backtrace:
   0:        0x1037a373c - <std::sys::backtrace::BacktraceLock::print::DisplayBacktrace as core::fmt::Display>::fmt::h243268f17d714c7f
   1:        0x1037e6688 - core::fmt::write::hb3cfb8a30e72d7ff
   2:        0x103799720 - std::io::Write::write_fmt::hfb2314975de9ecf1
   3:        0x1037a5c4c - std::panicking::default_hook::{{closure}}::h14c7718ccf39d316
   4:        0x1037a5870 - std::panicking::default_hook::hc62e60da3be2f352
   5:        0x10d39c5b8 - <alloc[47bc6d386d7ae45f]::boxed::Box<rustc_driver_impl[54c40c94c6cfc0b2]::install_ice_hook::{closure#0}> as core[f827f14b5e761a5d]::ops::function::Fn<(&dyn for<'a, 'b> core[f827f14b5e761a5d]::ops::function::Fn<(&'a std[4f7d7c3ef984657a]::panic::PanicHookInfo<'b>,), Output = ()> + core[f827f14b5e761a5d]::marker::Sync + core[f827f14b5e761a5d]::marker::Send, &std[4f7d7c3ef984657a]::panic::PanicHookInfo)>>::call
   6:        0x1037a6868 - std::panicking::rust_panic_with_hook::h09e8a656f11e82b2
   7:        0x1037a6150 - std::panicking::begin_panic_handler::{{closure}}::h1230eb3cc91b241c
   8:        0x1037a3bc8 - std::sys::backtrace::__rust_end_short_backtrace::hc3491307aceda2c2
   9:        0x1037a5e40 - _rust_begin_unwind
  10:        0x103800ad8 - core::panicking::panic_fmt::ha4b80a05b9fff47a
  11:        0x103800b44 - core::panicking::panic::h298549a7412a7069
  12:        0x103800a60 - core::option::unwrap_failed::hb7af631ec4f78cd6
  13:        0x10d0bab70 - <rustc_codegen_llvm[642c7aa2dd314cbe]::back::archive::LlvmArchiveBuilder as rustc_codegen_ssa[b20976fedc9b5a2b]::back::archive::ArchiveBuilder>::build
  14:        0x10d1789fc - rustc_codegen_ssa[b20976fedc9b5a2b]::back::link::link_binary
  15:        0x10d012454 - <rustc_codegen_llvm[642c7aa2dd314cbe]::LlvmCodegenBackend as rustc_codegen_ssa[b20976fedc9b5a2b]::traits::backend::CodegenBackend>::link
  16:        0x10dc68e10 - <rustc_interface[1340bb505392beac]::queries::Linker>::link
  17:        0x10d392a1c - <scoped_tls[df49f867320abf2e]::ScopedKey<rustc_span[ab16d476329f5d04]::SessionGlobals>>::set::<rustc_interface[1340bb505392beac]::util::run_in_thread_with_globals<rustc_interface[1340bb505392beac]::interface::run_compiler<core[f827f14b5e761a5d]::result::Result<(), rustc_span[ab16d476329f5d04]::ErrorGuaranteed>, rustc_driver_impl[54c40c94c6cfc0b2]::run_compiler::{closure#0}>::{closure#1}, core[f827f14b5e761a5d]::result::Result<(), rustc_span[ab16d476329f5d04]::ErrorGuaranteed>>::{closure#0}::{closure#0}::{closure#0}, core[f827f14b5e761a5d]::result::Result<(), rustc_span[ab16d476329f5d04]::ErrorGuaranteed>>
  18:        0x10d39db34 - rustc_span[ab16d476329f5d04]::create_session_globals_then::<core[f827f14b5e761a5d]::result::Result<(), rustc_span[ab16d476329f5d04]::ErrorGuaranteed>, rustc_interface[1340bb505392beac]::util::run_in_thread_with_globals<rustc_interface[1340bb505392beac]::interface::run_compiler<core[f827f14b5e761a5d]::result::Result<(), rustc_span[ab16d476329f5d04]::ErrorGuaranteed>, rustc_driver_impl[54c40c94c6cfc0b2]::run_compiler::{closure#0}>::{closure#1}, core[f827f14b5e761a5d]::result::Result<(), rustc_span[ab16d476329f5d04]::ErrorGuaranteed>>::{closure#0}::{closure#0}::{closure#0}>
  19:        0x10d3bb8fc - std[4f7d7c3ef984657a]::sys::backtrace::__rust_begin_short_backtrace::<rustc_interface[1340bb505392beac]::util::run_in_thread_with_globals<rustc_interface[1340bb505392beac]::interface::run_compiler<core[f827f14b5e761a5d]::result::Result<(), rustc_span[ab16d476329f5d04]::ErrorGuaranteed>, rustc_driver_impl[54c40c94c6cfc0b2]::run_compiler::{closure#0}>::{closure#1}, core[f827f14b5e761a5d]::result::Result<(), rustc_span[ab16d476329f5d04]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[f827f14b5e761a5d]::result::Result<(), rustc_span[ab16d476329f5d04]::ErrorGuaranteed>>
  20:        0x10d39b7dc - <<std[4f7d7c3ef984657a]::thread::Builder>::spawn_unchecked_<rustc_interface[1340bb505392beac]::util::run_in_thread_with_globals<rustc_interface[1340bb505392beac]::interface::run_compiler<core[f827f14b5e761a5d]::result::Result<(), rustc_span[ab16d476329f5d04]::ErrorGuaranteed>, rustc_driver_impl[54c40c94c6cfc0b2]::run_compiler::{closure#0}>::{closure#1}, core[f827f14b5e761a5d]::result::Result<(), rustc_span[ab16d476329f5d04]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[f827f14b5e761a5d]::result::Result<(), rustc_span[ab16d476329f5d04]::ErrorGuaranteed>>::{closure#1} as core[f827f14b5e761a5d]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
  21:        0x1037aefa4 - std::sys::pal::unix::thread::Thread::new::thread_start::h1bd1b9c95010bf71
  22:        0x1979b32e4 - __pthread_deallocate

error: the compiler unexpectedly panicked. this is a bug.

note: we would appreciate a bug report: https://github.com/rust-lang/rust/issues/new?labels=C-bug%2C+I-ICE%2C+T-compiler&template=ice.md

note: rustc 1.81.0 (eeb90cda1 2024-09-04) running on aarch64-apple-darwin

note: compiler flags: --crate-type rlib -C embed-bitcode=no -C debuginfo=2

note: some of the compiler flags provided by cargo are hidden

query stack during panic:
end of query stack
Backtrace

``` Compiling openssl-sys v0.9.103 thread 'rustc' panicked at compiler/rustc_codegen_llvm/src/back/archive.rs:418:67: called `Option::unwrap()` on a `None` value stack backtrace: 0: _rust_begin_unwind 1: core::panicking::panic_fmt Compiling openssl v0.10.66 2: core::panicking::panic 3: core::option::unwrap_failed 4: ::build 5: rustc_codegen_ssa::back::link::link_binary 6: ::link 7: ::link 8: >::set::, rustc_driver_impl::run_compiler::{closure#0}>::{closure#1}, core::result::Result<(), rustc_span::ErrorGuaranteed>>::{closure#0}::{closure#0}::{closure#0}, core::result::Result<(), rustc_span::ErrorGuaranteed>> 9: rustc_span::create_session_globals_then::, rustc_interface::util::run_in_thread_with_globals, rustc_driver_impl::run_compiler::{closure#0}>::{closure#1}, core::result::Result<(), rustc_span::ErrorGuaranteed>>::{closure#0}::{closure#0}::{closure#0}> note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace. error: the compiler unexpectedly panicked. this is a bug. note: we would appreciate a bug report: https://github.com/rust-lang/rust/issues/new?labels=C-bug%2C+I-ICE%2C+T-compiler&template=ice.md note: rustc 1.81.0 (eeb90cda1 2024-09-04) running on aarch64-apple-darwin note: compiler flags: --crate-type rlib -C embed-bitcode=no -C debuginfo=2 note: some of the compiler flags provided by cargo are hidden query stack during panic: end of query stack ```

xuxiaocheng0201 commented 3 weeks ago

Could it be due to my incorrect environment variable settings?

brew install filosottile/musl-cross/musl-cross

export CC_x86_64_unknown_linux_musl=/opt/homebrew/Cellar/musl-cross/0.9.9_2/bin/x86_64-linux-musl-gcc
export AR_x86_64_unknown_linux_musl=/opt/homebrew/Cellar/musl-cross/0.9.9_2/bin/x86_64-linux-musl-ar

What should the correct environment variable settings be like? I couldn't find any relevant documentation to describe it. If anyone could provide a clue, I would be extremely grateful.

bjorn3 commented 3 weeks ago

Could you show the output of xxd /Users/xuxiaocheng/Rust/tester/target/x86_64-unknown-linux-musl/debug/build/openssl-sys-46078b20ef0b4c0a/out/openssl-build/install/lib/libssl.a | head and ar t /Users/xuxiaocheng/Rust/tester/target/x86_64-unknown-linux-musl/debug/build/openssl-sys-46078b20ef0b4c0a/out/openssl-build/install/lib/libssl.a after you build with nightly and get the error?

Also I'm removed the ICE label as this no longer gives an ICE on nightly.

xuxiaocheng0201 commented 3 weeks ago

Sure! @bjorn3 The output of xxd /Users/xuxiaocheng/Rust/tester/target/x86_64-unknown-linux-musl/debug/build/openssl-sys-46078b20ef0b4c0a/out/openssl-build/install/lib/libssl.a | head is:

00000000: 213c 6172 6368 3e0a 2331 2f32 3020 2020  !<arch>.#1/20   
00000010: 2020 2020 2020 2020 3137 3238 3537 3434          17285744
00000020: 3839 2020 3530 3120 2020 3230 2020 2020  89  501   20    
00000030: 3130 3036 3434 2020 3238 2020 2020 2020  100644  28      
00000040: 2020 600a 5f5f 2e53 594d 4445 4620 534f    `.__.SYMDEF SO
00000050: 5254 4544 0000 0000 0000 0000 0000 0000  RTED............
00000060: 2331 2f31 3220 2020 2020 2020 2020 2020  #1/12           
00000070: 3020 2020 2020 2020 2020 2020 3020 2020  0           0   
00000080: 2020 3020 2020 2020 3020 2020 2020 2020    0     0       
00000090: 3534 3133 3220 2020 2020 600a 2f00 0000  54132     `./...

The output of ar t /Users/xuxiaocheng/Rust/tester/target/x86_64-unknown-linux-musl/debug/build/openssl-sys-46078b20ef0b4c0a/out/openssl-build/install/lib/libssl.a is: Image

bjorn3 commented 3 weeks ago

I looks like the libssl.a produced by openssl-build is corrupt. The output of ar t shows the names of all archive members, which are supposed to consist of exclusively ASCII characters. Rightfully errors out on corrupt archives. As nightly no longer ICE's, I don't think there is anything left to do on the rustc side. That said, I don't mind trying to help you with debugging why a corrupt libssl.a is produced. Something I've seen cause corruption of static library archives in the past is when something generates a macOS style archive and then the archive is manipulated by a tool that expects Linux style archives or vice versa. One thing you could try is setting export RANLIB_x86_64_unknown_linux_musl=/opt/homebrew/Cellar/musl-cross/0.9.9_2/bin/x86_64-linux-musl-ranlib and then rebuilding after a cargo clean. Ranlib is a tool which runs on archives to add/update a symbol table. The symbol table format is one of the major differences between macOS style and Linux style archives.

xuxiaocheng0201 commented 3 weeks ago

Wow, it works. But there is another question that is not related to this issue. Then the output is:

 WARN rustc_codegen_ssa::back::link Linker does not support -static-pie command line option. Retrying with -static instead.
error: linking with `cc` failed: exit status: 1
  |
  = note: LC_ALL="C" PATH="/Users/xuxiaocheng/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/bin:/Users/xuxiaocheng/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/bin/self-contained:/Users/xuxiaocheng/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/bin:/Users/xuxiaocheng/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/bin/self-contained:/Users/xuxiaocheng/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/bin:/Users/xuxiaocheng/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/bin/self-contained:/Users/xuxiaocheng/.cargo/bin:/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:/Library/Apple/usr/bin:/usr/local/share/dotnet:~/.dotnet/tools:/Users/xuxiaocheng/Applications/flutter/bin:/Users/xuxiaocheng/.cargo/bin:/Users/xuxiaocheng/.gem/bin:/Library/Java/JavaVirtualMachines/graalvm-jdk-17.0.12+8.1/Contents/Home/bin:/opt/homebrew/bin/:/opt/homebrew/opt/llvm/bin:/opt/homebrew/bin/python3:/opt/homebrew/opt/python@3.12/libexec/bin:/Users/xuxiaocheng/Library/Android/sdk/ndk/27.1.12297006/toolchains/llvm/prebuilt/darwin-x86_64/bin:/opt/homebrew/opt/node@20/bin" VSLANG="1033" "cc" "-m64" "/Users/xuxiaocheng/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/x86_64-unknown-linux-musl/lib/self-contained/crt1.o" "/Users/xuxiaocheng/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/x86_64-unknown-linux-musl/lib/self-contained/crti.o" "/Users/xuxiaocheng/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/x86_64-unknown-linux-musl/lib/self-contained/crtbegin.o" "/var/folders/_w/y4dz37394kq6ck56xzqcp6d40000gn/T/rustcUMg8zD/symbols.o" "/Users/xuxiaocheng/Rust/tester/target/x86_64-unknown-linux-musl/debug/deps/tester-d799fda059ad16a4.1iaj672wno4p7gtftdjcafzap.rcgu.o" "/Users/xuxiaocheng/Rust/tester/target/x86_64-unknown-linux-musl/debug/deps/tester-d799fda059ad16a4.1zus0a988jgf9u72jhzxk1top.rcgu.o" "/Users/xuxiaocheng/Rust/tester/target/x86_64-unknown-linux-musl/debug/deps/tester-d799fda059ad16a4.3swvung9ke81tlwo859ptp14c.rcgu.o" "/Users/xuxiaocheng/Rust/tester/target/x86_64-unknown-linux-musl/debug/deps/tester-d799fda059ad16a4.81g0etzz8ziqep3bzd7kuucva.rcgu.o" "/Users/xuxiaocheng/Rust/tester/target/x86_64-unknown-linux-musl/debug/deps/tester-d799fda059ad16a4.b2ghjnjoh6puzhgulo3e7o3bw.rcgu.o" "/Users/xuxiaocheng/Rust/tester/target/x86_64-unknown-linux-musl/debug/deps/tester-d799fda059ad16a4.a9zeabbofo5sewom0uxp4009e.rcgu.o" "-Wl,--as-needed" "-Wl,-Bstatic" "/Users/xuxiaocheng/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/x86_64-unknown-linux-musl/lib/libstd-eef8f5f1a6241997.rlib" "/Users/xuxiaocheng/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/x86_64-unknown-linux-musl/lib/libpanic_unwind-fb4457de67986823.rlib" "/Users/xuxiaocheng/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/x86_64-unknown-linux-musl/lib/libobject-f7bbd4335a62ca1b.rlib" "/Users/xuxiaocheng/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/x86_64-unknown-linux-musl/lib/libmemchr-08f38fe1d0dbe825.rlib" "/Users/xuxiaocheng/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/x86_64-unknown-linux-musl/lib/libaddr2line-8e5defbd8afe1bfa.rlib" "/Users/xuxiaocheng/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/x86_64-unknown-linux-musl/lib/libgimli-5a999f56fc2ac3ad.rlib" "/Users/xuxiaocheng/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/x86_64-unknown-linux-musl/lib/librustc_demangle-2bb4f5a5fda29daa.rlib" "/Users/xuxiaocheng/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/x86_64-unknown-linux-musl/lib/libstd_detect-0b5ce9a8a91a8736.rlib" "/Users/xuxiaocheng/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/x86_64-unknown-linux-musl/lib/libhashbrown-edcb15c1b392bf2e.rlib" "/Users/xuxiaocheng/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/x86_64-unknown-linux-musl/lib/librustc_std_workspace_alloc-459028a03e67e8c7.rlib" "/Users/xuxiaocheng/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/x86_64-unknown-linux-musl/lib/libminiz_oxide-b5e48f8f9535b218.rlib" "/Users/xuxiaocheng/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/x86_64-unknown-linux-musl/lib/libadler-9eedd7aa38f2108f.rlib" "/Users/xuxiaocheng/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/x86_64-unknown-linux-musl/lib/libunwind-b938e0bbdb9ad16e.rlib" "-lunwind" "/Users/xuxiaocheng/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/x86_64-unknown-linux-musl/lib/libcfg_if-6886fff82d1fb411.rlib" "/Users/xuxiaocheng/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/x86_64-unknown-linux-musl/lib/liblibc-e0d6a31e6f3d350b.rlib" "-lc" "/Users/xuxiaocheng/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/x86_64-unknown-linux-musl/lib/liballoc-b015fe652967a4a9.rlib" "/Users/xuxiaocheng/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/x86_64-unknown-linux-musl/lib/librustc_std_workspace_core-e8889f443ab9420f.rlib" "/Users/xuxiaocheng/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/x86_64-unknown-linux-musl/lib/libcore-db5962732af76041.rlib" "/Users/xuxiaocheng/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/x86_64-unknown-linux-musl/lib/libcompiler_builtins-a0b3069b5e99ee25.rlib" "-Wl,-Bdynamic" "-Wl,--eh-frame-hdr" "-Wl,-z,noexecstack" "-nostartfiles" "-L" "/Users/xuxiaocheng/Rust/tester/target/x86_64-unknown-linux-musl/debug/build/openssl-sys-46078b20ef0b4c0a/out/openssl-build/install/lib" "-L" "/Users/xuxiaocheng/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/x86_64-unknown-linux-musl/lib/self-contained" "-L" "/Users/xuxiaocheng/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/x86_64-unknown-linux-musl/lib" "-o" "/Users/xuxiaocheng/Rust/tester/target/x86_64-unknown-linux-musl/debug/deps/tester-d799fda059ad16a4" "-Wl,--gc-sections" "-static" "-Wl,-z,relro,-z,now" "-nodefaultlibs" "/Users/xuxiaocheng/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/x86_64-unknown-linux-musl/lib/self-contained/crtend.o" "/Users/xuxiaocheng/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/x86_64-unknown-linux-musl/lib/self-contained/crtn.o"
  = note: ld: unknown option: --as-needed
          clang: error: linker command failed with exit code 1 (use -v to see invocation)

error: could not compile `tester` (bin "tester") due to 1 previous error

I remove the dependency and got the similar output. Maybe cross-compiling from macos to linux is not supported?

bjorn3 commented 3 weeks ago

Try export RUSTFLAGS="-Clinker=/opt/homebrew/Cellar/musl-cross/0.9.9_2/bin/x86_64-linux-musl-gcc" to use the right linker.

xuxiaocheng0201 commented 3 weeks ago

That's great! Thank you very much!!

For future reader: Run brew install filosottile/musl-cross/musl-cross and export these.

export CARGO_TARGET_X86_64_UNKNOWN_LINUX_MUSL_LINKER=/opt/homebrew/Cellar/musl-cross/0.9.9_2/bin/x86_64-linux-musl-gcc
export CC_x86_64_unknown_linux_musl=/opt/homebrew/Cellar/musl-cross/0.9.9_2/bin/x86_64-linux-musl-gcc
export AR_x86_64_unknown_linux_musl=/opt/homebrew/Cellar/musl-cross/0.9.9_2/bin/x86_64-linux-musl-ar

To cross-compile from macos to linux.