-
Notifications
You must be signed in to change notification settings - Fork 477
Proper way to statically link rust_binary target with a static non-PIC cc_library #118
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
Comments
Curious if anyone has thoughts on this topic? The current annoyance, from my standpoint, is the need to specify I suppose one way around this is to configure the rust Bazel toolchain to automatically add this flag to a As mentioned in my original post, it seems one would want to convey link options "top-down", with a Or maybe something else entirely? How are others dealing with this issue? I presume this must be somewhat common for any Rust binaries that FFI to Bazel-built C libs. |
Just a quick link drop - this appears to be the code in bazel cpp rules that determines PIC'ness based on (among other things) the compilation mode. |
@acmcarther, curious - how do you guys handle this at Google (assuming you're using rules_rust there)? |
My team at Google doesn't use Rust[1], so I don't have an answer on this item. I'm also not well informed on nuances of linking (within Rust or elsewhere), so I'm afraid you're on the cutting edge here. [1] Some teams do, Fuchsia happens to be public facing, and their rust repos may or may not be enlightening. They don't use Bazel though. |
It looks like this was recently exposed in the cc starlark api, and could be as simple as reordering these two lines in our rules: rules_rust/rust/private/legacy_cc_starlark_api_shim.bzl Lines 20 to 21 in b3cc7e4
Did you have any particular thoughts @hlopko? |
@mfarrugi This issue is also causing us some pain. I tried the solution you mentioned, but I get this error:
|
We also tried to work around this issue with bazel option
|
@kalcutter I have no insight on either error. I also am not very confident in the snippet I posted earlier, and don't have access to the complicated codebase I used to work with. |
Rust binaries are compiled with -fPIC by default, and this fights with bazel's default of -fno-pic when using `-c opt` mode. This has caused the compilation to fail. After some running around with hair on fire, I found bazelbuild/rules_rust#118, which seems to work around the issue. I think in the long run, rules_rust should handle this automagically for the users.
Hi folks. I just randomly hit this issue while working on Kythe. First off, I am grateful for the workaround ( It seems to me that |
* feat(rust): Adds a C API for the kzip writer This will allow me to reuse the C++ kzip writing code for the rust indexer instead of redoing that work in rust. Issue #4606 * Renames the 'status' library Somewhat surprisingly -- if there are two libraries in the global namespace that have the same name, bazel does not disambiguate them, and an attempt to link them will fail. Not sure why, but that's what happens. * fixup: handled review comments Simplified and cleared up issues raised during code review. * Works around the -fPIC issue for rust Rust binaries are compiled with -fPIC by default, and this fights with bazel's default of -fno-pic when using `-c opt` mode. This has caused the compilation to fail. After some running around with hair on fire, I found bazelbuild/rules_rust#118, which seems to work around the issue. I think in the long run, rules_rust should handle this automagically for the users. * Document no NUL byte at the end of results
I'm looking into this and I cannot reproduce. Are you compiling your C++ as no-PIC and no-PIE (my testing toolchain uses PIE for C++)? |
We build our C++ libraries with @hlopko Are you testing with |
Yup I was using You may want to change your fix to |
One related thing I may pursue soonish is rust-lang/rust#87934 - that will not help with NOPIC and NOPIE objects, but it will make executables with PIE a bit faster. |
I'm running into this too. I have experience with C++ and Bazel toolchains, and I'm interested in helping with this. Before I dive in, any updates @hlopko? To hopefully help others find this from the error messages, here's the symptoms I saw:
|
No, the C++ part is not built with My whole toolchain is at https://github.com/frc971/971-Robot-Code/tree/master/third_party/bazel-toolchain (fork of https://github.com/grailbio/bazel-toolchain). It's using |
@bsilver8192 did you get a chance to push those changes? Looks like this is still very much broken :( |
It's actually merged now. If you comment out https://github.com/frc971/971-Robot-Code/blob/8e815fbb728ddc4df9714f72beb925cb8c93159d/third_party/cargo_raze/cargo_raze.patch#L194 and then So far I don't have any other rust_binary targets outside of third_party which are complex enough to run into this. |
This may be more of a bazel question but since it's in the context of rust, I figured the audience here is as good as any 😄 .
Suppose we have the following targets:
cc_library
building some static librust_library
FFI wrapper over the aboverust_binary
that uses therust_library
Pretty standard and simple setup for rust using c/c++ code.
In a
dbg
orfastbuild
setup, it seems the above just works. However, when I recently tried this with anopt
build using a custom cc toolchain, the code failed to link. Quick note on the custom toolchain - it's very close to the configuration/setup of bazel's builtin crosstool but uses vendored gcc (and related tools).The link failure was around invalid relocations. This is because
rustc
, by default, wants to build position-independent (PIE) executables. To do that, the c/c++ static libs need to be built with-fPIC
- building thecc_library
as PIC fixes the issue. However, this requires modifying a target whose other downstream users may not want to be PIC'd (and PIC can have non-zero perf cost, albeit low from what I gather).The other fix that I came across is changing the
rust_binary
target to specify a codegen option torustc
:--relocation-model=static
. This builds a non-PIE binary, and doesn't require-fPIC
on the dependent cc libs.This feels ... wrong, however. Either I'd need to modify all rust binary targets to select static relocation, or I'd need to modify the c/c++ static libs to be PIC. I also don't know why there's a difference between
dbg
/fastbuild
andopt
builds.Does anyone know if there's a better way to control linkage and relocation model? Ideally (I think), a
rust_binary
would select the linkage it wants (static or dynamic) and the relocation model (static or dynamic), and this choice would flow down to dependent libs, where bazel would build them as either static or dynamic libs and also select-fPIC
or not.Curious to hear thoughts/suggestions.
cc @mfarrugi as he's interested in this as well, I believe.
The text was updated successfully, but these errors were encountered: