1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
| # RUN: llvm-mc -filetype=obj -arch mipsel %s | llvm-readobj -r | FileCheck %s
# Test the order of records in the relocation table.
#
# MIPS has a few relocations that have an AHL component in the expression used
# to evaluate them. This AHL component is an addend with the same number of
# bits as a symbol value but not all of our ABI's are able to supply a
# sufficiently sized addend in a single relocation.
#
# The O32 ABI for example, uses REL relocations which store the addend in the
# section data. All the relocations with AHL components affect 16-bit fields
# so the addend is limited to 16-bit. This ABI resolves the limitation by
# linking relocations (e.g. R_MIPS_HI16 and R_MIPS_LO16) and distributing the
# addend between the linked relocations. The ABI mandates that such relocations
# must be next to each other in a particular order (e.g. R_MIPS_HI16 must be
# followed by a matching R_MIPS_LO16) but the rule is less strict in practice.
#
# See MipsELFObjectWriter.cpp for a full description of the rules.
#
# TODO: Add mips16 and micromips tests.
# HILO 1: HI/LO already match
.section .mips_hilo_1, "ax", @progbits
lui $2, %hi(sym1)
addiu $2, $2, %lo(sym1)
# CHECK-LABEL: Section ({{[0-9]+}}) .rel.mips_hilo_1 {
# CHECK-NEXT: 0x0 R_MIPS_HI16 sym1
# CHECK-NEXT: 0x4 R_MIPS_LO16 sym1
# CHECK-NEXT: }
# HILO 2: R_MIPS_HI16 must be followed by a matching R_MIPS_LO16.
.section .mips_hilo_2, "ax", @progbits
addiu $2, $2, %lo(sym1)
lui $2, %hi(sym1)
# CHECK-LABEL: Section ({{[0-9]+}}) .rel.mips_hilo_2 {
# CHECK-NEXT: 0x4 R_MIPS_HI16 sym1
# CHECK-NEXT: 0x0 R_MIPS_LO16 sym1
# CHECK-NEXT: }
# HILO 3: R_MIPS_HI16 must be followed by a matching R_MIPS_LO16.
# The second relocation matches if the symbol is the same.
.section .mips_hilo_3, "ax", @progbits
addiu $2, $2, %lo(sym1)
lui $2, %hi(sym2)
addiu $2, $2, %lo(sym2)
lui $2, %hi(sym1)
# CHECK-LABEL: Section ({{[0-9]+}}) .rel.mips_hilo_3 {
# CHECK-NEXT: 0xC R_MIPS_HI16 sym1
# CHECK-NEXT: 0x0 R_MIPS_LO16 sym1
# CHECK-NEXT: 0x4 R_MIPS_HI16 sym2
# CHECK-NEXT: 0x8 R_MIPS_LO16 sym2
# CHECK-NEXT: }
# HILO 3b: Same as 3 but a different starting order.
.section .mips_hilo_3b, "ax", @progbits
addiu $2, $2, %lo(sym1)
lui $2, %hi(sym1)
addiu $2, $2, %lo(sym2)
lui $2, %hi(sym2)
# CHECK-LABEL: Section ({{[0-9]+}}) .rel.mips_hilo_3b {
# CHECK-NEXT: 0x4 R_MIPS_HI16 sym1
# CHECK-NEXT: 0x0 R_MIPS_LO16 sym1
# CHECK-NEXT: 0xC R_MIPS_HI16 sym2
# CHECK-NEXT: 0x8 R_MIPS_LO16 sym2
# CHECK-NEXT: }
# HILO 4: R_MIPS_HI16 must be followed by a matching R_MIPS_LO16.
# The second relocation matches if the symbol is the same and the
# offset is the same.
.section .mips_hilo_4, "ax", @progbits
addiu $2, $2, %lo(sym1)
addiu $2, $2, %lo(sym1+4)
lui $2, %hi(sym1+4)
lui $2, %hi(sym1)
# CHECK-LABEL: Section ({{[0-9]+}}) .rel.mips_hilo_4 {
# CHECK-NEXT: 0xC R_MIPS_HI16 sym1
# CHECK-NEXT: 0x0 R_MIPS_LO16 sym1
# CHECK-NEXT: 0x8 R_MIPS_HI16 sym1
# CHECK-NEXT: 0x4 R_MIPS_LO16 sym1
# CHECK-NEXT: }
# HILO 5: R_MIPS_HI16 must be followed by a matching R_MIPS_LO16.
# The second relocation matches if the symbol is the same and the
# offset is greater or equal. Exact matches are preferred so both
# R_MIPS_HI16's match the same R_MIPS_LO16.
.section .mips_hilo_5, "ax", @progbits
lui $2, %hi(sym1)
lui $2, %hi(sym1)
addiu $2, $2, %lo(sym1+1)
addiu $2, $2, %lo(sym1)
# CHECK-LABEL: Section ({{[0-9]+}}) .rel.mips_hilo_5 {
# CHECK-NEXT: 0x8 R_MIPS_LO16 sym1
# CHECK-NEXT: 0x0 R_MIPS_HI16 sym1
# CHECK-NEXT: 0x4 R_MIPS_HI16 sym1
# CHECK-NEXT: 0xC R_MIPS_LO16 sym1
# CHECK-NEXT: }
# HILO 6: R_MIPS_HI16 must be followed by a matching R_MIPS_LO16.
# The second relocation matches if the symbol is the same and the
# offset is greater or equal. Smaller offsets are preferred so both
# R_MIPS_HI16's still match the same R_MIPS_LO16.
.section .mips_hilo_6, "ax", @progbits
lui $2, %hi(sym1)
lui $2, %hi(sym1)
addiu $2, $2, %lo(sym1+2)
addiu $2, $2, %lo(sym1+1)
# CHECK-LABEL: Section ({{[0-9]+}}) .rel.mips_hilo_6 {
# CHECK-NEXT: 0x8 R_MIPS_LO16 sym1
# CHECK-NEXT: 0x0 R_MIPS_HI16 sym1
# CHECK-NEXT: 0x4 R_MIPS_HI16 sym1
# CHECK-NEXT: 0xC R_MIPS_LO16 sym1
# CHECK-NEXT: }
# HILO 7: R_MIPS_HI16 must be followed by a matching R_MIPS_LO16.
# The second relocation matches if the symbol is the same and the
# offset is greater or equal so that the carry bit is correct. The two
# R_MIPS_HI16's therefore match different R_MIPS_LO16's.
.section .mips_hilo_7, "ax", @progbits
addiu $2, $2, %lo(sym1+1)
addiu $2, $2, %lo(sym1+6)
lui $2, %hi(sym1+4)
lui $2, %hi(sym1)
# CHECK-LABEL: Section ({{[0-9]+}}) .rel.mips_hilo_7 {
# CHECK-NEXT: 0xC R_MIPS_HI16 sym1
# CHECK-NEXT: 0x0 R_MIPS_LO16 sym1
# CHECK-NEXT: 0x8 R_MIPS_HI16 sym1
# CHECK-NEXT: 0x4 R_MIPS_LO16 sym1
# CHECK-NEXT: }
# HILO 8: R_MIPS_LO16's may be orphaned.
.section .mips_hilo_8, "ax", @progbits
lw $2, %lo(sym1)
# CHECK-LABEL: Section ({{[0-9]+}}) .rel.mips_hilo_8 {
# CHECK-NEXT: 0x0 R_MIPS_LO16 sym1
# CHECK-NEXT: }
# HILO 8b: Another example of 8. The R_MIPS_LO16 at 0x4 is orphaned.
.section .mips_hilo_8b, "ax", @progbits
lw $2, %lo(sym1)
lw $2, %lo(sym1)
lui $2, %hi(sym1)
# CHECK-LABEL: Section ({{[0-9]+}}) .rel.mips_hilo_8b {
# CHECK-NEXT: 0x8 R_MIPS_HI16 sym1
# CHECK-NEXT: 0x0 R_MIPS_LO16 sym1
# CHECK-NEXT: 0x4 R_MIPS_LO16 sym1
# CHECK-NEXT: }
# HILO 9: R_MIPS_HI16's don't need a matching R_MIPS_LO16 to immediately follow
# so long as there is one after the R_MIPS_HI16 somewhere. This isn't
# permitted by the ABI specification but has been allowed in practice
# for a very long time. The R_MIPS_HI16's should be ordered by the
# address they affect for purely cosmetic reasons.
.section .mips_hilo_9, "ax", @progbits
lw $2, %lo(sym1)
lui $2, %hi(sym1)
lui $2, %hi(sym1)
# CHECK-LABEL: Section ({{[0-9]+}}) .rel.mips_hilo_9 {
# CHECK-NEXT: 0x4 R_MIPS_HI16 sym1
# CHECK-NEXT: 0x8 R_MIPS_HI16 sym1
# CHECK-NEXT: 0x0 R_MIPS_LO16 sym1
# CHECK-NEXT: }
# HILO 10: R_MIPS_HI16's must have a matching R_MIPS_LO16 somewhere though.
# When this is impossible we have two possible bad behaviours
# depending on the linker implementation:
# * The linker silently computes the wrong value using a partially
# matching R_MIPS_LO16.
# * The linker rejects the relocation table as invalid.
# The latter is preferable since it's far easier to detect and debug so
# check that we encourage this behaviour by putting invalid
# R_MIPS_HI16's at the end of the relocation table where the risk of a
# partial match is very low.
.section .mips_hilo_10, "ax", @progbits
lui $2, %hi(sym1)
lw $2, %lo(sym1)
lui $2, %hi(sym2)
lui $2, %hi(sym3)
lw $2, %lo(sym3)
# CHECK-LABEL: Section ({{[0-9]+}}) .rel.mips_hilo_10 {
# CHECK-NEXT: 0x0 R_MIPS_HI16 sym1
# CHECK-NEXT: 0x4 R_MIPS_LO16 sym1
# CHECK-NEXT: 0xC R_MIPS_HI16 sym3
# CHECK-NEXT: 0x10 R_MIPS_LO16 sym3
# CHECK-NEXT: 0x8 R_MIPS_HI16 sym2
# CHECK-NEXT: }
# Now do the same tests for GOT/LO.
# The rules only apply to R_MIPS_GOT16 on local symbols which are also
# rewritten into section relative relocations.
# GOTLO 1: GOT/LO already match
.section .mips_gotlo_1, "ax", @progbits
lui $2, %got(local1)
addiu $2, $2, %lo(local1)
# CHECK-LABEL: Section ({{[0-9]+}}) .rel.mips_gotlo_1 {
# CHECK-NEXT: 0x0 R_MIPS_GOT16 .text
# CHECK-NEXT: 0x4 R_MIPS_LO16 .text
# CHECK-NEXT: }
# GOTLO 2: R_MIPS_GOT16 must be followed by a matching R_MIPS_LO16.
.section .mips_gotlo_2, "ax", @progbits
addiu $2, $2, %lo(local1)
lui $2, %got(local1)
# CHECK-LABEL: Section ({{[0-9]+}}) .rel.mips_gotlo_2 {
# CHECK-NEXT: 0x4 R_MIPS_GOT16 .text
# CHECK-NEXT: 0x0 R_MIPS_LO16 .text
# CHECK-NEXT: }
# GOTLO 3: R_MIPS_GOT16 must be followed by a matching R_MIPS_LO16.
# The second relocation matches if the symbol is the same.
.section .mips_gotlo_3, "ax", @progbits
addiu $2, $2, %lo(local1)
lui $2, %got(local2)
addiu $2, $2, %lo(local2)
lui $2, %got(local1)
# CHECK-LABEL: Section ({{[0-9]+}}) .rel.mips_gotlo_3 {
# CHECK-NEXT: 0xC R_MIPS_GOT16 .text
# CHECK-NEXT: 0x0 R_MIPS_LO16 .text
# CHECK-NEXT: 0x4 R_MIPS_GOT16 .text
# CHECK-NEXT: 0x8 R_MIPS_LO16 .text
# CHECK-NEXT: }
# GOTLO 3b: Same as 3 but a different starting order.
.section .mips_gotlo_3b, "ax", @progbits
addiu $2, $2, %lo(local1)
lui $2, %got(local1)
addiu $2, $2, %lo(local2)
lui $2, %got(local2)
# CHECK-LABEL: Section ({{[0-9]+}}) .rel.mips_gotlo_3b {
# CHECK-NEXT: 0x4 R_MIPS_GOT16 .text
# CHECK-NEXT: 0x0 R_MIPS_LO16 .text
# CHECK-NEXT: 0xC R_MIPS_GOT16 .text
# CHECK-NEXT: 0x8 R_MIPS_LO16 .text
# CHECK-NEXT: }
# GOTLO 4: R_MIPS_GOT16 must be followed by a matching R_MIPS_LO16.
# The second relocation matches if the symbol is the same and the
# offset is the same.
.section .mips_gotlo_4, "ax", @progbits
addiu $2, $2, %lo(local1)
addiu $2, $2, %lo(local1+4)
lui $2, %got(local1+4)
lui $2, %got(local1)
# CHECK-LABEL: Section ({{[0-9]+}}) .rel.mips_gotlo_4 {
# CHECK-NEXT: 0xC R_MIPS_GOT16 .text
# CHECK-NEXT: 0x0 R_MIPS_LO16 .text
# CHECK-NEXT: 0x8 R_MIPS_GOT16 .text
# CHECK-NEXT: 0x4 R_MIPS_LO16 .text
# CHECK-NEXT: }
# GOTLO 5: R_MIPS_GOT16 must be followed by a matching R_MIPS_LO16.
# The second relocation matches if the symbol is the same and the
# offset is greater or equal. Exact matches are preferred so both
# R_MIPS_GOT16's match the same R_MIPS_LO16.
.section .mips_gotlo_5, "ax", @progbits
lui $2, %got(local1)
lui $2, %got(local1)
addiu $2, $2, %lo(local1+1)
addiu $2, $2, %lo(local1)
# CHECK-LABEL: Section ({{[0-9]+}}) .rel.mips_gotlo_5 {
# CHECK-NEXT: 0x8 R_MIPS_LO16 .text
# CHECK-NEXT: 0x0 R_MIPS_GOT16 .text
# CHECK-NEXT: 0x4 R_MIPS_GOT16 .text
# CHECK-NEXT: 0xC R_MIPS_LO16 .text
# CHECK-NEXT: }
# GOTLO 6: R_MIPS_GOT16 must be followed by a matching R_MIPS_LO16.
# The second relocation matches if the symbol is the same and the
# offset is greater or equal. Smaller offsets are preferred so both
# R_MIPS_GOT16's still match the same R_MIPS_LO16.
.section .mips_gotlo_6, "ax", @progbits
lui $2, %got(local1)
lui $2, %got(local1)
addiu $2, $2, %lo(local1+2)
addiu $2, $2, %lo(local1+1)
# CHECK-LABEL: Section ({{[0-9]+}}) .rel.mips_gotlo_6 {
# CHECK-NEXT: 0x8 R_MIPS_LO16 .text
# CHECK-NEXT: 0x0 R_MIPS_GOT16 .text
# CHECK-NEXT: 0x4 R_MIPS_GOT16 .text
# CHECK-NEXT: 0xC R_MIPS_LO16 .text
# CHECK-NEXT: }
# GOTLO 7: R_MIPS_GOT16 must be followed by a matching R_MIPS_LO16.
# The second relocation matches if the symbol is the same and the
# offset is greater or equal so that the carry bit is correct. The two
# R_MIPS_GOT16's therefore match different R_MIPS_LO16's.
.section .mips_gotlo_7, "ax", @progbits
addiu $2, $2, %lo(local1+1)
addiu $2, $2, %lo(local1+6)
lui $2, %got(local1+4)
lui $2, %got(local1)
# CHECK-LABEL: Section ({{[0-9]+}}) .rel.mips_gotlo_7 {
# CHECK-NEXT: 0xC R_MIPS_GOT16 .text
# CHECK-NEXT: 0x0 R_MIPS_LO16 .text
# CHECK-NEXT: 0x8 R_MIPS_GOT16 .text
# CHECK-NEXT: 0x4 R_MIPS_LO16 .text
# CHECK-NEXT: }
# GOTLO 8: R_MIPS_LO16's may be orphaned.
.section .mips_gotlo_8, "ax", @progbits
lw $2, %lo(local1)
# CHECK-LABEL: Section ({{[0-9]+}}) .rel.mips_gotlo_8 {
# CHECK-NEXT: 0x0 R_MIPS_LO16 .text
# CHECK-NEXT: }
# GOTLO 8b: Another example of 8. The R_MIPS_LO16 at 0x4 is orphaned.
.section .mips_gotlo_8b, "ax", @progbits
lw $2, %lo(local1)
lw $2, %lo(local1)
lui $2, %got(local1)
# CHECK-LABEL: Section ({{[0-9]+}}) .rel.mips_gotlo_8b {
# CHECK-NEXT: 0x8 R_MIPS_GOT16 .text
# CHECK-NEXT: 0x0 R_MIPS_LO16 .text
# CHECK-NEXT: 0x4 R_MIPS_LO16 .text
# CHECK-NEXT: }
# GOTLO 9: R_MIPS_GOT16's don't need a matching R_MIPS_LO16 to immediately
# follow so long as there is one after the R_MIPS_GOT16 somewhere.
# This isn't permitted by the ABI specification but has been allowed
# in practice for a very long time. The R_MIPS_GOT16's should be
# ordered by the address they affect for purely cosmetic reasons.
.section .mips_gotlo_9, "ax", @progbits
lw $2, %lo(local1)
lui $2, %got(local1)
lui $2, %got(local1)
# CHECK-LABEL: Section ({{[0-9]+}}) .rel.mips_gotlo_9 {
# CHECK-NEXT: 0x4 R_MIPS_GOT16 .text
# CHECK-NEXT: 0x8 R_MIPS_GOT16 .text
# CHECK-NEXT: 0x0 R_MIPS_LO16 .text
# CHECK-NEXT: }
# GOTLO 10: R_MIPS_GOT16's must have a matching R_MIPS_LO16 somewhere though.
# When this is impossible we have two possible bad behaviours
# depending on the linker implementation:
# * The linker silently computes the wrong value using a partially
# matching R_MIPS_LO16.
# * The linker rejects the relocation table as invalid.
# The latter is preferable since it's far easier to detect and debug
# so check that we encourage this behaviour by putting invalid
# R_MIPS_GOT16's at the end of the relocation table where the risk of
# a partial match is very low.
.section .mips_gotlo_10, "ax", @progbits
lui $2, %got(local1)
lw $2, %lo(local1)
lui $2, %got(local2)
lui $2, %got(local3)
lw $2, %lo(local3)
# CHECK-LABEL: Section ({{[0-9]+}}) .rel.mips_gotlo_10 {
# CHECK-NEXT: 0x0 R_MIPS_GOT16 .text
# CHECK-NEXT: 0x4 R_MIPS_LO16 .text
# CHECK-NEXT: 0xC R_MIPS_GOT16 .text
# CHECK-NEXT: 0x10 R_MIPS_LO16 .text
# CHECK-NEXT: 0x8 R_MIPS_GOT16 .text
# CHECK-NEXT: }
# Finally, do test 2 for R_MIPS_GOT16 on external symbols to prove they are
# exempt from the rules for local symbols.
# External GOTLO 2: R_MIPS_GOT16 must be followed by a matching R_MIPS_LO16.
.section .mips_ext_gotlo_2, "ax", @progbits
addiu $2, $2, %lo(sym1)
lui $2, %got(sym1)
# CHECK-LABEL: Section ({{[0-9]+}}) .rel.mips_ext_gotlo_2 {
# CHECK-NEXT: 0x0 R_MIPS_LO16 sym1
# CHECK-NEXT: 0x4 R_MIPS_GOT16 sym1
# CHECK-NEXT: }
# Define some local symbols.
.text
nop
local1: nop
local2: nop
local3: nop
|