1 //===- MIPS.cpp -----------------------------------------------------------===//
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "InputFiles.h"
11 #include "OutputSections.h"
13 #include "SyntheticSections.h"
16 #include "lld/Common/ErrorHandler.h"
17 #include "llvm/Object/ELF.h"
18 #include "llvm/Support/Endian.h"
21 using namespace llvm::object;
22 using namespace llvm::support::endian;
23 using namespace llvm::ELF;
25 using namespace lld::elf;
28 template <class ELFT> class MIPS final : public TargetInfo {
31 uint32_t calcEFlags() const override;
32 RelExpr getRelExpr(RelType Type, const Symbol &S,
33 const uint8_t *Loc) const override;
34 int64_t getImplicitAddend(const uint8_t *Buf, RelType Type) const override;
35 RelType getDynRel(RelType Type) const override;
36 void writeGotPlt(uint8_t *Buf, const Symbol &S) const override;
37 void writePltHeader(uint8_t *Buf) const override;
38 void writePlt(uint8_t *Buf, uint64_t GotPltEntryAddr, uint64_t PltEntryAddr,
39 int32_t Index, unsigned RelOff) const override;
40 bool needsThunk(RelExpr Expr, RelType Type, const InputFile *File,
41 uint64_t BranchAddr, const Symbol &S) const override;
42 void relocateOne(uint8_t *Loc, RelType Type, uint64_t Val) const override;
43 bool usesOnlyLowPageBits(RelType Type) const override;
47 template <class ELFT> MIPS<ELFT>::MIPS() {
48 GotPltHeaderEntriesNum = 2;
49 DefaultMaxPageSize = 65536;
50 GotEntrySize = sizeof(typename ELFT::uint);
51 GotPltEntrySize = sizeof(typename ELFT::uint);
52 GotBaseSymInGotPlt = false;
55 CopyRel = R_MIPS_COPY;
56 NoneRel = R_MIPS_NONE;
57 PltRel = R_MIPS_JUMP_SLOT;
60 // Set `sigrie 1` as a trap instruction.
61 write32(TrapInstr.data(), 0x04170001);
64 RelativeRel = (R_MIPS_64 << 8) | R_MIPS_REL32;
65 TlsGotRel = R_MIPS_TLS_TPREL64;
66 TlsModuleIndexRel = R_MIPS_TLS_DTPMOD64;
67 TlsOffsetRel = R_MIPS_TLS_DTPREL64;
69 RelativeRel = R_MIPS_REL32;
70 TlsGotRel = R_MIPS_TLS_TPREL32;
71 TlsModuleIndexRel = R_MIPS_TLS_DTPMOD32;
72 TlsOffsetRel = R_MIPS_TLS_DTPREL32;
76 template <class ELFT> uint32_t MIPS<ELFT>::calcEFlags() const {
77 return calcMipsEFlags<ELFT>();
81 RelExpr MIPS<ELFT>::getRelExpr(RelType Type, const Symbol &S,
82 const uint8_t *Loc) const {
83 // See comment in the calculateMipsRelChain.
84 if (ELFT::Is64Bits || Config->MipsN32Abi)
89 case R_MICROMIPS_JALR:
93 case R_MICROMIPS_GPREL16:
94 case R_MICROMIPS_GPREL7_S2:
97 case R_MICROMIPS_26_S1:
99 case R_MICROMIPS_PC26_S1:
105 case R_MICROMIPS_HI16:
106 case R_MICROMIPS_LO16:
107 // R_MIPS_HI16/R_MIPS_LO16 relocations against _gp_disp calculate
108 // offset between start of function and 'gp' value which by default
109 // equal to the start of .got section. In that case we consider these
110 // relocations as relative.
111 if (&S == ElfSym::MipsGpDisp)
112 return R_MIPS_GOT_GP_PC;
113 if (&S == ElfSym::MipsLocalGp)
114 return R_MIPS_GOT_GP;
118 case R_MIPS_GOT_OFST:
120 case R_MIPS_TLS_DTPREL_HI16:
121 case R_MIPS_TLS_DTPREL_LO16:
122 case R_MIPS_TLS_DTPREL32:
123 case R_MIPS_TLS_DTPREL64:
124 case R_MIPS_TLS_TPREL_HI16:
125 case R_MIPS_TLS_TPREL_LO16:
126 case R_MIPS_TLS_TPREL32:
127 case R_MIPS_TLS_TPREL64:
128 case R_MICROMIPS_TLS_DTPREL_HI16:
129 case R_MICROMIPS_TLS_DTPREL_LO16:
130 case R_MICROMIPS_TLS_TPREL_HI16:
131 case R_MICROMIPS_TLS_TPREL_LO16:
140 case R_MICROMIPS_PC7_S1:
141 case R_MICROMIPS_PC10_S1:
142 case R_MICROMIPS_PC16_S1:
143 case R_MICROMIPS_PC18_S3:
144 case R_MICROMIPS_PC19_S2:
145 case R_MICROMIPS_PC23_S2:
146 case R_MICROMIPS_PC21_S1:
149 case R_MICROMIPS_GOT16:
151 return R_MIPS_GOT_LOCAL_PAGE;
154 case R_MIPS_GOT_DISP:
155 case R_MIPS_TLS_GOTTPREL:
156 case R_MICROMIPS_CALL16:
157 case R_MICROMIPS_TLS_GOTTPREL:
158 return R_MIPS_GOT_OFF;
159 case R_MIPS_CALL_HI16:
160 case R_MIPS_CALL_LO16:
161 case R_MIPS_GOT_HI16:
162 case R_MIPS_GOT_LO16:
163 case R_MICROMIPS_CALL_HI16:
164 case R_MICROMIPS_CALL_LO16:
165 case R_MICROMIPS_GOT_HI16:
166 case R_MICROMIPS_GOT_LO16:
167 return R_MIPS_GOT_OFF32;
168 case R_MIPS_GOT_PAGE:
169 return R_MIPS_GOT_LOCAL_PAGE;
171 case R_MICROMIPS_TLS_GD:
174 case R_MICROMIPS_TLS_LDM:
183 template <class ELFT> RelType MIPS<ELFT>::getDynRel(RelType Type) const {
184 if (Type == R_MIPS_32 || Type == R_MIPS_64)
189 template <class ELFT>
190 void MIPS<ELFT>::writeGotPlt(uint8_t *Buf, const Symbol &) const {
191 uint64_t VA = In.Plt->getVA();
194 write32<ELFT::TargetEndianness>(Buf, VA);
197 template <endianness E> static uint32_t readShuffle(const uint8_t *Loc) {
198 // The major opcode of a microMIPS instruction needs to appear
199 // in the first 16-bit word (lowest address) for efficient hardware
200 // decode so that it knows if the instruction is 16-bit or 32-bit
201 // as early as possible. To do so, little-endian binaries keep 16-bit
202 // words in a big-endian order. That is why we have to swap these
203 // words to get a correct value.
204 uint32_t V = read32<E>(Loc);
205 if (E == support::little)
206 return (V << 16) | (V >> 16);
210 template <endianness E>
211 static void writeValue(uint8_t *Loc, uint64_t V, uint8_t BitsSize,
213 uint32_t Instr = read32<E>(Loc);
214 uint32_t Mask = 0xffffffff >> (32 - BitsSize);
215 uint32_t Data = (Instr & ~Mask) | ((V >> Shift) & Mask);
216 write32<E>(Loc, Data);
219 template <endianness E>
220 static void writeShuffleValue(uint8_t *Loc, uint64_t V, uint8_t BitsSize,
222 // See comments in readShuffle for purpose of this code.
223 uint16_t *Words = (uint16_t *)Loc;
224 if (E == support::little)
225 std::swap(Words[0], Words[1]);
227 writeValue<E>(Loc, V, BitsSize, Shift);
229 if (E == support::little)
230 std::swap(Words[0], Words[1]);
233 template <endianness E>
234 static void writeMicroRelocation16(uint8_t *Loc, uint64_t V, uint8_t BitsSize,
236 uint16_t Instr = read16<E>(Loc);
237 uint16_t Mask = 0xffff >> (16 - BitsSize);
238 uint16_t Data = (Instr & ~Mask) | ((V >> Shift) & Mask);
239 write16<E>(Loc, Data);
242 template <class ELFT> void MIPS<ELFT>::writePltHeader(uint8_t *Buf) const {
243 const endianness E = ELFT::TargetEndianness;
245 uint64_t GotPlt = In.GotPlt->getVA();
246 uint64_t Plt = In.Plt->getVA();
247 // Overwrite trap instructions written by Writer::writeTrapInstr.
248 memset(Buf, 0, PltHeaderSize);
250 write16<E>(Buf, isMipsR6() ? 0x7860 : 0x7980); // addiupc v1, (GOTPLT) - .
251 write16<E>(Buf + 4, 0xff23); // lw $25, 0($3)
252 write16<E>(Buf + 8, 0x0535); // subu16 $2, $2, $3
253 write16<E>(Buf + 10, 0x2525); // srl16 $2, $2, 2
254 write16<E>(Buf + 12, 0x3302); // addiu $24, $2, -2
255 write16<E>(Buf + 14, 0xfffe);
256 write16<E>(Buf + 16, 0x0dff); // move $15, $31
258 write16<E>(Buf + 18, 0x0f83); // move $28, $3
259 write16<E>(Buf + 20, 0x472b); // jalrc $25
260 write16<E>(Buf + 22, 0x0c00); // nop
261 relocateOne(Buf, R_MICROMIPS_PC19_S2, GotPlt - Plt);
263 write16<E>(Buf + 18, 0x45f9); // jalrc $25
264 write16<E>(Buf + 20, 0x0f83); // move $28, $3
265 write16<E>(Buf + 22, 0x0c00); // nop
266 relocateOne(Buf, R_MICROMIPS_PC23_S2, GotPlt - Plt);
271 if (Config->MipsN32Abi) {
272 write32<E>(Buf, 0x3c0e0000); // lui $14, %hi(&GOTPLT[0])
273 write32<E>(Buf + 4, 0x8dd90000); // lw $25, %lo(&GOTPLT[0])($14)
274 write32<E>(Buf + 8, 0x25ce0000); // addiu $14, $14, %lo(&GOTPLT[0])
275 write32<E>(Buf + 12, 0x030ec023); // subu $24, $24, $14
276 write32<E>(Buf + 16, 0x03e07825); // move $15, $31
277 write32<E>(Buf + 20, 0x0018c082); // srl $24, $24, 2
278 } else if (ELFT::Is64Bits) {
279 write32<E>(Buf, 0x3c0e0000); // lui $14, %hi(&GOTPLT[0])
280 write32<E>(Buf + 4, 0xddd90000); // ld $25, %lo(&GOTPLT[0])($14)
281 write32<E>(Buf + 8, 0x25ce0000); // addiu $14, $14, %lo(&GOTPLT[0])
282 write32<E>(Buf + 12, 0x030ec023); // subu $24, $24, $14
283 write32<E>(Buf + 16, 0x03e07825); // move $15, $31
284 write32<E>(Buf + 20, 0x0018c0c2); // srl $24, $24, 3
286 write32<E>(Buf, 0x3c1c0000); // lui $28, %hi(&GOTPLT[0])
287 write32<E>(Buf + 4, 0x8f990000); // lw $25, %lo(&GOTPLT[0])($28)
288 write32<E>(Buf + 8, 0x279c0000); // addiu $28, $28, %lo(&GOTPLT[0])
289 write32<E>(Buf + 12, 0x031cc023); // subu $24, $24, $28
290 write32<E>(Buf + 16, 0x03e07825); // move $15, $31
291 write32<E>(Buf + 20, 0x0018c082); // srl $24, $24, 2
294 uint32_t JalrInst = Config->ZHazardplt ? 0x0320fc09 : 0x0320f809;
295 write32<E>(Buf + 24, JalrInst); // jalr.hb $25 or jalr $25
296 write32<E>(Buf + 28, 0x2718fffe); // subu $24, $24, 2
298 uint64_t GotPlt = In.GotPlt->getVA();
299 writeValue<E>(Buf, GotPlt + 0x8000, 16, 16);
300 writeValue<E>(Buf + 4, GotPlt, 16, 0);
301 writeValue<E>(Buf + 8, GotPlt, 16, 0);
304 template <class ELFT>
305 void MIPS<ELFT>::writePlt(uint8_t *Buf, uint64_t GotPltEntryAddr,
306 uint64_t PltEntryAddr, int32_t Index,
307 unsigned RelOff) const {
308 const endianness E = ELFT::TargetEndianness;
310 // Overwrite trap instructions written by Writer::writeTrapInstr.
311 memset(Buf, 0, PltEntrySize);
314 write16<E>(Buf, 0x7840); // addiupc $2, (GOTPLT) - .
315 write16<E>(Buf + 4, 0xff22); // lw $25, 0($2)
316 write16<E>(Buf + 8, 0x0f02); // move $24, $2
317 write16<E>(Buf + 10, 0x4723); // jrc $25 / jr16 $25
318 relocateOne(Buf, R_MICROMIPS_PC19_S2, GotPltEntryAddr - PltEntryAddr);
320 write16<E>(Buf, 0x7900); // addiupc $2, (GOTPLT) - .
321 write16<E>(Buf + 4, 0xff22); // lw $25, 0($2)
322 write16<E>(Buf + 8, 0x4599); // jrc $25 / jr16 $25
323 write16<E>(Buf + 10, 0x0f02); // move $24, $2
324 relocateOne(Buf, R_MICROMIPS_PC23_S2, GotPltEntryAddr - PltEntryAddr);
329 uint32_t JrInst = isMipsR6() ? (Config->ZHazardplt ? 0x03200409 : 0x03200009)
330 : (Config->ZHazardplt ? 0x03200408 : 0x03200008);
332 write32<E>(Buf, 0x3c0f0000); // lui $15, %hi(.got.plt entry)
333 write32<E>(Buf + 4, 0x8df90000); // l[wd] $25, %lo(.got.plt entry)($15)
334 write32<E>(Buf + 8, JrInst); // jr $25 / jr.hb $25
335 write32<E>(Buf + 12, 0x25f80000); // addiu $24, $15, %lo(.got.plt entry)
336 writeValue<E>(Buf, GotPltEntryAddr + 0x8000, 16, 16);
337 writeValue<E>(Buf + 4, GotPltEntryAddr, 16, 0);
338 writeValue<E>(Buf + 12, GotPltEntryAddr, 16, 0);
341 template <class ELFT>
342 bool MIPS<ELFT>::needsThunk(RelExpr Expr, RelType Type, const InputFile *File,
343 uint64_t BranchAddr, const Symbol &S) const {
344 // Any MIPS PIC code function is invoked with its address in register $t9.
345 // So if we have a branch instruction from non-PIC code to the PIC one
346 // we cannot make the jump directly and need to create a small stubs
347 // to save the target function address.
348 // See page 3-38 ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
349 if (Type != R_MIPS_26 && Type != R_MICROMIPS_26_S1 &&
350 Type != R_MICROMIPS_PC26_S1)
352 auto *F = dyn_cast_or_null<ELFFileBase<ELFT>>(File);
355 // If current file has PIC code, LA25 stub is not required.
356 if (F->getObj().getHeader()->e_flags & EF_MIPS_PIC)
358 auto *D = dyn_cast<Defined>(&S);
359 // LA25 is required if target file has PIC code
360 // or target symbol is a PIC symbol.
361 return D && isMipsPIC<ELFT>(D);
364 template <class ELFT>
365 int64_t MIPS<ELFT>::getImplicitAddend(const uint8_t *Buf, RelType Type) const {
366 const endianness E = ELFT::TargetEndianness;
370 case R_MIPS_TLS_DTPREL32:
371 case R_MIPS_TLS_TPREL32:
372 return SignExtend64<32>(read32<E>(Buf));
374 // FIXME (simon): If the relocation target symbol is not a PLT entry
375 // we should use another expression for calculation:
376 // ((A << 2) | (P & 0xf0000000)) >> 2
377 return SignExtend64<28>(read32<E>(Buf) << 2);
381 return SignExtend64<16>(read32<E>(Buf)) << 16;
385 case R_MIPS_TLS_DTPREL_HI16:
386 case R_MIPS_TLS_DTPREL_LO16:
387 case R_MIPS_TLS_TPREL_HI16:
388 case R_MIPS_TLS_TPREL_LO16:
389 return SignExtend64<16>(read32<E>(Buf));
390 case R_MICROMIPS_GOT16:
391 case R_MICROMIPS_HI16:
392 return SignExtend64<16>(readShuffle<E>(Buf)) << 16;
393 case R_MICROMIPS_GPREL16:
394 case R_MICROMIPS_LO16:
395 case R_MICROMIPS_TLS_DTPREL_HI16:
396 case R_MICROMIPS_TLS_DTPREL_LO16:
397 case R_MICROMIPS_TLS_TPREL_HI16:
398 case R_MICROMIPS_TLS_TPREL_LO16:
399 return SignExtend64<16>(readShuffle<E>(Buf));
400 case R_MICROMIPS_GPREL7_S2:
401 return SignExtend64<9>(readShuffle<E>(Buf) << 2);
403 return SignExtend64<18>(read32<E>(Buf) << 2);
405 return SignExtend64<21>(read32<E>(Buf) << 2);
407 return SignExtend64<23>(read32<E>(Buf) << 2);
409 return SignExtend64<28>(read32<E>(Buf) << 2);
411 return SignExtend64<32>(read32<E>(Buf));
412 case R_MICROMIPS_26_S1:
413 return SignExtend64<27>(readShuffle<E>(Buf) << 1);
414 case R_MICROMIPS_PC7_S1:
415 return SignExtend64<8>(read16<E>(Buf) << 1);
416 case R_MICROMIPS_PC10_S1:
417 return SignExtend64<11>(read16<E>(Buf) << 1);
418 case R_MICROMIPS_PC16_S1:
419 return SignExtend64<17>(readShuffle<E>(Buf) << 1);
420 case R_MICROMIPS_PC18_S3:
421 return SignExtend64<21>(readShuffle<E>(Buf) << 3);
422 case R_MICROMIPS_PC19_S2:
423 return SignExtend64<21>(readShuffle<E>(Buf) << 2);
424 case R_MICROMIPS_PC21_S1:
425 return SignExtend64<22>(readShuffle<E>(Buf) << 1);
426 case R_MICROMIPS_PC23_S2:
427 return SignExtend64<25>(readShuffle<E>(Buf) << 2);
428 case R_MICROMIPS_PC26_S1:
429 return SignExtend64<27>(readShuffle<E>(Buf) << 1);
435 static std::pair<uint32_t, uint64_t>
436 calculateMipsRelChain(uint8_t *Loc, RelType Type, uint64_t Val) {
437 // MIPS N64 ABI packs multiple relocations into the single relocation
438 // record. In general, all up to three relocations can have arbitrary
439 // types. In fact, Clang and GCC uses only a few combinations. For now,
440 // we support two of them. That is allow to pass at least all LLVM
442 // <any relocation> / R_MIPS_SUB / R_MIPS_HI16 | R_MIPS_LO16
443 // <any relocation> / R_MIPS_64 / R_MIPS_NONE
444 // The first relocation is a 'real' relocation which is calculated
445 // using the corresponding symbol's value. The second and the third
446 // relocations used to modify result of the first one: extend it to
447 // 64-bit, extract high or low part etc. For details, see part 2.9 Relocation
448 // at the https://dmz-portal.mips.com/mw/images/8/82/007-4658-001.pdf
449 RelType Type2 = (Type >> 8) & 0xff;
450 RelType Type3 = (Type >> 16) & 0xff;
451 if (Type2 == R_MIPS_NONE && Type3 == R_MIPS_NONE)
452 return std::make_pair(Type, Val);
453 if (Type2 == R_MIPS_64 && Type3 == R_MIPS_NONE)
454 return std::make_pair(Type2, Val);
455 if (Type2 == R_MIPS_SUB && (Type3 == R_MIPS_HI16 || Type3 == R_MIPS_LO16))
456 return std::make_pair(Type3, -Val);
457 error(getErrorLocation(Loc) + "unsupported relocations combination " +
459 return std::make_pair(Type & 0xff, Val);
462 template <class ELFT>
463 void MIPS<ELFT>::relocateOne(uint8_t *Loc, RelType Type, uint64_t Val) const {
464 const endianness E = ELFT::TargetEndianness;
466 if (ELFT::Is64Bits || Config->MipsN32Abi)
467 std::tie(Type, Val) = calculateMipsRelChain(Loc, Type, Val);
469 // Thread pointer and DRP offsets from the start of TLS data area.
470 // https://www.linux-mips.org/wiki/NPTL
471 if (Type == R_MIPS_TLS_DTPREL_HI16 || Type == R_MIPS_TLS_DTPREL_LO16 ||
472 Type == R_MIPS_TLS_DTPREL32 || Type == R_MIPS_TLS_DTPREL64 ||
473 Type == R_MICROMIPS_TLS_DTPREL_HI16 ||
474 Type == R_MICROMIPS_TLS_DTPREL_LO16) {
476 } else if (Type == R_MIPS_TLS_TPREL_HI16 || Type == R_MIPS_TLS_TPREL_LO16 ||
477 Type == R_MIPS_TLS_TPREL32 || Type == R_MIPS_TLS_TPREL64 ||
478 Type == R_MICROMIPS_TLS_TPREL_HI16 ||
479 Type == R_MICROMIPS_TLS_TPREL_LO16) {
486 case R_MIPS_TLS_DTPREL32:
487 case R_MIPS_TLS_TPREL32:
488 write32<E>(Loc, Val);
491 case R_MIPS_TLS_DTPREL64:
492 case R_MIPS_TLS_TPREL64:
493 write64<E>(Loc, Val);
496 writeValue<E>(Loc, Val, 26, 2);
499 // The R_MIPS_GOT16 relocation's value in "relocatable" linking mode
500 // is updated addend (not a GOT index). In that case write high 16 bits
501 // to store a correct addend value.
502 if (Config->Relocatable) {
503 writeValue<E>(Loc, Val + 0x8000, 16, 16);
505 checkInt(Loc, Val, 16, Type);
506 writeValue<E>(Loc, Val, 16, 0);
509 case R_MICROMIPS_GOT16:
510 if (Config->Relocatable) {
511 writeShuffleValue<E>(Loc, Val + 0x8000, 16, 16);
513 checkInt(Loc, Val, 16, Type);
514 writeShuffleValue<E>(Loc, Val, 16, 0);
518 case R_MIPS_GOT_DISP:
519 case R_MIPS_GOT_PAGE:
522 case R_MIPS_TLS_GOTTPREL:
524 checkInt(Loc, Val, 16, Type);
526 case R_MIPS_CALL_LO16:
527 case R_MIPS_GOT_LO16:
528 case R_MIPS_GOT_OFST:
531 case R_MIPS_TLS_DTPREL_LO16:
532 case R_MIPS_TLS_TPREL_LO16:
533 writeValue<E>(Loc, Val, 16, 0);
535 case R_MICROMIPS_GPREL16:
536 case R_MICROMIPS_TLS_GD:
537 case R_MICROMIPS_TLS_LDM:
538 checkInt(Loc, Val, 16, Type);
539 writeShuffleValue<E>(Loc, Val, 16, 0);
541 case R_MICROMIPS_CALL16:
542 case R_MICROMIPS_CALL_LO16:
543 case R_MICROMIPS_LO16:
544 case R_MICROMIPS_TLS_DTPREL_LO16:
545 case R_MICROMIPS_TLS_GOTTPREL:
546 case R_MICROMIPS_TLS_TPREL_LO16:
547 writeShuffleValue<E>(Loc, Val, 16, 0);
549 case R_MICROMIPS_GPREL7_S2:
550 checkInt(Loc, Val, 7, Type);
551 writeShuffleValue<E>(Loc, Val, 7, 2);
553 case R_MIPS_CALL_HI16:
554 case R_MIPS_GOT_HI16:
557 case R_MIPS_TLS_DTPREL_HI16:
558 case R_MIPS_TLS_TPREL_HI16:
559 writeValue<E>(Loc, Val + 0x8000, 16, 16);
561 case R_MICROMIPS_CALL_HI16:
562 case R_MICROMIPS_GOT_HI16:
563 case R_MICROMIPS_HI16:
564 case R_MICROMIPS_TLS_DTPREL_HI16:
565 case R_MICROMIPS_TLS_TPREL_HI16:
566 writeShuffleValue<E>(Loc, Val + 0x8000, 16, 16);
569 writeValue<E>(Loc, Val + 0x80008000, 16, 32);
572 writeValue<E>(Loc, Val + 0x800080008000, 16, 48);
575 case R_MICROMIPS_JALR:
576 // Ignore this optimization relocation for now
579 checkAlignment(Loc, Val, 4, Type);
580 checkInt(Loc, Val, 18, Type);
581 writeValue<E>(Loc, Val, 16, 2);
584 checkAlignment(Loc, Val, 4, Type);
585 checkInt(Loc, Val, 21, Type);
586 writeValue<E>(Loc, Val, 19, 2);
589 checkAlignment(Loc, Val, 4, Type);
590 checkInt(Loc, Val, 23, Type);
591 writeValue<E>(Loc, Val, 21, 2);
594 checkAlignment(Loc, Val, 4, Type);
595 checkInt(Loc, Val, 28, Type);
596 writeValue<E>(Loc, Val, 26, 2);
599 writeValue<E>(Loc, Val, 32, 0);
601 case R_MICROMIPS_26_S1:
602 case R_MICROMIPS_PC26_S1:
603 checkInt(Loc, Val, 27, Type);
604 writeShuffleValue<E>(Loc, Val, 26, 1);
606 case R_MICROMIPS_PC7_S1:
607 checkInt(Loc, Val, 8, Type);
608 writeMicroRelocation16<E>(Loc, Val, 7, 1);
610 case R_MICROMIPS_PC10_S1:
611 checkInt(Loc, Val, 11, Type);
612 writeMicroRelocation16<E>(Loc, Val, 10, 1);
614 case R_MICROMIPS_PC16_S1:
615 checkInt(Loc, Val, 17, Type);
616 writeShuffleValue<E>(Loc, Val, 16, 1);
618 case R_MICROMIPS_PC18_S3:
619 checkInt(Loc, Val, 21, Type);
620 writeShuffleValue<E>(Loc, Val, 18, 3);
622 case R_MICROMIPS_PC19_S2:
623 checkInt(Loc, Val, 21, Type);
624 writeShuffleValue<E>(Loc, Val, 19, 2);
626 case R_MICROMIPS_PC21_S1:
627 checkInt(Loc, Val, 22, Type);
628 writeShuffleValue<E>(Loc, Val, 21, 1);
630 case R_MICROMIPS_PC23_S2:
631 checkInt(Loc, Val, 25, Type);
632 writeShuffleValue<E>(Loc, Val, 23, 2);
635 error(getErrorLocation(Loc) + "unrecognized reloc " + Twine(Type));
639 template <class ELFT> bool MIPS<ELFT>::usesOnlyLowPageBits(RelType Type) const {
640 return Type == R_MIPS_LO16 || Type == R_MIPS_GOT_OFST ||
641 Type == R_MICROMIPS_LO16;
644 // Return true if the symbol is a PIC function.
645 template <class ELFT> bool elf::isMipsPIC(const Defined *Sym) {
649 if (Sym->StOther & STO_MIPS_PIC)
655 ObjFile<ELFT> *File =
656 cast<InputSectionBase>(Sym->Section)->template getFile<ELFT>();
660 return File->getObj().getHeader()->e_flags & EF_MIPS_PIC;
663 template <class ELFT> TargetInfo *elf::getMipsTargetInfo() {
664 static MIPS<ELFT> Target;
668 template TargetInfo *elf::getMipsTargetInfo<ELF32LE>();
669 template TargetInfo *elf::getMipsTargetInfo<ELF32BE>();
670 template TargetInfo *elf::getMipsTargetInfo<ELF64LE>();
671 template TargetInfo *elf::getMipsTargetInfo<ELF64BE>();
673 template bool elf::isMipsPIC<ELF32LE>(const Defined *);
674 template bool elf::isMipsPIC<ELF32BE>(const Defined *);
675 template bool elf::isMipsPIC<ELF64LE>(const Defined *);
676 template bool elf::isMipsPIC<ELF64BE>(const Defined *);