-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Incompatibility between musl and crates linking to shared libraries #82193
Comments
I will try to build the musl target with |
This stackoverflow question has good information on the subject of static pie and relocation. |
Although this is not solved, I solved it for my case. In config.toml, I put this rustc flag: [build]
rustflags = [ "-C", "target-feature=-crt-static" ] It looks like this overrides the I don't think I should close the issue, as I don't fully understand how the problem was solved. I welcome any explanation. |
Were you building the executable on musl based system (like Alpine or musl variant of Void Linux)? |
I was trying to build my crate on two different systems. Initially both would segfault. The mess between musl-libc and glibc on the arch system must have been the reason for all the subsequent problems. |
When cross compiling musl target it defaults to static linking yet
You were trying to mix glibc and musl libraries which is not supposed to work. |
Thanks for the explanation.
I guess that's the real bug then. The crate which links to libgbm is, to be precise, the gbm-sys subcrate. It uses bindgen and its code is there. The link attribute is located here. |
So it's |
I don't understand, the link attribute should select the appropriate link type if the |
AFAIK it cannot do it because of backwards compatibility. If you look at this text:
Empty |
Oh, I see.
Should I leave the issue open for this part ? |
I'll leave it to somebody more experienced like @petrochenkov but IMO this would be hard to take any action here because some libc implementations allow to mix static libc with shared libraries. |
The problem also touches cc crate based sys libraries which requires a c++ runtime to work. Without the workarround provided in the begining of this issue c++/c/rust binaries are generated with a broken path to the dynamic linker (ld-linux instead of ld-musl). |
This solved the problem for me too! |
I don't think this is the default when not cross-compiling, even though it should be. The |
@jbg I haven't been using musl at all since that comment so I have no idea on how things look right now.
I don't even know if this has never been the case or something has changed. |
I'm trying to rewrite kmscube in rust, targetting an environment with musl as libc implementation.
kmscube's goal is to demonstrate the use of opengl without any compositor. It relies on mesa3d's libgbm, which is present as a shared library in my musl environment, at
/usr/lib/libgbm.so
. There is a rust crate wrapping libgbm : gbm.rs. I'm using the crate as follows:Using the gnu (non-musl) target, this happens:
Using the musl target, this happens:
And this SIGSEGV is my problem.
Analysis
readelf
reveals the dynamic section of each file:GNU
MUSL
As well as the program headers:
GNU
MUSL
Using
strace
, I was able to see that the musl binary never tries to reach for libgbm.The function call at line 15 results in the use of the symbol
gbm_create_device
, which was never resolved and points to NULL.→ segmentation fault.
Interpretation
Upon inspection of the musl target specification in rust-lang/rust, I understand that the musl target systematically produces static binaries. Further in the targets code, the
static_position_independent_executables
target option is converted to a command line argument for gcc:-static
.From the GCC manual:
I think GCC achieves this by simply removing the code relocating imported symbols, leaving them unresolved. If we try to use them, we get a segfault.
Does this mean that the musl target is incompatible with any crate that links with shared library at runtime ?
My motivation for using musl is not getting fully static binaries, I just like that it's lightweight and simple. If this incompatibility exists, I'd be happier if I could disable the
static_position_independent_executables
as a feature, so I could use all crates with musl.I'm reporting this as a bug because rustc does not warn the developer that using crates linking to a shlib will lead to segfaults.
Meta
rustc --version --verbose
:The text was updated successfully, but these errors were encountered: