xref: /llvm-project-15.0.7/lld/ELF/Arch/Mips.cpp (revision de5ed0c5)
1 //===- MIPS.cpp -----------------------------------------------------------===//
2 //
3 //                             The LLVM Linker
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "InputFiles.h"
11 #include "OutputSections.h"
12 #include "Symbols.h"
13 #include "SyntheticSections.h"
14 #include "Target.h"
15 #include "Thunks.h"
16 #include "lld/Common/ErrorHandler.h"
17 #include "llvm/Object/ELF.h"
18 #include "llvm/Support/Endian.h"
19 
20 using namespace llvm;
21 using namespace llvm::object;
22 using namespace llvm::support::endian;
23 using namespace llvm::ELF;
24 using namespace lld;
25 using namespace lld::elf;
26 
27 namespace {
28 template <class ELFT> class MIPS final : public TargetInfo {
29 public:
30   MIPS();
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   bool isPicRel(RelType Type) const override;
36   RelType getDynRel(RelType Type) const override;
37   void writeGotPlt(uint8_t *Buf, const Symbol &S) const override;
38   void writePltHeader(uint8_t *Buf) const override;
39   void writePlt(uint8_t *Buf, uint64_t GotPltEntryAddr, uint64_t PltEntryAddr,
40                 int32_t Index, unsigned RelOff) const override;
41   bool needsThunk(RelExpr Expr, RelType Type, const InputFile *File,
42                   uint64_t BranchAddr, const Symbol &S) const override;
43   void relocateOne(uint8_t *Loc, RelType Type, uint64_t Val) const override;
44   bool usesOnlyLowPageBits(RelType Type) const override;
45 };
46 } // namespace
47 
48 template <class ELFT> MIPS<ELFT>::MIPS() {
49   GotPltHeaderEntriesNum = 2;
50   DefaultMaxPageSize = 65536;
51   GotEntrySize = sizeof(typename ELFT::uint);
52   GotPltEntrySize = sizeof(typename ELFT::uint);
53   PltEntrySize = 16;
54   PltHeaderSize = 32;
55   CopyRel = R_MIPS_COPY;
56   PltRel = R_MIPS_JUMP_SLOT;
57   NeedsThunks = true;
58   TrapInstr = 0xefefefef;
59 
60   if (ELFT::Is64Bits) {
61     RelativeRel = (R_MIPS_64 << 8) | R_MIPS_REL32;
62     TlsGotRel = R_MIPS_TLS_TPREL64;
63     TlsModuleIndexRel = R_MIPS_TLS_DTPMOD64;
64     TlsOffsetRel = R_MIPS_TLS_DTPREL64;
65   } else {
66     RelativeRel = R_MIPS_REL32;
67     TlsGotRel = R_MIPS_TLS_TPREL32;
68     TlsModuleIndexRel = R_MIPS_TLS_DTPMOD32;
69     TlsOffsetRel = R_MIPS_TLS_DTPREL32;
70   }
71 }
72 
73 template <class ELFT> uint32_t MIPS<ELFT>::calcEFlags() const {
74   return calcMipsEFlags<ELFT>();
75 }
76 
77 template <class ELFT>
78 RelExpr MIPS<ELFT>::getRelExpr(RelType Type, const Symbol &S,
79                                const uint8_t *Loc) const {
80   // See comment in the calculateMipsRelChain.
81   if (ELFT::Is64Bits || Config->MipsN32Abi)
82     Type &= 0xff;
83 
84   switch (Type) {
85   case R_MIPS_JALR:
86   case R_MICROMIPS_JALR:
87     return R_HINT;
88   case R_MIPS_GPREL16:
89   case R_MIPS_GPREL32:
90   case R_MICROMIPS_GPREL16:
91   case R_MICROMIPS_GPREL7_S2:
92     return R_MIPS_GOTREL;
93   case R_MIPS_26:
94   case R_MICROMIPS_26_S1:
95     return R_PLT;
96   case R_MICROMIPS_PC26_S1:
97     return R_PLT_PC;
98   case R_MIPS_HI16:
99   case R_MIPS_LO16:
100   case R_MIPS_HIGHER:
101   case R_MIPS_HIGHEST:
102   case R_MICROMIPS_HI16:
103   case R_MICROMIPS_LO16:
104   case R_MICROMIPS_HIGHER:
105   case R_MICROMIPS_HIGHEST:
106     // R_MIPS_HI16/R_MIPS_LO16 relocations against _gp_disp calculate
107     // offset between start of function and 'gp' value which by default
108     // equal to the start of .got section. In that case we consider these
109     // relocations as relative.
110     if (&S == ElfSym::MipsGpDisp)
111       return R_MIPS_GOT_GP_PC;
112     if (&S == ElfSym::MipsLocalGp)
113       return R_MIPS_GOT_GP;
114     LLVM_FALLTHROUGH;
115   case R_MIPS_32:
116   case R_MIPS_64:
117   case R_MIPS_GOT_OFST:
118   case R_MIPS_SUB:
119   case R_MIPS_TLS_DTPREL_HI16:
120   case R_MIPS_TLS_DTPREL_LO16:
121   case R_MIPS_TLS_DTPREL32:
122   case R_MIPS_TLS_DTPREL64:
123   case R_MIPS_TLS_TPREL_HI16:
124   case R_MIPS_TLS_TPREL_LO16:
125   case R_MIPS_TLS_TPREL32:
126   case R_MIPS_TLS_TPREL64:
127   case R_MICROMIPS_GOT_OFST:
128   case R_MICROMIPS_SUB:
129   case R_MICROMIPS_TLS_DTPREL_HI16:
130   case R_MICROMIPS_TLS_DTPREL_LO16:
131   case R_MICROMIPS_TLS_TPREL_HI16:
132   case R_MICROMIPS_TLS_TPREL_LO16:
133     return R_ABS;
134   case R_MIPS_PC32:
135   case R_MIPS_PC16:
136   case R_MIPS_PC19_S2:
137   case R_MIPS_PC21_S2:
138   case R_MIPS_PC26_S2:
139   case R_MIPS_PCHI16:
140   case R_MIPS_PCLO16:
141   case R_MICROMIPS_PC7_S1:
142   case R_MICROMIPS_PC10_S1:
143   case R_MICROMIPS_PC16_S1:
144   case R_MICROMIPS_PC18_S3:
145   case R_MICROMIPS_PC19_S2:
146   case R_MICROMIPS_PC23_S2:
147   case R_MICROMIPS_PC21_S1:
148     return R_PC;
149   case R_MIPS_GOT16:
150   case R_MICROMIPS_GOT16:
151     if (S.isLocal())
152       return R_MIPS_GOT_LOCAL_PAGE;
153     LLVM_FALLTHROUGH;
154   case R_MIPS_CALL16:
155   case R_MIPS_GOT_DISP:
156   case R_MIPS_TLS_GOTTPREL:
157   case R_MICROMIPS_CALL16:
158   case R_MICROMIPS_GOT_DISP:
159   case R_MICROMIPS_TLS_GOTTPREL:
160     return R_MIPS_GOT_OFF;
161   case R_MIPS_CALL_HI16:
162   case R_MIPS_CALL_LO16:
163   case R_MIPS_GOT_HI16:
164   case R_MIPS_GOT_LO16:
165   case R_MICROMIPS_CALL_HI16:
166   case R_MICROMIPS_CALL_LO16:
167   case R_MICROMIPS_GOT_HI16:
168   case R_MICROMIPS_GOT_LO16:
169     return R_MIPS_GOT_OFF32;
170   case R_MIPS_GOT_PAGE:
171   case R_MICROMIPS_GOT_PAGE:
172     return R_MIPS_GOT_LOCAL_PAGE;
173   case R_MIPS_TLS_GD:
174   case R_MICROMIPS_TLS_GD:
175     return R_MIPS_TLSGD;
176   case R_MIPS_TLS_LDM:
177   case R_MICROMIPS_TLS_LDM:
178     return R_MIPS_TLSLD;
179   case R_MIPS_NONE:
180     return R_NONE;
181   default:
182     return R_INVALID;
183   }
184 }
185 
186 template <class ELFT> bool MIPS<ELFT>::isPicRel(RelType Type) const {
187   return Type == R_MIPS_32 || Type == R_MIPS_64;
188 }
189 
190 template <class ELFT> RelType MIPS<ELFT>::getDynRel(RelType Type) const {
191   return RelativeRel;
192 }
193 
194 template <class ELFT>
195 void MIPS<ELFT>::writeGotPlt(uint8_t *Buf, const Symbol &) const {
196   uint64_t VA = InX::Plt->getVA();
197   if (isMicroMips())
198     VA |= 1;
199   write32<ELFT::TargetEndianness>(Buf, VA);
200 }
201 
202 template <endianness E> static uint32_t readShuffle(const uint8_t *Loc) {
203   // The major opcode of a microMIPS instruction needs to appear
204   // in the first 16-bit word (lowest address) for efficient hardware
205   // decode so that it knows if the instruction is 16-bit or 32-bit
206   // as early as possible. To do so, little-endian binaries keep 16-bit
207   // words in a big-endian order. That is why we have to swap these
208   // words to get a correct value.
209   uint32_t V = read32<E>(Loc);
210   if (E == support::little)
211     return (V << 16) | (V >> 16);
212   return V;
213 }
214 
215 template <endianness E>
216 static void writeRelocation(uint8_t *Loc, uint64_t V, uint8_t BitsSize,
217                             uint8_t Shift) {
218   uint32_t Instr = read32<E>(Loc);
219   uint32_t Mask = 0xffffffff >> (32 - BitsSize);
220   uint32_t Data = (Instr & ~Mask) | ((V >> Shift) & Mask);
221   write32<E>(Loc, Data);
222 }
223 
224 template <endianness E>
225 static void writeMicroRelocation32(uint8_t *Loc, uint64_t V, uint8_t BitsSize,
226                                    uint8_t Shift) {
227   // See comments in readShuffle for purpose of this code.
228   uint16_t *Words = (uint16_t *)Loc;
229   if (E == support::little)
230     std::swap(Words[0], Words[1]);
231 
232   writeRelocation<E>(Loc, V, BitsSize, Shift);
233 
234   if (E == support::little)
235     std::swap(Words[0], Words[1]);
236 }
237 
238 template <endianness E>
239 static void writeMicroRelocation16(uint8_t *Loc, uint64_t V, uint8_t BitsSize,
240                                    uint8_t Shift) {
241   uint16_t Instr = read16<E>(Loc);
242   uint16_t Mask = 0xffff >> (16 - BitsSize);
243   uint16_t Data = (Instr & ~Mask) | ((V >> Shift) & Mask);
244   write16<E>(Loc, Data);
245 }
246 
247 template <class ELFT> void MIPS<ELFT>::writePltHeader(uint8_t *Buf) const {
248   const endianness E = ELFT::TargetEndianness;
249   if (isMicroMips()) {
250     uint64_t GotPlt = InX::GotPlt->getVA();
251     uint64_t Plt = InX::Plt->getVA();
252     // Overwrite trap instructions written by Writer::writeTrapInstr.
253     memset(Buf, 0, PltHeaderSize);
254 
255     write16<E>(Buf, isMipsR6() ? 0x7860 : 0x7980);  // addiupc v1, (GOTPLT) - .
256     write16<E>(Buf + 4, 0xff23);    // lw      $25, 0($3)
257     write16<E>(Buf + 8, 0x0535);    // subu16  $2,  $2, $3
258     write16<E>(Buf + 10, 0x2525);   // srl16   $2,  $2, 2
259     write16<E>(Buf + 12, 0x3302);   // addiu   $24, $2, -2
260     write16<E>(Buf + 14, 0xfffe);
261     write16<E>(Buf + 16, 0x0dff);   // move    $15, $31
262     if (isMipsR6()) {
263       write16<E>(Buf + 18, 0x0f83); // move    $28, $3
264       write16<E>(Buf + 20, 0x472b); // jalrc   $25
265       write16<E>(Buf + 22, 0x0c00); // nop
266       relocateOne(Buf, R_MICROMIPS_PC19_S2, GotPlt - Plt);
267     } else {
268       write16<E>(Buf + 18, 0x45f9); // jalrc   $25
269       write16<E>(Buf + 20, 0x0f83); // move    $28, $3
270       write16<E>(Buf + 22, 0x0c00); // nop
271       relocateOne(Buf, R_MICROMIPS_PC23_S2, GotPlt - Plt);
272     }
273     return;
274   }
275 
276   if (Config->MipsN32Abi) {
277     write32<E>(Buf, 0x3c0e0000);      // lui   $14, %hi(&GOTPLT[0])
278     write32<E>(Buf + 4, 0x8dd90000);  // lw    $25, %lo(&GOTPLT[0])($14)
279     write32<E>(Buf + 8, 0x25ce0000);  // addiu $14, $14, %lo(&GOTPLT[0])
280     write32<E>(Buf + 12, 0x030ec023); // subu  $24, $24, $14
281   } else {
282     write32<E>(Buf, 0x3c1c0000);      // lui   $28, %hi(&GOTPLT[0])
283     write32<E>(Buf + 4, 0x8f990000);  // lw    $25, %lo(&GOTPLT[0])($28)
284     write32<E>(Buf + 8, 0x279c0000);  // addiu $28, $28, %lo(&GOTPLT[0])
285     write32<E>(Buf + 12, 0x031cc023); // subu  $24, $24, $28
286   }
287 
288   write32<E>(Buf + 16, 0x03e07825); // move  $15, $31
289   write32<E>(Buf + 20, 0x0018c082); // srl   $24, $24, 2
290   write32<E>(Buf + 24, 0x0320f809); // jalr  $25
291   write32<E>(Buf + 28, 0x2718fffe); // subu  $24, $24, 2
292 
293   uint64_t GotPlt = InX::GotPlt->getVA();
294   writeRelocation<E>(Buf, GotPlt + 0x8000, 16, 16);
295   writeRelocation<E>(Buf + 4, GotPlt, 16, 0);
296   writeRelocation<E>(Buf + 8, GotPlt, 16, 0);
297 }
298 
299 template <class ELFT>
300 void MIPS<ELFT>::writePlt(uint8_t *Buf, uint64_t GotPltEntryAddr,
301                           uint64_t PltEntryAddr, int32_t Index,
302                           unsigned RelOff) const {
303   const endianness E = ELFT::TargetEndianness;
304   if (isMicroMips()) {
305     // Overwrite trap instructions written by Writer::writeTrapInstr.
306     memset(Buf, 0, PltEntrySize);
307 
308     if (isMipsR6()) {
309       write16<E>(Buf, 0x7840);      // addiupc $2, (GOTPLT) - .
310       write16<E>(Buf + 4, 0xff22);  // lw $25, 0($2)
311       write16<E>(Buf + 8, 0x0f02);  // move $24, $2
312       write16<E>(Buf + 10, 0x4723); // jrc $25 / jr16 $25
313       relocateOne(Buf, R_MICROMIPS_PC19_S2, GotPltEntryAddr - PltEntryAddr);
314     } else {
315       write16<E>(Buf, 0x7900);      // addiupc $2, (GOTPLT) - .
316       write16<E>(Buf + 4, 0xff22);  // lw $25, 0($2)
317       write16<E>(Buf + 8, 0x4599);  // jrc $25 / jr16 $25
318       write16<E>(Buf + 10, 0x0f02); // move $24, $2
319       relocateOne(Buf, R_MICROMIPS_PC23_S2, GotPltEntryAddr - PltEntryAddr);
320     }
321     return;
322   }
323 
324   write32<E>(Buf, 0x3c0f0000);     // lui   $15, %hi(.got.plt entry)
325   write32<E>(Buf + 4, 0x8df90000); // l[wd] $25, %lo(.got.plt entry)($15)
326   write32<E>(Buf + 8, isMipsR6() ? 0x03200009 : 0x03200008);  // jr  $25
327   write32<E>(Buf + 12, 0x25f80000); // addiu $24, $15, %lo(.got.plt entry)
328   writeRelocation<E>(Buf, GotPltEntryAddr + 0x8000, 16, 16);
329   writeRelocation<E>(Buf + 4, GotPltEntryAddr, 16, 0);
330   writeRelocation<E>(Buf + 12, GotPltEntryAddr, 16, 0);
331 }
332 
333 template <class ELFT>
334 bool MIPS<ELFT>::needsThunk(RelExpr Expr, RelType Type, const InputFile *File,
335                             uint64_t BranchAddr, const Symbol &S) const {
336   // Any MIPS PIC code function is invoked with its address in register $t9.
337   // So if we have a branch instruction from non-PIC code to the PIC one
338   // we cannot make the jump directly and need to create a small stubs
339   // to save the target function address.
340   // See page 3-38 ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
341   if (Type != R_MIPS_26 && Type != R_MICROMIPS_26_S1 &&
342       Type != R_MICROMIPS_PC26_S1)
343     return false;
344   auto *F = dyn_cast_or_null<ELFFileBase<ELFT>>(File);
345   if (!F)
346     return false;
347   // If current file has PIC code, LA25 stub is not required.
348   if (F->getObj().getHeader()->e_flags & EF_MIPS_PIC)
349     return false;
350   auto *D = dyn_cast<Defined>(&S);
351   // LA25 is required if target file has PIC code
352   // or target symbol is a PIC symbol.
353   return D && isMipsPIC<ELFT>(D);
354 }
355 
356 template <class ELFT>
357 int64_t MIPS<ELFT>::getImplicitAddend(const uint8_t *Buf, RelType Type) const {
358   const endianness E = ELFT::TargetEndianness;
359   switch (Type) {
360   case R_MIPS_32:
361   case R_MIPS_GPREL32:
362   case R_MIPS_TLS_DTPREL32:
363   case R_MIPS_TLS_TPREL32:
364     return SignExtend64<32>(read32<E>(Buf));
365   case R_MIPS_26:
366     // FIXME (simon): If the relocation target symbol is not a PLT entry
367     // we should use another expression for calculation:
368     // ((A << 2) | (P & 0xf0000000)) >> 2
369     return SignExtend64<28>(read32<E>(Buf) << 2);
370   case R_MIPS_GOT16:
371   case R_MIPS_HI16:
372   case R_MIPS_PCHI16:
373     return SignExtend64<16>(read32<E>(Buf)) << 16;
374   case R_MIPS_GPREL16:
375   case R_MIPS_LO16:
376   case R_MIPS_PCLO16:
377   case R_MIPS_TLS_DTPREL_HI16:
378   case R_MIPS_TLS_DTPREL_LO16:
379   case R_MIPS_TLS_TPREL_HI16:
380   case R_MIPS_TLS_TPREL_LO16:
381     return SignExtend64<16>(read32<E>(Buf));
382   case R_MICROMIPS_GOT16:
383   case R_MICROMIPS_HI16:
384     return SignExtend64<16>(readShuffle<E>(Buf)) << 16;
385   case R_MICROMIPS_GPREL16:
386   case R_MICROMIPS_LO16:
387   case R_MICROMIPS_TLS_DTPREL_HI16:
388   case R_MICROMIPS_TLS_DTPREL_LO16:
389   case R_MICROMIPS_TLS_TPREL_HI16:
390   case R_MICROMIPS_TLS_TPREL_LO16:
391     return SignExtend64<16>(readShuffle<E>(Buf));
392   case R_MICROMIPS_GPREL7_S2:
393     return SignExtend64<9>(readShuffle<E>(Buf) << 2);
394   case R_MIPS_PC16:
395     return SignExtend64<18>(read32<E>(Buf) << 2);
396   case R_MIPS_PC19_S2:
397     return SignExtend64<21>(read32<E>(Buf) << 2);
398   case R_MIPS_PC21_S2:
399     return SignExtend64<23>(read32<E>(Buf) << 2);
400   case R_MIPS_PC26_S2:
401     return SignExtend64<28>(read32<E>(Buf) << 2);
402   case R_MIPS_PC32:
403     return SignExtend64<32>(read32<E>(Buf));
404   case R_MICROMIPS_26_S1:
405     return SignExtend64<27>(readShuffle<E>(Buf) << 1);
406   case R_MICROMIPS_PC7_S1:
407     return SignExtend64<8>(read16<E>(Buf) << 1);
408   case R_MICROMIPS_PC10_S1:
409     return SignExtend64<11>(read16<E>(Buf) << 1);
410   case R_MICROMIPS_PC16_S1:
411     return SignExtend64<17>(readShuffle<E>(Buf) << 1);
412   case R_MICROMIPS_PC18_S3:
413     return SignExtend64<21>(readShuffle<E>(Buf) << 3);
414   case R_MICROMIPS_PC19_S2:
415     return SignExtend64<21>(readShuffle<E>(Buf) << 2);
416   case R_MICROMIPS_PC21_S1:
417     return SignExtend64<22>(readShuffle<E>(Buf) << 1);
418   case R_MICROMIPS_PC23_S2:
419     return SignExtend64<25>(readShuffle<E>(Buf) << 2);
420   case R_MICROMIPS_PC26_S1:
421     return SignExtend64<27>(readShuffle<E>(Buf) << 1);
422   default:
423     return 0;
424   }
425 }
426 
427 static std::pair<uint32_t, uint64_t>
428 calculateMipsRelChain(uint8_t *Loc, RelType Type, uint64_t Val) {
429   // MIPS N64 ABI packs multiple relocations into the single relocation
430   // record. In general, all up to three relocations can have arbitrary
431   // types. In fact, Clang and GCC uses only a few combinations. For now,
432   // we support two of them. That is allow to pass at least all LLVM
433   // test suite cases.
434   // <any relocation> / R_MIPS_SUB / R_MIPS_HI16 | R_MIPS_LO16
435   // <any relocation> / R_MIPS_64 / R_MIPS_NONE
436   // The first relocation is a 'real' relocation which is calculated
437   // using the corresponding symbol's value. The second and the third
438   // relocations used to modify result of the first one: extend it to
439   // 64-bit, extract high or low part etc. For details, see part 2.9 Relocation
440   // at the https://dmz-portal.mips.com/mw/images/8/82/007-4658-001.pdf
441   RelType Type2 = (Type >> 8) & 0xff;
442   RelType Type3 = (Type >> 16) & 0xff;
443   if (Type2 == R_MIPS_NONE && Type3 == R_MIPS_NONE)
444     return std::make_pair(Type, Val);
445   if (Type2 == R_MIPS_64 && Type3 == R_MIPS_NONE)
446     return std::make_pair(Type2, Val);
447   if (Type2 == R_MIPS_SUB && (Type3 == R_MIPS_HI16 || Type3 == R_MIPS_LO16))
448     return std::make_pair(Type3, -Val);
449   if (Type2 == R_MICROMIPS_SUB &&
450       (Type3 == R_MICROMIPS_HI16 || Type3 == R_MICROMIPS_LO16))
451     return std::make_pair(Type3, -Val);
452   error(getErrorLocation(Loc) + "unsupported relocations combination " +
453         Twine(Type));
454   return std::make_pair(Type & 0xff, Val);
455 }
456 
457 template <class ELFT>
458 void MIPS<ELFT>::relocateOne(uint8_t *Loc, RelType Type, uint64_t Val) const {
459   const endianness E = ELFT::TargetEndianness;
460 
461   // Thread pointer and DRP offsets from the start of TLS data area.
462   // https://www.linux-mips.org/wiki/NPTL
463   if (Type == R_MIPS_TLS_DTPREL_HI16 || Type == R_MIPS_TLS_DTPREL_LO16 ||
464       Type == R_MIPS_TLS_DTPREL32 || Type == R_MIPS_TLS_DTPREL64 ||
465       Type == R_MICROMIPS_TLS_DTPREL_HI16 ||
466       Type == R_MICROMIPS_TLS_DTPREL_LO16) {
467     Val -= 0x8000;
468   } else if (Type == R_MIPS_TLS_TPREL_HI16 || Type == R_MIPS_TLS_TPREL_LO16 ||
469              Type == R_MIPS_TLS_TPREL32 || Type == R_MIPS_TLS_TPREL64 ||
470              Type == R_MICROMIPS_TLS_TPREL_HI16 ||
471              Type == R_MICROMIPS_TLS_TPREL_LO16) {
472     Val -= 0x7000;
473   }
474 
475   if (ELFT::Is64Bits || Config->MipsN32Abi)
476     std::tie(Type, Val) = calculateMipsRelChain(Loc, Type, Val);
477 
478   switch (Type) {
479   case R_MIPS_32:
480   case R_MIPS_GPREL32:
481   case R_MIPS_TLS_DTPREL32:
482   case R_MIPS_TLS_TPREL32:
483     write32<E>(Loc, Val);
484     break;
485   case R_MIPS_64:
486   case R_MIPS_TLS_DTPREL64:
487   case R_MIPS_TLS_TPREL64:
488     write64<E>(Loc, Val);
489     break;
490   case R_MIPS_26:
491     writeRelocation<E>(Loc, Val, 26, 2);
492     break;
493   case R_MIPS_GOT16:
494     // The R_MIPS_GOT16 relocation's value in "relocatable" linking mode
495     // is updated addend (not a GOT index). In that case write high 16 bits
496     // to store a correct addend value.
497     if (Config->Relocatable) {
498       writeRelocation<E>(Loc, Val + 0x8000, 16, 16);
499     } else {
500       checkInt<16>(Loc, Val, Type);
501       writeRelocation<E>(Loc, Val, 16, 0);
502     }
503     break;
504   case R_MICROMIPS_GOT16:
505     if (Config->Relocatable) {
506       writeMicroRelocation32<E>(Loc, Val + 0x8000, 16, 16);
507     } else {
508       checkInt<16>(Loc, Val, Type);
509       writeMicroRelocation32<E>(Loc, Val, 16, 0);
510     }
511     break;
512   case R_MIPS_CALL16:
513   case R_MIPS_GOT_DISP:
514   case R_MIPS_GOT_PAGE:
515   case R_MIPS_GPREL16:
516   case R_MIPS_TLS_GD:
517   case R_MIPS_TLS_GOTTPREL:
518   case R_MIPS_TLS_LDM:
519     checkInt<16>(Loc, Val, Type);
520     LLVM_FALLTHROUGH;
521   case R_MIPS_CALL_LO16:
522   case R_MIPS_GOT_LO16:
523   case R_MIPS_GOT_OFST:
524   case R_MIPS_LO16:
525   case R_MIPS_PCLO16:
526   case R_MIPS_TLS_DTPREL_LO16:
527   case R_MIPS_TLS_TPREL_LO16:
528     writeRelocation<E>(Loc, Val, 16, 0);
529     break;
530   case R_MICROMIPS_GOT_DISP:
531   case R_MICROMIPS_GOT_PAGE:
532   case R_MICROMIPS_GPREL16:
533   case R_MICROMIPS_TLS_GD:
534   case R_MICROMIPS_TLS_LDM:
535     checkInt<16>(Loc, Val, Type);
536     writeMicroRelocation32<E>(Loc, Val, 16, 0);
537     break;
538   case R_MICROMIPS_CALL16:
539   case R_MICROMIPS_CALL_LO16:
540   case R_MICROMIPS_GOT_OFST:
541   case R_MICROMIPS_LO16:
542   case R_MICROMIPS_TLS_DTPREL_LO16:
543   case R_MICROMIPS_TLS_GOTTPREL:
544   case R_MICROMIPS_TLS_TPREL_LO16:
545     writeMicroRelocation32<E>(Loc, Val, 16, 0);
546     break;
547   case R_MICROMIPS_GPREL7_S2:
548     checkInt<7>(Loc, Val, Type);
549     writeMicroRelocation32<E>(Loc, Val, 7, 2);
550     break;
551   case R_MIPS_CALL_HI16:
552   case R_MIPS_GOT_HI16:
553   case R_MIPS_HI16:
554   case R_MIPS_PCHI16:
555   case R_MIPS_TLS_DTPREL_HI16:
556   case R_MIPS_TLS_TPREL_HI16:
557     writeRelocation<E>(Loc, Val + 0x8000, 16, 16);
558     break;
559   case R_MICROMIPS_CALL_HI16:
560   case R_MICROMIPS_GOT_HI16:
561   case R_MICROMIPS_HI16:
562   case R_MICROMIPS_TLS_DTPREL_HI16:
563   case R_MICROMIPS_TLS_TPREL_HI16:
564     writeMicroRelocation32<E>(Loc, Val + 0x8000, 16, 16);
565     break;
566   case R_MIPS_HIGHER:
567     writeRelocation<E>(Loc, Val + 0x80008000, 16, 32);
568     break;
569   case R_MIPS_HIGHEST:
570     writeRelocation<E>(Loc, Val + 0x800080008000, 16, 48);
571     break;
572   case R_MICROMIPS_HIGHER:
573     writeMicroRelocation32<E>(Loc, Val + 0x80008000, 16, 32);
574     break;
575   case R_MICROMIPS_HIGHEST:
576     writeMicroRelocation32<E>(Loc, Val + 0x800080008000, 16, 48);
577     break;
578   case R_MIPS_JALR:
579   case R_MICROMIPS_JALR:
580     // Ignore this optimization relocation for now
581     break;
582   case R_MIPS_PC16:
583     checkAlignment<4>(Loc, Val, Type);
584     checkInt<18>(Loc, Val, Type);
585     writeRelocation<E>(Loc, Val, 16, 2);
586     break;
587   case R_MIPS_PC19_S2:
588     checkAlignment<4>(Loc, Val, Type);
589     checkInt<21>(Loc, Val, Type);
590     writeRelocation<E>(Loc, Val, 19, 2);
591     break;
592   case R_MIPS_PC21_S2:
593     checkAlignment<4>(Loc, Val, Type);
594     checkInt<23>(Loc, Val, Type);
595     writeRelocation<E>(Loc, Val, 21, 2);
596     break;
597   case R_MIPS_PC26_S2:
598     checkAlignment<4>(Loc, Val, Type);
599     checkInt<28>(Loc, Val, Type);
600     writeRelocation<E>(Loc, Val, 26, 2);
601     break;
602   case R_MIPS_PC32:
603     writeRelocation<E>(Loc, Val, 32, 0);
604     break;
605   case R_MICROMIPS_26_S1:
606   case R_MICROMIPS_PC26_S1:
607     checkInt<27>(Loc, Val, Type);
608     writeMicroRelocation32<E>(Loc, Val, 26, 1);
609     break;
610   case R_MICROMIPS_PC7_S1:
611     checkInt<8>(Loc, Val, Type);
612     writeMicroRelocation16<E>(Loc, Val, 7, 1);
613     break;
614   case R_MICROMIPS_PC10_S1:
615     checkInt<11>(Loc, Val, Type);
616     writeMicroRelocation16<E>(Loc, Val, 10, 1);
617     break;
618   case R_MICROMIPS_PC16_S1:
619     checkInt<17>(Loc, Val, Type);
620     writeMicroRelocation32<E>(Loc, Val, 16, 1);
621     break;
622   case R_MICROMIPS_PC18_S3:
623     checkInt<21>(Loc, Val, Type);
624     writeMicroRelocation32<E>(Loc, Val, 18, 3);
625     break;
626   case R_MICROMIPS_PC19_S2:
627     checkInt<21>(Loc, Val, Type);
628     writeMicroRelocation32<E>(Loc, Val, 19, 2);
629     break;
630   case R_MICROMIPS_PC21_S1:
631     checkInt<22>(Loc, Val, Type);
632     writeMicroRelocation32<E>(Loc, Val, 21, 1);
633     break;
634   case R_MICROMIPS_PC23_S2:
635     checkInt<25>(Loc, Val, Type);
636     writeMicroRelocation32<E>(Loc, Val, 23, 2);
637     break;
638   default:
639     error(getErrorLocation(Loc) + "unrecognized reloc " + Twine(Type));
640   }
641 }
642 
643 template <class ELFT> bool MIPS<ELFT>::usesOnlyLowPageBits(RelType Type) const {
644   return Type == R_MIPS_LO16 || Type == R_MIPS_GOT_OFST ||
645          Type == R_MICROMIPS_LO16 || Type == R_MICROMIPS_GOT_OFST;
646 }
647 
648 // Return true if the symbol is a PIC function.
649 template <class ELFT> bool elf::isMipsPIC(const Defined *Sym) {
650   typedef typename ELFT::Ehdr Elf_Ehdr;
651   if (!Sym->Section || !Sym->isFunc())
652     return false;
653 
654   auto *Sec = cast<InputSectionBase>(Sym->Section);
655   const Elf_Ehdr *Hdr = Sec->template getFile<ELFT>()->getObj().getHeader();
656   return (Sym->StOther & STO_MIPS_MIPS16) == STO_MIPS_PIC ||
657          (Hdr->e_flags & EF_MIPS_PIC);
658 }
659 
660 template <class ELFT> TargetInfo *elf::getMipsTargetInfo() {
661   static MIPS<ELFT> Target;
662   return &Target;
663 }
664 
665 template TargetInfo *elf::getMipsTargetInfo<ELF32LE>();
666 template TargetInfo *elf::getMipsTargetInfo<ELF32BE>();
667 template TargetInfo *elf::getMipsTargetInfo<ELF64LE>();
668 template TargetInfo *elf::getMipsTargetInfo<ELF64BE>();
669 
670 template bool elf::isMipsPIC<ELF32LE>(const Defined *);
671 template bool elf::isMipsPIC<ELF32BE>(const Defined *);
672 template bool elf::isMipsPIC<ELF64LE>(const Defined *);
673 template bool elf::isMipsPIC<ELF64BE>(const Defined *);
674