[mips] Fix off by one issue when applying a fixup.
authorMatheus Almeida <matheus.almeida@imgtec.com>
Tue, 17 Dec 2013 17:10:00 +0000 (17:10 +0000)
committerMatheus Almeida <matheus.almeida@imgtec.com>
Tue, 17 Dec 2013 17:10:00 +0000 (17:10 +0000)
The branch offset for a R_MIPS_PC16 relocation is indeed a 16-bit signed
immediate.

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

lib/Target/Mips/MCTargetDesc/MipsAsmBackend.cpp
test/MC/Mips/micromips-pc16-fixup.s [new file with mode: 0644]
test/MC/Mips/mips-pc16-fixup.s [new file with mode: 0644]

index 81d976a34cdab20bf957b6b1087e22475ec79497..25ce9c7365ca05a42c9af70df61710b6024dc250 100644 (file)
@@ -64,7 +64,7 @@ static unsigned adjustFixupValue(const MCFixup &Fixup, uint64_t Value,
     // address range. Forcing a signed division because Value can be negative.
     Value = (int64_t)Value / 4;
     // We now check if Value can be encoded as a 16-bit signed immediate.
-    if (!isIntN(15, Value) && Ctx)
+    if (!isIntN(16, Value) && Ctx)
       Ctx->FatalError(Fixup.getLoc(), "out of range PC16 fixup");
     break;
   case Mips::fixup_Mips_26:
@@ -97,7 +97,7 @@ static unsigned adjustFixupValue(const MCFixup &Fixup, uint64_t Value,
     // Forcing a signed division because Value can be negative.
     Value = (int64_t)Value / 2;
     // We now check if Value can be encoded as a 16-bit signed immediate.
-    if (!isIntN(15, Value) && Ctx)
+    if (!isIntN(16, Value) && Ctx)
       Ctx->FatalError(Fixup.getLoc(), "out of range PC16 fixup");
     break;
   }
diff --git a/test/MC/Mips/micromips-pc16-fixup.s b/test/MC/Mips/micromips-pc16-fixup.s
new file mode 100644 (file)
index 0000000..146a155
--- /dev/null
@@ -0,0 +1,10 @@
+# RUN: llvm-mc %s -triple=mips-unknown-linux -mcpu=mips32r2 -arch=mips -mattr=+micromips 2>&1 -filetype=obj | FileCheck %s
+#
+# CHECK-NOT: LLVM ERROR: out of range PC16 fixup
+
+.text
+  b foo
+  .space 65536 - 8, 1   # -8 = size of b instr plus size of automatically inserted nop
+foo:
+  add $0,$0,$0
+
diff --git a/test/MC/Mips/mips-pc16-fixup.s b/test/MC/Mips/mips-pc16-fixup.s
new file mode 100644 (file)
index 0000000..5443532
--- /dev/null
@@ -0,0 +1,10 @@
+# RUN: llvm-mc %s -triple=mips-unknown-linux -mcpu=mips32r2 -arch=mips 2>&1 -filetype=obj | FileCheck %s
+#
+# CHECK-NOT: LLVM ERROR: out of range PC16 fixup
+
+.text
+  b foo
+  .space 131072 - 8, 1  # -8 = size of b instr plus size of automatically inserted nop
+foo:
+  add $0,$0,$0
+