]> granicus.if.org Git - llvm/commitdiff
[MC][MachO] Emit an error for emitting relocations of the form -SYM + cst
authorFrancis Visoiu Mistrih <francisvm@yahoo.com>
Wed, 6 Mar 2019 18:10:41 +0000 (18:10 +0000)
committerFrancis Visoiu Mistrih <francisvm@yahoo.com>
Wed, 6 Mar 2019 18:10:41 +0000 (18:10 +0000)
Emit an error for an unsupported relocation. mach-o relocations can't
encode the form -SYM + cst.

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

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

lib/MC/MachObjectWriter.cpp
test/MC/MachO/bad-reloc.s [new file with mode: 0644]

index 395273e44f6f8e4f2f071f24da57d28a965d2ada..f0ceb86b25af4201dd2bf0987c75f93be5a07e05 100644 (file)
@@ -13,6 +13,7 @@
 #include "llvm/MC/MCAsmBackend.h"
 #include "llvm/MC/MCAsmLayout.h"
 #include "llvm/MC/MCAssembler.h"
+#include "llvm/MC/MCContext.h"
 #include "llvm/MC/MCDirectives.h"
 #include "llvm/MC/MCExpr.h"
 #include "llvm/MC/MCFixupKindInfo.h"
@@ -448,11 +449,25 @@ void MachObjectWriter::writeLinkerOptionsLoadCommand(
   assert(W.OS.tell() - Start == Size);
 }
 
+static bool isFixupTargetValid(const MCValue &Target) {
+  // Target is (LHS - RHS + cst).
+  // We don't support the form where LHS is null: -RHS + cst
+  if (!Target.getSymA() && Target.getSymB())
+    return false;
+  return true;
+}
+
 void MachObjectWriter::recordRelocation(MCAssembler &Asm,
                                         const MCAsmLayout &Layout,
                                         const MCFragment *Fragment,
                                         const MCFixup &Fixup, MCValue Target,
                                         uint64_t &FixedValue) {
+  if (!isFixupTargetValid(Target)) {
+    Asm.getContext().reportError(Fixup.getLoc(),
+                                 "unsupported relocation expression");
+    return;
+  }
+
   TargetObjectWriter->recordRelocation(this, Asm, Layout, Fragment, Fixup,
                                        Target, FixedValue);
 }
diff --git a/test/MC/MachO/bad-reloc.s b/test/MC/MachO/bad-reloc.s
new file mode 100644 (file)
index 0000000..2c591c4
--- /dev/null
@@ -0,0 +1,5 @@
+// RUN: not llvm-mc -triple x86_64-apple-darwin %s -filetype=obj -o - 2>&1 | FileCheck %s
+
+.quad (0 - undef)
+
+// CHECK: error: unsupported relocation expression