Skip to content
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

Make -fPIC follow rustc's default #1315

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions dev-tools/gen-target-info/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ fn generate_target_mapping(f: &mut File, target_specs: &RustcTargetSpecs) -> std
let os = spec.os.as_deref().unwrap_or("none");
let env = spec.env.as_deref().unwrap_or("");
let abi = spec.abi.as_deref().unwrap_or("");
let relocation_model_static = match spec.relocation_model.as_deref() {
Some("static") => true,
Some("pic") => false,
None => false,
Some(relocation_model) => {
unimplemented!("unknown relocation_model: {relocation_model}")
}
};

let unversioned_llvm_target = if spec.llvm_target.contains("apple") {
// Remove deployment target information from LLVM target triples (we
Expand Down Expand Up @@ -84,6 +92,10 @@ fn generate_target_mapping(f: &mut File, target_specs: &RustcTargetSpecs) -> std
f,
" unversioned_llvm_target: {unversioned_llvm_target:?},"
)?;
writeln!(
f,
" relocation_model_static: {relocation_model_static},"
)?;
writeln!(f, " }},")?;
writeln!(f, " ),")?;
}
Expand Down
1 change: 1 addition & 0 deletions dev-tools/gen-target-info/src/target_specs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pub struct TargetSpec {
pub env: Option<String>,
pub abi: Option<String>,
pub pre_link_args: Option<PreLinkArgs>,
pub relocation_model: Option<String>,
}

#[derive(Debug, Deserialize)]
Expand Down
44 changes: 22 additions & 22 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1281,8 +1281,9 @@ impl Build {

/// Configures whether the compiler will emit position independent code.
///
/// This option defaults to `false` for `windows-gnu` and bare metal targets and
/// to `true` for all other targets.
/// This option defaults to what `rustc` does (roughly `false` for
/// Windows, bare metal, WebAssembly and Real-Time operating system
/// targets and `true` for all others).
pub fn pic(&mut self, pic: bool) -> &mut Build {
self.pic = Some(pic);
self
Expand Down Expand Up @@ -2051,26 +2052,7 @@ impl Build {
cmd.push_cc_arg("-ffunction-sections".into());
cmd.push_cc_arg("-fdata-sections".into());
}
// Disable generation of PIC on bare-metal for now: rust-lld doesn't support this yet
//
// `rustc` also defaults to disable PIC on WASM:
// <https://github.com/rust-lang/rust/blob/1.82.0/compiler/rustc_target/src/spec/base/wasm.rs#L101-L108>
if self.pic.unwrap_or(
target.os != "windows"
&& target.os != "none"
&& target.os != "uefi"
&& target.arch != "wasm32"
&& target.arch != "wasm64",
) {
cmd.push_cc_arg("-fPIC".into());
// PLT only applies if code is compiled with PIC support,
// and only for ELF targets.
if (target.os == "linux" || target.os == "android")
&& !self.use_plt.unwrap_or(true)
{
cmd.push_cc_arg("-fno-plt".into());
}
}

if target.arch == "wasm32" || target.arch == "wasm64" {
// WASI does not support exceptions yet.
// https://github.com/WebAssembly/exception-handling
Expand All @@ -2097,6 +2079,24 @@ impl Build {
}
}

// -fPIC is not supported on Windows MSVC.
if !matches!(target.os, "windows" | "uefi") {
if self.pic.unwrap_or(!target.relocation_model_static) {
cmd.push_cc_arg("-fPIC".into());
} else {
cmd.push_cc_arg("-fno-PIC".into());
}
}

// PLT only applies if code is compiled with PIC support,
// and only for ELF targets.
if self.pic.unwrap_or(!target.relocation_model_static)
&& (target.os == "linux" || target.os == "android")
&& !self.use_plt.unwrap_or(true)
{
cmd.push_cc_arg("-fno-plt".into());
}

if self.get_debug() {
if self.cuda {
// NVCC debug flag
Expand Down
2 changes: 2 additions & 0 deletions src/target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ pub(crate) struct TargetInfo<'a> {
pub abi: &'a str,
/// The unversioned LLVM/Clang target triple.
unversioned_llvm_target: &'a str,
/// Whether the default relocation model is static (i.e. not PIC).
pub relocation_model_static: bool,
}

impl FromStr for TargetInfo<'_> {
Expand Down
Loading
Loading