Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

+whole-archive link modifier not behaving as expected on macOS #110624

Open
ArthurKValladares opened this issue Apr 20, 2023 · 12 comments
Open

+whole-archive link modifier not behaving as expected on macOS #110624

ArthurKValladares opened this issue Apr 20, 2023 · 12 comments
Labels
A-linkage Area: linking into static, shared libraries and binaries C-bug Category: This is a bug. O-macos Operating system: macOS

Comments

@ArthurKValladares
Copy link

ArthurKValladares commented Apr 20, 2023

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:


@ArthurKValladares ArthurKValladares added the C-bug Category: This is a bug. label Apr 20, 2023
@Jules-Bertholet
Copy link
Contributor

@rustbot label +A-linkage

@rustbot rustbot added the A-linkage Area: linking into static, shared libraries and binaries label Apr 26, 2023
@jyn514 jyn514 added the O-macos Operating system: macOS label Apr 26, 2023
@m00nwtchr
Copy link

m00nwtchr commented May 18, 2023

I think I'm seeing the same thing on Linux.

rustc 1.69.0 (84c898d65 2023-04-16)
binary: rustc
commit-hash: 84c898d65adf2f39a5a98507f1fe0ce10a2b8dbc
commit-date: 2023-04-16
host: x86_64-unknown-linux-gnu
release: 1.69.0
LLVM version: 15.0.7

Strangely enough, I can get the symbol to show up if I add a extern "C" definition with #[no_mangle]

#[link(name = "p", kind = "static", modifiers = "+whole-archive")]
extern "C" {
	#[no_mangle]
	pub fn add(a: u32, b: u32) -> u32;
}

@petrochenkov
Copy link
Contributor

I didn't check what happens in the examples in detail, but it's possible that linker throws away individual symbols due to --gc-sections, even if their object files are not thrown away due to +whole-archive.

Some attributes like #[no_mangle] can mark the symbol as exported, so it won't be garbage collected in that case.

@ArthurKValladares
Copy link
Author

The extern "C" + #[no-mangle] combination @m00nwtchr described above does work for me as well. We do get a warning that this will become an error in the future:

warning: `#[no_mangle]` has no effect on a foreign function
 --> src/lib.rs:3:5
  |
3 |     #[no_mangle]
  |     ^^^^^^^^^^^^ help: remove this attribute
4 |     pub fn linking_c_static();
  |     -------------------------- foreign function
  |
  = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
  = note: symbol names in extern blocks are not mangled
  = note: `#[warn(unused_attributes)]` on by default

@aidanhs
Copy link
Member

aidanhs commented Nov 11, 2023

I think I'm seeing this on linux - it appears to be because the linker version script looks like:

{
  global:
    cxxbridge1$Recorder$init;
[...]

  local:
    *;
};

If I save my linker invocation and temporaries with cargo rustc --release -p rec-cxx -- -C save-temps --print link-args >out, edit the version script to remove the local section and then run sh out then I get the symbols included.

@bjorn3
Copy link
Member

bjorn3 commented Nov 11, 2023

Are you using #[link(name = "...", kind = "static")] on the extern block that imports all functions you need? This is necessary for rustc to re-export them from a dylib.

@aidanhs
Copy link
Member

aidanhs commented Nov 11, 2023

I think what's described in this issue is that we want +whole-archive to pull an entire archive into a cdylib to re-export it without explicitly listing all the symbols from said archive (e.g. because the list changes often).

i.e.

  • libext.a exports foo()
  • crate
    • exports bar()
    • links to libext.a with +whole-archive without listing any symbols
  • libcrate.so - exports foo and bar

In the OP that's being done via a build script whereas I have the annotations on an empty extern block, but we're trying to do the same thing.

#[link(name="recorder-cxx-bridge", kind="static", modifiers = "+whole-archive")]
extern "C" {}

If rustc won't support this, I think I have two options:

  1. build a rust staticlib that I combine with my external staticlib into a cdylib by invoking the linker myself. That includes tons of symbols because of Don't leak non-exported symbols from staticlibs #104707, so I get rustc to emit the list of exported symbols (by parsing a version script saved with save-temps?), then use that list to generate my own version script which includes symbols from the ext library

  2. use a build script to pull symbols from the external library and list them in the extern block at compile time

@aidanhs
Copy link
Member

aidanhs commented Nov 11, 2023

Ah but of course, with option 2 I'm hitting the warning in #110624 (comment) because I have to put no_mangle on the extern symbols to export them and rust wants to prevent that at some point. I guess it'll do for now.

Demo repo https://github.com/aidanhs/rust-re-export-lib/

Semi related #73958 which talks about no_mangle visibility.

@bjorn3
Copy link
Member

bjorn3 commented Nov 11, 2023

#[no_mangle] inside an extern block shouldn't have any effect AFAIK. The kind = "static" inside #[link] should be enough for exporting all symbols defined in that block.

@aidanhs
Copy link
Member

aidanhs commented Nov 11, 2023

Unfortunately the repo above seems to demonstrate otherwise? At least on Linux (which matches up with what the other people in this issue have found).

@apiraino
Copy link
Contributor

The --whole-archive hack was removed in Rust 1.82 (see changelog). Makes sense to close this issue or do we prefer it open to track the progress of RFC#3556?

@aidanhs
Copy link
Member

aidanhs commented Nov 9, 2024

The whole-archive mention in that changelog seems to specifically be referring to something that was happening with tests - I don't think that's related to this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in 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
Projects
None yet
Development

No branches or pull requests

9 participants