Skip to content

Commit 82d730b

Browse files
authored
Merge pull request #351 from GabrielMajeri/no-plt
Add support for `-fno-plt`
2 parents ee7b97e + bf17fc1 commit 82d730b

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

src/lib.rs

+22
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ pub struct Build {
112112
archiver: Option<PathBuf>,
113113
cargo_metadata: bool,
114114
pic: Option<bool>,
115+
use_plt: Option<bool>,
115116
static_crt: Option<bool>,
116117
shared_flag: Option<bool>,
117118
static_flag: Option<bool>,
@@ -319,6 +320,7 @@ impl Build {
319320
archiver: None,
320321
cargo_metadata: true,
321322
pic: None,
323+
use_plt: None,
322324
static_crt: None,
323325
warnings: None,
324326
extra_warnings: None,
@@ -822,6 +824,21 @@ impl Build {
822824
self
823825
}
824826

827+
/// Configures whether the Procedure Linkage Table is used for indirect
828+
/// calls into shared libraries.
829+
///
830+
/// The PLT is used to provide features like lazy binding, but introduces
831+
/// a small performance loss due to extra pointer indirection. Setting
832+
/// `use_plt` to `false` can provide a small performance increase.
833+
///
834+
/// Note that skipping the PLT requires a recent version of GCC/Clang.
835+
///
836+
/// This only applies to ELF targets. It has no effect on other platforms.
837+
pub fn use_plt(&mut self, use_plt: bool) -> &mut Build {
838+
self.use_plt = Some(use_plt);
839+
self
840+
}
841+
825842
/// Configures whether the /MT flag or the /MD flag will be passed to msvc build tools.
826843
///
827844
/// This option defaults to `false`, and affect only msvc targets.
@@ -1123,6 +1140,11 @@ impl Build {
11231140
}
11241141
if self.pic.unwrap_or(!target.contains("windows-gnu")) {
11251142
cmd.push_cc_arg("-fPIC".into());
1143+
// PLT only applies if code is compiled with PIC support,
1144+
// and only for ELF targets.
1145+
if target.contains("linux") && !self.use_plt.unwrap_or(true) {
1146+
cmd.push_cc_arg("-fno-plt".into());
1147+
}
11261148
}
11271149
}
11281150
}

tests/test.rs

+14
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,20 @@ fn gnu_i686_pic() {
197197
}
198198
}
199199

200+
#[test]
201+
fn gnu_x86_64_no_plt() {
202+
let target = "x86_64-unknown-linux-gnu";
203+
let test = Test::gnu();
204+
test.gcc()
205+
.pic(true)
206+
.use_plt(false)
207+
.target(&target)
208+
.host(&target)
209+
.file("foo.c")
210+
.compile("foo");
211+
test.cmd(0).must_have("-fno-plt");
212+
}
213+
200214
#[test]
201215
fn gnu_set_stdlib() {
202216
let test = Test::gnu();

0 commit comments

Comments
 (0)