[PATCH] D13915: Properly evaluate MCBinaryExpr with a constant on one side.

Colin LeMahieu via llvm-commits llvm-commits at lists.llvm.org
Tue Oct 20 14:28:36 PDT 2015


colinl created this revision.
colinl added reviewers: rafael, sidneym.
colinl added a subscriber: llvm-commits.
colinl set the repository for this revision to rL LLVM.

Previously these tests would fail with:
LLVM ERROR: expected relocatable expression

The logic is changed so EvaluateSymbolicAdd is only called if both sides have a relocatable expression.

If one side is a relocatable it combines the addends and creates a single relocatable expression.

Repository:
  rL LLVM

http://reviews.llvm.org/D13915

Files:
  lib/MC/MCExpr.cpp
  test/MC/ARM/dot-symbol-relocation.s

Index: test/MC/ARM/dot-symbol-relocation.s
===================================================================
--- test/MC/ARM/dot-symbol-relocation.s
+++ test/MC/ARM/dot-symbol-relocation.s
@@ -0,0 +1,16 @@
+# RUN: llvm-mc -filetype=obj -triple=arm -o - %s | \
+# RUN: llvm-objdump -triple=arm -r -s - | FileCheck %s
+
+# CHECK: 00000004 R_ARM_ABS32 .text
+# CHECK: 00000008 R_ARM_ABS32 .text
+# CHECK: 0000000c R_ARM_ABS32 .text
+# CHECK: 00000010 R_ARM_ABS32 .text
+# CHECK: 00000014 R_ARM_ABS32 .text
+# CHECK: 42000000 46000000 c6ffffff 4e000000
+# CHECK: 52000000 1c010000
+.word 0x42
+.word 0x42 - .
+.word . - 0x42
+.word 0x42 + .
+.word . + 0x42
+.word 0x42 + 0x42 + . + 0x42 + 0x42
\ No newline at end of file
Index: lib/MC/MCExpr.cpp
===================================================================
--- lib/MC/MCExpr.cpp
+++ lib/MC/MCExpr.cpp
@@ -709,10 +709,12 @@
         !ABE->getRHS()->evaluateAsRelocatableImpl(RHSValue, Asm, Layout, Fixup,
                                                   Addrs, InSet))
       return false;
+    bool LHSAbs = LHSValue.isAbsolute();
+    bool RHSAbs = RHSValue.isAbsolute();
 
     // We only support a few operations on non-constant expressions, handle
     // those first.
-    if (!LHSValue.isAbsolute() || !RHSValue.isAbsolute()) {
+    if (!LHSAbs && !RHSAbs) {
       switch (ABE->getOpcode()) {
       default:
         return false;
@@ -766,6 +768,21 @@
     case MCBinaryExpr::Xor:  Result = LHS ^ RHS; break;
     }
 
+    // One side evaluates as absolute, adjust the relocatable addend
+    if (LHSAbs ^ RHSAbs) {
+      MCValue &RelValue = LHSAbs ? RHSValue : LHSValue;
+      switch (ABE->getOpcode()) {
+      default:
+        return false;
+      case MCBinaryExpr::Sub:
+      case MCBinaryExpr::Add:
+        break;
+      }
+      Res = MCValue::get(RelValue.getSymA(), RelValue.getSymB(),
+                         Result, RelValue.getRefKind());
+      return true;
+    }
+
     Res = MCValue::get(Result);
     return true;
   }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D13915.37919.patch
Type: text/x-patch
Size: 2011 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20151020/82728583/attachment.bin>


More information about the llvm-commits mailing list