Skip to content

Commit fd59191

Browse files
committed
Enable -fno-plt by default and add tests
1 parent ee7b97e commit fd59191

File tree

2 files changed

+67
-1
lines changed

2 files changed

+67
-1
lines changed

src/lib.rs

+26
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,25 @@ 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.
832+
///
833+
/// Note that skipping the PLT requires a recent version of GCC/Clang,
834+
/// therefore this option should only be considered a hint to disable the
835+
/// PLT where supported.
836+
///
837+
/// The PLT is disabled by default when PIC is enabled, with the exception
838+
/// of `powerpc64-unknown-linux-gnu`.
839+
///
840+
/// This only applies to ELF targets. It has no effect on other platforms.
841+
pub fn use_plt(&mut self, use_plt: bool) -> &mut Build {
842+
self.use_plt = Some(use_plt);
843+
self
844+
}
845+
825846
/// Configures whether the /MT flag or the /MD flag will be passed to msvc build tools.
826847
///
827848
/// This option defaults to `false`, and affect only msvc targets.
@@ -1123,6 +1144,11 @@ impl Build {
11231144
}
11241145
if self.pic.unwrap_or(!target.contains("windows-gnu")) {
11251146
cmd.push_cc_arg("-fPIC".into());
1147+
if !self.use_plt.unwrap_or(target.contains("powerpc64-unknown-linux")) {
1148+
if self.is_flag_supported("-fno-plt").unwrap_or(false) {
1149+
cmd.push_cc_arg("-fno-plt".into());
1150+
}
1151+
}
11261152
}
11271153
}
11281154
}

tests/test.rs

+41-1
Original file line numberDiff line numberDiff line change
@@ -193,10 +193,50 @@ fn gnu_i686_pic() {
193193
.file("foo.c")
194194
.compile("foo");
195195

196-
test.cmd(0).must_have("-fPIC");
196+
test.cmd(0).must_have("-fPIC").must_have("-fno-plt");
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+
.target(&target)
206+
.host(&target)
207+
.file("foo.c")
208+
.compile("foo");
209+
210+
test.cmd(0).must_have("-fno-plt");
211+
}
212+
213+
#[test]
214+
fn gnu_i686_use_plt() {
215+
let target = format!("i686-unknown-linux-gnu");
216+
let test = Test::gnu();
217+
test.gcc()
218+
.pic(true)
219+
.target(&target)
220+
.host(&target)
221+
.file("foo.c")
222+
.compile("foo");
223+
224+
test.cmd(0).must_have("-fno-plt");
225+
}
226+
227+
#[test]
228+
fn powerpc64_unknown_linux_use_plt() {
229+
let target = "powerpc64-unknown-linux-gnu";
230+
let test = Test::gnu();
231+
test.gcc()
232+
.pic(true)
233+
.target(&target)
234+
.host(&target)
235+
.file("foo.c")
236+
.compile("foo");
237+
test.cmd(0).must_not_have("-fno-plt");
238+
}
239+
200240
#[test]
201241
fn gnu_set_stdlib() {
202242
let test = Test::gnu();

0 commit comments

Comments
 (0)