[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

llvm-svn: 254146
This commit is contained in:
Igor Kudrin 2015-11-26 10:05:24 +00:00
parent fb419e71f4
commit fea8ed50ef
13 changed files with 134 additions and 79 deletions

View File

@ -56,6 +56,13 @@ template <unsigned N> static void checkUInt(uint64_t V, uint32_t Type) {
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(uint8_t *Loc, uint8_t *BufEnd,
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(uint8_t *Loc, uint8_t *BufEnd,
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:

View File

@ -0,0 +1,2 @@
.global foo
foo = 255

View File

@ -0,0 +1,2 @@
.global foo
foo = 256

View File

@ -0,0 +1,2 @@
.global foo
foo = 257

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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",@progbits
.hword sym+12
.section .R_AARCH64_ABS32, "ax",@progbits
.word sym+24
.section .R_AARCH64_ABS64, "ax",@progbits
.xword sym+36
.xword sym + 36
// CHECK: Disassembly of section .R_AARCH64_ABS16:
// CHECK-NEXT: $d.0:
// CHECK-NEXT: 1000: 0c 00 .short
// ^-- A = 0xc
// CHECK: Contents of section .R_AARCH64_ABS64:
// CHECK-NEXT: 1000 24000000 00000000
// ^-- A = 0x24
// CHECK-NEXT: Disassembly of section .R_AARCH64_ABS32:
// CHECK-NEXT: $d.1:
// CHECK-NEXT: 1002: 18 00 00 00 .word
// ^-- A = 0x18
// 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",@progbits
.hword sym - . + 12
.section .R_AARCH64_PREL32, "ax",@progbits
.word sym - . + 24
.section .R_AARCH64_PREL64, "ax",@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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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