fix: prevent dynamic linking when targeting musl #109
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
When targeting
x86_64-unknown-linux-musl
, the default features will successfully compile a binary that always segfaults when run (see #106). This is because the default features will try to dynamic link against libyara.so, which means adding it to the list of dynamic dependencies in the resulting ELF for it to be loaded by theld
(glibc) interpreter at the start of the program execution.However, musl binaries produced by rustc do not have any interpreter, they jump straight to the ELF's entrypoint, and libyara.so is never loaded, even though it is listed as a dynamic dependencies in the ELF. This of course ultimately results in a segfault when trying to access any memory belonging to libyara.so, since it was not loaded in our address space.
In my opinion this is a bug in rustc, it should never allow dynamic linking when targeting musl, and this has been reported in this issue rust-lang/rust#82193
Until it is fixed in rustc, this PR adds a new check in
build.rs
to abort compilation when dynamic linking and targeting musl, and prompts the user to use theyara-static
feature instead to static link against libyara.a instead of libyara.so, because that's probably what they actually want when targeting musl.