From 8df1506a8f89c05a622ab6c66640d5c69f59a3b0 Mon Sep 17 00:00:00 2001 From: Sriraman Tallam Date: Tue, 7 Nov 2017 19:37:51 +0000 Subject: [PATCH] New clang option -fno-plt which avoids the PLT and lazy binding while making external calls. Differential Revision: https://reviews.llvm.org/D39079 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@317605 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/Driver/Options.td | 4 ++++ include/clang/Frontend/CodeGenOptions.def | 2 ++ lib/CodeGen/CGCall.cpp | 10 ++++++++++ lib/Driver/ToolChains/Clang.cpp | 4 ++++ lib/Frontend/CompilerInvocation.cpp | 1 + test/CodeGen/noplt.c | 9 +++++++++ 6 files changed, 30 insertions(+) create mode 100644 test/CodeGen/noplt.c diff --git a/include/clang/Driver/Options.td b/include/clang/Driver/Options.td index e3476c721a..597e03b563 100644 --- a/include/clang/Driver/Options.td +++ b/include/clang/Driver/Options.td @@ -1384,6 +1384,10 @@ def fpic : Flag<["-"], "fpic">, Group; def fno_pic : Flag<["-"], "fno-pic">, Group; def fpie : Flag<["-"], "fpie">, Group; def fno_pie : Flag<["-"], "fno-pie">, Group; +def fplt : Flag<["-"], "fplt">, Group, Flags<[CC1Option]>, + HelpText<"Use the PLT to make function calls">; +def fno_plt : Flag<["-"], "fno-plt">, Group, Flags<[CC1Option]>, + HelpText<"Do not use the PLT to make function calls">; def fropi : Flag<["-"], "fropi">, Group; def fno_ropi : Flag<["-"], "fno-ropi">, Group; def frwpi : Flag<["-"], "frwpi">, Group; diff --git a/include/clang/Frontend/CodeGenOptions.def b/include/clang/Frontend/CodeGenOptions.def index 8f2aae2f14..b4c68a49bb 100644 --- a/include/clang/Frontend/CodeGenOptions.def +++ b/include/clang/Frontend/CodeGenOptions.def @@ -297,6 +297,8 @@ CODEGENOPT(PreserveVec3Type, 1, 0) /// Whether to emit .debug_gnu_pubnames section instead of .debug_pubnames. CODEGENOPT(GnuPubnames, 1, 0) +CODEGENOPT(NoPLT, 1, 0) + #undef CODEGENOPT #undef ENUM_CODEGENOPT #undef VALUE_CODEGENOPT diff --git a/lib/CodeGen/CGCall.cpp b/lib/CodeGen/CGCall.cpp index 971455a873..cefd73be27 100644 --- a/lib/CodeGen/CGCall.cpp +++ b/lib/CodeGen/CGCall.cpp @@ -1855,6 +1855,16 @@ void CodeGenModule::ConstructAttributeList( !(TargetDecl && TargetDecl->hasAttr())) FuncAttrs.addAttribute("split-stack"); + // Add NonLazyBind attribute to function declarations when -fno-plt + // is used. + if (TargetDecl && CodeGenOpts.NoPLT) { + if (auto *Fn = dyn_cast(TargetDecl)) { + if (!Fn->isDefined() && !AttrOnCallSite) { + FuncAttrs.addAttribute(llvm::Attribute::NonLazyBind); + } + } + } + if (!AttrOnCallSite) { bool DisableTailCalls = CodeGenOpts.DisableTailCalls || diff --git a/lib/Driver/ToolChains/Clang.cpp b/lib/Driver/ToolChains/Clang.cpp index 6ba5d048a4..758ece19e1 100644 --- a/lib/Driver/ToolChains/Clang.cpp +++ b/lib/Driver/ToolChains/Clang.cpp @@ -3423,6 +3423,10 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, CmdArgs.push_back("-mpie-copy-relocations"); } + if (Args.hasFlag(options::OPT_fno_plt, options::OPT_fplt, false)) { + CmdArgs.push_back("-fno-plt"); + } + // -fhosted is default. // TODO: Audit uses of KernelOrKext and see where it'd be more appropriate to // use Freestanding. diff --git a/lib/Frontend/CompilerInvocation.cpp b/lib/Frontend/CompilerInvocation.cpp index 692b20bd3a..ca1c65e95a 100644 --- a/lib/Frontend/CompilerInvocation.cpp +++ b/lib/Frontend/CompilerInvocation.cpp @@ -653,6 +653,7 @@ static bool ParseCodeGenArgs(CodeGenOptions &Opts, ArgList &Args, InputKind IK, Args.hasArg(OPT_mincremental_linker_compatible); Opts.PIECopyRelocations = Args.hasArg(OPT_mpie_copy_relocations); + Opts.NoPLT = Args.hasArg(OPT_fno_plt); Opts.OmitLeafFramePointer = Args.hasArg(OPT_momit_leaf_frame_pointer); Opts.SaveTempLabels = Args.hasArg(OPT_msave_temp_labels); Opts.NoDwarfDirectoryAsm = Args.hasArg(OPT_fno_dwarf_directory_asm); diff --git a/test/CodeGen/noplt.c b/test/CodeGen/noplt.c new file mode 100644 index 0000000000..ce054e2b7c --- /dev/null +++ b/test/CodeGen/noplt.c @@ -0,0 +1,9 @@ +// RUN: %clang_cc1 -emit-llvm -fno-plt %s -o - | FileCheck %s -check-prefix=CHECK-NOPLT + +// CHECK-NOPLT: Function Attrs: nonlazybind +// CHECK-NOPLT-NEXT: declare i32 @foo +int foo(); + +int bar() { + return foo(); +} -- 2.40.0