Skip to content

Commit

Permalink
[ELF/AArch64] Allow only valid dynamic relocations in the output.
Browse files Browse the repository at this point in the history
All relocations, which cannot be handled by the dynamic linker,
cause a linking error "rebuild with -fPIC".

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

llvm-svn: 254840
  • Loading branch information
Igor Kudrin committed Dec 5, 2015
1 parent a569e24 commit cfe47f5
Show file tree
Hide file tree
Showing 18 changed files with 167 additions and 55 deletions.
9 changes: 9 additions & 0 deletions lld/ELF/Target.cpp
Expand Up @@ -143,6 +143,7 @@ class PPC64TargetInfo final : public TargetInfo {
class AArch64TargetInfo final : public TargetInfo {
public:
AArch64TargetInfo();
unsigned getDynReloc(unsigned Type) const override;
unsigned getPltRefReloc(unsigned Type) const override;
void writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const override;
void writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
Expand Down Expand Up @@ -907,6 +908,14 @@ AArch64TargetInfo::AArch64TargetInfo() {
PltZeroEntrySize = 32;
}

unsigned AArch64TargetInfo::getDynReloc(unsigned Type) const {
if (Type == R_AARCH64_ABS32 || Type == R_AARCH64_ABS64)
return Type;
StringRef S = getELFRelocationTypeName(EM_AARCH64, Type);
error("Relocation " + S + " cannot be used when making a shared object; "
"recompile with -fPIC.");
}

unsigned AArch64TargetInfo::getPltRefReloc(unsigned Type) const { return Type; }

void AArch64TargetInfo::writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const {
Expand Down
20 changes: 11 additions & 9 deletions lld/test/ELF/aarch64-abs16.s
Expand Up @@ -4,22 +4,24 @@
// 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

.globl _start
_start:
.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
// RUN: ld.lld %t.o %t256.o -o %t2
// RUN: llvm-objdump -s -section=.data %t2 | 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
// 11000: S = 0x100, A = 0xfeff
// S + A = 0xffff
// 11002: S = 0x100, A = -0x8100
// S + A = 0x8000
// CHECK-NEXT: 11000 ffff0080

// RUN: not ld.lld -shared %t.o %t255.o -o %t.so
// RUN: not ld.lld %t.o %t255.o -o %t2
// | FileCheck %s --check-prefix=OVERFLOW
// RUN: not ld.lld -shared %t.o %t257.o -o %t.so
// RUN: not ld.lld %t.o %t257.o -o %t2
// | FileCheck %s --check-prefix=OVERFLOW
// OVERFLOW: Relocation R_AARCH64_ABS16 out of range
20 changes: 11 additions & 9 deletions lld/test/ELF/aarch64-abs32.s
Expand Up @@ -4,22 +4,24 @@
// 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

.globl _start
_start:
.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
// RUN: ld.lld %t.o %t256.o -o %t2
// RUN: llvm-objdump -s -section=.data %t2 | 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
// 11000: S = 0x100, A = 0xfffffeff
// S + A = 0xffffffff
// 11004: S = 0x100, A = -0x80000100
// S + A = 0x80000000
// CHECK-NEXT: 11000 ffffffff 00000080

// RUN: not ld.lld -shared %t.o %t255.o -o %t.so
// RUN: not ld.lld %t.o %t255.o -o %t2
// | FileCheck %s --check-prefix=OVERFLOW
// RUN: not ld.lld -shared %t.o %t257.o -o %t.so
// RUN: not ld.lld %t.o %t257.o -o %t2
// | FileCheck %s --check-prefix=OVERFLOW
// OVERFLOW: Relocation R_AARCH64_ABS32 out of range
21 changes: 12 additions & 9 deletions lld/test/ELF/aarch64-data-relocs.s
@@ -1,20 +1,23 @@
// RUN: llvm-mc -filetype=obj -triple=aarch64-none-freebsd %s -o %t
// RUN: ld.lld -shared %t -o %t2
// RUN: llvm-mc -filetype=obj -triple=aarch64-none-freebsd %S/Inputs/abs256.s -o %t256.o
// RUN: ld.lld %t %t256.o -o %t2
// RUN: llvm-objdump -s %t2 | FileCheck %s
// REQUIRES: aarch64

.globl _start
_start:
.section .R_AARCH64_ABS64, "ax",@progbits
.xword sym + 36
.xword foo + 0x24

// S = 0x100, A = 0x24
// S + A = 0x124
// CHECK: Contents of section .R_AARCH64_ABS64:
// CHECK-NEXT: 1000 24000000 00000000
// ^-- A = 0x24
// CHECK-NEXT: 11000 24010000 00000000

.section .R_AARCH64_PREL64, "ax",@progbits
.xword sym - . + 36
.xword foo - . + 0x24

// S + A = 0x24
// P = 0x1008
// SA - P = 0xfffffffffffff01c
// S = 0x100, A = 0x24, P = 0x11008
// S + A - P = 0xfffffffffffef11c
// CHECK: Contents of section .R_AARCH64_PREL64:
// CHECK-NEXT: 1008 1cf0ffff ffffffff
// CHECK-NEXT: 11008 1cf1feff ffffffff
7 changes: 7 additions & 0 deletions lld/test/ELF/aarch64-fpic-abs16.s
@@ -0,0 +1,7 @@
// REQUIRES: aarch64
// RUN: llvm-mc -filetype=obj -triple=aarch64-none-freebsd %s -o %t.o
// RUN: not ld.lld -shared %t.o -o %t.so 2>&1 | FileCheck %s
// CHECK: Relocation R_AARCH64_ABS16 cannot be used when making a shared object; recompile with -fPIC.

.data
.hword foo
10 changes: 10 additions & 0 deletions lld/test/ELF/aarch64-fpic-add_abs_lo12_nc.s
@@ -0,0 +1,10 @@
// REQUIRES: aarch64
// RUN: llvm-mc -filetype=obj -triple=aarch64-none-freebsd %s -o %t.o
// RUN: not ld.lld -shared %t.o -o %t.so 2>&1 | FileCheck %s
// CHECK: Relocation R_AARCH64_ADD_ABS_LO12_NC cannot be used when making a shared object; recompile with -fPIC.

add x0, x0, :lo12:dat
.data
.globl dat
dat:
.word 0
10 changes: 10 additions & 0 deletions lld/test/ELF/aarch64-fpic-adr_prel_lo21.s
@@ -0,0 +1,10 @@
// REQUIRES: aarch64
// RUN: llvm-mc -filetype=obj -triple=aarch64-none-freebsd %s -o %t.o
// RUN: not ld.lld -shared %t.o -o %t.so 2>&1 | FileCheck %s
// CHECK: Relocation R_AARCH64_ADR_PREL_LO21 cannot be used when making a shared object; recompile with -fPIC.

adr x0, dat
.data
.globl dat
dat:
.word 0
10 changes: 10 additions & 0 deletions lld/test/ELF/aarch64-fpic-adr_prel_pg_hi21.s
@@ -0,0 +1,10 @@
// REQUIRES: aarch64
// RUN: llvm-mc -filetype=obj -triple=aarch64-none-freebsd %s -o %t.o
// RUN: not ld.lld -shared %t.o -o %t.so 2>&1 | FileCheck %s
// CHECK: Relocation R_AARCH64_ADR_PREL_PG_HI21 cannot be used when making a shared object; recompile with -fPIC.

adrp x0, dat
.data
.globl dat
dat:
.word 0
10 changes: 10 additions & 0 deletions lld/test/ELF/aarch64-fpic-ldst32_abs_lo12_nc.s
@@ -0,0 +1,10 @@
// REQUIRES: aarch64
// RUN: llvm-mc -filetype=obj -triple=aarch64-none-freebsd %s -o %t.o
// RUN: not ld.lld -shared %t.o -o %t.so 2>&1 | FileCheck %s
// CHECK: Relocation R_AARCH64_LDST32_ABS_LO12_NC cannot be used when making a shared object; recompile with -fPIC.

ldr s4, [x0, :lo12:dat]
.data
.globl dat
dat:
.word 0
10 changes: 10 additions & 0 deletions lld/test/ELF/aarch64-fpic-ldst64_abs_lo12_nc.s
@@ -0,0 +1,10 @@
// REQUIRES: aarch64
// RUN: llvm-mc -filetype=obj -triple=aarch64-none-freebsd %s -o %t.o
// RUN: not ld.lld -shared %t.o -o %t.so 2>&1 | FileCheck %s
// CHECK: Relocation R_AARCH64_LDST64_ABS_LO12_NC cannot be used when making a shared object; recompile with -fPIC.

ldr x0, [x0, :lo12:dat]
.data
.globl dat
dat:
.word 0
10 changes: 10 additions & 0 deletions lld/test/ELF/aarch64-fpic-ldst8_abs_lo12_nc.s
@@ -0,0 +1,10 @@
// REQUIRES: aarch64
// RUN: llvm-mc -filetype=obj -triple=aarch64-none-freebsd %s -o %t.o
// RUN: not ld.lld -shared %t.o -o %t.so 2>&1 | FileCheck %s
// CHECK: Relocation R_AARCH64_LDST8_ABS_LO12_NC cannot be used when making a shared object; recompile with -fPIC.

ldrsb x0, [x1, :lo12:dat]
.data
.globl dat
dat:
.word 0
7 changes: 7 additions & 0 deletions lld/test/ELF/aarch64-fpic-prel16.s
@@ -0,0 +1,7 @@
// REQUIRES: aarch64
// RUN: llvm-mc -filetype=obj -triple=aarch64-none-freebsd %s -o %t.o
// RUN: not ld.lld -shared %t.o -o %t.so 2>&1 | FileCheck %s
// CHECK: Relocation R_AARCH64_PREL16 cannot be used when making a shared object; recompile with -fPIC.

.data
.hword foo - .
7 changes: 7 additions & 0 deletions lld/test/ELF/aarch64-fpic-prel32.s
@@ -0,0 +1,7 @@
// REQUIRES: aarch64
// RUN: llvm-mc -filetype=obj -triple=aarch64-none-freebsd %s -o %t.o
// RUN: not ld.lld -shared %t.o -o %t.so 2>&1 | FileCheck %s
// CHECK: Relocation R_AARCH64_PREL32 cannot be used when making a shared object; recompile with -fPIC.

.data
.word foo - .
7 changes: 7 additions & 0 deletions lld/test/ELF/aarch64-fpic-prel64.s
@@ -0,0 +1,7 @@
// REQUIRES: aarch64
// RUN: llvm-mc -filetype=obj -triple=aarch64-none-freebsd %s -o %t.o
// RUN: not ld.lld -shared %t.o -o %t.so 2>&1 | FileCheck %s
// CHECK: Relocation R_AARCH64_PREL64 cannot be used when making a shared object; recompile with -fPIC.

.data
.xword foo - .
4 changes: 3 additions & 1 deletion lld/test/ELF/aarch64-hi21-error.s
@@ -1,8 +1,10 @@
// RUN: llvm-mc -filetype=obj -triple=aarch64-pc-freebsd %S/Inputs/abs.s -o %tabs
// RUN: llvm-mc -filetype=obj -triple=aarch64-pc-freebsd %s -o %t
// RUN: not ld.lld %tabs -shared %t -o %t2 2>&1 | FileCheck %s
// RUN: not ld.lld %tabs %t -o %t2 2>&1 | FileCheck %s
// REQUIRES: aarch64

.globl _start
_start:
adrp x0, big

#CHECK: R_AARCH64_ADR_PREL_PG_HI21 out of range
4 changes: 3 additions & 1 deletion lld/test/ELF/aarch64-lo21-error.s
@@ -1,8 +1,10 @@
// RUN: llvm-mc -filetype=obj -triple=aarch64-pc-freebsd %S/Inputs/abs.s -o %tabs
// RUN: llvm-mc -filetype=obj -triple=aarch64-pc-freebsd %s -o %t
// RUN: not ld.lld %tabs -shared %t -o %t2 2>&1 | FileCheck %s
// RUN: not ld.lld %tabs %t -o %t2 2>&1 | FileCheck %s
// REQUIRES: aarch64

.globl _start
_start:
adr x0, big

#CHECK: R_AARCH64_ADR_PREL_LO21 out of range
28 changes: 15 additions & 13 deletions lld/test/ELF/aarch64-prel16.s
Expand Up @@ -4,26 +4,28 @@
// 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

.globl _start
_start:
.data
.hword foo - . + 0x10f8f
.hword foo - . - 0x706e
.hword foo - . + 0x20eff
.hword foo - . + 0x8f02

// Note: If this test fails, it is probably results from
// Note: If this test fails, it probably happens because of
// 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
// if it is already fixed. Then, update addends accordingly.
// RUN: ld.lld %t.o %t256.o -o %t2
// RUN: llvm-objdump -s -section=.data %t2 | 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
// 11000: S = 0x100, A = 0x20eff, P = 0x11000
// S + A - P = 0xffff
// 11002: S = 0x100, A = 0x8f02, P = 0x11002
// S + A - P = 0x8000
// CHECK-NEXT: 11000 ffff0080

// RUN: not ld.lld -shared %t.o %t255.o -o %t.so
// RUN: not ld.lld %t.o %t255.o -o %t2
// | FileCheck %s --check-prefix=OVERFLOW
// RUN: not ld.lld -shared %t.o %t257.o -o %t.so
// RUN: not ld.lld %t.o %t257.o -o %t2
// | FileCheck %s --check-prefix=OVERFLOW
// OVERFLOW: Relocation R_AARCH64_PREL16 out of range
28 changes: 15 additions & 13 deletions lld/test/ELF/aarch64-prel32.s
Expand Up @@ -4,26 +4,28 @@
// 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

.globl _start
_start:
.data
.word foo - . + 0x100000f8f
.word foo - . - 0x7ffff06c
.word foo - . + 0x100010eff
.word foo - . - 0x7ffef0fc

// Note: If this test fails, it is probably results from
// Note: If this test fails, it probably happens because of
// 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
// if it is already fixed. Then, update addends accordingly.
// RUN: ld.lld %t.o %t256.o -o %t2
// RUN: llvm-objdump -s -section=.data %t2 | 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
// 11000: S = 0x100, A = 0x100010eff, P = 0x11000
// S + A - P = 0xffffffff
// 11004: S = 0x100, A = -0x7ffef0fc, P = 0x11004
// S + A - P = 0x80000000
// CHECK-NEXT: 11000 ffffffff 00000080

// RUN: not ld.lld -shared %t.o %t255.o -o %t.so
// RUN: not ld.lld %t.o %t255.o -o %t2
// | FileCheck %s --check-prefix=OVERFLOW
// RUN: not ld.lld -shared %t.o %t257.o -o %t.so
// RUN: not ld.lld %t.o %t257.o -o %t2
// | FileCheck %s --check-prefix=OVERFLOW
// OVERFLOW: Relocation R_AARCH64_PREL32 out of range

0 comments on commit cfe47f5

Please sign in to comment.