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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
|
//===- lib/ReaderWriter/ELF/Mips/MipsRelocationHandler.cpp ----------------===//
//
// The LLVM Linker
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "MipsTargetHandler.h"
#include "MipsLinkingContext.h"
#include "MipsRelocationHandler.h"
#include "lld/ReaderWriter/RelocationHelperFunctions.h"
using namespace lld;
using namespace elf;
using namespace llvm::ELF;
using namespace llvm::support;
namespace {
enum class CrossJumpMode {
None, // Not a jump or non-isa-cross jump
ToRegular, // cross isa jump to regular symbol
ToMicro // cross isa jump to microMips symbol
};
struct MipsRelocationParams {
uint8_t _size; // Relocations's size in bytes
uint64_t _mask; // Read/write mask of relocation
uint8_t _shift; // Relocation's addendum left shift size
bool _shuffle; // Relocation's addendum/result needs to be shuffled
};
}
static MipsRelocationParams getRelocationParams(uint32_t rType) {
switch (rType) {
case llvm::ELF::R_MIPS_NONE:
return {4, 0x0, 0, false};
case llvm::ELF::R_MIPS_64:
return {8, 0xffffffffffffffffull, 0, false};
case llvm::ELF::R_MIPS_32:
case llvm::ELF::R_MIPS_GPREL32:
case llvm::ELF::R_MIPS_PC32:
case LLD_R_MIPS_32_HI16:
return {4, 0xffffffff, 0, false};
case llvm::ELF::R_MIPS_26:
case LLD_R_MIPS_GLOBAL_26:
return {4, 0x3ffffff, 2, false};
case llvm::ELF::R_MIPS_HI16:
case llvm::ELF::R_MIPS_LO16:
case llvm::ELF::R_MIPS_GPREL16:
case llvm::ELF::R_MIPS_GOT16:
case llvm::ELF::R_MIPS_TLS_DTPREL_HI16:
case llvm::ELF::R_MIPS_TLS_DTPREL_LO16:
case llvm::ELF::R_MIPS_TLS_TPREL_HI16:
case llvm::ELF::R_MIPS_TLS_TPREL_LO16:
case LLD_R_MIPS_HI16:
case LLD_R_MIPS_LO16:
return {4, 0xffff, 0, false};
case llvm::ELF::R_MICROMIPS_TLS_DTPREL_HI16:
case llvm::ELF::R_MICROMIPS_TLS_DTPREL_LO16:
case llvm::ELF::R_MICROMIPS_TLS_TPREL_HI16:
case llvm::ELF::R_MICROMIPS_TLS_TPREL_LO16:
return {4, 0xffff, 0, true};
case llvm::ELF::R_MICROMIPS_26_S1:
case LLD_R_MICROMIPS_GLOBAL_26_S1:
return {4, 0x3ffffff, 1, true};
case llvm::ELF::R_MICROMIPS_HI16:
case llvm::ELF::R_MICROMIPS_LO16:
case llvm::ELF::R_MICROMIPS_GOT16:
return {4, 0xffff, 0, true};
case llvm::ELF::R_MICROMIPS_PC16_S1:
return {4, 0xffff, 1, true};
case llvm::ELF::R_MICROMIPS_PC7_S1:
return {4, 0x7f, 1, false};
case llvm::ELF::R_MICROMIPS_PC10_S1:
return {4, 0x3ff, 1, false};
case llvm::ELF::R_MICROMIPS_PC23_S2:
return {4, 0x7fffff, 2, true};
case llvm::ELF::R_MIPS_CALL16:
case llvm::ELF::R_MIPS_TLS_GD:
case llvm::ELF::R_MIPS_TLS_LDM:
case llvm::ELF::R_MIPS_TLS_GOTTPREL:
return {4, 0xffff, 0, false};
case llvm::ELF::R_MICROMIPS_CALL16:
case llvm::ELF::R_MICROMIPS_TLS_GD:
case llvm::ELF::R_MICROMIPS_TLS_LDM:
case llvm::ELF::R_MICROMIPS_TLS_GOTTPREL:
return {4, 0xffff, 0, true};
case R_MIPS_JALR:
return {4, 0x0, 0, false};
case R_MICROMIPS_JALR:
return {4, 0x0, 0, true};
case R_MIPS_REL32:
case R_MIPS_JUMP_SLOT:
case R_MIPS_COPY:
case R_MIPS_TLS_DTPMOD32:
case R_MIPS_TLS_DTPREL32:
case R_MIPS_TLS_TPREL32:
// Ignore runtime relocations.
return {4, 0x0, 0, false};
case LLD_R_MIPS_GLOBAL_GOT:
case LLD_R_MIPS_STO_PLT:
// Do nothing.
return {4, 0x0, 0, false};
default:
llvm_unreachable("Unknown relocation");
}
}
template <size_t BITS, class T> inline T signExtend(T val) {
if (val & (T(1) << (BITS - 1)))
val |= T(-1) << BITS;
return val;
}
/// \brief R_MIPS_32
/// local/external: word32 S + A (truncate)
static uint32_t reloc32(uint64_t S, int64_t A) { return S + A; }
/// \brief R_MIPS_64
/// local/external: word64 S + A (truncate)
static uint64_t reloc64(uint64_t S, int64_t A) { return S + A; }
/// \brief R_MIPS_PC32
/// local/external: word32 S + A i- P (truncate)
static uint32_t relocpc32(uint64_t P, uint64_t S, int64_t A) {
return S + A - P;
}
/// \brief R_MIPS_26, R_MICROMIPS_26_S1
/// local : ((A | ((P + 4) & 0x3F000000)) + S) >> 2
static uint32_t reloc26loc(uint64_t P, uint64_t S, int32_t A, uint32_t shift) {
uint32_t result = (A | ((P + 4) & (0xfc000000 << shift))) + S;
return result >> shift;
}
/// \brief LLD_R_MIPS_GLOBAL_26, LLD_R_MICROMIPS_GLOBAL_26_S1
/// external: (sign-extend(A) + S) >> 2
static uint32_t reloc26ext(uint64_t S, int32_t A, uint32_t shift) {
uint32_t result = shift == 1 ? signExtend<27>(A) : signExtend<28>(A);
return (result + S) >> shift;
}
/// \brief R_MIPS_HI16, R_MIPS_TLS_DTPREL_HI16, R_MIPS_TLS_TPREL_HI16,
/// R_MICROMIPS_HI16, R_MICROMIPS_TLS_DTPREL_HI16, R_MICROMIPS_TLS_TPREL_HI16,
/// LLD_R_MIPS_HI16
/// local/external: hi16 (AHL + S) - (short)(AHL + S) (truncate)
/// _gp_disp : hi16 (AHL + GP - P) - (short)(AHL + GP - P) (verify)
static uint32_t relocHi16(uint64_t P, uint64_t S, int64_t AHL, bool isGPDisp) {
int32_t result = isGPDisp ? AHL + S - P : AHL + S;
return (result + 0x8000) >> 16;
}
/// \brief R_MIPS_LO16, R_MIPS_TLS_DTPREL_LO16, R_MIPS_TLS_TPREL_LO16,
/// R_MICROMIPS_LO16, R_MICROMIPS_TLS_DTPREL_LO16, R_MICROMIPS_TLS_TPREL_LO16,
/// LLD_R_MIPS_LO16
/// local/external: lo16 AHL + S (truncate)
/// _gp_disp : lo16 AHL + GP - P + 4 (verify)
static uint32_t relocLo16(uint64_t P, uint64_t S, int64_t AHL, bool isGPDisp,
bool micro) {
int32_t result = isGPDisp ? AHL + S - P + (micro ? 3 : 4) : AHL + S;
return result;
}
/// \brief R_MIPS_GOT16, R_MIPS_CALL16, R_MICROMIPS_GOT16, R_MICROMIPS_CALL16
/// rel16 G (verify)
static uint32_t relocGOT(uint64_t S, uint64_t GP) {
int32_t G = (int32_t)(S - GP);
return G;
}
/// \brief R_MIPS_GPREL16
/// local: sign-extend(A) + S + GP0 - GP
/// external: sign-extend(A) + S - GP
static uint32_t relocGPRel16(uint64_t S, int64_t A, uint64_t GP) {
// We added GP0 to addendum for a local symbol during a Relocation pass.
int32_t result = signExtend<16>(A) + S - GP;
return result;
}
/// \brief R_MIPS_GPREL32
/// local: rel32 A + S + GP0 - GP (truncate)
static uint32_t relocGPRel32(uint64_t S, int64_t A, uint64_t GP) {
// We added GP0 to addendum for a local symbol during a Relocation pass.
int32_t result = A + S - GP;
return result;
}
/// \brief R_MICROMIPS_PC7_S1
static uint32_t relocPc7(uint64_t P, uint64_t S, int64_t A) {
A = signExtend<8>(A);
int32_t result = S + A - P;
return result >> 1;
}
/// \brief R_MICROMIPS_PC10_S1
static uint32_t relocPc10(uint64_t P, uint64_t S, int64_t A) {
A = signExtend<11>(A);
int32_t result = S + A - P;
return result >> 1;
}
/// \brief R_MICROMIPS_PC16_S1
static uint32_t relocPc16(uint64_t P, uint64_t S, int64_t A) {
A = signExtend<17>(A);
int32_t result = S + A - P;
return result >> 1;
}
/// \brief R_MICROMIPS_PC23_S2
static uint32_t relocPc23(uint64_t P, uint64_t S, int64_t A) {
A = signExtend<25>(A);
int32_t result = S + A - P;
// Check addiupc 16MB range.
if (result + 0x1000000 >= 0x2000000)
llvm::errs() << "The addiupc instruction immediate "
<< llvm::format_hex(result, 10) << " is out of range.\n";
return result >> 2;
}
/// \brief LLD_R_MIPS_32_HI16
static uint32_t reloc32hi16(uint64_t S, int64_t A) {
return (S + A + 0x8000) & 0xffff0000;
}
static std::error_code adjustJumpOpCode(uint64_t &ins, uint64_t tgt,
CrossJumpMode mode) {
if (mode == CrossJumpMode::None)
return std::error_code();
bool toMicro = mode == CrossJumpMode::ToMicro;
uint32_t opNative = toMicro ? 0x03 : 0x3d;
uint32_t opCross = toMicro ? 0x1d : 0x3c;
if ((tgt & 1) != toMicro)
return make_dynamic_error_code(
Twine("Incorrect bit 0 for the jalx target"));
if (tgt & 2)
return make_dynamic_error_code(Twine("The jalx target 0x") +
Twine::utohexstr(tgt) +
" is not word-aligned");
uint8_t op = ins >> 26;
if (op != opNative && op != opCross)
return make_dynamic_error_code(Twine("Unsupported jump opcode (0x") +
Twine::utohexstr(op) +
") for ISA modes cross call");
ins = (ins & ~(0x3f << 26)) | (opCross << 26);
return std::error_code();
}
static bool isMicroMipsAtom(const Atom *a) {
if (const auto *da = dyn_cast<DefinedAtom>(a))
return da->codeModel() == DefinedAtom::codeMipsMicro ||
da->codeModel() == DefinedAtom::codeMipsMicroPIC;
return false;
}
static CrossJumpMode getCrossJumpMode(const Reference &ref) {
if (!isa<DefinedAtom>(ref.target()))
return CrossJumpMode::None;
bool isTgtMicro = isMicroMipsAtom(ref.target());
switch (ref.kindValue()) {
case R_MIPS_26:
case LLD_R_MIPS_GLOBAL_26:
return isTgtMicro ? CrossJumpMode::ToMicro : CrossJumpMode::None;
case R_MICROMIPS_26_S1:
case LLD_R_MICROMIPS_GLOBAL_26_S1:
return isTgtMicro ? CrossJumpMode::None : CrossJumpMode::ToRegular;
default:
return CrossJumpMode::None;
}
}
static uint32_t microShuffle(uint32_t ins) {
return ((ins & 0xffff) << 16) | ((ins & 0xffff0000) >> 16);
}
static ErrorOr<uint64_t> calculateRelocation(const Reference &ref,
uint64_t tgtAddr, uint64_t relAddr,
uint64_t gpAddr, bool isGP) {
bool isCrossJump = getCrossJumpMode(ref) != CrossJumpMode::None;
switch (ref.kindValue()) {
case R_MIPS_NONE:
return 0;
case R_MIPS_32:
return reloc32(tgtAddr, ref.addend());
case R_MIPS_64:
return reloc64(tgtAddr, ref.addend());
case R_MIPS_26:
return reloc26loc(relAddr, tgtAddr, ref.addend(), 2);
case R_MICROMIPS_26_S1:
return reloc26loc(relAddr, tgtAddr, ref.addend(), isCrossJump ? 2 : 1);
case R_MIPS_HI16:
return relocHi16(relAddr, tgtAddr, ref.addend(), isGP);
case R_MICROMIPS_HI16:
return relocHi16(relAddr, tgtAddr, ref.addend(), isGP);
case R_MIPS_LO16:
return relocLo16(relAddr, tgtAddr, ref.addend(), isGP, false);
case R_MICROMIPS_LO16:
return relocLo16(relAddr, tgtAddr, ref.addend(), isGP, true);
case R_MIPS_GOT16:
case R_MIPS_CALL16:
return relocGOT(tgtAddr, gpAddr);
case R_MICROMIPS_GOT16:
case R_MICROMIPS_CALL16:
return relocGOT(tgtAddr, gpAddr);
case R_MICROMIPS_PC7_S1:
return relocPc7(relAddr, tgtAddr, ref.addend());
case R_MICROMIPS_PC10_S1:
return relocPc10(relAddr, tgtAddr, ref.addend());
case R_MICROMIPS_PC16_S1:
return relocPc16(relAddr, tgtAddr, ref.addend());
case R_MICROMIPS_PC23_S2:
return relocPc23(relAddr, tgtAddr, ref.addend());
case R_MIPS_TLS_GD:
case R_MIPS_TLS_LDM:
case R_MIPS_TLS_GOTTPREL:
case R_MICROMIPS_TLS_GD:
case R_MICROMIPS_TLS_LDM:
case R_MICROMIPS_TLS_GOTTPREL:
return relocGOT(tgtAddr, gpAddr);
case R_MIPS_TLS_DTPREL_HI16:
case R_MIPS_TLS_TPREL_HI16:
case R_MICROMIPS_TLS_DTPREL_HI16:
case R_MICROMIPS_TLS_TPREL_HI16:
return relocHi16(0, tgtAddr, ref.addend(), false);
case R_MIPS_TLS_DTPREL_LO16:
case R_MIPS_TLS_TPREL_LO16:
return relocLo16(0, tgtAddr, ref.addend(), false, false);
case R_MICROMIPS_TLS_DTPREL_LO16:
case R_MICROMIPS_TLS_TPREL_LO16:
return relocLo16(0, tgtAddr, ref.addend(), false, true);
case R_MIPS_GPREL16:
return relocGPRel16(tgtAddr, ref.addend(), gpAddr);
case R_MIPS_GPREL32:
return relocGPRel32(tgtAddr, ref.addend(), gpAddr);
case R_MIPS_JALR:
case R_MICROMIPS_JALR:
// We do not do JALR optimization now.
return 0;
case R_MIPS_REL32:
case R_MIPS_JUMP_SLOT:
case R_MIPS_COPY:
case R_MIPS_TLS_DTPMOD32:
case R_MIPS_TLS_DTPREL32:
case R_MIPS_TLS_TPREL32:
// Ignore runtime relocations.
return 0;
case R_MIPS_PC32:
return relocpc32(relAddr, tgtAddr, ref.addend());
case LLD_R_MIPS_GLOBAL_GOT:
// Do nothing.
case LLD_R_MIPS_32_HI16:
return reloc32hi16(tgtAddr, ref.addend());
case LLD_R_MIPS_GLOBAL_26:
return reloc26ext(tgtAddr, ref.addend(), 2);
case LLD_R_MICROMIPS_GLOBAL_26_S1:
return reloc26ext(tgtAddr, ref.addend(), isCrossJump ? 2 : 1);
case LLD_R_MIPS_HI16:
return relocHi16(0, tgtAddr, 0, false);
case LLD_R_MIPS_LO16:
return relocLo16(0, tgtAddr, 0, false, false);
case LLD_R_MIPS_STO_PLT:
// Do nothing.
return 0;
default:
return make_unhandled_reloc_error();
}
}
template <class ELFT>
static uint64_t relocRead(const MipsRelocationParams ¶ms,
const uint8_t *loc) {
uint64_t data;
switch (params._size) {
case 4:
data = endian::read<uint32_t, ELFT::TargetEndianness, unaligned>(loc);
break;
case 8:
data = endian::read<uint64_t, ELFT::TargetEndianness, unaligned>(loc);
break;
default:
llvm_unreachable("Unexpected size");
}
if (params._shuffle)
data = microShuffle(data);
return data;
}
template <class ELFT>
static void relocWrite(uint64_t data, const MipsRelocationParams ¶ms,
uint8_t *loc) {
if (params._shuffle)
data = microShuffle(data);
switch (params._size) {
case 4:
endian::write<uint32_t, ELFT::TargetEndianness, unaligned>(loc, data);
break;
case 8:
endian::write<uint64_t, ELFT::TargetEndianness, unaligned>(loc, data);
break;
default:
llvm_unreachable("Unexpected size");
}
}
template <class ELFT>
std::error_code MipsRelocationHandler<ELFT>::applyRelocation(
ELFWriter &writer, llvm::FileOutputBuffer &buf, const lld::AtomLayout &atom,
const Reference &ref) const {
if (ref.kindNamespace() != lld::Reference::KindNamespace::ELF)
return std::error_code();
assert(ref.kindArch() == Reference::KindArch::Mips);
auto &targetLayout = static_cast<MipsTargetLayout<ELFT> &>(
_ctx.getTargetHandler<ELFT>().getTargetLayout());
AtomLayout *gpAtom = targetLayout.getGP();
uint64_t gpAddr = gpAtom ? gpAtom->_virtualAddr : 0;
AtomLayout *gpDispAtom = targetLayout.getGPDisp();
bool isGpDisp = gpDispAtom && ref.target() == gpDispAtom->_atom;
uint8_t *atomContent = buf.getBufferStart() + atom._fileOffset;
uint8_t *location = atomContent + ref.offsetInAtom();
uint64_t targetVAddress = writer.addressOfAtom(ref.target());
uint64_t relocVAddress = atom._virtualAddr + ref.offsetInAtom();
if (isMicroMipsAtom(ref.target()))
targetVAddress |= 1;
auto res =
calculateRelocation(ref, targetVAddress, relocVAddress, gpAddr, isGpDisp);
if (auto ec = res.getError())
return ec;
auto params = getRelocationParams(ref.kindValue());
uint64_t ins = relocRead<ELFT>(params, location);
if (auto ec = adjustJumpOpCode(ins, targetVAddress, getCrossJumpMode(ref)))
return ec;
ins = (ins & ~params._mask) | (*res & params._mask);
relocWrite<ELFT>(ins, params, location);
return std::error_code();
}
template <class ELFT>
Reference::Addend
MipsRelocationHandler<ELFT>::readAddend(Reference::KindValue kind,
const uint8_t *content) {
auto params = getRelocationParams(kind);
uint64_t ins = relocRead<ELFT>(params, content);
return (ins & params._mask) << params._shift;
}
namespace lld {
namespace elf {
template class MipsRelocationHandler<Mips32ELType>;
template class MipsRelocationHandler<Mips32BEType>;
template class MipsRelocationHandler<Mips64ELType>;
template class MipsRelocationHandler<Mips64BEType>;
template <>
std::unique_ptr<TargetRelocationHandler>
createMipsRelocationHandler<Mips32ELType>(MipsLinkingContext &ctx) {
return std::unique_ptr<TargetRelocationHandler>(
new MipsRelocationHandler<Mips32ELType>(ctx));
}
template <>
std::unique_ptr<TargetRelocationHandler>
createMipsRelocationHandler<Mips64ELType>(MipsLinkingContext &ctx) {
return std::unique_ptr<TargetRelocationHandler>(
new MipsRelocationHandler<Mips64ELType>(ctx));
}
} // elf
} // lld
|