OSDN Git Service

[MC] Don't crash on division by zero.
authorDavide Italiano <davide@freebsd.org>
Fri, 11 Sep 2015 20:47:35 +0000 (20:47 +0000)
committerDavide Italiano <davide@freebsd.org>
Fri, 11 Sep 2015 20:47:35 +0000 (20:47 +0000)
Differential Revision: http://reviews.llvm.org/D12776

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

lib/MC/MCExpr.cpp
test/MC/ELF/div-by-zero.s [new file with mode: 0644]

index a30ceec..90cd7fa 100644 (file)
@@ -739,7 +739,17 @@ bool MCExpr::evaluateAsRelocatableImpl(MCValue &Res, const MCAssembler *Asm,
     case MCBinaryExpr::AShr: Result = LHS >> RHS; break;
     case MCBinaryExpr::Add:  Result = LHS + RHS; break;
     case MCBinaryExpr::And:  Result = LHS & RHS; break;
-    case MCBinaryExpr::Div:  Result = LHS / RHS; break;
+    case MCBinaryExpr::Div: {
+      // Handle division by zero. gas just emits a warning and keeps going,
+      // we try to be stricter.
+      // FIXME: Currently the caller of this function has no way to understand
+      // we're bailing out because of 'division by zero'. Therefore, it will
+      // emit a 'expected relocatable expression' error. It would be nice to
+      // change this code to emit a better diagnostic.
+      if (RHS == 0)
+        return false;
+      Result = LHS / RHS; break;
+    }
     case MCBinaryExpr::EQ:   Result = LHS == RHS; break;
     case MCBinaryExpr::GT:   Result = LHS > RHS; break;
     case MCBinaryExpr::GTE:  Result = LHS >= RHS; break;
diff --git a/test/MC/ELF/div-by-zero.s b/test/MC/ELF/div-by-zero.s
new file mode 100644 (file)
index 0000000..8c7f773
--- /dev/null
@@ -0,0 +1,6 @@
+// Check that llvm-mc doesn't crash on division by zero.
+// RUN: not llvm-mc -filetype=obj -triple x86_64-pc-linux-gnu %s 2> %t
+// RUN: FileCheck -input-file %t %s
+
+// CHECK: expected relocatable expression
+.int 1/0