1// REQUIRES: arm
2// RUN: llvm-mc -filetype=obj -triple=armv7a-none-linux-gnueabi %p/Inputs/arm-plt-reloc.s -o %t1
3// RUN: llvm-mc -filetype=obj -triple=armv7a-none-linux-gnueabi %s -o %t2
4// RUN: ld.lld %t1 %t2 -o %t
5// RUN: llvm-objdump --triple=armv7a-none-linux-gnueabi -d --no-show-raw-insn %t | FileCheck %s
6// RUN: ld.lld -shared %t1 %t2 -o %t3
7// RUN: llvm-objdump --triple=armv7a-none-linux-gnueabi -d --no-show-raw-insn %t3 | FileCheck --check-prefix=DSO %s
8// RUN: llvm-readobj -S -r %t3 | FileCheck -check-prefix=DSOREL %s
9//
10// Test PLT entry generation
11 .syntax unified
12 .text
13 .align 2
14 .globl _start
15 .type  _start,%function
16_start:
17 b func1
18 bl func2
19 beq func3
20
21// Executable, expect no PLT
22// CHECK: Disassembly of section .text:
23// CHECK-EMPTY:
24// CHECK-NEXT: <func1>:
25// CHECK-NEXT:   200b4:       bx      lr
26// CHECK: <func2>:
27// CHECK-NEXT:   200b8:       bx      lr
28// CHECK: <func3>:
29// CHECK-NEXT:   200bc:       bx      lr
30// CHECK: <_start>:
31// CHECK-NEXT:   200c0:       b       #-20 <func1>
32// CHECK-NEXT:   200c4:       bl      #-20 <func2>
33// CHECK-NEXT:   200c8:       beq     #-20 <func3>
34
35// Expect PLT entries as symbols can be preempted
36// The .got.plt and .plt displacement is small so we can use small PLT entries.
37// DSO: Disassembly of section .text:
38// DSO-EMPTY:
39// DSO-NEXT: <func1>:
40// DSO-NEXT:     10214:       bx      lr
41// DSO: <func2>:
42// DSO-NEXT:     10218:       bx      lr
43// DSO: <func3>:
44// DSO-NEXT:     1021c:       bx      lr
45// DSO: <_start>:
46// S(0x10214) - P(0x10220) + A(-8) = 0x2c = 32
47// DSO-NEXT:     10220:       b       #40
48// S(0x10218) - P(0x10224) + A(-8) = 0x38 = 56
49// DSO-NEXT:     10224:       bl      #52
50// S(0x1021c) - P(0x10228) + A(-8) = 0x44 = 68
51// DSO-NEXT:     10228:       beq     #64
52// DSO-EMPTY:
53// DSO-NEXT: Disassembly of section .plt:
54// DSO-EMPTY:
55// DSO-NEXT: <$a>:
56// DSO-NEXT:     10230:       str     lr, [sp, #-4]!
57// (0x10234 + 8) + (0 RoR 12) + 8192 + 164 = 0x32e0 = .got.plt[2]
58// DSO-NEXT:     10234:       add     lr, pc, #0, #12
59// DSO-NEXT:     10238:       add     lr, lr, #32
60// DSO-NEXT:     1023c:       ldr     pc, [lr, #164]!
61// DSO: <$d>:
62// DSO-NEXT:     10240:       d4 d4 d4 d4     .word   0xd4d4d4d4
63// DSO-NEXT:     10244:       d4 d4 d4 d4     .word   0xd4d4d4d4
64// DSO-NEXT:     10248:       d4 d4 d4 d4     .word   0xd4d4d4d4
65// DSO-NEXT:     1024c:       d4 d4 d4 d4     .word   0xd4d4d4d4
66// DSO: <$a>:
67// (0x10250 + 8) + (0 RoR 12) + 8192 + 140 = 0x32e4
68// DSO-NEXT:     10250:       add     r12, pc, #0, #12
69// DSO-NEXT:     10254:       add     r12, r12, #32
70// DSO-NEXT:     10258:       ldr     pc, [r12, #140]!
71// DSO: <$d>:
72// DSO-NEXT:     1025c:       d4 d4 d4 d4     .word   0xd4d4d4d4
73// DSO: <$a>:
74// (0x10260 + 8) + (0 RoR 12) + 8192 + 128 = 0x32e8
75// DSO-NEXT:     10260:       add     r12, pc, #0, #12
76// DSO-NEXT:     10264:       add     r12, r12, #32
77// DSO-NEXT:     10268:       ldr     pc, [r12, #128]!
78// DSO: <$d>:
79// DSO-NEXT:     1026c:       d4 d4 d4 d4     .word   0xd4d4d4d4
80// DSO: <$a>:
81// (0x10270 + 8) + (0 RoR 12) + 8192 + 116 = 0x32ec
82// DSO-NEXT:     10270:       add     r12, pc, #0, #12
83// DSO-NEXT:     10274:       add     r12, r12, #32
84// DSO-NEXT:     10278:       ldr     pc, [r12, #116]!
85// DSO: <$d>:
86// DSO-NEXT:     1027c:       d4 d4 d4 d4     .word   0xd4d4d4d4
87
88
89// DSOREL:    Name: .got.plt
90// DSOREL-NEXT:    Type: SHT_PROGBITS
91// DSOREL-NEXT:    Flags [
92// DSOREL-NEXT:      SHF_ALLOC
93// DSOREL-NEXT:      SHF_WRITE
94// DSOREL-NEXT:    ]
95// DSOREL-NEXT:    Address: 0x302D8
96// DSOREL-NEXT:    Offset:
97// DSOREL-NEXT:    Size: 24
98// DSOREL-NEXT:    Link:
99// DSOREL-NEXT:    Info:
100// DSOREL-NEXT:    AddressAlignment: 4
101// DSOREL-NEXT:    EntrySize:
102// DSOREL:  Relocations [
103// DSOREL-NEXT:  Section {{.*}} .rel.plt {
104// DSOREL-NEXT:    0x302E4 R_ARM_JUMP_SLOT func1 0x0
105// DSOREL-NEXT:    0x302E8 R_ARM_JUMP_SLOT func2 0x0
106// DSOREL-NEXT:    0x302EC R_ARM_JUMP_SLOT func3 0x0
107
108// Test a large separation between the .plt and .got.plt
109// The .got.plt and .plt displacement is large but still within the range
110// of the short plt sequence.
111// RUN: echo "SECTIONS { \
112// RUN:       .text 0x1000 : { *(.text) } \
113// RUN:       .plt  0x2000 : { *(.plt) *(.plt.*) } \
114// RUN:       .got.plt 0x1100000 : { *(.got.plt) } \
115// RUN:       }" > %t.script
116// RUN: ld.lld --hash-style=sysv --script %t.script -shared %t1 %t2 -o %t4
117// RUN: llvm-objdump --triple=armv7a-none-linux-gnueabi -d --no-show-raw-insn %t4 | FileCheck --check-prefix=CHECKHIGH %s
118// RUN: llvm-readobj -S -r %t4 | FileCheck --check-prefix=DSORELHIGH %s
119
120// CHECKHIGH: Disassembly of section .text:
121// CHECKHIGH-EMPTY:
122// CHECKHIGH-NEXT: <func1>:
123// CHECKHIGH-NEXT:     1000:       bx      lr
124// CHECKHIGH: <func2>:
125// CHECKHIGH-NEXT:     1004:       bx      lr
126// CHECKHIGH: <func3>:
127// CHECKHIGH-NEXT:     1008:       bx      lr
128// CHECKHIGH: <_start>:
129// CHECKHIGH-NEXT:     100c:       b       #4108 <$a>
130// CHECKHIGH-NEXT:     1010:       bl      #4120 <$a>
131// CHECKHIGH-NEXT:     1014:       beq     #4132 <$a>
132// CHECKHIGH-EMPTY:
133// CHECKHIGH-NEXT: Disassembly of section .plt:
134// CHECKHIGH-EMPTY:
135// CHECKHIGH-NEXT: <$a>:
136// CHECKHIGH-NEXT:     2000:       str     lr, [sp, #-4]!
137// CHECKHIGH-NEXT:     2004:       add     lr, pc, #16, #12
138// CHECKHIGH-NEXT:     2008:       add     lr, lr, #1036288
139// CHECKHIGH-NEXT:     200c:       ldr     pc, [lr, #4092]!
140// CHECKHIGH: <$d>:
141// CHECKHIGH-NEXT:     2010:       d4 d4 d4 d4     .word   0xd4d4d4d4
142// CHECKHIGH-NEXT:     2014:       d4 d4 d4 d4     .word   0xd4d4d4d4
143// CHECKHIGH-NEXT:     2018:       d4 d4 d4 d4     .word   0xd4d4d4d4
144// CHECKHIGH-NEXT:     201c:       d4 d4 d4 d4     .word   0xd4d4d4d4
145// CHECKHIGH: <$a>:
146// CHECKHIGH-NEXT:     2020:       add     r12, pc, #16, #12
147// CHECKHIGH-NEXT:     2024:       add     r12, r12, #1036288
148// CHECKHIGH-NEXT:     2028:       ldr     pc, [r12, #4068]!
149// CHECKHIGH: <$d>:
150// CHECKHIGH-NEXT:     202c:       d4 d4 d4 d4     .word   0xd4d4d4d4
151// CHECKHIGH: <$a>:
152// CHECKHIGH-NEXT:     2030:       add     r12, pc, #16, #12
153// CHECKHIGH-NEXT:     2034:       add     r12, r12, #1036288
154// CHECKHIGH-NEXT:     2038:       ldr     pc, [r12, #4056]!
155// CHECKHIGH: <$d>:
156// CHECKHIGH-NEXT:     203c:       d4 d4 d4 d4     .word   0xd4d4d4d4
157// CHECKHIGH: <$a>:
158// CHECKHIGH-NEXT:     2040:       add     r12, pc, #16, #12
159// CHECKHIGH-NEXT:     2044:       add     r12, r12, #1036288
160// CHECKHIGH-NEXT:     2048:       ldr     pc, [r12, #4044]!
161// CHECKHIGH: <$d>:
162// CHECKHIGH-NEXT:     204c:       d4 d4 d4 d4     .word   0xd4d4d4d4
163
164// DSORELHIGH:     Name: .got.plt
165// DSORELHIGH-NEXT:     Type: SHT_PROGBITS
166// DSORELHIGH-NEXT:     Flags [
167// DSORELHIGH-NEXT:       SHF_ALLOC
168// DSORELHIGH-NEXT:       SHF_WRITE
169// DSORELHIGH-NEXT:     ]
170// DSORELHIGH-NEXT:     Address: 0x1100000
171// DSORELHIGH: Relocations [
172// DSORELHIGH-NEXT:   Section {{.*}} .rel.plt {
173// DSORELHIGH-NEXT:     0x110000C R_ARM_JUMP_SLOT func1 0x0
174// DSORELHIGH-NEXT:     0x1100010 R_ARM_JUMP_SLOT func2 0x0
175// DSORELHIGH-NEXT:     0x1100014 R_ARM_JUMP_SLOT func3 0x0
176
177// Test a very large separation between the .plt and .got.plt so we must use
178// large plt entries that do not have any range restriction.
179// RUN: echo "SECTIONS { \
180// RUN:       .text 0x1000 : { *(.text) } \
181// RUN:       .plt  0x2000 : { *(.plt) *(.plt.*) } \
182// RUN:       .got.plt 0x11111100 : { *(.got.plt) } \
183// RUN:       }" > %t2.script
184// RUN: ld.lld --hash-style=sysv --script %t2.script -shared %t1 %t2 -o %t5
185// RUN: llvm-objdump --triple=armv7a-none-linux-gnueabi -d --no-show-raw-insn %t5 | FileCheck --check-prefix=CHECKLONG %s
186// RUN: llvm-readobj -S -r %t5 | FileCheck --check-prefix=DSORELLONG %s
187
188// CHECKLONG: Disassembly of section .text:
189// CHECKLONG-EMPTY:
190// CHECKLONG-NEXT: <func1>:
191// CHECKLONG-NEXT:     1000:       bx      lr
192// CHECKLONG: <func2>:
193// CHECKLONG-NEXT:     1004:       bx      lr
194// CHECKLONG: <func3>:
195// CHECKLONG-NEXT:     1008:       bx      lr
196// CHECKLONG: <_start>:
197// CHECKLONG-NEXT:     100c:       b       #4108 <$a>
198// CHECKLONG-NEXT:     1010:       bl      #4120 <$a>
199// CHECKLONG-NEXT:     1014:       beq     #4132 <$a>
200// CHECKLONG-EMPTY:
201// CHECKLONG-NEXT: Disassembly of section .plt:
202// CHECKLONG-EMPTY:
203// CHECKLONG-NEXT: <$a>:
204// CHECKLONG-NEXT:     2000:       str     lr, [sp, #-4]!
205// CHECKLONG-NEXT:     2004:       ldr     lr, [pc, #4]
206// CHECKLONG-NEXT:     2008:       add     lr, pc, lr
207// CHECKLONG-NEXT:     200c:       ldr     pc, [lr, #8]!
208// CHECKLONG: <$d>:
209// CHECKLONG-NEXT:     2010:       f0 f0 10 11     .word   0x1110f0f0
210// CHECKLONG-NEXT:     2014:       d4 d4 d4 d4     .word   0xd4d4d4d4
211// CHECKLONG-NEXT:     2018:       d4 d4 d4 d4     .word   0xd4d4d4d4
212// CHECKLONG-NEXT:     201c:       d4 d4 d4 d4     .word   0xd4d4d4d4
213// CHECKLONG: <$a>:
214// CHECKLONG-NEXT:     2020:       ldr     r12, [pc, #4]
215// CHECKLONG-NEXT:     2024:       add     r12, r12, pc
216// CHECKLONG-NEXT:     2028:       ldr     pc, [r12]
217// CHECKLONG: <$d>:
218// CHECKLONG-NEXT:     202c:       e0 f0 10 11     .word   0x1110f0e0
219// CHECKLONG: <$a>:
220// CHECKLONG-NEXT:     2030:       ldr     r12, [pc, #4]
221// CHECKLONG-NEXT:     2034:       add     r12, r12, pc
222// CHECKLONG-NEXT:     2038:       ldr     pc, [r12]
223// CHECKLONG: <$d>:
224// CHECKLONG-NEXT:     203c:       d4 f0 10 11     .word   0x1110f0d4
225// CHECKLONG: <$a>:
226// CHECKLONG-NEXT:     2040:       ldr     r12, [pc, #4]
227// CHECKLONG-NEXT:     2044:       add     r12, r12, pc
228// CHECKLONG-NEXT:     2048:       ldr     pc, [r12]
229// CHECKLONG: <$d>:
230// CHECKLONG-NEXT:     204c:       c8 f0 10 11     .word   0x1110f0c8
231
232// DSORELLONG: Name: .got.plt
233// DSORELLONG-NEXT:     Type: SHT_PROGBITS
234// DSORELLONG-NEXT:     Flags [
235// DSORELLONG-NEXT:       SHF_ALLOC
236// DSORELLONG-NEXT:       SHF_WRITE
237// DSORELLONG-NEXT:     ]
238// DSORELLONG-NEXT:     Address: 0x11111100
239// DSORELLONG: Relocations [
240// DSORELLONG-NEXT:   Section {{.*}} .rel.plt {
241// DSORELLONG-NEXT:     0x1111110C R_ARM_JUMP_SLOT func1 0x0
242// DSORELLONG-NEXT:     0x11111110 R_ARM_JUMP_SLOT func2 0x0
243// DSORELLONG-NEXT:     0x11111114 R_ARM_JUMP_SLOT func3 0x0
244
245// Test a separation between the .plt and .got.plt that is part in range of
246// short table entries and part needing long entries. We use the long entries
247// only when we need to.
248// RUN: echo "SECTIONS { \
249// RUN:       .text 0x1000 : { *(.text) } \
250// RUN:       .plt  0x2000 : { *(.plt) *(.plt.*) } \
251// RUN:       .got.plt 0x8002020 : { *(.got.plt) } \
252// RUN:       }" > %t3.script
253// RUN: ld.lld --hash-style=sysv --script %t3.script -shared %t1 %t2 -o %t6
254// RUN: llvm-objdump --triple=armv7a-none-linux-gnueabi -d --no-show-raw-insn %t6 | FileCheck --check-prefix=CHECKMIX %s
255// RUN: llvm-readobj -S -r %t6 | FileCheck --check-prefix=DSORELMIX %s
256
257// CHECKMIX: Disassembly of section .text:
258// CHECKMIX-EMPTY:
259// CHECKMIX-NEXT: <func1>:
260// CHECKMIX-NEXT:     1000:       bx      lr
261// CHECKMIX: <func2>:
262// CHECKMIX-NEXT:     1004:       bx      lr
263// CHECKMIX: <func3>:
264// CHECKMIX-NEXT:     1008:       bx      lr
265// CHECKMIX: <_start>:
266// CHECKMIX-NEXT:     100c:       b       #4108 <$a>
267// CHECKMIX-NEXT:     1010:       bl      #4120 <$a>
268// CHECKMIX-NEXT:     1014:       beq     #4132 <$a>
269// CHECKMIX-EMPTY:
270// CHECKMIX-NEXT: Disassembly of section .plt:
271// CHECKMIX-EMPTY:
272// CHECKMIX-NEXT: <$a>:
273// CHECKMIX-NEXT:     2000:       str     lr, [sp, #-4]!
274// CHECKMIX-NEXT:     2004:       ldr     lr, [pc, #4]
275// CHECKMIX-NEXT:     2008:       add     lr, pc, lr
276// CHECKMIX-NEXT:     200c:       ldr     pc, [lr, #8]!
277// CHECKMIX: <$d>:
278// CHECKMIX-NEXT:     2010:     10 00 00 08     .word   0x08000010
279// CHECKMIX-NEXT:     2014:     d4 d4 d4 d4     .word   0xd4d4d4d4
280// CHECKMIX-NEXT:     2018:     d4 d4 d4 d4     .word   0xd4d4d4d4
281// CHECKMIX-NEXT:     201c:     d4 d4 d4 d4     .word   0xd4d4d4d4
282// CHECKMIX: <$a>:
283// CHECKMIX-NEXT:     2020:       ldr     r12, [pc, #4]
284// CHECKMIX-NEXT:     2024:       add     r12, r12, pc
285// CHECKMIX-NEXT:     2028:       ldr     pc, [r12]
286// CHECKMIX: <$d>:
287// CHECKMIX-NEXT:     202c:     00 00 00 08     .word   0x08000000
288// CHECKMIX: <$a>:
289// CHECKMIX-NEXT:     2030:       add     r12, pc, #133169152
290// CHECKMIX-NEXT:     2034:       add     r12, r12, #1044480
291// CHECKMIX-NEXT:     2038:       ldr     pc, [r12, #4088]!
292// CHECKMIX: <$d>:
293// CHECKMIX-NEXT:     203c:     d4 d4 d4 d4     .word   0xd4d4d4d4
294// CHECKMIX: <$a>:
295// CHECKMIX-NEXT:     2040:       add     r12, pc, #133169152
296// CHECKMIX-NEXT:     2044:       add     r12, r12, #1044480
297// CHECKMIX-NEXT:     2048:       ldr     pc, [r12, #4076]!
298// CHECKMIX: <$d>:
299// CHECKMIX-NEXT:     204c:     d4 d4 d4 d4     .word   0xd4d4d4d4
300
301// DSORELMIX:    Name: .got.plt
302// DSORELMIX-NEXT:     Type: SHT_PROGBITS
303// DSORELMIX-NEXT:     Flags [
304// DSORELMIX-NEXT:       SHF_ALLOC
305// DSORELMIX-NEXT:       SHF_WRITE
306// DSORELMIX-NEXT:     ]
307// DSORELMIX-NEXT:     Address: 0x8002020
308// DSORELMIX:   Section {{.*}} .rel.plt {
309// DSORELMIX-NEXT:     0x800202C R_ARM_JUMP_SLOT func1 0x0
310// DSORELMIX-NEXT:     0x8002030 R_ARM_JUMP_SLOT func2 0x0
311// DSORELMIX-NEXT:     0x8002034 R_ARM_JUMP_SLOT func3 0x0
312