Skip to content

Commit

Permalink
Enable -fno-plt by default and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
GabrielMajeri committed Sep 27, 2018
1 parent ee7b97e commit e16de19
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ pub struct Build {
archiver: Option<PathBuf>,
cargo_metadata: bool,
pic: Option<bool>,
use_plt: Option<bool>,
static_crt: Option<bool>,
shared_flag: Option<bool>,
static_flag: Option<bool>,
Expand Down Expand Up @@ -319,6 +320,7 @@ impl Build {
archiver: None,
cargo_metadata: true,
pic: None,
use_plt: None,
static_crt: None,
warnings: None,
extra_warnings: None,
Expand Down Expand Up @@ -822,6 +824,25 @@ impl Build {
self
}

/// Configures whether the Procedure Linkage Table is used for indirect
/// calls into shared libraries.
///
/// The PLT is used to provide features like lazy binding, but introduces
/// a small performance loss due to extra pointer indirection.
///
/// Note that skipping the PLT requires a recent version of GCC/Clang,
/// therefore this option should only be considered a hint to disable the
/// PLT where supported.
///
/// The PLT is disabled by default when PIC is enabled, with the exception
/// of `powerpc64-unknown-linux-gnu`.
///
/// This only applies to ELF targets. It has no effect on other platforms.
pub fn use_plt(&mut self, use_plt: bool) -> &mut Build {
self.use_plt = Some(use_plt);
self
}

/// Configures whether the /MT flag or the /MD flag will be passed to msvc build tools.
///
/// This option defaults to `false`, and affect only msvc targets.
Expand Down Expand Up @@ -1123,6 +1144,11 @@ impl Build {
}
if self.pic.unwrap_or(!target.contains("windows-gnu")) {
cmd.push_cc_arg("-fPIC".into());
if !self.use_plt.unwrap_or(target.contains("powerpc64-unknown-linux")) {
if self.is_flag_supported("-fno-plt").unwrap_or(false) {
cmd.push_cc_arg("-fno-plt".into());
}
}
}
}
}
Expand Down
13 changes: 13 additions & 0 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,19 @@ fn gnu_i686_pic() {
}
}

#[test]
fn powerpc64_unknown_linux_use_plt() {
let target = "powerpc64-unknown-linux-gnu";
let test = Test::gnu();
test.gcc()
.pic(true)
.target(&target)
.host(&target)
.file("foo.c")
.compile("foo");
test.cmd(0).must_not_have("-fno-plt");
}

#[test]
fn gnu_set_stdlib() {
let test = Test::gnu();
Expand Down

0 comments on commit e16de19

Please sign in to comment.