xref: /llvm-project/llvm/test/ExecutionEngine/JITLink/x86-64/ELF_small_pic_relocations.s (revision 8ad75c1037289b06bd1b95ff445797d380d93039)
1# RUN: rm -rf %t && mkdir -p %t
2# RUN: llvm-mc -triple=x86_64-unknown-linux -position-independent \
3# RUN:     -filetype=obj -o %t/elf_sm_pic_reloc.o %s
4# RUN: llvm-jitlink -noexec \
5# RUN:     -slab-allocate 100Kb -slab-address 0xfff00000 -slab-page-size 4096 \
6# RUN:     -abs external_data=0x1 \
7# RUN:     -abs extern_out_of_range32=0x7fff00000000 \
8# RUN:     -check %s %t/elf_sm_pic_reloc.o
9#
10# Test ELF small/PIC relocations.
11
12        .text
13        .file   "testcase.c"
14
15# Empty main entry point.
16        .globl  main
17        .p2align        4, 0x90
18        .type   main,@function
19main:
20        retq
21
22        .size   main, .-main
23
24# Test PCRel32 / R_X86_64_PC32 handling.
25# jitlink-check: decode_operand(test_pcrel32, 4) = named_data - next_pc(test_pcrel32)
26        .globl  test_pcrel32
27        .p2align       4, 0x90
28        .type   test_pcrel32,@function
29test_pcrel32:
30        movl    named_data(%rip), %eax
31
32         .size   test_pcrel32, .-test_pcrel32
33
34        .globl  named_func
35        .p2align       4, 0x90
36        .type   named_func,@function
37named_func:
38        xorq    %rax, %rax
39
40        .size   named_func, .-named_func
41
42# Check R_X86_64_PLT32 handling with a call to a local function in the text
43# section. This produces a Branch32 edge that is resolved like a regular
44# PCRel32 (no PLT entry created).
45#
46# jitlink-check: decode_operand(test_call_local, 0) = \
47# jitlink-check:   named_func - next_pc(test_call_local)
48        .globl  test_call_local
49        .p2align       4, 0x90
50        .type   test_call_local,@function
51test_call_local:
52        callq   named_func
53
54        .size   test_call_local, .-test_call_local
55
56# Check R_X86_64_PLT32 handling with a call to a local linkage function in a
57# different text section and at a non-zero offset. This produces a Branch32 edge
58# that is resolved like a regular PCRel32 (no PLT entry created). The non-zero
59# offset requires us to handle addends for branch relocations correctly.
60#
61# jitlink-check: decode_operand(test_call_alt_sec_at_offset, 0) = \
62# jitlink-check:   (section_addr(elf_sm_pic_reloc.o, .text.alt) + 16) - \
63# jitlink-check:   next_pc(test_call_alt_sec_at_offset)
64        .globl  test_call_alt_sec_at_offset
65        .p2align       4, 0x90
66        .type   test_call_alt_sec_at_offset,@function
67test_call_alt_sec_at_offset:
68        callq   named_func_alt_sec_at_offset
69
70        .size   test_call_alt_sec_at_offset, .-test_call_alt_sec_at_offset
71
72# Check R_X86_64_PLT32 handling with a call to an external via PLT. This
73# produces a Branch32ToStub edge, because externals are not defined locally.
74# As the target is out-of-range from the callsite, the edge keeps using its PLT
75# entry.
76#
77# jitlink-check: decode_operand(test_call_extern_plt, 0) = \
78# jitlink-check:     stub_addr(elf_sm_pic_reloc.o, extern_out_of_range32) - \
79# jitlink-check:        next_pc(test_call_extern_plt)
80# jitlink-check: *{8}(got_addr(elf_sm_pic_reloc.o, extern_out_of_range32)) = \
81# jitlink-check:     extern_out_of_range32
82        .globl  test_call_extern_plt
83        .p2align       4, 0x90
84        .type   test_call_extern_plt,@function
85test_call_extern_plt:
86        callq   extern_out_of_range32@plt
87
88        .size   test_call_extern_plt, .-test_call_extern_plt
89
90# Test GOTPCREL handling. We want to check both the offset to the GOT entry and its
91# contents. "movl" will be optimized to "leal" and a non-got access if the pc relative
92# offset to named_data is in range of 32 bits signed immediate. So use "leal" here to
93# suppress optimization
94# jitlink-check: decode_operand(test_gotpcrel, 4) = \
95# jitlink-check:     got_addr(elf_sm_pic_reloc.o, named_data) - next_pc(test_gotpcrel)
96# jitlink-check: *{8}(got_addr(elf_sm_pic_reloc.o, named_data)) = named_data
97
98        .globl test_gotpcrel
99        .p2align      4, 0x90
100        .type   test_gotpcrel,@function
101test_gotpcrel:
102	leal    named_data@GOTPCREL(%rip), %eax
103
104        .size   test_gotpcrel, .-test_gotpcrel
105
106# Test REX_GOTPCRELX handling. We want to check both the offset to the GOT entry and its
107# contents.
108# jitlink-check: decode_operand(test_rex_gotpcrelx, 4) = \
109# jitlink-check:   got_addr(elf_sm_pic_reloc.o, external_data) - next_pc(test_rex_gotpcrelx)
110
111        .globl test_rex_gotpcrelx
112        .p2align      4, 0x90
113        .type   test_rex_gotpcrelx,@function
114test_rex_gotpcrelx:
115	movq    external_data@GOTPCREL(%rip), %rax
116
117        .size   test_rex_gotpcrelx, .-test_rex_gotpcrelx
118
119# Test GOTOFF64 handling.
120# jitlink-check: decode_operand(test_gotoff64, 1) = named_func - _GLOBAL_OFFSET_TABLE_
121        .globl test_gotoff64
122        .p2align     4, 0x90
123        .type  test_gotoff64,@function
124test_gotoff64:
125        movabsq $named_func@GOTOFF, %rax
126
127        .size   test_gotoff64, .-test_gotoff64
128
129# Test that relocations to anonymous constant pool entries work.
130        .globl  test_anchor_LCPI
131        .p2align        4, 0x90
132        .type   test_anchor_LCPI,@function
133test_anchor_LCPI:
134        movq    .LCPI0_0(%rip), %rax
135
136        .size   test_anchor_LCPI, .-test_anchor_LCPI
137
138        .data
139
140        .type   named_data,@object
141        .p2align        3
142named_data:
143        .quad   42
144        .size   named_data, 8
145
146# Test BSS / zero-fill section handling.
147# llvm-jitlink: *{4}bss_variable = 0
148
149	.type	bss_variable,@object
150	.bss
151	.globl	bss_variable
152	.p2align	2
153bss_variable:
154	.long	0
155	.size	bss_variable, 4
156
157# Named functions in a separate section.
158	.section	.text.alt,"ax",@progbits
159# .byte plus alignment of 16 should put named_func_alt_sec_at_offset at offset
160# 16 within .text.alt.
161	.byte   7
162	.p2align	4, 0x90
163	.type	named_func_alt_sec_at_offset,@function
164named_func_alt_sec_at_offset:
165	retq
166	.size	named_func_alt_sec_at_offset, .-named_func_alt_sec_at_offset
167
168# Constant pool entry with type STT_NOTYPE.
169        .section        .rodata.cst8,"aM",@progbits,8
170        .p2align        3
171.LCPI0_0:
172        .quad   0x400921fb54442d18
173
174        .ident  "clang version 10.0.0-4ubuntu1 "
175        .section        ".note.GNU-stack","",@progbits
176        .addrsig
177