xref: /openbsd-src/gnu/llvm/lld/ELF/Arch/Mips.cpp (revision dfe94b169149f14cc1aee2cf6dad58a8d9a1860c)
1ece8a530Spatrick //===- MIPS.cpp -----------------------------------------------------------===//
2ece8a530Spatrick //
3ece8a530Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4ece8a530Spatrick // See https://llvm.org/LICENSE.txt for license information.
5ece8a530Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6ece8a530Spatrick //
7ece8a530Spatrick //===----------------------------------------------------------------------===//
8ece8a530Spatrick 
9ece8a530Spatrick #include "InputFiles.h"
10ece8a530Spatrick #include "OutputSections.h"
11ece8a530Spatrick #include "Symbols.h"
12ece8a530Spatrick #include "SyntheticSections.h"
13ece8a530Spatrick #include "Target.h"
14ece8a530Spatrick #include "lld/Common/ErrorHandler.h"
15*dfe94b16Srobert #include "llvm/BinaryFormat/ELF.h"
16ece8a530Spatrick 
17ece8a530Spatrick using namespace llvm;
18ece8a530Spatrick using namespace llvm::object;
19ece8a530Spatrick using namespace llvm::ELF;
20bb684c34Spatrick using namespace lld;
21bb684c34Spatrick using namespace lld::elf;
22ece8a530Spatrick 
23ece8a530Spatrick namespace {
24ece8a530Spatrick template <class ELFT> class MIPS final : public TargetInfo {
25ece8a530Spatrick public:
26ece8a530Spatrick   MIPS();
27ece8a530Spatrick   uint32_t calcEFlags() const override;
28ece8a530Spatrick   RelExpr getRelExpr(RelType type, const Symbol &s,
29ece8a530Spatrick                      const uint8_t *loc) const override;
30ece8a530Spatrick   int64_t getImplicitAddend(const uint8_t *buf, RelType type) const override;
31ece8a530Spatrick   RelType getDynRel(RelType type) const override;
32ece8a530Spatrick   void writeGotPlt(uint8_t *buf, const Symbol &s) const override;
33ece8a530Spatrick   void writePltHeader(uint8_t *buf) const override;
34ece8a530Spatrick   void writePlt(uint8_t *buf, const Symbol &sym,
35ece8a530Spatrick                 uint64_t pltEntryAddr) const override;
36ece8a530Spatrick   bool needsThunk(RelExpr expr, RelType type, const InputFile *file,
37ece8a530Spatrick                   uint64_t branchAddr, const Symbol &s,
38ece8a530Spatrick                   int64_t a) const override;
39bb684c34Spatrick   void relocate(uint8_t *loc, const Relocation &rel,
40bb684c34Spatrick                 uint64_t val) const override;
41ece8a530Spatrick   bool usesOnlyLowPageBits(RelType type) const override;
42ece8a530Spatrick };
43ece8a530Spatrick } // namespace
44ece8a530Spatrick 
MIPS()45ece8a530Spatrick template <class ELFT> MIPS<ELFT>::MIPS() {
46ece8a530Spatrick   gotPltHeaderEntriesNum = 2;
47ece8a530Spatrick   defaultMaxPageSize = 65536;
48ece8a530Spatrick   pltEntrySize = 16;
49ece8a530Spatrick   pltHeaderSize = 32;
50ece8a530Spatrick   copyRel = R_MIPS_COPY;
51ece8a530Spatrick   pltRel = R_MIPS_JUMP_SLOT;
52ece8a530Spatrick   needsThunks = true;
53ece8a530Spatrick 
54ece8a530Spatrick   // Set `sigrie 1` as a trap instruction.
55ece8a530Spatrick   write32(trapInstr.data(), 0x04170001);
56ece8a530Spatrick 
57ece8a530Spatrick   if (ELFT::Is64Bits) {
58ece8a530Spatrick     relativeRel = (R_MIPS_64 << 8) | R_MIPS_REL32;
59ece8a530Spatrick     symbolicRel = R_MIPS_64;
60ece8a530Spatrick     tlsGotRel = R_MIPS_TLS_TPREL64;
61ece8a530Spatrick     tlsModuleIndexRel = R_MIPS_TLS_DTPMOD64;
62ece8a530Spatrick     tlsOffsetRel = R_MIPS_TLS_DTPREL64;
63ece8a530Spatrick   } else {
64ece8a530Spatrick     relativeRel = R_MIPS_REL32;
65ece8a530Spatrick     symbolicRel = R_MIPS_32;
66ece8a530Spatrick     tlsGotRel = R_MIPS_TLS_TPREL32;
67ece8a530Spatrick     tlsModuleIndexRel = R_MIPS_TLS_DTPMOD32;
68ece8a530Spatrick     tlsOffsetRel = R_MIPS_TLS_DTPREL32;
69ece8a530Spatrick   }
70ece8a530Spatrick }
71ece8a530Spatrick 
calcEFlags() const72ece8a530Spatrick template <class ELFT> uint32_t MIPS<ELFT>::calcEFlags() const {
73ece8a530Spatrick   return calcMipsEFlags<ELFT>();
74ece8a530Spatrick }
75ece8a530Spatrick 
76ece8a530Spatrick template <class ELFT>
getRelExpr(RelType type,const Symbol & s,const uint8_t * loc) const77ece8a530Spatrick RelExpr MIPS<ELFT>::getRelExpr(RelType type, const Symbol &s,
78ece8a530Spatrick                                const uint8_t *loc) const {
79ece8a530Spatrick   // See comment in the calculateMipsRelChain.
80ece8a530Spatrick   if (ELFT::Is64Bits || config->mipsN32Abi)
81ece8a530Spatrick     type &= 0xff;
82ece8a530Spatrick 
83ece8a530Spatrick   switch (type) {
84ece8a530Spatrick   case R_MIPS_JALR:
85ece8a530Spatrick     // Older versions of clang would erroneously emit this relocation not only
86ece8a530Spatrick     // against functions (loaded from the GOT) but also against data symbols
87ece8a530Spatrick     // (e.g. a table of function pointers). When we encounter this, ignore the
88ece8a530Spatrick     // relocation and emit a warning instead.
89ece8a530Spatrick     if (!s.isFunc() && s.type != STT_NOTYPE) {
90ece8a530Spatrick       warn(getErrorLocation(loc) +
91ece8a530Spatrick            "found R_MIPS_JALR relocation against non-function symbol " +
92ece8a530Spatrick            toString(s) + ". This is invalid and most likely a compiler bug.");
93ece8a530Spatrick       return R_NONE;
94ece8a530Spatrick     }
95ece8a530Spatrick 
96ece8a530Spatrick     // If the target symbol is not preemptible and is not microMIPS,
97ece8a530Spatrick     // it might be possible to replace jalr/jr instruction by bal/b.
98ece8a530Spatrick     // It depends on the target symbol's offset.
99ece8a530Spatrick     if (!s.isPreemptible && !(s.getVA() & 0x1))
100ece8a530Spatrick       return R_PC;
101ece8a530Spatrick     return R_NONE;
102ece8a530Spatrick   case R_MICROMIPS_JALR:
103ece8a530Spatrick     return R_NONE;
104ece8a530Spatrick   case R_MIPS_GPREL16:
105ece8a530Spatrick   case R_MIPS_GPREL32:
106ece8a530Spatrick   case R_MICROMIPS_GPREL16:
107ece8a530Spatrick   case R_MICROMIPS_GPREL7_S2:
108ece8a530Spatrick     return R_MIPS_GOTREL;
109ece8a530Spatrick   case R_MIPS_26:
110ece8a530Spatrick   case R_MICROMIPS_26_S1:
111ece8a530Spatrick     return R_PLT;
112ece8a530Spatrick   case R_MICROMIPS_PC26_S1:
113ece8a530Spatrick     return R_PLT_PC;
114ece8a530Spatrick   case R_MIPS_HI16:
115ece8a530Spatrick   case R_MIPS_LO16:
116ece8a530Spatrick   case R_MIPS_HIGHER:
117ece8a530Spatrick   case R_MIPS_HIGHEST:
118ece8a530Spatrick   case R_MICROMIPS_HI16:
119ece8a530Spatrick   case R_MICROMIPS_LO16:
120ece8a530Spatrick     // R_MIPS_HI16/R_MIPS_LO16 relocations against _gp_disp calculate
121ece8a530Spatrick     // offset between start of function and 'gp' value which by default
122ece8a530Spatrick     // equal to the start of .got section. In that case we consider these
123ece8a530Spatrick     // relocations as relative.
124ece8a530Spatrick     if (&s == ElfSym::mipsGpDisp)
125ece8a530Spatrick       return R_MIPS_GOT_GP_PC;
126ece8a530Spatrick     if (&s == ElfSym::mipsLocalGp)
127ece8a530Spatrick       return R_MIPS_GOT_GP;
128*dfe94b16Srobert     [[fallthrough]];
129ece8a530Spatrick   case R_MIPS_32:
130ece8a530Spatrick   case R_MIPS_64:
131ece8a530Spatrick   case R_MIPS_GOT_OFST:
132ece8a530Spatrick   case R_MIPS_SUB:
133*dfe94b16Srobert     return R_ABS;
134ece8a530Spatrick   case R_MIPS_TLS_DTPREL_HI16:
135ece8a530Spatrick   case R_MIPS_TLS_DTPREL_LO16:
136ece8a530Spatrick   case R_MIPS_TLS_DTPREL32:
137ece8a530Spatrick   case R_MIPS_TLS_DTPREL64:
138ece8a530Spatrick   case R_MICROMIPS_TLS_DTPREL_HI16:
139ece8a530Spatrick   case R_MICROMIPS_TLS_DTPREL_LO16:
140*dfe94b16Srobert     return R_DTPREL;
141ece8a530Spatrick   case R_MIPS_TLS_TPREL_HI16:
142ece8a530Spatrick   case R_MIPS_TLS_TPREL_LO16:
143ece8a530Spatrick   case R_MIPS_TLS_TPREL32:
144ece8a530Spatrick   case R_MIPS_TLS_TPREL64:
145ece8a530Spatrick   case R_MICROMIPS_TLS_TPREL_HI16:
146ece8a530Spatrick   case R_MICROMIPS_TLS_TPREL_LO16:
1471cf9926bSpatrick     return R_TPREL;
148ece8a530Spatrick   case R_MIPS_PC32:
149ece8a530Spatrick   case R_MIPS_PC16:
150ece8a530Spatrick   case R_MIPS_PC19_S2:
151ece8a530Spatrick   case R_MIPS_PC21_S2:
152ece8a530Spatrick   case R_MIPS_PC26_S2:
153ece8a530Spatrick   case R_MIPS_PCHI16:
154ece8a530Spatrick   case R_MIPS_PCLO16:
155ece8a530Spatrick   case R_MICROMIPS_PC7_S1:
156ece8a530Spatrick   case R_MICROMIPS_PC10_S1:
157ece8a530Spatrick   case R_MICROMIPS_PC16_S1:
158ece8a530Spatrick   case R_MICROMIPS_PC18_S3:
159ece8a530Spatrick   case R_MICROMIPS_PC19_S2:
160ece8a530Spatrick   case R_MICROMIPS_PC23_S2:
161ece8a530Spatrick   case R_MICROMIPS_PC21_S1:
162ece8a530Spatrick     return R_PC;
163ece8a530Spatrick   case R_MIPS_GOT16:
164ece8a530Spatrick   case R_MICROMIPS_GOT16:
165ece8a530Spatrick     if (s.isLocal())
166ece8a530Spatrick       return R_MIPS_GOT_LOCAL_PAGE;
167*dfe94b16Srobert     [[fallthrough]];
168ece8a530Spatrick   case R_MIPS_CALL16:
169ece8a530Spatrick   case R_MIPS_GOT_DISP:
170ece8a530Spatrick   case R_MIPS_TLS_GOTTPREL:
171ece8a530Spatrick   case R_MICROMIPS_CALL16:
172ece8a530Spatrick   case R_MICROMIPS_TLS_GOTTPREL:
173ece8a530Spatrick     return R_MIPS_GOT_OFF;
174ece8a530Spatrick   case R_MIPS_CALL_HI16:
175ece8a530Spatrick   case R_MIPS_CALL_LO16:
176ece8a530Spatrick   case R_MIPS_GOT_HI16:
177ece8a530Spatrick   case R_MIPS_GOT_LO16:
178ece8a530Spatrick   case R_MICROMIPS_CALL_HI16:
179ece8a530Spatrick   case R_MICROMIPS_CALL_LO16:
180ece8a530Spatrick   case R_MICROMIPS_GOT_HI16:
181ece8a530Spatrick   case R_MICROMIPS_GOT_LO16:
182ece8a530Spatrick     return R_MIPS_GOT_OFF32;
183ece8a530Spatrick   case R_MIPS_GOT_PAGE:
184ece8a530Spatrick     return R_MIPS_GOT_LOCAL_PAGE;
185ece8a530Spatrick   case R_MIPS_TLS_GD:
186ece8a530Spatrick   case R_MICROMIPS_TLS_GD:
187ece8a530Spatrick     return R_MIPS_TLSGD;
188ece8a530Spatrick   case R_MIPS_TLS_LDM:
189ece8a530Spatrick   case R_MICROMIPS_TLS_LDM:
190ece8a530Spatrick     return R_MIPS_TLSLD;
191ece8a530Spatrick   case R_MIPS_NONE:
192ece8a530Spatrick     return R_NONE;
193ece8a530Spatrick   default:
194ece8a530Spatrick     error(getErrorLocation(loc) + "unknown relocation (" + Twine(type) +
195ece8a530Spatrick           ") against symbol " + toString(s));
196ece8a530Spatrick     return R_NONE;
197ece8a530Spatrick   }
198ece8a530Spatrick }
199ece8a530Spatrick 
getDynRel(RelType type) const200ece8a530Spatrick template <class ELFT> RelType MIPS<ELFT>::getDynRel(RelType type) const {
201ece8a530Spatrick   if (type == symbolicRel)
202ece8a530Spatrick     return type;
203ece8a530Spatrick   return R_MIPS_NONE;
204ece8a530Spatrick }
205ece8a530Spatrick 
206ece8a530Spatrick template <class ELFT>
writeGotPlt(uint8_t * buf,const Symbol &) const207ece8a530Spatrick void MIPS<ELFT>::writeGotPlt(uint8_t *buf, const Symbol &) const {
208ece8a530Spatrick   uint64_t va = in.plt->getVA();
209ece8a530Spatrick   if (isMicroMips())
210ece8a530Spatrick     va |= 1;
211ece8a530Spatrick   write32(buf, va);
212ece8a530Spatrick }
213ece8a530Spatrick 
readShuffle(const uint8_t * loc)214ece8a530Spatrick template <endianness E> static uint32_t readShuffle(const uint8_t *loc) {
215ece8a530Spatrick   // The major opcode of a microMIPS instruction needs to appear
216ece8a530Spatrick   // in the first 16-bit word (lowest address) for efficient hardware
217ece8a530Spatrick   // decode so that it knows if the instruction is 16-bit or 32-bit
218ece8a530Spatrick   // as early as possible. To do so, little-endian binaries keep 16-bit
219ece8a530Spatrick   // words in a big-endian order. That is why we have to swap these
220ece8a530Spatrick   // words to get a correct value.
221ece8a530Spatrick   uint32_t v = read32(loc);
222ece8a530Spatrick   if (E == support::little)
223ece8a530Spatrick     return (v << 16) | (v >> 16);
224ece8a530Spatrick   return v;
225ece8a530Spatrick }
226ece8a530Spatrick 
writeValue(uint8_t * loc,uint64_t v,uint8_t bitsSize,uint8_t shift)227ece8a530Spatrick static void writeValue(uint8_t *loc, uint64_t v, uint8_t bitsSize,
228ece8a530Spatrick                        uint8_t shift) {
229ece8a530Spatrick   uint32_t instr = read32(loc);
230ece8a530Spatrick   uint32_t mask = 0xffffffff >> (32 - bitsSize);
231ece8a530Spatrick   uint32_t data = (instr & ~mask) | ((v >> shift) & mask);
232ece8a530Spatrick   write32(loc, data);
233ece8a530Spatrick }
234ece8a530Spatrick 
235ece8a530Spatrick template <endianness E>
writeShuffleValue(uint8_t * loc,uint64_t v,uint8_t bitsSize,uint8_t shift)236ece8a530Spatrick static void writeShuffleValue(uint8_t *loc, uint64_t v, uint8_t bitsSize,
237ece8a530Spatrick                               uint8_t shift) {
238ece8a530Spatrick   // See comments in readShuffle for purpose of this code.
239ece8a530Spatrick   uint16_t *words = (uint16_t *)loc;
240ece8a530Spatrick   if (E == support::little)
241ece8a530Spatrick     std::swap(words[0], words[1]);
242ece8a530Spatrick 
243ece8a530Spatrick   writeValue(loc, v, bitsSize, shift);
244ece8a530Spatrick 
245ece8a530Spatrick   if (E == support::little)
246ece8a530Spatrick     std::swap(words[0], words[1]);
247ece8a530Spatrick }
248ece8a530Spatrick 
249ece8a530Spatrick template <endianness E>
writeMicroRelocation16(uint8_t * loc,uint64_t v,uint8_t bitsSize,uint8_t shift)250ece8a530Spatrick static void writeMicroRelocation16(uint8_t *loc, uint64_t v, uint8_t bitsSize,
251ece8a530Spatrick                                    uint8_t shift) {
252ece8a530Spatrick   uint16_t instr = read16(loc);
253ece8a530Spatrick   uint16_t mask = 0xffff >> (16 - bitsSize);
254ece8a530Spatrick   uint16_t data = (instr & ~mask) | ((v >> shift) & mask);
255ece8a530Spatrick   write16(loc, data);
256ece8a530Spatrick }
257ece8a530Spatrick 
writePltHeader(uint8_t * buf) const258ece8a530Spatrick template <class ELFT> void MIPS<ELFT>::writePltHeader(uint8_t *buf) const {
259ece8a530Spatrick   if (isMicroMips()) {
260ece8a530Spatrick     uint64_t gotPlt = in.gotPlt->getVA();
261ece8a530Spatrick     uint64_t plt = in.plt->getVA();
262ece8a530Spatrick     // Overwrite trap instructions written by Writer::writeTrapInstr.
263ece8a530Spatrick     memset(buf, 0, pltHeaderSize);
264ece8a530Spatrick 
265ece8a530Spatrick     write16(buf, isMipsR6() ? 0x7860 : 0x7980);  // addiupc v1, (GOTPLT) - .
266ece8a530Spatrick     write16(buf + 4, 0xff23);    // lw      $25, 0($3)
267ece8a530Spatrick     write16(buf + 8, 0x0535);    // subu16  $2,  $2, $3
268ece8a530Spatrick     write16(buf + 10, 0x2525);   // srl16   $2,  $2, 2
269ece8a530Spatrick     write16(buf + 12, 0x3302);   // addiu   $24, $2, -2
270ece8a530Spatrick     write16(buf + 14, 0xfffe);
271ece8a530Spatrick     write16(buf + 16, 0x0dff);   // move    $15, $31
272ece8a530Spatrick     if (isMipsR6()) {
273ece8a530Spatrick       write16(buf + 18, 0x0f83); // move    $28, $3
274ece8a530Spatrick       write16(buf + 20, 0x472b); // jalrc   $25
275ece8a530Spatrick       write16(buf + 22, 0x0c00); // nop
276bb684c34Spatrick       relocateNoSym(buf, R_MICROMIPS_PC19_S2, gotPlt - plt);
277ece8a530Spatrick     } else {
278ece8a530Spatrick       write16(buf + 18, 0x45f9); // jalrc   $25
279ece8a530Spatrick       write16(buf + 20, 0x0f83); // move    $28, $3
280ece8a530Spatrick       write16(buf + 22, 0x0c00); // nop
281bb684c34Spatrick       relocateNoSym(buf, R_MICROMIPS_PC23_S2, gotPlt - plt);
282ece8a530Spatrick     }
283ece8a530Spatrick     return;
284ece8a530Spatrick   }
285ece8a530Spatrick 
286ece8a530Spatrick   if (config->mipsN32Abi) {
287ece8a530Spatrick     write32(buf, 0x3c0e0000);      // lui   $14, %hi(&GOTPLT[0])
288ece8a530Spatrick     write32(buf + 4, 0x8dd90000);  // lw    $25, %lo(&GOTPLT[0])($14)
289ece8a530Spatrick     write32(buf + 8, 0x25ce0000);  // addiu $14, $14, %lo(&GOTPLT[0])
290ece8a530Spatrick     write32(buf + 12, 0x030ec023); // subu  $24, $24, $14
291ece8a530Spatrick     write32(buf + 16, 0x03e07825); // move  $15, $31
292ece8a530Spatrick     write32(buf + 20, 0x0018c082); // srl   $24, $24, 2
293ece8a530Spatrick   } else if (ELFT::Is64Bits) {
294ece8a530Spatrick     write32(buf, 0x3c0e0000);      // lui   $14, %hi(&GOTPLT[0])
295ece8a530Spatrick     write32(buf + 4, 0xddd90000);  // ld    $25, %lo(&GOTPLT[0])($14)
296ece8a530Spatrick     write32(buf + 8, 0x25ce0000);  // addiu $14, $14, %lo(&GOTPLT[0])
297ece8a530Spatrick     write32(buf + 12, 0x030ec023); // subu  $24, $24, $14
298ece8a530Spatrick     write32(buf + 16, 0x03e07825); // move  $15, $31
299ece8a530Spatrick     write32(buf + 20, 0x0018c0c2); // srl   $24, $24, 3
300ece8a530Spatrick   } else {
301ece8a530Spatrick     write32(buf, 0x3c1c0000);      // lui   $28, %hi(&GOTPLT[0])
302ece8a530Spatrick     write32(buf + 4, 0x8f990000);  // lw    $25, %lo(&GOTPLT[0])($28)
303ece8a530Spatrick     write32(buf + 8, 0x279c0000);  // addiu $28, $28, %lo(&GOTPLT[0])
304ece8a530Spatrick     write32(buf + 12, 0x031cc023); // subu  $24, $24, $28
305ece8a530Spatrick     write32(buf + 16, 0x03e07825); // move  $15, $31
306ece8a530Spatrick     write32(buf + 20, 0x0018c082); // srl   $24, $24, 2
307ece8a530Spatrick   }
308ece8a530Spatrick 
309ece8a530Spatrick   uint32_t jalrInst = config->zHazardplt ? 0x0320fc09 : 0x0320f809;
310ece8a530Spatrick   write32(buf + 24, jalrInst); // jalr.hb $25 or jalr $25
311ece8a530Spatrick   write32(buf + 28, 0x2718fffe); // subu  $24, $24, 2
312ece8a530Spatrick 
313ece8a530Spatrick   uint64_t gotPlt = in.gotPlt->getVA();
314ece8a530Spatrick   writeValue(buf, gotPlt + 0x8000, 16, 16);
315ece8a530Spatrick   writeValue(buf + 4, gotPlt, 16, 0);
316ece8a530Spatrick   writeValue(buf + 8, gotPlt, 16, 0);
317ece8a530Spatrick }
318ece8a530Spatrick 
319ece8a530Spatrick template <class ELFT>
writePlt(uint8_t * buf,const Symbol & sym,uint64_t pltEntryAddr) const320ece8a530Spatrick void MIPS<ELFT>::writePlt(uint8_t *buf, const Symbol &sym,
321ece8a530Spatrick                           uint64_t pltEntryAddr) const {
322ece8a530Spatrick   uint64_t gotPltEntryAddr = sym.getGotPltVA();
323ece8a530Spatrick   if (isMicroMips()) {
324ece8a530Spatrick     // Overwrite trap instructions written by Writer::writeTrapInstr.
325ece8a530Spatrick     memset(buf, 0, pltEntrySize);
326ece8a530Spatrick 
327ece8a530Spatrick     if (isMipsR6()) {
328ece8a530Spatrick       write16(buf, 0x7840);      // addiupc $2, (GOTPLT) - .
329ece8a530Spatrick       write16(buf + 4, 0xff22);  // lw $25, 0($2)
330ece8a530Spatrick       write16(buf + 8, 0x0f02);  // move $24, $2
331ece8a530Spatrick       write16(buf + 10, 0x4723); // jrc $25 / jr16 $25
332bb684c34Spatrick       relocateNoSym(buf, R_MICROMIPS_PC19_S2, gotPltEntryAddr - pltEntryAddr);
333ece8a530Spatrick     } else {
334ece8a530Spatrick       write16(buf, 0x7900);      // addiupc $2, (GOTPLT) - .
335ece8a530Spatrick       write16(buf + 4, 0xff22);  // lw $25, 0($2)
336ece8a530Spatrick       write16(buf + 8, 0x4599);  // jrc $25 / jr16 $25
337ece8a530Spatrick       write16(buf + 10, 0x0f02); // move $24, $2
338bb684c34Spatrick       relocateNoSym(buf, R_MICROMIPS_PC23_S2, gotPltEntryAddr - pltEntryAddr);
339ece8a530Spatrick     }
340ece8a530Spatrick     return;
341ece8a530Spatrick   }
342ece8a530Spatrick 
343ece8a530Spatrick   uint32_t loadInst = ELFT::Is64Bits ? 0xddf90000 : 0x8df90000;
344ece8a530Spatrick   uint32_t jrInst = isMipsR6() ? (config->zHazardplt ? 0x03200409 : 0x03200009)
345ece8a530Spatrick                                : (config->zHazardplt ? 0x03200408 : 0x03200008);
346ece8a530Spatrick   uint32_t addInst = ELFT::Is64Bits ? 0x65f80000 : 0x25f80000;
347ece8a530Spatrick 
348ece8a530Spatrick   write32(buf, 0x3c0f0000);     // lui   $15, %hi(.got.plt entry)
349ece8a530Spatrick   write32(buf + 4, loadInst);   // l[wd] $25, %lo(.got.plt entry)($15)
350ece8a530Spatrick   write32(buf + 8, jrInst);     // jr  $25 / jr.hb $25
351ece8a530Spatrick   write32(buf + 12, addInst);   // [d]addiu $24, $15, %lo(.got.plt entry)
352ece8a530Spatrick   writeValue(buf, gotPltEntryAddr + 0x8000, 16, 16);
353ece8a530Spatrick   writeValue(buf + 4, gotPltEntryAddr, 16, 0);
354ece8a530Spatrick   writeValue(buf + 12, gotPltEntryAddr, 16, 0);
355ece8a530Spatrick }
356ece8a530Spatrick 
357ece8a530Spatrick template <class ELFT>
needsThunk(RelExpr expr,RelType type,const InputFile * file,uint64_t branchAddr,const Symbol & s,int64_t) const358ece8a530Spatrick bool MIPS<ELFT>::needsThunk(RelExpr expr, RelType type, const InputFile *file,
359ece8a530Spatrick                             uint64_t branchAddr, const Symbol &s,
360ece8a530Spatrick                             int64_t /*a*/) const {
361ece8a530Spatrick   // Any MIPS PIC code function is invoked with its address in register $t9.
362ece8a530Spatrick   // So if we have a branch instruction from non-PIC code to the PIC one
363ece8a530Spatrick   // we cannot make the jump directly and need to create a small stubs
364ece8a530Spatrick   // to save the target function address.
365ece8a530Spatrick   // See page 3-38 ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
366ece8a530Spatrick   if (type != R_MIPS_26 && type != R_MIPS_PC26_S2 &&
367ece8a530Spatrick       type != R_MICROMIPS_26_S1 && type != R_MICROMIPS_PC26_S1)
368ece8a530Spatrick     return false;
369ece8a530Spatrick   auto *f = dyn_cast_or_null<ObjFile<ELFT>>(file);
370ece8a530Spatrick   if (!f)
371ece8a530Spatrick     return false;
372ece8a530Spatrick   // If current file has PIC code, LA25 stub is not required.
3731cf9926bSpatrick   if (f->getObj().getHeader().e_flags & EF_MIPS_PIC)
374ece8a530Spatrick     return false;
375ece8a530Spatrick   auto *d = dyn_cast<Defined>(&s);
376ece8a530Spatrick   // LA25 is required if target file has PIC code
377ece8a530Spatrick   // or target symbol is a PIC symbol.
378ece8a530Spatrick   return d && isMipsPIC<ELFT>(d);
379ece8a530Spatrick }
380ece8a530Spatrick 
381ece8a530Spatrick template <class ELFT>
getImplicitAddend(const uint8_t * buf,RelType type) const382ece8a530Spatrick int64_t MIPS<ELFT>::getImplicitAddend(const uint8_t *buf, RelType type) const {
383ece8a530Spatrick   const endianness e = ELFT::TargetEndianness;
384ece8a530Spatrick   switch (type) {
385ece8a530Spatrick   case R_MIPS_32:
3861cf9926bSpatrick   case R_MIPS_REL32:
387ece8a530Spatrick   case R_MIPS_GPREL32:
388ece8a530Spatrick   case R_MIPS_TLS_DTPREL32:
3891cf9926bSpatrick   case R_MIPS_TLS_DTPMOD32:
390ece8a530Spatrick   case R_MIPS_TLS_TPREL32:
391ece8a530Spatrick     return SignExtend64<32>(read32(buf));
392ece8a530Spatrick   case R_MIPS_26:
393ece8a530Spatrick     // FIXME (simon): If the relocation target symbol is not a PLT entry
394ece8a530Spatrick     // we should use another expression for calculation:
395ece8a530Spatrick     // ((A << 2) | (P & 0xf0000000)) >> 2
396ece8a530Spatrick     return SignExtend64<28>(read32(buf) << 2);
3971cf9926bSpatrick   case R_MIPS_CALL_HI16:
398ece8a530Spatrick   case R_MIPS_GOT16:
3991cf9926bSpatrick   case R_MIPS_GOT_HI16:
400ece8a530Spatrick   case R_MIPS_HI16:
401ece8a530Spatrick   case R_MIPS_PCHI16:
402ece8a530Spatrick     return SignExtend64<16>(read32(buf)) << 16;
4031cf9926bSpatrick   case R_MIPS_CALL16:
4041cf9926bSpatrick   case R_MIPS_CALL_LO16:
4051cf9926bSpatrick   case R_MIPS_GOT_LO16:
406ece8a530Spatrick   case R_MIPS_GPREL16:
407ece8a530Spatrick   case R_MIPS_LO16:
408ece8a530Spatrick   case R_MIPS_PCLO16:
409ece8a530Spatrick   case R_MIPS_TLS_DTPREL_HI16:
410ece8a530Spatrick   case R_MIPS_TLS_DTPREL_LO16:
4111cf9926bSpatrick   case R_MIPS_TLS_GD:
4121cf9926bSpatrick   case R_MIPS_TLS_GOTTPREL:
4131cf9926bSpatrick   case R_MIPS_TLS_LDM:
414ece8a530Spatrick   case R_MIPS_TLS_TPREL_HI16:
415ece8a530Spatrick   case R_MIPS_TLS_TPREL_LO16:
416ece8a530Spatrick     return SignExtend64<16>(read32(buf));
417ece8a530Spatrick   case R_MICROMIPS_GOT16:
418ece8a530Spatrick   case R_MICROMIPS_HI16:
419ece8a530Spatrick     return SignExtend64<16>(readShuffle<e>(buf)) << 16;
4201cf9926bSpatrick   case R_MICROMIPS_CALL16:
421ece8a530Spatrick   case R_MICROMIPS_GPREL16:
422ece8a530Spatrick   case R_MICROMIPS_LO16:
423ece8a530Spatrick   case R_MICROMIPS_TLS_DTPREL_HI16:
424ece8a530Spatrick   case R_MICROMIPS_TLS_DTPREL_LO16:
4251cf9926bSpatrick   case R_MICROMIPS_TLS_GD:
4261cf9926bSpatrick   case R_MICROMIPS_TLS_GOTTPREL:
4271cf9926bSpatrick   case R_MICROMIPS_TLS_LDM:
428ece8a530Spatrick   case R_MICROMIPS_TLS_TPREL_HI16:
429ece8a530Spatrick   case R_MICROMIPS_TLS_TPREL_LO16:
430ece8a530Spatrick     return SignExtend64<16>(readShuffle<e>(buf));
431ece8a530Spatrick   case R_MICROMIPS_GPREL7_S2:
432ece8a530Spatrick     return SignExtend64<9>(readShuffle<e>(buf) << 2);
433ece8a530Spatrick   case R_MIPS_PC16:
434ece8a530Spatrick     return SignExtend64<18>(read32(buf) << 2);
435ece8a530Spatrick   case R_MIPS_PC19_S2:
436ece8a530Spatrick     return SignExtend64<21>(read32(buf) << 2);
437ece8a530Spatrick   case R_MIPS_PC21_S2:
438ece8a530Spatrick     return SignExtend64<23>(read32(buf) << 2);
439ece8a530Spatrick   case R_MIPS_PC26_S2:
440ece8a530Spatrick     return SignExtend64<28>(read32(buf) << 2);
441ece8a530Spatrick   case R_MIPS_PC32:
442ece8a530Spatrick     return SignExtend64<32>(read32(buf));
443ece8a530Spatrick   case R_MICROMIPS_26_S1:
444ece8a530Spatrick     return SignExtend64<27>(readShuffle<e>(buf) << 1);
445ece8a530Spatrick   case R_MICROMIPS_PC7_S1:
446ece8a530Spatrick     return SignExtend64<8>(read16(buf) << 1);
447ece8a530Spatrick   case R_MICROMIPS_PC10_S1:
448ece8a530Spatrick     return SignExtend64<11>(read16(buf) << 1);
449ece8a530Spatrick   case R_MICROMIPS_PC16_S1:
450ece8a530Spatrick     return SignExtend64<17>(readShuffle<e>(buf) << 1);
451ece8a530Spatrick   case R_MICROMIPS_PC18_S3:
452ece8a530Spatrick     return SignExtend64<21>(readShuffle<e>(buf) << 3);
453ece8a530Spatrick   case R_MICROMIPS_PC19_S2:
454ece8a530Spatrick     return SignExtend64<21>(readShuffle<e>(buf) << 2);
455ece8a530Spatrick   case R_MICROMIPS_PC21_S1:
456ece8a530Spatrick     return SignExtend64<22>(readShuffle<e>(buf) << 1);
457ece8a530Spatrick   case R_MICROMIPS_PC23_S2:
458ece8a530Spatrick     return SignExtend64<25>(readShuffle<e>(buf) << 2);
459ece8a530Spatrick   case R_MICROMIPS_PC26_S1:
460ece8a530Spatrick     return SignExtend64<27>(readShuffle<e>(buf) << 1);
4611cf9926bSpatrick   case R_MIPS_64:
4621cf9926bSpatrick   case R_MIPS_TLS_DTPMOD64:
4631cf9926bSpatrick   case R_MIPS_TLS_DTPREL64:
4641cf9926bSpatrick   case R_MIPS_TLS_TPREL64:
4651cf9926bSpatrick   case (R_MIPS_64 << 8) | R_MIPS_REL32:
4661cf9926bSpatrick     return read64(buf);
4671cf9926bSpatrick   case R_MIPS_COPY:
4681cf9926bSpatrick     return config->is64 ? read64(buf) : read32(buf);
4691cf9926bSpatrick   case R_MIPS_NONE:
4701cf9926bSpatrick   case R_MIPS_JUMP_SLOT:
4711cf9926bSpatrick   case R_MIPS_JALR:
4721cf9926bSpatrick     // These relocations are defined as not having an implicit addend.
4731cf9926bSpatrick     return 0;
474ece8a530Spatrick   default:
4751cf9926bSpatrick     internalLinkerError(getErrorLocation(buf),
4761cf9926bSpatrick                         "cannot read addend for relocation " + toString(type));
477ece8a530Spatrick     return 0;
478ece8a530Spatrick   }
479ece8a530Spatrick }
480ece8a530Spatrick 
481ece8a530Spatrick static std::pair<uint32_t, uint64_t>
calculateMipsRelChain(uint8_t * loc,RelType type,uint64_t val)482ece8a530Spatrick calculateMipsRelChain(uint8_t *loc, RelType type, uint64_t val) {
483ece8a530Spatrick   // MIPS N64 ABI packs multiple relocations into the single relocation
484ece8a530Spatrick   // record. In general, all up to three relocations can have arbitrary
485ece8a530Spatrick   // types. In fact, Clang and GCC uses only a few combinations. For now,
486ece8a530Spatrick   // we support two of them. That is allow to pass at least all LLVM
487ece8a530Spatrick   // test suite cases.
488ece8a530Spatrick   // <any relocation> / R_MIPS_SUB / R_MIPS_HI16 | R_MIPS_LO16
489ece8a530Spatrick   // <any relocation> / R_MIPS_64 / R_MIPS_NONE
490ece8a530Spatrick   // The first relocation is a 'real' relocation which is calculated
491ece8a530Spatrick   // using the corresponding symbol's value. The second and the third
492ece8a530Spatrick   // relocations used to modify result of the first one: extend it to
493ece8a530Spatrick   // 64-bit, extract high or low part etc. For details, see part 2.9 Relocation
494ece8a530Spatrick   // at the https://dmz-portal.mips.com/mw/images/8/82/007-4658-001.pdf
495ece8a530Spatrick   RelType type2 = (type >> 8) & 0xff;
496ece8a530Spatrick   RelType type3 = (type >> 16) & 0xff;
497ece8a530Spatrick   if (type2 == R_MIPS_NONE && type3 == R_MIPS_NONE)
498ece8a530Spatrick     return std::make_pair(type, val);
499ece8a530Spatrick   if (type2 == R_MIPS_64 && type3 == R_MIPS_NONE)
500ece8a530Spatrick     return std::make_pair(type2, val);
501ece8a530Spatrick   if (type2 == R_MIPS_SUB && (type3 == R_MIPS_HI16 || type3 == R_MIPS_LO16))
502ece8a530Spatrick     return std::make_pair(type3, -val);
503ece8a530Spatrick   error(getErrorLocation(loc) + "unsupported relocations combination " +
504ece8a530Spatrick         Twine(type));
505ece8a530Spatrick   return std::make_pair(type & 0xff, val);
506ece8a530Spatrick }
507ece8a530Spatrick 
isBranchReloc(RelType type)508ece8a530Spatrick static bool isBranchReloc(RelType type) {
509ece8a530Spatrick   return type == R_MIPS_26 || type == R_MIPS_PC26_S2 ||
510ece8a530Spatrick          type == R_MIPS_PC21_S2 || type == R_MIPS_PC16;
511ece8a530Spatrick }
512ece8a530Spatrick 
isMicroBranchReloc(RelType type)513ece8a530Spatrick static bool isMicroBranchReloc(RelType type) {
514ece8a530Spatrick   return type == R_MICROMIPS_26_S1 || type == R_MICROMIPS_PC16_S1 ||
515ece8a530Spatrick          type == R_MICROMIPS_PC10_S1 || type == R_MICROMIPS_PC7_S1;
516ece8a530Spatrick }
517ece8a530Spatrick 
518ece8a530Spatrick template <class ELFT>
fixupCrossModeJump(uint8_t * loc,RelType type,uint64_t val)519ece8a530Spatrick static uint64_t fixupCrossModeJump(uint8_t *loc, RelType type, uint64_t val) {
520ece8a530Spatrick   // Here we need to detect jump/branch from regular MIPS code
521ece8a530Spatrick   // to a microMIPS target and vice versa. In that cases jump
522ece8a530Spatrick   // instructions need to be replaced by their "cross-mode"
523ece8a530Spatrick   // equivalents.
524ece8a530Spatrick   const endianness e = ELFT::TargetEndianness;
525ece8a530Spatrick   bool isMicroTgt = val & 0x1;
526ece8a530Spatrick   bool isCrossJump = (isMicroTgt && isBranchReloc(type)) ||
527ece8a530Spatrick                      (!isMicroTgt && isMicroBranchReloc(type));
528ece8a530Spatrick   if (!isCrossJump)
529ece8a530Spatrick     return val;
530ece8a530Spatrick 
531ece8a530Spatrick   switch (type) {
532ece8a530Spatrick   case R_MIPS_26: {
533ece8a530Spatrick     uint32_t inst = read32(loc) >> 26;
534ece8a530Spatrick     if (inst == 0x3 || inst == 0x1d) { // JAL or JALX
535ece8a530Spatrick       writeValue(loc, 0x1d << 26, 32, 0);
536ece8a530Spatrick       return val;
537ece8a530Spatrick     }
538ece8a530Spatrick     break;
539ece8a530Spatrick   }
540ece8a530Spatrick   case R_MICROMIPS_26_S1: {
541ece8a530Spatrick     uint32_t inst = readShuffle<e>(loc) >> 26;
542ece8a530Spatrick     if (inst == 0x3d || inst == 0x3c) { // JAL32 or JALX32
543ece8a530Spatrick       val >>= 1;
544ece8a530Spatrick       writeShuffleValue<e>(loc, 0x3c << 26, 32, 0);
545ece8a530Spatrick       return val;
546ece8a530Spatrick     }
547ece8a530Spatrick     break;
548ece8a530Spatrick   }
549ece8a530Spatrick   case R_MIPS_PC26_S2:
550ece8a530Spatrick   case R_MIPS_PC21_S2:
551ece8a530Spatrick   case R_MIPS_PC16:
552ece8a530Spatrick   case R_MICROMIPS_PC16_S1:
553ece8a530Spatrick   case R_MICROMIPS_PC10_S1:
554ece8a530Spatrick   case R_MICROMIPS_PC7_S1:
555ece8a530Spatrick     // FIXME (simon): Support valid branch relocations.
556ece8a530Spatrick     break;
557ece8a530Spatrick   default:
558ece8a530Spatrick     llvm_unreachable("unexpected jump/branch relocation");
559ece8a530Spatrick   }
560ece8a530Spatrick 
561ece8a530Spatrick   error(getErrorLocation(loc) +
562ece8a530Spatrick         "unsupported jump/branch instruction between ISA modes referenced by " +
563ece8a530Spatrick         toString(type) + " relocation");
564ece8a530Spatrick   return val;
565ece8a530Spatrick }
566ece8a530Spatrick 
567ece8a530Spatrick template <class ELFT>
relocate(uint8_t * loc,const Relocation & rel,uint64_t val) const568bb684c34Spatrick void MIPS<ELFT>::relocate(uint8_t *loc, const Relocation &rel,
569bb684c34Spatrick                           uint64_t val) const {
570ece8a530Spatrick   const endianness e = ELFT::TargetEndianness;
571bb684c34Spatrick   RelType type = rel.type;
572ece8a530Spatrick 
573ece8a530Spatrick   if (ELFT::Is64Bits || config->mipsN32Abi)
574ece8a530Spatrick     std::tie(type, val) = calculateMipsRelChain(loc, type, val);
575ece8a530Spatrick 
576ece8a530Spatrick   // Detect cross-mode jump/branch and fix instruction.
577ece8a530Spatrick   val = fixupCrossModeJump<ELFT>(loc, type, val);
578ece8a530Spatrick 
579ece8a530Spatrick   // Thread pointer and DRP offsets from the start of TLS data area.
580ece8a530Spatrick   // https://www.linux-mips.org/wiki/NPTL
581ece8a530Spatrick   if (type == R_MIPS_TLS_DTPREL_HI16 || type == R_MIPS_TLS_DTPREL_LO16 ||
582ece8a530Spatrick       type == R_MIPS_TLS_DTPREL32 || type == R_MIPS_TLS_DTPREL64 ||
583ece8a530Spatrick       type == R_MICROMIPS_TLS_DTPREL_HI16 ||
584ece8a530Spatrick       type == R_MICROMIPS_TLS_DTPREL_LO16) {
585ece8a530Spatrick     val -= 0x8000;
586ece8a530Spatrick   }
587ece8a530Spatrick 
588ece8a530Spatrick   switch (type) {
589ece8a530Spatrick   case R_MIPS_32:
590ece8a530Spatrick   case R_MIPS_GPREL32:
591ece8a530Spatrick   case R_MIPS_TLS_DTPREL32:
592ece8a530Spatrick   case R_MIPS_TLS_TPREL32:
593ece8a530Spatrick     write32(loc, val);
594ece8a530Spatrick     break;
595ece8a530Spatrick   case R_MIPS_64:
596ece8a530Spatrick   case R_MIPS_TLS_DTPREL64:
597ece8a530Spatrick   case R_MIPS_TLS_TPREL64:
598ece8a530Spatrick     write64(loc, val);
599ece8a530Spatrick     break;
600ece8a530Spatrick   case R_MIPS_26:
601ece8a530Spatrick     writeValue(loc, val, 26, 2);
602ece8a530Spatrick     break;
603ece8a530Spatrick   case R_MIPS_GOT16:
604ece8a530Spatrick     // The R_MIPS_GOT16 relocation's value in "relocatable" linking mode
605ece8a530Spatrick     // is updated addend (not a GOT index). In that case write high 16 bits
606ece8a530Spatrick     // to store a correct addend value.
607ece8a530Spatrick     if (config->relocatable) {
608ece8a530Spatrick       writeValue(loc, val + 0x8000, 16, 16);
609ece8a530Spatrick     } else {
610bb684c34Spatrick       checkInt(loc, val, 16, rel);
611ece8a530Spatrick       writeValue(loc, val, 16, 0);
612ece8a530Spatrick     }
613ece8a530Spatrick     break;
614ece8a530Spatrick   case R_MICROMIPS_GOT16:
615ece8a530Spatrick     if (config->relocatable) {
616ece8a530Spatrick       writeShuffleValue<e>(loc, val + 0x8000, 16, 16);
617ece8a530Spatrick     } else {
618bb684c34Spatrick       checkInt(loc, val, 16, rel);
619ece8a530Spatrick       writeShuffleValue<e>(loc, val, 16, 0);
620ece8a530Spatrick     }
621ece8a530Spatrick     break;
622ece8a530Spatrick   case R_MIPS_CALL16:
623ece8a530Spatrick   case R_MIPS_GOT_DISP:
624ece8a530Spatrick   case R_MIPS_GOT_PAGE:
625ece8a530Spatrick   case R_MIPS_GPREL16:
626ece8a530Spatrick   case R_MIPS_TLS_GD:
627ece8a530Spatrick   case R_MIPS_TLS_GOTTPREL:
628ece8a530Spatrick   case R_MIPS_TLS_LDM:
629bb684c34Spatrick     checkInt(loc, val, 16, rel);
630*dfe94b16Srobert     [[fallthrough]];
631ece8a530Spatrick   case R_MIPS_CALL_LO16:
632ece8a530Spatrick   case R_MIPS_GOT_LO16:
633ece8a530Spatrick   case R_MIPS_GOT_OFST:
634ece8a530Spatrick   case R_MIPS_LO16:
635ece8a530Spatrick   case R_MIPS_PCLO16:
636ece8a530Spatrick   case R_MIPS_TLS_DTPREL_LO16:
637ece8a530Spatrick   case R_MIPS_TLS_TPREL_LO16:
638ece8a530Spatrick     writeValue(loc, val, 16, 0);
639ece8a530Spatrick     break;
640ece8a530Spatrick   case R_MICROMIPS_GPREL16:
641ece8a530Spatrick   case R_MICROMIPS_TLS_GD:
642ece8a530Spatrick   case R_MICROMIPS_TLS_LDM:
643bb684c34Spatrick     checkInt(loc, val, 16, rel);
644ece8a530Spatrick     writeShuffleValue<e>(loc, val, 16, 0);
645ece8a530Spatrick     break;
646ece8a530Spatrick   case R_MICROMIPS_CALL16:
647ece8a530Spatrick   case R_MICROMIPS_CALL_LO16:
648ece8a530Spatrick   case R_MICROMIPS_LO16:
649ece8a530Spatrick   case R_MICROMIPS_TLS_DTPREL_LO16:
650ece8a530Spatrick   case R_MICROMIPS_TLS_GOTTPREL:
651ece8a530Spatrick   case R_MICROMIPS_TLS_TPREL_LO16:
652ece8a530Spatrick     writeShuffleValue<e>(loc, val, 16, 0);
653ece8a530Spatrick     break;
654ece8a530Spatrick   case R_MICROMIPS_GPREL7_S2:
655bb684c34Spatrick     checkInt(loc, val, 7, rel);
656ece8a530Spatrick     writeShuffleValue<e>(loc, val, 7, 2);
657ece8a530Spatrick     break;
658ece8a530Spatrick   case R_MIPS_CALL_HI16:
659ece8a530Spatrick   case R_MIPS_GOT_HI16:
660ece8a530Spatrick   case R_MIPS_HI16:
661ece8a530Spatrick   case R_MIPS_PCHI16:
662ece8a530Spatrick   case R_MIPS_TLS_DTPREL_HI16:
663ece8a530Spatrick   case R_MIPS_TLS_TPREL_HI16:
664ece8a530Spatrick     writeValue(loc, val + 0x8000, 16, 16);
665ece8a530Spatrick     break;
666ece8a530Spatrick   case R_MICROMIPS_CALL_HI16:
667ece8a530Spatrick   case R_MICROMIPS_GOT_HI16:
668ece8a530Spatrick   case R_MICROMIPS_HI16:
669ece8a530Spatrick   case R_MICROMIPS_TLS_DTPREL_HI16:
670ece8a530Spatrick   case R_MICROMIPS_TLS_TPREL_HI16:
671ece8a530Spatrick     writeShuffleValue<e>(loc, val + 0x8000, 16, 16);
672ece8a530Spatrick     break;
673ece8a530Spatrick   case R_MIPS_HIGHER:
674ece8a530Spatrick     writeValue(loc, val + 0x80008000, 16, 32);
675ece8a530Spatrick     break;
676ece8a530Spatrick   case R_MIPS_HIGHEST:
677ece8a530Spatrick     writeValue(loc, val + 0x800080008000, 16, 48);
678ece8a530Spatrick     break;
679ece8a530Spatrick   case R_MIPS_JALR:
680ece8a530Spatrick     val -= 4;
681ece8a530Spatrick     // Replace jalr/jr instructions by bal/b if the target
682ece8a530Spatrick     // offset fits into the 18-bit range.
683ece8a530Spatrick     if (isInt<18>(val)) {
684ece8a530Spatrick       switch (read32(loc)) {
685ece8a530Spatrick       case 0x0320f809:  // jalr $25 => bal sym
686ece8a530Spatrick         write32(loc, 0x04110000 | ((val >> 2) & 0xffff));
687ece8a530Spatrick         break;
688ece8a530Spatrick       case 0x03200008:  // jr $25 => b sym
689ece8a530Spatrick         write32(loc, 0x10000000 | ((val >> 2) & 0xffff));
690ece8a530Spatrick         break;
691ece8a530Spatrick       }
692ece8a530Spatrick     }
693ece8a530Spatrick     break;
694ece8a530Spatrick   case R_MICROMIPS_JALR:
695ece8a530Spatrick     // Ignore this optimization relocation for now
696ece8a530Spatrick     break;
697ece8a530Spatrick   case R_MIPS_PC16:
698bb684c34Spatrick     checkAlignment(loc, val, 4, rel);
699bb684c34Spatrick     checkInt(loc, val, 18, rel);
700ece8a530Spatrick     writeValue(loc, val, 16, 2);
701ece8a530Spatrick     break;
702ece8a530Spatrick   case R_MIPS_PC19_S2:
703bb684c34Spatrick     checkAlignment(loc, val, 4, rel);
704bb684c34Spatrick     checkInt(loc, val, 21, rel);
705ece8a530Spatrick     writeValue(loc, val, 19, 2);
706ece8a530Spatrick     break;
707ece8a530Spatrick   case R_MIPS_PC21_S2:
708bb684c34Spatrick     checkAlignment(loc, val, 4, rel);
709bb684c34Spatrick     checkInt(loc, val, 23, rel);
710ece8a530Spatrick     writeValue(loc, val, 21, 2);
711ece8a530Spatrick     break;
712ece8a530Spatrick   case R_MIPS_PC26_S2:
713bb684c34Spatrick     checkAlignment(loc, val, 4, rel);
714bb684c34Spatrick     checkInt(loc, val, 28, rel);
715ece8a530Spatrick     writeValue(loc, val, 26, 2);
716ece8a530Spatrick     break;
717ece8a530Spatrick   case R_MIPS_PC32:
718ece8a530Spatrick     writeValue(loc, val, 32, 0);
719ece8a530Spatrick     break;
720ece8a530Spatrick   case R_MICROMIPS_26_S1:
721ece8a530Spatrick   case R_MICROMIPS_PC26_S1:
722bb684c34Spatrick     checkInt(loc, val, 27, rel);
723ece8a530Spatrick     writeShuffleValue<e>(loc, val, 26, 1);
724ece8a530Spatrick     break;
725ece8a530Spatrick   case R_MICROMIPS_PC7_S1:
726bb684c34Spatrick     checkInt(loc, val, 8, rel);
727ece8a530Spatrick     writeMicroRelocation16<e>(loc, val, 7, 1);
728ece8a530Spatrick     break;
729ece8a530Spatrick   case R_MICROMIPS_PC10_S1:
730bb684c34Spatrick     checkInt(loc, val, 11, rel);
731ece8a530Spatrick     writeMicroRelocation16<e>(loc, val, 10, 1);
732ece8a530Spatrick     break;
733ece8a530Spatrick   case R_MICROMIPS_PC16_S1:
734bb684c34Spatrick     checkInt(loc, val, 17, rel);
735ece8a530Spatrick     writeShuffleValue<e>(loc, val, 16, 1);
736ece8a530Spatrick     break;
737ece8a530Spatrick   case R_MICROMIPS_PC18_S3:
738bb684c34Spatrick     checkInt(loc, val, 21, rel);
739ece8a530Spatrick     writeShuffleValue<e>(loc, val, 18, 3);
740ece8a530Spatrick     break;
741ece8a530Spatrick   case R_MICROMIPS_PC19_S2:
742bb684c34Spatrick     checkInt(loc, val, 21, rel);
743ece8a530Spatrick     writeShuffleValue<e>(loc, val, 19, 2);
744ece8a530Spatrick     break;
745ece8a530Spatrick   case R_MICROMIPS_PC21_S1:
746bb684c34Spatrick     checkInt(loc, val, 22, rel);
747ece8a530Spatrick     writeShuffleValue<e>(loc, val, 21, 1);
748ece8a530Spatrick     break;
749ece8a530Spatrick   case R_MICROMIPS_PC23_S2:
750bb684c34Spatrick     checkInt(loc, val, 25, rel);
751ece8a530Spatrick     writeShuffleValue<e>(loc, val, 23, 2);
752ece8a530Spatrick     break;
753ece8a530Spatrick   default:
754ece8a530Spatrick     llvm_unreachable("unknown relocation");
755ece8a530Spatrick   }
756ece8a530Spatrick }
757ece8a530Spatrick 
usesOnlyLowPageBits(RelType type) const758ece8a530Spatrick template <class ELFT> bool MIPS<ELFT>::usesOnlyLowPageBits(RelType type) const {
759ece8a530Spatrick   return type == R_MIPS_LO16 || type == R_MIPS_GOT_OFST ||
760ece8a530Spatrick          type == R_MICROMIPS_LO16;
761ece8a530Spatrick }
762ece8a530Spatrick 
763ece8a530Spatrick // Return true if the symbol is a PIC function.
isMipsPIC(const Defined * sym)764bb684c34Spatrick template <class ELFT> bool elf::isMipsPIC(const Defined *sym) {
765ece8a530Spatrick   if (!sym->isFunc())
766ece8a530Spatrick     return false;
767ece8a530Spatrick 
768ece8a530Spatrick   if (sym->stOther & STO_MIPS_PIC)
769ece8a530Spatrick     return true;
770ece8a530Spatrick 
771ece8a530Spatrick   if (!sym->section)
772ece8a530Spatrick     return false;
773ece8a530Spatrick 
774ece8a530Spatrick   ObjFile<ELFT> *file =
775ece8a530Spatrick       cast<InputSectionBase>(sym->section)->template getFile<ELFT>();
776ece8a530Spatrick   if (!file)
777ece8a530Spatrick     return false;
778ece8a530Spatrick 
7791cf9926bSpatrick   return file->getObj().getHeader().e_flags & EF_MIPS_PIC;
780ece8a530Spatrick }
781ece8a530Spatrick 
getMipsTargetInfo()782bb684c34Spatrick template <class ELFT> TargetInfo *elf::getMipsTargetInfo() {
783ece8a530Spatrick   static MIPS<ELFT> target;
784ece8a530Spatrick   return &target;
785ece8a530Spatrick }
786ece8a530Spatrick 
787bb684c34Spatrick template TargetInfo *elf::getMipsTargetInfo<ELF32LE>();
788bb684c34Spatrick template TargetInfo *elf::getMipsTargetInfo<ELF32BE>();
789bb684c34Spatrick template TargetInfo *elf::getMipsTargetInfo<ELF64LE>();
790bb684c34Spatrick template TargetInfo *elf::getMipsTargetInfo<ELF64BE>();
791ece8a530Spatrick 
792bb684c34Spatrick template bool elf::isMipsPIC<ELF32LE>(const Defined *);
793bb684c34Spatrick template bool elf::isMipsPIC<ELF32BE>(const Defined *);
794bb684c34Spatrick template bool elf::isMipsPIC<ELF64LE>(const Defined *);
795bb684c34Spatrick template bool elf::isMipsPIC<ELF64BE>(const Defined *);
796