Skip to content

Commit d4695e1

Browse files
committed
[ELF][AArch64] Support for movz, movk tprel relocations
This patch Implements the R_AARCH64_TLSLE_MOVW_TPREL_G*[_NC]. These are logically the same calculation as the existing TLSLE relocations with the result written back to mov[nz] and movk instructions. A typical code sequence is: movz x0, #:tprel_g2:foo // bits [47:32] of R_TLS with overflow check movk x0, #:tprel_g1_nc:foo // bits [31:16] of R_TLS with no overflow check movk x0, #:tprel_g0_nc:foo // bits [15:0] of R_TLS with no overflow check This type of code sequence is usually used with a large code model. Differential Revision: https://reviews.llvm.org/D65882 Fixes: PR42853 llvm-svn: 368293
1 parent 59fabf9 commit d4695e1

File tree

3 files changed

+94
-0
lines changed

3 files changed

+94
-0
lines changed

lld/ELF/Arch/AArch64.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,11 @@ RelExpr AArch64::getRelExpr(RelType type, const Symbol &s,
9090
case R_AARCH64_TLSLE_LDST32_TPREL_LO12_NC:
9191
case R_AARCH64_TLSLE_LDST64_TPREL_LO12_NC:
9292
case R_AARCH64_TLSLE_LDST128_TPREL_LO12_NC:
93+
case R_AARCH64_TLSLE_MOVW_TPREL_G0:
94+
case R_AARCH64_TLSLE_MOVW_TPREL_G0_NC:
95+
case R_AARCH64_TLSLE_MOVW_TPREL_G1:
96+
case R_AARCH64_TLSLE_MOVW_TPREL_G1_NC:
97+
case R_AARCH64_TLSLE_MOVW_TPREL_G2:
9398
return R_TLS;
9499
case R_AARCH64_CALL26:
95100
case R_AARCH64_CONDBR19:
@@ -376,20 +381,25 @@ void AArch64::relocateOne(uint8_t *loc, RelType type, uint64_t val) const {
376381
break;
377382
case R_AARCH64_MOVW_PREL_G0:
378383
case R_AARCH64_MOVW_SABS_G0:
384+
case R_AARCH64_TLSLE_MOVW_TPREL_G0:
379385
checkInt(loc, val, 17, type);
380386
LLVM_FALLTHROUGH;
381387
case R_AARCH64_MOVW_PREL_G0_NC:
388+
case R_AARCH64_TLSLE_MOVW_TPREL_G0_NC:
382389
writeSMovWImm(loc, val);
383390
break;
384391
case R_AARCH64_MOVW_PREL_G1:
385392
case R_AARCH64_MOVW_SABS_G1:
393+
case R_AARCH64_TLSLE_MOVW_TPREL_G1:
386394
checkInt(loc, val, 33, type);
387395
LLVM_FALLTHROUGH;
388396
case R_AARCH64_MOVW_PREL_G1_NC:
397+
case R_AARCH64_TLSLE_MOVW_TPREL_G1_NC:
389398
writeSMovWImm(loc, val >> 16);
390399
break;
391400
case R_AARCH64_MOVW_PREL_G2:
392401
case R_AARCH64_MOVW_SABS_G2:
402+
case R_AARCH64_TLSLE_MOVW_TPREL_G2:
393403
checkInt(loc, val, 49, type);
394404
LLVM_FALLTHROUGH;
395405
case R_AARCH64_MOVW_PREL_G2_NC:

lld/test/ELF/aarch64-movw-error.s

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,22 @@ movn x0, #:prel_g0:.-0x10001
3434
movn x0, #:prel_g1:.-0x100010000
3535
# CHECK: relocation R_AARCH64_MOVW_PREL_G2 out of range: -281479271677952 is not in [-281474976710656, 281474976710655]
3636
movn x0, #:prel_g2:.-0x1000100000000
37+
38+
movz x0, #:tprel_g0: v1
39+
# CHECK: relocation R_AARCH64_TLSLE_MOVW_TPREL_G0 out of range: 65552 is not in [-65536, 65535]
40+
movz x0, #:tprel_g1: v2
41+
# CHECK: relocation R_AARCH64_TLSLE_MOVW_TPREL_G1 out of range: 4295032848 is not in [-4294967296, 4294967295]
42+
movz x0, #:tprel_g2: v3
43+
# CHECK: relocation R_AARCH64_TLSLE_MOVW_TPREL_G2 out of range: 281479271743496 is not in [-281474976710656, 281474976710655]
44+
45+
.section .tbss,"awT",@nobits
46+
.balign 16
47+
.space 0x10000
48+
v1:
49+
.quad 0
50+
.space 0x100000000 - 8
51+
v2:
52+
.quad 0
53+
.space 0x1000000000000 - 16
54+
v3:
55+
.quad 0

lld/test/ELF/aarch64-movw-tprel.s

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# REQUIRES: aarch64
2+
# RUN: llvm-mc -filetype=obj -triple=aarch64-linux-gnu %s -o %t.o
3+
# RUN: ld.lld %t.o -o %t
4+
# RUN: llvm-objdump -d %t
5+
# RUN: llvm-readobj --symbols %t
6+
# RUN: llvm-objdump --no-show-raw-insn -d %t | FileCheck %s
7+
# RUN: llvm-readobj --symbols %t | FileCheck --check-prefix=CHECK-SYM %s
8+
9+
## Test the the local exec relocations that map to:
10+
## R_AARCH64_TLSLE_MOVW_TPREL_G2
11+
## R_AARCH64_TLSLE_MOVW_TPREL_G1
12+
## R_AARCH64_TLSLE_MOVW_TPREL_G1_NC
13+
## R_AARCH64_TLSLE_MOVW_TPREL_G0
14+
## R_AARCH64_TLSLE_MOVW_TPREL_G0_NC
15+
## They calculate the same value as the other TPREL relocations, namely the
16+
## offset from the thread pointer TP. The G0, G1 and G2 refer to partitions
17+
## of the result with G2 bits [47:32], G1 bits [31:16] and G0 bits [15:0]
18+
## the NC variant does not check for overflow.
19+
## In AArch64 the structure of the TLS at runtime is:
20+
## | TCB | Alignment Padding | TLS Block |
21+
## With TP pointing to the start of the TCB. All offsets will be positive.
22+
23+
.text
24+
## Access variable in first partition
25+
movz x0, #:tprel_g0:v0
26+
## TCB + 0 == 16
27+
# CHECK: mov x0, #16
28+
29+
# CHECK-SYM: Name: v0
30+
# CHECK-SYM-NEXT: Value: 0x0
31+
32+
## Access variable in second partition
33+
movz x0, #:tprel_g1:v1
34+
movk x0, #:tprel_g0_nc:v1
35+
36+
## TCB + 65536 across movz and movk
37+
# CHECK-NEXT: mov x0, #65536
38+
# CHECK-NEXT: movk x0, #16
39+
40+
# CHECK-SYM: Name: v1
41+
# CHECK-SYM-NEXT: Value: 0x10000
42+
43+
## Access variable in third partition
44+
movz x0, #:tprel_g2:v2
45+
movk x0, #:tprel_g1_nc:v2
46+
movk x0, #:tprel_g0_nc:v2
47+
48+
## TCB + 65536 + 4294967296 across movz and 2 movk instructions
49+
# CHECK-NEXT: mov x0, #4294967296
50+
# CHECK-NEXT: movk x0, #1, lsl #16
51+
# CHECK-NEXT: movk x0, #16
52+
53+
# CHECK-SYM: Name: v2
54+
# CHECK-SYM-NEXT: Value: 0x100010000
55+
56+
.section .tbss,"awT",@nobits
57+
.balign 16
58+
v0:
59+
.quad 0
60+
.space 0x10000 - 8
61+
v1:
62+
.quad 0
63+
.space 0x100000000 - 8
64+
v2:
65+
.quad 0

0 commit comments

Comments
 (0)