Skip to content

Commit

Permalink
New clang option -fno-plt which avoids the PLT and lazy binding while…
Browse files Browse the repository at this point in the history
… making external calls.

Differential Revision: https://reviews.llvm.org/D39079

llvm-svn: 317605
  • Loading branch information
tmsri committed Nov 7, 2017
1 parent 4e8ce02 commit 5c65148
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 0 deletions.
4 changes: 4 additions & 0 deletions clang/include/clang/Driver/Options.td
Expand Up @@ -1384,6 +1384,10 @@ def fpic : Flag<["-"], "fpic">, Group<f_Group>;
def fno_pic : Flag<["-"], "fno-pic">, Group<f_Group>;
def fpie : Flag<["-"], "fpie">, Group<f_Group>;
def fno_pie : Flag<["-"], "fno-pie">, Group<f_Group>;
def fplt : Flag<["-"], "fplt">, Group<f_Group>, Flags<[CC1Option]>,
HelpText<"Use the PLT to make function calls">;
def fno_plt : Flag<["-"], "fno-plt">, Group<f_Group>, Flags<[CC1Option]>,
HelpText<"Do not use the PLT to make function calls">;
def fropi : Flag<["-"], "fropi">, Group<f_Group>;
def fno_ropi : Flag<["-"], "fno-ropi">, Group<f_Group>;
def frwpi : Flag<["-"], "frwpi">, Group<f_Group>;
Expand Down
2 changes: 2 additions & 0 deletions clang/include/clang/Frontend/CodeGenOptions.def
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions clang/lib/CodeGen/CGCall.cpp
Expand Up @@ -1855,6 +1855,16 @@ void CodeGenModule::ConstructAttributeList(
!(TargetDecl && TargetDecl->hasAttr<NoSplitStackAttr>()))
FuncAttrs.addAttribute("split-stack");

// Add NonLazyBind attribute to function declarations when -fno-plt
// is used.
if (TargetDecl && CodeGenOpts.NoPLT) {
if (auto *Fn = dyn_cast<FunctionDecl>(TargetDecl)) {
if (!Fn->isDefined() && !AttrOnCallSite) {
FuncAttrs.addAttribute(llvm::Attribute::NonLazyBind);
}
}
}

if (!AttrOnCallSite) {
bool DisableTailCalls =
CodeGenOpts.DisableTailCalls ||
Expand Down
4 changes: 4 additions & 0 deletions clang/lib/Driver/ToolChains/Clang.cpp
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions clang/lib/Frontend/CompilerInvocation.cpp
Expand Up @@ -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);
Expand Down
9 changes: 9 additions & 0 deletions clang/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();
}

0 comments on commit 5c65148

Please sign in to comment.