Fix section relocation for SECTIONREL32 with immediate offset.
authorRafael Espindola <rafael.espindola@gmail.com>
Thu, 25 Apr 2013 19:27:05 +0000 (19:27 +0000)
committerRafael Espindola <rafael.espindola@gmail.com>
Thu, 25 Apr 2013 19:27:05 +0000 (19:27 +0000)
Patch by Kai Nacke. This matches the gnu as output.

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

lib/MC/MCExpr.cpp
lib/Target/X86/MCTargetDesc/X86MCCodeEmitter.cpp
test/MC/COFF/secrel-variant.s [new file with mode: 0644]

index d54c26418340843f0a39e487b7679430cdc7cec5..6cde26cff1767e847617420e589f65bb5d526039 100644 (file)
@@ -288,6 +288,8 @@ MCSymbolRefExpr::getVariantKindForName(StringRef Name) {
     .Case("tlvp", VK_TLVP)
     .Case("IMGREL", VK_COFF_IMGREL32)
     .Case("imgrel", VK_COFF_IMGREL32)
+    .Case("SECREL32", VK_SECREL)
+    .Case("secrel32", VK_SECREL)
     .Default(VK_Invalid);
 }
 
index 182bec1e84155e65b64975b85cf92a6d5973bb29..016af71501aaf0564a516a89ffc6068154bf3bd5 100644 (file)
@@ -237,6 +237,14 @@ StartsWithGlobalOffsetTable(const MCExpr *Expr) {
   return GOT_Normal;
 }
 
+static bool HasSecRelSymbolRef(const MCExpr *Expr) {
+  if (Expr->getKind() == MCExpr::SymbolRef) {
+    const MCSymbolRefExpr *Ref = static_cast<const MCSymbolRefExpr*>(Expr);
+    return Ref->getKind() == MCSymbolRefExpr::VK_SECREL;
+  }
+  return false;
+}
+
 void X86MCCodeEmitter::
 EmitImmediate(const MCOperand &DispOp, SMLoc Loc, unsigned Size,
               MCFixupKind FixupKind, unsigned &CurByte, raw_ostream &OS,
@@ -268,8 +276,13 @@ EmitImmediate(const MCOperand &DispOp, SMLoc Loc, unsigned Size,
       if (Kind == GOT_Normal)
         ImmOffset = CurByte;
     } else if (Expr->getKind() == MCExpr::SymbolRef) {
-      const MCSymbolRefExpr *Ref = static_cast<const MCSymbolRefExpr*>(Expr);
-      if (Ref->getKind() == MCSymbolRefExpr::VK_SECREL) {
+      if (HasSecRelSymbolRef(Expr)) {
+        FixupKind = MCFixupKind(FK_SecRel_4);
+      }
+    } else if (Expr->getKind() == MCExpr::Binary) {
+      const MCBinaryExpr *Bin = static_cast<const MCBinaryExpr*>(Expr);
+      if (HasSecRelSymbolRef(Bin->getLHS())
+          || HasSecRelSymbolRef(Bin->getRHS())) {
         FixupKind = MCFixupKind(FK_SecRel_4);
       }
     }
diff --git a/test/MC/COFF/secrel-variant.s b/test/MC/COFF/secrel-variant.s
new file mode 100644 (file)
index 0000000..1061bd4
--- /dev/null
@@ -0,0 +1,19 @@
+// COFF section-relative relocations
+
+// RUN: llvm-mc -filetype=obj -triple x86_64-pc-win32 %s | llvm-readobj -r | FileCheck %s
+
+.data
+values:
+    .long 1
+    .long 0
+
+.text
+    movq    values@SECREL32(%rax), %rcx
+    movq    values@SECREL32+8(%rax), %rax
+
+// CHECK:      Relocations [
+// CHECK-NEXT:   Section (1) .text {
+// CHECK-NEXT:     0x3 IMAGE_REL_AMD64_SECREL values
+// CHECK-NEXT:     0xA IMAGE_REL_AMD64_SECREL values
+// CHECK-NEXT:   }
+// CHECK-NEXT: ]