Open
Description
I'm trying to link a C static lib to a rust cdylib using the +whole-archive
link modifier in order to expose the library symbols from the generated dylib, but they do not show up in the final library. A minimal example is as follows:
static.c
#include <stdio.h>
void linking_c_static()
{
printf("foo");
}
compiled using:
gcc -c -fPIC static.c -o static.o
ar -rv libstatic.a static.o
lib.rs
#[no_mangle]
pub fn rust_linking() {
println!("hello!");
}
build.rs
fn main() {
println!("cargo:rustc-link-lib=static:+whole-archive=static");
}
Cargo..toml
[package]
name = "rust"
version = "0.1.0"
edition = "2021"
[lib]
crate-type = ["cdylib"]
I expected to see this happen:
I expected to be able to see the static lib symbols in the dylib
, as I can when linking the same lib to a C dylib generated by gcc:
dynamic.c
#include <stdio.h>
void linking_c_dynamic()
{
printf("bar");
}
compiled using:
gcc -c -fPIC dynamic.c -o dynamic.o
gcc -dynamiclib -o libdynamic.dylib dynamic.o -Wl,-force_load libstatic.a
Symbols listed nm -g libdynamic.dylib
:
0000000000003f70 T _linking_c_dynamic
0000000000003f90 T _linking_c_static
U _printf
Instead, this happened:
Instead, I cannot find the static lib symbols in the generated dylib nm -g target/debug/librust.dylib
:
U __Unwind_Backtrace
U __Unwind_GetDataRelBase
U __Unwind_GetIP
U __Unwind_GetIPInfo
U __Unwind_GetLanguageSpecificData
U __Unwind_GetRegionStart
U __Unwind_GetTextRelBase
U __Unwind_RaiseException
U __Unwind_Resume
U __Unwind_SetGR
U __Unwind_SetIP
U ___bzero
U ___error
U __dyld_get_image_header
U __dyld_get_image_name
U __dyld_get_image_vmaddr_slide
U __dyld_image_count
U __tlv_atexit
U __tlv_bootstrap
U _abort
U _close$NOCANCEL
U _closedir
U _dispatch_release
U _dispatch_semaphore_create
U _dispatch_semaphore_signal
U _dispatch_semaphore_wait
U _free
U _fstat$INODE64
U _getcwd
U _getenv
U _malloc
U _memcmp
U _memcpy
U _memmove
U _mmap
U _munmap
U _open
U _opendir$INODE64
U _posix_memalign
U _pthread_mutex_destroy
U _pthread_mutex_init
U _pthread_mutex_lock
U _pthread_mutex_trylock
U _pthread_mutex_unlock
U _pthread_mutexattr_destroy
U _pthread_mutexattr_init
U _pthread_mutexattr_settype
U _pthread_rwlock_destroy
U _pthread_rwlock_rdlock
U _pthread_rwlock_unlock
U _readdir_r$INODE64
U _realloc
0000000000003c20 T _rust_linking
U _strerror_r
U _strlen
U _write
U _writev
Meta
rustc --version --verbose
:
rustc 1.68.2 (9eb3afe9e 2023-03-27)
binary: rustc
commit-hash: 9eb3afe9ebe9c7d2b84b71002d44f4a0edac95e0
commit-date: 2023-03-27
host: x86_64-apple-darwin
release: 1.68.2
LLVM version: 15.0.6
Backtrace
<backtrace>
Linker info:
arthurvalladares@Arthurs-MacBook rust % export RUSTC_LOG=rustc_codegen_ssa::back::link=info
arthurvalladares@Arthurs-MacBook rust % cargo build -v
Compiling rust v0.1.0 (/Users/arthurvalladares/Documents/Brainium/rust-linking-test/rust)
Running `rustc --crate-name build_script_build --edition=2021 build.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts,future-incompat --diagnostic-width=80 --crate-type bin --emit=dep-info,link -C embed-bitcode=no -C split-debuginfo=unpacked -C debuginfo=2 -C metadata=9fc2614f827c81ce -C extra-filename=-9fc2614f827c81ce --out-dir /Users/arthurvalladares/Documents/Brainium/rust-linking-test/rust/target/debug/build/rust-9fc2614f827c81ce -C incremental=/Users/arthurvalladares/Documents/Brainium/rust-linking-test/rust/target/debug/incremental -L dependency=/Users/arthurvalladares/Documents/Brainium/rust-linking-test/rust/target/debug/deps -C link-arg=-undefined -C link-arg=dynamic_lookup`
INFO rustc_codegen_ssa::back::link preparing Executable to "/Users/arthurvalladares/Documents/Brainium/rust-linking-test/rust/target/debug/build/rust-9fc2614f827c81ce/build_script_build-9fc2614f827c81ce"
INFO rustc_codegen_ssa::back::link LC_ALL="C" PATH="/Users/arthurvalladares/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/bin:/Users/arthurvalladares/.rvm/gems/ruby-2.7.2/bin:/Users/arthurvalladares/.rvm/gems/ruby-2.7.2@global/bin:/Users/arthurvalladares/.rvm/rubies/ruby-2.7.2/bin:/usr/local/opt/[email protected]/bin:/Users/arthurvalladares/Library/Android/sdk/platform-tools:/Users/arthurvalladares/Library/Android/sdk/tools:/Users/arthurvalladares/Documents/spirv-tools/bin:/Users/arthurvalladares/.pyenv/shims:/Library/Frameworks/Python.framework/Versions/3.8/bin:/Library/Frameworks/Python.framework/Versions/2.7/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/Apple/usr/bin:/Users/arthurvalladares/.cargo/bin:/Users/arthurvalladares/.rvm/bin" VSLANG="1033" ZERO_AR_DATE="1" "cc" "-arch" "x86_64" "-m64" "/var/folders/g4/8gyynk8n1hl9frp6w3v22z4c0000gn/T/rustcYiGEDC/symbols.o" "/Users/arthurvalladares/Documents/Brainium/rust-linking-test/rust/target/debug/build/rust-9fc2614f827c81ce/build_script_build-9fc2614f827c81ce.148w6spp8bbexu3h.rcgu.o" "/Users/arthurvalladares/Documents/Brainium/rust-linking-test/rust/target/debug/build/rust-9fc2614f827c81ce/build_script_build-9fc2614f827c81ce.18cdj71jewiji4xx.rcgu.o" "/Users/arthurvalladares/Documents/Brainium/rust-linking-test/rust/target/debug/build/rust-9fc2614f827c81ce/build_script_build-9fc2614f827c81ce.1k618e00qfe1nsxt.rcgu.o" "/Users/arthurvalladares/Documents/Brainium/rust-linking-test/rust/target/debug/build/rust-9fc2614f827c81ce/build_script_build-9fc2614f827c81ce.1n7ouiwlqty0qm0u.rcgu.o" "/Users/arthurvalladares/Documents/Brainium/rust-linking-test/rust/target/debug/build/rust-9fc2614f827c81ce/build_script_build-9fc2614f827c81ce.1t0yal8w4nqc9bey.rcgu.o" "/Users/arthurvalladares/Documents/Brainium/rust-linking-test/rust/target/debug/build/rust-9fc2614f827c81ce/build_script_build-9fc2614f827c81ce.1tba9wer1o6narf6.rcgu.o" "/Users/arthurvalladares/Documents/Brainium/rust-linking-test/rust/target/debug/build/rust-9fc2614f827c81ce/build_script_build-9fc2614f827c81ce.1xo7ohzz3fon8sgo.rcgu.o" "/Users/arthurvalladares/Documents/Brainium/rust-linking-test/rust/target/debug/build/rust-9fc2614f827c81ce/build_script_build-9fc2614f827c81ce.2dswrc8j2ez1uwpm.rcgu.o" "/Users/arthurvalladares/Documents/Brainium/rust-linking-test/rust/target/debug/build/rust-9fc2614f827c81ce/build_script_build-9fc2614f827c81ce.38vzlr9legqx14jg.rcgu.o" "/Users/arthurvalladares/Documents/Brainium/rust-linking-test/rust/target/debug/build/rust-9fc2614f827c81ce/build_script_build-9fc2614f827c81ce.3fn06lljzrbqhvqf.rcgu.o" "/Users/arthurvalladares/Documents/Brainium/rust-linking-test/rust/target/debug/build/rust-9fc2614f827c81ce/build_script_build-9fc2614f827c81ce.3k3szf92za1ihy5q.rcgu.o" "/Users/arthurvalladares/Documents/Brainium/rust-linking-test/rust/target/debug/build/rust-9fc2614f827c81ce/build_script_build-9fc2614f827c81ce.3m6csywzzqs15f0k.rcgu.o" "/Users/arthurvalladares/Documents/Brainium/rust-linking-test/rust/target/debug/build/rust-9fc2614f827c81ce/build_script_build-9fc2614f827c81ce.3rragy3p0g864p55.rcgu.o" "/Users/arthurvalladares/Documents/Brainium/rust-linking-test/rust/target/debug/build/rust-9fc2614f827c81ce/build_script_build-9fc2614f827c81ce.4lse6n2sxzesbdtj.rcgu.o" "/Users/arthurvalladares/Documents/Brainium/rust-linking-test/rust/target/debug/build/rust-9fc2614f827c81ce/build_script_build-9fc2614f827c81ce.4zruildvtdm7mk72.rcgu.o" "/Users/arthurvalladares/Documents/Brainium/rust-linking-test/rust/target/debug/build/rust-9fc2614f827c81ce/build_script_build-9fc2614f827c81ce.91fzws68jx5u5sq.rcgu.o" "/Users/arthurvalladares/Documents/Brainium/rust-linking-test/rust/target/debug/build/rust-9fc2614f827c81ce/build_script_build-9fc2614f827c81ce.b8e33l75m8j507m.rcgu.o" "/Users/arthurvalladares/Documents/Brainium/rust-linking-test/rust/target/debug/build/rust-9fc2614f827c81ce/build_script_build-9fc2614f827c81ce.ik1wraweljwlofa.rcgu.o" "/Users/arthurvalladares/Documents/Brainium/rust-linking-test/rust/target/debug/build/rust-9fc2614f827c81ce/build_script_build-9fc2614f827c81ce.nehcvldcc05ibxa.rcgu.o" "/Users/arthurvalladares/Documents/Brainium/rust-linking-test/rust/target/debug/build/rust-9fc2614f827c81ce/build_script_build-9fc2614f827c81ce.vay3url2ofecrpo.rcgu.o" "/Users/arthurvalladares/Documents/Brainium/rust-linking-test/rust/target/debug/build/rust-9fc2614f827c81ce/build_script_build-9fc2614f827c81ce.xcasarpz3rj3qfb.rcgu.o" "/Users/arthurvalladares/Documents/Brainium/rust-linking-test/rust/target/debug/build/rust-9fc2614f827c81ce/build_script_build-9fc2614f827c81ce.58a7w3ajspfsi75.rcgu.o" "-L" "/Users/arthurvalladares/Documents/Brainium/rust-linking-test/rust/target/debug/deps" "-L" "/Users/arthurvalladares/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib" "/Users/arthurvalladares/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libstd-8fa007cdc882403b.rlib" "/Users/arthurvalladares/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libpanic_unwind-8294ffb4a69497ab.rlib" "/Users/arthurvalladares/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libobject-472e943b681632d8.rlib" "/Users/arthurvalladares/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libmemchr-7d8d567e92ad1dd8.rlib" "/Users/arthurvalladares/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libaddr2line-ed004df1bb903adb.rlib" "/Users/arthurvalladares/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libgimli-af580b13d5715ba8.rlib" "/Users/arthurvalladares/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/librustc_demangle-20fb3dd872494ad9.rlib" "/Users/arthurvalladares/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libstd_detect-50a89c754eac916d.rlib" "/Users/arthurvalladares/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libhashbrown-9ac74abb1b852e5d.rlib" "/Users/arthurvalladares/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libminiz_oxide-68f98cc52d315b53.rlib" "/Users/arthurvalladares/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libadler-35546c54bc82a9ad.rlib" "/Users/arthurvalladares/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/librustc_std_workspace_alloc-bd9ee649e3151af7.rlib" "/Users/arthurvalladares/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libunwind-235725c3b2d78362.rlib" "/Users/arthurvalladares/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libcfg_if-c569d380514bde32.rlib" "/Users/arthurvalladares/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/liblibc-20fad06cc491dc7d.rlib" "/Users/arthurvalladares/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/liballoc-d6ca0a93cb563c8b.rlib" "/Users/arthurvalladares/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/librustc_std_workspace_core-6f5d01ccd9a8d6da.rlib" "/Users/arthurvalladares/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libcore-1eef3d4aa79d6846.rlib" "/Users/arthurvalladares/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libcompiler_builtins-81f9fecd362517eb.rlib" "-lSystem" "-lc" "-lm" "-L" "/Users/arthurvalladares/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib" "-o" "/Users/arthurvalladares/Documents/Brainium/rust-linking-test/rust/target/debug/build/rust-9fc2614f827c81ce/build_script_build-9fc2614f827c81ce" "-Wl,-dead_strip" "-nodefaultlibs" "-undefined" "dynamic_lookup"
INFO rustc_codegen_ssa::back::link linker stderr:
INFO rustc_codegen_ssa::back::link linker stdout:
Running `/Users/arthurvalladares/Documents/Brainium/rust-linking-test/rust/target/debug/build/rust-9fc2614f827c81ce/build-script-build`
Running `rustc --crate-name rust --edition=2021 src/lib.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts,future-incompat --diagnostic-width=80 --crate-type cdylib --emit=dep-info,link -C embed-bitcode=no -C split-debuginfo=unpacked -C debuginfo=2 -C metadata=aadcbc7450cb9cc6 --out-dir /Users/arthurvalladares/Documents/Brainium/rust-linking-test/rust/target/debug/deps -C incremental=/Users/arthurvalladares/Documents/Brainium/rust-linking-test/rust/target/debug/incremental -L dependency=/Users/arthurvalladares/Documents/Brainium/rust-linking-test/rust/target/debug/deps -C link-arg=-undefined -C link-arg=dynamic_lookup -L native=/Users/arthurvalladares/Documents/Brainium/rust-linking-test/rust/../c -l 'static:+whole-archive=static'`
INFO rustc_codegen_ssa::back::link preparing Cdylib to "/Users/arthurvalladares/Documents/Brainium/rust-linking-test/rust/target/debug/deps/librust.dylib"
INFO rustc_codegen_ssa::back::link LC_ALL="C" PATH="/Users/arthurvalladares/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/bin:/Users/arthurvalladares/.rvm/gems/ruby-2.7.2/bin:/Users/arthurvalladares/.rvm/gems/ruby-2.7.2@global/bin:/Users/arthurvalladares/.rvm/rubies/ruby-2.7.2/bin:/usr/local/opt/[email protected]/bin:/Users/arthurvalladares/Library/Android/sdk/platform-tools:/Users/arthurvalladares/Library/Android/sdk/tools:/Users/arthurvalladares/Documents/spirv-tools/bin:/Users/arthurvalladares/.pyenv/shims:/Library/Frameworks/Python.framework/Versions/3.8/bin:/Library/Frameworks/Python.framework/Versions/2.7/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/Apple/usr/bin:/Users/arthurvalladares/.cargo/bin:/Users/arthurvalladares/.rvm/bin" VSLANG="1033" ZERO_AR_DATE="1" "cc" "-Wl,-exported_symbols_list,/var/folders/g4/8gyynk8n1hl9frp6w3v22z4c0000gn/T/rustcDEuymC/list" "-arch" "x86_64" "-m64" "/var/folders/g4/8gyynk8n1hl9frp6w3v22z4c0000gn/T/rustcDEuymC/symbols.o" "/Users/arthurvalladares/Documents/Brainium/rust-linking-test/rust/target/debug/deps/rust.2iirdhpfrzg7c1ux.rcgu.o" "/Users/arthurvalladares/Documents/Brainium/rust-linking-test/rust/target/debug/deps/rust.3t24jnsy45ne6yne.rcgu.o" "/Users/arthurvalladares/Documents/Brainium/rust-linking-test/rust/target/debug/deps/rust.27m1axkv3p075yww.rcgu.o" "-L" "/Users/arthurvalladares/Documents/Brainium/rust-linking-test/rust/target/debug/deps" "-L" "/Users/arthurvalladares/Documents/Brainium/rust-linking-test/rust/../c" "-L" "/Users/arthurvalladares/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib" "-Wl,-force_load" "-Wl,/Users/arthurvalladares/Documents/Brainium/rust-linking-test/rust/../c/libstatic.a" "/Users/arthurvalladares/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libstd-8fa007cdc882403b.rlib" "/Users/arthurvalladares/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libpanic_unwind-8294ffb4a69497ab.rlib" "/Users/arthurvalladares/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libobject-472e943b681632d8.rlib" "/Users/arthurvalladares/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libmemchr-7d8d567e92ad1dd8.rlib" "/Users/arthurvalladares/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libaddr2line-ed004df1bb903adb.rlib" "/Users/arthurvalladares/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libgimli-af580b13d5715ba8.rlib" "/Users/arthurvalladares/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/librustc_demangle-20fb3dd872494ad9.rlib" "/Users/arthurvalladares/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libstd_detect-50a89c754eac916d.rlib" "/Users/arthurvalladares/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libhashbrown-9ac74abb1b852e5d.rlib" "/Users/arthurvalladares/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libminiz_oxide-68f98cc52d315b53.rlib" "/Users/arthurvalladares/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libadler-35546c54bc82a9ad.rlib" "/Users/arthurvalladares/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/librustc_std_workspace_alloc-bd9ee649e3151af7.rlib" "/Users/arthurvalladares/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libunwind-235725c3b2d78362.rlib" "/Users/arthurvalladares/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libcfg_if-c569d380514bde32.rlib" "/Users/arthurvalladares/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/liblibc-20fad06cc491dc7d.rlib" "/Users/arthurvalladares/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/liballoc-d6ca0a93cb563c8b.rlib" "/Users/arthurvalladares/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/librustc_std_workspace_core-6f5d01ccd9a8d6da.rlib" "/Users/arthurvalladares/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libcore-1eef3d4aa79d6846.rlib" "/Users/arthurvalladares/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libcompiler_builtins-81f9fecd362517eb.rlib" "-lSystem" "-lc" "-lm" "-L" "/Users/arthurvalladares/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib" "-o" "/Users/arthurvalladares/Documents/Brainium/rust-linking-test/rust/target/debug/deps/librust.dylib" "-Wl,-dead_strip" "-dynamiclib" "-Wl,-dylib" "-nodefaultlibs" "-undefined" "dynamic_lookup"
INFO rustc_codegen_ssa::back::link linker stderr:
ld: warning: -undefined dynamic_lookup may not work with chained fixups
INFO rustc_codegen_ssa::back::link linker stdout: