]> granicus.if.org Git - llvm/commitdiff
[COFF] Use 32-bit jump table entries in .rdata for Win64
authorReid Kleckner <rnk@google.com>
Thu, 29 Dec 2016 00:12:39 +0000 (00:12 +0000)
committerReid Kleckner <rnk@google.com>
Thu, 29 Dec 2016 00:12:39 +0000 (00:12 +0000)
Summary:
We were already using 32-bit jump table entries, but this was a
consequence of the default PIC model on Win64, and not an intentional
design decision. This patch ensures that we always use 32-bit label
difference jump table entries on Win64 regardless of the PIC model. This
is a good idea because it saves executable size and object file size.

Moving the jump tables to .rdata cleans up the disassembled object code
and reduces the available ROP targets, but it requires adding one more
RIP-relative lea to the code.  COFF doesn't have relocations to express
the difference between two arbitrary symbols, so we can't use the jump
table label in the label difference like we do elsewhere.

Fixes PR31488

Reviewers: majnemer, compnerd

Subscribers: llvm-commits

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

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@290694 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/CodeGen/TargetLoweringObjectFileImpl.h
lib/CodeGen/TargetLoweringObjectFileImpl.cpp
lib/Target/X86/X86ISelLowering.cpp
lib/Target/X86/X86ISelLowering.h
test/CodeGen/X86/win64-jumptable.ll

index cc71fa3918a1332278380dbb6cb898a7aca20dab..1839b6275c5c95f9298d431911abf54c6da0318b 100644 (file)
@@ -156,6 +156,9 @@ public:
   MCSection *getSectionForJumpTable(const Function &F,
                                     const TargetMachine &TM) const override;
 
+  bool shouldPutJumpTableInFunctionSection(bool UsesLabelDifference,
+                                           const Function &F) const override;
+
   /// Emit Obj-C garbage collection and linker options. Only linker option
   /// emission is implemented for COFF.
   void emitModuleFlags(MCStreamer &Streamer,
index eb2a28f574a5449b45e9fa7821a5777a271d7088..d19ca47ec1aa09ba39932c982d9afbed1e4c1f2f 100644 (file)
@@ -1055,6 +1055,13 @@ MCSection *TargetLoweringObjectFileCOFF::getSectionForJumpTable(
                                      COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE, UniqueID);
 }
 
+bool TargetLoweringObjectFileCOFF::shouldPutJumpTableInFunctionSection(
+    bool UsesLabelDifference, const Function &F) const {
+  // We can always create relative relocations, so use another section
+  // that can be marked non-executable.
+  return false;
+}
+
 void TargetLoweringObjectFileCOFF::emitModuleFlags(
     MCStreamer &Streamer, ArrayRef<Module::ModuleFlagEntry> ModuleFlags,
     const TargetMachine &TM) const {
index b293dfa98f825c712cde0f9898f66cbd3aaa5c5e..d0c77030acdc9c27cfaeb2ce9bbec6348eff2cb1 100644 (file)
@@ -1926,10 +1926,20 @@ unsigned X86TargetLowering::getJumpTableEncoding() const {
   if (isPositionIndependent() && Subtarget.isPICStyleGOT())
     return MachineJumpTableInfo::EK_Custom32;
 
+  // On Win64, we want to use both label differences and a separate section.
+  if (Subtarget.isTargetWin64())
+    return MachineJumpTableInfo::EK_LabelDifference32;
+
   // Otherwise, use the normal jump table encoding heuristics.
   return TargetLowering::getJumpTableEncoding();
 }
 
+bool X86TargetLowering::isJumpTableRelative() const {
+  if (Subtarget.isTargetWin64())
+    return true;
+  return TargetLowering::isJumpTableRelative();
+}
+
 bool X86TargetLowering::useSoftFloat() const {
   return Subtarget.useSoftFloat();
 }
@@ -1948,11 +1958,19 @@ X86TargetLowering::LowerCustomJumpTableEntry(const MachineJumpTableInfo *MJTI,
 /// Returns relocation base for the given PIC jumptable.
 SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
                                                     SelectionDAG &DAG) const {
+  // COFF doesn't have relocations to take the difference between two arbitrary
+  // symbols. The assembler, however, can resolve a fixup between the function
+  // entry and a basic block label, so use the function entry as the base.
+  if (Subtarget.isTargetWin64())
+    return DAG.getGlobalAddress(DAG.getMachineFunction().getFunction(), SDLoc(),
+                                getPointerTy(DAG.getDataLayout()));
+
   if (!Subtarget.is64Bit())
     // This doesn't have SDLoc associated with it, but is not really the
     // same as a Register.
     return DAG.getNode(X86ISD::GlobalBaseReg, SDLoc(),
                        getPointerTy(DAG.getDataLayout()));
+
   return Table;
 }
 
@@ -1961,6 +1979,13 @@ SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
 const MCExpr *X86TargetLowering::
 getPICJumpTableRelocBaseExpr(const MachineFunction *MF, unsigned JTI,
                              MCContext &Ctx) const {
+  // COFF doesn't have relocations to take the difference between two arbitrary
+  // symbols. The assembler, however, can resolve a fixup between the function
+  // entry and a basic block label, so use the function entry as the base.
+  if (Subtarget.isTargetWin64())
+    return MCSymbolRefExpr::create(
+        getTargetMachine().getSymbol(MF->getFunction()), Ctx);
+
   // X86-64 uses RIP relative addressing based on the jump table label.
   if (Subtarget.isPICStyleRIPRel())
     return TargetLowering::getPICJumpTableRelocBaseExpr(MF, JTI, Ctx);
index 37f9353042b188297a9664163778e205f3b65631..643727d7aed0fe037ef2b13e609a5a76cee5e55e 100644 (file)
@@ -684,6 +684,7 @@ namespace llvm {
                                const X86Subtarget &STI);
 
     unsigned getJumpTableEncoding() const override;
+    bool isJumpTableRelative() const override;
     bool useSoftFloat() const override;
 
     MVT getScalarShiftAmountTy(const DataLayout &, EVT) const override {
index 6bb9d64c05c01b0637ab24f3473128c70c88d11f..4fd24918864d4149c9ce60349e650c64f18209f5 100644 (file)
@@ -1,7 +1,5 @@
-; RUN: llc < %s -relocation-model static | FileCheck %s
-
-; FIXME: Remove '-relocation-model static' when it is no longer necessary to
-; trigger the separate .rdata section.
+; RUN: llc < %s -relocation-model static | FileCheck %s --check-prefix=CHECK --check-prefix=STATIC
+; RUN: llc < %s -relocation-model pic | FileCheck %s --check-prefix=CHECK --check-prefix=PIC
 
 target datalayout = "e-m:w-i64:64-f80:128-n8:16:32:64-S128"
 target triple = "x86_64-pc-windows-msvc19.0.24215"
@@ -41,7 +39,17 @@ declare void @g(i32)
 ; CHECK: .text
 ; CHECK: f:
 ; CHECK: .seh_proc f
-; CHECK: jmpq    *.LJTI0_0
+
+; STATIC: movslq .LJTI0_0(,%{{.*}},4), %[[target:[^ ]*]]
+; STATIC: leaq f(%[[target]]), %[[target]]
+; STATIC: jmpq *%[[target]]
+
+; PIC: leaq .LJTI0_0(%rip), %[[jt:[^ ]*]]
+; PIC: movslq (%[[jt]],%{{.*}},4), %[[offset:[^ ]*]]
+; PIC: leaq f(%rip), %[[base:[^ ]*]]
+; PIC: addq %[[offset]], %[[base]]
+; PIC: jmpq *%[[base]]
+
 ; CHECK: .LBB0_{{.*}}: # %sw.bb
 ; CHECK: .LBB0_{{.*}}: # %sw.bb1
 ; CHECK: .LBB0_{{.*}}: # %sw.bb2
@@ -49,10 +57,10 @@ declare void @g(i32)
 ; CHECK: callq g
 ; CHECK: jmp g # TAILCALL
 ; CHECK: .section        .rdata,"dr"
-; CHECK: .quad .LBB0_
-; CHECK: .quad .LBB0_
-; CHECK: .quad .LBB0_
-; CHECK: .quad .LBB0_
+; CHECK: .long .LBB0_{{.*}}-f
+; CHECK: .long .LBB0_{{.*}}-f
+; CHECK: .long .LBB0_{{.*}}-f
+; CHECK: .long .LBB0_{{.*}}-f
 ; CHECK: .seh_handlerdata
 
 ; It's important that we switch back to .text here, not .rdata.