Skip to content

Commit fdf9f62

Browse files
authored
Don't link with libdl on musl (#195)
On musl dlopen is part of libc and there's no separate libdl. Signed-off-by: Marcel Guzik <marcel.guzik@inetum.com>
1 parent 6b0b32c commit fdf9f62

3 files changed

Lines changed: 23 additions & 1 deletion

File tree

‎.github/workflows/libloading.yml‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,16 @@ jobs:
4848
- name: Compile without the standard library
4949
run: cargo clean && cargo +nightly build -Zbuild-std=core,alloc --no-default-features
5050

51+
musl-rust-lld-link-test:
52+
runs-on: ubuntu-latest
53+
# This test checks if binaries using libloading can be linked using rust-lld linker for target environments that
54+
# provide a unified libc (e.g. musl) without needing to provide additional library paths to the linker.
55+
steps:
56+
- uses: actions/checkout@v4
57+
- run: rustup install stable --profile=minimal
58+
- run: rustup target add x86_64-unknown-linux-musl
59+
- run: RUSTFLAGS="-Clinker=rust-lld" cargo build --target=x86_64-unknown-linux-musl --example e1
60+
5161
windows-test:
5262
runs-on: windows-latest
5363
strategy:

‎examples/e1.rs‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
fn main() {
2+
unsafe {
3+
libloading::Library::new("libc.so").unwrap();
4+
}
5+
}

‎src/os/unix/mod.rs‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,14 @@ impl<T> fmt::Debug for Symbol<T> {
479479
}
480480

481481
// Platform specific things
482-
#[cfg_attr(any(target_os = "linux", target_os = "android"), link(name = "dl"))]
482+
#[cfg_attr(
483+
any(
484+
all(target_os = "linux", not(target_env = "musl")),
485+
target_os = "android"
486+
),
487+
link(name = "dl")
488+
)]
489+
#[cfg_attr(all(target_os = "linux", target_env = "musl"), link(name = "c"))]
483490
#[cfg_attr(any(target_os = "freebsd", target_os = "dragonfly"), link(name = "c"))]
484491
extern "C" {
485492
fn dlopen(

0 commit comments

Comments
 (0)