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