Skip to content

Commit 76c4031

Browse files
committed
Refactor code and address review comments
1 parent 3249f0b commit 76c4031

File tree

5 files changed

+32
-13
lines changed

5 files changed

+32
-13
lines changed

src/librustc/session/config.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1387,7 +1387,7 @@ options! {DebuggingOptions, DebuggingSetter, basic_debugging_options,
13871387
"output a json file with profiler results"),
13881388
emit_stack_sizes: bool = (false, parse_bool, [UNTRACKED],
13891389
"emits a section containing stack size metadata"),
1390-
plt: Option<bool> = (Some(false), parse_opt_bool, [TRACKED],
1390+
plt: Option<bool> = (None, parse_opt_bool, [TRACKED],
13911391
"whether to use the PLT when calling into shared libraries;
13921392
only has effect for PIC code on systems with ELF binaries
13931393
(default: PLT is disabled if full relro is enabled)"),

src/librustc/session/mod.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@ use syntax::feature_gate::AttributeType;
4141
use syntax_pos::{MultiSpan, Span};
4242
use util::profiling::SelfProfiler;
4343

44-
use rustc_target::spec::PanicStrategy;
45-
use rustc_target::spec::{Target, TargetTriple};
44+
use rustc_target::spec::{PanicStrategy, RelroLevel, Target, TargetTriple};
4645
use rustc_data_structures::flock;
4746
use jobserver::Client;
4847

@@ -981,6 +980,23 @@ impl Session {
981980
pub fn edition(&self) -> Edition {
982981
self.opts.edition
983982
}
983+
984+
/// True if we cannot skip the PLT for shared library calls.
985+
pub fn needs_plt(&self) -> bool {
986+
let dbg_opts = &self.opts.debugging_opts;
987+
988+
let relro_level = dbg_opts.relro_level
989+
.unwrap_or(self.target.target.options.relro_level);
990+
991+
// Only enable this optimization by default if full relro is also enabled.
992+
// In this case, lazy binding was already unavailable, so nothing is lost.
993+
// This also ensures `-Wl,-z,now` is supported by the linker.
994+
let full_relro = RelroLevel::Full == relro_level;
995+
996+
// If user didn't explicitly forced us to use the PLT,
997+
// then try to skip it where possible.
998+
dbg_opts.plt.unwrap_or(!full_relro)
999+
}
9841000
}
9851001

9861002
pub fn build_session(

src/librustc_codegen_llvm/attributes.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use rustc::ty::layout::HasTyCtxt;
2020
use rustc::ty::query::Providers;
2121
use rustc_data_structures::sync::Lrc;
2222
use rustc_data_structures::fx::FxHashMap;
23-
use rustc_target::spec::{PanicStrategy, RelroLevel};
23+
use rustc_target::spec::PanicStrategy;
2424

2525
use attributes;
2626
use llvm::{self, Attribute};
@@ -138,6 +138,15 @@ pub fn apply_target_cpu_attr(cx: &CodegenCx<'ll, '_>, llfn: &'ll Value) {
138138
target_cpu.as_c_str());
139139
}
140140

141+
/// Sets the `NonLazyBind` LLVM attribute on a given function,
142+
/// assuming the codegen options allow skipping the PLT.
143+
pub fn non_lazy_bind(sess: &Session, llfn: &'ll Value) {
144+
// Don't generate calls through PLT if it's not necessary
145+
if !sess.needs_plt() {
146+
Attribute::NonLazyBind.apply_llfn(Function, llfn);
147+
}
148+
}
149+
141150
/// Composite function which sets LLVM attributes for function depending on its AST (#[attribute])
142151
/// attributes.
143152
pub fn from_fn_attrs(
@@ -174,14 +183,6 @@ pub fn from_fn_attrs(
174183
set_frame_pointer_elimination(cx, llfn);
175184
set_probestack(cx, llfn);
176185

177-
if !cx.sess().opts.debugging_opts.plt.unwrap_or(false) {
178-
// Only enable this optimization if full relro is also enabled.
179-
// In this case, lazy binding was already unavailable, so nothing is lost.
180-
if let RelroLevel::Full = cx.sess().target.target.options.relro_level {
181-
Attribute::NonLazyBind.apply_llfn(Function, llfn);
182-
}
183-
}
184-
185186
if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::COLD) {
186187
Attribute::Cold.apply_llfn(Function, llfn);
187188
}

src/librustc_codegen_llvm/context.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ pub unsafe fn create_module(
210210

211211
// If skipping the PLT is enabled, we need to add some module metadata
212212
// to ensure intrinsic calls don't use it.
213-
if !sess.opts.debugging_opts.plt.unwrap_or(false) {
213+
if !sess.needs_plt() {
214214
let avoid_plt = "RtLibUseGOT\0".as_ptr() as *const _;
215215
llvm::LLVMRustAddModuleFlag(llmod, avoid_plt, 1);
216216
}

src/librustc_codegen_llvm/declare.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ fn declare_raw_fn(
104104
attributes::unwind(llfn, false);
105105
}
106106

107+
attributes::non_lazy_bind(cx.sess(), llfn);
108+
107109
llfn
108110
}
109111

0 commit comments

Comments
 (0)