Musl-gcc undefined reference to _dl_find_object

I'm having problems creating a rust library that can be called from static musl-gcc. Any help would be appreciated!

Cargo.toml

[package]
name = "c-example"
version = "0.1.0"
edition = "2024"

[dependencies]

[profile.release]
panic = "abort"
lto = true

[lib]
crate-type = ["staticlib"]

src/lib.rs

#[repr(C)]
pub struct Example {
    pub id: u32,
    pub value: u64,
}

#[unsafe(no_mangle)]
pub extern "C" fn example_new(id: u32, value: u64) -> *mut Example {
    let obj = Example { id, value };
    Box::into_raw(Box::new(obj))
}

test.c

#include <stdio.h>
#include <stdlib.h>

extern void *example_new(unsigned int id, unsigned long value);

int main() {
  void *obj = example_new(42, 123456789);
  if (!obj) {
    printf("Failed to create struct\n");
    return 1;
  }
}

.cargo/config.toml

cat .cargo/config.toml
[build]
rustflags = ["-C", "target-feature=+crt-static"]
target = "x86_64-unknown-linux-musl"
$ cargo build --release --target x86_64-unknown-linux-musl
$ musl-gcc -static -o test test.c target/x86_64-unknown-linux-musl/release/c_example.a
/usr/bin/ld: /usr/lib/gcc/x86_64-pc-linux-gnu/14.2.1/libgcc_eh.a(unwind-dw2-fde-dip.o): in function `_Unwind_Find_FDE':
(.text+0x2941): undefined reference to `_dl_find_object'
collect2: error: ld returned 1 exit status

For information:

$ rustc -V
rustc 1.87.0-nightly (e16a049ad 2025-03-03)
$ musl-gcc -v
Using built-in specs.
Reading specs from /usr/lib/musl/lib/musl-gcc.specs
rename spec cpp_options to old_cpp_options
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-pc-linux-gnu/14.2.1/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: /build/gcc/src/gcc/configure --enable-languages=ada,c,c++,d,fortran,go,lto,m2,objc,obj-c++,rust --enable-bootstrap --prefix=/usr --libdir=/usr/lib --libexecdir=/usr/lib --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=https://gitlab.archlinux.org/archlinux/packaging/packages/gcc/-/issues --with-build-config=bootstrap-lto --with-linker-hash-style=gnu --with-system-zlib --enable-__cxa_atexit --enable-cet=auto --enable-checking=release --enable-clocale=gnu --enable-default-pie --enable-default-ssp --enable-gnu-indirect-function --enable-gnu-unique-object --enable-libstdcxx-backtrace --enable-link-serialization=1 --enable-linker-build-id --enable-lto --enable-multilib --enable-plugin --enable-shared --enable-threads=posix --disable-libssp --disable-libstdcxx-pch --disable-werror
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 14.2.1 20250207 (GCC)

I think that's a glibc symbol, I'm not sure if musl supports it. looking at the error message, it seems to be referenced from libunwind, which wanted to link against libgcc_eh.a.

I'm not sure the libunwind is from the rust side or the gcc side. but I highly suspect it's from the musl-gcc.

first, to check if the rust cross toolchain is working, try to build a hello world, e.g.:

$ cargo new helloworld && cd helloworld
$ RUSTFLAGS='-C linker=musl-gcc' cargo build --target x86_64-unknown-linux-musl

then check if musl-gcc is working properly, e.g. by building a simple hello world:

// helloworld.c
#include <stdio.h>
int main(int argc, char **argv) {
    printf("hello world!\n");
}
$ musl-gcc helloworld.c

in any case, I suggest you build the program in a native musl system, such as "alpine", instead of cross compiling from a gnu system, such as "ubuntu".

containers make it really to run a different linux distro from the host system. if you are not familiar with docker or podman, you can checkout distrobox.

It is quite possible that you don't have a musl version of libgcc_eh.a installed. Rustc ships the musl target with a copy of libunwind.a compiled for musl which is a drop-in replacement. You might want to try linking against it instead.

2 Likes

Thanks both of you! I used a compiler made for cross compilation from GitHub - cross-rs/cross: “Zero setup” cross compilation and “cross testing” of Rust crates for my C compiler and it just worked.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.