Skip to content

Commit

Permalink
PIC redo tmp
Browse files Browse the repository at this point in the history
  • Loading branch information
madsmtm committed Dec 6, 2024
1 parent ec73df9 commit f0e2aa4
Show file tree
Hide file tree
Showing 6 changed files with 410 additions and 23 deletions.
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

0 comments on commit f0e2aa4

Please sign in to comment.