[PATCH] D88085: AArch64: avoid error when truncating MCExprs

Tim Northover via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 22 03:51:25 PDT 2020


t.p.northover created this revision.
Herald added subscribers: danielkiss, hiraditya, kristof.beyls, mcrosier.
Herald added a project: LLVM.
t.p.northover requested review of this revision.

A while back, a diagnostic was added to object file generation codepath for when an `MCExpr` overflows its size. So for assembly something like

  .long a - b + 5000000000

(except produced from CodeGen because the assembler would reject that). Unfortunately I don't think it's really valid when looked at from the IR level. We've seen an obfuscator (naturally) produce code looking something like this:

  @other = global i32 42
  @var = global i32 sub(i32 646102975,
                        i32 add (i32 trunc(i64 sub(i64 ptrtoint(i32* @var to i64),
                                                           i64 ptrtoint(i32* @other to i64)) to i32),
                                 i32 3432360802))

where the resulting `SymB - SymA + Val` has a `Val` outside the 32-bit range (because `MCExpr`s are always evaluated at 64-bits precision). But LLVM IR is 2s-complement unless you add no-wrap flags, so I think that calculation is legitimate and has to be allowed.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D88085

Files:
  llvm/lib/Target/AArch64/MCTargetDesc/AArch64AsmBackend.cpp


Index: llvm/lib/Target/AArch64/MCTargetDesc/AArch64AsmBackend.cpp
===================================================================
--- llvm/lib/Target/AArch64/MCTargetDesc/AArch64AsmBackend.cpp
+++ llvm/lib/Target/AArch64/MCTargetDesc/AArch64AsmBackend.cpp
@@ -154,19 +154,6 @@
   return (hi19 << 5) | (lo2 << 29);
 }
 
-static bool valueFitsIntoFixupKind(unsigned Kind, uint64_t Value) {
-  unsigned NumBits;
-  switch(Kind) {
-  case FK_Data_1: NumBits = 8; break;
-  case FK_Data_2: NumBits = 16; break;
-  case FK_Data_4: NumBits = 32; break;
-  case FK_Data_8: NumBits = 64; break;
-  default: return true;
-  }
-  return isUIntN(NumBits, Value) ||
-    isIntN(NumBits, static_cast<int64_t>(Value));
-}
-
 static uint64_t adjustFixupValue(const MCFixup &Fixup, const MCValue &Target,
                                  uint64_t Value, MCContext &Ctx,
                                  const Triple &TheTriple, bool IsResolved) {
@@ -341,9 +328,6 @@
   case FK_Data_2:
   case FK_Data_4:
   case FK_Data_8:
-    if (!valueFitsIntoFixupKind(Fixup.getTargetKind(), Value))
-      Ctx.reportError(Fixup.getLoc(), "fixup value too large for data type!");
-    LLVM_FALLTHROUGH;
   case FK_SecRel_2:
   case FK_SecRel_4:
     return Value;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D88085.293405.patch
Type: text/x-patch
Size: 1241 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200922/135f158e/attachment.bin>


More information about the llvm-commits mailing list