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 LD->LE relaxation.
7## LD uses the same relocation as GD: R_RISCV_GD_HI20, the difference is that it
8## references a local symbol (.LANCHOR0).
9
10# RUN: llvm-mc -filetype=obj -triple=riscv32 -position-independent %s -o %t.32.o
11# RUN: llvm-mc -filetype=obj -triple=riscv32 %tga.s -o %tga.o
12## rv32 LD
13# RUN: ld.lld -shared %t.32.o -o %t.32.so
14# RUN: llvm-readobj -r %t.32.so | FileCheck --check-prefix=LD32-REL %s
15# RUN: llvm-readelf -x .got %t.32.so | FileCheck --check-prefix=LD32-GOT %s
16# RUN: llvm-objdump -d --no-show-raw-insn %t.32.so | FileCheck --check-prefixes=LD,LD32 %s
17## rv32 LD -> LE
18# RUN: ld.lld %t.32.o %tga.o -o %t.32
19# RUN: llvm-readelf -r %t.32 | FileCheck --check-prefix=NOREL %s
20# RUN: llvm-readelf -x .got %t.32 | FileCheck --check-prefix=LE32-GOT %s
21# RUN: llvm-objdump -d --no-show-raw-insn %t.32 | FileCheck --check-prefixes=LE,LE32 %s
22
23# RUN: llvm-mc -filetype=obj -triple=riscv64 -position-independent %s -o %t.64.o
24# RUN: llvm-mc -filetype=obj -triple=riscv64 %tga.s -o %tga.o
25## rv64 LD
26# RUN: ld.lld -shared %t.64.o -o %t.64.so
27# RUN: llvm-readobj -r %t.64.so | FileCheck --check-prefix=LD64-REL %s
28# RUN: llvm-readelf -x .got %t.64.so | FileCheck --check-prefix=LD64-GOT %s
29# RUN: llvm-objdump -d --no-show-raw-insn %t.64.so | FileCheck --check-prefixes=LD,LD64 %s
30## rv64 LD -> LE
31# RUN: ld.lld %t.64.o %tga.o -o %t.64
32# RUN: llvm-readelf -r %t.64 | FileCheck --check-prefix=NOREL %s
33# RUN: llvm-readelf -x .got %t.64 | FileCheck --check-prefix=LE64-GOT %s
34# RUN: llvm-objdump -d --no-show-raw-insn %t.64 | FileCheck --check-prefixes=LE,LE64 %s
35
36## a@dtprel = st_value(a)-0x800 = 0xfffff808 is a link-time constant.
37# LD32-REL:      .rela.dyn {
38# LD32-REL-NEXT:   0x22B4
39# LD32-REL-NEXT:   0x22AC R_RISCV_TLS_DTPMOD32 - 0x0
40# LD32-REL-NEXT: }
41# LD32-GOT:      section '.got':
42# LD32-GOT-NEXT: 0x000022a8 30220000 00000000 00f8ffff 00000000
43
44# LD64-REL:      .rela.dyn {
45# LD64-REL-NEXT:   0x2458
46# LD64-REL-NEXT:   0x2448 R_RISCV_TLS_DTPMOD64 - 0x0
47# LD64-REL-NEXT: }
48# LD64-GOT:      section '.got':
49# LD64-GOT-NEXT: 0x00002440 50230000 00000000 00000000 00000000
50# LD64-GOT-NEXT: 0x00002450 00f8ffff ffffffff 00000000 00000000
51
52## rv32: &DTPMOD(a) - . = 0x22ac - 0x11d8 = 4096*1+212
53## rv64: &DTPMOD(a) - . = 0x2448 - 0x12f8 = 4096*1+336
54# LD32:      11d8: auipc a0, 1
55# LD32-NEXT:       addi a0, a0, 212
56# LD64:      12f8: auipc a0, 1
57# LD64-NEXT:       addi a0, a0, 336
58# LD-NEXT:         auipc ra, 0
59# LD-NEXT:         jalr 64(ra)
60
61# NOREL: no relocations
62
63## a is local - its DTPMOD/DTPREL slots are link-time constants.
64## a@dtpmod = 1 (main module)
65# LE32-GOT: section '.got':
66# LE32-GOT-NEXT: 0x00012134 00000000 01000000 00f8ffff 34210100
67
68# LE64-GOT: section '.got':
69# LE64-GOT-NEXT: 0x000121e8 00000000 00000000 01000000 00000000
70# LE64-GOT-NEXT: 0x000121f8 00f8ffff ffffffff e8210100 00000000
71
72## rv32: DTPMOD(.LANCHOR0) - . = 0x12138 - 0x11114 = 4096*1+36
73## rv64: DTPMOD(.LANCHOR0) - . = 0x121f0 - 0x111c8 = 4096*1+40
74# LE32:      11114: auipc a0, 1
75# LE32-NEXT:        addi a0, a0, 36
76# LE64:      111c8: auipc a0, 1
77# LE64-NEXT:        addi a0, a0, 40
78# LE-NEXT:          auipc ra, 0
79# LE-NEXT:          jalr 24(ra)
80
81la.tls.gd a0, .LANCHOR0
82call __tls_get_addr@plt
83lw a4, 0(a0)
84lh a0, 4(a0)
85
86## This is irrelevant to TLS. We use it to take 2 GOT slots to check DTPREL
87## offsets are correct.
88la a5, _GLOBAL_OFFSET_TABLE_
89
90.section .tbss,"awT",@nobits
91.set .LANCHOR0, . + 0
92.zero 8
93