You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
relocation-model=dynamic-no-pic passes -pie to ld #35061
I'm having trouble linking a Rust program to a non-position-independent C library. When I manually remove the -pie flag from the ld step that rustc runs, everything works correctly. I think that -C relocation-model=dynamic-no-pic should remove the -pie flag for me, but it doesn't.
But when I read the rustc code here, it seems like the intention is that -pie should only be passed to ld when relocation-model=pic. That is, if we're not compiling a position independent executable, then we should not pass the position independent executable -pie flag to ld.
I think what may be happening here is that somehow the defaults from
-pie and -pic are two distinct things. -pie is also unrelated to the relocation-model option. Interestingly, I do not remember us having an option to disable PIE; we should have one.
I did a bit of reading and here's what I figured out, please correct me if any of this is wrong. Linkers are new to me!
Rust builds position independent executables by default on platforms that support it. These platforms have a relocation model of pic in their target config. The code @jvns linked is what sets that linker option, except if it's disallowed by -static.
The correct solution to this is to add a -C no-pie codegen flag, or something like that. Does this sound right?
Reacted by Roman Bartusiak, Others, Arvid E. Picciani and AregevDev
I am having the same issue. I am trying to link against non-position-independent C library, build process ends without error but results returned by Rust bindings are different (and incorrect) than those returned from clean C implementation.
In order to overcome that issue currently I am setting RUSTFLAGS="-C relocation-model=dynamic-no-pic -C linker=./linker" where linker is bash script that wraps cc and filter -pie flags:
#!/bin/bash
filtered_args=""
for arg; do
if [ "$arg" != "-pie" ]
then
filtered_args="$filtered_args $arg"
fi
done
command="cc $filtered_args"
echo " $command" > /tmp/linker
eval "$command"
I'm also having this problem. I assumed that passing -C relocation-model=static when doing a --crate-type=bin build would suppress -pie on the linker command line.
I'm still seeing this issue. Not sure if it's a regression or I'm doing something wrong. I have a "hello world" program in main.rs, and when I run strace -s 300 -e execve -f rustc main.rs -C relocation-model=dynamic-no-pic, the resulting call to ld includes -pie.
I'm using nightly rust on Ubuntu: rustc 1.19.0-nightly (386b0b9d3 2017-05-14).
So, the problem here isn't with rust, but with the linker. In Ubuntu 15.10 and later the default GCC toolchain installed has been compiled with --enable-default-pie which causes -fPIE and -fpie to be passed to cc during code generation and -pie to ld during linking. Now, the linking part is what is important here as rust on most platforms uses cc as the linker for most targets. So, though no PIC or PIE code may have been generated by rustc, the linker will still try to link as with PIE in mind, breaking when trying to compile with -static and the like. To work around this rustc will need to see that the linker has been compiled with PIE as the default and pass -no-pie to the linker if using GCC or something similar for other linkers. Code to do this can be seen in this PR for Tarpaulin though it is only meant for Linux in that state and can be improved greatly if in tree.
Can this issue be resolved by always passing -no-pie, except when compiling with pic?
Sadly, no. In testing I have seen that any build of GCC that was not built with --enable-default-pie rejects the option and fails to run. This is why in the PR that I linked the linker was queried to see if it was compiled with --enable-default-pie before tacking on -no-pie.
So it's came out from the discussion on an issue related to this in tarpaulin that passing -no-pie before -shared (and maybe then -static if it exists for static linkages as well) resolves the issue. I'm going to test this tarpaulin side and I can report back but I feel it would be best if this could be solved rustc side as it can affect any user in a similar situation.
Activity
nagisa commentedon Jul 27, 2016
-pieand-picare two distinct things.-pieis also unrelated to therelocation-modeloption. Interestingly, I do not remember us having an option to disable PIE; we should have one.kamalmarhubi commentedon Aug 2, 2016
I did a bit of reading and here's what I figured out, please correct me if any of this is wrong. Linkers are new to me!
Rust builds position independent executables by default on platforms that support it. These platforms have a relocation model of pic in their target config. The code @jvns linked is what sets that linker option, except if it's disallowed by
-static.The correct solution to this is to add a
-C no-piecodegen flag, or something like that. Does this sound right?riomus commentedon Nov 16, 2016
Hi,
I am having the same issue. I am trying to link against non-position-independent C library, build process ends without error but results returned by Rust bindings are different (and incorrect) than those returned from clean C implementation.
In order to overcome that issue currently I am setting RUSTFLAGS="-C relocation-model=dynamic-no-pic -C linker=./linker" where linker is bash script that wraps cc and filter -pie flags:
jsgf commentedon Jan 2, 2017
I'm also having this problem. I assumed that passing
-C relocation-model=staticwhen doing a--crate-type=binbuild would suppress-pieon the linker command line.(cc: @alexcrichton)
Auto merge of #40245 - cuviper:maybe-not-pie, r=alexcrichton
nstoddard commentedon May 16, 2017
I'm still seeing this issue. Not sure if it's a regression or I'm doing something wrong. I have a "hello world" program in main.rs, and when I run
strace -s 300 -e execve -f rustc main.rs -C relocation-model=dynamic-no-pic, the resulting call toldincludes-pie.I'm using nightly rust on Ubuntu:
rustc 1.19.0-nightly (386b0b9d3 2017-05-14).yodaldevoid commentedon Jul 23, 2017
So, the problem here isn't with rust, but with the linker. In Ubuntu 15.10 and later the default GCC toolchain installed has been compiled with
--enable-default-piewhich causes-fPIEand-fpieto be passed toccduring code generation and-pietoldduring linking. Now, the linking part is what is important here as rust on most platforms usesccas the linker for most targets. So, though no PIC or PIE code may have been generated byrustc, the linker will still try to link as with PIE in mind, breaking when trying to compile with-staticand the like. To work around thisrustcwill need to see that the linker has been compiled with PIE as the default and pass-no-pieto the linker if using GCC or something similar for other linkers. Code to do this can be seen in this PR for Tarpaulin though it is only meant for Linux in that state and can be improved greatly if in tree.EDIT: Don't know why Github isn't linking cross-project PRs correctly right now.
Others commentedon Jul 26, 2017
Can this issue be resolved by always passing
-no-pie, except when compiling withpic?yodaldevoid commentedon Jul 26, 2017
Sadly, no. In testing I have seen that any build of GCC that was not built with
--enable-default-pierejects the option and fails to run. This is why in the PR that I linked the linker was queried to see if it was compiled with--enable-default-piebefore tacking on-no-pie.xd009642 commentedon Aug 8, 2017
So it's came out from the discussion on an issue related to this in tarpaulin that passing -no-pie before -shared (and maybe then -static if it exists for static linkages as well) resolves the issue. I'm going to test this tarpaulin side and I can report back but I feel it would be best if this could be solved rustc side as it can affect any user in a similar situation.
pass correct pie args to gcc linker
Rollup merge of rust-lang#48076 - canarysnort01:fix_pie, r=alexcrichton
Switch to dynamic linking with `liblightning`