[lld] r254146 - [ELF/AArch64] Fix overflow checks for R_AARCH64_{ABS, PREL}{16, 32} relocations.

Igor Kudrin via llvm-commits llvm-commits at lists.llvm.org
Thu Nov 26 02:05:26 PST 2015


Author: ikudrin
Date: Thu Nov 26 04:05:24 2015
New Revision: 254146

URL: http://llvm.org/viewvc/llvm-project?rev=254146&view=rev
Log:
[ELF/AArch64] Fix overflow checks for R_AARCH64_{ABS,PREL}{16,32} relocations.

ABI specifies the allowed range for these relocations as 2^(n-1) <= X < 2^n.

The patch fixes checks and introduces precise tests for these relocations.

Differential revision: http://reviews.llvm.org/D14957

Added:
    lld/trunk/test/ELF/Inputs/abs255.s
    lld/trunk/test/ELF/Inputs/abs256.s
    lld/trunk/test/ELF/Inputs/abs257.s
    lld/trunk/test/ELF/aarch64-abs16.s
    lld/trunk/test/ELF/aarch64-abs32.s
    lld/trunk/test/ELF/aarch64-prel16.s
    lld/trunk/test/ELF/aarch64-prel32.s
Removed:
    lld/trunk/test/ELF/aarch64-abs16-error.s
    lld/trunk/test/ELF/aarch64-abs32-error.s
    lld/trunk/test/ELF/aarch64-prel16-error.s
    lld/trunk/test/ELF/aarch64-prel32-error.s
Modified:
    lld/trunk/ELF/Target.cpp
    lld/trunk/test/ELF/aarch64-data-relocs.s

Modified: lld/trunk/ELF/Target.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Target.cpp?rev=254146&r1=254145&r2=254146&view=diff
==============================================================================
--- lld/trunk/ELF/Target.cpp (original)
+++ lld/trunk/ELF/Target.cpp Thu Nov 26 04:05:24 2015
@@ -56,6 +56,13 @@ template <unsigned N> static void checkU
   error("Relocation " + S + " out of range");
 }
 
+template <unsigned N> static void checkIntUInt(uint64_t V, uint32_t Type) {
+  if (isInt<N>(V) || isUInt<N>(V))
+    return;
+  StringRef S = getELFRelocationTypeName(Config->EMachine, Type);
+  error("Relocation " + S + " out of range");
+}
+
 template <unsigned N> static void checkAlignment(uint64_t V, uint32_t Type) {
   if ((V & (N - 1)) == 0)
     return;
@@ -933,11 +940,11 @@ void AArch64TargetInfo::relocateOne(uint
                                     uint64_t SA) const {
   switch (Type) {
   case R_AARCH64_ABS16:
-    checkInt<16>(SA, Type);
+    checkIntUInt<16>(SA, Type);
     write16le(Loc, SA);
     break;
   case R_AARCH64_ABS32:
-    checkInt<32>(SA, Type);
+    checkIntUInt<32>(SA, Type);
     write32le(Loc, SA);
     break;
   case R_AARCH64_ABS64:
@@ -990,11 +997,11 @@ void AArch64TargetInfo::relocateOne(uint
     or32le(Loc, (SA & 0xFFF) << 10);
     break;
   case R_AARCH64_PREL16:
-    checkInt<16>(SA - P, Type);
+    checkIntUInt<16>(SA - P, Type);
     write16le(Loc, SA - P);
     break;
   case R_AARCH64_PREL32:
-    checkInt<32>(SA - P, Type);
+    checkIntUInt<32>(SA - P, Type);
     write32le(Loc, SA - P);
     break;
   case R_AARCH64_PREL64:

Added: lld/trunk/test/ELF/Inputs/abs255.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/Inputs/abs255.s?rev=254146&view=auto
==============================================================================
--- lld/trunk/test/ELF/Inputs/abs255.s (added)
+++ lld/trunk/test/ELF/Inputs/abs255.s Thu Nov 26 04:05:24 2015
@@ -0,0 +1,2 @@
+.global foo
+foo = 255

Added: lld/trunk/test/ELF/Inputs/abs256.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/Inputs/abs256.s?rev=254146&view=auto
==============================================================================
--- lld/trunk/test/ELF/Inputs/abs256.s (added)
+++ lld/trunk/test/ELF/Inputs/abs256.s Thu Nov 26 04:05:24 2015
@@ -0,0 +1,2 @@
+.global foo
+foo = 256

Added: lld/trunk/test/ELF/Inputs/abs257.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/Inputs/abs257.s?rev=254146&view=auto
==============================================================================
--- lld/trunk/test/ELF/Inputs/abs257.s (added)
+++ lld/trunk/test/ELF/Inputs/abs257.s Thu Nov 26 04:05:24 2015
@@ -0,0 +1,2 @@
+.global foo
+foo = 257

Removed: lld/trunk/test/ELF/aarch64-abs16-error.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/aarch64-abs16-error.s?rev=254145&view=auto
==============================================================================
--- lld/trunk/test/ELF/aarch64-abs16-error.s (original)
+++ lld/trunk/test/ELF/aarch64-abs16-error.s (removed)
@@ -1,7 +0,0 @@
-// RUN: llvm-mc -filetype=obj -triple=aarch64-none-freebsd %s -o %t
-// RUN: not ld.lld -shared %t -o %t2 2>&1 | FileCheck %s
-// REQUIRES: aarch64
-
-.hword sym+65539
-
-// CHECK: R_AARCH64_ABS16 out of range

Added: lld/trunk/test/ELF/aarch64-abs16.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/aarch64-abs16.s?rev=254146&view=auto
==============================================================================
--- lld/trunk/test/ELF/aarch64-abs16.s (added)
+++ lld/trunk/test/ELF/aarch64-abs16.s Thu Nov 26 04:05:24 2015
@@ -0,0 +1,25 @@
+// REQUIRES: aarch64
+// RUN: llvm-mc -filetype=obj -triple=aarch64-none-freebsd %s -o %t.o
+// RUN: llvm-mc -filetype=obj -triple=aarch64-none-freebsd %S/Inputs/abs255.s -o %t255.o
+// RUN: llvm-mc -filetype=obj -triple=aarch64-none-freebsd %S/Inputs/abs256.s -o %t256.o
+// RUN: llvm-mc -filetype=obj -triple=aarch64-none-freebsd %S/Inputs/abs257.s -o %t257.o
+
+.data
+  .hword foo + 0xfeff
+  .hword foo - 0x8100
+
+// RUN: ld.lld -shared %t.o %t256.o -o %t.so
+// RUN: llvm-objdump -s -section=.data %t.so | FileCheck %s
+
+// CHECK: Contents of section .data:
+// 1090: S = 0x100, A = 0xfeff
+//       S + A = 0xffff
+// 1092: S = 0x100, A = -0x8100
+//       S + A = 0x8000
+// CHECK-NEXT: 1090 ffff0080
+
+// RUN: not ld.lld -shared %t.o %t255.o -o %t.so
+//   | FileCheck %s --check-prefix=OVERFLOW
+// RUN: not ld.lld -shared %t.o %t257.o -o %t.so
+//   | FileCheck %s --check-prefix=OVERFLOW
+// OVERFLOW: Relocation R_AARCH64_ABS16 out of range

Removed: lld/trunk/test/ELF/aarch64-abs32-error.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/aarch64-abs32-error.s?rev=254145&view=auto
==============================================================================
--- lld/trunk/test/ELF/aarch64-abs32-error.s (original)
+++ lld/trunk/test/ELF/aarch64-abs32-error.s (removed)
@@ -1,7 +0,0 @@
-// RUN: llvm-mc -filetype=obj -triple=aarch64-none-freebsd %s -o %t
-// RUN: not ld.lld -shared %t -o %t2 2>&1 | FileCheck %s
-// REQUIRES: aarch64
-
-.word sym+99999999999
-
-// CHECK: R_AARCH64_ABS32 out of range

Added: lld/trunk/test/ELF/aarch64-abs32.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/aarch64-abs32.s?rev=254146&view=auto
==============================================================================
--- lld/trunk/test/ELF/aarch64-abs32.s (added)
+++ lld/trunk/test/ELF/aarch64-abs32.s Thu Nov 26 04:05:24 2015
@@ -0,0 +1,25 @@
+// REQUIRES: aarch64
+// RUN: llvm-mc -filetype=obj -triple=aarch64-none-freebsd %s -o %t.o
+// RUN: llvm-mc -filetype=obj -triple=aarch64-none-freebsd %S/Inputs/abs255.s -o %t255.o
+// RUN: llvm-mc -filetype=obj -triple=aarch64-none-freebsd %S/Inputs/abs256.s -o %t256.o
+// RUN: llvm-mc -filetype=obj -triple=aarch64-none-freebsd %S/Inputs/abs257.s -o %t257.o
+
+.data
+  .word foo + 0xfffffeff
+  .word foo - 0x80000100
+
+// RUN: ld.lld -shared %t.o %t256.o -o %t.so
+// RUN: llvm-objdump -s -section=.data %t.so | FileCheck %s
+
+// CHECK: Contents of section .data:
+// 1090: S = 0x100, A = 0xfffffeff
+//       S + A = 0xffffffff
+// 1094: S = 0x100, A = -0x80000100
+//       S + A = 0x80000000
+// CHECK-NEXT: 1090 ffffffff 00000080
+
+// RUN: not ld.lld -shared %t.o %t255.o -o %t.so
+//   | FileCheck %s --check-prefix=OVERFLOW
+// RUN: not ld.lld -shared %t.o %t257.o -o %t.so
+//   | FileCheck %s --check-prefix=OVERFLOW
+// OVERFLOW: Relocation R_AARCH64_ABS32 out of range

Modified: lld/trunk/test/ELF/aarch64-data-relocs.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/aarch64-data-relocs.s?rev=254146&r1=254145&r2=254146&view=diff
==============================================================================
--- lld/trunk/test/ELF/aarch64-data-relocs.s (original)
+++ lld/trunk/test/ELF/aarch64-data-relocs.s Thu Nov 26 04:05:24 2015
@@ -1,58 +1,20 @@
 // RUN: llvm-mc -filetype=obj -triple=aarch64-none-freebsd %s -o %t
 // RUN: ld.lld -shared %t -o %t2
-// RUN: llvm-objdump -d %t2 | FileCheck %s
+// RUN: llvm-objdump -s %t2 | FileCheck %s
 // REQUIRES: aarch64
 
-.section .R_AARCH64_ABS16, "ax", at progbits
-  .hword sym+12
-
-.section .R_AARCH64_ABS32, "ax", at progbits
-  .word sym+24
-
 .section .R_AARCH64_ABS64, "ax", at progbits
-  .xword sym+36
-
-// CHECK: Disassembly of section .R_AARCH64_ABS16:
-// CHECK-NEXT: $d.0:
-// CHECK-NEXT:     1000:       0c 00   .short
-//                             ^-- A = 0xc
+  .xword sym + 36
 
-// CHECK-NEXT: Disassembly of section .R_AARCH64_ABS32:
-// CHECK-NEXT: $d.1:
-// CHECK-NEXT:     1002:       18 00 00 00     .word
-//                             ^-- A = 0x18
+// CHECK: Contents of section .R_AARCH64_ABS64:
+// CHECK-NEXT: 1000 24000000 00000000
+//                  ^-- A = 0x24
 
-// CHECK-NEXT: Disassembly of section .R_AARCH64_ABS64:
-// CHECK-NEXT: $d.2:
-// CHECK-NEXT:     1006:       24 00 00 00     .word
-//                             ^-- A = 0x24
-// CHECK-NEXT:     100a:       00 00 00 00     .word
-
-.section .R_AARCH64_PREL16, "ax", at progbits
-  .hword sym - . + 12
-.section .R_AARCH64_PREL32, "ax", at progbits
-  .word sym - . + 24
 .section .R_AARCH64_PREL64, "ax", at progbits
   .xword sym - . + 36
 
-// S + A = 0xc
-// P = 0x100e
-// SA - P = 0xeffe
-// CHECK: Disassembly of section .R_AARCH64_PREL16:
-// CHECK-NEXT: $d.3:
-// CHECK-NEXT:     100e:       fe ef   .short
-
-// S + A = 0x18
-// P = 0x1010
-// SA - P = 0xfffff016
-// CHECK: Disassembly of section .R_AARCH64_PREL32:
-// CHECK-NEXT: $d.4:
-// CHECK-NEXT:     1010:       08 f0 ff ff     .word
-
 // S + A = 0x24
-// P = 0x1014
-// SA - P = 0xfffffffffffff010
-// CHECK: Disassembly of section .R_AARCH64_PREL64:
-// CHECK-NEXT: $d.5:
-// CHECK-NEXT:     1014:       10 f0 ff ff     .word
-// CHECK-NEXT:     1018:       ff ff ff ff     .word
+// P = 0x1008
+// SA - P = 0xfffffffffffff01c
+// CHECK: Contents of section .R_AARCH64_PREL64:
+// CHECK-NEXT: 1008 1cf0ffff ffffffff

Removed: lld/trunk/test/ELF/aarch64-prel16-error.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/aarch64-prel16-error.s?rev=254145&view=auto
==============================================================================
--- lld/trunk/test/ELF/aarch64-prel16-error.s (original)
+++ lld/trunk/test/ELF/aarch64-prel16-error.s (removed)
@@ -1,7 +0,0 @@
-// RUN: llvm-mc -filetype=obj -triple=aarch64-none-freebsd %s -o %t
-// RUN: not ld.lld -shared %t -o %t2 2>&1 | FileCheck %s
-// REQUIRES: aarch64
-
-.hword sym + 65539 - .
-
-// CHECK: R_AARCH64_PREL16 out of range

Added: lld/trunk/test/ELF/aarch64-prel16.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/aarch64-prel16.s?rev=254146&view=auto
==============================================================================
--- lld/trunk/test/ELF/aarch64-prel16.s (added)
+++ lld/trunk/test/ELF/aarch64-prel16.s Thu Nov 26 04:05:24 2015
@@ -0,0 +1,29 @@
+// REQUIRES: aarch64
+// RUN: llvm-mc -filetype=obj -triple=aarch64-none-freebsd %s -o %t.o
+// RUN: llvm-mc -filetype=obj -triple=aarch64-none-freebsd %S/Inputs/abs255.s -o %t255.o
+// RUN: llvm-mc -filetype=obj -triple=aarch64-none-freebsd %S/Inputs/abs256.s -o %t256.o
+// RUN: llvm-mc -filetype=obj -triple=aarch64-none-freebsd %S/Inputs/abs257.s -o %t257.o
+
+.data
+  .hword foo - . + 0x10f8f
+  .hword foo - . - 0x706e
+
+// Note: If this test fails, it is probably results from
+//       the change of the address of the .data section.
+//       You may found the correct address in the aarch64_abs16.s test,
+//       if it's already fixed. Then, update addends accordingly.
+// RUN: ld.lld -shared %t.o %t256.o -o %t.so
+// RUN: llvm-objdump -s -section=.data %t.so | FileCheck %s
+
+// CHECK: Contents of section .data:
+// 1090: S = 0x100, A = 0x10f8f, P = 0x1090
+//       S + A - P = 0xffff
+// 1092: S = 0x100, A = -0x706e, P = 0x1092
+//       S + A - P = 0x8000
+// CHECK-NEXT: 1090 ffff0080
+
+// RUN: not ld.lld -shared %t.o %t255.o -o %t.so
+//   | FileCheck %s --check-prefix=OVERFLOW
+// RUN: not ld.lld -shared %t.o %t257.o -o %t.so
+//   | FileCheck %s --check-prefix=OVERFLOW
+// OVERFLOW: Relocation R_AARCH64_PREL16 out of range

Removed: lld/trunk/test/ELF/aarch64-prel32-error.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/aarch64-prel32-error.s?rev=254145&view=auto
==============================================================================
--- lld/trunk/test/ELF/aarch64-prel32-error.s (original)
+++ lld/trunk/test/ELF/aarch64-prel32-error.s (removed)
@@ -1,7 +0,0 @@
-// RUN: llvm-mc -filetype=obj -triple=aarch64-none-freebsd %s -o %t
-// RUN: not ld.lld -shared %t -o %t2 2>&1 | FileCheck %s
-// REQUIRES: aarch64
-
-.word sym + 99999999999 - .
-
-// CHECK: R_AARCH64_PREL32 out of range

Added: lld/trunk/test/ELF/aarch64-prel32.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/aarch64-prel32.s?rev=254146&view=auto
==============================================================================
--- lld/trunk/test/ELF/aarch64-prel32.s (added)
+++ lld/trunk/test/ELF/aarch64-prel32.s Thu Nov 26 04:05:24 2015
@@ -0,0 +1,29 @@
+// REQUIRES: aarch64
+// RUN: llvm-mc -filetype=obj -triple=aarch64-none-freebsd %s -o %t.o
+// RUN: llvm-mc -filetype=obj -triple=aarch64-none-freebsd %S/Inputs/abs255.s -o %t255.o
+// RUN: llvm-mc -filetype=obj -triple=aarch64-none-freebsd %S/Inputs/abs256.s -o %t256.o
+// RUN: llvm-mc -filetype=obj -triple=aarch64-none-freebsd %S/Inputs/abs257.s -o %t257.o
+
+.data
+  .word foo - . + 0x100000f8f
+  .word foo - . - 0x7ffff06c
+
+// Note: If this test fails, it is probably results from
+//       the change of the address of the .data section.
+//       You may found the correct address in the aarch64_abs32.s test,
+//       if it's already fixed. Then, update addends accordingly.
+// RUN: ld.lld -shared %t.o %t256.o -o %t.so
+// RUN: llvm-objdump -s -section=.data %t.so | FileCheck %s
+
+// CHECK: Contents of section .data:
+// 1090: S = 0x100, A = 0x100000f8f, P = 0x1090
+//       S + A - P = 0xffffffff
+// 1094: S = 0x100, A = -0x7ffff06c, P = 0x1094
+//       S + A - P = 0x80000000
+// CHECK-NEXT: 1090 ffffffff 00000080
+
+// RUN: not ld.lld -shared %t.o %t255.o -o %t.so
+//   | FileCheck %s --check-prefix=OVERFLOW
+// RUN: not ld.lld -shared %t.o %t257.o -o %t.so
+//   | FileCheck %s --check-prefix=OVERFLOW
+// OVERFLOW: Relocation R_AARCH64_PREL32 out of range




More information about the llvm-commits mailing list