Skip to content

Commit

Permalink
[MC] Don't crash on modulo by zero (PR35650)
Browse files Browse the repository at this point in the history
Extension to D12776, handle modulo by zero in the same way we handle divide by zero.

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

llvm-svn: 325810
  • Loading branch information
RKSimon committed Feb 22, 2018
1 parent 925529b commit 8831f6e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
7 changes: 5 additions & 2 deletions llvm/lib/MC/MCExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -754,6 +754,7 @@ bool MCExpr::evaluateAsRelocatableImpl(MCValue &Res, const MCAssembler *Asm,
case MCBinaryExpr::Add: Result = LHS + RHS; break;
case MCBinaryExpr::And: Result = LHS & RHS; break;
case MCBinaryExpr::Div:
case MCBinaryExpr::Mod:
// 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
Expand All @@ -762,7 +763,10 @@ bool MCExpr::evaluateAsRelocatableImpl(MCValue &Res, const MCAssembler *Asm,
// change this code to emit a better diagnostic.
if (RHS == 0)
return false;
Result = LHS / RHS;
if (ABE->getOpcode() == MCBinaryExpr::Div)
Result = LHS / RHS;
else
Result = LHS % RHS;
break;
case MCBinaryExpr::EQ: Result = LHS == RHS; break;
case MCBinaryExpr::GT: Result = LHS > RHS; break;
Expand All @@ -772,7 +776,6 @@ bool MCExpr::evaluateAsRelocatableImpl(MCValue &Res, const MCAssembler *Asm,
case MCBinaryExpr::LShr: Result = uint64_t(LHS) >> uint64_t(RHS); break;
case MCBinaryExpr::LT: Result = LHS < RHS; break;
case MCBinaryExpr::LTE: Result = LHS <= RHS; break;
case MCBinaryExpr::Mod: Result = LHS % RHS; break;
case MCBinaryExpr::Mul: Result = LHS * RHS; break;
case MCBinaryExpr::NE: Result = LHS != RHS; break;
case MCBinaryExpr::Or: Result = LHS | RHS; break;
Expand Down
3 changes: 3 additions & 0 deletions llvm/test/MC/ELF/div-by-zero.s
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@

// CHECK: expected relocatable expression
.int 1/0

// CHECK: expected relocatable expression
.int 2%0

0 comments on commit 8831f6e

Please sign in to comment.