redbpf icon indicating copy to clipboard operation
redbpf copied to clipboard

Linking failed with cdylib crate

Open sebastiaoamaro opened this issue 3 years ago • 3 comments

Hello everyone, I am trying to create a cdylib crate to be used as a python library using (pyo3) that has a function that reads from a pinned map.

However, I cannot build giving this error:

note: /"path"/target/release/deps/libbpf_sys-5ebb6e925d9bf342.rlib(libbpf.o): warning: relocation against stderr@@GLIBC_2.2.5' in read-only section .text' /"path"/target/release/deps/libbpf_sys-5ebb6e925d9bf342.rlib(libbpf.o): relocation R_X86_64_PC32 against symbol `stderr@@GLIBC_2.2.5' can not be used when making a shared object; recompile with -fPIC /usr/bin/ld: final link failed: bad value collect2: error: ld returned 1 exit status

So I created a .cargo folder with a config file in it with the following line:

rustflags = ["-C", "link-args=-fPIC"]

This seems to add the flag:

"/home/sebas/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib" "-o" "/home/sebas/tese/kollaps-private/communication/target/release/deps/libcommunication.so" "-Wl,--gc-sections" "-shared" "-Wl,-zrelro" "-Wl,-znow" "-Wl,-O1" "-nodefaultlibs" "-fPIC"

But still, the same error appears does anyone know why this might be happening and how to solve it?

Thanks in advance.

sebastiaoamaro avatar Sep 24 '21 12:09 sebastiaoamaro

@sebastiaoamaro Is there a way you could publish a minimum reproducible example for this? Also, please take a look if the main branch solves your issue, as #183 could be a show stopper here.

rsdy avatar Sep 24 '21 14:09 rsdy

Hello, I solved the problem by adding the -fPIC flag to this file https://github.com/foniod/redbpf/blob/main/bpf-sys/build.rs (line 51) and using the dependency locally.

sebastiaoamaro avatar Sep 25 '21 14:09 sebastiaoamaro

Wow @sebastiaoamaro You made it! I guess that you are make something great with cutting edge technologies. I have never used pyo3 to create a python library written in rust. Maybe very few people can achieve what you are trying to create because BPF & rust & python & pyo3 is so rare combination.

And if you have enough time and if you don't mind, please send a PR for this. Perhaps some people in the future would be in trouble with the problem you just fixed.

rhdxmr avatar Sep 25 '21 14:09 rhdxmr