From 6ff0b507b6e37ed37deb564f6a36eec3b2feedc7 Mon Sep 17 00:00:00 2001 From: Gabriel Majeri Date: Wed, 26 Sep 2018 19:19:55 +0300 Subject: [PATCH 1/3] Add the `NonLazyBind` attribute to `rustllvm` This attribute informs LLVM to skip PLT calls in codegen. --- src/librustc_codegen_llvm/llvm/ffi.rs | 1 + src/rustllvm/RustWrapper.cpp | 2 ++ src/rustllvm/rustllvm.h | 1 + 3 files changed, 4 insertions(+) diff --git a/src/librustc_codegen_llvm/llvm/ffi.rs b/src/librustc_codegen_llvm/llvm/ffi.rs index a5f4137c62b14..870a03c6f3415 100644 --- a/src/librustc_codegen_llvm/llvm/ffi.rs +++ b/src/librustc_codegen_llvm/llvm/ffi.rs @@ -122,6 +122,7 @@ pub enum Attribute { SanitizeThread = 20, SanitizeAddress = 21, SanitizeMemory = 22, + NonLazyBind = 23, } /// LLVMIntPredicate diff --git a/src/rustllvm/RustWrapper.cpp b/src/rustllvm/RustWrapper.cpp index 9b9c908ea5272..31282c35792cf 100644 --- a/src/rustllvm/RustWrapper.cpp +++ b/src/rustllvm/RustWrapper.cpp @@ -178,6 +178,8 @@ static Attribute::AttrKind fromRust(LLVMRustAttribute Kind) { return Attribute::SanitizeAddress; case SanitizeMemory: return Attribute::SanitizeMemory; + case NonLazyBind: + return Attribute::NonLazyBind; } report_fatal_error("bad AttributeKind"); } diff --git a/src/rustllvm/rustllvm.h b/src/rustllvm/rustllvm.h index 1070068b99845..b6fa9a2fa9508 100644 --- a/src/rustllvm/rustllvm.h +++ b/src/rustllvm/rustllvm.h @@ -97,6 +97,7 @@ enum LLVMRustAttribute { SanitizeThread = 20, SanitizeAddress = 21, SanitizeMemory = 22, + NonLazyBind = 23, }; typedef struct OpaqueRustString *RustStringRef; From 3b08b137187d85783b6dec17879efb55ad04bc06 Mon Sep 17 00:00:00 2001 From: Gabriel Majeri Date: Wed, 26 Sep 2018 19:24:38 +0300 Subject: [PATCH 2/3] Disable PLT unconditionally Apply the `NonLazyBind` attribute on every function. --- src/librustc_codegen_llvm/attributes.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/librustc_codegen_llvm/attributes.rs b/src/librustc_codegen_llvm/attributes.rs index 51380db5b23df..57ce740154a29 100644 --- a/src/librustc_codegen_llvm/attributes.rs +++ b/src/librustc_codegen_llvm/attributes.rs @@ -173,6 +173,7 @@ pub fn from_fn_attrs( set_frame_pointer_elimination(cx, llfn); set_probestack(cx, llfn); + Attribute::NonLazyBind.apply_llfn(Function, llfn); if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::COLD) { Attribute::Cold.apply_llfn(Function, llfn); From ddf98c1f387301842714a5a2d92267ea20c49669 Mon Sep 17 00:00:00 2001 From: Gabriel Majeri Date: Wed, 26 Sep 2018 21:13:49 +0300 Subject: [PATCH 3/3] Only enable no-plt when full relro is enabled --- src/librustc_codegen_llvm/attributes.rs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/librustc_codegen_llvm/attributes.rs b/src/librustc_codegen_llvm/attributes.rs index 57ce740154a29..44e169ae853de 100644 --- a/src/librustc_codegen_llvm/attributes.rs +++ b/src/librustc_codegen_llvm/attributes.rs @@ -20,7 +20,7 @@ use rustc::ty::layout::HasTyCtxt; use rustc::ty::query::Providers; use rustc_data_structures::sync::Lrc; use rustc_data_structures::fx::FxHashMap; -use rustc_target::spec::PanicStrategy; +use rustc_target::spec::{PanicStrategy, RelroLevel}; use attributes; use llvm::{self, Attribute}; @@ -173,7 +173,12 @@ pub fn from_fn_attrs( set_frame_pointer_elimination(cx, llfn); set_probestack(cx, llfn); - Attribute::NonLazyBind.apply_llfn(Function, llfn); + + // Only enable this optimization if full relro is also enabled. + // In this case, lazy binding was already unavailable, so nothing is lost. + if let RelroLevel::Full = cx.sess().target.target.options.relro_level { + Attribute::NonLazyBind.apply_llfn(Function, llfn); + } if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::COLD) { Attribute::Cold.apply_llfn(Function, llfn);