libbpf / libbpf-sys

Rust bindings to libbpf from the Linux kernel
https://crates.io/crates/libbpf-sys/
BSD 2-Clause "Simplified" License
199 stars 48 forks source link

libbpf-sys not static linking vendored libbpf if libbpf.pc was present in system #112

Closed EHfive closed 2 days ago

EHfive commented 2 days ago

Environment

[dependencies]
# implies libbpf-sys/vendored-libbpf and libbpf-sys/static-libbpf
libbpf-rs = "0.24.8"
libbpf-sys = "1.5.0"

Unexpected

libbpf.so is dynamically linked dispute (implicitly) specifying "static-libbpf".

ldd target/debug/binary
        linux-vdso.so.1 (0x00007f916424b000)
        libbpf.so.1 => /usr/lib/libbpf.so.1 (0x00007f91641a0000)
        libgcc_s.so.1 => /usr/lib/libgcc_s.so.1 (0x00007f91637d2000)
        libc.so.6 => /usr/lib/libc.so.6 (0x00007f91635e1000)
        /lib64/ld-linux-x86-64.so.2 => /usr/lib64/ld-linux-x86-64.so.2 (0x00007f916424d000)
        libelf.so.1 => /usr/lib/libelf.so.1 (0x00007f91635c5000)
        libz.so.1 => /usr/lib/libz.so.1 (0x00007f9164187000)
        libzstd.so.1 => /usr/lib/libzstd.so.1 (0x00007f91634e4000)

Observed #987 when system libbpf version does not match libbpf-sys'.

Workaround

Delete system libbpf or /usr/lib/pkgconfig/libbpf.pc can make libbpf-sys static link to vendored libbpf again.

ldd target/debug/binary
        linux-vdso.so.1 (0x00007ecc9b5e5000)
        libelf.so.1 => /usr/lib/libelf.so.1 (0x00007ecc9aba1000)
        libz.so.1 => /usr/lib/libz.so.1 (0x00007ecc9ab88000)
        libgcc_s.so.1 => /usr/lib/libgcc_s.so.1 (0x00007ecc9ab5a000)
        libc.so.6 => /usr/lib/libc.so.6 (0x00007ecc9a969000)
        /lib64/ld-linux-x86-64.so.2 => /usr/lib64/ld-linux-x86-64.so.2 (0x00007ecc9b5e7000)
        libzstd.so.1 => /usr/lib/libzstd.so.1 (0x00007ecc9a88a000)
EHfive commented 2 days ago

OK, this is invalid.

I was also using pkg-config crate to location libbpf headers in build script, however it also implicitly produces cargo:rustc-link-search=native=<libbpf> etc. causing system libbpf to be linked.

edit: For future reference, specifying cargo_metadata=false with pkg_config::Config builder can prevent it from emitting those linking instructions.

pkg_config::Config::new()
        .cargo_metadata(false)
        .probe("libbpf")
danielocfb commented 1 day ago

Thanks for getting to the bottom of it