1# REQUIRES: riscv
2# RUN: echo '.tbss; .globl b, c; b: .zero 4; c:' > %t.s
3# RUN: echo '.globl __tls_get_addr; __tls_get_addr:' > %tga.s
4
5## RISC-V psABI doesn't specify TLS relaxation. Though the code sequences are not
6## relaxed, dynamic relocations can be omitted for GD->LE relaxation.
7
8# RUN: llvm-mc -filetype=obj -triple=riscv32 %s -o %t.32.o
9# RUN: llvm-mc -filetype=obj -triple=riscv32 %t.s -o %t1.32.o
10# RUN: ld.lld -shared -soname=t1.so %t1.32.o -o %t1.32.so
11# RUN: llvm-mc -filetype=obj -triple=riscv32 %tga.s -o %tga.32.o
12## rv32 GD
13# RUN: ld.lld -shared %t.32.o %t1.32.o -o %t.32.so
14# RUN: llvm-readobj -r %t.32.so | FileCheck --check-prefix=GD32-REL %s
15# RUN: llvm-objdump -d --no-show-raw-insn %t.32.so | FileCheck --check-prefix=GD32 %s
16## rv32 GD -> LE
17# RUN: ld.lld %t.32.o %t1.32.o %tga.32.o -o %t.32
18# RUN: llvm-readelf -r %t.32 | FileCheck --check-prefix=NOREL %s
19# RUN: llvm-readelf -x .got %t.32 | FileCheck --check-prefix=LE32-GOT %s
20# RUN: ld.lld -pie %t.32.o %t1.32.o %tga.32.o -o %t.32
21# RUN: llvm-readelf -r %t.32 | FileCheck --check-prefix=NOREL %s
22# RUN: llvm-readelf -x .got %t.32 | FileCheck --check-prefix=LE32-GOT %s
23## rv32 GD -> IE
24# RUN: ld.lld %t.32.o %t1.32.so %tga.32.o -o %t.32
25# RUN: llvm-readobj -r %t.32 | FileCheck --check-prefix=IE32-REL %s
26# RUN: llvm-readelf -x .got %t.32 | FileCheck --check-prefix=IE32-GOT %s
27
28# RUN: llvm-mc -filetype=obj -triple=riscv64 %s -o %t.64.o
29# RUN: llvm-mc -filetype=obj -triple=riscv64 %t.s -o %t1.64.o
30# RUN: ld.lld -shared -soname=t1.so %t1.64.o -o %t1.64.so
31# RUN: llvm-mc -filetype=obj -triple=riscv64 %tga.s -o %tga.64.o
32## rv64 GD
33# RUN: ld.lld -shared %t.64.o %t1.64.o -o %t.64.so
34# RUN: llvm-readobj -r %t.64.so | FileCheck --check-prefix=GD64-REL %s
35# RUN: llvm-objdump -d --no-show-raw-insn %t.64.so | FileCheck --check-prefix=GD64 %s
36## rv64 GD -> LE
37# RUN: ld.lld %t.64.o %t1.64.o %tga.64.o -o %t.64
38# RUN: llvm-readelf -r %t.64 | FileCheck --check-prefix=NOREL %s
39# RUN: llvm-readelf -x .got %t.64 | FileCheck --check-prefix=LE64-GOT %s
40# RUN: ld.lld -pie %t.64.o %t1.64.o %tga.64.o -o %t.64
41# RUN: llvm-readelf -r %t.64 | FileCheck --check-prefix=NOREL %s
42# RUN: llvm-readelf -x .got %t.64 | FileCheck --check-prefix=LE64-GOT %s
43## rv64 GD -> IE
44# RUN: ld.lld %t.64.o %t1.64.so %tga.64.o -o %t.64
45# RUN: llvm-readobj -r %t.64 | FileCheck --check-prefix=IE64-REL %s
46# RUN: llvm-readelf -x .got %t.64 | FileCheck --check-prefix=IE64-GOT %s
47
48# GD32-REL:      .rela.dyn {
49# GD32-REL-NEXT:   0x2310 R_RISCV_TLS_DTPMOD32 a 0x0
50# GD32-REL-NEXT:   0x2314 R_RISCV_TLS_DTPREL32 a 0x0
51# GD32-REL-NEXT:   0x2318 R_RISCV_TLS_DTPMOD32 b 0x0
52# GD32-REL-NEXT:   0x231C R_RISCV_TLS_DTPREL32 b 0x0
53# GD32-REL-NEXT: }
54
55## &DTPMOD(a) - . = 0x2310 - 0x1250 = 4096*1+192
56# GD32:      1250: auipc a0, 1
57# GD32-NEXT:       addi a0, a0, 192
58# GD32-NEXT:       auipc ra, 0
59# GD32-NEXT:       jalr 56(ra)
60
61## &DTPMOD(b) - . = 0x2318 - 0x1260 = 4096*1+184
62# GD32:      1260: auipc a0, 1
63# GD32-NEXT:       addi a0, a0, 184
64# GD32-NEXT:       auipc ra, 0
65# GD32-NEXT:       jalr 40(ra)
66
67# GD64-REL:      .rela.dyn {
68# GD64-REL-NEXT:   0x24D0 R_RISCV_TLS_DTPMOD64 a 0x0
69# GD64-REL-NEXT:   0x24D8 R_RISCV_TLS_DTPREL64 a 0x0
70# GD64-REL-NEXT:   0x24E0 R_RISCV_TLS_DTPMOD64 b 0x0
71# GD64-REL-NEXT:   0x24E8 R_RISCV_TLS_DTPREL64 b 0x0
72# GD64-REL-NEXT: }
73
74## &DTPMOD(a) - . = 0x24d0 - 0x1398 = 4096*1+312
75# GD64:      1398: auipc a0, 1
76# GD64-NEXT:       addi a0, a0, 312
77# GD64-NEXT:       auipc ra, 0
78# GD64-NEXT:       jalr 64(ra)
79
80## &DTPMOD(b) - . = 0x24e0 - 0x13a8 = 4096*1+312
81# GD64:      13a8: auipc a0, 1
82# GD64-NEXT:       addi a0, a0, 312
83# GD64-NEXT:       auipc ra, 0
84# GD64-NEXT:       jalr 48(ra)
85
86# NOREL: no relocations
87
88## .got contains pre-populated values: [a@dtpmod, a@dtprel, b@dtpmod, b@dtprel]
89## a@dtprel = st_value(a)-0x800 = 0xfffff808
90## b@dtprel = st_value(b)-0x800 = 0xfffff80c
91# LE32-GOT: section '.got':
92# LE32-GOT-NEXT: 0x{{[0-9a-f]+}} 01000000 08f8ffff 01000000 0cf8ffff
93# LE64-GOT: section '.got':
94# LE64-GOT-NEXT: 0x{{[0-9a-f]+}} 01000000 00000000 08f8ffff ffffffff
95# LE64-GOT-NEXT: 0x{{[0-9a-f]+}} 01000000 00000000 0cf8ffff ffffffff
96
97## a is local - relaxed to LE - its DTPMOD/DTPREL slots are link-time constants.
98## b is external - DTPMOD/DTPREL dynamic relocations are required.
99# IE32-REL:      .rela.dyn {
100# IE32-REL-NEXT:   0x12230 R_RISCV_TLS_DTPMOD32 b 0x0
101# IE32-REL-NEXT:   0x12234 R_RISCV_TLS_DTPREL32 b 0x0
102# IE32-REL-NEXT: }
103# IE32-GOT:      section '.got':
104# IE32-GOT-NEXT: 0x00012228 01000000 08f8ffff 00000000 00000000
105
106# IE64-REL:      .rela.dyn {
107# IE64-REL-NEXT:   0x12390 R_RISCV_TLS_DTPMOD64 b 0x0
108# IE64-REL-NEXT:   0x12398 R_RISCV_TLS_DTPREL64 b 0x0
109# IE64-REL-NEXT: }
110# IE64-GOT:      section '.got':
111# IE64-GOT-NEXT: 0x00012380 01000000 00000000 08f8ffff ffffffff
112# IE64-GOT-NEXT: 0x00012390 00000000 00000000 00000000 00000000
113
114la.tls.gd a0,a
115call __tls_get_addr@plt
116
117la.tls.gd a0,b
118call __tls_get_addr@plt
119
120.section .tbss
121.globl a
122.zero 8
123a:
124.zero 4
125